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/
18 (autoload 'pp-to-string "pp")
19 (autoload 'Info-goto-node "info")
21 ;; The following should go away when bundled with Emacs.
27 (unless (and (featurep 'custom) (fboundp 'custom-declare-variable))
28 ;; We have the old custom-library, hack around it!
29 (defmacro defgroup (&rest args) nil)
30 (defmacro defcustom (&rest args) nil)
31 (defmacro defface (&rest args) nil)
32 (when (fboundp 'copy-face)
33 (copy-face 'default 'widget-documentation-face)
34 (copy-face 'bold 'widget-button-face)
35 (copy-face 'italic 'widget-field-face))
36 (defvar widget-mouse-face 'highlight)
37 (defvar widget-menu-max-size 40)))
41 (or (fboundp 'event-point)
42 ;; XEmacs function missing in Emacs.
43 (defun event-point (event)
44 "Return the character position of the given mouse-motion, button-press,
45 or button-release event. If the event did not occur over a window, or did
46 not occur over text, then this returns nil. Otherwise, it returns an index
47 into the buffer visible in the event's window."
48 (posn-point (event-start event))))
53 "Customization support for the Widget Library."
56 (defface widget-documentation-face '((t ()))
57 "Face used for documentation text."
60 (defface widget-button-face '((t (:bold t)))
61 "Face used for widget buttons."
64 (defcustom widget-mouse-face 'highlight
65 "Face used for widget buttons when the mouse is above them."
69 (defface widget-field-face '((((type x)
70 (class grayscale color)
72 (:background "light gray"))
74 (class grayscale color)
76 (:background "dark gray"))
79 "Face used for editable fields."
82 (defcustom widget-menu-max-size 40
83 "Largest number of items allowed in a popup-menu.
84 Larger menus are read through the minibuffer."
87 ;;; Utility functions.
89 ;; These are not really widget specific.
91 (defun widget-plist-member (plist prop)
92 ;; Return non-nil if PLIST has the property PROP.
93 ;; PLIST is a property list, which is a list of the form
94 ;; (PROP1 VALUE1 PROP2 VALUE2 ...). PROP is a symbol.
95 ;; Unlike `plist-get', this allows you to distinguish between a missing
96 ;; property and a property with the value nil.
97 ;; The value is actually the tail of PLIST whose car is PROP.
98 (while (and plist (not (eq (car plist) prop)))
99 (setq plist (cdr (cdr plist))))
102 (defun widget-princ-to-string (object)
103 ;; Return string representation of OBJECT, any Lisp object.
104 ;; No quoting characters are used; no delimiters are printed around
105 ;; the contents of strings.
107 (set-buffer (get-buffer-create " *widget-tmp*"))
109 (let ((standard-output (current-buffer)))
113 (defun widget-clear-undo ()
114 "Clear all undo information."
115 (buffer-disable-undo (current-buffer))
116 (buffer-enable-undo))
118 (defun widget-choose (title items &optional event)
119 "Choose an item from a list.
121 First argument TITLE is the name of the list.
122 Second argument ITEMS is an alist (NAME . VALUE).
123 Optional third argument EVENT is an input event.
125 The user is asked to choose between each NAME from the items alist,
126 and the VALUE of the chosen element will be returned. If EVENT is a
127 mouse event, and the number of elements in items is less than
128 `widget-menu-max-size', a popup menu will be used, otherwise the
130 (cond ((and (< (length items) widget-menu-max-size)
131 event (fboundp 'x-popup-menu) window-system)
132 ;; We are in Emacs-19, pressed by the mouse
134 (list title (cons "" items))))
135 ((and (< (length items) widget-menu-max-size)
136 event (fboundp 'popup-menu) window-system)
137 ;; We are in XEmacs, pressed by the mouse
138 (let ((val (get-popup-menu-response
143 (vector (car x) (list (car x)) t)))
146 (listp (event-object val))
147 (stringp (car-safe (event-object val)))
148 (car (event-object val))))
149 (cdr (assoc val items))))
151 (cdr (assoc (completing-read (concat title ": ")
155 ;;; Widget text specifications.
157 ;; These functions are for specifying text properties.
159 (defun widget-specify-none (from to)
160 ;; Clear all text properties between FROM and TO.
161 (set-text-properties from to nil))
163 (defun widget-specify-text (from to)
164 ;; Default properties.
165 (add-text-properties from to (list 'read-only t
167 'rear-nonsticky nil)))
169 (defun widget-specify-field (widget from to)
170 ;; Specify editable button for WIDGET between FROM and TO.
171 (widget-specify-field-update widget from to)
173 ;; Make it possible to edit the front end of the field.
174 (add-text-properties (1- from) from (list 'rear-nonsticky t
177 (when (or (string-match "\\(.\\|\n\\)%v" (widget-get widget :format))
178 (widget-get widget :hide-front-space))
179 ;; WARNING: This is going to lose horrible if the character just
180 ;; before the field can be modified (e.g. if it belongs to a
181 ;; choice widget). We try to compensate by checking the format
182 ;; string, and hope the user hasn't changed the :create method.
183 (put-text-property (- from 2) from 'intangible 'front))
185 ;; Make it possible to edit back end of the field.
186 (add-text-properties to (1+ to) (list 'front-sticky nil
189 (when (widget-get widget :size)
190 (put-text-property to (1+ to) 'invisible t)
191 (when (or (string-match "%v\\(.\\|\n\\)" (widget-get widget :format))
192 (widget-get widget :hide-rear-space))
193 ;; WARNING: This is going to lose horrible if the character just
194 ;; after the field can be modified (e.g. if it belongs to a
195 ;; choice widget). We try to compensate by checking the format
196 ;; string, and hope the user hasn't changed the :create method.
197 (put-text-property to (+ to 2) 'intangible 'rear))))
199 (defun widget-specify-field-update (widget from to)
200 ;; Specify editable button for WIDGET between FROM and TO.
201 (let ((map (widget-get widget :keymap))
202 (face (or (widget-get widget :value-face)
203 'widget-field-face)))
204 (set-text-properties from to (list 'field widget
209 (unless (widget-get widget :size)
210 (put-text-property to (1+ to) 'face face))))
212 (defun widget-specify-button (widget from to)
213 ;; Specify button for WIDGET between FROM and TO.
214 (let ((face (widget-apply widget :button-face-get)))
215 (add-text-properties from to (list 'button widget
216 'mouse-face widget-mouse-face
219 (defun widget-specify-doc (widget from to)
220 ;; Specify documentation for WIDGET between FROM and TO.
221 (add-text-properties from to (list 'widget-doc widget
222 'face 'widget-documentation-face)))
224 (defmacro widget-specify-insert (&rest form)
225 ;; Execute FORM without inheriting any text properties.
227 (let ((inhibit-read-only t)
229 after-change-functions)
231 (narrow-to-region (- (point) 2) (point))
232 (widget-specify-none (point-min) (point-max))
233 (goto-char (1+ (point-min)))
234 (setq result (progn ,@form))
235 (delete-region (point-min) (1+ (point-min)))
236 (delete-region (1- (point-max)) (point-max))
237 (goto-char (point-max))
240 ;;; Widget Properties.
242 (defun widget-put (widget property value)
243 "In WIDGET set PROPERTY to VALUE.
244 The value can later be retrived with `widget-get'."
245 (setcdr widget (plist-put (cdr widget) property value)))
247 (defun widget-get (widget property)
248 "In WIDGET, get the value of PROPERTY.
249 The value could either be specified when the widget was created, or
250 later with `widget-put'."
251 (cond ((widget-plist-member (cdr widget) property)
252 (plist-get (cdr widget) property))
254 (widget-get (get (car widget) 'widget-type) property))
257 (defun widget-member (widget property)
258 "Non-nil iff there is a definition in WIDGET for PROPERTY."
259 (cond ((widget-plist-member (cdr widget) property)
262 (widget-member (get (car widget) 'widget-type) property))
265 (defun widget-apply (widget property &rest args)
266 "Apply the value of WIDGET's PROPERTY to the widget itself.
267 ARGS are passed as extra argments to the function."
268 (apply (widget-get widget property) widget args))
270 (defun widget-value (widget)
271 "Extract the current value of WIDGET."
273 :value-to-external (widget-apply widget :value-get)))
275 (defun widget-value-set (widget value)
276 "Set the current value of WIDGET to VALUE."
278 :value-set (widget-apply widget
279 :value-to-internal value)))
281 (defun widget-match-inline (widget vals)
282 ;; In WIDGET, match the start of VALS.
283 (cond ((widget-get widget :inline)
284 (widget-apply widget :match-inline vals))
286 (widget-apply widget :match (car vals)))
287 (cons (list (car vals)) (cdr vals)))
290 ;;; Creating Widgets.
293 (defun widget-create (type &rest args)
294 "Create widget of TYPE.
295 The optional ARGS are additional keyword arguments."
296 (let ((widget (apply 'widget-convert type args)))
297 (widget-apply widget :create)
300 (defun widget-create-child-and-convert (parent type &rest args)
301 "As part of the widget PARENT, create a child widget TYPE.
302 The child is converted, using the keyword arguments ARGS."
303 (let ((widget (apply 'widget-convert type args)))
304 (widget-put widget :parent parent)
305 (unless (widget-get widget :indent)
306 (widget-put widget :indent (+ (or (widget-get parent :indent) 0)
307 (or (widget-get widget :extra-offset) 0)
308 (widget-get parent :offset))))
309 (widget-apply widget :create)
312 (defun widget-create-child (parent type)
313 "Create widget of TYPE. "
314 (let ((widget (copy-list type)))
315 (widget-put widget :parent parent)
316 (unless (widget-get widget :indent)
317 (widget-put widget :indent (+ (or (widget-get parent :indent) 0)
318 (widget-get parent :offset))))
319 (widget-apply widget :create)
323 (defun widget-delete (widget)
325 (widget-apply widget :delete))
327 (defun widget-convert (type &rest args)
328 "Convert TYPE to a widget without inserting it in the buffer.
329 The optional ARGS are additional keyword arguments."
330 ;; Don't touch the type.
331 (let* ((widget (if (symbolp type)
336 ;; First set the :args keyword.
337 (while (cdr current) ;Look in the type.
338 (let ((next (car (cdr current))))
339 (if (and (symbolp next) (eq (aref (symbol-name next) 0) ?:))
340 (setq current (cdr (cdr current)))
341 (setcdr current (list :args (cdr current)))
342 (setq current nil))))
343 (while args ;Look in the args.
344 (let ((next (nth 0 args)))
345 (if (and (symbolp next) (eq (aref (symbol-name next) 0) ?:))
346 (setq args (nthcdr 2 args))
347 (widget-put widget :args args)
349 ;; Then Convert the widget.
352 (let ((convert-widget (plist-get (cdr type) :convert-widget)))
354 (setq widget (funcall convert-widget widget))))
355 (setq type (get (car type) 'widget-type)))
356 ;; Finally set the keyword args.
358 (let ((next (nth 0 keys)))
359 (if (and (symbolp next) (eq (aref (symbol-name next) 0) ?:))
361 (widget-put widget next (nth 1 keys))
362 (setq keys (nthcdr 2 keys)))
364 ;; Convert the :value to internal format.
365 (if (widget-member widget :value)
366 (let ((value (widget-get widget :value)))
368 :value (widget-apply widget :value-to-internal value))))
369 ;; Return the newly create widget.
372 (defun widget-insert (&rest args)
373 "Call `insert' with ARGS and make the text read only."
374 (let ((inhibit-read-only t)
375 after-change-functions
378 (widget-specify-text from (point))))
380 ;;; Keymap and Comands.
382 (defvar widget-keymap nil
383 "Keymap containing useful binding for buffers containing widgets.
384 Recommended as a parent keymap for modes using widgets.")
388 (setq widget-keymap (make-sparse-keymap))
389 (set-keymap-parent widget-keymap global-map)
390 (define-key widget-keymap "\t" 'widget-forward)
391 (define-key widget-keymap "\M-\t" 'widget-backward)
392 (define-key widget-keymap [(shift tab)] 'widget-backward)
393 (if (string-match "XEmacs" (emacs-version))
394 (define-key widget-keymap [button2] 'widget-button-click)
395 (define-key widget-keymap [menu-bar] 'nil)
396 (define-key widget-keymap [mouse-2] 'widget-button-click))
397 (define-key widget-keymap "\C-m" 'widget-button-press))
399 (defvar widget-global-map global-map
400 "Keymap used for events the widget does not handle themselves.")
401 (make-variable-buffer-local 'widget-global-map)
403 (defun widget-button-click (event)
404 "Activate button below mouse pointer."
406 (widget-button-press (event-point event) event))
408 (defun widget-button-press (pos &optional event)
409 "Activate button at POS."
411 (let* ((button (get-text-property pos 'button)))
413 (widget-apply button :action event)
415 (lookup-key widget-global-map (this-command-keys))))))
417 (defun widget-forward (arg)
418 "Move point to the next field or button.
419 With optional ARG, move across that many fields."
423 (let ((next (cond ((get-text-property (point) 'button)
424 (next-single-property-change (point) 'button))
425 ((get-text-property (point) 'field)
426 (next-single-property-change (point) 'field))
429 (if (null next) ; Widget extends to end. of buffer
430 (setq next (point-min)))
431 (let ((button (next-single-property-change next 'button))
432 (field (next-single-property-change next 'field)))
433 (cond ((or (get-text-property next 'button)
434 (get-text-property next 'field))
437 (goto-char (min button field)))
438 (button (goto-char button))
439 (field (goto-char field))
441 (let ((button (next-single-property-change (point-min) 'button))
442 (field (next-single-property-change (point-min) 'field)))
443 (cond ((and button field) (goto-char (min button field)))
444 (button (goto-char button))
445 (field (goto-char field))
447 (error "No buttons or fields found")))))))))
449 (if (= (point-min) (point))
452 (let ((previous (cond ((get-text-property (1- (point)) 'button)
453 (previous-single-property-change (point) 'button))
454 ((get-text-property (1- (point)) 'field)
455 (previous-single-property-change (point) 'field))
458 (if (null previous) ; Widget extends to beg. of buffer
459 (setq previous (point-max)))
460 (let ((button (previous-single-property-change previous 'button))
461 (field (previous-single-property-change previous 'field)))
462 (cond ((and button field)
463 (goto-char (max button field)))
464 (button (goto-char button))
465 (field (goto-char field))
467 (let ((button (previous-single-property-change
468 (point-max) 'button))
469 (field (previous-single-property-change
470 (point-max) 'field)))
471 (cond ((and button field) (goto-char (max button field)))
472 (button (goto-char button))
473 (field (goto-char field))
475 (error "No buttons or fields found"))))))))
476 (let ((button (previous-single-property-change (point) 'button))
477 (field (previous-single-property-change (point) 'field)))
478 (cond ((and button field)
479 (goto-char (max button field)))
480 (button (goto-char button))
481 (field (goto-char field)))))
482 (widget-echo-help (point)))
484 (defun widget-backward (arg)
485 "Move point to the previous field or button.
486 With optional ARG, move across that many fields."
488 (widget-forward (- arg)))
490 ;;; Setting up the buffer.
492 (defvar widget-field-new nil)
493 ;; List of all newly created editable fields in the buffer.
494 (make-variable-buffer-local 'widget-field-new)
496 (defvar widget-field-list nil)
497 ;; List of all editable fields in the buffer.
498 (make-variable-buffer-local 'widget-field-list)
500 (defun widget-setup ()
501 "Setup current buffer so editing string widgets works."
502 (let ((inhibit-read-only t)
503 (after-change-functions nil)
505 (while widget-field-new
506 (setq field (car widget-field-new)
507 widget-field-new (cdr widget-field-new)
508 widget-field-list (cons field widget-field-list))
509 (let ((from (widget-get field :value-from))
510 (to (widget-get field :value-to)))
511 (widget-specify-field field from to)
512 (move-marker from (1- from))
513 (move-marker to (1+ to)))))
515 ;; We need to maintain text properties and size of the editing fields.
516 (make-local-variable 'after-change-functions)
517 (if widget-field-list
518 (setq after-change-functions '(widget-after-change))
519 (setq after-change-functions nil)))
521 (defvar widget-field-last nil)
522 ;; Last field containing point.
523 (make-variable-buffer-local 'widget-field-last)
525 (defvar widget-field-was nil)
526 ;; The widget data before the change.
527 (make-variable-buffer-local 'widget-field-was)
529 (defun widget-field-find (pos)
530 ;; Find widget whose editing field is located at POS.
531 ;; Return nil if POS is not inside and editing field.
533 ;; This is only used in `widget-field-modified', since ordinarily
534 ;; you would just test the field property.
535 (let ((fields widget-field-list)
538 (setq field (car fields)
540 (let ((from (widget-get field :value-from))
541 (to (widget-get field :value-to)))
542 (if (and from to (< from pos) (> to pos))
547 (defun widget-after-change (from to old)
548 ;; Adjust field size and text properties.
550 (let ((field (widget-field-find from))
551 (inhibit-read-only t))
553 ((not (eq field (widget-field-find to)))
555 (message "Error: `widget-after-change' called on two fields"))
557 (let ((size (widget-get field :size)))
559 (let ((begin (1+ (widget-get field :value-from)))
560 (end (1- (widget-get field :value-to))))
561 (widget-specify-field-update field begin end)
562 (cond ((< (- end begin) size)
566 (insert-char ?\ (- (+ begin size) end))
567 (widget-specify-field-update field
570 ((> (- end begin) size)
571 ;; Field too large and
572 (if (or (< (point) (+ begin size))
574 ;; Point is outside extra space.
575 (setq begin (+ begin size))
576 ;; Point is within the extra space.
577 (setq begin (point)))
580 (while (and (eq (preceding-char) ?\ )
582 (delete-backward-char 1))))))
583 (widget-specify-field-update field from to)))
584 (widget-apply field :notify field))))
587 ;;; The `default' Widget.
589 (define-widget 'default nil
590 "Basic widget other widgets are derived from."
591 :value-to-internal (lambda (widget value) value)
592 :value-to-external (lambda (widget value) value)
593 :create 'widget-default-create
596 :format-handler 'widget-default-format-handler
597 :button-face-get 'widget-default-button-face-get
598 :delete 'widget-default-delete
599 :value-set 'widget-default-value-set
600 :value-inline 'widget-default-value-inline
601 :menu-tag-get 'widget-default-menu-tag-get
602 :validate (lambda (widget) nil)
603 :action 'widget-default-action
604 :notify 'widget-default-notify)
606 (defun widget-default-create (widget)
607 "Create WIDGET at point in the current buffer."
608 (widget-specify-insert
610 (tag (widget-get widget :tag))
611 (doc (widget-get widget :doc))
612 button-begin button-end
615 (insert (widget-get widget :format))
617 ;; Parse % escapes in format.
618 (while (re-search-forward "%\\(.\\)" nil t)
619 (let ((escape (aref (match-string 1) 0)))
620 (replace-match "" t t)
621 (cond ((eq escape ?%)
624 (setq button-begin (point)))
626 (setq button-end (point)))
628 (when (widget-get widget :indent)
630 (insert-char ? (widget-get widget :indent))))
634 (let ((standard-output (current-buffer)))
635 (princ (widget-get widget :value)))))
638 (setq doc-begin (point))
640 (while (eq (preceding-char) ?\n)
641 (delete-backward-char 1))
643 (setq doc-end (point))))
645 (if (and button-begin (not button-end))
646 (widget-apply widget :value-create)
647 (setq value-pos (point))))
649 (widget-apply widget :format-handler escape)))))
650 ;; Specify button and doc, and insert value.
651 (and button-begin button-end
652 (widget-specify-button widget button-begin button-end))
653 (and doc-begin doc-end
654 (widget-specify-doc widget doc-begin doc-end))
656 (goto-char value-pos)
657 (widget-apply widget :value-create)))
658 (let ((from (copy-marker (point-min)))
659 (to (copy-marker (point-max))))
660 (widget-specify-text from to)
661 (set-marker-insertion-type from t)
662 (set-marker-insertion-type to nil)
663 (widget-put widget :from from)
664 (widget-put widget :to to))))
666 (defun widget-default-format-handler (widget escape)
667 ;; We recognize the %h escape by default.
668 (let* ((buttons (widget-get widget :buttons))
669 (doc-property (widget-get widget :documentation-property))
670 (doc-try (cond ((widget-get widget :doc))
671 ((symbolp doc-property)
672 (documentation-property (widget-get widget :value)
675 (funcall doc-property (widget-get widget :value)))))
676 (doc-text (and (stringp doc-try)
677 (> (length doc-try) 1)
679 (cond ((eq escape ?h)
681 (and (eq (preceding-char) ?\n)
682 (widget-get widget :indent)
683 (insert-char ? (widget-get widget :indent)))
684 ;; The `*' in the beginning is redundant.
685 (when (eq (aref doc-text 0) ?*)
686 (setq doc-text (substring doc-text 1)))
687 ;; Get rid of trailing newlines.
688 (when (string-match "\n+\\'" doc-text)
689 (setq doc-text (substring doc-text 0 (match-beginning 0))))
690 (push (if (string-match "\n." doc-text)
691 ;; Allow multiline doc to be hiden.
692 (widget-create-child-and-convert
695 (string-match "\\`.*" doc-text)
696 (match-string 0 doc-text))
699 ;; A single line is just inserted.
700 (widget-create-child-and-convert
701 widget 'item :format "%d" :doc doc-text nil))
704 (error "Unknown escape `%c'" escape)))
705 (widget-put widget :buttons buttons)))
707 (defun widget-default-button-face-get (widget)
708 ;; Use :button-face or widget-button-face
709 (or (widget-get widget :button-face) 'widget-button-face))
711 (defun widget-default-delete (widget)
712 ;; Remove widget from the buffer.
713 (let ((from (widget-get widget :from))
714 (to (widget-get widget :to))
715 (inhibit-read-only t)
716 after-change-functions)
717 (widget-apply widget :value-delete)
718 (delete-region from to)
719 (set-marker from nil)
720 (set-marker to nil)))
722 (defun widget-default-value-set (widget value)
723 ;; Recreate widget with new value.
725 (goto-char (widget-get widget :from))
726 (widget-apply widget :delete)
727 (widget-put widget :value value)
728 (widget-apply widget :create)))
730 (defun widget-default-value-inline (widget)
731 ;; Wrap value in a list unless it is inline.
732 (if (widget-get widget :inline)
733 (widget-value widget)
734 (list (widget-value widget))))
736 (defun widget-default-menu-tag-get (widget)
737 ;; Use tag or value for menus.
738 (or (widget-get widget :menu-tag)
739 (widget-get widget :tag)
740 (widget-princ-to-string (widget-get widget :value))))
742 (defun widget-default-action (widget &optional event)
743 ;; Notify the parent when a widget change
744 (let ((parent (widget-get widget :parent)))
746 (widget-apply parent :notify widget event))))
748 (defun widget-default-notify (widget child &optional event)
749 ;; Pass notification to parent.
750 (widget-default-action widget event))
752 ;;; The `item' Widget.
754 (define-widget 'item 'default
755 "Constant items for inclusion in other widgets."
756 :convert-widget 'widget-item-convert-widget
757 :value-create 'widget-item-value-create
758 :value-delete 'ignore
759 :value-get 'widget-item-value-get
760 :match 'widget-item-match
761 :match-inline 'widget-item-match-inline
762 :action 'widget-item-action
765 (defun widget-item-convert-widget (widget)
766 ;; Initialize :value and :tag from :args in WIDGET.
767 (let ((args (widget-get widget :args)))
769 (widget-put widget :value (widget-apply widget
770 :value-to-internal (car args)))
771 (widget-put widget :args nil)))
774 (defun widget-item-value-create (widget)
775 ;; Insert the printed representation of the value.
776 (let ((standard-output (current-buffer)))
777 (princ (widget-get widget :value))))
779 (defun widget-item-match (widget value)
780 ;; Match if the value is the same.
781 (equal (widget-get widget :value) value))
783 (defun widget-item-match-inline (widget values)
784 ;; Match if the value is the same.
785 (let ((value (widget-get widget :value)))
787 (<= (length value) (length values))
788 (let ((head (subseq values 0 (length value))))
789 (and (equal head value)
790 (cons head (subseq values (length value))))))))
792 (defun widget-item-action (widget &optional event)
793 ;; Just notify itself.
794 (widget-apply widget :notify widget event))
796 (defun widget-item-value-get (widget)
798 (widget-get widget :value))
800 ;;; The `push-button' Widget.
802 (define-widget 'push-button 'item
806 ;;; The `link' Widget.
808 (define-widget 'link 'item
812 ;;; The `info-link' Widget.
814 (define-widget 'info-link 'link
815 "A link to an info file."
816 :action 'widget-info-link-action)
818 (defun widget-info-link-action (widget &optional event)
819 "Open the info node specified by WIDGET."
820 (Info-goto-node (widget-value widget)))
822 ;;; The `url-link' Widget.
824 (define-widget 'url-link 'link
825 "A link to an www page."
826 :action 'widget-url-link-action)
828 (defun widget-url-link-action (widget &optional event)
829 "Open the url specified by WIDGET."
830 (require 'browse-url)
831 (funcall browse-url-browser-function (widget-value widget)))
833 ;;; The `editable-field' Widget.
835 (define-widget 'editable-field 'default
836 "An editable text field."
837 :convert-widget 'widget-item-convert-widget
840 :action 'widget-field-action
841 :value-create 'widget-field-value-create
842 :value-delete 'widget-field-value-delete
843 :value-get 'widget-field-value-get
844 :match 'widget-field-match)
846 ;; History of field minibuffer edits.
847 (defvar widget-field-history nil)
849 (defun widget-field-action (widget &optional event)
850 ;; Edit the value in the minibuffer.
851 (let ((tag (widget-apply widget :menu-tag-get))
852 (invalid (widget-apply widget :validate)))
854 (error (widget-get invalid :error)))
855 (widget-value-set widget
858 (read-string (concat tag ": ")
862 (widget-value widget))
863 'widget-field-history)))
864 (widget-apply widget :notify widget event)
867 (defun widget-field-value-create (widget)
868 ;; Create an editable text field.
870 (let ((size (widget-get widget :size))
871 (value (widget-get widget :value))
874 (if (zerop (length value))
878 (if (< (length value) size)
879 (insert-char ?\ (- size (length value)))))
880 (unless (memq widget widget-field-list)
881 (setq widget-field-new (cons widget widget-field-new)))
882 (widget-put widget :value-to (copy-marker (point)))
883 (set-marker-insertion-type (widget-get widget :value-to) nil)
887 (widget-put widget :value-from (copy-marker from))
888 (set-marker-insertion-type (widget-get widget :value-from) t)))
890 (defun widget-field-value-delete (widget)
891 ;; Remove the widget from the list of active editing fields.
892 (setq widget-field-list (delq widget widget-field-list))
893 (set-marker (widget-get widget :value-from) nil)
894 (set-marker (widget-get widget :value-to) nil))
896 (defun widget-field-value-get (widget)
897 ;; Return current text in editing field.
898 (let ((from (widget-get widget :value-from))
899 (to (widget-get widget :value-to))
900 (size (widget-get widget :size))
901 (old (current-buffer)))
904 (set-buffer (marker-buffer from))
910 (eq (char-after (1- to)) ?\ ))
912 (prog1 (buffer-substring-no-properties from to)
914 (widget-get widget :value))))
916 (defun widget-field-match (widget value)
920 ;;; The `text' Widget.
922 (define-widget 'text 'editable-field
923 "A multiline text area.")
925 ;;; The `menu-choice' Widget.
927 (define-widget 'menu-choice 'default
929 :convert-widget 'widget-choice-convert-widget
933 :void '(item :format "invalid (%t)\n")
934 :value-create 'widget-choice-value-create
935 :value-delete 'widget-radio-value-delete
936 :value-get 'widget-choice-value-get
937 :value-inline 'widget-choice-value-inline
938 :action 'widget-choice-action
939 :error "Make a choice"
940 :validate 'widget-choice-validate
941 :match 'widget-choice-match
942 :match-inline 'widget-choice-match-inline)
944 (defun widget-choice-convert-widget (widget)
945 ;; Expand type args into widget objects.
946 ; (widget-put widget :args (mapcar (lambda (child)
947 ; (if (widget-get child ':converted)
949 ; (widget-put child ':converted t)
950 ; (widget-convert child)))
951 ; (widget-get widget :args)))
952 (widget-put widget :args (mapcar 'widget-convert (widget-get widget :args)))
955 (defun widget-choice-value-create (widget)
956 ;; Insert the first choice that matches the value.
957 (let ((value (widget-get widget :value))
958 (args (widget-get widget :args))
961 (setq current (car args)
963 (when (widget-apply current :match value)
964 (widget-put widget :children (list (widget-create-child-and-convert
965 widget current :value value)))
966 (widget-put widget :choice current)
970 (let ((void (widget-get widget :void)))
971 (widget-put widget :children (list (widget-create-child-and-convert
972 widget void :value value)))
973 (widget-put widget :choice void)))))
975 (defun widget-choice-value-get (widget)
976 ;; Get value of the child widget.
977 (widget-value (car (widget-get widget :children))))
979 (defun widget-choice-value-inline (widget)
980 ;; Get value of the child widget.
981 (widget-apply (car (widget-get widget :children)) :value-inline))
983 (defun widget-choice-action (widget &optional event)
985 (let ((args (widget-get widget :args))
986 (old (widget-get widget :choice))
987 (tag (widget-apply widget :menu-tag-get))
988 (completion-ignore-case (widget-get widget :case-fold))
990 ;; Remember old value.
991 (if (and old (not (widget-apply widget :validate)))
992 (let* ((external (widget-value widget))
993 (internal (widget-apply old :value-to-internal external)))
994 (widget-put old :value internal)))
997 (cond ((= (length args) 0)
1001 ((and (= (length args) 2)
1003 (if (eq old (nth 0 args))
1008 (setq current (car args)
1011 (cons (cons (widget-apply current :menu-tag-get)
1014 (widget-choose tag (reverse choices) event))))
1016 (widget-value-set widget
1017 (widget-apply current :value-to-external
1018 (widget-get current :value)))
1019 (widget-apply widget :notify widget event)
1022 (widget-apply widget :notify widget event)
1023 (widget-clear-undo))
1025 (defun widget-choice-validate (widget)
1026 ;; Valid if we have made a valid choice.
1027 (let ((void (widget-get widget :void))
1028 (choice (widget-get widget :choice))
1029 (child (car (widget-get widget :children))))
1030 (if (eq void choice)
1032 (widget-apply child :validate))))
1034 (defun widget-choice-match (widget value)
1035 ;; Matches if one of the choices matches.
1036 (let ((args (widget-get widget :args))
1038 (while (and args (not found))
1039 (setq current (car args)
1041 found (widget-apply current :match value)))
1044 (defun widget-choice-match-inline (widget values)
1045 ;; Matches if one of the choices matches.
1046 (let ((args (widget-get widget :args))
1048 (while (and args (null found))
1049 (setq current (car args)
1051 found (widget-match-inline current values)))
1054 ;;; The `toggle' Widget.
1056 (define-widget 'toggle 'menu-choice
1057 "Toggle between two states."
1058 :convert-widget 'widget-toggle-convert-widget
1063 (defun widget-toggle-convert-widget (widget)
1064 ;; Create the types representing the `on' and `off' states.
1065 (let ((on-type (widget-get widget :on-type))
1066 (off-type (widget-get widget :off-type)))
1071 :match (lambda (widget value) value)
1072 :tag (widget-get widget :on))))
1075 (list 'choice-item :value nil :tag (widget-get widget :off))))
1076 (widget-put widget :args (list on-type off-type)))
1079 ;;; The `checkbox' Widget.
1081 (define-widget 'checkbox 'toggle
1082 "A checkbox toggle."
1083 :convert-widget 'widget-item-convert-widget
1084 :on-type '(choice-item :format "%[[X]%]" t)
1085 :off-type '(choice-item :format "%[[ ]%]" nil))
1087 ;;; The `checklist' Widget.
1089 (define-widget 'checklist 'default
1090 "A multiple choice widget."
1091 :convert-widget 'widget-choice-convert-widget
1094 :entry-format "%b %v"
1095 :menu-tag "checklist"
1097 :value-create 'widget-checklist-value-create
1098 :value-delete 'widget-radio-value-delete
1099 :value-get 'widget-checklist-value-get
1100 :validate 'widget-checklist-validate
1101 :match 'widget-checklist-match
1102 :match-inline 'widget-checklist-match-inline)
1104 (defun widget-checklist-value-create (widget)
1105 ;; Insert all values
1106 (let ((alist (widget-checklist-match-find widget (widget-get widget :value)))
1107 (args (widget-get widget :args)))
1109 (widget-checklist-add-item widget (car args) (assq (car args) alist))
1110 (setq args (cdr args)))
1111 (widget-put widget :children (nreverse (widget-get widget :children)))))
1113 (defun widget-checklist-add-item (widget type chosen)
1114 ;; Create checklist item in WIDGET of type TYPE.
1115 ;; If the item is checked, CHOSEN is a cons whose cdr is the value.
1116 (and (eq (preceding-char) ?\n)
1117 (widget-get widget :indent)
1118 (insert-char ? (widget-get widget :indent)))
1119 (widget-specify-insert
1120 (let* ((children (widget-get widget :children))
1121 (buttons (widget-get widget :buttons))
1124 (insert (widget-get widget :entry-format))
1126 ;; Parse % escapes in format.
1127 (while (re-search-forward "%\\([bv%]\\)" nil t)
1128 (let ((escape (aref (match-string 1) 0)))
1129 (replace-match "" t t)
1130 (cond ((eq escape ?%)
1133 (setq button (widget-create-child-and-convert
1134 widget 'checkbox :value (not (null chosen)))))
1138 (widget-create-child widget type))
1139 ((widget-get type :inline)
1140 (widget-create-child-and-convert
1141 widget type :value (cdr chosen)))
1143 (widget-create-child-and-convert
1144 widget type :value (car (cdr chosen)))))))
1146 (error "Unknown escape `%c'" escape)))))
1147 ;; Update properties.
1148 (and button child (widget-put child :button button))
1149 (and button (widget-put widget :buttons (cons button buttons)))
1150 (and child (widget-put widget :children (cons child children))))))
1152 (defun widget-checklist-match (widget values)
1153 ;; All values must match a type in the checklist.
1155 (null (cdr (widget-checklist-match-inline widget values)))))
1157 (defun widget-checklist-match-inline (widget values)
1158 ;; Find the values which match a type in the checklist.
1159 (let ((greedy (widget-get widget :greedy))
1160 (args (copy-list (widget-get widget :args)))
1163 (let ((answer (widget-checklist-match-up args values)))
1165 (let ((vals (widget-match-inline answer values)))
1166 (setq found (append found (car vals))
1168 args (delq answer args))))
1170 (setq rest (append rest (list (car values)))
1171 values (cdr values)))
1173 (setq rest (append rest values)
1177 (defun widget-checklist-match-find (widget vals)
1178 ;; Find the vals which match a type in the checklist.
1179 ;; Return an alist of (TYPE MATCH).
1180 (let ((greedy (widget-get widget :greedy))
1181 (args (copy-list (widget-get widget :args)))
1184 (let ((answer (widget-checklist-match-up args vals)))
1186 (let ((match (widget-match-inline answer vals)))
1187 (setq found (cons (cons answer (car match)) found)
1189 args (delq answer args))))
1191 (setq vals (cdr vals)))
1196 (defun widget-checklist-match-up (args vals)
1197 ;; Rerturn the first type from ARGS that matches VALS.
1198 (let (current found)
1199 (while (and args (null found))
1200 (setq current (car args)
1202 found (widget-match-inline current vals)))
1203 (and found current)))
1205 (defun widget-checklist-value-get (widget)
1206 ;; The values of all selected items.
1207 (let ((children (widget-get widget :children))
1210 (setq child (car children)
1211 children (cdr children))
1212 (if (widget-value (widget-get child :button))
1213 (setq result (append result (widget-apply child :value-inline)))))
1216 (defun widget-checklist-validate (widget)
1217 ;; Ticked chilren must be valid.
1218 (let ((children (widget-get widget :children))
1220 (while (and children (not found))
1221 (setq child (car children)
1222 children (cdr children)
1223 button (widget-get child :button)
1224 found (and (widget-value button)
1225 (widget-apply child :validate))))
1228 ;;; The `option' Widget
1230 (define-widget 'option 'checklist
1231 "An widget with an optional item."
1234 ;;; The `choice-item' Widget.
1236 (define-widget 'choice-item 'item
1237 "Button items that delegate action events to their parents."
1238 :action 'widget-choice-item-action
1239 :format "%[%t%] \n")
1241 (defun widget-choice-item-action (widget &optional event)
1242 ;; Tell parent what happened.
1243 (widget-apply (widget-get widget :parent) :action event))
1245 ;;; The `radio-button' Widget.
1247 (define-widget 'radio-button 'toggle
1248 "A radio button for use in the `radio' widget."
1249 :notify 'widget-radio-button-notify
1250 :on-type '(choice-item :format "%[(*)%]" t)
1251 :off-type '(choice-item :format "%[( )%]" nil))
1253 (defun widget-radio-button-notify (widget child &optional event)
1254 ;; Notify the parent.
1255 (widget-apply (widget-get widget :parent) :action widget event))
1257 ;;; The `radio-button-choice' Widget.
1259 (define-widget 'radio-button-choice 'default
1260 "Select one of multiple options."
1261 :convert-widget 'widget-choice-convert-widget
1264 :entry-format "%b %v"
1266 :value-create 'widget-radio-value-create
1267 :value-delete 'widget-radio-value-delete
1268 :value-get 'widget-radio-value-get
1269 :value-inline 'widget-radio-value-inline
1270 :value-set 'widget-radio-value-set
1271 :error "You must push one of the buttons"
1272 :validate 'widget-radio-validate
1273 :match 'widget-choice-match
1274 :match-inline 'widget-choice-match-inline
1275 :action 'widget-radio-action)
1277 (defun widget-radio-value-create (widget)
1278 ;; Insert all values
1279 (let ((args (widget-get widget :args))
1282 (setq arg (car args)
1284 (widget-radio-add-item widget arg))))
1286 (defun widget-radio-add-item (widget type)
1287 "Add to radio widget WIDGET a new radio button item of type TYPE."
1288 ;; (setq type (widget-convert type))
1289 (and (eq (preceding-char) ?\n)
1290 (widget-get widget :indent)
1291 (insert-char ? (widget-get widget :indent)))
1292 (widget-specify-insert
1293 (let* ((value (widget-get widget :value))
1294 (children (widget-get widget :children))
1295 (buttons (widget-get widget :buttons))
1297 (chosen (and (null (widget-get widget :choice))
1298 (widget-apply type :match value)))
1300 (insert (widget-get widget :entry-format))
1302 ;; Parse % escapes in format.
1303 (while (re-search-forward "%\\([bv%]\\)" nil t)
1304 (let ((escape (aref (match-string 1) 0)))
1305 (replace-match "" t t)
1306 (cond ((eq escape ?%)
1309 (setq button (widget-create-child-and-convert
1310 widget 'radio-button
1311 :value (not (null chosen)))))
1313 (setq child (if chosen
1314 (widget-create-child-and-convert
1315 widget type :value value)
1316 (widget-create-child widget type))))
1318 (error "Unknown escape `%c'" escape)))))
1319 ;; Update properties.
1321 (widget-put widget :choice type))
1323 (widget-put child :button button)
1324 (widget-put widget :buttons (nconc buttons (list button))))
1326 (widget-put widget :children (nconc children (list child))))
1329 (defun widget-radio-value-delete (widget)
1330 ;; Delete the child widgets.
1331 (mapcar 'widget-delete (widget-get widget :children))
1332 (widget-put widget :children nil)
1333 (mapcar 'widget-delete (widget-get widget :buttons))
1334 (widget-put widget :buttons nil))
1336 (defun widget-radio-value-get (widget)
1337 ;; Get value of the child widget.
1338 (let ((chosen (widget-radio-chosen widget)))
1339 (and chosen (widget-value chosen))))
1341 (defun widget-radio-chosen (widget)
1342 "Return the widget representing the chosen radio button."
1343 (let ((children (widget-get widget :children))
1346 (setq current (car children)
1347 children (cdr children))
1348 (let* ((button (widget-get current :button))
1349 (value (widget-apply button :value-get)))
1355 (defun widget-radio-value-inline (widget)
1356 ;; Get value of the child widget.
1357 (let ((children (widget-get widget :children))
1360 (setq current (car children)
1361 children (cdr children))
1362 (let* ((button (widget-get current :button))
1363 (value (widget-apply button :value-get)))
1365 (setq found (widget-apply current :value-inline)
1369 (defun widget-radio-value-set (widget value)
1370 ;; We can't just delete and recreate a radio widget, since children
1371 ;; can be added after the original creation and won't be recreated
1373 (let ((children (widget-get widget :children))
1376 (setq current (car children)
1377 children (cdr children))
1378 (let* ((button (widget-get current :button))
1379 (match (and (not found)
1380 (widget-apply current :match value))))
1381 (widget-value-set button match)
1383 (widget-value-set current value))
1384 (setq found (or found match))))))
1386 (defun widget-radio-validate (widget)
1387 ;; Valid if we have made a valid choice.
1388 (let ((children (widget-get widget :children))
1389 current found button)
1390 (while (and children (not found))
1391 (setq current (car children)
1392 children (cdr children)
1393 button (widget-get current :button)
1394 found (widget-apply button :value-get)))
1396 (widget-apply current :validate)
1399 (defun widget-radio-action (widget child event)
1400 ;; Check if a radio button was pressed.
1401 (let ((children (widget-get widget :children))
1402 (buttons (widget-get widget :buttons))
1404 (when (memq child buttons)
1406 (setq current (car children)
1407 children (cdr children))
1408 (let* ((button (widget-get current :button)))
1409 (cond ((eq child button)
1410 (widget-value-set button t))
1411 ((widget-value button)
1412 (widget-value-set button nil)))))))
1413 ;; Pass notification to parent.
1414 (widget-apply widget :notify child event))
1416 ;;; The `insert-button' Widget.
1418 (define-widget 'insert-button 'push-button
1419 "An insert button for the `editable-list' widget."
1421 :action 'widget-insert-button-action)
1423 (defun widget-insert-button-action (widget &optional event)
1424 ;; Ask the parent to insert a new item.
1425 (widget-apply (widget-get widget :parent)
1426 :insert-before (widget-get widget :widget)))
1428 ;;; The `delete-button' Widget.
1430 (define-widget 'delete-button 'push-button
1431 "A delete button for the `editable-list' widget."
1433 :action 'widget-delete-button-action)
1435 (defun widget-delete-button-action (widget &optional event)
1436 ;; Ask the parent to insert a new item.
1437 (widget-apply (widget-get widget :parent)
1438 :delete-at (widget-get widget :widget)))
1440 ;;; The `editable-list' Widget.
1442 (define-widget 'editable-list 'default
1443 "A variable list of widgets of the same type."
1444 :convert-widget 'widget-choice-convert-widget
1447 :format-handler 'widget-editable-list-format-handler
1448 :entry-format "%i %d %v"
1449 :menu-tag "editable-list"
1450 :value-create 'widget-editable-list-value-create
1451 :value-delete 'widget-radio-value-delete
1452 :value-get 'widget-editable-list-value-get
1453 :validate 'widget-editable-list-validate
1454 :match 'widget-editable-list-match
1455 :match-inline 'widget-editable-list-match-inline
1456 :insert-before 'widget-editable-list-insert-before
1457 :delete-at 'widget-editable-list-delete-at)
1459 (defun widget-editable-list-format-handler (widget escape)
1460 ;; We recognize the insert button.
1461 (cond ((eq escape ?i)
1462 (and (widget-get widget :indent)
1463 (insert-char ? (widget-get widget :indent)))
1464 (widget-create-child-and-convert widget 'insert-button))
1466 (widget-default-format-handler widget escape))))
1468 ;(defun widget-editable-list-format-handler (widget escape)
1469 ; ;; We recognize the insert button.
1470 ; (cond ((eq escape ?i)
1473 ; (let* ((from (point))
1474 ; (button (widget-create-child-and-convert
1475 ; widget 'insert-button)))
1476 ; (widget-specify-button button from (point)))
1479 ; (widget-default-format-handler widget escape))))
1481 (defun widget-editable-list-value-create (widget)
1482 ;; Insert all values
1483 (let* ((value (widget-get widget :value))
1484 (type (nth 0 (widget-get widget :args)))
1485 (inlinep (widget-get type :inline))
1487 (widget-put widget :value-pos (copy-marker (point)))
1488 (set-marker-insertion-type (widget-get widget :value-pos) t)
1490 (let ((answer (widget-match-inline type value)))
1492 (setq children (cons (widget-editable-list-entry-create
1501 (widget-put widget :children (nreverse children))))
1503 (defun widget-editable-list-value-get (widget)
1504 ;; Get value of the child widget.
1505 (apply 'append (mapcar (lambda (child) (widget-apply child :value-inline))
1506 (widget-get widget :children))))
1508 (defun widget-editable-list-validate (widget)
1509 ;; All the chilren must be valid.
1510 (let ((children (widget-get widget :children))
1512 (while (and children (not found))
1513 (setq child (car children)
1514 children (cdr children)
1515 found (widget-apply child :validate)))
1518 (defun widget-editable-list-match (widget value)
1519 ;; Value must be a list and all the members must match the type.
1521 (null (cdr (widget-editable-list-match-inline widget value)))))
1523 (defun widget-editable-list-match-inline (widget value)
1524 (let ((type (nth 0 (widget-get widget :args)))
1527 (while (and value ok)
1528 (let ((answer (widget-match-inline type value)))
1530 (setq found (append found (car answer))
1533 (cons found value)))
1535 (defun widget-editable-list-insert-before (widget before)
1536 ;; Insert a new child in the list of children.
1538 (let ((children (widget-get widget :children))
1539 (inhibit-read-only t)
1540 after-change-functions)
1542 (goto-char (widget-get before :entry-from)))
1544 (goto-char (widget-get widget :value-pos))))
1545 (let ((child (widget-editable-list-entry-create
1547 (when (< (widget-get child :entry-from) (widget-get widget :from))
1548 (set-marker (widget-get widget :from)
1549 (widget-get child :entry-from)))
1550 (widget-specify-text (widget-get child :entry-from)
1551 (widget-get child :entry-to))
1552 (if (eq (car children) before)
1553 (widget-put widget :children (cons child children))
1554 (while (not (eq (car (cdr children)) before))
1555 (setq children (cdr children)))
1556 (setcdr children (cons child (cdr children)))))))
1558 (widget-apply widget :notify widget))
1560 (defun widget-editable-list-delete-at (widget child)
1561 ;; Delete child from list of children.
1563 (let ((buttons (copy-list (widget-get widget :buttons)))
1565 (inhibit-read-only t)
1566 after-change-functions)
1568 (setq button (car buttons)
1569 buttons (cdr buttons))
1570 (when (eq (widget-get button :widget) child)
1572 :buttons (delq button (widget-get widget :buttons)))
1573 (widget-delete button))))
1574 (let ((entry-from (widget-get child :entry-from))
1575 (entry-to (widget-get child :entry-to))
1576 (inhibit-read-only t)
1577 after-change-functions)
1578 (widget-delete child)
1579 (delete-region entry-from entry-to)
1580 (set-marker entry-from nil)
1581 (set-marker entry-to nil))
1582 (widget-put widget :children (delq child (widget-get widget :children))))
1584 (widget-apply widget :notify widget))
1586 (defun widget-editable-list-entry-create (widget value conv)
1587 ;; Create a new entry to the list.
1588 (let ((type (nth 0 (widget-get widget :args)))
1589 child delete insert)
1590 (widget-specify-insert
1592 (and (widget-get widget :indent)
1593 (insert-char ? (widget-get widget :indent)))
1594 (insert (widget-get widget :entry-format)))
1595 ;; Parse % escapes in format.
1596 (while (re-search-forward "%\\(.\\)" nil t)
1597 (let ((escape (aref (match-string 1) 0)))
1598 (replace-match "" t t)
1599 (cond ((eq escape ?%)
1602 (setq insert (widget-create-child-and-convert
1603 widget 'insert-button)))
1605 (setq delete (widget-create-child-and-convert
1606 widget 'delete-button)))
1609 (setq child (widget-create-child-and-convert
1610 widget type :value value))
1611 (setq child (widget-create-child widget type))))
1613 (error "Unknown escape `%c'" escape)))))
1615 :buttons (cons delete
1617 (widget-get widget :buttons))))
1618 (let ((entry-from (copy-marker (point-min)))
1619 (entry-to (copy-marker (point-max))))
1620 (widget-specify-text entry-from entry-to)
1621 (set-marker-insertion-type entry-from t)
1622 (set-marker-insertion-type entry-to nil)
1623 (widget-put child :entry-from entry-from)
1624 (widget-put child :entry-to entry-to)))
1625 (widget-put insert :widget child)
1626 (widget-put delete :widget child)
1629 ;;; The `group' Widget.
1631 (define-widget 'group 'default
1632 "A widget which group other widgets inside."
1633 :convert-widget 'widget-choice-convert-widget
1635 :value-create 'widget-group-value-create
1636 :value-delete 'widget-radio-value-delete
1637 :value-get 'widget-editable-list-value-get
1638 :validate 'widget-editable-list-validate
1639 :match 'widget-group-match
1640 :match-inline 'widget-group-match-inline)
1642 (defun widget-group-value-create (widget)
1643 ;; Create each component.
1644 (let ((args (widget-get widget :args))
1645 (value (widget-get widget :value))
1646 arg answer children)
1648 (setq arg (car args)
1650 answer (widget-match-inline arg value)
1652 (and (eq (preceding-char) ?\n)
1653 (widget-get widget :indent)
1654 (insert-char ? (widget-get widget :indent)))
1655 (push (cond ((null answer)
1656 (widget-create-child widget arg))
1657 ((widget-get arg :inline)
1658 (widget-create-child-and-convert
1659 widget arg :value (car answer)))
1661 (widget-create-child-and-convert
1662 widget arg :value (car (car answer)))))
1664 (widget-put widget :children (nreverse children))))
1666 (defun widget-group-match (widget values)
1667 ;; Match if the components match.
1669 (let ((match (widget-group-match-inline widget values)))
1670 (and match (null (cdr match))))))
1672 (defun widget-group-match-inline (widget vals)
1673 ;; Match if the components match.
1674 (let ((args (widget-get widget :args))
1675 argument answer found)
1677 (setq argument (car args)
1679 answer (widget-match-inline argument vals))
1681 (setq vals (cdr answer)
1682 found (append found (car answer)))
1689 ;;; The `widget-help' Widget.
1691 (define-widget 'widget-help 'push-button
1692 "The widget documentation button."
1693 :format "%[[%t]%] %d"
1694 :help-echo "Push me to toggle the documentation."
1695 :action 'widget-help-action)
1697 (defun widget-help-action (widget &optional event)
1698 "Toggle documentation for WIDGET."
1699 (let ((old (widget-get widget :doc))
1700 (new (widget-get widget :widget-doc)))
1701 (widget-put widget :doc new)
1702 (widget-put widget :widget-doc old))
1703 (widget-value-set widget (widget-value widget)))
1705 ;;; The Sexp Widgets.
1707 (define-widget 'const 'item
1708 "An immutable sexp."
1711 (define-widget 'function-item 'item
1712 "An immutable function name."
1714 :documentation-property (lambda (symbol)
1716 (documentation symbol t)
1718 :value-delete 'widget-radio-value-delete
1719 :match (lambda (widget value) (symbolp value)))
1721 (define-widget 'variable-item 'item
1722 "An immutable variable name."
1724 :documentation-property 'variable-documentation
1725 :value-delete 'widget-radio-value-delete
1726 :match (lambda (widget value) (symbolp value)))
1728 (define-widget 'string 'editable-field
1731 :format "%[%t%]: %v")
1733 (define-widget 'regexp 'string
1734 "A regular expression."
1735 ;; Should do validation.
1738 (define-widget 'file 'string
1740 It will read a file name from the minibuffer when activated."
1741 :format "%[%t%]: %v"
1743 :action 'widget-file-action)
1745 (defun widget-file-action (widget &optional event)
1746 ;; Read a file name from the minibuffer.
1747 (let* ((value (widget-value widget))
1748 (dir (file-name-directory value))
1749 (file (file-name-nondirectory value))
1750 (menu-tag (widget-apply widget :menu-tag-get))
1751 (must-match (widget-get widget :must-match))
1752 (answer (read-file-name (concat menu-tag ": (defalt `" value "') ")
1753 dir nil must-match file)))
1754 (widget-value-set widget (abbreviate-file-name answer))
1755 (widget-apply widget :notify widget event)
1758 (define-widget 'directory 'file
1759 "A directory widget.
1760 It will read a directory name from the minibuffer when activated."
1763 (define-widget 'symbol 'string
1767 :match (lambda (widget value) (symbolp value))
1768 :value-to-internal (lambda (widget value)
1772 :value-to-external (lambda (widget value)
1777 (define-widget 'function 'symbol
1778 ;; Should complete on functions.
1782 (define-widget 'variable 'symbol
1783 ;; Should complete on variables.
1787 (define-widget 'sexp 'string
1788 "An arbitrary lisp expression."
1789 :tag "Lisp expression"
1791 :validate 'widget-sexp-validate
1792 :match (lambda (widget value) t)
1793 :value-to-internal 'widget-sexp-value-to-internal
1794 :value-to-external (lambda (widget value) (read value)))
1796 (defun widget-sexp-value-to-internal (widget value)
1797 ;; Use pp for printer representation.
1798 (let ((pp (pp-to-string value)))
1799 (while (string-match "\n\\'" pp)
1800 (setq pp (substring pp 0 -1)))
1801 (if (or (string-match "\n\\'" pp)
1806 (defun widget-sexp-validate (widget)
1807 ;; Valid if we can read the string and there is no junk left after it.
1809 (let ((buffer (set-buffer (get-buffer-create " *Widget Scratch*"))))
1811 (insert (widget-apply widget :value-get))
1812 (goto-char (point-min))
1813 (condition-case data
1814 (let ((value (read buffer)))
1816 (if (widget-apply widget :match value)
1818 (widget-put widget :error (widget-get widget :type-error))
1821 :error (format "Junk at end of expression: %s"
1822 (buffer-substring (point)
1825 (error (widget-put widget :error (error-message-string data))
1828 (define-widget 'integer 'sexp
1832 :type-error "This field should contain an integer"
1833 :value-to-internal (lambda (widget value)
1834 (if (integerp value)
1835 (prin1-to-string value)
1837 :match (lambda (widget value) (integerp value)))
1839 (define-widget 'character 'string
1845 :type-error "This field should contain a character"
1846 :value-to-internal (lambda (widget value)
1847 (if (integerp value)
1848 (char-to-string value)
1850 :value-to-external (lambda (widget value)
1854 :match (lambda (widget value) (integerp value)))
1856 (define-widget 'number 'sexp
1857 "A floating point number."
1860 :type-error "This field should contain a number"
1861 :value-to-internal (lambda (widget value)
1863 (prin1-to-string value)
1865 :match (lambda (widget value) (numberp value)))
1867 (define-widget 'hook 'sexp
1871 (define-widget 'list 'group
1876 (define-widget 'vector 'group
1880 :match 'widget-vector-match
1881 :value-to-internal (lambda (widget value) (append value nil))
1882 :value-to-external (lambda (widget value) (apply 'vector value)))
1884 (defun widget-vector-match (widget value)
1885 (and (vectorp value)
1886 (widget-group-match widget
1887 (widget-apply :value-to-internal widget value))))
1889 (define-widget 'cons 'group
1893 :match 'widget-cons-match
1894 :value-to-internal (lambda (widget value)
1895 (list (car value) (cdr value)))
1896 :value-to-external (lambda (widget value)
1897 (cons (nth 0 value) (nth 1 value))))
1899 (defun widget-cons-match (widget value)
1901 (widget-group-match widget
1902 (widget-apply widget :value-to-internal value))))
1904 (define-widget 'choice 'menu-choice
1905 "A union of several sexp types."
1907 :format "%[%t%]: %v")
1909 (define-widget 'radio 'radio-button-choice
1910 "A union of several sexp types."
1914 (define-widget 'repeat 'editable-list
1915 "A variable length homogeneous list."
1917 :format "%t:\n%v%i\n")
1919 (define-widget 'set 'checklist
1920 "A list of members from a fixed set."
1924 (define-widget 'boolean 'toggle
1925 "To be nil or non-nil, that is the question."
1929 ;;; The `color' Widget.
1931 (define-widget 'color-item 'choice-item
1932 "A color name (with sample)."
1933 :format "%v (%[sample%])\n"
1934 :button-face-get 'widget-color-item-button-face-get)
1936 (defun widget-color-item-button-face-get (widget)
1937 ;; We create a face from the value.
1940 (facemenu-get-face (intern (concat "fg:" (widget-value widget))))
1943 (define-widget 'color 'push-button
1944 "Choose a color name (with sample)."
1945 :format "%[%t%]: %v"
1948 :value-create 'widget-color-value-create
1949 :value-delete 'widget-radio-value-delete
1950 :value-get 'widget-color-value-get
1951 :value-set 'widget-color-value-set
1952 :action 'widget-color-action
1953 :match 'widget-field-match
1956 (defvar widget-color-choice-list nil)
1957 ;; Variable holding the possible colors.
1959 (defun widget-color-choice-list ()
1960 (unless widget-color-choice-list
1961 (setq widget-color-choice-list
1962 (mapcar '(lambda (color) (list color))
1963 (x-defined-colors))))
1964 widget-color-choice-list)
1966 (defun widget-color-value-create (widget)
1967 (let ((child (widget-create-child-and-convert
1968 widget 'color-item (widget-get widget :value))))
1969 (widget-put widget :children (list child))))
1971 (defun widget-color-value-get (widget)
1972 ;; Pass command to first child.
1973 (widget-apply (car (widget-get widget :children)) :value-get))
1975 (defun widget-color-value-set (widget value)
1976 ;; Pass command to first child.
1977 (widget-apply (car (widget-get widget :children)) :value-set value))
1979 (defvar widget-color-history nil
1980 "History of entered colors")
1982 (defun widget-color-action (widget &optional event)
1983 ;; Prompt for a color.
1984 (let* ((tag (widget-apply widget :menu-tag-get))
1985 (prompt (concat tag ": "))
1986 (answer (cond ((string-match "XEmacs" emacs-version)
1987 (read-color prompt))
1988 ((fboundp 'x-defined-colors)
1989 (completing-read (concat tag ": ")
1990 (widget-color-choice-list)
1991 nil nil nil 'widget-color-history))
1993 (read-string prompt (widget-value widget))))))
1994 (unless (zerop (length answer))
1995 (widget-value-set widget answer)
1996 (widget-apply widget :notify widget event)
2001 (defun widget-echo-help-mouse ()
2002 "Display the help message for the widget under the mouse.
2003 Enable with (run-with-idle-timer 1 t 'widget-echo-help-mouse)"
2004 (let* ((pos (mouse-position))
2008 (win (window-at x y frame))
2009 (where (coordinates-in-window-p (cons x y) win)))
2011 (save-window-excursion
2012 (progn ; save-excursion
2014 (let* ((result (compute-motion (window-start win)
2019 (cons (window-hscroll) 0)
2021 (when (and (eq (nth 1 result) x)
2022 (eq (nth 2 result) y))
2023 (widget-echo-help (nth 0 result))))))))
2025 (setq track-mouse t)
2026 (add-hook 'post-command-hook 'widget-stop-mouse-tracking)))
2028 (defun widget-stop-mouse-tracking (&rest args)
2029 "Stop the mouse tracking done while idle."
2030 (remove-hook 'post-command-hook 'widget-stop-mouse-tracking)
2031 (setq track-mouse nil))
2033 (defun widget-echo-help (pos)
2034 "Display the help echo for widget at POS."
2035 (let* ((widget (or (get-text-property pos 'button)
2036 (get-text-property pos 'field)))
2037 (help-echo (and widget (widget-get widget :help-echo))))
2038 (cond ((stringp help-echo)
2039 (message "%s" help-echo))
2040 ((and (symbolp help-echo) (fboundp help-echo)
2041 (stringp (setq help-echo (funcall help-echo widget))))
2042 (message "%s" help-echo)))))
2046 (provide 'widget-edit)
2048 ;; widget-edit.el ends here