1 ;;; widget-edit.el --- Functions for creating and using widgets.
3 ;; Copyright (C) 1996 Free Software Foundation, Inc.
5 ;; Author: Per Abrahamsen <abraham@dina.kvl.dk>
6 ;; Keywords: extensions
8 ;; X-URL: http://www.dina.kvl.dk/~abraham/custom/
19 (autoload 'pp-to-string "pp")
23 (or (fboundp 'event-point)
24 ;; XEmacs function missing in Emacs.
25 (defun event-point (event)
26 "Return the character position of the given mouse-motion, button-press,
27 or button-release event. If the event did not occur over a window, or did
28 not occur over text, then this returns nil. Otherwise, it returns an index
29 into the buffer visible in the event's window."
30 (posn-point (event-start event))))
35 "Customization support for the Widget Library."
38 (defface widget-documentation-face '((t ()))
39 "Face used for documentation text."
42 (defface widget-button-face '((t (:bold t)))
43 "Face used for widget buttons."
46 (defcustom widget-mouse-face 'highlight
47 "Face used for widget buttons when the mouse is above them."
51 (defface widget-field-face '((((type x)
52 (class grayscale color)
54 (:background "light gray"))
56 (class grayscale color)
58 (:background "dark gray"))
61 "Face used for editable fields."
64 (defcustom widget-menu-max-size 40
65 "Largest number of items allowed in a popup-menu.
66 Larger menus are read through the minibuffer."
69 ;;; Utility functions.
71 ;; These are not really widget specific.
73 (defun widget-plist-member (plist prop)
74 ;; Return non-nil if PLIST has the property PROP.
75 ;; PLIST is a property list, which is a list of the form
76 ;; (PROP1 VALUE1 PROP2 VALUE2 ...). PROP is a symbol.
77 ;; Unlike `plist-get', this allows you to distinguish between a missing
78 ;; property and a property with the value nil.
79 ;; The value is actually the tail of PLIST whose car is PROP.
80 (while (and plist (not (eq (car plist) prop)))
81 (setq plist (cdr (cdr plist))))
84 (defun widget-princ-to-string (object)
85 ;; Return string representation of OBJECT, any Lisp object.
86 ;; No quoting characters are used; no delimiters are printed around
87 ;; the contents of strings.
89 (set-buffer (get-buffer-create " *widget-tmp*"))
91 (let ((standard-output (current-buffer)))
95 (defun widget-clear-undo ()
96 "Clear all undo information."
97 (buffer-disable-undo (current-buffer))
100 (defun widget-choose (title items &optional event)
101 "Choose an item from a list.
103 First argument TITLE is the name of the list.
104 Second argument ITEMS is an alist (NAME . VALUE).
105 Optional third argument EVENT is an input event.
107 The user is asked to choose between each NAME from the items alist,
108 and the VALUE of the chosen element will be returned. If EVENT is a
109 mouse event, and the number of elements in items is less than
110 `widget-menu-max-size', a popup menu will be used, otherwise the
112 (cond ((and (< (length items) widget-menu-max-size)
113 event (fboundp 'x-popup-menu) window-system)
114 ;; We are in Emacs-19, pressed by the mouse
116 (list title (cons "" items))))
117 ((and (< (length items) widget-menu-max-size)
118 event (fboundp 'popup-menu) window-system)
119 ;; We are in XEmacs, pressed by the mouse
120 (let ((val (get-popup-menu-response
125 (vector (car x) (list (car x)) t)))
128 (listp (event-object val))
129 (stringp (car-safe (event-object val)))
130 (car (event-object val))))
131 (cdr (assoc val items))))
133 (cdr (assoc (completing-read (concat title ": ")
137 ;;; Widget text specifications.
139 ;; These functions are for specifying text properties.
141 (defun widget-specify-none (from to)
142 ;; Clear all text properties between FROM and TO.
143 (set-text-properties from to nil))
145 (defun widget-specify-text (from to)
146 ;; Default properties.
147 (add-text-properties from to (list 'read-only t
149 'rear-nonsticky nil)))
151 (defun widget-specify-field (widget from to)
152 ;; Specify editable button for WIDGET between FROM and TO.
153 (widget-specify-field-update widget from to)
154 ;; Make it possible to edit both end of the field.
155 (put-text-property (- from 2) from 'intangible t)
156 (add-text-properties (1- from) from (list 'rear-nonsticky t
159 (add-text-properties to (1+ to) (list 'front-sticky nil
162 (defun widget-specify-field-update (widget from to)
163 ;; Specify editable button for WIDGET between FROM and TO.
164 (let ((map (widget-get widget :keymap))
165 (face (or (widget-get widget :value-face)
166 'widget-field-face)))
167 (set-text-properties from to (list 'field widget
173 (defun widget-specify-button (widget from to)
174 ;; Specify button for WIDGET between FROM and TO.
175 (let ((face (widget-apply widget :button-face-get)))
176 (add-text-properties from to (list 'button widget
177 'mouse-face widget-mouse-face
180 (defun widget-specify-doc (widget from to)
181 ;; Specify documentation for WIDGET between FROM and TO.
182 (add-text-properties from to (list 'widget-doc widget
183 'face 'widget-documentation-face)))
185 (defmacro widget-specify-insert (&rest form)
186 ;; Execute FORM without inheriting any text properties.
188 (let ((inhibit-read-only t)
190 after-change-functions)
192 (narrow-to-region (- (point) 2) (point))
193 (widget-specify-none (point-min) (point-max))
194 (goto-char (1+ (point-min)))
195 (setq result (progn ,@form))
196 (delete-region (point-min) (1+ (point-min)))
197 (delete-region (1- (point-max)) (point-max))
198 (goto-char (point-max))
201 ;;; Widget Properties.
203 (defun widget-put (widget property value)
204 "In WIDGET set PROPERTY to VALUE.
205 The value can later be retrived with `widget-get'."
206 (setcdr widget (plist-put (cdr widget) property value)))
208 (defun widget-get (widget property)
209 "In WIDGET, get the value of PROPERTY.
210 The value could either be specified when the widget was created, or
211 later with `widget-put'."
212 (cond ((widget-plist-member (cdr widget) property)
213 (plist-get (cdr widget) property))
215 (widget-get (get (car widget) 'widget-type) property))
218 (defun widget-member (widget property)
219 "Non-nil iff there is a definition in WIDGET for PROPERTY."
220 (cond ((widget-plist-member (cdr widget) property)
223 (widget-member (get (car widget) 'widget-type) property))
226 (defun widget-apply (widget property &rest args)
227 "Apply the value of WIDGET's PROPERTY to the widget itself.
228 ARGS are passed as extra argments to the function."
229 (apply (widget-get widget property) widget args))
231 (defun widget-value (widget)
232 "Extract the current value of WIDGET."
234 :value-to-external (widget-apply widget :value-get)))
236 (defun widget-value-set (widget value)
237 "Set the current value of WIDGET to VALUE."
239 :value-set (widget-apply widget
240 :value-to-internal value)))
242 (defun widget-match-inline (widget vals)
243 ;; In WIDGET, match the start of VALS.
244 (cond ((widget-get widget :inline)
245 (widget-apply widget :match-inline vals))
247 (widget-apply widget :match (car vals)))
248 (cons (list (car vals)) (cdr vals)))
251 ;;; Creating Widgets.
254 (defun widget-create (type &rest args)
255 "Create widget of TYPE.
256 The optional ARGS are additional keyword arguments."
257 (let ((widget (apply 'widget-convert type args)))
258 (widget-apply widget :create)
261 (defun widget-create-child-and-convert (parent type &rest args)
262 "As part of the widget PARENT, create a child widget TYPE.
263 The child is converted, using the keyword arguments ARGS."
264 (let ((widget (apply 'widget-convert type args)))
265 (widget-put widget :parent parent)
266 (unless (widget-get widget :indent)
267 (widget-put widget :indent (+ (or (widget-get parent :indent) 0)
268 (or (widget-get widget :extra-offset) 0)
269 (widget-get parent :offset))))
270 (widget-apply widget :create)
273 (defun widget-create-child (parent type)
274 "Create widget of TYPE. "
275 (let ((widget (copy-list type)))
276 (widget-put widget :parent parent)
277 (unless (widget-get widget :indent)
278 (widget-put widget :indent (+ (or (widget-get parent :indent) 0)
279 (widget-get parent :offset))))
280 (widget-apply widget :create)
284 (defun widget-delete (widget)
286 (widget-apply widget :delete))
288 (defun widget-convert (type &rest args)
289 "Convert TYPE to a widget without inserting it in the buffer.
290 The optional ARGS are additional keyword arguments."
291 ;; Don't touch the type.
292 (let* ((widget (if (symbolp type)
297 ;; First set the :args keyword.
298 (while (cdr current) ;Look in the type.
299 (let ((next (car (cdr current))))
300 (if (and (symbolp next) (eq (aref (symbol-name next) 0) ?:))
301 (setq current (cdr (cdr current)))
302 (setcdr current (list :args (cdr current)))
303 (setq current nil))))
304 (while args ;Look in the args.
305 (let ((next (nth 0 args)))
306 (if (and (symbolp next) (eq (aref (symbol-name next) 0) ?:))
307 (setq args (nthcdr 2 args))
308 (widget-put widget :args args)
310 ;; Then Convert the widget.
313 (let ((convert-widget (plist-get (cdr type) :convert-widget)))
315 (setq widget (funcall convert-widget widget))))
316 (setq type (get (car type) 'widget-type)))
317 ;; Finally set the keyword args.
319 (let ((next (nth 0 keys)))
320 (if (and (symbolp next) (eq (aref (symbol-name next) 0) ?:))
322 (widget-put widget next (nth 1 keys))
323 (setq keys (nthcdr 2 keys)))
325 ;; Convert the :value to internal format.
326 (if (widget-member widget :value)
327 (let ((value (widget-get widget :value)))
329 :value (widget-apply widget :value-to-internal value))))
330 ;; Return the newly create widget.
333 (defun widget-insert (&rest args)
334 "Call `insert' with ARGS and make the text read only."
335 (let ((inhibit-read-only t)
336 after-change-functions
339 (widget-specify-text from (point))))
341 ;;; Keymap and Comands.
343 (defvar widget-keymap nil
344 "Keymap containing useful binding for buffers containing widgets.
345 Recommended as a parent keymap for modes using widgets.")
349 (setq widget-keymap (make-sparse-keymap))
350 (set-keymap-parent widget-keymap global-map)
351 (define-key widget-keymap "\t" 'widget-forward)
352 (define-key widget-keymap "\M-\t" 'widget-backward)
353 (define-key widget-keymap [(shift tab)] 'widget-backward)
354 (if (string-match "XEmacs" (emacs-version))
355 (define-key widget-keymap [button2] 'widget-button-click)
356 (define-key widget-keymap [menu-bar] 'nil)
357 (define-key widget-keymap [mouse-2] 'widget-button-click))
358 (define-key widget-keymap "\C-m" 'widget-button-press))
360 (defvar widget-global-map global-map
361 "Keymap used for events the widget does not handle themselves.")
362 (make-variable-buffer-local 'widget-global-map)
364 (defun widget-button-click (event)
365 "Activate button below mouse pointer."
367 (widget-button-press (event-point event) event))
369 (defun widget-button-press (pos &optional event)
370 "Activate button at POS."
372 (let* ((button (get-text-property pos 'button)))
374 (widget-apply button :action event)
376 (lookup-key widget-global-map (this-command-keys))))))
378 (defun widget-forward (arg)
379 "Move point to the next field or button.
380 With optional ARG, move across that many fields."
384 (let ((next (cond ((get-text-property (point) 'button)
385 (next-single-property-change (point) 'button))
386 ((get-text-property (point) 'field)
387 (next-single-property-change (point) 'field))
390 (if (null next) ; Widget extends to end. of buffer
391 (setq next (point-min)))
392 (let ((button (next-single-property-change next 'button))
393 (field (next-single-property-change next 'field)))
394 (cond ((or (get-text-property next 'button)
395 (get-text-property next 'field))
398 (goto-char (min button field)))
399 (button (goto-char button))
400 (field (goto-char field))
402 (let ((button (next-single-property-change (point-min) 'button))
403 (field (next-single-property-change (point-min) 'field)))
404 (cond ((and button field) (goto-char (min button field)))
405 (button (goto-char button))
406 (field (goto-char field))
408 (error "No buttons or fields found")))))))))
410 (if (= (point-min) (point))
413 (let ((previous (cond ((get-text-property (1- (point)) 'button)
414 (previous-single-property-change (point) 'button))
415 ((get-text-property (1- (point)) 'field)
416 (previous-single-property-change (point) 'field))
419 (if (null previous) ; Widget extends to beg. of buffer
420 (setq previous (point-max)))
421 (let ((button (previous-single-property-change previous 'button))
422 (field (previous-single-property-change previous 'field)))
423 (cond ((and button field)
424 (goto-char (max button field)))
425 (button (goto-char button))
426 (field (goto-char field))
428 (let ((button (previous-single-property-change
429 (point-max) 'button))
430 (field (previous-single-property-change
431 (point-max) 'field)))
432 (cond ((and button field) (goto-char (max button field)))
433 (button (goto-char button))
434 (field (goto-char field))
436 (error "No buttons or fields found"))))))))
437 (let ((button (previous-single-property-change (point) 'button))
438 (field (previous-single-property-change (point) 'field)))
439 (cond ((and button field)
440 (goto-char (max button field)))
441 (button (goto-char button))
442 (field (goto-char field)))))
443 (let ((help-echo (or (get-text-property (point) 'button)
444 (get-text-property (point) 'field))))
445 (if (and help-echo (setq help-echo (widget-get help-echo :help-echo)))
446 (message "%s" help-echo))))
448 (defun widget-backward (arg)
449 "Move point to the previous field or button.
450 With optional ARG, move across that many fields."
452 (widget-forward (- arg)))
454 ;;; Setting up the buffer.
456 (defvar widget-field-new nil)
457 ;; List of all newly created editable fields in the buffer.
458 (make-variable-buffer-local 'widget-field-new)
460 (defvar widget-field-list nil)
461 ;; List of all editable fields in the buffer.
462 (make-variable-buffer-local 'widget-field-list)
464 (defun widget-setup ()
465 "Setup current buffer so editing string widgets works."
466 (let ((inhibit-read-only t)
467 (after-change-functions nil)
469 (while widget-field-new
470 (setq field (car widget-field-new)
471 widget-field-new (cdr widget-field-new)
472 widget-field-list (cons field widget-field-list))
473 (let ((from (widget-get field :value-from))
474 (to (widget-get field :value-to)))
475 (widget-specify-field field from to)
476 (move-marker from (1- from))
477 (move-marker to (1+ to)))))
479 ;; We need to maintain text properties and size of the editing fields.
480 (make-local-variable 'after-change-functions)
481 (if widget-field-list
482 (setq after-change-functions '(widget-after-change))
483 (setq after-change-functions nil)))
485 (defvar widget-field-last nil)
486 ;; Last field containing point.
487 (make-variable-buffer-local 'widget-field-last)
489 (defvar widget-field-was nil)
490 ;; The widget data before the change.
491 (make-variable-buffer-local 'widget-field-was)
493 (defun widget-field-find (pos)
494 ;; Find widget whose editing field is located at POS.
495 ;; Return nil if POS is not inside and editing field.
497 ;; This is only used in `widget-field-modified', since ordinarily
498 ;; you would just test the field property.
499 (let ((fields widget-field-list)
502 (setq field (car fields)
504 (let ((from (widget-get field :value-from))
505 (to (widget-get field :value-to)))
506 (if (and from to (< from pos) (> to pos))
511 (defun widget-after-change (from to old)
512 ;; Adjust field size and text properties.
514 (let ((field (widget-field-find from))
515 (inhibit-read-only t))
517 ((not (eq field (widget-field-find to)))
519 (message "Error: `widget-after-change' called on two fields"))
521 (let ((size (widget-get field :size)))
523 (let ((begin (1+ (widget-get field :value-from)))
524 (end (1- (widget-get field :value-to))))
525 (widget-specify-field-update field begin end)
526 (cond ((< (- end begin) size)
530 (insert-char ?\ (- (+ begin size) end))))
531 ((> (- end begin) size)
532 ;; Field too large and
533 (if (or (< (point) (+ begin size))
535 ;; Point is outside extra space.
536 (setq begin (+ begin size))
537 ;; Point is within the extra space.
538 (setq begin (point)))
541 (while (and (eq (preceding-char) ?\ )
543 (delete-backward-char 1))))))
544 (widget-specify-field-update field from to)))
545 (widget-apply field :notify field))))
548 ;;; The `default' Widget.
550 (define-widget 'default nil
551 "Basic widget other widgets are derived from."
552 :value-to-internal (lambda (widget value) value)
553 :value-to-external (lambda (widget value) value)
554 :create 'widget-default-create
557 :format-handler 'widget-default-format-handler
558 :button-face-get 'widget-default-button-face-get
559 :delete 'widget-default-delete
560 :value-set 'widget-default-value-set
561 :value-inline 'widget-default-value-inline
562 :menu-tag-get 'widget-default-menu-tag-get
563 :validate (lambda (widget) nil)
564 :action 'widget-default-action
565 :notify 'widget-default-notify)
567 (defun widget-default-create (widget)
568 "Create WIDGET at point in the current buffer."
569 (widget-specify-insert
571 (tag (widget-get widget :tag))
572 (doc (widget-get widget :doc))
573 button-begin button-end
576 (insert (widget-get widget :format))
578 ;; Parse % escapes in format.
579 (while (re-search-forward "%\\(.\\)" nil t)
580 (let ((escape (aref (match-string 1) 0)))
581 (replace-match "" t t)
582 (cond ((eq escape ?%)
585 (setq button-begin (point)))
587 (setq button-end (point)))
589 (when (widget-get widget :indent)
591 (insert-char ? (widget-get widget :indent))))
595 (let ((standard-output (current-buffer)))
596 (princ (widget-get widget :value)))))
599 (setq doc-begin (point))
601 (while (eq (preceding-char) ?\n)
602 (delete-backward-char 1))
604 (setq doc-end (point))))
606 (if (and button-begin (not button-end))
607 (widget-apply widget :value-create)
608 (setq value-pos (point))))
610 (widget-apply widget :format-handler escape)))))
611 ;; Specify button and doc, and insert value.
612 (and button-begin button-end
613 (widget-specify-button widget button-begin button-end))
614 (and doc-begin doc-end
615 (widget-specify-doc widget doc-begin doc-end))
617 (goto-char value-pos)
618 (widget-apply widget :value-create)))
619 (let ((from (copy-marker (point-min)))
620 (to (copy-marker (point-max))))
621 (widget-specify-text from to)
622 (set-marker-insertion-type from t)
623 (set-marker-insertion-type to nil)
624 (widget-put widget :from from)
625 (widget-put widget :to to))))
627 (defun widget-default-format-handler (widget escape)
628 ;; By default unknown escapes are errors.
629 (error "Unknown escape `%c'" escape))
631 (defun widget-default-button-face-get (widget)
632 ;; Use :button-face or widget-button-face
633 (or (widget-get widget :button-face) 'widget-button-face))
635 (defun widget-default-delete (widget)
636 ;; Remove widget from the buffer.
637 (let ((from (widget-get widget :from))
638 (to (widget-get widget :to))
639 (inhibit-read-only t)
640 after-change-functions)
641 (widget-apply widget :value-delete)
642 (delete-region from to)
643 (set-marker from nil)
644 (set-marker to nil)))
646 (defun widget-default-value-set (widget value)
647 ;; Recreate widget with new value.
649 (goto-char (widget-get widget :from))
650 (widget-apply widget :delete)
651 (widget-put widget :value value)
652 (widget-apply widget :create)))
654 (defun widget-default-value-inline (widget)
655 ;; Wrap value in a list unless it is inline.
656 (if (widget-get widget :inline)
657 (widget-value widget)
658 (list (widget-value widget))))
660 (defun widget-default-menu-tag-get (widget)
661 ;; Use tag or value for menus.
662 (or (widget-get widget :menu-tag)
663 (widget-get widget :tag)
664 (widget-princ-to-string (widget-get widget :value))))
666 (defun widget-default-action (widget &optional event)
667 ;; Notify the parent when a widget change
668 (let ((parent (widget-get widget :parent)))
670 (widget-apply parent :notify widget event))))
672 (defun widget-default-notify (widget child &optional event)
673 ;; Pass notification to parent.
674 (widget-default-action widget event))
676 ;;; The `item' Widget.
678 (define-widget 'item 'default
679 "Constant items for inclusion in other widgets."
680 :convert-widget 'widget-item-convert-widget
681 :value-create 'widget-item-value-create
682 :value-delete 'ignore
683 :value-get 'widget-item-value-get
684 :match 'widget-item-match
685 :match-inline 'widget-item-match-inline
686 :action 'widget-item-action
689 (defun widget-item-convert-widget (widget)
690 ;; Initialize :value and :tag from :args in WIDGET.
691 (let ((args (widget-get widget :args)))
693 (widget-put widget :value (widget-apply widget
694 :value-to-internal (car args)))
695 (widget-put widget :args nil)))
698 (defun widget-item-value-create (widget)
699 ;; Insert the printed representation of the value.
700 (let ((standard-output (current-buffer)))
701 (princ (widget-get widget :value))))
703 (defun widget-item-match (widget value)
704 ;; Match if the value is the same.
705 (equal (widget-get widget :value) value))
707 (defun widget-item-match-inline (widget values)
708 ;; Match if the value is the same.
709 (let ((value (widget-get widget :value)))
711 (<= (length value) (length values))
712 (let ((head (subseq values 0 (length value))))
713 (and (equal head value)
714 (cons head (subseq values (length value))))))))
716 (defun widget-item-action (widget &optional event)
717 ;; Just notify itself.
718 (widget-apply widget :notify widget event))
720 (defun widget-item-value-get (widget)
722 (widget-get widget :value))
724 ;;; The `push-button' Widget.
726 (define-widget 'push-button 'item
728 :format "%[[%t]%]%d")
730 ;;; The `link' Widget.
732 (define-widget 'link 'item
734 :format "%[_%t_%]%d")
736 ;;; The `editable-field' Widget.
738 (define-widget 'editable-field 'default
739 "An editable text field."
740 :convert-widget 'widget-item-convert-widget
743 :action 'widget-field-action
744 :value-create 'widget-field-value-create
745 :value-delete 'widget-field-value-delete
746 :value-get 'widget-field-value-get
747 :match 'widget-field-match)
749 ;; History of field minibuffer edits.
750 (defvar widget-field-history nil)
752 (defun widget-field-action (widget &optional event)
753 ;; Edit the value in the minibuffer.
754 (let ((tag (widget-apply widget :menu-tag-get))
755 (invalid (widget-apply widget :validate)))
757 (error (widget-get invalid :error)))
758 (widget-value-set widget (read-string (concat tag ": ")
759 (widget-get widget :value)
760 'widget-field-history))))
762 (defun widget-field-value-create (widget)
763 ;; Create an editable text field.
765 (let ((size (widget-get widget :size))
766 (value (widget-get widget :value))
771 (if (< (length value) size)
772 (insert-char ?\ (- size (length value)))))
773 (unless (memq widget widget-field-list)
774 (setq widget-field-new (cons widget widget-field-new)))
775 (widget-put widget :value-from (copy-marker from))
776 (set-marker-insertion-type (widget-get widget :value-from) t)
777 (widget-put widget :value-to (copy-marker (point)))
778 (set-marker-insertion-type (widget-get widget :value-to) nil)
783 (defun widget-field-value-delete (widget)
784 ;; Remove the widget from the list of active editing fields.
785 (setq widget-field-list (delq widget widget-field-list))
786 (set-marker (widget-get widget :value-from) nil)
787 (set-marker (widget-get widget :value-to) nil))
789 (defun widget-field-value-get (widget)
790 ;; Return current text in editing field.
791 (let ((from (widget-get widget :value-from))
792 (to (widget-get widget :value-to))
793 (old (current-buffer)))
796 (set-buffer (marker-buffer from))
799 (while (and (> to from)
800 (eq (char-after (1- to)) ?\ ))
802 (prog1 (buffer-substring-no-properties from to)
804 (widget-get widget :value))))
806 (defun widget-field-match (widget value)
810 ;;; The `text' Widget.
812 (define-widget 'text 'field
813 "A multiline text area.")
815 ;;; The `menu-choice' Widget.
817 (define-widget 'menu-choice 'default
819 :convert-widget 'widget-choice-convert-widget
822 :void '(item :format "invalid (%t)\n")
823 :value-create 'widget-choice-value-create
824 :value-delete 'widget-radio-value-delete
825 :value-get 'widget-choice-value-get
826 :value-inline 'widget-choice-value-inline
827 :action 'widget-choice-action
828 :error "Make a choice"
829 :validate 'widget-choice-validate
830 :match 'widget-choice-match
831 :match-inline 'widget-choice-match-inline)
833 (defun widget-choice-convert-widget (widget)
834 ;; Expand type args into widget objects.
835 (widget-put widget :args (mapcar 'widget-convert (widget-get widget :args)))
838 (defun widget-choice-value-create (widget)
839 ;; Insert the first choice that matches the value.
840 (let ((value (widget-get widget :value))
841 (args (widget-get widget :args))
844 (setq current (car args)
846 (when (widget-apply current :match value)
847 (widget-put widget :children (list (widget-create-child-and-convert
848 widget current :value value)))
849 (widget-put widget :choice current)
853 (let ((void (widget-get widget :void)))
854 (widget-put widget :children (list (widget-create-child-and-convert
855 widget void :value value)))
856 (widget-put widget :choice void)))))
858 (defun widget-choice-value-get (widget)
859 ;; Get value of the child widget.
860 (widget-value (car (widget-get widget :children))))
862 (defun widget-choice-value-inline (widget)
863 ;; Get value of the child widget.
864 (widget-apply (car (widget-get widget :children)) :value-inline))
866 (defun widget-choice-action (widget &optional event)
868 (let ((args (widget-get widget :args))
869 (old (widget-get widget :choice))
870 (tag (widget-apply widget :menu-tag-get))
872 ;; Remember old value.
873 (if (and old (widget-apply widget :validate))
874 (let* ((external (widget-value widget))
875 (internal (widget-apply old :value-to-internal external)))
876 (widget-put old :value internal)))
879 (cond ((= (length args) 0)
883 ((and (= (length args) 2)
885 (if (eq old (nth 0 args))
890 (setq current (car args)
893 (cons (cons (widget-apply current :menu-tag-get)
896 (widget-choose tag (reverse choices) event))))
898 (widget-value-set widget
899 (widget-apply current :value-to-external
900 (widget-get current :value)))
903 (widget-apply widget :notify widget event)
906 (defun widget-choice-validate (widget)
907 ;; Valid if we have made a valid choice.
908 (let ((void (widget-get widget :void))
909 (choice (widget-get widget :choice))
910 (child (car (widget-get widget :children))))
913 (widget-apply child :validate))))
915 (defun widget-choice-match (widget value)
916 ;; Matches if one of the choices matches.
917 (let ((args (widget-get widget :args))
919 (while (and args (not found))
920 (setq current (car args)
922 found (widget-apply current :match value)))
925 (defun widget-choice-match-inline (widget values)
926 ;; Matches if one of the choices matches.
927 (let ((args (widget-get widget :args))
929 (while (and args (null found))
930 (setq current (car args)
932 found (widget-match-inline current values)))
935 ;;; The `toggle' Widget.
937 (define-widget 'toggle 'menu-choice
938 "Toggle between two states."
939 :convert-widget 'widget-toggle-convert-widget
944 (defun widget-toggle-convert-widget (widget)
945 ;; Create the types representing the `on' and `off' states.
946 (let ((on-type (widget-get widget :on-type))
947 (off-type (widget-get widget :off-type)))
952 :match (lambda (widget value) value)
953 :tag (widget-get widget :on))))
956 (list 'choice-item :value nil :tag (widget-get widget :off))))
957 (widget-put widget :args (list on-type off-type)))
960 ;;; The `checkbox' Widget.
962 (define-widget 'checkbox 'toggle
964 :convert-widget 'widget-item-convert-widget
965 :on-type '(choice-item :format "%[[X]%]" t)
966 :off-type '(choice-item :format "%[[ ]%]" nil))
968 ;;; The `checklist' Widget.
970 (define-widget 'checklist 'default
971 "A multiple choice widget."
972 :convert-widget 'widget-choice-convert-widget
975 :entry-format "%b %v"
976 :menu-tag "checklist"
978 :value-create 'widget-checklist-value-create
979 :value-delete 'widget-radio-value-delete
980 :value-get 'widget-checklist-value-get
981 :validate 'widget-checklist-validate
982 :match 'widget-checklist-match
983 :match-inline 'widget-checklist-match-inline)
985 (defun widget-checklist-value-create (widget)
987 (let ((alist (widget-checklist-match-find widget (widget-get widget :value)))
988 (args (widget-get widget :args)))
990 (widget-checklist-add-item widget (car args) (assq (car args) alist))
991 (setq args (cdr args)))
992 (widget-put widget :children (nreverse (widget-get widget :children)))))
994 (defun widget-checklist-add-item (widget type chosen)
995 ;; Create checklist item in WIDGET of type TYPE.
996 ;; If the item is checked, CHOSEN is a cons whose cdr is the value.
997 (and (eq (preceding-char) ?\n)
998 (widget-get widget :indent)
999 (insert-char ? (widget-get widget :indent)))
1000 (widget-specify-insert
1001 (let* ((children (widget-get widget :children))
1002 (buttons (widget-get widget :buttons))
1005 (insert (widget-get widget :entry-format))
1007 ;; Parse % escapes in format.
1008 (while (re-search-forward "%\\([bv%]\\)" nil t)
1009 (let ((escape (aref (match-string 1) 0)))
1010 (replace-match "" t t)
1011 (cond ((eq escape ?%)
1014 (setq button (widget-create-child-and-convert
1015 widget 'checkbox :value (not (null chosen)))))
1019 (widget-create-child widget type))
1020 ((widget-get type :inline)
1021 (widget-create-child-and-convert
1022 widget type :value (cdr chosen)))
1024 (widget-create-child-and-convert
1025 widget type :value (car (cdr chosen)))))))
1027 (error "Unknown escape `%c'" escape)))))
1028 ;; Update properties.
1029 (and button child (widget-put child :button button))
1030 (and button (widget-put widget :buttons (cons button buttons)))
1031 (and child (widget-put widget :children (cons child children))))))
1033 (defun widget-checklist-match (widget values)
1034 ;; All values must match a type in the checklist.
1036 (null (cdr (widget-checklist-match-inline widget values)))))
1038 (defun widget-checklist-match-inline (widget values)
1039 ;; Find the values which match a type in the checklist.
1040 (let ((greedy (widget-get widget :greedy))
1041 (args (copy-list (widget-get widget :args)))
1044 (let ((answer (widget-checklist-match-up args values)))
1046 (let ((vals (widget-match-inline answer values)))
1047 (setq found (append found (car vals))
1049 args (delq answer args))))
1051 (setq rest (append rest (list (car values)))
1052 values (cdr values)))
1054 (setq rest (append rest values)
1058 (defun widget-checklist-match-find (widget vals)
1059 ;; Find the vals which match a type in the checklist.
1060 ;; Return an alist of (TYPE MATCH).
1061 (let ((greedy (widget-get widget :greedy))
1062 (args (copy-list (widget-get widget :args)))
1065 (let ((answer (widget-checklist-match-up args vals)))
1067 (let ((match (widget-match-inline answer vals)))
1068 (setq found (cons (cons answer (car match)) found)
1070 args (delq answer args))))
1072 (setq vals (cdr vals)))
1077 (defun widget-checklist-match-up (args vals)
1078 ;; Rerturn the first type from ARGS that matches VALS.
1079 (let (current found)
1080 (while (and args (null found))
1081 (setq current (car args)
1083 found (widget-match-inline current vals)))
1084 (and found current)))
1086 (defun widget-checklist-value-get (widget)
1087 ;; The values of all selected items.
1088 (let ((children (widget-get widget :children))
1091 (setq child (car children)
1092 children (cdr children))
1093 (if (widget-value (widget-get child :button))
1094 (setq result (append result (widget-apply child :value-inline)))))
1097 (defun widget-checklist-validate (widget)
1098 ;; Ticked chilren must be valid.
1099 (let ((children (widget-get widget :children))
1101 (while (and children (not found))
1102 (setq child (car children)
1103 children (cdr children)
1104 button (widget-get child :button)
1105 found (and (widget-value button)
1106 (widget-apply child :validate))))
1109 ;;; The `option' Widget
1111 (define-widget 'option 'checklist
1112 "An widget with an optional item."
1115 ;;; The `choice-item' Widget.
1117 (define-widget 'choice-item 'item
1118 "Button items that delegate action events to their parents."
1119 :action 'widget-choice-item-action
1120 :format "%[%t%] \n")
1122 (defun widget-choice-item-action (widget &optional event)
1123 ;; Tell parent what happened.
1124 (widget-apply (widget-get widget :parent) :action event))
1126 ;;; The `radio-button' Widget.
1128 (define-widget 'radio-button 'toggle
1129 "A radio button for use in the `radio' widget."
1130 :notify 'widget-radio-button-notify
1131 :on-type '(choice-item :format "%[(*)%]" t)
1132 :off-type '(choice-item :format "%[( )%]" nil))
1134 (defun widget-radio-button-notify (widget child &optional event)
1135 ;; Notify the parent.
1136 (widget-apply (widget-get widget :parent) :action widget event))
1138 ;;; The `radio-button-choice' Widget.
1140 (define-widget 'radio-button-choice 'default
1141 "Select one of multiple options."
1142 :convert-widget 'widget-choice-convert-widget
1145 :entry-format "%b %v"
1147 :value-create 'widget-radio-value-create
1148 :value-delete 'widget-radio-value-delete
1149 :value-get 'widget-radio-value-get
1150 :value-inline 'widget-radio-value-inline
1151 :value-set 'widget-radio-value-set
1152 :error "You must push one of the buttons"
1153 :validate 'widget-radio-validate
1154 :match 'widget-choice-match
1155 :match-inline 'widget-choice-match-inline
1156 :action 'widget-radio-action)
1158 (defun widget-radio-value-create (widget)
1159 ;; Insert all values
1160 (let ((args (widget-get widget :args))
1163 (setq arg (car args)
1165 (widget-radio-add-item widget arg))))
1167 (defun widget-radio-add-item (widget type)
1168 "Add to radio widget WIDGET a new radio button item of type TYPE."
1169 ;; (setq type (widget-convert type))
1170 (and (eq (preceding-char) ?\n)
1171 (widget-get widget :indent)
1172 (insert-char ? (widget-get widget :indent)))
1173 (widget-specify-insert
1174 (let* ((value (widget-get widget :value))
1175 (children (widget-get widget :children))
1176 (buttons (widget-get widget :buttons))
1178 (chosen (and (null (widget-get widget :choice))
1179 (widget-apply type :match value)))
1181 (insert (widget-get widget :entry-format))
1183 ;; Parse % escapes in format.
1184 (while (re-search-forward "%\\([bv%]\\)" nil t)
1185 (let ((escape (aref (match-string 1) 0)))
1186 (replace-match "" t t)
1187 (cond ((eq escape ?%)
1190 (setq button (widget-create-child-and-convert
1191 widget 'radio-button
1192 :value (not (null chosen)))))
1194 (setq child (if chosen
1195 (widget-create-child-and-convert
1196 widget type :value value)
1197 (widget-create-child widget type))))
1199 (error "Unknown escape `%c'" escape)))))
1200 ;; Update properties.
1202 (widget-put widget :choice type))
1204 (widget-put child :button button)
1205 (widget-put widget :buttons (nconc buttons (list button))))
1207 (widget-put widget :children (nconc children (list child))))
1210 (defun widget-radio-value-delete (widget)
1211 ;; Delete the child widgets.
1212 (mapcar 'widget-delete (widget-get widget :children))
1213 (widget-put widget :children nil)
1214 (mapcar 'widget-delete (widget-get widget :buttons))
1215 (widget-put widget :buttons nil))
1217 (defun widget-radio-value-get (widget)
1218 ;; Get value of the child widget.
1219 (let ((chosen (widget-radio-chosen widget)))
1220 (and chosen (widget-value chosen))))
1222 (defun widget-radio-chosen (widget)
1223 "Return the widget representing the chosen radio button."
1224 (let ((children (widget-get widget :children))
1227 (setq current (car children)
1228 children (cdr children))
1229 (let* ((button (widget-get current :button))
1230 (value (widget-apply button :value-get)))
1236 (defun widget-radio-value-inline (widget)
1237 ;; Get value of the child widget.
1238 (let ((children (widget-get widget :children))
1241 (setq current (car children)
1242 children (cdr children))
1243 (let* ((button (widget-get current :button))
1244 (value (widget-apply button :value-get)))
1246 (setq found (widget-apply current :value-inline)
1250 (defun widget-radio-value-set (widget value)
1251 ;; We can't just delete and recreate a radio widget, since children
1252 ;; can be added after the original creation and won't be recreated
1254 (let ((children (widget-get widget :children))
1257 (setq current (car children)
1258 children (cdr children))
1259 (let* ((button (widget-get current :button))
1260 (match (and (not found)
1261 (widget-apply current :match value))))
1262 (widget-value-set button match)
1264 (widget-value-set current value))
1265 (setq found (or found match))))))
1267 (defun widget-radio-validate (widget)
1268 ;; Valid if we have made a valid choice.
1269 (let ((children (widget-get widget :children))
1270 current found button)
1271 (while (and children (not found))
1272 (setq current (car children)
1273 children (cdr children)
1274 button (widget-get current :button)
1275 found (widget-apply button :value-get)))
1277 (widget-apply current :validate)
1280 (defun widget-radio-action (widget child event)
1281 ;; Check if a radio button was pressed.
1282 (let ((children (widget-get widget :children))
1283 (buttons (widget-get widget :buttons))
1285 (when (memq child buttons)
1287 (setq current (car children)
1288 children (cdr children))
1289 (let* ((button (widget-get current :button)))
1290 (cond ((eq child button)
1291 (widget-value-set button t))
1292 ((widget-value button)
1293 (widget-value-set button nil)))))))
1294 ;; Pass notification to parent.
1295 (widget-apply widget :notify child event))
1297 ;;; The `insert-button' Widget.
1299 (define-widget 'insert-button 'push-button
1300 "An insert button for the `editable-list' widget."
1302 :action 'widget-insert-button-action)
1304 (defun widget-insert-button-action (widget &optional event)
1305 ;; Ask the parent to insert a new item.
1306 (widget-apply (widget-get widget :parent)
1307 :insert-before (widget-get widget :widget)))
1309 ;;; The `delete-button' Widget.
1311 (define-widget 'delete-button 'push-button
1312 "A delete button for the `editable-list' widget."
1314 :action 'widget-delete-button-action)
1316 (defun widget-delete-button-action (widget &optional event)
1317 ;; Ask the parent to insert a new item.
1318 (widget-apply (widget-get widget :parent)
1319 :delete-at (widget-get widget :widget)))
1321 ;;; The `editable-list' Widget.
1323 (define-widget 'editable-list 'default
1324 "A variable list of widgets of the same type."
1325 :convert-widget 'widget-choice-convert-widget
1328 :format-handler 'widget-editable-list-format-handler
1329 :entry-format "%i %d %v"
1330 :menu-tag "editable-list"
1331 :value-create 'widget-editable-list-value-create
1332 :value-delete 'widget-radio-value-delete
1333 :value-get 'widget-editable-list-value-get
1334 :validate 'widget-editable-list-validate
1335 :match 'widget-editable-list-match
1336 :match-inline 'widget-editable-list-match-inline
1337 :insert-before 'widget-editable-list-insert-before
1338 :delete-at 'widget-editable-list-delete-at)
1340 (defun widget-editable-list-format-handler (widget escape)
1341 ;; We recognize the insert button.
1342 (cond ((eq escape ?i)
1343 (and (widget-get widget :indent)
1344 (insert-char ? (widget-get widget :indent)))
1345 (widget-create-child-and-convert widget 'insert-button))
1347 (widget-default-format-handler widget escape))))
1349 ;(defun widget-editable-list-format-handler (widget escape)
1350 ; ;; We recognize the insert button.
1351 ; (cond ((eq escape ?i)
1354 ; (let* ((from (point))
1355 ; (button (widget-create-child-and-convert
1356 ; widget 'insert-button)))
1357 ; (widget-specify-button button from (point)))
1360 ; (widget-default-format-handler widget escape))))
1362 (defun widget-editable-list-value-create (widget)
1363 ;; Insert all values
1364 (let* ((value (widget-get widget :value))
1365 (type (nth 0 (widget-get widget :args)))
1366 (inlinep (widget-get type :inline))
1368 (widget-put widget :value-pos (copy-marker (point)))
1369 (set-marker-insertion-type (widget-get widget :value-pos) t)
1371 (let ((answer (widget-match-inline type value)))
1373 (setq children (cons (widget-editable-list-entry-create
1382 (widget-put widget :children (nreverse children))))
1384 (defun widget-editable-list-value-get (widget)
1385 ;; Get value of the child widget.
1386 (apply 'append (mapcar (lambda (child) (widget-apply child :value-inline))
1387 (widget-get widget :children))))
1389 (defun widget-editable-list-validate (widget)
1390 ;; All the chilren must be valid.
1391 (let ((children (widget-get widget :children))
1393 (while (and children (not found))
1394 (setq child (car children)
1395 children (cdr children)
1396 found (widget-apply child :validate)))
1399 (defun widget-editable-list-match (widget value)
1400 ;; Value must be a list and all the members must match the type.
1402 (null (cdr (widget-editable-list-match-inline widget value)))))
1404 (defun widget-editable-list-match-inline (widget value)
1405 (let ((type (nth 0 (widget-get widget :args)))
1408 (while (and value ok)
1409 (let ((answer (widget-match-inline type value)))
1411 (setq found (append found (car answer))
1414 (cons found value)))
1416 (defun widget-editable-list-insert-before (widget before)
1417 ;; Insert a new child in the list of children.
1419 (let ((children (widget-get widget :children))
1420 (inhibit-read-only t)
1421 after-change-functions)
1423 (goto-char (widget-get before :entry-from)))
1425 (goto-char (widget-get widget :value-pos))))
1426 (let ((child (widget-editable-list-entry-create
1428 (when (< (widget-get child :entry-from) (widget-get widget :from))
1429 (set-marker (widget-get widget :from)
1430 (widget-get child :entry-from)))
1431 (widget-specify-text (widget-get child :entry-from)
1432 (widget-get child :entry-to))
1433 (if (eq (car children) before)
1434 (widget-put widget :children (cons child children))
1435 (while (not (eq (car (cdr children)) before))
1436 (setq children (cdr children)))
1437 (setcdr children (cons child (cdr children)))))))
1439 (widget-apply widget :notify widget))
1441 (defun widget-editable-list-delete-at (widget child)
1442 ;; Delete child from list of children.
1444 (let ((buttons (copy-list (widget-get widget :buttons)))
1446 (inhibit-read-only t)
1447 after-change-functions)
1449 (setq button (car buttons)
1450 buttons (cdr buttons))
1451 (when (eq (widget-get button :widget) child)
1453 :buttons (delq button (widget-get widget :buttons)))
1454 (widget-delete button))))
1455 (let ((entry-from (widget-get child :entry-from))
1456 (entry-to (widget-get child :entry-to))
1457 (inhibit-read-only t)
1458 after-change-functions)
1459 (widget-delete child)
1460 (delete-region entry-from entry-to)
1461 (set-marker entry-from nil)
1462 (set-marker entry-to nil))
1463 (widget-put widget :children (delq child (widget-get widget :children))))
1465 (widget-apply widget :notify widget))
1467 (defun widget-editable-list-entry-create (widget value conv)
1468 ;; Create a new entry to the list.
1469 (let ((type (nth 0 (widget-get widget :args)))
1470 child delete insert)
1471 (widget-specify-insert
1473 (and (widget-get widget :indent)
1474 (insert-char ? (widget-get widget :indent)))
1475 (insert (widget-get widget :entry-format)))
1476 ;; Parse % escapes in format.
1477 (while (re-search-forward "%\\(.\\)" nil t)
1478 (let ((escape (aref (match-string 1) 0)))
1479 (replace-match "" t t)
1480 (cond ((eq escape ?%)
1483 (setq insert (widget-create-child-and-convert
1484 widget 'insert-button)))
1486 (setq delete (widget-create-child-and-convert
1487 widget 'delete-button)))
1490 (setq child (widget-create-child-and-convert
1491 widget type :value value))
1492 (setq child (widget-create-child widget type))))
1494 (error "Unknown escape `%c'" escape)))))
1496 :buttons (cons delete
1498 (widget-get widget :buttons))))
1499 (let ((entry-from (copy-marker (point-min)))
1500 (entry-to (copy-marker (point-max))))
1501 (widget-specify-text entry-from entry-to)
1502 (set-marker-insertion-type entry-from t)
1503 (set-marker-insertion-type entry-to nil)
1504 (widget-put child :entry-from entry-from)
1505 (widget-put child :entry-to entry-to)))
1506 (widget-put insert :widget child)
1507 (widget-put delete :widget child)
1510 ;;; The `group' Widget.
1512 (define-widget 'group 'default
1513 "A widget which group other widgets inside."
1514 :convert-widget 'widget-choice-convert-widget
1516 :value-create 'widget-group-value-create
1517 :value-delete 'widget-radio-value-delete
1518 :value-get 'widget-editable-list-value-get
1519 :validate 'widget-editable-list-validate
1520 :match 'widget-group-match
1521 :match-inline 'widget-group-match-inline)
1523 (defun widget-group-value-create (widget)
1524 ;; Create each component.
1525 (let ((args (widget-get widget :args))
1526 (value (widget-get widget :value))
1527 arg answer children)
1529 (setq arg (car args)
1531 answer (widget-match-inline arg value)
1533 (and (eq (preceding-char) ?\n)
1534 (widget-get widget :indent)
1535 (insert-char ? (widget-get widget :indent)))
1536 (push (cond ((null answer)
1537 (widget-create-child widget arg))
1538 ((widget-get arg :inline)
1539 (widget-create-child-and-convert
1540 widget arg :value (car answer)))
1542 (widget-create-child-and-convert
1543 widget arg :value (car (car answer)))))
1545 (widget-put widget :children (nreverse children))))
1547 (defun widget-group-match (widget values)
1548 ;; Match if the components match.
1550 (let ((match (widget-group-match-inline widget values)))
1551 (and match (null (cdr match))))))
1553 (defun widget-group-match-inline (widget vals)
1554 ;; Match if the components match.
1555 (let ((args (widget-get widget :args))
1556 argument answer found)
1558 (setq argument (car args)
1560 answer (widget-match-inline argument vals))
1562 (setq vals (cdr answer)
1563 found (append found (car answer)))
1570 ;;; The Sexp Widgets.
1572 (define-widget 'const 'item
1573 "An immutable sexp."
1576 (define-widget 'string 'editable-field
1579 :format "%[%t%]: %v")
1581 (define-widget 'regexp 'string
1582 ;; Should do validation.
1583 "A regular expression.")
1585 (define-widget 'file 'string
1587 It will read a file name from the minibuffer when activated."
1588 :format "%[%t%]: %v"
1590 :action 'widget-file-action)
1592 (defun widget-file-action (widget &optional event)
1593 ;; Read a file name from the minibuffer.
1594 (let* ((value (widget-value widget))
1595 (dir (file-name-directory value))
1596 (file (file-name-nondirectory value))
1597 (menu-tag (widget-apply widget :menu-tag-get))
1598 (must-match (widget-get widget :must-match))
1599 (answer (read-file-name (concat menu-tag ": (defalt `" value "') ")
1600 dir nil must-match file)))
1601 (widget-value-set widget (abbreviate-file-name answer))
1604 (define-widget 'directory 'file
1605 "A directory widget.
1606 It will read a directory name from the minibuffer when activated."
1609 (define-widget 'symbol 'string
1613 :match (lambda (widget value) (symbolp value))
1614 :value-to-internal (lambda (widget value) (symbol-name value))
1615 :value-to-external (lambda (widget value) (intern value)))
1617 (define-widget 'function 'symbol
1618 ;; Should complete on functions.
1622 (define-widget 'variable 'symbol
1623 ;; Should complete on variables.
1627 (define-widget 'sexp 'string
1628 "An arbitrary lisp expression."
1629 :tag "Lisp expression"
1631 :validate 'widget-sexp-validate
1632 :match (lambda (widget value) t)
1633 :value-to-internal 'widget-sexp-value-to-internal
1634 :value-to-external (lambda (widget value) (read value)))
1636 (defun widget-sexp-value-to-internal (widget value)
1637 ;; Use pp for printer representation.
1638 (let ((pp (pp-to-string value)))
1639 (while (string-match "\n\\'" pp)
1640 (setq pp (substring pp 0 -1)))
1641 (if (or (string-match "\n\\'" pp)
1646 (defun widget-sexp-validate (widget)
1647 ;; Valid if we can read the string and there is no junk left after it.
1649 (let ((buffer (set-buffer (get-buffer-create " *Widget Scratch*"))))
1651 (insert (widget-apply widget :value-get))
1652 (goto-char (point-min))
1653 (condition-case data
1654 (let ((value (read buffer)))
1656 (if (widget-apply widget :match value)
1658 (widget-put widget :error (widget-get widget :type-error))
1661 :error (format "Junk at end of expression: %s"
1662 (buffer-substring (point)
1665 (error (widget-put widget :error (error-message-string data))
1668 (define-widget 'integer 'sexp
1672 :type-error "This field should contain an integer"
1673 :match (lambda (widget value) (integerp value)))
1675 (define-widget 'number 'sexp
1676 "A floating point number."
1679 :type-error "This field should contain a number"
1680 :match (lambda (widget value) (numberp value)))
1682 (define-widget 'list 'group
1687 (define-widget 'vector 'group
1691 :match 'widget-vector-match
1692 :value-to-internal (lambda (widget value) (append value nil))
1693 :value-to-external (lambda (widget value) (apply 'vector value)))
1695 (defun widget-vector-match (widget value)
1696 (and (vectorp value)
1697 (widget-group-match widget
1698 (widget-apply :value-to-internal widget value))))
1700 (define-widget 'cons 'group
1704 :match 'widget-cons-match
1705 :value-to-internal (lambda (widget value)
1706 (list (car value) (cdr value)))
1707 :value-to-external (lambda (widget value)
1708 (cons (nth 0 value) (nth 1 value))))
1710 (defun widget-cons-match (widget value)
1712 (widget-group-match widget
1713 (widget-apply :value-to-internal widget value))))
1715 (define-widget 'choice 'menu-choice
1716 "A union of several sexp types."
1718 :format "%[%t%]: %v")
1720 (define-widget 'repeat 'editable-list
1721 "A variable length homogeneous list."
1723 :format "%[%t%]:\n%v%i\n")
1725 (define-widget 'set 'checklist
1726 "A list of members from a fixed set."
1728 :format "%[%t%]:\n%v")
1730 (define-widget 'boolean 'toggle
1731 "To be nil or non-nil, that is the question."
1735 ;;; The `color' Widget.
1737 (define-widget 'color-item 'choice-item
1738 "A color name (with sample)."
1739 :format "%v (%[sample%])\n"
1740 :button-face-get 'widget-color-item-button-face-get)
1742 (defun widget-color-item-button-face-get (widget)
1743 ;; We create a face from the value.
1746 (facemenu-get-face (intern (concat "fg:" (widget-value widget))))
1749 (define-widget 'color 'push-button
1750 "Choose a color name (with sample)."
1751 :format "%[%t%]: %v"
1754 :value-create 'widget-color-value-create
1755 :value-delete 'widget-radio-value-delete
1756 :value-get 'widget-color-value-get
1757 :value-set 'widget-color-value-set
1758 :action 'widget-color-action
1759 :match 'widget-field-match
1762 (defvar widget-color-choice-list nil)
1763 ;; Variable holding the possible colors.
1765 (defun widget-color-choice-list ()
1766 (unless widget-color-choice-list
1767 (setq widget-color-choice-list
1768 (mapcar '(lambda (color) (list color))
1769 (x-defined-colors))))
1770 widget-color-choice-list)
1772 (defun widget-color-value-create (widget)
1773 (let ((child (widget-create-child-and-convert
1774 widget 'color-item (widget-get widget :value))))
1775 (widget-put widget :children (list child))))
1777 (defun widget-color-value-get (widget)
1778 ;; Pass command to first child.
1779 (widget-apply (car (widget-get widget :children)) :value-get))
1781 (defun widget-color-value-set (widget value)
1782 ;; Pass command to first child.
1783 (widget-apply (car (widget-get widget :children)) :value-set value))
1785 (defvar widget-color-history nil
1786 "History of entered colors")
1788 (defun widget-color-action (widget &optional event)
1789 ;; Prompt for a color.
1790 (let* ((tag (widget-apply widget :menu-tag-get))
1791 (prompt (concat tag ": "))
1792 (answer (cond ((string-match "XEmacs" emacs-version)
1793 (read-color prompt))
1794 ((fboundp 'x-defined-colors)
1795 (completing-read (concat tag ": ")
1796 (widget-color-choice-list)
1797 nil nil nil 'widget-color-history))
1799 (read-string prompt (widget-value widget))))))
1800 (unless (zerop (length answer))
1801 (widget-value-set widget answer))))
1805 (provide 'widget-edit)
1807 ;; widget-edit.el ends here