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 (if (string-match "XEmacs" emacs-version)
22 ;; XEmacs spell `intangible' as `atomic'.
23 (defun widget-make-intangible (from to side)
24 "Make text between FROM and TO atomic with regard to movement.
25 Third argument should be `start-open' if it should be sticky to the rear,
26 and `end-open' if it should sticky to the front."
27 (require 'atomic-extents)
28 (let ((ext (make-extent from to)))
29 ;; XEmacs doesn't understant different kinds of read-only, so
30 ;; we have to use extents instead.
31 (put-text-property from to 'read-only nil)
32 (set-extent-property ext 'read-only t)
33 (set-extent-property ext 'start-open nil)
34 (set-extent-property ext 'end-open nil)
35 (set-extent-property ext side t)
36 (set-extent-property ext 'atomic t)))
37 (defun widget-make-intangible (from to size)
38 "Make text between FROM and TO intangible."
39 (put-text-property from to 'intangible 'front)))
41 ;; The following should go away when bundled with Emacs.
47 (unless (and (featurep 'custom) (fboundp 'custom-declare-variable))
48 ;; We have the old custom-library, hack around it!
49 (defmacro defgroup (&rest args) nil)
50 (defmacro defcustom (&rest args) nil)
51 (defmacro defface (&rest args) nil)
52 (when (fboundp 'copy-face)
53 (copy-face 'default 'widget-documentation-face)
54 (copy-face 'bold 'widget-button-face)
55 (copy-face 'italic 'widget-field-face))
56 (defvar widget-mouse-face 'highlight)
57 (defvar widget-menu-max-size 40)))
61 (unless (fboundp 'event-point)
62 ;; XEmacs function missing in Emacs.
63 (defun event-point (event)
64 "Return the character position of the given mouse-motion, button-press,
65 or button-release event. If the event did not occur over a window, or did
66 not occur over text, then this returns nil. Otherwise, it returns an index
67 into the buffer visible in the event's window."
68 (posn-point (event-start event))))
70 (unless (fboundp 'error-message-string)
71 ;; Emacs function missing in XEmacs.
72 (defun error-message-string (obj)
73 "Convert an error value to an error message."
74 (let ((buf (get-buffer-create " *error-message*")))
76 (display-error obj buf)
77 (buffer-string buf))))
82 "Customization support for the Widget Library."
83 :link '(custom-manual "(widget)Top")
84 :link '(url-link :tag "Development Page"
85 "http://www.dina.kvl.dk/~abraham/custom/")
89 (defface widget-documentation-face '((((class color)
91 (:foreground "lime green"))
94 (:foreground "dark green"))
96 "Face used for documentation text."
99 (defface widget-button-face '((t (:bold t)))
100 "Face used for widget buttons."
103 (defcustom widget-mouse-face 'highlight
104 "Face used for widget buttons when the mouse is above them."
108 (defface widget-field-face '((((class grayscale color)
110 (:background "light gray"))
111 (((class grayscale color)
113 (:background "dark gray"))
116 "Face used for editable fields."
119 (defcustom widget-menu-max-size 40
120 "Largest number of items allowed in a popup-menu.
121 Larger menus are read through the minibuffer."
125 ;;; Utility functions.
127 ;; These are not really widget specific.
129 (defun widget-plist-member (plist prop)
130 ;; Return non-nil if PLIST has the property PROP.
131 ;; PLIST is a property list, which is a list of the form
132 ;; (PROP1 VALUE1 PROP2 VALUE2 ...). PROP is a symbol.
133 ;; Unlike `plist-get', this allows you to distinguish between a missing
134 ;; property and a property with the value nil.
135 ;; The value is actually the tail of PLIST whose car is PROP.
136 (while (and plist (not (eq (car plist) prop)))
137 (setq plist (cdr (cdr plist))))
140 (defun widget-princ-to-string (object)
141 ;; Return string representation of OBJECT, any Lisp object.
142 ;; No quoting characters are used; no delimiters are printed around
143 ;; the contents of strings.
145 (set-buffer (get-buffer-create " *widget-tmp*"))
147 (let ((standard-output (current-buffer)))
151 (defun widget-clear-undo ()
152 "Clear all undo information."
153 (buffer-disable-undo (current-buffer))
154 (buffer-enable-undo))
156 (defun widget-choose (title items &optional event)
157 "Choose an item from a list.
159 First argument TITLE is the name of the list.
160 Second argument ITEMS is an alist (NAME . VALUE).
161 Optional third argument EVENT is an input event.
163 The user is asked to choose between each NAME from the items alist,
164 and the VALUE of the chosen element will be returned. If EVENT is a
165 mouse event, and the number of elements in items is less than
166 `widget-menu-max-size', a popup menu will be used, otherwise the
168 (cond ((and (< (length items) widget-menu-max-size)
169 event (fboundp 'x-popup-menu) window-system)
170 ;; We are in Emacs-19, pressed by the mouse
172 (list title (cons "" items))))
173 ((and (< (length items) widget-menu-max-size)
174 event (fboundp 'popup-menu) window-system)
175 ;; We are in XEmacs, pressed by the mouse
176 (let ((val (get-popup-menu-response
181 (vector (car x) (list (car x)) t)))
184 (listp (event-object val))
185 (stringp (car-safe (event-object val)))
186 (car (event-object val))))
187 (cdr (assoc val items))))
189 (cdr (assoc (completing-read (concat title ": ")
193 ;;; Widget text specifications.
195 ;; These functions are for specifying text properties.
197 (defun widget-specify-none (from to)
198 ;; Clear all text properties between FROM and TO.
199 (set-text-properties from to nil))
201 (defun widget-specify-text (from to)
202 ;; Default properties.
203 (add-text-properties from to (list 'read-only t
207 'rear-nonsticky nil)))
209 (defun widget-specify-field (widget from to)
210 ;; Specify editable button for WIDGET between FROM and TO.
211 (widget-specify-field-update widget from to)
213 ;; Make it possible to edit the front end of the field.
214 (add-text-properties (1- from) from (list 'rear-nonsticky t
217 (when (or (string-match "\\(.\\|\n\\)%v" (widget-get widget :format))
218 (widget-get widget :hide-front-space))
219 ;; WARNING: This is going to lose horrible if the character just
220 ;; before the field can be modified (e.g. if it belongs to a
221 ;; choice widget). We try to compensate by checking the format
222 ;; string, and hope the user hasn't changed the :create method.
223 (widget-make-intangible (- from 2) from 'end-open))
225 ;; Make it possible to edit back end of the field.
226 (add-text-properties to (1+ to) (list 'front-sticky nil
230 (cond ((widget-get widget :size)
231 (put-text-property to (1+ to) 'invisible t)
232 (when (or (string-match "%v\\(.\\|\n\\)" (widget-get widget :format))
233 (widget-get widget :hide-rear-space))
234 ;; WARNING: This is going to lose horrible if the character just
235 ;; after the field can be modified (e.g. if it belongs to a
236 ;; choice widget). We try to compensate by checking the format
237 ;; string, and hope the user hasn't changed the :create method.
238 (widget-make-intangible to (+ to 2) 'start-open)))
239 ((string-match "XEmacs" emacs-version)
240 ;; XEmacs does not allow you to insert before a read-only
241 ;; character, even if it is start.open.
242 ;; XEmacs does allow you to delete an read-only extent, so
243 ;; making the terminating newline read only doesn't help.
244 ;; I tried putting an invisible intangible read-only space
245 ;; before the newline, which gave really weird effects.
246 ;; So for now, we just have trust the user not to delete the
248 (put-text-property to (1+ to) 'read-only nil))))
250 (defun widget-specify-field-update (widget from to)
251 ;; Specify editable button for WIDGET between FROM and TO.
252 (let ((map (widget-get widget :keymap))
253 (secret (widget-get widget :secret))
255 (size (widget-get widget :size))
256 (face (or (widget-get widget :value-face)
257 'widget-field-face)))
263 (eq (char-after (1- secret-to)) ?\ ))
264 (setq secret-to (1- secret-to)))
268 (while (< (point) secret-to)
269 (let ((old (get-text-property (point) 'secret)))
271 (subst-char-in-region (point) (1+ (point)) secret old)))
274 (set-text-properties from to (list 'field widget
283 (while (< (point) secret-to)
284 (let ((old (following-char)))
285 (subst-char-in-region (point) (1+ (point)) old secret)
286 (put-text-property (point) (1+ (point)) 'secret old))
289 (unless (widget-get widget :size)
290 (add-text-properties to (1+ to) (list 'field widget
295 (defun widget-specify-button (widget from to)
296 ;; Specify button for WIDGET between FROM and TO.
297 (let ((face (widget-apply widget :button-face-get)))
298 (add-text-properties from to (list 'button widget
299 'mouse-face widget-mouse-face
304 (defun widget-specify-sample (widget from to)
305 ;; Specify sample for WIDGET between FROM and TO.
306 (let ((face (widget-apply widget :sample-face-get)))
308 (add-text-properties from to (list 'start-open t
312 (defun widget-specify-doc (widget from to)
313 ;; Specify documentation for WIDGET between FROM and TO.
314 (add-text-properties from to (list 'widget-doc widget
315 'face 'widget-documentation-face)))
317 (defmacro widget-specify-insert (&rest form)
318 ;; Execute FORM without inheriting any text properties.
320 (let ((inhibit-read-only t)
322 after-change-functions)
324 (narrow-to-region (- (point) 2) (point))
325 (widget-specify-none (point-min) (point-max))
326 (goto-char (1+ (point-min)))
327 (setq result (progn ,@form))
328 (delete-region (point-min) (1+ (point-min)))
329 (delete-region (1- (point-max)) (point-max))
330 (goto-char (point-max))
333 ;;; Widget Properties.
335 (defun widget-put (widget property value)
336 "In WIDGET set PROPERTY to VALUE.
337 The value can later be retrived with `widget-get'."
338 (setcdr widget (plist-put (cdr widget) property value)))
340 (defun widget-get (widget property)
341 "In WIDGET, get the value of PROPERTY.
342 The value could either be specified when the widget was created, or
343 later with `widget-put'."
344 (cond ((widget-plist-member (cdr widget) property)
345 (plist-get (cdr widget) property))
347 (widget-get (get (car widget) 'widget-type) property))
350 (defun widget-member (widget property)
351 "Non-nil iff there is a definition in WIDGET for PROPERTY."
352 (cond ((widget-plist-member (cdr widget) property)
355 (widget-member (get (car widget) 'widget-type) property))
358 (defun widget-apply (widget property &rest args)
359 "Apply the value of WIDGET's PROPERTY to the widget itself.
360 ARGS are passed as extra argments to the function."
361 (apply (widget-get widget property) widget args))
363 (defun widget-value (widget)
364 "Extract the current value of WIDGET."
366 :value-to-external (widget-apply widget :value-get)))
368 (defun widget-value-set (widget value)
369 "Set the current value of WIDGET to VALUE."
371 :value-set (widget-apply widget
372 :value-to-internal value)))
374 (defun widget-match-inline (widget vals)
375 ;; In WIDGET, match the start of VALS.
376 (cond ((widget-get widget :inline)
377 (widget-apply widget :match-inline vals))
379 (widget-apply widget :match (car vals)))
380 (cons (list (car vals)) (cdr vals)))
383 ;;; Creating Widgets.
386 (defun widget-create (type &rest args)
387 "Create widget of TYPE.
388 The optional ARGS are additional keyword arguments."
389 (let ((widget (apply 'widget-convert type args)))
390 (widget-apply widget :create)
393 (defun widget-create-child-and-convert (parent type &rest args)
394 "As part of the widget PARENT, create a child widget TYPE.
395 The child is converted, using the keyword arguments ARGS."
396 (let ((widget (apply 'widget-convert type args)))
397 (widget-put widget :parent parent)
398 (unless (widget-get widget :indent)
399 (widget-put widget :indent (+ (or (widget-get parent :indent) 0)
400 (or (widget-get widget :extra-offset) 0)
401 (widget-get parent :offset))))
402 (widget-apply widget :create)
405 (defun widget-create-child (parent type)
406 "Create widget of TYPE."
407 (let ((widget (copy-list type)))
408 (widget-put widget :parent parent)
409 (unless (widget-get widget :indent)
410 (widget-put widget :indent (+ (or (widget-get parent :indent) 0)
411 (or (widget-get widget :extra-offset) 0)
412 (widget-get parent :offset))))
413 (widget-apply widget :create)
416 (defun widget-create-child-value (parent type value)
417 "Create widget of TYPE with value VALUE."
418 (let ((widget (copy-list type)))
419 (widget-put widget :value (widget-apply widget :value-to-internal value))
420 (widget-put widget :parent parent)
421 (unless (widget-get widget :indent)
422 (widget-put widget :indent (+ (or (widget-get parent :indent) 0)
423 (or (widget-get widget :extra-offset) 0)
424 (widget-get parent :offset))))
425 (widget-apply widget :create)
429 (defun widget-delete (widget)
431 (widget-apply widget :delete))
433 (defun widget-convert (type &rest args)
434 "Convert TYPE to a widget without inserting it in the buffer.
435 The optional ARGS are additional keyword arguments."
436 ;; Don't touch the type.
437 (let* ((widget (if (symbolp type)
442 ;; First set the :args keyword.
443 (while (cdr current) ;Look in the type.
444 (let ((next (car (cdr current))))
445 (if (and (symbolp next) (eq (aref (symbol-name next) 0) ?:))
446 (setq current (cdr (cdr current)))
447 (setcdr current (list :args (cdr current)))
448 (setq current nil))))
449 (while args ;Look in the args.
450 (let ((next (nth 0 args)))
451 (if (and (symbolp next) (eq (aref (symbol-name next) 0) ?:))
452 (setq args (nthcdr 2 args))
453 (widget-put widget :args args)
455 ;; Then Convert the widget.
458 (let ((convert-widget (plist-get (cdr type) :convert-widget)))
460 (setq widget (funcall convert-widget widget))))
461 (setq type (get (car type) 'widget-type)))
462 ;; Finally set the keyword args.
464 (let ((next (nth 0 keys)))
465 (if (and (symbolp next) (eq (aref (symbol-name next) 0) ?:))
467 (widget-put widget next (nth 1 keys))
468 (setq keys (nthcdr 2 keys)))
470 ;; Convert the :value to internal format.
471 (if (widget-member widget :value)
472 (let ((value (widget-get widget :value)))
474 :value (widget-apply widget :value-to-internal value))))
475 ;; Return the newly create widget.
478 (defun widget-insert (&rest args)
479 "Call `insert' with ARGS and make the text read only."
480 (let ((inhibit-read-only t)
481 after-change-functions
484 (widget-specify-text from (point))))
486 ;;; Keymap and Comands.
488 (defvar widget-keymap nil
489 "Keymap containing useful binding for buffers containing widgets.
490 Recommended as a parent keymap for modes using widgets.")
492 (unless widget-keymap
493 (setq widget-keymap (make-sparse-keymap))
494 (define-key widget-keymap "\t" 'widget-forward)
495 (define-key widget-keymap "\M-\t" 'widget-backward)
496 (define-key widget-keymap [(shift tab)] 'widget-backward)
497 (define-key widget-keymap [(shift tab)] 'widget-backward)
498 (define-key widget-keymap [backtab] 'widget-backward)
499 (if (string-match "XEmacs" (emacs-version))
500 (define-key widget-keymap [button2] 'widget-button-click)
501 (define-key widget-keymap [menu-bar] 'nil)
502 (define-key widget-keymap [mouse-2] 'widget-button-click))
503 (define-key widget-keymap "\C-m" 'widget-button-press))
505 (defvar widget-global-map global-map
506 "Keymap used for events the widget does not handle themselves.")
507 (make-variable-buffer-local 'widget-global-map)
509 (defvar widget-field-keymap nil
510 "Keymap used inside an editable field.")
512 (unless widget-field-keymap
513 (setq widget-field-keymap (copy-keymap widget-keymap))
514 (define-key widget-field-keymap "\C-m" 'widget-field-activate)
515 (set-keymap-parent widget-field-keymap global-map))
517 (defvar widget-text-keymap nil
518 "Keymap used inside a text field.")
520 (unless widget-text-keymap
521 (setq widget-text-keymap (copy-keymap widget-keymap))
522 (set-keymap-parent widget-text-keymap global-map))
524 (defun widget-field-activate (pos &optional event)
525 "Activate the ediable field at point."
527 (let* ((field (get-text-property pos 'field)))
529 (widget-apply field :action event)
531 (lookup-key widget-global-map (this-command-keys))))))
533 (defun widget-button-click (event)
534 "Activate button below mouse pointer."
536 (widget-button-press (event-point event) event))
538 (defun widget-button-press (pos &optional event)
539 "Activate button at POS."
541 (let* ((button (get-text-property pos 'button)))
543 (widget-apply button :action event)
545 (lookup-key widget-global-map (this-command-keys))))))
547 (defun widget-move (arg)
548 "Move point to the ARG next field or button.
549 ARG may be negative to move backward."
552 (let ((next (cond ((get-text-property (point) 'button)
553 (next-single-property-change (point) 'button))
554 ((get-text-property (point) 'field)
555 (next-single-property-change (point) 'field))
558 (if (null next) ; Widget extends to end. of buffer
559 (setq next (point-min)))
560 (let ((button (next-single-property-change next 'button))
561 (field (next-single-property-change next 'field)))
562 (cond ((or (get-text-property next 'button)
563 (get-text-property next 'field))
566 (goto-char (min button field)))
567 (button (goto-char button))
568 (field (goto-char field))
570 (let ((button (next-single-property-change (point-min) 'button))
571 (field (next-single-property-change (point-min) 'field)))
572 (cond ((and button field) (goto-char (min button field)))
573 (button (goto-char button))
574 (field (goto-char field))
576 (error "No buttons or fields found")))))))))
578 (if (= (point-min) (point))
581 (let ((previous (cond ((get-text-property (1- (point)) 'button)
582 (previous-single-property-change (point) 'button))
583 ((get-text-property (1- (point)) 'field)
584 (previous-single-property-change (point) 'field))
587 (if (null previous) ; Widget extends to beg. of buffer
588 (setq previous (point-max)))
589 (let ((button (previous-single-property-change previous 'button))
590 (field (previous-single-property-change previous 'field)))
591 (cond ((and button field)
592 (goto-char (max button field)))
593 (button (goto-char button))
594 (field (goto-char field))
596 (let ((button (previous-single-property-change
597 (point-max) 'button))
598 (field (previous-single-property-change
599 (point-max) 'field)))
600 (cond ((and button field) (goto-char (max button field)))
601 (button (goto-char button))
602 (field (goto-char field))
604 (error "No buttons or fields found"))))))))
605 (let ((button (previous-single-property-change (point) 'button))
606 (field (previous-single-property-change (point) 'field)))
607 (cond ((and button field)
608 (goto-char (max button field)))
609 (button (goto-char button))
610 (field (goto-char field)))))
611 (widget-echo-help (point))
612 (run-hooks 'widget-move-hook))
614 (defun widget-forward (arg)
615 "Move point to the next field or button.
616 With optional ARG, move across that many fields."
618 (run-hooks 'widget-forward-hook)
621 (defun widget-backward (arg)
622 "Move point to the previous field or button.
623 With optional ARG, move across that many fields."
625 (run-hooks 'widget-backward-hook)
626 (widget-move (- arg)))
628 ;;; Setting up the buffer.
630 (defvar widget-field-new nil)
631 ;; List of all newly created editable fields in the buffer.
632 (make-variable-buffer-local 'widget-field-new)
634 (defvar widget-field-list nil)
635 ;; List of all editable fields in the buffer.
636 (make-variable-buffer-local 'widget-field-list)
638 (defun widget-setup ()
639 "Setup current buffer so editing string widgets works."
640 (let ((inhibit-read-only t)
641 (after-change-functions nil)
643 (while widget-field-new
644 (setq field (car widget-field-new)
645 widget-field-new (cdr widget-field-new)
646 widget-field-list (cons field widget-field-list))
647 (let ((from (widget-get field :value-from))
648 (to (widget-get field :value-to)))
649 (widget-specify-field field from to)
650 (move-marker from (1- from))
651 (move-marker to (1+ to)))))
653 ;; We need to maintain text properties and size of the editing fields.
654 (make-local-variable 'after-change-functions)
655 (if widget-field-list
656 (setq after-change-functions '(widget-after-change))
657 (setq after-change-functions nil)))
659 (defvar widget-field-last nil)
660 ;; Last field containing point.
661 (make-variable-buffer-local 'widget-field-last)
663 (defvar widget-field-was nil)
664 ;; The widget data before the change.
665 (make-variable-buffer-local 'widget-field-was)
667 (defun widget-field-find (pos)
668 ;; Find widget whose editing field is located at POS.
669 ;; Return nil if POS is not inside and editing field.
671 ;; This is only used in `widget-field-modified', since ordinarily
672 ;; you would just test the field property.
673 (let ((fields widget-field-list)
676 (setq field (car fields)
678 (let ((from (widget-get field :value-from))
679 (to (widget-get field :value-to)))
680 (if (and from to (< from pos) (> to pos))
685 (defun widget-after-change (from to old)
686 ;; Adjust field size and text properties.
688 (let ((field (widget-field-find from))
689 (inhibit-read-only t))
691 ((not (eq field (widget-field-find to)))
693 (message "Error: `widget-after-change' called on two fields"))
695 (let ((size (widget-get field :size)))
697 (let ((begin (1+ (widget-get field :value-from)))
698 (end (1- (widget-get field :value-to))))
699 (widget-specify-field-update field begin end)
700 (cond ((< (- end begin) size)
704 (insert-char ?\ (- (+ begin size) end))
705 (widget-specify-field-update field
708 ((> (- end begin) size)
709 ;; Field too large and
710 (if (or (< (point) (+ begin size))
712 ;; Point is outside extra space.
713 (setq begin (+ begin size))
714 ;; Point is within the extra space.
715 (setq begin (point)))
718 (while (and (eq (preceding-char) ?\ )
720 (delete-backward-char 1))))))
721 (widget-specify-field-update field from to)))
722 (widget-apply field :notify field))))
727 ;; These functions are used in the definition of multiple widgets.
729 (defun widget-children-value-delete (widget)
730 "Delete all :children and :buttons in WIDGET."
731 (mapcar 'widget-delete (widget-get widget :children))
732 (widget-put widget :children nil)
733 (mapcar 'widget-delete (widget-get widget :buttons))
734 (widget-put widget :buttons nil))
736 (defun widget-types-convert-widget (widget)
737 "Convert :args as widget types in WIDGET."
738 (widget-put widget :args (mapcar 'widget-convert (widget-get widget :args)))
741 ;;; The `default' Widget.
743 (define-widget 'default nil
744 "Basic widget other widgets are derived from."
745 :value-to-internal (lambda (widget value) value)
746 :value-to-external (lambda (widget value) value)
747 :create 'widget-default-create
750 :format-handler 'widget-default-format-handler
751 :button-face-get 'widget-default-button-face-get
752 :sample-face-get 'widget-default-sample-face-get
753 :delete 'widget-default-delete
754 :value-set 'widget-default-value-set
755 :value-inline 'widget-default-value-inline
756 :menu-tag-get 'widget-default-menu-tag-get
757 :validate (lambda (widget) nil)
758 :action 'widget-default-action
759 :notify 'widget-default-notify)
761 (defun widget-default-create (widget)
762 "Create WIDGET at point in the current buffer."
763 (widget-specify-insert
765 (tag (widget-get widget :tag))
766 (doc (widget-get widget :doc))
767 button-begin button-end
768 sample-begin sample-end
771 (insert (widget-get widget :format))
773 ;; Parse % escapes in format.
774 (while (re-search-forward "%\\(.\\)" nil t)
775 (let ((escape (aref (match-string 1) 0)))
776 (replace-match "" t t)
777 (cond ((eq escape ?%)
780 (setq button-begin (point)))
782 (setq button-end (point)))
784 (setq sample-begin (point)))
786 (setq sample-end (point)))
788 (when (widget-get widget :indent)
790 (insert-char ? (widget-get widget :indent))))
794 (let ((standard-output (current-buffer)))
795 (princ (widget-get widget :value)))))
798 (setq doc-begin (point))
800 (while (eq (preceding-char) ?\n)
801 (delete-backward-char 1))
803 (setq doc-end (point))))
805 (if (and button-begin (not button-end))
806 (widget-apply widget :value-create)
807 (setq value-pos (point))))
809 (widget-apply widget :format-handler escape)))))
810 ;; Specify button, sample, and doc, and insert value.
811 (and button-begin button-end
812 (widget-specify-button widget button-begin button-end))
813 (and sample-begin sample-end
814 (widget-specify-sample widget sample-begin sample-end))
815 (and doc-begin doc-end
816 (widget-specify-doc widget doc-begin doc-end))
818 (goto-char value-pos)
819 (widget-apply widget :value-create)))
820 (let ((from (copy-marker (point-min)))
821 (to (copy-marker (point-max))))
822 (widget-specify-text from to)
823 (set-marker-insertion-type from t)
824 (set-marker-insertion-type to nil)
825 (widget-put widget :from from)
826 (widget-put widget :to to))))
828 (defun widget-default-format-handler (widget escape)
829 ;; We recognize the %h escape by default.
830 (let* ((buttons (widget-get widget :buttons))
831 (doc-property (widget-get widget :documentation-property))
832 (doc-try (cond ((widget-get widget :doc))
833 ((symbolp doc-property)
834 (documentation-property (widget-get widget :value)
837 (funcall doc-property (widget-get widget :value)))))
838 (doc-text (and (stringp doc-try)
839 (> (length doc-try) 1)
841 (cond ((eq escape ?h)
843 (and (eq (preceding-char) ?\n)
844 (widget-get widget :indent)
845 (insert-char ? (widget-get widget :indent)))
846 ;; The `*' in the beginning is redundant.
847 (when (eq (aref doc-text 0) ?*)
848 (setq doc-text (substring doc-text 1)))
849 ;; Get rid of trailing newlines.
850 (when (string-match "\n+\\'" doc-text)
851 (setq doc-text (substring doc-text 0 (match-beginning 0))))
852 (push (if (string-match "\n." doc-text)
853 ;; Allow multiline doc to be hiden.
854 (widget-create-child-and-convert
857 (string-match "\\`.*" doc-text)
858 (match-string 0 doc-text))
861 ;; A single line is just inserted.
862 (widget-create-child-and-convert
863 widget 'item :format "%d" :doc doc-text nil))
866 (error "Unknown escape `%c'" escape)))
867 (widget-put widget :buttons buttons)))
869 (defun widget-default-button-face-get (widget)
870 ;; Use :button-face or widget-button-face
871 (or (widget-get widget :button-face) 'widget-button-face))
873 (defun widget-default-sample-face-get (widget)
875 (widget-get widget :sample-face))
877 (defun widget-default-delete (widget)
878 ;; Remove widget from the buffer.
879 (let ((from (widget-get widget :from))
880 (to (widget-get widget :to))
881 (inhibit-read-only t)
882 after-change-functions)
883 (widget-apply widget :value-delete)
884 (delete-region from to)
885 (set-marker from nil)
886 (set-marker to nil)))
888 (defun widget-default-value-set (widget value)
889 ;; Recreate widget with new value.
891 (goto-char (widget-get widget :from))
892 (widget-apply widget :delete)
893 (widget-put widget :value value)
894 (widget-apply widget :create)))
896 (defun widget-default-value-inline (widget)
897 ;; Wrap value in a list unless it is inline.
898 (if (widget-get widget :inline)
899 (widget-value widget)
900 (list (widget-value widget))))
902 (defun widget-default-menu-tag-get (widget)
903 ;; Use tag or value for menus.
904 (or (widget-get widget :menu-tag)
905 (widget-get widget :tag)
906 (widget-princ-to-string (widget-get widget :value))))
908 (defun widget-default-action (widget &optional event)
909 ;; Notify the parent when a widget change
910 (let ((parent (widget-get widget :parent)))
912 (widget-apply parent :notify widget event))))
914 (defun widget-default-notify (widget child &optional event)
915 ;; Pass notification to parent.
916 (widget-default-action widget event))
918 ;;; The `item' Widget.
920 (define-widget 'item 'default
921 "Constant items for inclusion in other widgets."
922 :convert-widget 'widget-item-convert-widget
923 :value-create 'widget-item-value-create
924 :value-delete 'ignore
925 :value-get 'widget-item-value-get
926 :match 'widget-item-match
927 :match-inline 'widget-item-match-inline
928 :action 'widget-item-action
931 (defun widget-item-convert-widget (widget)
932 ;; Initialize :value from :args in WIDGET.
933 (let ((args (widget-get widget :args)))
935 (widget-put widget :value (widget-apply widget
936 :value-to-internal (car args)))
937 (widget-put widget :args nil)))
940 (defun widget-item-value-create (widget)
941 ;; Insert the printed representation of the value.
942 (let ((standard-output (current-buffer)))
943 (princ (widget-get widget :value))))
945 (defun widget-item-match (widget value)
946 ;; Match if the value is the same.
947 (equal (widget-get widget :value) value))
949 (defun widget-item-match-inline (widget values)
950 ;; Match if the value is the same.
951 (let ((value (widget-get widget :value)))
953 (<= (length value) (length values))
954 (let ((head (subseq values 0 (length value))))
955 (and (equal head value)
956 (cons head (subseq values (length value))))))))
958 (defun widget-item-action (widget &optional event)
959 ;; Just notify itself.
960 (widget-apply widget :notify widget event))
962 (defun widget-item-value-get (widget)
964 (widget-get widget :value))
966 ;;; The `push-button' Widget.
968 (define-widget 'push-button 'item
972 ;;; The `link' Widget.
974 (define-widget 'link 'item
976 :help-echo "Push me to follow the link."
979 ;;; The `info-link' Widget.
981 (define-widget 'info-link 'link
982 "A link to an info file."
983 :action 'widget-info-link-action)
985 (defun widget-info-link-action (widget &optional event)
986 "Open the info node specified by WIDGET."
987 (Info-goto-node (widget-value widget)))
989 ;;; The `url-link' Widget.
991 (define-widget 'url-link 'link
992 "A link to an www page."
993 :action 'widget-url-link-action)
995 (defun widget-url-link-action (widget &optional event)
996 "Open the url specified by WIDGET."
997 (require 'browse-url)
998 (funcall browse-url-browser-function (widget-value widget)))
1000 ;;; The `editable-field' Widget.
1002 (define-widget 'editable-field 'default
1003 "An editable text field."
1004 :convert-widget 'widget-item-convert-widget
1005 :keymap widget-field-keymap
1008 :action 'widget-field-action
1009 :validate 'widget-field-validate
1012 :value-create 'widget-field-value-create
1013 :value-delete 'widget-field-value-delete
1014 :value-get 'widget-field-value-get
1015 :match 'widget-field-match)
1017 ;; History of field minibuffer edits.
1018 (defvar widget-field-history nil)
1020 (defun widget-field-action (widget &optional event)
1021 ;; Edit the value in the minibuffer.
1022 (let ((tag (widget-apply widget :menu-tag-get))
1023 (invalid (widget-apply widget :validate)))
1025 (error (widget-get invalid :error)))
1026 (widget-value-set widget
1027 (widget-apply widget
1029 (read-string (concat tag ": ")
1033 (widget-value widget))
1034 'widget-field-history)))
1035 (widget-apply widget :notify widget event)
1038 (defun widget-field-validate (widget)
1039 ;; Valid if the content matches `:valid-regexp'.
1041 (let ((value (widget-apply widget :value-get))
1042 (regexp (widget-get widget :valid-regexp)))
1043 (if (string-match regexp value)
1047 (defun widget-field-value-create (widget)
1048 ;; Create an editable text field.
1050 (let ((size (widget-get widget :size))
1051 (value (widget-get widget :value))
1055 (< (length value) size)
1056 (insert-char ?\ (- size (length value))))
1057 (unless (memq widget widget-field-list)
1058 (setq widget-field-new (cons widget widget-field-new)))
1059 (widget-put widget :value-to (copy-marker (point)))
1060 (set-marker-insertion-type (widget-get widget :value-to) nil)
1064 (widget-put widget :value-from (copy-marker from))
1065 (set-marker-insertion-type (widget-get widget :value-from) t)))
1067 (defun widget-field-value-delete (widget)
1068 ;; Remove the widget from the list of active editing fields.
1069 (setq widget-field-list (delq widget widget-field-list))
1070 ;; These are nil if the :format string doesn't contain `%v'.
1071 (when (widget-get widget :value-from)
1072 (set-marker (widget-get widget :value-from) nil))
1073 (when (widget-get widget :value-from)
1074 (set-marker (widget-get widget :value-to) nil)))
1076 (defun widget-field-value-get (widget)
1077 ;; Return current text in editing field.
1078 (let ((from (widget-get widget :value-from))
1079 (to (widget-get widget :value-to))
1080 (size (widget-get widget :size))
1081 (secret (widget-get widget :secret))
1082 (old (current-buffer)))
1085 (set-buffer (marker-buffer from))
1086 (setq from (1+ from)
1091 (eq (char-after (1- to)) ?\ ))
1093 (let ((result (buffer-substring-no-properties from to)))
1096 (while (< (+ from index) to)
1098 (get-text-property (+ from index) 'secret))
1099 (setq index (1+ index)))))
1102 (widget-get widget :value))))
1104 (defun widget-field-match (widget value)
1105 ;; Match any string.
1108 ;;; The `text' Widget.
1110 (define-widget 'text 'editable-field
1111 :keymap widget-text-keymap
1112 "A multiline text area.")
1114 ;;; The `menu-choice' Widget.
1116 (define-widget 'menu-choice 'default
1117 "A menu of options."
1118 :convert-widget 'widget-types-convert-widget
1119 :format "%[%t%]: %v"
1122 :void '(item :format "invalid (%t)\n")
1123 :value-create 'widget-choice-value-create
1124 :value-delete 'widget-children-value-delete
1125 :value-get 'widget-choice-value-get
1126 :value-inline 'widget-choice-value-inline
1127 :action 'widget-choice-action
1128 :error "Make a choice"
1129 :validate 'widget-choice-validate
1130 :match 'widget-choice-match
1131 :match-inline 'widget-choice-match-inline)
1133 (defun widget-choice-value-create (widget)
1134 ;; Insert the first choice that matches the value.
1135 (let ((value (widget-get widget :value))
1136 (args (widget-get widget :args))
1139 (setq current (car args)
1141 (when (widget-apply current :match value)
1142 (widget-put widget :children (list (widget-create-child-value
1143 widget current value)))
1144 (widget-put widget :choice current)
1148 (let ((void (widget-get widget :void)))
1149 (widget-put widget :children (list (widget-create-child-and-convert
1150 widget void :value value)))
1151 (widget-put widget :choice void)))))
1153 (defun widget-choice-value-get (widget)
1154 ;; Get value of the child widget.
1155 (widget-value (car (widget-get widget :children))))
1157 (defun widget-choice-value-inline (widget)
1158 ;; Get value of the child widget.
1159 (widget-apply (car (widget-get widget :children)) :value-inline))
1161 (defun widget-choice-action (widget &optional event)
1163 (let ((args (widget-get widget :args))
1164 (old (widget-get widget :choice))
1165 (tag (widget-apply widget :menu-tag-get))
1166 (completion-ignore-case (widget-get widget :case-fold))
1168 ;; Remember old value.
1169 (if (and old (not (widget-apply widget :validate)))
1170 (let* ((external (widget-value widget))
1171 (internal (widget-apply old :value-to-internal external)))
1172 (widget-put old :value internal)))
1175 (cond ((= (length args) 0)
1177 ((= (length args) 1)
1179 ((and (= (length args) 2)
1181 (if (eq old (nth 0 args))
1186 (setq current (car args)
1189 (cons (cons (widget-apply current :menu-tag-get)
1192 (widget-choose tag (reverse choices) event))))
1194 (widget-value-set widget
1195 (widget-apply current :value-to-external
1196 (widget-get current :value)))
1197 (widget-apply widget :notify widget event)
1200 (widget-apply widget :notify widget event)
1201 (widget-clear-undo))
1203 (defun widget-choice-validate (widget)
1204 ;; Valid if we have made a valid choice.
1205 (let ((void (widget-get widget :void))
1206 (choice (widget-get widget :choice))
1207 (child (car (widget-get widget :children))))
1208 (if (eq void choice)
1210 (widget-apply child :validate))))
1212 (defun widget-choice-match (widget value)
1213 ;; Matches if one of the choices matches.
1214 (let ((args (widget-get widget :args))
1216 (while (and args (not found))
1217 (setq current (car args)
1219 found (widget-apply current :match value)))
1222 (defun widget-choice-match-inline (widget values)
1223 ;; Matches if one of the choices matches.
1224 (let ((args (widget-get widget :args))
1226 (while (and args (null found))
1227 (setq current (car args)
1229 found (widget-match-inline current values)))
1232 ;;; The `toggle' Widget.
1234 (define-widget 'toggle 'menu-choice
1235 "Toggle between two states."
1236 :convert-widget 'widget-toggle-convert-widget
1241 (defun widget-toggle-convert-widget (widget)
1242 ;; Create the types representing the `on' and `off' states.
1243 (let ((on-type (widget-get widget :on-type))
1244 (off-type (widget-get widget :off-type)))
1249 :match (lambda (widget value) value)
1250 :tag (widget-get widget :on))))
1253 (list 'choice-item :value nil :tag (widget-get widget :off))))
1254 (widget-put widget :args (list on-type off-type)))
1257 ;;; The `checkbox' Widget.
1259 (define-widget 'checkbox 'toggle
1260 "A checkbox toggle."
1261 :convert-widget 'widget-item-convert-widget
1262 :on-type '(choice-item :format "%[[X]%]" t)
1263 :off-type '(choice-item :format "%[[ ]%]" nil))
1265 ;;; The `checklist' Widget.
1267 (define-widget 'checklist 'default
1268 "A multiple choice widget."
1269 :convert-widget 'widget-types-convert-widget
1272 :entry-format "%b %v"
1273 :menu-tag "checklist"
1275 :value-create 'widget-checklist-value-create
1276 :value-delete 'widget-children-value-delete
1277 :value-get 'widget-checklist-value-get
1278 :validate 'widget-checklist-validate
1279 :match 'widget-checklist-match
1280 :match-inline 'widget-checklist-match-inline)
1282 (defun widget-checklist-value-create (widget)
1283 ;; Insert all values
1284 (let ((alist (widget-checklist-match-find widget (widget-get widget :value)))
1285 (args (widget-get widget :args)))
1287 (widget-checklist-add-item widget (car args) (assq (car args) alist))
1288 (setq args (cdr args)))
1289 (widget-put widget :children (nreverse (widget-get widget :children)))))
1291 (defun widget-checklist-add-item (widget type chosen)
1292 ;; Create checklist item in WIDGET of type TYPE.
1293 ;; If the item is checked, CHOSEN is a cons whose cdr is the value.
1294 (and (eq (preceding-char) ?\n)
1295 (widget-get widget :indent)
1296 (insert-char ? (widget-get widget :indent)))
1297 (widget-specify-insert
1298 (let* ((children (widget-get widget :children))
1299 (buttons (widget-get widget :buttons))
1302 (insert (widget-get widget :entry-format))
1304 ;; Parse % escapes in format.
1305 (while (re-search-forward "%\\([bv%]\\)" nil t)
1306 (let ((escape (aref (match-string 1) 0)))
1307 (replace-match "" t t)
1308 (cond ((eq escape ?%)
1311 (setq button (widget-create-child-and-convert
1312 widget 'checkbox :value (not (null chosen)))))
1316 (widget-create-child widget type))
1317 ((widget-get type :inline)
1318 (widget-create-child-value
1319 widget type (cdr chosen)))
1321 (widget-create-child-value
1322 widget type (car (cdr chosen)))))))
1324 (error "Unknown escape `%c'" escape)))))
1325 ;; Update properties.
1326 (and button child (widget-put child :button button))
1327 (and button (widget-put widget :buttons (cons button buttons)))
1328 (and child (widget-put widget :children (cons child children))))))
1330 (defun widget-checklist-match (widget values)
1331 ;; All values must match a type in the checklist.
1333 (null (cdr (widget-checklist-match-inline widget values)))))
1335 (defun widget-checklist-match-inline (widget values)
1336 ;; Find the values which match a type in the checklist.
1337 (let ((greedy (widget-get widget :greedy))
1338 (args (copy-list (widget-get widget :args)))
1341 (let ((answer (widget-checklist-match-up args values)))
1343 (let ((vals (widget-match-inline answer values)))
1344 (setq found (append found (car vals))
1346 args (delq answer args))))
1348 (setq rest (append rest (list (car values)))
1349 values (cdr values)))
1351 (setq rest (append rest values)
1355 (defun widget-checklist-match-find (widget vals)
1356 ;; Find the vals which match a type in the checklist.
1357 ;; Return an alist of (TYPE MATCH).
1358 (let ((greedy (widget-get widget :greedy))
1359 (args (copy-list (widget-get widget :args)))
1362 (let ((answer (widget-checklist-match-up args vals)))
1364 (let ((match (widget-match-inline answer vals)))
1365 (setq found (cons (cons answer (car match)) found)
1367 args (delq answer args))))
1369 (setq vals (cdr vals)))
1374 (defun widget-checklist-match-up (args vals)
1375 ;; Rerturn the first type from ARGS that matches VALS.
1376 (let (current found)
1377 (while (and args (null found))
1378 (setq current (car args)
1380 found (widget-match-inline current vals)))
1385 (defun widget-checklist-value-get (widget)
1386 ;; The values of all selected items.
1387 (let ((children (widget-get widget :children))
1390 (setq child (car children)
1391 children (cdr children))
1392 (if (widget-value (widget-get child :button))
1393 (setq result (append result (widget-apply child :value-inline)))))
1396 (defun widget-checklist-validate (widget)
1397 ;; Ticked chilren must be valid.
1398 (let ((children (widget-get widget :children))
1400 (while (and children (not found))
1401 (setq child (car children)
1402 children (cdr children)
1403 button (widget-get child :button)
1404 found (and (widget-value button)
1405 (widget-apply child :validate))))
1408 ;;; The `option' Widget
1410 (define-widget 'option 'checklist
1411 "An widget with an optional item."
1414 ;;; The `choice-item' Widget.
1416 (define-widget 'choice-item 'item
1417 "Button items that delegate action events to their parents."
1418 :action 'widget-choice-item-action
1419 :format "%[%t%] \n")
1421 (defun widget-choice-item-action (widget &optional event)
1422 ;; Tell parent what happened.
1423 (widget-apply (widget-get widget :parent) :action event))
1425 ;;; The `radio-button' Widget.
1427 (define-widget 'radio-button 'toggle
1428 "A radio button for use in the `radio' widget."
1429 :notify 'widget-radio-button-notify
1430 :on-type '(choice-item :format "%[(*)%]" t)
1431 :off-type '(choice-item :format "%[( )%]" nil))
1433 (defun widget-radio-button-notify (widget child &optional event)
1434 ;; Notify the parent.
1435 (widget-apply (widget-get widget :parent) :action widget event))
1437 ;;; The `radio-button-choice' Widget.
1439 (define-widget 'radio-button-choice 'default
1440 "Select one of multiple options."
1441 :convert-widget 'widget-types-convert-widget
1444 :entry-format "%b %v"
1446 :value-create 'widget-radio-value-create
1447 :value-delete 'widget-children-value-delete
1448 :value-get 'widget-radio-value-get
1449 :value-inline 'widget-radio-value-inline
1450 :value-set 'widget-radio-value-set
1451 :error "You must push one of the buttons"
1452 :validate 'widget-radio-validate
1453 :match 'widget-choice-match
1454 :match-inline 'widget-choice-match-inline
1455 :action 'widget-radio-action)
1457 (defun widget-radio-value-create (widget)
1458 ;; Insert all values
1459 (let ((args (widget-get widget :args))
1462 (setq arg (car args)
1464 (widget-radio-add-item widget arg))))
1466 (defun widget-radio-add-item (widget type)
1467 "Add to radio widget WIDGET a new radio button item of type TYPE."
1468 ;; (setq type (widget-convert type))
1469 (and (eq (preceding-char) ?\n)
1470 (widget-get widget :indent)
1471 (insert-char ? (widget-get widget :indent)))
1472 (widget-specify-insert
1473 (let* ((value (widget-get widget :value))
1474 (children (widget-get widget :children))
1475 (buttons (widget-get widget :buttons))
1477 (chosen (and (null (widget-get widget :choice))
1478 (widget-apply type :match value)))
1480 (insert (widget-get widget :entry-format))
1482 ;; Parse % escapes in format.
1483 (while (re-search-forward "%\\([bv%]\\)" nil t)
1484 (let ((escape (aref (match-string 1) 0)))
1485 (replace-match "" t t)
1486 (cond ((eq escape ?%)
1489 (setq button (widget-create-child-and-convert
1490 widget 'radio-button
1491 :value (not (null chosen)))))
1493 (setq child (if chosen
1494 (widget-create-child-value
1496 (widget-create-child widget type))))
1498 (error "Unknown escape `%c'" escape)))))
1499 ;; Update properties.
1501 (widget-put widget :choice type))
1503 (widget-put child :button button)
1504 (widget-put widget :buttons (nconc buttons (list button))))
1506 (widget-put widget :children (nconc children (list child))))
1509 (defun widget-radio-value-get (widget)
1510 ;; Get value of the child widget.
1511 (let ((chosen (widget-radio-chosen widget)))
1512 (and chosen (widget-value chosen))))
1514 (defun widget-radio-chosen (widget)
1515 "Return the widget representing the chosen radio button."
1516 (let ((children (widget-get widget :children))
1519 (setq current (car children)
1520 children (cdr children))
1521 (let* ((button (widget-get current :button))
1522 (value (widget-apply button :value-get)))
1528 (defun widget-radio-value-inline (widget)
1529 ;; Get value of the child widget.
1530 (let ((children (widget-get widget :children))
1533 (setq current (car children)
1534 children (cdr children))
1535 (let* ((button (widget-get current :button))
1536 (value (widget-apply button :value-get)))
1538 (setq found (widget-apply current :value-inline)
1542 (defun widget-radio-value-set (widget value)
1543 ;; We can't just delete and recreate a radio widget, since children
1544 ;; can be added after the original creation and won't be recreated
1546 (let ((children (widget-get widget :children))
1549 (setq current (car children)
1550 children (cdr children))
1551 (let* ((button (widget-get current :button))
1552 (match (and (not found)
1553 (widget-apply current :match value))))
1554 (widget-value-set button match)
1556 (widget-value-set current value))
1557 (setq found (or found match))))))
1559 (defun widget-radio-validate (widget)
1560 ;; Valid if we have made a valid choice.
1561 (let ((children (widget-get widget :children))
1562 current found button)
1563 (while (and children (not found))
1564 (setq current (car children)
1565 children (cdr children)
1566 button (widget-get current :button)
1567 found (widget-apply button :value-get)))
1569 (widget-apply current :validate)
1572 (defun widget-radio-action (widget child event)
1573 ;; Check if a radio button was pressed.
1574 (let ((children (widget-get widget :children))
1575 (buttons (widget-get widget :buttons))
1577 (when (memq child buttons)
1579 (setq current (car children)
1580 children (cdr children))
1581 (let* ((button (widget-get current :button)))
1582 (cond ((eq child button)
1583 (widget-value-set button t))
1584 ((widget-value button)
1585 (widget-value-set button nil)))))))
1586 ;; Pass notification to parent.
1587 (widget-apply widget :notify child event))
1589 ;;; The `insert-button' Widget.
1591 (define-widget 'insert-button 'push-button
1592 "An insert button for the `editable-list' widget."
1594 :action 'widget-insert-button-action)
1596 (defun widget-insert-button-action (widget &optional event)
1597 ;; Ask the parent to insert a new item.
1598 (widget-apply (widget-get widget :parent)
1599 :insert-before (widget-get widget :widget)))
1601 ;;; The `delete-button' Widget.
1603 (define-widget 'delete-button 'push-button
1604 "A delete button for the `editable-list' widget."
1606 :action 'widget-delete-button-action)
1608 (defun widget-delete-button-action (widget &optional event)
1609 ;; Ask the parent to insert a new item.
1610 (widget-apply (widget-get widget :parent)
1611 :delete-at (widget-get widget :widget)))
1613 ;;; The `editable-list' Widget.
1615 (define-widget 'editable-list 'default
1616 "A variable list of widgets of the same type."
1617 :convert-widget 'widget-types-convert-widget
1620 :format-handler 'widget-editable-list-format-handler
1621 :entry-format "%i %d %v"
1622 :menu-tag "editable-list"
1623 :value-create 'widget-editable-list-value-create
1624 :value-delete 'widget-children-value-delete
1625 :value-get 'widget-editable-list-value-get
1626 :validate 'widget-editable-list-validate
1627 :match 'widget-editable-list-match
1628 :match-inline 'widget-editable-list-match-inline
1629 :insert-before 'widget-editable-list-insert-before
1630 :delete-at 'widget-editable-list-delete-at)
1632 (defun widget-editable-list-format-handler (widget escape)
1633 ;; We recognize the insert button.
1634 (cond ((eq escape ?i)
1635 (and (widget-get widget :indent)
1636 (insert-char ? (widget-get widget :indent)))
1637 (widget-create-child-and-convert widget 'insert-button))
1639 (widget-default-format-handler widget escape))))
1641 (defun widget-editable-list-value-create (widget)
1642 ;; Insert all values
1643 (let* ((value (widget-get widget :value))
1644 (type (nth 0 (widget-get widget :args)))
1645 (inlinep (widget-get type :inline))
1647 (widget-put widget :value-pos (copy-marker (point)))
1648 (set-marker-insertion-type (widget-get widget :value-pos) t)
1650 (let ((answer (widget-match-inline type value)))
1652 (setq children (cons (widget-editable-list-entry-create
1661 (widget-put widget :children (nreverse children))))
1663 (defun widget-editable-list-value-get (widget)
1664 ;; Get value of the child widget.
1665 (apply 'append (mapcar (lambda (child) (widget-apply child :value-inline))
1666 (widget-get widget :children))))
1668 (defun widget-editable-list-validate (widget)
1669 ;; All the chilren must be valid.
1670 (let ((children (widget-get widget :children))
1672 (while (and children (not found))
1673 (setq child (car children)
1674 children (cdr children)
1675 found (widget-apply child :validate)))
1678 (defun widget-editable-list-match (widget value)
1679 ;; Value must be a list and all the members must match the type.
1681 (null (cdr (widget-editable-list-match-inline widget value)))))
1683 (defun widget-editable-list-match-inline (widget value)
1684 (let ((type (nth 0 (widget-get widget :args)))
1687 (while (and value ok)
1688 (let ((answer (widget-match-inline type value)))
1690 (setq found (append found (car answer))
1693 (cons found value)))
1695 (defun widget-editable-list-insert-before (widget before)
1696 ;; Insert a new child in the list of children.
1698 (let ((children (widget-get widget :children))
1699 (inhibit-read-only t)
1700 after-change-functions)
1702 (goto-char (widget-get before :entry-from)))
1704 (goto-char (widget-get widget :value-pos))))
1705 (let ((child (widget-editable-list-entry-create
1707 (when (< (widget-get child :entry-from) (widget-get widget :from))
1708 (set-marker (widget-get widget :from)
1709 (widget-get child :entry-from)))
1710 (widget-specify-text (widget-get child :entry-from)
1711 (widget-get child :entry-to))
1712 (if (eq (car children) before)
1713 (widget-put widget :children (cons child children))
1714 (while (not (eq (car (cdr children)) before))
1715 (setq children (cdr children)))
1716 (setcdr children (cons child (cdr children)))))))
1718 (widget-apply widget :notify widget))
1720 (defun widget-editable-list-delete-at (widget child)
1721 ;; Delete child from list of children.
1723 (let ((buttons (copy-list (widget-get widget :buttons)))
1725 (inhibit-read-only t)
1726 after-change-functions)
1728 (setq button (car buttons)
1729 buttons (cdr buttons))
1730 (when (eq (widget-get button :widget) child)
1732 :buttons (delq button (widget-get widget :buttons)))
1733 (widget-delete button))))
1734 (let ((entry-from (widget-get child :entry-from))
1735 (entry-to (widget-get child :entry-to))
1736 (inhibit-read-only t)
1737 after-change-functions)
1738 (widget-delete child)
1739 (delete-region entry-from entry-to)
1740 (set-marker entry-from nil)
1741 (set-marker entry-to nil))
1742 (widget-put widget :children (delq child (widget-get widget :children))))
1744 (widget-apply widget :notify widget))
1746 (defun widget-editable-list-entry-create (widget value conv)
1747 ;; Create a new entry to the list.
1748 (let ((type (nth 0 (widget-get widget :args)))
1749 child delete insert)
1750 (widget-specify-insert
1752 (and (widget-get widget :indent)
1753 (insert-char ? (widget-get widget :indent)))
1754 (insert (widget-get widget :entry-format)))
1755 ;; Parse % escapes in format.
1756 (while (re-search-forward "%\\(.\\)" nil t)
1757 (let ((escape (aref (match-string 1) 0)))
1758 (replace-match "" t t)
1759 (cond ((eq escape ?%)
1762 (setq insert (widget-create-child-and-convert
1763 widget 'insert-button)))
1765 (setq delete (widget-create-child-and-convert
1766 widget 'delete-button)))
1769 (setq child (widget-create-child-value
1771 (setq child (widget-create-child widget type))))
1773 (error "Unknown escape `%c'" escape)))))
1775 :buttons (cons delete
1777 (widget-get widget :buttons))))
1778 (let ((entry-from (copy-marker (point-min)))
1779 (entry-to (copy-marker (point-max))))
1780 (widget-specify-text entry-from entry-to)
1781 (set-marker-insertion-type entry-from t)
1782 (set-marker-insertion-type entry-to nil)
1783 (widget-put child :entry-from entry-from)
1784 (widget-put child :entry-to entry-to)))
1785 (widget-put insert :widget child)
1786 (widget-put delete :widget child)
1789 ;;; The `group' Widget.
1791 (define-widget 'group 'default
1792 "A widget which group other widgets inside."
1793 :convert-widget 'widget-types-convert-widget
1795 :value-create 'widget-group-value-create
1796 :value-delete 'widget-children-value-delete
1797 :value-get 'widget-editable-list-value-get
1798 :validate 'widget-editable-list-validate
1799 :match 'widget-group-match
1800 :match-inline 'widget-group-match-inline)
1802 (defun widget-group-value-create (widget)
1803 ;; Create each component.
1804 (let ((args (widget-get widget :args))
1805 (value (widget-get widget :value))
1806 arg answer children)
1808 (setq arg (car args)
1810 answer (widget-match-inline arg value)
1812 (and (eq (preceding-char) ?\n)
1813 (widget-get widget :indent)
1814 (insert-char ? (widget-get widget :indent)))
1815 (push (cond ((null answer)
1816 (widget-create-child widget arg))
1817 ((widget-get arg :inline)
1818 (widget-create-child-value widget arg (car answer)))
1820 (widget-create-child-value widget arg (car (car answer)))))
1822 (widget-put widget :children (nreverse children))))
1824 (defun widget-group-match (widget values)
1825 ;; Match if the components match.
1827 (let ((match (widget-group-match-inline widget values)))
1828 (and match (null (cdr match))))))
1830 (defun widget-group-match-inline (widget vals)
1831 ;; Match if the components match.
1832 (let ((args (widget-get widget :args))
1833 argument answer found)
1835 (setq argument (car args)
1837 answer (widget-match-inline argument vals))
1839 (setq vals (cdr answer)
1840 found (append found (car answer)))
1847 ;;; The `widget-help' Widget.
1849 (define-widget 'widget-help 'push-button
1850 "The widget documentation button."
1851 :format "%[[%t]%] %d"
1852 :help-echo "Push me to toggle the documentation."
1853 :action 'widget-help-action)
1855 (defun widget-help-action (widget &optional event)
1856 "Toggle documentation for WIDGET."
1857 (let ((old (widget-get widget :doc))
1858 (new (widget-get widget :widget-doc)))
1859 (widget-put widget :doc new)
1860 (widget-put widget :widget-doc old))
1861 (widget-value-set widget (widget-value widget)))
1863 ;;; The Sexp Widgets.
1865 (define-widget 'const 'item
1866 "An immutable sexp."
1869 (define-widget 'function-item 'item
1870 "An immutable function name."
1872 :documentation-property (lambda (symbol)
1874 (documentation symbol t)
1877 (define-widget 'variable-item 'item
1878 "An immutable variable name."
1880 :documentation-property 'variable-documentation)
1882 (define-widget 'string 'editable-field
1885 :format "%[%t%]: %v")
1887 (define-widget 'regexp 'string
1888 "A regular expression."
1889 ;; Should do validation.
1892 (define-widget 'file 'string
1894 It will read a file name from the minibuffer when activated."
1895 :format "%[%t%]: %v"
1897 :action 'widget-file-action)
1899 (defun widget-file-action (widget &optional event)
1900 ;; Read a file name from the minibuffer.
1901 (let* ((value (widget-value widget))
1902 (dir (file-name-directory value))
1903 (file (file-name-nondirectory value))
1904 (menu-tag (widget-apply widget :menu-tag-get))
1905 (must-match (widget-get widget :must-match))
1906 (answer (read-file-name (concat menu-tag ": (defalt `" value "') ")
1907 dir nil must-match file)))
1908 (widget-value-set widget (abbreviate-file-name answer))
1909 (widget-apply widget :notify widget event)
1912 (define-widget 'directory 'file
1913 "A directory widget.
1914 It will read a directory name from the minibuffer when activated."
1917 (define-widget 'symbol 'string
1921 :match (lambda (widget value) (symbolp value))
1922 :value-to-internal (lambda (widget value)
1926 :value-to-external (lambda (widget value)
1931 (define-widget 'function 'sexp
1932 ;; Should complete on functions.
1936 (define-widget 'variable 'symbol
1937 ;; Should complete on variables.
1941 (define-widget 'sexp 'string
1942 "An arbitrary lisp expression."
1943 :tag "Lisp expression"
1945 :validate 'widget-sexp-validate
1946 :match (lambda (widget value) t)
1947 :value-to-internal 'widget-sexp-value-to-internal
1948 :value-to-external (lambda (widget value) (read value)))
1950 (defun widget-sexp-value-to-internal (widget value)
1951 ;; Use pp for printer representation.
1952 (let ((pp (pp-to-string value)))
1953 (while (string-match "\n\\'" pp)
1954 (setq pp (substring pp 0 -1)))
1955 (if (or (string-match "\n\\'" pp)
1960 (defun widget-sexp-validate (widget)
1961 ;; Valid if we can read the string and there is no junk left after it.
1963 (let ((buffer (set-buffer (get-buffer-create " *Widget Scratch*"))))
1965 (insert (widget-apply widget :value-get))
1966 (goto-char (point-min))
1967 (condition-case data
1968 (let ((value (read buffer)))
1970 (if (widget-apply widget :match value)
1972 (widget-put widget :error (widget-get widget :type-error))
1975 :error (format "Junk at end of expression: %s"
1976 (buffer-substring (point)
1979 (error (widget-put widget :error (error-message-string data))
1982 (define-widget 'integer 'sexp
1986 :type-error "This field should contain an integer"
1987 :value-to-internal (lambda (widget value)
1988 (if (integerp value)
1989 (prin1-to-string value)
1991 :match (lambda (widget value) (integerp value)))
1993 (define-widget 'character 'string
1998 :format "%{%t%}: %v\n"
1999 :type-error "This field should contain a character"
2000 :value-to-internal (lambda (widget value)
2001 (if (integerp value)
2002 (char-to-string value)
2004 :value-to-external (lambda (widget value)
2008 :match (lambda (widget value) (integerp value)))
2010 (define-widget 'number 'sexp
2011 "A floating point number."
2014 :type-error "This field should contain a number"
2015 :value-to-internal (lambda (widget value)
2017 (prin1-to-string value)
2019 :match (lambda (widget value) (numberp value)))
2021 (define-widget 'list 'group
2024 :format "%{%t%}:\n%v")
2026 (define-widget 'vector 'group
2029 :format "%{%t%}:\n%v"
2030 :match 'widget-vector-match
2031 :value-to-internal (lambda (widget value) (append value nil))
2032 :value-to-external (lambda (widget value) (apply 'vector value)))
2034 (defun widget-vector-match (widget value)
2035 (and (vectorp value)
2036 (widget-group-match widget
2037 (widget-apply :value-to-internal widget value))))
2039 (define-widget 'cons 'group
2042 :format "%{%t%}:\n%v"
2043 :match 'widget-cons-match
2044 :value-to-internal (lambda (widget value)
2045 (list (car value) (cdr value)))
2046 :value-to-external (lambda (widget value)
2047 (cons (nth 0 value) (nth 1 value))))
2049 (defun widget-cons-match (widget value)
2051 (widget-group-match widget
2052 (widget-apply widget :value-to-internal value))))
2054 (define-widget 'choice 'menu-choice
2055 "A union of several sexp types."
2057 :format "%[%t%]: %v")
2059 (define-widget 'radio 'radio-button-choice
2060 "A union of several sexp types."
2062 :format "%{%t%}:\n%v")
2064 (define-widget 'repeat 'editable-list
2065 "A variable length homogeneous list."
2067 :format "%{%t%}:\n%v%i\n")
2069 (define-widget 'set 'checklist
2070 "A list of members from a fixed set."
2072 :format "%{%t%}:\n%v")
2074 (define-widget 'boolean 'toggle
2075 "To be nil or non-nil, that is the question."
2077 :format "%{%t%}: %v")
2079 ;;; The `color' Widget.
2081 (define-widget 'color-item 'choice-item
2082 "A color name (with sample)."
2083 :format "%v (%[sample%])\n"
2084 :button-face-get 'widget-color-item-button-face-get)
2086 (defun widget-color-item-button-face-get (widget)
2087 ;; We create a face from the value.
2090 (facemenu-get-face (intern (concat "fg:" (widget-value widget))))
2093 (define-widget 'color 'push-button
2094 "Choose a color name (with sample)."
2095 :format "%[%t%]: %v"
2098 :value-create 'widget-color-value-create
2099 :value-delete 'widget-children-value-delete
2100 :value-get 'widget-color-value-get
2101 :value-set 'widget-color-value-set
2102 :action 'widget-color-action
2103 :match 'widget-field-match
2106 (defvar widget-color-choice-list nil)
2107 ;; Variable holding the possible colors.
2109 (defun widget-color-choice-list ()
2110 (unless widget-color-choice-list
2111 (setq widget-color-choice-list
2112 (mapcar '(lambda (color) (list color))
2113 (x-defined-colors))))
2114 widget-color-choice-list)
2116 (defun widget-color-value-create (widget)
2117 (let ((child (widget-create-child-and-convert
2118 widget 'color-item (widget-get widget :value))))
2119 (widget-put widget :children (list child))))
2121 (defun widget-color-value-get (widget)
2122 ;; Pass command to first child.
2123 (widget-apply (car (widget-get widget :children)) :value-get))
2125 (defun widget-color-value-set (widget value)
2126 ;; Pass command to first child.
2127 (widget-apply (car (widget-get widget :children)) :value-set value))
2129 (defvar widget-color-history nil
2130 "History of entered colors")
2132 (defun widget-color-action (widget &optional event)
2133 ;; Prompt for a color.
2134 (let* ((tag (widget-apply widget :menu-tag-get))
2135 (prompt (concat tag ": "))
2136 (answer (cond ((string-match "XEmacs" emacs-version)
2137 (read-color prompt))
2138 ((fboundp 'x-defined-colors)
2139 (completing-read (concat tag ": ")
2140 (widget-color-choice-list)
2141 nil nil nil 'widget-color-history))
2143 (read-string prompt (widget-value widget))))))
2144 (unless (zerop (length answer))
2145 (widget-value-set widget answer)
2146 (widget-apply widget :notify widget event)
2151 (defun widget-echo-help-mouse ()
2152 "Display the help message for the widget under the mouse.
2153 Enable with (run-with-idle-timer 1 t 'widget-echo-help-mouse)"
2154 (let* ((pos (mouse-position))
2158 (win (window-at x y frame))
2159 (where (coordinates-in-window-p (cons x y) win)))
2161 (save-window-excursion
2162 (progn ; save-excursion
2164 (let* ((result (compute-motion (window-start win)
2169 (cons (window-hscroll) 0)
2171 (when (and (eq (nth 1 result) x)
2172 (eq (nth 2 result) y))
2173 (widget-echo-help (nth 0 result))))))))
2175 (setq track-mouse t)
2176 (add-hook 'post-command-hook 'widget-stop-mouse-tracking)))
2178 (defun widget-stop-mouse-tracking (&rest args)
2179 "Stop the mouse tracking done while idle."
2180 (remove-hook 'post-command-hook 'widget-stop-mouse-tracking)
2181 (setq track-mouse nil))
2183 (defun widget-at (pos)
2184 "The button or field at POS."
2185 (or (get-text-property pos 'button)
2186 (get-text-property pos 'field)))
2188 (defun widget-echo-help (pos)
2189 "Display the help echo for widget at POS."
2190 (let* ((widget (widget-at pos))
2191 (help-echo (and widget (widget-get widget :help-echo))))
2192 (cond ((stringp help-echo)
2193 (message "%s" help-echo))
2194 ((and (symbolp help-echo) (fboundp help-echo)
2195 (stringp (setq help-echo (funcall help-echo widget))))
2196 (message "%s" help-echo)))))
2200 (provide 'widget-edit)
2202 ;; widget-edit.el ends here