1 ;;; wid-edit.el --- Functions for creating and using widgets.
3 ;; Copyright (C) 1996, 1997 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 (var value doc &rest args)
51 `(defvar ,var ,value ,doc))
52 (defmacro defface (&rest args) nil)
53 (define-widget-keywords :prefix :tag :load :link :options :type :group)
54 (when (fboundp 'copy-face)
55 (copy-face 'default 'widget-documentation-face)
56 (copy-face 'bold 'widget-button-face)
57 (copy-face 'italic 'widget-field-face))))
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/")
91 (defface widget-documentation-face '((((class color)
93 (:foreground "lime green"))
96 (:foreground "dark green"))
98 "Face used for documentation text."
101 (defface widget-button-face '((t (:bold t)))
102 "Face used for widget buttons."
105 (defcustom widget-mouse-face 'highlight
106 "Face used for widget buttons when the mouse is above them."
110 (defface widget-field-face '((((class grayscale color)
112 (:background "light gray"))
113 (((class grayscale color)
115 (:background "dark gray"))
118 "Face used for editable fields."
121 (defcustom widget-menu-max-size 40
122 "Largest number of items allowed in a popup-menu.
123 Larger menus are read through the minibuffer."
127 ;;; Utility functions.
129 ;; These are not really widget specific.
131 (defsubst widget-plist-member (plist prop)
132 ;; Return non-nil if PLIST has the property PROP.
133 ;; PLIST is a property list, which is a list of the form
134 ;; (PROP1 VALUE1 PROP2 VALUE2 ...). PROP is a symbol.
135 ;; Unlike `plist-get', this allows you to distinguish between a missing
136 ;; property and a property with the value nil.
137 ;; The value is actually the tail of PLIST whose car is PROP.
138 (while (and plist (not (eq (car plist) prop)))
139 (setq plist (cdr (cdr plist))))
142 (defun widget-princ-to-string (object)
143 ;; Return string representation of OBJECT, any Lisp object.
144 ;; No quoting characters are used; no delimiters are printed around
145 ;; the contents of strings.
147 (set-buffer (get-buffer-create " *widget-tmp*"))
149 (let ((standard-output (current-buffer)))
153 (defun widget-clear-undo ()
154 "Clear all undo information."
155 (buffer-disable-undo (current-buffer))
156 (buffer-enable-undo))
158 (defun widget-choose (title items &optional event)
159 "Choose an item from a list.
161 First argument TITLE is the name of the list.
162 Second argument ITEMS is an alist (NAME . VALUE).
163 Optional third argument EVENT is an input event.
165 The user is asked to choose between each NAME from the items alist,
166 and the VALUE of the chosen element will be returned. If EVENT is a
167 mouse event, and the number of elements in items is less than
168 `widget-menu-max-size', a popup menu will be used, otherwise the
170 (cond ((and (< (length items) widget-menu-max-size)
171 event (fboundp 'x-popup-menu) window-system)
172 ;; We are in Emacs-19, pressed by the mouse
174 (list title (cons "" items))))
175 ((and (< (length items) widget-menu-max-size)
176 event (fboundp 'popup-menu) window-system)
177 ;; We are in XEmacs, pressed by the mouse
178 (let ((val (get-popup-menu-response
183 (vector (car x) (list (car x)) t)))
186 (listp (event-object val))
187 (stringp (car-safe (event-object val)))
188 (car (event-object val))))
189 (cdr (assoc val items))))
191 (let ((val (completing-read (concat title ": ") items nil t)))
193 (let ((try (try-completion val items)))
196 (cdr (assoc val items)))
199 (defun widget-get-sibling (widget)
200 "Get the item WIDGET is assumed to toggle.
201 This is only meaningful for radio buttons or checkboxes in a list."
202 (let* ((parent (widget-get widget :parent))
203 (children (widget-get parent :children))
207 (setq child (car children)
208 children (cdr children))
209 (when (eq (widget-get child :button) widget)
210 (throw 'child child)))
213 ;;; Widget text specifications.
215 ;; These functions are for specifying text properties.
217 (defun widget-specify-none (from to)
218 ;; Clear all text properties between FROM and TO.
219 (set-text-properties from to nil))
221 (defun widget-specify-text (from to)
222 ;; Default properties.
223 (add-text-properties from to (list 'read-only t
227 'rear-nonsticky nil)))
229 (defun widget-specify-field (widget from to)
230 ;; Specify editable button for WIDGET between FROM and TO.
231 (widget-specify-field-update widget from to)
233 ;; Make it possible to edit the front end of the field.
234 (add-text-properties (1- from) from (list 'rear-nonsticky t
237 (when (or (string-match "\\(.\\|\n\\)%v" (widget-get widget :format))
238 (widget-get widget :hide-front-space))
239 ;; WARNING: This is going to lose horrible if the character just
240 ;; before the field can be modified (e.g. if it belongs to a
241 ;; choice widget). We try to compensate by checking the format
242 ;; string, and hope the user hasn't changed the :create method.
243 (widget-make-intangible (- from 2) from 'end-open))
245 ;; Make it possible to edit back end of the field.
246 (add-text-properties to (1+ to) (list 'front-sticky nil
250 (cond ((widget-get widget :size)
251 (put-text-property to (1+ to) 'invisible t)
252 (when (or (string-match "%v\\(.\\|\n\\)" (widget-get widget :format))
253 (widget-get widget :hide-rear-space))
254 ;; WARNING: This is going to lose horrible if the character just
255 ;; after the field can be modified (e.g. if it belongs to a
256 ;; choice widget). We try to compensate by checking the format
257 ;; string, and hope the user hasn't changed the :create method.
258 (widget-make-intangible to (+ to 2) 'start-open)))
259 ((string-match "XEmacs" emacs-version)
260 ;; XEmacs does not allow you to insert before a read-only
261 ;; character, even if it is start.open.
262 ;; XEmacs does allow you to delete an read-only extent, so
263 ;; making the terminating newline read only doesn't help.
264 ;; I tried putting an invisible intangible read-only space
265 ;; before the newline, which gave really weird effects.
266 ;; So for now, we just have trust the user not to delete the
268 (put-text-property to (1+ to) 'read-only nil))))
270 (defun widget-specify-field-update (widget from to)
271 ;; Specify editable button for WIDGET between FROM and TO.
272 (let ((map (widget-get widget :keymap))
273 (secret (widget-get widget :secret))
275 (size (widget-get widget :size))
276 (face (or (widget-get widget :value-face)
277 'widget-field-face)))
283 (eq (char-after (1- secret-to)) ?\ ))
284 (setq secret-to (1- secret-to)))
288 (while (< (point) secret-to)
289 (let ((old (get-text-property (point) 'secret)))
291 (subst-char-in-region (point) (1+ (point)) secret old)))
294 (set-text-properties from to (list 'field widget
303 (while (< (point) secret-to)
304 (let ((old (following-char)))
305 (subst-char-in-region (point) (1+ (point)) old secret)
306 (put-text-property (point) (1+ (point)) 'secret old))
309 (unless (widget-get widget :size)
310 (add-text-properties to (1+ to) (list 'field widget
312 (add-text-properties to (1+ to) (list 'local-map map
315 (defun widget-specify-button (widget from to)
316 ;; Specify button for WIDGET between FROM and TO.
317 (let ((face (widget-apply widget :button-face-get)))
318 (add-text-properties from to (list 'button widget
319 'mouse-face widget-mouse-face
324 (defun widget-specify-sample (widget from to)
325 ;; Specify sample for WIDGET between FROM and TO.
326 (let ((face (widget-apply widget :sample-face-get)))
328 (add-text-properties from to (list 'start-open t
332 (defun widget-specify-doc (widget from to)
333 ;; Specify documentation for WIDGET between FROM and TO.
334 (add-text-properties from to (list 'widget-doc widget
335 'face 'widget-documentation-face)))
337 (defmacro widget-specify-insert (&rest form)
338 ;; Execute FORM without inheriting any text properties.
340 (let ((inhibit-read-only t)
342 after-change-functions)
344 (narrow-to-region (- (point) 2) (point))
345 (widget-specify-none (point-min) (point-max))
346 (goto-char (1+ (point-min)))
347 (setq result (progn ,@form))
348 (delete-region (point-min) (1+ (point-min)))
349 (delete-region (1- (point-max)) (point-max))
350 (goto-char (point-max))
353 ;;; Widget Properties.
355 (defsubst widget-type (widget)
356 "Return the type of WIDGET, a symbol."
359 (defun widget-put (widget property value)
360 "In WIDGET set PROPERTY to VALUE.
361 The value can later be retrived with `widget-get'."
362 (setcdr widget (plist-put (cdr widget) property value)))
364 (defun widget-get (widget property)
365 "In WIDGET, get the value of PROPERTY.
366 The value could either be specified when the widget was created, or
367 later with `widget-put'."
371 (cond ((setq tmp (widget-plist-member (cdr widget) property))
372 (setq value (car (cdr tmp))
374 ((setq tmp (car widget))
375 (setq widget (get tmp 'widget-type)))
377 (setq missing nil))))
380 (defun widget-member (widget property)
381 "Non-nil iff there is a definition in WIDGET for PROPERTY."
382 (cond ((widget-plist-member (cdr widget) property)
385 (widget-member (get (car widget) 'widget-type) property))
388 (defun widget-apply (widget property &rest args)
389 "Apply the value of WIDGET's PROPERTY to the widget itself.
390 ARGS are passed as extra argments to the function."
391 (apply (widget-get widget property) widget args))
393 (defun widget-value (widget)
394 "Extract the current value of WIDGET."
396 :value-to-external (widget-apply widget :value-get)))
398 (defun widget-value-set (widget value)
399 "Set the current value of WIDGET to VALUE."
401 :value-set (widget-apply widget
402 :value-to-internal value)))
404 (defun widget-match-inline (widget vals)
405 ;; In WIDGET, match the start of VALS.
406 (cond ((widget-get widget :inline)
407 (widget-apply widget :match-inline vals))
409 (widget-apply widget :match (car vals)))
410 (cons (list (car vals)) (cdr vals)))
415 (defcustom widget-glyph-directory (concat data-directory "custom/")
416 "Where widget glyphs are located.
417 If this variable is nil, widget will try to locate the directory
418 automatically. This does not work yet."
422 (defcustom widget-glyph-enable t
423 "If non nil, use glyphs in images when available."
427 (defun widget-glyph-insert (widget tag image)
428 "In WIDGET, insert the text TAG or, if supported, IMAGE.
429 IMAGE should either be a glyph, or a name sans extension of an xpm or
430 xbm file located in `widget-glyph-directory'.
432 WARNING: If you call this with a glyph, and you want theuser to be
433 able to activate the glyph, make sure it is unique. If you use the
434 same glyph for multiple widgets, "
435 (cond ((not (and (string-match "XEmacs" emacs-version)
437 (fboundp 'make-glyph)
439 ;; We don't want or can't use glyphs.
441 ((and (fboundp 'glyphp)
443 ;; Already a glyph. Insert it.
444 (widget-glyph-insert-glyph widget tag image))
446 ;; A string. Look it up in.
447 (let ((file (concat widget-glyph-directory
448 (if (string-match "/\\'" widget-glyph-directory)
452 (if (featurep 'xpm) ".xpm" ".xbm"))))
453 (if (file-readable-p file)
454 (widget-glyph-insert-glyph widget tag (make-glyph file))
455 ;; File not readable, give up.
458 (defun widget-glyph-insert-glyph (widget tag glyph)
459 "In WIDGET, with alternative text TAG, insert GLYPH."
460 (set-glyph-image glyph (cons 'tty tag))
461 (set-glyph-property glyph 'widget widget)
463 (add-text-properties (1- (point)) (point)
467 ;;; Creating Widgets.
470 (defun widget-create (type &rest args)
471 "Create widget of TYPE.
472 The optional ARGS are additional keyword arguments."
473 (let ((widget (apply 'widget-convert type args)))
474 (widget-apply widget :create)
477 (defun widget-create-child-and-convert (parent type &rest args)
478 "As part of the widget PARENT, create a child widget TYPE.
479 The child is converted, using the keyword arguments ARGS."
480 (let ((widget (apply 'widget-convert type args)))
481 (widget-put widget :parent parent)
482 (unless (widget-get widget :indent)
483 (widget-put widget :indent (+ (or (widget-get parent :indent) 0)
484 (or (widget-get widget :extra-offset) 0)
485 (widget-get parent :offset))))
486 (widget-apply widget :create)
489 (defun widget-create-child (parent type)
490 "Create widget of TYPE."
491 (let ((widget (copy-list type)))
492 (widget-put widget :parent parent)
493 (unless (widget-get widget :indent)
494 (widget-put widget :indent (+ (or (widget-get parent :indent) 0)
495 (or (widget-get widget :extra-offset) 0)
496 (widget-get parent :offset))))
497 (widget-apply widget :create)
500 (defun widget-create-child-value (parent type value)
501 "Create widget of TYPE with value VALUE."
502 (let ((widget (copy-list type)))
503 (widget-put widget :value (widget-apply widget :value-to-internal value))
504 (widget-put widget :parent parent)
505 (unless (widget-get widget :indent)
506 (widget-put widget :indent (+ (or (widget-get parent :indent) 0)
507 (or (widget-get widget :extra-offset) 0)
508 (widget-get parent :offset))))
509 (widget-apply widget :create)
513 (defun widget-delete (widget)
515 (widget-apply widget :delete))
517 (defun widget-convert (type &rest args)
518 "Convert TYPE to a widget without inserting it in the buffer.
519 The optional ARGS are additional keyword arguments."
520 ;; Don't touch the type.
521 (let* ((widget (if (symbolp type)
526 ;; First set the :args keyword.
527 (while (cdr current) ;Look in the type.
528 (let ((next (car (cdr current))))
529 (if (and (symbolp next) (eq (aref (symbol-name next) 0) ?:))
530 (setq current (cdr (cdr current)))
531 (setcdr current (list :args (cdr current)))
532 (setq current nil))))
533 (while args ;Look in the args.
534 (let ((next (nth 0 args)))
535 (if (and (symbolp next) (eq (aref (symbol-name next) 0) ?:))
536 (setq args (nthcdr 2 args))
537 (widget-put widget :args args)
539 ;; Then Convert the widget.
542 (let ((convert-widget (plist-get (cdr type) :convert-widget)))
544 (setq widget (funcall convert-widget widget))))
545 (setq type (get (car type) 'widget-type)))
546 ;; Finally set the keyword args.
548 (let ((next (nth 0 keys)))
549 (if (and (symbolp next) (eq (aref (symbol-name next) 0) ?:))
551 (widget-put widget next (nth 1 keys))
552 (setq keys (nthcdr 2 keys)))
554 ;; Convert the :value to internal format.
555 (if (widget-member widget :value)
556 (let ((value (widget-get widget :value)))
558 :value (widget-apply widget :value-to-internal value))))
559 ;; Return the newly create widget.
562 (defun widget-insert (&rest args)
563 "Call `insert' with ARGS and make the text read only."
564 (let ((inhibit-read-only t)
565 after-change-functions
568 (widget-specify-text from (point))))
570 ;;; Keymap and Comands.
572 (defvar widget-keymap nil
573 "Keymap containing useful binding for buffers containing widgets.
574 Recommended as a parent keymap for modes using widgets.")
576 (unless widget-keymap
577 (setq widget-keymap (make-sparse-keymap))
578 (define-key widget-keymap "\C-k" 'widget-kill-line)
579 (define-key widget-keymap "\t" 'widget-forward)
580 (define-key widget-keymap "\M-\t" 'widget-backward)
581 (define-key widget-keymap [(shift tab)] 'widget-backward)
582 (define-key widget-keymap [backtab] 'widget-backward)
583 (if (string-match "XEmacs" (emacs-version))
585 (define-key widget-keymap [button2] 'widget-button-click)
586 (define-key widget-keymap [button1] 'widget-button1-click))
587 (define-key widget-keymap [mouse-2] 'ignore)
588 (define-key widget-keymap [down-mouse-2] 'widget-button-click))
589 (define-key widget-keymap "\C-m" 'widget-button-press))
591 (defvar widget-global-map global-map
592 "Keymap used for events the widget does not handle themselves.")
593 (make-variable-buffer-local 'widget-global-map)
595 (defvar widget-field-keymap nil
596 "Keymap used inside an editable field.")
598 (unless widget-field-keymap
599 (setq widget-field-keymap (copy-keymap widget-keymap))
600 (unless (string-match "XEmacs" (emacs-version))
601 (define-key widget-field-keymap [menu-bar] 'nil))
602 (define-key widget-field-keymap "\C-m" 'widget-field-activate)
603 (define-key widget-field-keymap "\C-a" 'widget-beginning-of-line)
604 (define-key widget-field-keymap "\C-e" 'widget-end-of-line)
605 (set-keymap-parent widget-field-keymap global-map))
607 (defvar widget-text-keymap nil
608 "Keymap used inside a text field.")
610 (unless widget-text-keymap
611 (setq widget-text-keymap (copy-keymap widget-keymap))
612 (unless (string-match "XEmacs" (emacs-version))
613 (define-key widget-text-keymap [menu-bar] 'nil))
614 (define-key widget-text-keymap "\C-a" 'widget-beginning-of-line)
615 (define-key widget-text-keymap "\C-e" 'widget-end-of-line)
616 (set-keymap-parent widget-text-keymap global-map))
618 (defun widget-field-activate (pos &optional event)
619 "Activate the ediable field at point."
621 (let ((field (get-text-property pos 'field)))
623 (widget-apply field :action event)
625 (lookup-key widget-global-map (this-command-keys))))))
627 (defun widget-button-click (event)
628 "Activate button below mouse pointer."
630 (cond ((and (fboundp 'event-glyph)
632 (let ((widget (glyph-property (event-glyph event) 'widget)))
634 (widget-apply widget :action event)
635 (message "You clicked on a glyph."))))
637 (let ((button (get-text-property (event-point event) 'button)))
639 (widget-apply button :action event)
641 (or (lookup-key widget-global-map [ button2 ])
642 (lookup-key widget-global-map [ down-mouse-2 ])
643 (lookup-key widget-global-map [ mouse-2]))))))
645 (message "You clicked somewhere weird."))))
647 (defun widget-button1-click (event)
648 "Activate glyph below mouse pointer."
650 (if (and (fboundp 'event-glyph)
652 (let ((widget (glyph-property (event-glyph event) 'widget)))
654 (widget-apply widget :action event)
655 (message "You clicked on a glyph.")))
656 (call-interactively (lookup-key widget-global-map (this-command-keys)))))
658 (defun widget-button-press (pos &optional event)
659 "Activate button at POS."
661 (let ((button (get-text-property pos 'button)))
663 (widget-apply button :action event)
664 (let ((command (lookup-key widget-global-map (this-command-keys))))
665 (when (commandp command)
666 (call-interactively command))))))
668 (defun widget-move (arg)
669 "Move point to the ARG next field or button.
670 ARG may be negative to move backward."
673 (let ((next (cond ((get-text-property (point) 'button)
674 (next-single-property-change (point) 'button))
675 ((get-text-property (point) 'field)
676 (next-single-property-change (point) 'field))
679 (if (null next) ; Widget extends to end. of buffer
680 (setq next (point-min)))
681 (let ((button (next-single-property-change next 'button))
682 (field (next-single-property-change next 'field)))
683 (cond ((or (get-text-property next 'button)
684 (get-text-property next 'field))
687 (goto-char (min button field)))
688 (button (goto-char button))
689 (field (goto-char field))
691 (let ((button (next-single-property-change (point-min) 'button))
692 (field (next-single-property-change (point-min) 'field)))
693 (cond ((and button field) (goto-char (min button field)))
694 (button (goto-char button))
695 (field (goto-char field))
697 (error "No buttons or fields found")))))))))
699 (if (= (point-min) (point))
702 (let ((previous (cond ((get-text-property (1- (point)) 'button)
703 (previous-single-property-change (point) 'button))
704 ((get-text-property (1- (point)) 'field)
705 (previous-single-property-change (point) 'field))
708 (if (null previous) ; Widget extends to beg. of buffer
709 (setq previous (point-max)))
710 (let ((button (previous-single-property-change previous 'button))
711 (field (previous-single-property-change previous 'field)))
712 (cond ((and button field)
713 (goto-char (max button field)))
714 (button (goto-char button))
715 (field (goto-char field))
717 (let ((button (previous-single-property-change
718 (point-max) 'button))
719 (field (previous-single-property-change
720 (point-max) 'field)))
721 (cond ((and button field) (goto-char (max button field)))
722 (button (goto-char button))
723 (field (goto-char field))
725 (error "No buttons or fields found"))))))))
726 (let ((button (previous-single-property-change (point) 'button))
727 (field (previous-single-property-change (point) 'field)))
728 (cond ((and button field)
729 (goto-char (max button field)))
730 (button (goto-char button))
731 (field (goto-char field)))))
732 (widget-echo-help (point))
733 (run-hooks 'widget-move-hook))
735 (defun widget-forward (arg)
736 "Move point to the next field or button.
737 With optional ARG, move across that many fields."
739 (run-hooks 'widget-forward-hook)
742 (defun widget-backward (arg)
743 "Move point to the previous field or button.
744 With optional ARG, move across that many fields."
746 (run-hooks 'widget-backward-hook)
747 (widget-move (- arg)))
749 (defun widget-beginning-of-line ()
750 "Go to beginning of field or beginning of line, whichever is first."
752 (let ((bol (save-excursion (beginning-of-line) (point)))
753 (prev (previous-single-property-change (point) 'field)))
754 (goto-char (max bol (or prev bol)))))
756 (defun widget-end-of-line ()
757 "Go to end of field or end of line, whichever is first."
759 (let ((bol (save-excursion (end-of-line) (point)))
760 (prev (next-single-property-change (point) 'field)))
761 (goto-char (min bol (or prev bol)))))
763 (defun widget-kill-line ()
764 "Kill to end of field or end of line, whichever is first."
766 (let ((field (get-text-property (point) 'field))
767 (newline (save-excursion (search-forward "\n")))
768 (next (next-single-property-change (point) 'field)))
769 (if (and field (> newline next))
770 (kill-region (point) next)
771 (call-interactively 'kill-line))))
773 ;;; Setting up the buffer.
775 (defvar widget-field-new nil)
776 ;; List of all newly created editable fields in the buffer.
777 (make-variable-buffer-local 'widget-field-new)
779 (defvar widget-field-list nil)
780 ;; List of all editable fields in the buffer.
781 (make-variable-buffer-local 'widget-field-list)
783 (defun widget-setup ()
784 "Setup current buffer so editing string widgets works."
785 (let ((inhibit-read-only t)
786 (after-change-functions nil)
788 (while widget-field-new
789 (setq field (car widget-field-new)
790 widget-field-new (cdr widget-field-new)
791 widget-field-list (cons field widget-field-list))
792 (let ((from (widget-get field :value-from))
793 (to (widget-get field :value-to)))
794 (widget-specify-field field from to)
795 (move-marker from (1- from))
796 (move-marker to (1+ to)))))
798 ;; We need to maintain text properties and size of the editing fields.
799 (make-local-variable 'after-change-functions)
800 (if widget-field-list
801 (setq after-change-functions '(widget-after-change))
802 (setq after-change-functions nil)))
804 (defvar widget-field-last nil)
805 ;; Last field containing point.
806 (make-variable-buffer-local 'widget-field-last)
808 (defvar widget-field-was nil)
809 ;; The widget data before the change.
810 (make-variable-buffer-local 'widget-field-was)
812 (defun widget-field-find (pos)
813 ;; Find widget whose editing field is located at POS.
814 ;; Return nil if POS is not inside and editing field.
816 ;; This is only used in `widget-field-modified', since ordinarily
817 ;; you would just test the field property.
818 (let ((fields widget-field-list)
821 (setq field (car fields)
823 (let ((from (widget-get field :value-from))
824 (to (widget-get field :value-to)))
825 (if (and from to (< from pos) (> to pos))
830 (defun widget-after-change (from to old)
831 ;; Adjust field size and text properties.
833 (let ((field (widget-field-find from))
834 (inhibit-read-only t))
836 ((not (eq field (widget-field-find to)))
838 (message "Error: `widget-after-change' called on two fields"))
840 (let ((size (widget-get field :size)))
842 (let ((begin (1+ (widget-get field :value-from)))
843 (end (1- (widget-get field :value-to))))
844 (widget-specify-field-update field begin end)
845 (cond ((< (- end begin) size)
849 (insert-char ?\ (- (+ begin size) end))
850 (widget-specify-field-update field
853 ((> (- end begin) size)
854 ;; Field too large and
855 (if (or (< (point) (+ begin size))
857 ;; Point is outside extra space.
858 (setq begin (+ begin size))
859 ;; Point is within the extra space.
860 (setq begin (point)))
863 (while (and (eq (preceding-char) ?\ )
865 (delete-backward-char 1))))))
866 (widget-specify-field-update field from to)))
867 (widget-apply field :notify field))))
872 ;; These functions are used in the definition of multiple widgets.
874 (defun widget-children-value-delete (widget)
875 "Delete all :children and :buttons in WIDGET."
876 (mapcar 'widget-delete (widget-get widget :children))
877 (widget-put widget :children nil)
878 (mapcar 'widget-delete (widget-get widget :buttons))
879 (widget-put widget :buttons nil))
881 (defun widget-types-convert-widget (widget)
882 "Convert :args as widget types in WIDGET."
883 (widget-put widget :args (mapcar 'widget-convert (widget-get widget :args)))
886 ;;; The `default' Widget.
888 (define-widget 'default nil
889 "Basic widget other widgets are derived from."
890 :value-to-internal (lambda (widget value) value)
891 :value-to-external (lambda (widget value) value)
892 :create 'widget-default-create
895 :format-handler 'widget-default-format-handler
896 :button-face-get 'widget-default-button-face-get
897 :sample-face-get 'widget-default-sample-face-get
898 :delete 'widget-default-delete
899 :value-set 'widget-default-value-set
900 :value-inline 'widget-default-value-inline
901 :menu-tag-get 'widget-default-menu-tag-get
902 :validate (lambda (widget) nil)
903 :action 'widget-default-action
904 :notify 'widget-default-notify)
906 (defun widget-default-create (widget)
907 "Create WIDGET at point in the current buffer."
908 (widget-specify-insert
910 (tag (widget-get widget :tag))
911 (glyph (widget-get widget :tag-glyph))
912 (doc (widget-get widget :doc))
913 button-begin button-end
914 sample-begin sample-end
917 (insert (widget-get widget :format))
919 ;; Parse escapes in format.
920 (while (re-search-forward "%\\(.\\)" nil t)
921 (let ((escape (aref (match-string 1) 0)))
922 (replace-match "" t t)
923 (cond ((eq escape ?%)
926 (setq button-begin (point)))
928 (setq button-end (point)))
930 (setq sample-begin (point)))
932 (setq sample-end (point)))
934 (when (widget-get widget :indent)
936 (insert-char ? (widget-get widget :indent))))
939 (widget-glyph-insert widget (or tag "image") glyph))
943 (let ((standard-output (current-buffer)))
944 (princ (widget-get widget :value))))))
947 (setq doc-begin (point))
949 (while (eq (preceding-char) ?\n)
950 (delete-backward-char 1))
952 (setq doc-end (point))))
954 (if (and button-begin (not button-end))
955 (widget-apply widget :value-create)
956 (setq value-pos (point))))
958 (widget-apply widget :format-handler escape)))))
959 ;; Specify button, sample, and doc, and insert value.
960 (and button-begin button-end
961 (widget-specify-button widget button-begin button-end))
962 (and sample-begin sample-end
963 (widget-specify-sample widget sample-begin sample-end))
964 (and doc-begin doc-end
965 (widget-specify-doc widget doc-begin doc-end))
967 (goto-char value-pos)
968 (widget-apply widget :value-create)))
969 (let ((from (copy-marker (point-min)))
970 (to (copy-marker (point-max))))
971 (widget-specify-text from to)
972 (set-marker-insertion-type from t)
973 (set-marker-insertion-type to nil)
974 (widget-put widget :from from)
975 (widget-put widget :to to))))
977 (defun widget-default-format-handler (widget escape)
978 ;; We recognize the %h escape by default.
979 (let* ((buttons (widget-get widget :buttons))
980 (doc-property (widget-get widget :documentation-property))
981 (doc-try (cond ((widget-get widget :doc))
982 ((symbolp doc-property)
983 (documentation-property (widget-get widget :value)
986 (funcall doc-property (widget-get widget :value)))))
987 (doc-text (and (stringp doc-try)
988 (> (length doc-try) 1)
990 (cond ((eq escape ?h)
992 (and (eq (preceding-char) ?\n)
993 (widget-get widget :indent)
994 (insert-char ? (widget-get widget :indent)))
995 ;; The `*' in the beginning is redundant.
996 (when (eq (aref doc-text 0) ?*)
997 (setq doc-text (substring doc-text 1)))
998 ;; Get rid of trailing newlines.
999 (when (string-match "\n+\\'" doc-text)
1000 (setq doc-text (substring doc-text 0 (match-beginning 0))))
1001 (push (if (string-match "\n." doc-text)
1002 ;; Allow multiline doc to be hiden.
1003 (widget-create-child-and-convert
1006 (string-match "\\`.*" doc-text)
1007 (match-string 0 doc-text))
1008 :widget-doc doc-text
1010 ;; A single line is just inserted.
1011 (widget-create-child-and-convert
1012 widget 'item :format "%d" :doc doc-text nil))
1015 (error "Unknown escape `%c'" escape)))
1016 (widget-put widget :buttons buttons)))
1018 (defun widget-default-button-face-get (widget)
1019 ;; Use :button-face or widget-button-face
1020 (or (widget-get widget :button-face) 'widget-button-face))
1022 (defun widget-default-sample-face-get (widget)
1023 ;; Use :sample-face.
1024 (widget-get widget :sample-face))
1026 (defun widget-default-delete (widget)
1027 ;; Remove widget from the buffer.
1028 (let ((from (widget-get widget :from))
1029 (to (widget-get widget :to))
1030 (inhibit-read-only t)
1031 after-change-functions)
1032 (widget-apply widget :value-delete)
1033 (delete-region from to)
1034 (set-marker from nil)
1035 (set-marker to nil)))
1037 (defun widget-default-value-set (widget value)
1038 ;; Recreate widget with new value.
1040 (goto-char (widget-get widget :from))
1041 (widget-apply widget :delete)
1042 (widget-put widget :value value)
1043 (widget-apply widget :create)))
1045 (defun widget-default-value-inline (widget)
1046 ;; Wrap value in a list unless it is inline.
1047 (if (widget-get widget :inline)
1048 (widget-value widget)
1049 (list (widget-value widget))))
1051 (defun widget-default-menu-tag-get (widget)
1052 ;; Use tag or value for menus.
1053 (or (widget-get widget :menu-tag)
1054 (widget-get widget :tag)
1055 (widget-princ-to-string (widget-get widget :value))))
1057 (defun widget-default-action (widget &optional event)
1058 ;; Notify the parent when a widget change
1059 (let ((parent (widget-get widget :parent)))
1061 (widget-apply parent :notify widget event))))
1063 (defun widget-default-notify (widget child &optional event)
1064 ;; Pass notification to parent.
1065 (widget-default-action widget event))
1067 ;;; The `item' Widget.
1069 (define-widget 'item 'default
1070 "Constant items for inclusion in other widgets."
1071 :convert-widget 'widget-item-convert-widget
1072 :value-create 'widget-item-value-create
1073 :value-delete 'ignore
1074 :value-get 'widget-item-value-get
1075 :match 'widget-item-match
1076 :match-inline 'widget-item-match-inline
1077 :action 'widget-item-action
1080 (defun widget-item-convert-widget (widget)
1081 ;; Initialize :value from :args in WIDGET.
1082 (let ((args (widget-get widget :args)))
1084 (widget-put widget :value (widget-apply widget
1085 :value-to-internal (car args)))
1086 (widget-put widget :args nil)))
1089 (defun widget-item-value-create (widget)
1090 ;; Insert the printed representation of the value.
1091 (let ((standard-output (current-buffer)))
1092 (princ (widget-get widget :value))))
1094 (defun widget-item-match (widget value)
1095 ;; Match if the value is the same.
1096 (equal (widget-get widget :value) value))
1098 (defun widget-item-match-inline (widget values)
1099 ;; Match if the value is the same.
1100 (let ((value (widget-get widget :value)))
1102 (<= (length value) (length values))
1103 (let ((head (subseq values 0 (length value))))
1104 (and (equal head value)
1105 (cons head (subseq values (length value))))))))
1107 (defun widget-item-action (widget &optional event)
1108 ;; Just notify itself.
1109 (widget-apply widget :notify widget event))
1111 (defun widget-item-value-get (widget)
1112 ;; Items are simple.
1113 (widget-get widget :value))
1115 ;;; The `push-button' Widget.
1117 (defcustom widget-push-button-gui t
1118 "If non nil, use GUI push buttons when available."
1122 ;; Cache already created GUI objects.
1123 (defvar widget-push-button-cache nil)
1125 (define-widget 'push-button 'item
1126 "A pushable button."
1127 :value-create 'widget-push-button-value-create
1130 (defun widget-push-button-value-create (widget)
1131 ;; Insert text representing the `on' and `off' states.
1132 (let* ((tag (or (widget-get widget :tag)
1133 (widget-get widget :value)))
1134 (text (concat "[" tag "]"))
1135 (gui (cdr (assoc tag widget-push-button-cache))))
1136 (if (and (fboundp 'make-gui-button)
1137 (fboundp 'make-glyph)
1138 widget-push-button-gui
1139 (fboundp 'device-on-window-system-p)
1140 (device-on-window-system-p)
1141 (string-match "XEmacs" emacs-version))
1144 (setq gui (make-gui-button tag 'widget-gui-action widget))
1145 (push (cons tag gui) widget-push-button-cache))
1146 (widget-glyph-insert-glyph widget text
1147 (make-glyph (car (aref gui 1)))))
1150 (defun widget-gui-action (widget)
1151 "Apply :action for WIDGET."
1152 (widget-apply widget :action (this-command-keys)))
1154 ;;; The `link' Widget.
1156 (define-widget 'link 'item
1158 :help-echo "Push me to follow the link."
1161 ;;; The `info-link' Widget.
1163 (define-widget 'info-link 'link
1164 "A link to an info file."
1165 :action 'widget-info-link-action)
1167 (defun widget-info-link-action (widget &optional event)
1168 "Open the info node specified by WIDGET."
1169 (Info-goto-node (widget-value widget)))
1171 ;;; The `url-link' Widget.
1173 (define-widget 'url-link 'link
1174 "A link to an www page."
1175 :action 'widget-url-link-action)
1177 (defun widget-url-link-action (widget &optional event)
1178 "Open the url specified by WIDGET."
1179 (require 'browse-url)
1180 (funcall browse-url-browser-function (widget-value widget)))
1182 ;;; The `editable-field' Widget.
1184 (define-widget 'editable-field 'default
1185 "An editable text field."
1186 :convert-widget 'widget-item-convert-widget
1187 :keymap widget-field-keymap
1190 :action 'widget-field-action
1191 :validate 'widget-field-validate
1194 :value-create 'widget-field-value-create
1195 :value-delete 'widget-field-value-delete
1196 :value-get 'widget-field-value-get
1197 :match 'widget-field-match)
1199 ;; History of field minibuffer edits.
1200 (defvar widget-field-history nil)
1202 (defun widget-field-action (widget &optional event)
1203 ;; Edit the value in the minibuffer.
1204 (let ((tag (widget-apply widget :menu-tag-get))
1205 (invalid (widget-apply widget :validate)))
1207 (error (widget-get invalid :error)))
1208 (widget-value-set widget
1209 (widget-apply widget
1211 (read-string (concat tag ": ")
1215 (widget-value widget))
1216 'widget-field-history)))
1217 (widget-apply widget :notify widget event)
1220 (defun widget-field-validate (widget)
1221 ;; Valid if the content matches `:valid-regexp'.
1223 (let ((value (widget-apply widget :value-get))
1224 (regexp (widget-get widget :valid-regexp)))
1225 (if (string-match regexp value)
1229 (defun widget-field-value-create (widget)
1230 ;; Create an editable text field.
1232 (let ((size (widget-get widget :size))
1233 (value (widget-get widget :value))
1237 (< (length value) size)
1238 (insert-char ?\ (- size (length value))))
1239 (unless (memq widget widget-field-list)
1240 (setq widget-field-new (cons widget widget-field-new)))
1241 (widget-put widget :value-to (copy-marker (point)))
1242 (set-marker-insertion-type (widget-get widget :value-to) nil)
1246 (widget-put widget :value-from (copy-marker from))
1247 (set-marker-insertion-type (widget-get widget :value-from) t)))
1249 (defun widget-field-value-delete (widget)
1250 ;; Remove the widget from the list of active editing fields.
1251 (setq widget-field-list (delq widget widget-field-list))
1252 ;; These are nil if the :format string doesn't contain `%v'.
1253 (when (widget-get widget :value-from)
1254 (set-marker (widget-get widget :value-from) nil))
1255 (when (widget-get widget :value-from)
1256 (set-marker (widget-get widget :value-to) nil)))
1258 (defun widget-field-value-get (widget)
1259 ;; Return current text in editing field.
1260 (let ((from (widget-get widget :value-from))
1261 (to (widget-get widget :value-to))
1262 (size (widget-get widget :size))
1263 (secret (widget-get widget :secret))
1264 (old (current-buffer)))
1267 (set-buffer (marker-buffer from))
1268 (setq from (1+ from)
1273 (eq (char-after (1- to)) ?\ ))
1275 (let ((result (buffer-substring-no-properties from to)))
1278 (while (< (+ from index) to)
1280 (get-text-property (+ from index) 'secret))
1281 (setq index (1+ index)))))
1284 (widget-get widget :value))))
1286 (defun widget-field-match (widget value)
1287 ;; Match any string.
1290 ;;; The `text' Widget.
1292 (define-widget 'text 'editable-field
1293 :keymap widget-text-keymap
1294 "A multiline text area.")
1296 ;;; The `menu-choice' Widget.
1298 (define-widget 'menu-choice 'default
1299 "A menu of options."
1300 :convert-widget 'widget-types-convert-widget
1301 :format "%[%t%]: %v"
1304 :void '(item :format "invalid (%t)\n")
1305 :value-create 'widget-choice-value-create
1306 :value-delete 'widget-children-value-delete
1307 :value-get 'widget-choice-value-get
1308 :value-inline 'widget-choice-value-inline
1309 :action 'widget-choice-action
1310 :error "Make a choice"
1311 :validate 'widget-choice-validate
1312 :match 'widget-choice-match
1313 :match-inline 'widget-choice-match-inline)
1315 (defun widget-choice-value-create (widget)
1316 ;; Insert the first choice that matches the value.
1317 (let ((value (widget-get widget :value))
1318 (args (widget-get widget :args))
1321 (setq current (car args)
1323 (when (widget-apply current :match value)
1324 (widget-put widget :children (list (widget-create-child-value
1325 widget current value)))
1326 (widget-put widget :choice current)
1330 (let ((void (widget-get widget :void)))
1331 (widget-put widget :children (list (widget-create-child-and-convert
1332 widget void :value value)))
1333 (widget-put widget :choice void)))))
1335 (defun widget-choice-value-get (widget)
1336 ;; Get value of the child widget.
1337 (widget-value (car (widget-get widget :children))))
1339 (defun widget-choice-value-inline (widget)
1340 ;; Get value of the child widget.
1341 (widget-apply (car (widget-get widget :children)) :value-inline))
1343 (defun widget-choice-action (widget &optional event)
1345 (let ((args (widget-get widget :args))
1346 (old (widget-get widget :choice))
1347 (tag (widget-apply widget :menu-tag-get))
1348 (completion-ignore-case (widget-get widget :case-fold))
1350 ;; Remember old value.
1351 (if (and old (not (widget-apply widget :validate)))
1352 (let* ((external (widget-value widget))
1353 (internal (widget-apply old :value-to-internal external)))
1354 (widget-put old :value internal)))
1357 (cond ((= (length args) 0)
1359 ((= (length args) 1)
1361 ((and (= (length args) 2)
1363 (if (eq old (nth 0 args))
1368 (setq current (car args)
1371 (cons (cons (widget-apply current :menu-tag-get)
1374 (widget-choose tag (reverse choices) event))))
1376 (widget-value-set widget
1377 (widget-apply current :value-to-external
1378 (widget-get current :value)))
1379 (widget-apply widget :notify widget event)
1382 (widget-apply widget :notify widget event)
1383 (widget-clear-undo))
1385 (defun widget-choice-validate (widget)
1386 ;; Valid if we have made a valid choice.
1387 (let ((void (widget-get widget :void))
1388 (choice (widget-get widget :choice))
1389 (child (car (widget-get widget :children))))
1390 (if (eq void choice)
1392 (widget-apply child :validate))))
1394 (defun widget-choice-match (widget value)
1395 ;; Matches if one of the choices matches.
1396 (let ((args (widget-get widget :args))
1398 (while (and args (not found))
1399 (setq current (car args)
1401 found (widget-apply current :match value)))
1404 (defun widget-choice-match-inline (widget values)
1405 ;; Matches if one of the choices matches.
1406 (let ((args (widget-get widget :args))
1408 (while (and args (null found))
1409 (setq current (car args)
1411 found (widget-match-inline current values)))
1414 ;;; The `toggle' Widget.
1416 (define-widget 'toggle 'item
1417 "Toggle between two states."
1419 :value-create 'widget-toggle-value-create
1420 :action 'widget-toggle-action
1421 :match (lambda (widget value) t)
1425 (defun widget-toggle-value-create (widget)
1426 ;; Insert text representing the `on' and `off' states.
1427 (if (widget-value widget)
1428 (widget-glyph-insert widget
1429 (widget-get widget :on)
1430 (widget-get widget :on-glyph))
1431 (widget-glyph-insert widget
1432 (widget-get widget :off)
1433 (widget-get widget :off-glyph))))
1435 (defun widget-toggle-action (widget &optional event)
1437 (widget-value-set widget (not (widget-value widget)))
1438 (widget-apply widget :notify widget event))
1440 ;;; The `checkbox' Widget.
1442 (define-widget 'checkbox 'toggle
1443 "A checkbox toggle."
1448 :off-glyph "check0")
1450 ;;; The `checklist' Widget.
1452 (define-widget 'checklist 'default
1453 "A multiple choice widget."
1454 :convert-widget 'widget-types-convert-widget
1457 :entry-format "%b %v"
1458 :menu-tag "checklist"
1460 :value-create 'widget-checklist-value-create
1461 :value-delete 'widget-children-value-delete
1462 :value-get 'widget-checklist-value-get
1463 :validate 'widget-checklist-validate
1464 :match 'widget-checklist-match
1465 :match-inline 'widget-checklist-match-inline)
1467 (defun widget-checklist-value-create (widget)
1468 ;; Insert all values
1469 (let ((alist (widget-checklist-match-find widget (widget-get widget :value)))
1470 (args (widget-get widget :args)))
1472 (widget-checklist-add-item widget (car args) (assq (car args) alist))
1473 (setq args (cdr args)))
1474 (widget-put widget :children (nreverse (widget-get widget :children)))))
1476 (defun widget-checklist-add-item (widget type chosen)
1477 ;; Create checklist item in WIDGET of type TYPE.
1478 ;; If the item is checked, CHOSEN is a cons whose cdr is the value.
1479 (and (eq (preceding-char) ?\n)
1480 (widget-get widget :indent)
1481 (insert-char ? (widget-get widget :indent)))
1482 (widget-specify-insert
1483 (let* ((children (widget-get widget :children))
1484 (buttons (widget-get widget :buttons))
1487 (insert (widget-get widget :entry-format))
1489 ;; Parse % escapes in format.
1490 (while (re-search-forward "%\\([bv%]\\)" nil t)
1491 (let ((escape (aref (match-string 1) 0)))
1492 (replace-match "" t t)
1493 (cond ((eq escape ?%)
1496 (setq button (widget-create-child-and-convert
1497 widget 'checkbox :value (not (null chosen)))))
1501 (widget-create-child widget type))
1502 ((widget-get type :inline)
1503 (widget-create-child-value
1504 widget type (cdr chosen)))
1506 (widget-create-child-value
1507 widget type (car (cdr chosen)))))))
1509 (error "Unknown escape `%c'" escape)))))
1510 ;; Update properties.
1511 (and button child (widget-put child :button button))
1512 (and button (widget-put widget :buttons (cons button buttons)))
1513 (and child (widget-put widget :children (cons child children))))))
1515 (defun widget-checklist-match (widget values)
1516 ;; All values must match a type in the checklist.
1518 (null (cdr (widget-checklist-match-inline widget values)))))
1520 (defun widget-checklist-match-inline (widget values)
1521 ;; Find the values which match a type in the checklist.
1522 (let ((greedy (widget-get widget :greedy))
1523 (args (copy-list (widget-get widget :args)))
1526 (let ((answer (widget-checklist-match-up args values)))
1528 (let ((vals (widget-match-inline answer values)))
1529 (setq found (append found (car vals))
1531 args (delq answer args))))
1533 (setq rest (append rest (list (car values)))
1534 values (cdr values)))
1536 (setq rest (append rest values)
1540 (defun widget-checklist-match-find (widget vals)
1541 ;; Find the vals which match a type in the checklist.
1542 ;; Return an alist of (TYPE MATCH).
1543 (let ((greedy (widget-get widget :greedy))
1544 (args (copy-list (widget-get widget :args)))
1547 (let ((answer (widget-checklist-match-up args vals)))
1549 (let ((match (widget-match-inline answer vals)))
1550 (setq found (cons (cons answer (car match)) found)
1552 args (delq answer args))))
1554 (setq vals (cdr vals)))
1559 (defun widget-checklist-match-up (args vals)
1560 ;; Rerturn the first type from ARGS that matches VALS.
1561 (let (current found)
1562 (while (and args (null found))
1563 (setq current (car args)
1565 found (widget-match-inline current vals)))
1570 (defun widget-checklist-value-get (widget)
1571 ;; The values of all selected items.
1572 (let ((children (widget-get widget :children))
1575 (setq child (car children)
1576 children (cdr children))
1577 (if (widget-value (widget-get child :button))
1578 (setq result (append result (widget-apply child :value-inline)))))
1581 (defun widget-checklist-validate (widget)
1582 ;; Ticked chilren must be valid.
1583 (let ((children (widget-get widget :children))
1585 (while (and children (not found))
1586 (setq child (car children)
1587 children (cdr children)
1588 button (widget-get child :button)
1589 found (and (widget-value button)
1590 (widget-apply child :validate))))
1593 ;;; The `option' Widget
1595 (define-widget 'option 'checklist
1596 "An widget with an optional item."
1599 ;;; The `choice-item' Widget.
1601 (define-widget 'choice-item 'item
1602 "Button items that delegate action events to their parents."
1603 :action 'widget-choice-item-action
1604 :format "%[%t%] \n")
1606 (defun widget-choice-item-action (widget &optional event)
1607 ;; Tell parent what happened.
1608 (widget-apply (widget-get widget :parent) :action event))
1610 ;;; The `radio-button' Widget.
1612 (define-widget 'radio-button 'toggle
1613 "A radio button for use in the `radio' widget."
1614 :notify 'widget-radio-button-notify
1619 :off-glyph "radio0")
1621 (defun widget-radio-button-notify (widget child &optional event)
1623 (widget-apply (widget-get widget :parent) :action widget event))
1625 ;;; The `radio-button-choice' Widget.
1627 (define-widget 'radio-button-choice 'default
1628 "Select one of multiple options."
1629 :convert-widget 'widget-types-convert-widget
1632 :entry-format "%b %v"
1634 :value-create 'widget-radio-value-create
1635 :value-delete 'widget-children-value-delete
1636 :value-get 'widget-radio-value-get
1637 :value-inline 'widget-radio-value-inline
1638 :value-set 'widget-radio-value-set
1639 :error "You must push one of the buttons"
1640 :validate 'widget-radio-validate
1641 :match 'widget-choice-match
1642 :match-inline 'widget-choice-match-inline
1643 :action 'widget-radio-action)
1645 (defun widget-radio-value-create (widget)
1646 ;; Insert all values
1647 (let ((args (widget-get widget :args))
1650 (setq arg (car args)
1652 (widget-radio-add-item widget arg))))
1654 (defun widget-radio-add-item (widget type)
1655 "Add to radio widget WIDGET a new radio button item of type TYPE."
1656 ;; (setq type (widget-convert type))
1657 (and (eq (preceding-char) ?\n)
1658 (widget-get widget :indent)
1659 (insert-char ? (widget-get widget :indent)))
1660 (widget-specify-insert
1661 (let* ((value (widget-get widget :value))
1662 (children (widget-get widget :children))
1663 (buttons (widget-get widget :buttons))
1665 (chosen (and (null (widget-get widget :choice))
1666 (widget-apply type :match value)))
1668 (insert (widget-get widget :entry-format))
1670 ;; Parse % escapes in format.
1671 (while (re-search-forward "%\\([bv%]\\)" nil t)
1672 (let ((escape (aref (match-string 1) 0)))
1673 (replace-match "" t t)
1674 (cond ((eq escape ?%)
1677 (setq button (widget-create-child-and-convert
1678 widget 'radio-button
1679 :value (not (null chosen)))))
1681 (setq child (if chosen
1682 (widget-create-child-value
1684 (widget-create-child widget type))))
1686 (error "Unknown escape `%c'" escape)))))
1687 ;; Update properties.
1689 (widget-put widget :choice type))
1691 (widget-put child :button button)
1692 (widget-put widget :buttons (nconc buttons (list button))))
1694 (widget-put widget :children (nconc children (list child))))
1697 (defun widget-radio-value-get (widget)
1698 ;; Get value of the child widget.
1699 (let ((chosen (widget-radio-chosen widget)))
1700 (and chosen (widget-value chosen))))
1702 (defun widget-radio-chosen (widget)
1703 "Return the widget representing the chosen radio button."
1704 (let ((children (widget-get widget :children))
1707 (setq current (car children)
1708 children (cdr children))
1709 (let* ((button (widget-get current :button))
1710 (value (widget-apply button :value-get)))
1716 (defun widget-radio-value-inline (widget)
1717 ;; Get value of the child widget.
1718 (let ((children (widget-get widget :children))
1721 (setq current (car children)
1722 children (cdr children))
1723 (let* ((button (widget-get current :button))
1724 (value (widget-apply button :value-get)))
1726 (setq found (widget-apply current :value-inline)
1730 (defun widget-radio-value-set (widget value)
1731 ;; We can't just delete and recreate a radio widget, since children
1732 ;; can be added after the original creation and won't be recreated
1734 (let ((children (widget-get widget :children))
1737 (setq current (car children)
1738 children (cdr children))
1739 (let* ((button (widget-get current :button))
1740 (match (and (not found)
1741 (widget-apply current :match value))))
1742 (widget-value-set button match)
1744 (widget-value-set current value))
1745 (setq found (or found match))))))
1747 (defun widget-radio-validate (widget)
1748 ;; Valid if we have made a valid choice.
1749 (let ((children (widget-get widget :children))
1750 current found button)
1751 (while (and children (not found))
1752 (setq current (car children)
1753 children (cdr children)
1754 button (widget-get current :button)
1755 found (widget-apply button :value-get)))
1757 (widget-apply current :validate)
1760 (defun widget-radio-action (widget child event)
1761 ;; Check if a radio button was pressed.
1762 (let ((children (widget-get widget :children))
1763 (buttons (widget-get widget :buttons))
1765 (when (memq child buttons)
1767 (setq current (car children)
1768 children (cdr children))
1769 (let* ((button (widget-get current :button)))
1770 (cond ((eq child button)
1771 (widget-value-set button t))
1772 ((widget-value button)
1773 (widget-value-set button nil)))))))
1774 ;; Pass notification to parent.
1775 (widget-apply widget :notify child event))
1777 ;;; The `insert-button' Widget.
1779 (define-widget 'insert-button 'push-button
1780 "An insert button for the `editable-list' widget."
1782 :action 'widget-insert-button-action)
1784 (defun widget-insert-button-action (widget &optional event)
1785 ;; Ask the parent to insert a new item.
1786 (widget-apply (widget-get widget :parent)
1787 :insert-before (widget-get widget :widget)))
1789 ;;; The `delete-button' Widget.
1791 (define-widget 'delete-button 'push-button
1792 "A delete button for the `editable-list' widget."
1794 :action 'widget-delete-button-action)
1796 (defun widget-delete-button-action (widget &optional event)
1797 ;; Ask the parent to insert a new item.
1798 (widget-apply (widget-get widget :parent)
1799 :delete-at (widget-get widget :widget)))
1801 ;;; The `editable-list' Widget.
1803 (defcustom widget-editable-list-gui nil
1804 "If non nil, use GUI push-buttons in editable list when available."
1808 (define-widget 'editable-list 'default
1809 "A variable list of widgets of the same type."
1810 :convert-widget 'widget-types-convert-widget
1813 :format-handler 'widget-editable-list-format-handler
1814 :entry-format "%i %d %v"
1815 :menu-tag "editable-list"
1816 :value-create 'widget-editable-list-value-create
1817 :value-delete 'widget-children-value-delete
1818 :value-get 'widget-editable-list-value-get
1819 :validate 'widget-editable-list-validate
1820 :match 'widget-editable-list-match
1821 :match-inline 'widget-editable-list-match-inline
1822 :insert-before 'widget-editable-list-insert-before
1823 :delete-at 'widget-editable-list-delete-at)
1825 (defun widget-editable-list-format-handler (widget escape)
1826 ;; We recognize the insert button.
1827 (let ((widget-push-button-gui widget-editable-list-gui))
1828 (cond ((eq escape ?i)
1829 (and (widget-get widget :indent)
1830 (insert-char ? (widget-get widget :indent)))
1831 (widget-create-child-and-convert widget 'insert-button))
1833 (widget-default-format-handler widget escape)))))
1835 (defun widget-editable-list-value-create (widget)
1836 ;; Insert all values
1837 (let* ((value (widget-get widget :value))
1838 (type (nth 0 (widget-get widget :args)))
1839 (inlinep (widget-get type :inline))
1841 (widget-put widget :value-pos (copy-marker (point)))
1842 (set-marker-insertion-type (widget-get widget :value-pos) t)
1844 (let ((answer (widget-match-inline type value)))
1846 (setq children (cons (widget-editable-list-entry-create
1855 (widget-put widget :children (nreverse children))))
1857 (defun widget-editable-list-value-get (widget)
1858 ;; Get value of the child widget.
1859 (apply 'append (mapcar (lambda (child) (widget-apply child :value-inline))
1860 (widget-get widget :children))))
1862 (defun widget-editable-list-validate (widget)
1863 ;; All the chilren must be valid.
1864 (let ((children (widget-get widget :children))
1866 (while (and children (not found))
1867 (setq child (car children)
1868 children (cdr children)
1869 found (widget-apply child :validate)))
1872 (defun widget-editable-list-match (widget value)
1873 ;; Value must be a list and all the members must match the type.
1875 (null (cdr (widget-editable-list-match-inline widget value)))))
1877 (defun widget-editable-list-match-inline (widget value)
1878 (let ((type (nth 0 (widget-get widget :args)))
1881 (while (and value ok)
1882 (let ((answer (widget-match-inline type value)))
1884 (setq found (append found (car answer))
1887 (cons found value)))
1889 (defun widget-editable-list-insert-before (widget before)
1890 ;; Insert a new child in the list of children.
1892 (let ((children (widget-get widget :children))
1893 (inhibit-read-only t)
1894 after-change-functions)
1896 (goto-char (widget-get before :entry-from)))
1898 (goto-char (widget-get widget :value-pos))))
1899 (let ((child (widget-editable-list-entry-create
1901 (when (< (widget-get child :entry-from) (widget-get widget :from))
1902 (set-marker (widget-get widget :from)
1903 (widget-get child :entry-from)))
1904 (widget-specify-text (widget-get child :entry-from)
1905 (widget-get child :entry-to))
1906 (if (eq (car children) before)
1907 (widget-put widget :children (cons child children))
1908 (while (not (eq (car (cdr children)) before))
1909 (setq children (cdr children)))
1910 (setcdr children (cons child (cdr children)))))))
1912 (widget-apply widget :notify widget))
1914 (defun widget-editable-list-delete-at (widget child)
1915 ;; Delete child from list of children.
1917 (let ((buttons (copy-list (widget-get widget :buttons)))
1919 (inhibit-read-only t)
1920 after-change-functions)
1922 (setq button (car buttons)
1923 buttons (cdr buttons))
1924 (when (eq (widget-get button :widget) child)
1926 :buttons (delq button (widget-get widget :buttons)))
1927 (widget-delete button))))
1928 (let ((entry-from (widget-get child :entry-from))
1929 (entry-to (widget-get child :entry-to))
1930 (inhibit-read-only t)
1931 after-change-functions)
1932 (widget-delete child)
1933 (delete-region entry-from entry-to)
1934 (set-marker entry-from nil)
1935 (set-marker entry-to nil))
1936 (widget-put widget :children (delq child (widget-get widget :children))))
1938 (widget-apply widget :notify widget))
1940 (defun widget-editable-list-entry-create (widget value conv)
1941 ;; Create a new entry to the list.
1942 (let ((type (nth 0 (widget-get widget :args)))
1943 (widget-push-button-gui widget-editable-list-gui)
1944 child delete insert)
1945 (widget-specify-insert
1947 (and (widget-get widget :indent)
1948 (insert-char ? (widget-get widget :indent)))
1949 (insert (widget-get widget :entry-format)))
1950 ;; Parse % escapes in format.
1951 (while (re-search-forward "%\\(.\\)" nil t)
1952 (let ((escape (aref (match-string 1) 0)))
1953 (replace-match "" t t)
1954 (cond ((eq escape ?%)
1957 (setq insert (widget-create-child-and-convert
1958 widget 'insert-button)))
1960 (setq delete (widget-create-child-and-convert
1961 widget 'delete-button)))
1964 (setq child (widget-create-child-value
1966 (setq child (widget-create-child widget type))))
1968 (error "Unknown escape `%c'" escape)))))
1970 :buttons (cons delete
1972 (widget-get widget :buttons))))
1973 (let ((entry-from (copy-marker (point-min)))
1974 (entry-to (copy-marker (point-max))))
1975 (widget-specify-text entry-from entry-to)
1976 (set-marker-insertion-type entry-from t)
1977 (set-marker-insertion-type entry-to nil)
1978 (widget-put child :entry-from entry-from)
1979 (widget-put child :entry-to entry-to)))
1980 (widget-put insert :widget child)
1981 (widget-put delete :widget child)
1984 ;;; The `group' Widget.
1986 (define-widget 'group 'default
1987 "A widget which group other widgets inside."
1988 :convert-widget 'widget-types-convert-widget
1990 :value-create 'widget-group-value-create
1991 :value-delete 'widget-children-value-delete
1992 :value-get 'widget-editable-list-value-get
1993 :validate 'widget-editable-list-validate
1994 :match 'widget-group-match
1995 :match-inline 'widget-group-match-inline)
1997 (defun widget-group-value-create (widget)
1998 ;; Create each component.
1999 (let ((args (widget-get widget :args))
2000 (value (widget-get widget :value))
2001 arg answer children)
2003 (setq arg (car args)
2005 answer (widget-match-inline arg value)
2007 (and (eq (preceding-char) ?\n)
2008 (widget-get widget :indent)
2009 (insert-char ? (widget-get widget :indent)))
2010 (push (cond ((null answer)
2011 (widget-create-child widget arg))
2012 ((widget-get arg :inline)
2013 (widget-create-child-value widget arg (car answer)))
2015 (widget-create-child-value widget arg (car (car answer)))))
2017 (widget-put widget :children (nreverse children))))
2019 (defun widget-group-match (widget values)
2020 ;; Match if the components match.
2022 (let ((match (widget-group-match-inline widget values)))
2023 (and match (null (cdr match))))))
2025 (defun widget-group-match-inline (widget vals)
2026 ;; Match if the components match.
2027 (let ((args (widget-get widget :args))
2028 argument answer found)
2030 (setq argument (car args)
2032 answer (widget-match-inline argument vals))
2034 (setq vals (cdr answer)
2035 found (append found (car answer)))
2042 ;;; The `widget-help' Widget.
2044 (define-widget 'widget-help 'push-button
2045 "The widget documentation button."
2046 :format "%[[%t]%] %d"
2047 :help-echo "Push me to toggle the documentation."
2048 :action 'widget-help-action)
2050 (defun widget-help-action (widget &optional event)
2051 "Toggle documentation for WIDGET."
2052 (let ((old (widget-get widget :doc))
2053 (new (widget-get widget :widget-doc)))
2054 (widget-put widget :doc new)
2055 (widget-put widget :widget-doc old))
2056 (widget-value-set widget (widget-value widget)))
2058 ;;; The Sexp Widgets.
2060 (define-widget 'const 'item
2061 "An immutable sexp."
2064 (define-widget 'function-item 'item
2065 "An immutable function name."
2067 :documentation-property (lambda (symbol)
2069 (documentation symbol t)
2072 (define-widget 'variable-item 'item
2073 "An immutable variable name."
2075 :documentation-property 'variable-documentation)
2077 (define-widget 'string 'editable-field
2080 :format "%[%t%]: %v")
2082 (define-widget 'regexp 'string
2083 "A regular expression."
2084 ;; Should do validation.
2087 (define-widget 'file 'string
2089 It will read a file name from the minibuffer when activated."
2090 :format "%[%t%]: %v"
2092 :action 'widget-file-action)
2094 (defun widget-file-action (widget &optional event)
2095 ;; Read a file name from the minibuffer.
2096 (let* ((value (widget-value widget))
2097 (dir (file-name-directory value))
2098 (file (file-name-nondirectory value))
2099 (menu-tag (widget-apply widget :menu-tag-get))
2100 (must-match (widget-get widget :must-match))
2101 (answer (read-file-name (concat menu-tag ": (defalt `" value "') ")
2102 dir nil must-match file)))
2103 (widget-value-set widget (abbreviate-file-name answer))
2104 (widget-apply widget :notify widget event)
2107 (define-widget 'directory 'file
2108 "A directory widget.
2109 It will read a directory name from the minibuffer when activated."
2112 (define-widget 'symbol 'string
2116 :match (lambda (widget value) (symbolp value))
2117 :value-to-internal (lambda (widget value)
2121 :value-to-external (lambda (widget value)
2126 (define-widget 'function 'sexp
2127 ;; Should complete on functions.
2131 (define-widget 'variable 'symbol
2132 ;; Should complete on variables.
2136 (define-widget 'sexp 'string
2137 "An arbitrary lisp expression."
2138 :tag "Lisp expression"
2140 :validate 'widget-sexp-validate
2141 :match (lambda (widget value) t)
2142 :value-to-internal 'widget-sexp-value-to-internal
2143 :value-to-external (lambda (widget value) (read value)))
2145 (defun widget-sexp-value-to-internal (widget value)
2146 ;; Use pp for printer representation.
2147 (let ((pp (pp-to-string value)))
2148 (while (string-match "\n\\'" pp)
2149 (setq pp (substring pp 0 -1)))
2150 (if (or (string-match "\n\\'" pp)
2155 (defun widget-sexp-validate (widget)
2156 ;; Valid if we can read the string and there is no junk left after it.
2158 (let ((buffer (set-buffer (get-buffer-create " *Widget Scratch*"))))
2160 (insert (widget-apply widget :value-get))
2161 (goto-char (point-min))
2162 (condition-case data
2163 (let ((value (read buffer)))
2165 (if (widget-apply widget :match value)
2167 (widget-put widget :error (widget-get widget :type-error))
2170 :error (format "Junk at end of expression: %s"
2171 (buffer-substring (point)
2174 (error (widget-put widget :error (error-message-string data))
2177 (define-widget 'integer 'sexp
2181 :type-error "This field should contain an integer"
2182 :value-to-internal (lambda (widget value)
2183 (if (integerp value)
2184 (prin1-to-string value)
2186 :match (lambda (widget value) (integerp value)))
2188 (define-widget 'character 'string
2193 :format "%{%t%}: %v\n"
2194 :type-error "This field should contain a character"
2195 :value-to-internal (lambda (widget value)
2196 (if (integerp value)
2197 (char-to-string value)
2199 :value-to-external (lambda (widget value)
2203 :match (lambda (widget value) (integerp value)))
2205 (define-widget 'number 'sexp
2206 "A floating point number."
2209 :type-error "This field should contain a number"
2210 :value-to-internal (lambda (widget value)
2212 (prin1-to-string value)
2214 :match (lambda (widget value) (numberp value)))
2216 (define-widget 'list 'group
2219 :format "%{%t%}:\n%v")
2221 (define-widget 'vector 'group
2224 :format "%{%t%}:\n%v"
2225 :match 'widget-vector-match
2226 :value-to-internal (lambda (widget value) (append value nil))
2227 :value-to-external (lambda (widget value) (apply 'vector value)))
2229 (defun widget-vector-match (widget value)
2230 (and (vectorp value)
2231 (widget-group-match widget
2232 (widget-apply :value-to-internal widget value))))
2234 (define-widget 'cons 'group
2237 :format "%{%t%}:\n%v"
2238 :match 'widget-cons-match
2239 :value-to-internal (lambda (widget value)
2240 (list (car value) (cdr value)))
2241 :value-to-external (lambda (widget value)
2242 (cons (nth 0 value) (nth 1 value))))
2244 (defun widget-cons-match (widget value)
2246 (widget-group-match widget
2247 (widget-apply widget :value-to-internal value))))
2249 (define-widget 'choice 'menu-choice
2250 "A union of several sexp types."
2252 :format "%[%t%]: %v")
2254 (define-widget 'radio 'radio-button-choice
2255 "A union of several sexp types."
2257 :format "%{%t%}:\n%v")
2259 (define-widget 'repeat 'editable-list
2260 "A variable length homogeneous list."
2262 :format "%{%t%}:\n%v%i\n")
2264 (define-widget 'set 'checklist
2265 "A list of members from a fixed set."
2267 :format "%{%t%}:\n%v")
2269 (define-widget 'boolean 'toggle
2270 "To be nil or non-nil, that is the question."
2272 :format "%{%t%}: %[%v%]\n")
2274 ;;; The `color' Widget.
2276 (define-widget 'color-item 'choice-item
2277 "A color name (with sample)."
2278 :format "%v (%[sample%])\n"
2279 :button-face-get 'widget-color-item-button-face-get)
2281 (defun widget-color-item-button-face-get (widget)
2282 ;; We create a face from the value.
2285 (facemenu-get-face (intern (concat "fg:" (widget-value widget))))
2288 (define-widget 'color 'push-button
2289 "Choose a color name (with sample)."
2290 :format "%[%t%]: %v"
2293 :value-create 'widget-color-value-create
2294 :value-delete 'widget-children-value-delete
2295 :value-get 'widget-color-value-get
2296 :value-set 'widget-color-value-set
2297 :action 'widget-color-action
2298 :match 'widget-field-match
2301 (defvar widget-color-choice-list nil)
2302 ;; Variable holding the possible colors.
2304 (defun widget-color-choice-list ()
2305 (unless widget-color-choice-list
2306 (setq widget-color-choice-list
2307 (mapcar '(lambda (color) (list color))
2308 (x-defined-colors))))
2309 widget-color-choice-list)
2311 (defun widget-color-value-create (widget)
2312 (let ((child (widget-create-child-and-convert
2313 widget 'color-item (widget-get widget :value))))
2314 (widget-put widget :children (list child))))
2316 (defun widget-color-value-get (widget)
2317 ;; Pass command to first child.
2318 (widget-apply (car (widget-get widget :children)) :value-get))
2320 (defun widget-color-value-set (widget value)
2321 ;; Pass command to first child.
2322 (widget-apply (car (widget-get widget :children)) :value-set value))
2324 (defvar widget-color-history nil
2325 "History of entered colors")
2327 (defun widget-color-action (widget &optional event)
2328 ;; Prompt for a color.
2329 (let* ((tag (widget-apply widget :menu-tag-get))
2330 (prompt (concat tag ": "))
2331 (answer (cond ((string-match "XEmacs" emacs-version)
2332 (read-color prompt))
2333 ((fboundp 'x-defined-colors)
2334 (completing-read (concat tag ": ")
2335 (widget-color-choice-list)
2336 nil nil nil 'widget-color-history))
2338 (read-string prompt (widget-value widget))))))
2339 (unless (zerop (length answer))
2340 (widget-value-set widget answer)
2341 (widget-apply widget :notify widget event)
2346 (defun widget-echo-help-mouse ()
2347 "Display the help message for the widget under the mouse.
2348 Enable with (run-with-idle-timer 1 t 'widget-echo-help-mouse)"
2349 (let* ((pos (mouse-position))
2353 (win (window-at x y frame))
2354 (where (coordinates-in-window-p (cons x y) win)))
2356 (save-window-excursion
2357 (progn ; save-excursion
2359 (let* ((result (compute-motion (window-start win)
2364 (cons (window-hscroll) 0)
2366 (when (and (eq (nth 1 result) x)
2367 (eq (nth 2 result) y))
2368 (widget-echo-help (nth 0 result))))))))
2370 (setq track-mouse t)
2371 (add-hook 'post-command-hook 'widget-stop-mouse-tracking)))
2373 (defun widget-stop-mouse-tracking (&rest args)
2374 "Stop the mouse tracking done while idle."
2375 (remove-hook 'post-command-hook 'widget-stop-mouse-tracking)
2376 (setq track-mouse nil))
2378 (defun widget-at (pos)
2379 "The button or field at POS."
2380 (or (get-text-property pos 'button)
2381 (get-text-property pos 'field)))
2383 (defun widget-echo-help (pos)
2384 "Display the help echo for widget at POS."
2385 (let* ((widget (widget-at pos))
2386 (help-echo (and widget (widget-get widget :help-echo))))
2387 (cond ((stringp help-echo)
2388 (message "%s" help-echo))
2389 ((and (symbolp help-echo) (fboundp help-echo)
2390 (stringp (setq help-echo (funcall help-echo widget))))
2391 (message "%s" help-echo)))))
2397 ;; wid-edit.el ends here