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/
24 (autoload 'pp-to-string "pp")
25 (autoload 'Info-goto-node "info")
27 (when (string-match "XEmacs" emacs-version)
30 (error (load-library "x-overlay"))))
32 (if (string-match "XEmacs" emacs-version)
33 ;; XEmacs spell `intangible' as `atomic'.
34 (defun widget-make-intangible (from to side)
35 "Make text between FROM and TO atomic with regard to movement.
36 Third argument should be `start-open' if it should be sticky to the rear,
37 and `end-open' if it should sticky to the front."
38 (require 'atomic-extents)
39 (let ((ext (make-extent from to)))
40 ;; XEmacs doesn't understant different kinds of read-only, so
41 ;; we have to use extents instead.
42 (put-text-property from to 'read-only nil)
43 (set-extent-property ext 'read-only t)
44 (set-extent-property ext 'start-open nil)
45 (set-extent-property ext 'end-open nil)
46 (set-extent-property ext side t)
47 (set-extent-property ext 'atomic t)))
48 (defun widget-make-intangible (from to size)
49 "Make text between FROM and TO intangible."
50 (put-text-property from to 'intangible 'front)))
52 ;; The following should go away when bundled with Emacs.
57 (unless (and (featurep 'custom) (fboundp 'custom-declare-variable))
58 ;; We have the old custom-library, hack around it!
59 (defmacro defgroup (&rest args) nil)
60 (defmacro defcustom (var value doc &rest args)
61 `(defvar ,var ,value ,doc))
62 (defmacro defface (&rest args) nil)
63 (define-widget-keywords :prefix :tag :load :link :options :type :group)
64 (when (fboundp 'copy-face)
65 (copy-face 'default 'widget-documentation-face)
66 (copy-face 'bold 'widget-button-face)
67 (copy-face 'italic 'widget-field-face)))
69 (unless (fboundp 'event-point)
70 ;; XEmacs function missing in Emacs.
71 (defun event-point (event)
72 "Return the character position of the given mouse-motion, button-press,
73 or button-release event. If the event did not occur over a window, or did
74 not occur over text, then this returns nil. Otherwise, it returns an index
75 into the buffer visible in the event's window."
76 (posn-point (event-start event))))
78 (unless (fboundp 'error-message-string)
79 ;; Emacs function missing in XEmacs.
80 (defun error-message-string (obj)
81 "Convert an error value to an error message."
82 (let ((buf (get-buffer-create " *error-message*")))
84 (display-error obj buf)
85 (buffer-string buf)))))
90 "Customization support for the Widget Library."
91 :link '(custom-manual "(widget)Top")
92 :link '(url-link :tag "Development Page"
93 "http://www.dina.kvl.dk/~abraham/custom/")
99 (defface widget-documentation-face '((((class color)
101 (:foreground "lime green"))
104 (:foreground "dark green"))
106 "Face used for documentation text."
109 (defface widget-button-face '((t (:bold t)))
110 "Face used for widget buttons."
113 (defcustom widget-mouse-face 'highlight
114 "Face used for widget buttons when the mouse is above them."
118 (defface widget-field-face '((((class grayscale color)
120 (:background "light gray"))
121 (((class grayscale color)
123 (:background "dark gray"))
126 "Face used for editable fields."
129 (defcustom widget-menu-max-size 40
130 "Largest number of items allowed in a popup-menu.
131 Larger menus are read through the minibuffer."
135 ;;; Utility functions.
137 ;; These are not really widget specific.
139 (defsubst widget-plist-member (plist prop)
140 ;; Return non-nil if PLIST has the property PROP.
141 ;; PLIST is a property list, which is a list of the form
142 ;; (PROP1 VALUE1 PROP2 VALUE2 ...). PROP is a symbol.
143 ;; Unlike `plist-get', this allows you to distinguish between a missing
144 ;; property and a property with the value nil.
145 ;; The value is actually the tail of PLIST whose car is PROP.
146 (while (and plist (not (eq (car plist) prop)))
147 (setq plist (cdr (cdr plist))))
150 (defun widget-princ-to-string (object)
151 ;; Return string representation of OBJECT, any Lisp object.
152 ;; No quoting characters are used; no delimiters are printed around
153 ;; the contents of strings.
155 (set-buffer (get-buffer-create " *widget-tmp*"))
157 (let ((standard-output (current-buffer)))
161 (defun widget-clear-undo ()
162 "Clear all undo information."
163 (buffer-disable-undo (current-buffer))
164 (buffer-enable-undo))
166 (defun widget-choose (title items &optional event)
167 "Choose an item from a list.
169 First argument TITLE is the name of the list.
170 Second argument ITEMS is an alist (NAME . VALUE).
171 Optional third argument EVENT is an input event.
173 The user is asked to choose between each NAME from the items alist,
174 and the VALUE of the chosen element will be returned. If EVENT is a
175 mouse event, and the number of elements in items is less than
176 `widget-menu-max-size', a popup menu will be used, otherwise the
178 (cond ((and (< (length items) widget-menu-max-size)
179 event (fboundp 'x-popup-menu) window-system)
180 ;; We are in Emacs-19, pressed by the mouse
182 (list title (cons "" items))))
183 ((and (< (length items) widget-menu-max-size)
184 event (fboundp 'popup-menu) window-system)
185 ;; We are in XEmacs, pressed by the mouse
186 (let ((val (get-popup-menu-response
191 (vector (car x) (list (car x)) t)))
194 (listp (event-object val))
195 (stringp (car-safe (event-object val)))
196 (car (event-object val))))
197 (cdr (assoc val items))))
199 (let ((val (completing-read (concat title ": ") items nil t)))
201 (let ((try (try-completion val items)))
204 (cdr (assoc val items)))
207 (defun widget-get-sibling (widget)
208 "Get the item WIDGET is assumed to toggle.
209 This is only meaningful for radio buttons or checkboxes in a list."
210 (let* ((parent (widget-get widget :parent))
211 (children (widget-get parent :children))
215 (setq child (car children)
216 children (cdr children))
217 (when (eq (widget-get child :button) widget)
218 (throw 'child child)))
221 ;;; Widget text specifications.
223 ;; These functions are for specifying text properties.
225 (defun widget-specify-none (from to)
226 ;; Clear all text properties between FROM and TO.
227 (set-text-properties from to nil))
229 (defun widget-specify-text (from to)
230 ;; Default properties.
231 (add-text-properties from to (list 'read-only t
235 'rear-nonsticky nil)))
237 (defun widget-specify-field (widget from to)
238 ;; Specify editable button for WIDGET between FROM and TO.
239 (widget-specify-field-update widget from to)
241 ;; Make it possible to edit the front end of the field.
242 (add-text-properties (1- from) from (list 'rear-nonsticky t
245 (when (or (string-match "\\(.\\|\n\\)%v" (widget-get widget :format))
246 (widget-get widget :hide-front-space))
247 ;; WARNING: This is going to lose horrible if the character just
248 ;; before the field can be modified (e.g. if it belongs to a
249 ;; choice widget). We try to compensate by checking the format
250 ;; string, and hope the user hasn't changed the :create method.
251 (widget-make-intangible (- from 2) from 'end-open))
253 ;; Make it possible to edit back end of the field.
254 (add-text-properties to (1+ to) (list 'front-sticky nil
258 (cond ((widget-get widget :size)
259 (put-text-property to (1+ to) 'invisible t)
260 (when (or (string-match "%v\\(.\\|\n\\)" (widget-get widget :format))
261 (widget-get widget :hide-rear-space))
262 ;; WARNING: This is going to lose horrible if the character just
263 ;; after the field can be modified (e.g. if it belongs to a
264 ;; choice widget). We try to compensate by checking the format
265 ;; string, and hope the user hasn't changed the :create method.
266 (widget-make-intangible to (+ to 2) 'start-open)))
267 ((string-match "XEmacs" emacs-version)
268 ;; XEmacs does not allow you to insert before a read-only
269 ;; character, even if it is start.open.
270 ;; XEmacs does allow you to delete an read-only extent, so
271 ;; making the terminating newline read only doesn't help.
272 ;; I tried putting an invisible intangible read-only space
273 ;; before the newline, which gave really weird effects.
274 ;; So for now, we just have trust the user not to delete the
276 (put-text-property to (1+ to) 'read-only nil))))
278 (defun widget-specify-field-update (widget from to)
279 ;; Specify editable button for WIDGET between FROM and TO.
280 (let ((map (widget-get widget :keymap))
281 (secret (widget-get widget :secret))
283 (size (widget-get widget :size))
284 (face (or (widget-get widget :value-face)
286 (help-echo (widget-get widget :help-echo))
287 (help-property (if (featurep 'balloon-help)
290 (unless (or (stringp help-echo) (null help-echo))
291 (setq help-echo 'widget-mouse-help))
297 (eq (char-after (1- secret-to)) ?\ ))
298 (setq secret-to (1- secret-to)))
302 (while (< (point) secret-to)
303 (let ((old (get-text-property (point) 'secret)))
305 (subst-char-in-region (point) (1+ (point)) secret old)))
308 (set-text-properties from to (list 'field widget
312 help-property help-echo
318 (while (< (point) secret-to)
319 (let ((old (following-char)))
320 (subst-char-in-region (point) (1+ (point)) old secret)
321 (put-text-property (point) (1+ (point)) 'secret old))
324 (unless (widget-get widget :size)
325 (add-text-properties to (1+ to) (list 'field widget
326 help-property help-echo
328 (add-text-properties to (1+ to) (list 'local-map map
331 (defun widget-specify-button (widget from to)
332 ;; Specify button for WIDGET between FROM and TO.
333 (let ((face (widget-apply widget :button-face-get))
334 (help-echo (widget-get widget :help-echo))
335 (help-property (if (featurep 'balloon-help)
338 (unless (or (null help-echo) (stringp help-echo))
339 (setq help-echo 'widget-mouse-help))
340 (add-text-properties from to (list 'button widget
341 'mouse-face widget-mouse-face
344 help-property help-echo
347 (defun widget-mouse-help (extent)
348 "Find mouse help string for button in extent."
349 (let* ((widget (widget-at (extent-start-position extent)))
350 (help-echo (and widget (widget-get widget :help-echo))))
351 (cond ((stringp help-echo)
353 ((and (symbolp help-echo) (fboundp help-echo)
354 (stringp (setq help-echo (funcall help-echo widget))))
357 (format "(widget %S :help-echo %S)" widget help-echo)))))
359 (defun widget-specify-sample (widget from to)
360 ;; Specify sample for WIDGET between FROM and TO.
361 (let ((face (widget-apply widget :sample-face-get)))
363 (add-text-properties from to (list 'start-open t
367 (defun widget-specify-doc (widget from to)
368 ;; Specify documentation for WIDGET between FROM and TO.
369 (add-text-properties from to (list 'widget-doc widget
370 'face 'widget-documentation-face)))
372 (defmacro widget-specify-insert (&rest form)
373 ;; Execute FORM without inheriting any text properties.
375 (let ((inhibit-read-only t)
377 after-change-functions)
379 (narrow-to-region (- (point) 2) (point))
380 (widget-specify-none (point-min) (point-max))
381 (goto-char (1+ (point-min)))
382 (setq result (progn ,@form))
383 (delete-region (point-min) (1+ (point-min)))
384 (delete-region (1- (point-max)) (point-max))
385 (goto-char (point-max))
388 (defface widget-inactive-face '((((class grayscale color)
390 (:foreground "light gray"))
391 (((class grayscale color)
393 (:foreground "dark gray"))
396 "Face used for inactive widgets."
399 (defun widget-specify-inactive (widget from to)
400 "Make WIDGET inactive for user modifications."
401 (unless (widget-get widget :inactive)
402 (let ((overlay (make-overlay from to nil t nil)))
403 (overlay-put overlay 'face 'widget-inactive-face)
404 (overlay-put overlay 'evaporate 't)
405 (overlay-put overlay (if (string-match "XEmacs" emacs-version)
407 'modification-hooks) '(widget-overlay-inactive))
408 (widget-put widget :inactive overlay))))
410 (defun widget-overlay-inactive (&rest junk)
411 "Ignoring the arguments, signal an error."
412 (unless inhibit-read-only
413 (error "Attempt to modify inactive widget")))
416 (defun widget-specify-active (widget)
417 "Make WIDGET active for user modifications."
418 (let ((inactive (widget-get widget :inactive)))
420 (delete-overlay inactive)
421 (widget-put widget :inactive nil))))
423 ;;; Widget Properties.
425 (defsubst widget-type (widget)
426 "Return the type of WIDGET, a symbol."
429 (defun widget-put (widget property value)
430 "In WIDGET set PROPERTY to VALUE.
431 The value can later be retrived with `widget-get'."
432 (setcdr widget (plist-put (cdr widget) property value)))
434 (defun widget-get (widget property)
435 "In WIDGET, get the value of PROPERTY.
436 The value could either be specified when the widget was created, or
437 later with `widget-put'."
441 (cond ((setq tmp (widget-plist-member (cdr widget) property))
442 (setq value (car (cdr tmp))
444 ((setq tmp (car widget))
445 (setq widget (get tmp 'widget-type)))
447 (setq missing nil))))
450 (defun widget-member (widget property)
451 "Non-nil iff there is a definition in WIDGET for PROPERTY."
452 (cond ((widget-plist-member (cdr widget) property)
455 (widget-member (get (car widget) 'widget-type) property))
459 (defun widget-apply (widget property &rest args)
460 "Apply the value of WIDGET's PROPERTY to the widget itself.
461 ARGS are passed as extra arguments to the function."
462 (apply (widget-get widget property) widget args))
464 (defun widget-value (widget)
465 "Extract the current value of WIDGET."
467 :value-to-external (widget-apply widget :value-get)))
469 (defun widget-value-set (widget value)
470 "Set the current value of WIDGET to VALUE."
472 :value-set (widget-apply widget
473 :value-to-internal value)))
475 (defun widget-match-inline (widget vals)
476 ;; In WIDGET, match the start of VALS.
477 (cond ((widget-get widget :inline)
478 (widget-apply widget :match-inline vals))
480 (widget-apply widget :match (car vals)))
481 (cons (list (car vals)) (cdr vals)))
484 (defun widget-apply-action (widget &optional event)
485 "Apply :action in WIDGET in response to EVENT."
486 (if (widget-apply widget :active)
487 (widget-apply widget :action event)
488 (error "Attempt to perform action on inactive widget")))
492 (defcustom widget-glyph-directory (concat data-directory "custom/")
493 "Where widget glyphs are located.
494 If this variable is nil, widget will try to locate the directory
495 automatically. This does not work yet."
499 (defcustom widget-glyph-enable t
500 "If non nil, use glyphs in images when available."
504 (defun widget-glyph-insert (widget tag image)
505 "In WIDGET, insert the text TAG or, if supported, IMAGE.
506 IMAGE should either be a glyph, or a name sans extension of an xpm or
507 xbm file located in `widget-glyph-directory'.
509 WARNING: If you call this with a glyph, and you want the user to be
510 able to activate the glyph, make sure it is unique. If you use the
511 same glyph for multiple widgets, activating any of the glyphs will
512 cause the last created widget to be activated."
513 (cond ((not (and (string-match "XEmacs" emacs-version)
515 (fboundp 'make-glyph)
517 ;; We don't want or can't use glyphs.
519 ((and (fboundp 'glyphp)
521 ;; Already a glyph. Insert it.
522 (widget-glyph-insert-glyph widget tag image))
524 ;; A string. Look it up in.
525 (let ((file (concat widget-glyph-directory
526 (if (string-match "/\\'" widget-glyph-directory)
530 (if (featurep 'xpm) ".xpm" ".xbm"))))
531 (if (file-readable-p file)
532 (widget-glyph-insert-glyph widget tag (make-glyph file))
533 ;; File not readable, give up.
536 (defun widget-glyph-insert-glyph (widget tag glyph)
537 "In WIDGET, with alternative text TAG, insert GLYPH."
538 (set-glyph-image glyph (cons 'tty tag))
539 (set-glyph-property glyph 'widget widget)
541 (add-text-properties (1- (point)) (point)
544 (let ((help-echo (widget-get widget :help-echo)))
546 (let ((extent (extent-at (1- (point)) nil 'end-glyph))
547 (help-property (if (featurep 'balloon-help)
550 (set-extent-property extent help-property (if (stringp help-echo)
552 'widget-mouse-help))))))
554 ;;; Creating Widgets.
557 (defun widget-create (type &rest args)
558 "Create widget of TYPE.
559 The optional ARGS are additional keyword arguments."
560 (let ((widget (apply 'widget-convert type args)))
561 (widget-apply widget :create)
564 (defun widget-create-child-and-convert (parent type &rest args)
565 "As part of the widget PARENT, create a child widget TYPE.
566 The child is converted, using the keyword arguments ARGS."
567 (let ((widget (apply 'widget-convert type args)))
568 (widget-put widget :parent parent)
569 (unless (widget-get widget :indent)
570 (widget-put widget :indent (+ (or (widget-get parent :indent) 0)
571 (or (widget-get widget :extra-offset) 0)
572 (widget-get parent :offset))))
573 (widget-apply widget :create)
576 (defun widget-create-child (parent type)
577 "Create widget of TYPE."
578 (let ((widget (copy-list type)))
579 (widget-put widget :parent parent)
580 (unless (widget-get widget :indent)
581 (widget-put widget :indent (+ (or (widget-get parent :indent) 0)
582 (or (widget-get widget :extra-offset) 0)
583 (widget-get parent :offset))))
584 (widget-apply widget :create)
587 (defun widget-create-child-value (parent type value)
588 "Create widget of TYPE with value VALUE."
589 (let ((widget (copy-list type)))
590 (widget-put widget :value (widget-apply widget :value-to-internal value))
591 (widget-put widget :parent parent)
592 (unless (widget-get widget :indent)
593 (widget-put widget :indent (+ (or (widget-get parent :indent) 0)
594 (or (widget-get widget :extra-offset) 0)
595 (widget-get parent :offset))))
596 (widget-apply widget :create)
600 (defun widget-delete (widget)
602 (widget-apply widget :delete))
604 (defun widget-convert (type &rest args)
605 "Convert TYPE to a widget without inserting it in the buffer.
606 The optional ARGS are additional keyword arguments."
607 ;; Don't touch the type.
608 (let* ((widget (if (symbolp type)
613 ;; First set the :args keyword.
614 (while (cdr current) ;Look in the type.
615 (let ((next (car (cdr current))))
616 (if (and (symbolp next) (eq (aref (symbol-name next) 0) ?:))
617 (setq current (cdr (cdr current)))
618 (setcdr current (list :args (cdr current)))
619 (setq current nil))))
620 (while args ;Look in the args.
621 (let ((next (nth 0 args)))
622 (if (and (symbolp next) (eq (aref (symbol-name next) 0) ?:))
623 (setq args (nthcdr 2 args))
624 (widget-put widget :args args)
626 ;; Then Convert the widget.
629 (let ((convert-widget (plist-get (cdr type) :convert-widget)))
631 (setq widget (funcall convert-widget widget))))
632 (setq type (get (car type) 'widget-type)))
633 ;; Finally set the keyword args.
635 (let ((next (nth 0 keys)))
636 (if (and (symbolp next) (eq (aref (symbol-name next) 0) ?:))
638 (widget-put widget next (nth 1 keys))
639 (setq keys (nthcdr 2 keys)))
641 ;; Convert the :value to internal format.
642 (if (widget-member widget :value)
643 (let ((value (widget-get widget :value)))
645 :value (widget-apply widget :value-to-internal value))))
646 ;; Return the newly create widget.
649 (defun widget-insert (&rest args)
650 "Call `insert' with ARGS and make the text read only."
651 (let ((inhibit-read-only t)
652 after-change-functions
655 (widget-specify-text from (point))))
657 ;;; Keymap and Commands.
659 (defvar widget-keymap nil
660 "Keymap containing useful binding for buffers containing widgets.
661 Recommended as a parent keymap for modes using widgets.")
663 (unless widget-keymap
664 (setq widget-keymap (make-sparse-keymap))
665 (define-key widget-keymap "\C-k" 'widget-kill-line)
666 (define-key widget-keymap "\t" 'widget-forward)
667 (define-key widget-keymap "\M-\t" 'widget-backward)
668 (define-key widget-keymap [(shift tab)] 'widget-backward)
669 (define-key widget-keymap [backtab] 'widget-backward)
670 (if (string-match "XEmacs" (emacs-version))
672 (define-key widget-keymap [button2] 'widget-button-click)
673 (define-key widget-keymap [button1] 'widget-button1-click))
674 (define-key widget-keymap [mouse-2] 'ignore)
675 (define-key widget-keymap [down-mouse-2] 'widget-button-click))
676 (define-key widget-keymap "\C-m" 'widget-button-press))
678 (defvar widget-global-map global-map
679 "Keymap used for events the widget does not handle themselves.")
680 (make-variable-buffer-local 'widget-global-map)
682 (defvar widget-field-keymap nil
683 "Keymap used inside an editable field.")
685 (unless widget-field-keymap
686 (setq widget-field-keymap (copy-keymap widget-keymap))
687 (unless (string-match "XEmacs" (emacs-version))
688 (define-key widget-field-keymap [menu-bar] 'nil))
689 (define-key widget-field-keymap "\C-m" 'widget-field-activate)
690 (define-key widget-field-keymap "\C-a" 'widget-beginning-of-line)
691 (define-key widget-field-keymap "\C-e" 'widget-end-of-line)
692 (set-keymap-parent widget-field-keymap global-map))
694 (defvar widget-text-keymap nil
695 "Keymap used inside a text field.")
697 (unless widget-text-keymap
698 (setq widget-text-keymap (copy-keymap widget-keymap))
699 (unless (string-match "XEmacs" (emacs-version))
700 (define-key widget-text-keymap [menu-bar] 'nil))
701 (define-key widget-text-keymap "\C-a" 'widget-beginning-of-line)
702 (define-key widget-text-keymap "\C-e" 'widget-end-of-line)
703 (set-keymap-parent widget-text-keymap global-map))
705 (defun widget-field-activate (pos &optional event)
706 "Activate the ediable field at point."
708 (let ((field (get-text-property pos 'field)))
710 (widget-apply-action field event)
712 (lookup-key widget-global-map (this-command-keys))))))
714 (defun widget-button-click (event)
715 "Activate button below mouse pointer."
717 (cond ((and (fboundp 'event-glyph)
719 (let ((widget (glyph-property (event-glyph event) 'widget)))
721 (widget-apply-action widget event)
722 (message "You clicked on a glyph."))))
724 (let ((button (get-text-property (event-point event) 'button)))
726 (widget-apply-action button event)
728 (or (lookup-key widget-global-map [ button2 ])
729 (lookup-key widget-global-map [ down-mouse-2 ])
730 (lookup-key widget-global-map [ mouse-2]))))))
732 (message "You clicked somewhere weird."))))
734 (defun widget-button1-click (event)
735 "Activate glyph below mouse pointer."
737 (if (and (fboundp 'event-glyph)
739 (let ((widget (glyph-property (event-glyph event) 'widget)))
741 (widget-apply-action widget event)
742 (message "You clicked on a glyph.")))
743 (call-interactively (lookup-key widget-global-map (this-command-keys)))))
745 (defun widget-button-press (pos &optional event)
746 "Activate button at POS."
748 (let ((button (get-text-property pos 'button)))
750 (widget-apply-action button event)
751 (let ((command (lookup-key widget-global-map (this-command-keys))))
752 (when (commandp command)
753 (call-interactively command))))))
755 (defun widget-move (arg)
756 "Move point to the ARG next field or button.
757 ARG may be negative to move backward."
760 (let ((next (cond ((get-text-property (point) 'button)
761 (next-single-property-change (point) 'button))
762 ((get-text-property (point) 'field)
763 (next-single-property-change (point) 'field))
766 (if (null next) ; Widget extends to end. of buffer
767 (setq next (point-min)))
768 (let ((button (next-single-property-change next 'button))
769 (field (next-single-property-change next 'field)))
770 (cond ((or (get-text-property next 'button)
771 (get-text-property next 'field))
774 (goto-char (min button field)))
775 (button (goto-char button))
776 (field (goto-char field))
778 (let ((button (next-single-property-change (point-min) 'button))
779 (field (next-single-property-change (point-min) 'field)))
780 (cond ((and button field) (goto-char (min button field)))
781 (button (goto-char button))
782 (field (goto-char field))
784 (error "No buttons or fields found"))))))
785 (setq button (widget-at (point)))
786 (if (and button (widget-get button :tab-order)
787 (< (widget-get button :tab-order) 0))
788 (setq arg (1+ arg))))))
790 (if (= (point-min) (point))
793 (let ((previous (cond ((get-text-property (1- (point)) 'button)
794 (previous-single-property-change (point) 'button))
795 ((get-text-property (1- (point)) 'field)
796 (previous-single-property-change (point) 'field))
799 (if (null previous) ; Widget extends to beg. of buffer
800 (setq previous (point-max)))
801 (let ((button (previous-single-property-change previous 'button))
802 (field (previous-single-property-change previous 'field)))
803 (cond ((and button field)
804 (goto-char (max button field)))
805 (button (goto-char button))
806 (field (goto-char field))
808 (let ((button (previous-single-property-change
809 (point-max) 'button))
810 (field (previous-single-property-change
811 (point-max) 'field)))
812 (cond ((and button field) (goto-char (max button field)))
813 (button (goto-char button))
814 (field (goto-char field))
816 (error "No buttons or fields found"))))))))
817 (let ((button (previous-single-property-change (point) 'button))
818 (field (previous-single-property-change (point) 'field)))
819 (cond ((and button field)
820 (goto-char (max button field)))
821 (button (goto-char button))
822 (field (goto-char field)))
823 (setq button (widget-at (point)))
824 (if (and button (widget-get button :tab-order)
825 (< (widget-get button :tab-order) 0))
826 (setq arg (1- arg)))))
827 (widget-echo-help (point))
828 (run-hooks 'widget-move-hook))
830 (defun widget-forward (arg)
831 "Move point to the next field or button.
832 With optional ARG, move across that many fields."
834 (run-hooks 'widget-forward-hook)
837 (defun widget-backward (arg)
838 "Move point to the previous field or button.
839 With optional ARG, move across that many fields."
841 (run-hooks 'widget-backward-hook)
842 (widget-move (- arg)))
844 (defun widget-beginning-of-line ()
845 "Go to beginning of field or beginning of line, whichever is first."
847 (let ((bol (save-excursion (beginning-of-line) (point)))
848 (prev (previous-single-property-change (point) 'field)))
849 (goto-char (max bol (or prev bol)))))
851 (defun widget-end-of-line ()
852 "Go to end of field or end of line, whichever is first."
854 (let ((bol (save-excursion (end-of-line) (point)))
855 (prev (next-single-property-change (point) 'field)))
856 (goto-char (min bol (or prev bol)))))
858 (defun widget-kill-line ()
859 "Kill to end of field or end of line, whichever is first."
861 (let ((field (get-text-property (point) 'field))
862 (newline (save-excursion (search-forward "\n")))
863 (next (next-single-property-change (point) 'field)))
864 (if (and field (> newline next))
865 (kill-region (point) next)
866 (call-interactively 'kill-line))))
868 ;;; Setting up the buffer.
870 (defvar widget-field-new nil)
871 ;; List of all newly created editable fields in the buffer.
872 (make-variable-buffer-local 'widget-field-new)
874 (defvar widget-field-list nil)
875 ;; List of all editable fields in the buffer.
876 (make-variable-buffer-local 'widget-field-list)
878 (defun widget-setup ()
879 "Setup current buffer so editing string widgets works."
880 (let ((inhibit-read-only t)
881 (after-change-functions nil)
883 (while widget-field-new
884 (setq field (car widget-field-new)
885 widget-field-new (cdr widget-field-new)
886 widget-field-list (cons field widget-field-list))
887 (let ((from (widget-get field :value-from))
888 (to (widget-get field :value-to)))
889 (widget-specify-field field from to)
890 (move-marker from (1- from))
891 (move-marker to (1+ to)))))
893 ;; We need to maintain text properties and size of the editing fields.
894 (make-local-variable 'after-change-functions)
895 (if widget-field-list
896 (setq after-change-functions '(widget-after-change))
897 (setq after-change-functions nil)))
899 (defvar widget-field-last nil)
900 ;; Last field containing point.
901 (make-variable-buffer-local 'widget-field-last)
903 (defvar widget-field-was nil)
904 ;; The widget data before the change.
905 (make-variable-buffer-local 'widget-field-was)
907 (defun widget-field-find (pos)
908 ;; Find widget whose editing field is located at POS.
909 ;; Return nil if POS is not inside and editing field.
911 ;; This is only used in `widget-field-modified', since ordinarily
912 ;; you would just test the field property.
913 (let ((fields widget-field-list)
916 (setq field (car fields)
918 (let ((from (widget-get field :value-from))
919 (to (widget-get field :value-to)))
920 (if (and from to (< from pos) (> to pos))
925 (defun widget-after-change (from to old)
926 ;; Adjust field size and text properties.
928 (let ((field (widget-field-find from))
929 (inhibit-read-only t))
931 ((not (eq field (widget-field-find to)))
933 (message "Error: `widget-after-change' called on two fields"))
935 (let ((size (widget-get field :size)))
937 (let ((begin (1+ (widget-get field :value-from)))
938 (end (1- (widget-get field :value-to))))
939 (widget-specify-field-update field begin end)
940 (cond ((< (- end begin) size)
944 (insert-char ?\ (- (+ begin size) end))
945 (widget-specify-field-update field
948 ((> (- end begin) size)
949 ;; Field too large and
950 (if (or (< (point) (+ begin size))
952 ;; Point is outside extra space.
953 (setq begin (+ begin size))
954 ;; Point is within the extra space.
955 (setq begin (point)))
958 (while (and (eq (preceding-char) ?\ )
960 (delete-backward-char 1))))))
961 (widget-specify-field-update field from to)))
962 (widget-apply field :notify field))))
967 ;; These functions are used in the definition of multiple widgets.
969 (defun widget-children-value-delete (widget)
970 "Delete all :children and :buttons in WIDGET."
971 (mapcar 'widget-delete (widget-get widget :children))
972 (widget-put widget :children nil)
973 (mapcar 'widget-delete (widget-get widget :buttons))
974 (widget-put widget :buttons nil))
976 (defun widget-types-convert-widget (widget)
977 "Convert :args as widget types in WIDGET."
978 (widget-put widget :args (mapcar 'widget-convert (widget-get widget :args)))
981 ;;; The `default' Widget.
983 (define-widget 'default nil
984 "Basic widget other widgets are derived from."
985 :value-to-internal (lambda (widget value) value)
986 :value-to-external (lambda (widget value) value)
987 :create 'widget-default-create
990 :format-handler 'widget-default-format-handler
991 :button-face-get 'widget-default-button-face-get
992 :sample-face-get 'widget-default-sample-face-get
993 :delete 'widget-default-delete
994 :value-set 'widget-default-value-set
995 :value-inline 'widget-default-value-inline
996 :menu-tag-get 'widget-default-menu-tag-get
997 :validate (lambda (widget) nil)
998 :active 'widget-default-active
999 :activate 'widget-specify-active
1000 :deactivate 'widget-default-deactivate
1001 :action 'widget-default-action
1002 :notify 'widget-default-notify)
1004 (defun widget-default-create (widget)
1005 "Create WIDGET at point in the current buffer."
1006 (widget-specify-insert
1007 (let ((from (point))
1008 (tag (widget-get widget :tag))
1009 (glyph (widget-get widget :tag-glyph))
1010 (doc (widget-get widget :doc))
1011 button-begin button-end
1012 sample-begin sample-end
1015 (insert (widget-get widget :format))
1017 ;; Parse escapes in format.
1018 (while (re-search-forward "%\\(.\\)" nil t)
1019 (let ((escape (aref (match-string 1) 0)))
1020 (replace-match "" t t)
1021 (cond ((eq escape ?%)
1024 (setq button-begin (point)))
1026 (setq button-end (point)))
1028 (setq sample-begin (point)))
1030 (setq sample-end (point)))
1032 (when (widget-get widget :indent)
1034 (insert-char ? (widget-get widget :indent))))
1037 (widget-glyph-insert widget (or tag "image") glyph))
1041 (let ((standard-output (current-buffer)))
1042 (princ (widget-get widget :value))))))
1045 (setq doc-begin (point))
1047 (while (eq (preceding-char) ?\n)
1048 (delete-backward-char 1))
1050 (setq doc-end (point))))
1052 (if (and button-begin (not button-end))
1053 (widget-apply widget :value-create)
1054 (setq value-pos (point))))
1056 (widget-apply widget :format-handler escape)))))
1057 ;; Specify button, sample, and doc, and insert value.
1058 (and button-begin button-end
1059 (widget-specify-button widget button-begin button-end))
1060 (and sample-begin sample-end
1061 (widget-specify-sample widget sample-begin sample-end))
1062 (and doc-begin doc-end
1063 (widget-specify-doc widget doc-begin doc-end))
1065 (goto-char value-pos)
1066 (widget-apply widget :value-create)))
1067 (let ((from (copy-marker (point-min)))
1068 (to (copy-marker (point-max))))
1069 (widget-specify-text from to)
1070 (set-marker-insertion-type from t)
1071 (set-marker-insertion-type to nil)
1072 (widget-put widget :from from)
1073 (widget-put widget :to to))))
1075 (defun widget-default-format-handler (widget escape)
1076 ;; We recognize the %h escape by default.
1077 (let* ((buttons (widget-get widget :buttons))
1078 (doc-property (widget-get widget :documentation-property))
1079 (doc-try (cond ((widget-get widget :doc))
1080 ((symbolp doc-property)
1081 (documentation-property (widget-get widget :value)
1084 (funcall doc-property (widget-get widget :value)))))
1085 (doc-text (and (stringp doc-try)
1086 (> (length doc-try) 1)
1088 (cond ((eq escape ?h)
1090 (and (eq (preceding-char) ?\n)
1091 (widget-get widget :indent)
1092 (insert-char ? (widget-get widget :indent)))
1093 ;; The `*' in the beginning is redundant.
1094 (when (eq (aref doc-text 0) ?*)
1095 (setq doc-text (substring doc-text 1)))
1096 ;; Get rid of trailing newlines.
1097 (when (string-match "\n+\\'" doc-text)
1098 (setq doc-text (substring doc-text 0 (match-beginning 0))))
1099 (push (if (string-match "\n." doc-text)
1100 ;; Allow multiline doc to be hiden.
1101 (widget-create-child-and-convert
1104 (string-match "\\`.*" doc-text)
1105 (match-string 0 doc-text))
1106 :widget-doc doc-text
1108 ;; A single line is just inserted.
1109 (widget-create-child-and-convert
1110 widget 'item :format "%d" :doc doc-text nil))
1113 (error "Unknown escape `%c'" escape)))
1114 (widget-put widget :buttons buttons)))
1116 (defun widget-default-button-face-get (widget)
1117 ;; Use :button-face or widget-button-face
1118 (or (widget-get widget :button-face) 'widget-button-face))
1120 (defun widget-default-sample-face-get (widget)
1121 ;; Use :sample-face.
1122 (widget-get widget :sample-face))
1124 (defun widget-default-delete (widget)
1125 ;; Remove widget from the buffer.
1126 (let ((from (widget-get widget :from))
1127 (to (widget-get widget :to))
1128 (inhibit-read-only t)
1129 after-change-functions)
1130 (widget-apply widget :value-delete)
1132 ;; Kludge: this doesn't need to be true for empty formats.
1133 (delete-region from to))
1134 (set-marker from nil)
1135 (set-marker to nil)))
1137 (defun widget-default-value-set (widget value)
1138 ;; Recreate widget with new value.
1140 (goto-char (widget-get widget :from))
1141 (widget-apply widget :delete)
1142 (widget-put widget :value value)
1143 (widget-apply widget :create)))
1145 (defun widget-default-value-inline (widget)
1146 ;; Wrap value in a list unless it is inline.
1147 (if (widget-get widget :inline)
1148 (widget-value widget)
1149 (list (widget-value widget))))
1151 (defun widget-default-menu-tag-get (widget)
1152 ;; Use tag or value for menus.
1153 (or (widget-get widget :menu-tag)
1154 (widget-get widget :tag)
1155 (widget-princ-to-string (widget-get widget :value))))
1157 (defun widget-default-active (widget)
1158 "Return t iff this widget active (user modifiable)."
1159 (and (not (widget-get widget :inactive))
1160 (let ((parent (widget-get widget :parent)))
1162 (widget-apply parent :active)))))
1164 (defun widget-default-deactivate (widget)
1165 "Make WIDGET inactive for user modifications."
1166 (widget-specify-inactive widget
1167 (widget-get widget :from)
1168 (widget-get widget :to)))
1170 (defun widget-default-action (widget &optional event)
1171 ;; Notify the parent when a widget change
1172 (let ((parent (widget-get widget :parent)))
1174 (widget-apply parent :notify widget event))))
1176 (defun widget-default-notify (widget child &optional event)
1177 ;; Pass notification to parent.
1178 (widget-default-action widget event))
1180 ;;; The `item' Widget.
1182 (define-widget 'item 'default
1183 "Constant items for inclusion in other widgets."
1184 :convert-widget 'widget-item-convert-widget
1185 :value-create 'widget-item-value-create
1186 :value-delete 'ignore
1187 :value-get 'widget-item-value-get
1188 :match 'widget-item-match
1189 :match-inline 'widget-item-match-inline
1190 :action 'widget-item-action
1193 (defun widget-item-convert-widget (widget)
1194 ;; Initialize :value from :args in WIDGET.
1195 (let ((args (widget-get widget :args)))
1197 (widget-put widget :value (widget-apply widget
1198 :value-to-internal (car args)))
1199 (widget-put widget :args nil)))
1202 (defun widget-item-value-create (widget)
1203 ;; Insert the printed representation of the value.
1204 (let ((standard-output (current-buffer)))
1205 (princ (widget-get widget :value))))
1207 (defun widget-item-match (widget value)
1208 ;; Match if the value is the same.
1209 (equal (widget-get widget :value) value))
1211 (defun widget-item-match-inline (widget values)
1212 ;; Match if the value is the same.
1213 (let ((value (widget-get widget :value)))
1215 (<= (length value) (length values))
1216 (let ((head (subseq values 0 (length value))))
1217 (and (equal head value)
1218 (cons head (subseq values (length value))))))))
1220 (defun widget-item-action (widget &optional event)
1221 ;; Just notify itself.
1222 (widget-apply widget :notify widget event))
1224 (defun widget-item-value-get (widget)
1225 ;; Items are simple.
1226 (widget-get widget :value))
1228 ;;; The `push-button' Widget.
1230 (defcustom widget-push-button-gui t
1231 "If non nil, use GUI push buttons when available."
1235 ;; Cache already created GUI objects.
1236 (defvar widget-push-button-cache nil)
1238 (define-widget 'push-button 'item
1239 "A pushable button."
1240 :value-create 'widget-push-button-value-create
1243 (defun widget-push-button-value-create (widget)
1244 ;; Insert text representing the `on' and `off' states.
1245 (let* ((tag (or (widget-get widget :tag)
1246 (widget-get widget :value)))
1247 (text (concat "[" tag "]"))
1248 (gui (cdr (assoc tag widget-push-button-cache))))
1249 (if (and (fboundp 'make-gui-button)
1250 (fboundp 'make-glyph)
1251 widget-push-button-gui
1252 (fboundp 'device-on-window-system-p)
1253 (device-on-window-system-p)
1254 (string-match "XEmacs" emacs-version))
1257 (setq gui (make-gui-button tag 'widget-gui-action widget))
1258 (push (cons tag gui) widget-push-button-cache))
1259 (widget-glyph-insert-glyph widget text
1260 (make-glyph (car (aref gui 1)))))
1263 (defun widget-gui-action (widget)
1264 "Apply :action for WIDGET."
1265 (widget-apply-action widget (this-command-keys)))
1267 ;;; The `link' Widget.
1269 (define-widget 'link 'item
1271 :help-echo "Follow the link."
1274 ;;; The `info-link' Widget.
1276 (define-widget 'info-link 'link
1277 "A link to an info file."
1278 :action 'widget-info-link-action)
1280 (defun widget-info-link-action (widget &optional event)
1281 "Open the info node specified by WIDGET."
1282 (Info-goto-node (widget-value widget)))
1284 ;;; The `url-link' Widget.
1286 (define-widget 'url-link 'link
1287 "A link to an www page."
1288 :action 'widget-url-link-action)
1290 (defun widget-url-link-action (widget &optional event)
1291 "Open the url specified by WIDGET."
1292 (require 'browse-url)
1293 (funcall browse-url-browser-function (widget-value widget)))
1295 ;;; The `editable-field' Widget.
1297 (define-widget 'editable-field 'default
1298 "An editable text field."
1299 :convert-widget 'widget-item-convert-widget
1300 :keymap widget-field-keymap
1303 :action 'widget-field-action
1304 :validate 'widget-field-validate
1307 :value-create 'widget-field-value-create
1308 :value-delete 'widget-field-value-delete
1309 :value-get 'widget-field-value-get
1310 :match 'widget-field-match)
1312 ;; History of field minibuffer edits.
1313 (defvar widget-field-history nil)
1315 (defun widget-field-action (widget &optional event)
1316 ;; Edit the value in the minibuffer.
1317 (let ((tag (widget-apply widget :menu-tag-get))
1318 (invalid (widget-apply widget :validate)))
1320 (error (widget-get invalid :error)))
1321 (widget-value-set widget
1322 (widget-apply widget
1324 (read-string (concat tag ": ")
1328 (widget-value widget))
1329 'widget-field-history)))
1330 (widget-apply widget :notify widget event)
1333 (defun widget-field-validate (widget)
1334 ;; Valid if the content matches `:valid-regexp'.
1336 (let ((value (widget-apply widget :value-get))
1337 (regexp (widget-get widget :valid-regexp)))
1338 (if (string-match regexp value)
1342 (defun widget-field-value-create (widget)
1343 ;; Create an editable text field.
1345 (let ((size (widget-get widget :size))
1346 (value (widget-get widget :value))
1350 (< (length value) size)
1351 (insert-char ?\ (- size (length value))))
1352 (unless (memq widget widget-field-list)
1353 (setq widget-field-new (cons widget widget-field-new)))
1354 (widget-put widget :value-to (copy-marker (point)))
1355 (set-marker-insertion-type (widget-get widget :value-to) nil)
1359 (widget-put widget :value-from (copy-marker from))
1360 (set-marker-insertion-type (widget-get widget :value-from) t)))
1362 (defun widget-field-value-delete (widget)
1363 ;; Remove the widget from the list of active editing fields.
1364 (setq widget-field-list (delq widget widget-field-list))
1365 ;; These are nil if the :format string doesn't contain `%v'.
1366 (when (widget-get widget :value-from)
1367 (set-marker (widget-get widget :value-from) nil))
1368 (when (widget-get widget :value-from)
1369 (set-marker (widget-get widget :value-to) nil)))
1371 (defun widget-field-value-get (widget)
1372 ;; Return current text in editing field.
1373 (let ((from (widget-get widget :value-from))
1374 (to (widget-get widget :value-to))
1375 (size (widget-get widget :size))
1376 (secret (widget-get widget :secret))
1377 (old (current-buffer)))
1380 (set-buffer (marker-buffer from))
1381 (setq from (1+ from)
1386 (eq (char-after (1- to)) ?\ ))
1388 (let ((result (buffer-substring-no-properties from to)))
1391 (while (< (+ from index) to)
1393 (get-text-property (+ from index) 'secret))
1394 (setq index (1+ index)))))
1397 (widget-get widget :value))))
1399 (defun widget-field-match (widget value)
1400 ;; Match any string.
1403 ;;; The `text' Widget.
1405 (define-widget 'text 'editable-field
1406 :keymap widget-text-keymap
1407 "A multiline text area.")
1409 ;;; The `menu-choice' Widget.
1411 (define-widget 'menu-choice 'default
1412 "A menu of options."
1413 :convert-widget 'widget-types-convert-widget
1414 :format "%[%t%]: %v"
1417 :void '(item :format "invalid (%t)\n")
1418 :value-create 'widget-choice-value-create
1419 :value-delete 'widget-children-value-delete
1420 :value-get 'widget-choice-value-get
1421 :value-inline 'widget-choice-value-inline
1422 :action 'widget-choice-action
1423 :error "Make a choice"
1424 :validate 'widget-choice-validate
1425 :match 'widget-choice-match
1426 :match-inline 'widget-choice-match-inline)
1428 (defun widget-choice-value-create (widget)
1429 ;; Insert the first choice that matches the value.
1430 (let ((value (widget-get widget :value))
1431 (args (widget-get widget :args))
1434 (setq current (car args)
1436 (when (widget-apply current :match value)
1437 (widget-put widget :children (list (widget-create-child-value
1438 widget current value)))
1439 (widget-put widget :choice current)
1443 (let ((void (widget-get widget :void)))
1444 (widget-put widget :children (list (widget-create-child-and-convert
1445 widget void :value value)))
1446 (widget-put widget :choice void)))))
1448 (defun widget-choice-value-get (widget)
1449 ;; Get value of the child widget.
1450 (widget-value (car (widget-get widget :children))))
1452 (defun widget-choice-value-inline (widget)
1453 ;; Get value of the child widget.
1454 (widget-apply (car (widget-get widget :children)) :value-inline))
1456 (defun widget-choice-action (widget &optional event)
1458 (let ((args (widget-get widget :args))
1459 (old (widget-get widget :choice))
1460 (tag (widget-apply widget :menu-tag-get))
1461 (completion-ignore-case (widget-get widget :case-fold))
1463 ;; Remember old value.
1464 (if (and old (not (widget-apply widget :validate)))
1465 (let* ((external (widget-value widget))
1466 (internal (widget-apply old :value-to-internal external)))
1467 (widget-put old :value internal)))
1470 (cond ((= (length args) 0)
1472 ((= (length args) 1)
1474 ((and (= (length args) 2)
1476 (if (eq old (nth 0 args))
1481 (setq current (car args)
1484 (cons (cons (widget-apply current :menu-tag-get)
1487 (widget-choose tag (reverse choices) event))))
1489 (widget-value-set widget
1490 (widget-apply current :value-to-external
1491 (widget-get current :value)))
1492 (widget-apply widget :notify widget event)
1495 (widget-apply widget :notify widget event)
1496 (widget-clear-undo))
1498 (defun widget-choice-validate (widget)
1499 ;; Valid if we have made a valid choice.
1500 (let ((void (widget-get widget :void))
1501 (choice (widget-get widget :choice))
1502 (child (car (widget-get widget :children))))
1503 (if (eq void choice)
1505 (widget-apply child :validate))))
1507 (defun widget-choice-match (widget value)
1508 ;; Matches if one of the choices matches.
1509 (let ((args (widget-get widget :args))
1511 (while (and args (not found))
1512 (setq current (car args)
1514 found (widget-apply current :match value)))
1517 (defun widget-choice-match-inline (widget values)
1518 ;; Matches if one of the choices matches.
1519 (let ((args (widget-get widget :args))
1521 (while (and args (null found))
1522 (setq current (car args)
1524 found (widget-match-inline current values)))
1527 ;;; The `toggle' Widget.
1529 (define-widget 'toggle 'item
1530 "Toggle between two states."
1532 :value-create 'widget-toggle-value-create
1533 :action 'widget-toggle-action
1534 :match (lambda (widget value) t)
1538 (defun widget-toggle-value-create (widget)
1539 ;; Insert text representing the `on' and `off' states.
1540 (if (widget-value widget)
1541 (widget-glyph-insert widget
1542 (widget-get widget :on)
1543 (widget-get widget :on-glyph))
1544 (widget-glyph-insert widget
1545 (widget-get widget :off)
1546 (widget-get widget :off-glyph))))
1548 (defun widget-toggle-action (widget &optional event)
1550 (widget-value-set widget (not (widget-value widget)))
1551 (widget-apply widget :notify widget event))
1553 ;;; The `checkbox' Widget.
1555 (define-widget 'checkbox 'toggle
1556 "A checkbox toggle."
1562 :action 'widget-checkbox-action)
1564 (defun widget-checkbox-action (widget &optional event)
1565 "Toggle checkbox, notify parent, and set active state of sibling."
1566 (widget-toggle-action widget event)
1567 (let ((sibling (widget-get-sibling widget)))
1569 (if (widget-value widget)
1570 (widget-apply sibling :activate)
1571 (widget-apply sibling :deactivate)))))
1573 ;;; The `checklist' Widget.
1575 (define-widget 'checklist 'default
1576 "A multiple choice widget."
1577 :convert-widget 'widget-types-convert-widget
1580 :entry-format "%b %v"
1581 :menu-tag "checklist"
1583 :value-create 'widget-checklist-value-create
1584 :value-delete 'widget-children-value-delete
1585 :value-get 'widget-checklist-value-get
1586 :validate 'widget-checklist-validate
1587 :match 'widget-checklist-match
1588 :match-inline 'widget-checklist-match-inline)
1590 (defun widget-checklist-value-create (widget)
1591 ;; Insert all values
1592 (let ((alist (widget-checklist-match-find widget (widget-get widget :value)))
1593 (args (widget-get widget :args)))
1595 (widget-checklist-add-item widget (car args) (assq (car args) alist))
1596 (setq args (cdr args)))
1597 (widget-put widget :children (nreverse (widget-get widget :children)))))
1599 (defun widget-checklist-add-item (widget type chosen)
1600 ;; Create checklist item in WIDGET of type TYPE.
1601 ;; If the item is checked, CHOSEN is a cons whose cdr is the value.
1602 (and (eq (preceding-char) ?\n)
1603 (widget-get widget :indent)
1604 (insert-char ? (widget-get widget :indent)))
1605 (widget-specify-insert
1606 (let* ((children (widget-get widget :children))
1607 (buttons (widget-get widget :buttons))
1608 (button-args (or (widget-get type :sibling-args)
1609 (widget-get widget :button-args)))
1612 (insert (widget-get widget :entry-format))
1614 ;; Parse % escapes in format.
1615 (while (re-search-forward "%\\([bv%]\\)" nil t)
1616 (let ((escape (aref (match-string 1) 0)))
1617 (replace-match "" t t)
1618 (cond ((eq escape ?%)
1621 (setq button (apply 'widget-create-child-and-convert
1623 :value (not (null chosen))
1628 (let ((child (widget-create-child widget type)))
1629 (widget-apply child :deactivate)
1631 ((widget-get type :inline)
1632 (widget-create-child-value
1633 widget type (cdr chosen)))
1635 (widget-create-child-value
1636 widget type (car (cdr chosen)))))))
1638 (error "Unknown escape `%c'" escape)))))
1639 ;; Update properties.
1640 (and button child (widget-put child :button button))
1641 (and button (widget-put widget :buttons (cons button buttons)))
1642 (and child (widget-put widget :children (cons child children))))))
1644 (defun widget-checklist-match (widget values)
1645 ;; All values must match a type in the checklist.
1647 (null (cdr (widget-checklist-match-inline widget values)))))
1649 (defun widget-checklist-match-inline (widget values)
1650 ;; Find the values which match a type in the checklist.
1651 (let ((greedy (widget-get widget :greedy))
1652 (args (copy-list (widget-get widget :args)))
1655 (let ((answer (widget-checklist-match-up args values)))
1657 (let ((vals (widget-match-inline answer values)))
1658 (setq found (append found (car vals))
1660 args (delq answer args))))
1662 (setq rest (append rest (list (car values)))
1663 values (cdr values)))
1665 (setq rest (append rest values)
1669 (defun widget-checklist-match-find (widget vals)
1670 ;; Find the vals which match a type in the checklist.
1671 ;; Return an alist of (TYPE MATCH).
1672 (let ((greedy (widget-get widget :greedy))
1673 (args (copy-list (widget-get widget :args)))
1676 (let ((answer (widget-checklist-match-up args vals)))
1678 (let ((match (widget-match-inline answer vals)))
1679 (setq found (cons (cons answer (car match)) found)
1681 args (delq answer args))))
1683 (setq vals (cdr vals)))
1688 (defun widget-checklist-match-up (args vals)
1689 ;; Rerturn the first type from ARGS that matches VALS.
1690 (let (current found)
1691 (while (and args (null found))
1692 (setq current (car args)
1694 found (widget-match-inline current vals)))
1699 (defun widget-checklist-value-get (widget)
1700 ;; The values of all selected items.
1701 (let ((children (widget-get widget :children))
1704 (setq child (car children)
1705 children (cdr children))
1706 (if (widget-value (widget-get child :button))
1707 (setq result (append result (widget-apply child :value-inline)))))
1710 (defun widget-checklist-validate (widget)
1711 ;; Ticked chilren must be valid.
1712 (let ((children (widget-get widget :children))
1714 (while (and children (not found))
1715 (setq child (car children)
1716 children (cdr children)
1717 button (widget-get child :button)
1718 found (and (widget-value button)
1719 (widget-apply child :validate))))
1722 ;;; The `option' Widget
1724 (define-widget 'option 'checklist
1725 "An widget with an optional item."
1728 ;;; The `choice-item' Widget.
1730 (define-widget 'choice-item 'item
1731 "Button items that delegate action events to their parents."
1732 :action 'widget-choice-item-action
1733 :format "%[%t%] \n")
1735 (defun widget-choice-item-action (widget &optional event)
1736 ;; Tell parent what happened.
1737 (widget-apply (widget-get widget :parent) :action event))
1739 ;;; The `radio-button' Widget.
1741 (define-widget 'radio-button 'toggle
1742 "A radio button for use in the `radio' widget."
1743 :notify 'widget-radio-button-notify
1748 :off-glyph "radio0")
1750 (defun widget-radio-button-notify (widget child &optional event)
1752 (widget-apply (widget-get widget :parent) :action widget event))
1754 ;;; The `radio-button-choice' Widget.
1756 (define-widget 'radio-button-choice 'default
1757 "Select one of multiple options."
1758 :convert-widget 'widget-types-convert-widget
1761 :entry-format "%b %v"
1763 :value-create 'widget-radio-value-create
1764 :value-delete 'widget-children-value-delete
1765 :value-get 'widget-radio-value-get
1766 :value-inline 'widget-radio-value-inline
1767 :value-set 'widget-radio-value-set
1768 :error "You must push one of the buttons"
1769 :validate 'widget-radio-validate
1770 :match 'widget-choice-match
1771 :match-inline 'widget-choice-match-inline
1772 :action 'widget-radio-action)
1774 (defun widget-radio-value-create (widget)
1775 ;; Insert all values
1776 (let ((args (widget-get widget :args))
1779 (setq arg (car args)
1781 (widget-radio-add-item widget arg))))
1783 (defun widget-radio-add-item (widget type)
1784 "Add to radio widget WIDGET a new radio button item of type TYPE."
1785 ;; (setq type (widget-convert type))
1786 (and (eq (preceding-char) ?\n)
1787 (widget-get widget :indent)
1788 (insert-char ? (widget-get widget :indent)))
1789 (widget-specify-insert
1790 (let* ((value (widget-get widget :value))
1791 (children (widget-get widget :children))
1792 (buttons (widget-get widget :buttons))
1793 (button-args (or (widget-get type :sibling-args)
1794 (widget-get widget :button-args)))
1796 (chosen (and (null (widget-get widget :choice))
1797 (widget-apply type :match value)))
1799 (insert (widget-get widget :entry-format))
1801 ;; Parse % escapes in format.
1802 (while (re-search-forward "%\\([bv%]\\)" nil t)
1803 (let ((escape (aref (match-string 1) 0)))
1804 (replace-match "" t t)
1805 (cond ((eq escape ?%)
1808 (setq button (apply 'widget-create-child-and-convert
1809 widget 'radio-button
1810 :value (not (null chosen))
1813 (setq child (if chosen
1814 (widget-create-child-value
1816 (widget-create-child widget type)))
1818 (widget-apply child :deactivate)))
1820 (error "Unknown escape `%c'" escape)))))
1821 ;; Update properties.
1823 (widget-put widget :choice type))
1825 (widget-put child :button button)
1826 (widget-put widget :buttons (nconc buttons (list button))))
1828 (widget-put widget :children (nconc children (list child))))
1831 (defun widget-radio-value-get (widget)
1832 ;; Get value of the child widget.
1833 (let ((chosen (widget-radio-chosen widget)))
1834 (and chosen (widget-value chosen))))
1836 (defun widget-radio-chosen (widget)
1837 "Return the widget representing the chosen radio button."
1838 (let ((children (widget-get widget :children))
1841 (setq current (car children)
1842 children (cdr children))
1843 (let* ((button (widget-get current :button))
1844 (value (widget-apply button :value-get)))
1850 (defun widget-radio-value-inline (widget)
1851 ;; Get value of the child widget.
1852 (let ((children (widget-get widget :children))
1855 (setq current (car children)
1856 children (cdr children))
1857 (let* ((button (widget-get current :button))
1858 (value (widget-apply button :value-get)))
1860 (setq found (widget-apply current :value-inline)
1864 (defun widget-radio-value-set (widget value)
1865 ;; We can't just delete and recreate a radio widget, since children
1866 ;; can be added after the original creation and won't be recreated
1868 (let ((children (widget-get widget :children))
1871 (setq current (car children)
1872 children (cdr children))
1873 (let* ((button (widget-get current :button))
1874 (match (and (not found)
1875 (widget-apply current :match value))))
1876 (widget-value-set button match)
1879 (widget-value-set current value)
1880 (widget-apply current :activate))
1881 (widget-apply current :deactivate))
1882 (setq found (or found match))))))
1884 (defun widget-radio-validate (widget)
1885 ;; Valid if we have made a valid choice.
1886 (let ((children (widget-get widget :children))
1887 current found button)
1888 (while (and children (not found))
1889 (setq current (car children)
1890 children (cdr children)
1891 button (widget-get current :button)
1892 found (widget-apply button :value-get)))
1894 (widget-apply current :validate)
1897 (defun widget-radio-action (widget child event)
1898 ;; Check if a radio button was pressed.
1899 (let ((children (widget-get widget :children))
1900 (buttons (widget-get widget :buttons))
1902 (when (memq child buttons)
1904 (setq current (car children)
1905 children (cdr children))
1906 (let* ((button (widget-get current :button)))
1907 (cond ((eq child button)
1908 (widget-value-set button t)
1909 (widget-apply current :activate))
1910 ((widget-value button)
1911 (widget-value-set button nil)
1912 (widget-apply current :deactivate)))))))
1913 ;; Pass notification to parent.
1914 (widget-apply widget :notify child event))
1916 ;;; The `insert-button' Widget.
1918 (define-widget 'insert-button 'push-button
1919 "An insert button for the `editable-list' widget."
1921 :help-echo "Insert a new item into the list at this position."
1922 :action 'widget-insert-button-action)
1924 (defun widget-insert-button-action (widget &optional event)
1925 ;; Ask the parent to insert a new item.
1926 (widget-apply (widget-get widget :parent)
1927 :insert-before (widget-get widget :widget)))
1929 ;;; The `delete-button' Widget.
1931 (define-widget 'delete-button 'push-button
1932 "A delete button for the `editable-list' widget."
1934 :help-echo "Delete this item from the list."
1935 :action 'widget-delete-button-action)
1937 (defun widget-delete-button-action (widget &optional event)
1938 ;; Ask the parent to insert a new item.
1939 (widget-apply (widget-get widget :parent)
1940 :delete-at (widget-get widget :widget)))
1942 ;;; The `editable-list' Widget.
1944 (defcustom widget-editable-list-gui nil
1945 "If non nil, use GUI push-buttons in editable list when available."
1949 (define-widget 'editable-list 'default
1950 "A variable list of widgets of the same type."
1951 :convert-widget 'widget-types-convert-widget
1954 :format-handler 'widget-editable-list-format-handler
1955 :entry-format "%i %d %v"
1956 :menu-tag "editable-list"
1957 :value-create 'widget-editable-list-value-create
1958 :value-delete 'widget-children-value-delete
1959 :value-get 'widget-editable-list-value-get
1960 :validate 'widget-editable-list-validate
1961 :match 'widget-editable-list-match
1962 :match-inline 'widget-editable-list-match-inline
1963 :insert-before 'widget-editable-list-insert-before
1964 :delete-at 'widget-editable-list-delete-at)
1966 (defun widget-editable-list-format-handler (widget escape)
1967 ;; We recognize the insert button.
1968 (let ((widget-push-button-gui widget-editable-list-gui))
1969 (cond ((eq escape ?i)
1970 (and (widget-get widget :indent)
1971 (insert-char ? (widget-get widget :indent)))
1972 (apply 'widget-create-child-and-convert
1973 widget 'insert-button
1974 (widget-get widget :append-button-args)))
1976 (widget-default-format-handler widget escape)))))
1978 (defun widget-editable-list-value-create (widget)
1979 ;; Insert all values
1980 (let* ((value (widget-get widget :value))
1981 (type (nth 0 (widget-get widget :args)))
1982 (inlinep (widget-get type :inline))
1984 (widget-put widget :value-pos (copy-marker (point)))
1985 (set-marker-insertion-type (widget-get widget :value-pos) t)
1987 (let ((answer (widget-match-inline type value)))
1989 (setq children (cons (widget-editable-list-entry-create
1998 (widget-put widget :children (nreverse children))))
2000 (defun widget-editable-list-value-get (widget)
2001 ;; Get value of the child widget.
2002 (apply 'append (mapcar (lambda (child) (widget-apply child :value-inline))
2003 (widget-get widget :children))))
2005 (defun widget-editable-list-validate (widget)
2006 ;; All the chilren must be valid.
2007 (let ((children (widget-get widget :children))
2009 (while (and children (not found))
2010 (setq child (car children)
2011 children (cdr children)
2012 found (widget-apply child :validate)))
2015 (defun widget-editable-list-match (widget value)
2016 ;; Value must be a list and all the members must match the type.
2018 (null (cdr (widget-editable-list-match-inline widget value)))))
2020 (defun widget-editable-list-match-inline (widget value)
2021 (let ((type (nth 0 (widget-get widget :args)))
2024 (while (and value ok)
2025 (let ((answer (widget-match-inline type value)))
2027 (setq found (append found (car answer))
2030 (cons found value)))
2032 (defun widget-editable-list-insert-before (widget before)
2033 ;; Insert a new child in the list of children.
2035 (let ((children (widget-get widget :children))
2036 (inhibit-read-only t)
2037 after-change-functions)
2039 (goto-char (widget-get before :entry-from)))
2041 (goto-char (widget-get widget :value-pos))))
2042 (let ((child (widget-editable-list-entry-create
2044 (when (< (widget-get child :entry-from) (widget-get widget :from))
2045 (set-marker (widget-get widget :from)
2046 (widget-get child :entry-from)))
2047 (widget-specify-text (widget-get child :entry-from)
2048 (widget-get child :entry-to))
2049 (if (eq (car children) before)
2050 (widget-put widget :children (cons child children))
2051 (while (not (eq (car (cdr children)) before))
2052 (setq children (cdr children)))
2053 (setcdr children (cons child (cdr children)))))))
2055 widget (widget-apply widget :notify widget))
2057 (defun widget-editable-list-delete-at (widget child)
2058 ;; Delete child from list of children.
2060 (let ((buttons (copy-list (widget-get widget :buttons)))
2062 (inhibit-read-only t)
2063 after-change-functions)
2065 (setq button (car buttons)
2066 buttons (cdr buttons))
2067 (when (eq (widget-get button :widget) child)
2069 :buttons (delq button (widget-get widget :buttons)))
2070 (widget-delete button))))
2071 (let ((entry-from (widget-get child :entry-from))
2072 (entry-to (widget-get child :entry-to))
2073 (inhibit-read-only t)
2074 after-change-functions)
2075 (widget-delete child)
2076 (delete-region entry-from entry-to)
2077 (set-marker entry-from nil)
2078 (set-marker entry-to nil))
2079 (widget-put widget :children (delq child (widget-get widget :children))))
2081 (widget-apply widget :notify widget))
2083 (defun widget-editable-list-entry-create (widget value conv)
2084 ;; Create a new entry to the list.
2085 (let ((type (nth 0 (widget-get widget :args)))
2086 (widget-push-button-gui widget-editable-list-gui)
2087 child delete insert)
2088 (widget-specify-insert
2090 (and (widget-get widget :indent)
2091 (insert-char ? (widget-get widget :indent)))
2092 (insert (widget-get widget :entry-format)))
2093 ;; Parse % escapes in format.
2094 (while (re-search-forward "%\\(.\\)" nil t)
2095 (let ((escape (aref (match-string 1) 0)))
2096 (replace-match "" t t)
2097 (cond ((eq escape ?%)
2100 (setq insert (apply 'widget-create-child-and-convert
2101 widget 'insert-button
2102 (widget-get widget :insert-button-args))))
2104 (setq delete (apply 'widget-create-child-and-convert
2105 widget 'delete-button
2106 (widget-get widget :delete-button-args))))
2109 (setq child (widget-create-child-value
2111 (setq child (widget-create-child widget type))))
2113 (error "Unknown escape `%c'" escape)))))
2115 :buttons (cons delete
2117 (widget-get widget :buttons))))
2118 (let ((entry-from (copy-marker (point-min)))
2119 (entry-to (copy-marker (point-max))))
2120 (widget-specify-text entry-from entry-to)
2121 (set-marker-insertion-type entry-from t)
2122 (set-marker-insertion-type entry-to nil)
2123 (widget-put child :entry-from entry-from)
2124 (widget-put child :entry-to entry-to)))
2125 (widget-put insert :widget child)
2126 (widget-put delete :widget child)
2129 ;;; The `group' Widget.
2131 (define-widget 'group 'default
2132 "A widget which group other widgets inside."
2133 :convert-widget 'widget-types-convert-widget
2135 :value-create 'widget-group-value-create
2136 :value-delete 'widget-children-value-delete
2137 :value-get 'widget-editable-list-value-get
2138 :validate 'widget-editable-list-validate
2139 :match 'widget-group-match
2140 :match-inline 'widget-group-match-inline)
2142 (defun widget-group-value-create (widget)
2143 ;; Create each component.
2144 (let ((args (widget-get widget :args))
2145 (value (widget-get widget :value))
2146 arg answer children)
2148 (setq arg (car args)
2150 answer (widget-match-inline arg value)
2152 (and (eq (preceding-char) ?\n)
2153 (widget-get widget :indent)
2154 (insert-char ? (widget-get widget :indent)))
2155 (push (cond ((null answer)
2156 (widget-create-child widget arg))
2157 ((widget-get arg :inline)
2158 (widget-create-child-value widget arg (car answer)))
2160 (widget-create-child-value widget arg (car (car answer)))))
2162 (widget-put widget :children (nreverse children))))
2164 (defun widget-group-match (widget values)
2165 ;; Match if the components match.
2167 (let ((match (widget-group-match-inline widget values)))
2168 (and match (null (cdr match))))))
2170 (defun widget-group-match-inline (widget vals)
2171 ;; Match if the components match.
2172 (let ((args (widget-get widget :args))
2173 argument answer found)
2175 (setq argument (car args)
2177 answer (widget-match-inline argument vals))
2179 (setq vals (cdr answer)
2180 found (append found (car answer)))
2187 ;;; The `widget-help' Widget.
2189 (define-widget 'widget-help 'push-button
2190 "The widget documentation button."
2191 :format "%[[%t]%] %d"
2192 :help-echo "Toggle display of documentation."
2193 :action 'widget-help-action)
2195 (defun widget-help-action (widget &optional event)
2196 "Toggle documentation for WIDGET."
2197 (let ((old (widget-get widget :doc))
2198 (new (widget-get widget :widget-doc)))
2199 (widget-put widget :doc new)
2200 (widget-put widget :widget-doc old))
2201 (widget-value-set widget (widget-value widget)))
2203 ;;; The Sexp Widgets.
2205 (define-widget 'const 'item
2206 "An immutable sexp."
2209 (define-widget 'function-item 'item
2210 "An immutable function name."
2212 :documentation-property (lambda (symbol)
2214 (documentation symbol t)
2217 (define-widget 'variable-item 'item
2218 "An immutable variable name."
2220 :documentation-property 'variable-documentation)
2222 (define-widget 'string 'editable-field
2225 :format "%[%t%]: %v")
2227 (define-widget 'regexp 'string
2228 "A regular expression."
2229 ;; Should do validation.
2232 (define-widget 'file 'string
2234 It will read a file name from the minibuffer when activated."
2235 :format "%[%t%]: %v"
2237 :action 'widget-file-action)
2239 (defun widget-file-action (widget &optional event)
2240 ;; Read a file name from the minibuffer.
2241 (let* ((value (widget-value widget))
2242 (dir (file-name-directory value))
2243 (file (file-name-nondirectory value))
2244 (menu-tag (widget-apply widget :menu-tag-get))
2245 (must-match (widget-get widget :must-match))
2246 (answer (read-file-name (concat menu-tag ": (default `" value "') ")
2247 dir nil must-match file)))
2248 (widget-value-set widget (abbreviate-file-name answer))
2249 (widget-apply widget :notify widget event)
2252 (define-widget 'directory 'file
2253 "A directory widget.
2254 It will read a directory name from the minibuffer when activated."
2257 (define-widget 'symbol 'string
2261 :match (lambda (widget value) (symbolp value))
2262 :value-to-internal (lambda (widget value)
2266 :value-to-external (lambda (widget value)
2271 (define-widget 'function 'sexp
2272 ;; Should complete on functions.
2276 (define-widget 'variable 'symbol
2277 ;; Should complete on variables.
2281 (define-widget 'sexp 'string
2282 "An arbitrary lisp expression."
2283 :tag "Lisp expression"
2285 :validate 'widget-sexp-validate
2286 :match (lambda (widget value) t)
2287 :value-to-internal 'widget-sexp-value-to-internal
2288 :value-to-external (lambda (widget value) (read value)))
2290 (defun widget-sexp-value-to-internal (widget value)
2291 ;; Use pp for printer representation.
2292 (let ((pp (pp-to-string value)))
2293 (while (string-match "\n\\'" pp)
2294 (setq pp (substring pp 0 -1)))
2295 (if (or (string-match "\n\\'" pp)
2300 (defun widget-sexp-validate (widget)
2301 ;; Valid if we can read the string and there is no junk left after it.
2303 (let ((buffer (set-buffer (get-buffer-create " *Widget Scratch*"))))
2305 (insert (widget-apply widget :value-get))
2306 (goto-char (point-min))
2307 (condition-case data
2308 (let ((value (read buffer)))
2310 (if (widget-apply widget :match value)
2312 (widget-put widget :error (widget-get widget :type-error))
2315 :error (format "Junk at end of expression: %s"
2316 (buffer-substring (point)
2319 (error (widget-put widget :error (error-message-string data))
2322 (define-widget 'integer 'sexp
2326 :type-error "This field should contain an integer"
2327 :value-to-internal (lambda (widget value)
2328 (if (integerp value)
2329 (prin1-to-string value)
2331 :match (lambda (widget value) (integerp value)))
2333 (define-widget 'character 'string
2338 :format "%{%t%}: %v\n"
2339 :type-error "This field should contain a character"
2340 :value-to-internal (lambda (widget value)
2341 (if (integerp value)
2342 (char-to-string value)
2344 :value-to-external (lambda (widget value)
2348 :match (lambda (widget value) (integerp value)))
2350 (define-widget 'number 'sexp
2351 "A floating point number."
2354 :type-error "This field should contain a number"
2355 :value-to-internal (lambda (widget value)
2357 (prin1-to-string value)
2359 :match (lambda (widget value) (numberp value)))
2361 (define-widget 'list 'group
2364 :format "%{%t%}:\n%v")
2366 (define-widget 'vector 'group
2369 :format "%{%t%}:\n%v"
2370 :match 'widget-vector-match
2371 :value-to-internal (lambda (widget value) (append value nil))
2372 :value-to-external (lambda (widget value) (apply 'vector value)))
2374 (defun widget-vector-match (widget value)
2375 (and (vectorp value)
2376 (widget-group-match widget
2377 (widget-apply :value-to-internal widget value))))
2379 (define-widget 'cons 'group
2382 :format "%{%t%}:\n%v"
2383 :match 'widget-cons-match
2384 :value-to-internal (lambda (widget value)
2385 (list (car value) (cdr value)))
2386 :value-to-external (lambda (widget value)
2387 (cons (nth 0 value) (nth 1 value))))
2389 (defun widget-cons-match (widget value)
2391 (widget-group-match widget
2392 (widget-apply widget :value-to-internal value))))
2394 (define-widget 'choice 'menu-choice
2395 "A union of several sexp types."
2397 :format "%[%t%]: %v")
2399 (define-widget 'radio 'radio-button-choice
2400 "A union of several sexp types."
2402 :format "%{%t%}:\n%v")
2404 (define-widget 'repeat 'editable-list
2405 "A variable length homogeneous list."
2407 :format "%{%t%}:\n%v%i\n")
2409 (define-widget 'set 'checklist
2410 "A list of members from a fixed set."
2412 :format "%{%t%}:\n%v")
2414 (define-widget 'boolean 'toggle
2415 "To be nil or non-nil, that is the question."
2417 :format "%{%t%}: %[%v%]\n")
2419 ;;; The `color' Widget.
2421 (define-widget 'color-item 'choice-item
2422 "A color name (with sample)."
2423 :format "%v (%{sample%})\n"
2424 :sample-face-get 'widget-color-item-button-face-get)
2426 (defun widget-color-item-button-face-get (widget)
2427 ;; We create a face from the value.
2430 (facemenu-get-face (intern (concat "fg:" (widget-value widget))))
2433 (define-widget 'color 'push-button
2434 "Choose a color name (with sample)."
2435 :format "%[%t%]: %v"
2438 :value-create 'widget-color-value-create
2439 :value-delete 'widget-children-value-delete
2440 :value-get 'widget-color-value-get
2441 :value-set 'widget-color-value-set
2442 :action 'widget-color-action
2443 :match 'widget-field-match
2446 (defvar widget-color-choice-list nil)
2447 ;; Variable holding the possible colors.
2449 (defun widget-color-choice-list ()
2450 (unless widget-color-choice-list
2451 (setq widget-color-choice-list
2452 (mapcar '(lambda (color) (list color))
2453 (x-defined-colors))))
2454 widget-color-choice-list)
2456 (defun widget-color-value-create (widget)
2457 (let ((child (widget-create-child-and-convert
2458 widget 'color-item (widget-get widget :value))))
2459 (widget-put widget :children (list child))))
2461 (defun widget-color-value-get (widget)
2462 ;; Pass command to first child.
2463 (widget-apply (car (widget-get widget :children)) :value-get))
2465 (defun widget-color-value-set (widget value)
2466 ;; Pass command to first child.
2467 (widget-apply (car (widget-get widget :children)) :value-set value))
2469 (defvar widget-color-history nil
2470 "History of entered colors")
2472 (defun widget-color-action (widget &optional event)
2473 ;; Prompt for a color.
2474 (let* ((tag (widget-apply widget :menu-tag-get))
2475 (prompt (concat tag ": "))
2476 (answer (cond ((string-match "XEmacs" emacs-version)
2477 (read-color prompt))
2478 ((fboundp 'x-defined-colors)
2479 (completing-read (concat tag ": ")
2480 (widget-color-choice-list)
2481 nil nil nil 'widget-color-history))
2483 (read-string prompt (widget-value widget))))))
2484 (unless (zerop (length answer))
2485 (widget-value-set widget answer)
2486 (widget-apply widget :notify widget event)
2491 (defun widget-echo-help-mouse ()
2492 "Display the help message for the widget under the mouse.
2493 Enable with (run-with-idle-timer 1 t 'widget-echo-help-mouse)"
2494 (let* ((pos (mouse-position))
2498 (win (window-at x y frame))
2499 (where (coordinates-in-window-p (cons x y) win)))
2501 (save-window-excursion
2502 (progn ; save-excursion
2504 (let* ((result (compute-motion (window-start win)
2509 (cons (window-hscroll) 0)
2511 (when (and (eq (nth 1 result) x)
2512 (eq (nth 2 result) y))
2513 (widget-echo-help (nth 0 result))))))))
2515 (setq track-mouse t)
2516 (add-hook 'post-command-hook 'widget-stop-mouse-tracking)))
2518 (defun widget-stop-mouse-tracking (&rest args)
2519 "Stop the mouse tracking done while idle."
2520 (remove-hook 'post-command-hook 'widget-stop-mouse-tracking)
2521 (setq track-mouse nil))
2523 (defun widget-at (pos)
2524 "The button or field at POS."
2525 (or (get-text-property pos 'button)
2526 (get-text-property pos 'field)))
2528 (defun widget-echo-help (pos)
2529 "Display the help echo for widget at POS."
2530 (let* ((widget (widget-at pos))
2531 (help-echo (and widget (widget-get widget :help-echo))))
2532 (cond ((stringp help-echo)
2533 (message "%s" help-echo))
2534 ((and (symbolp help-echo) (fboundp help-echo)
2535 (stringp (setq help-echo (funcall help-echo widget))))
2536 (message "%s" help-echo)))))
2542 ;; wid-edit.el ends here