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/
10 ;; This file is part of GNU Emacs.
12 ;; GNU Emacs is free software; you can redistribute it and/or modify
13 ;; it under the terms of the GNU General Public License as published by
14 ;; the Free Software Foundation; either version 2, or (at your option)
17 ;; GNU Emacs is distributed in the hope that it will be useful,
18 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
19 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 ;; GNU General Public License for more details.
22 ;; You should have received a copy of the GNU General Public License
23 ;; along with GNU Emacs; see the file COPYING. If not, write to the
24 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
25 ;; Boston, MA 02111-1307, USA.
34 (eval-when-compile (require 'cl
))
39 (autoload 'pp-to-string
"pp")
40 (autoload 'Info-goto-node
"info")
42 (when (string-match "XEmacs" emacs-version
)
45 (error (load-library "x-overlay"))))
47 (if (string-match "XEmacs" emacs-version
)
48 (defun widget-event-point (event)
49 "Character position of the end of event if that exists, or nil."
50 (if (mouse-event-p event
)
53 (defun widget-event-point (event)
54 "Character position of the end of event if that exists, or nil."
55 (posn-point (event-end event
))))
57 (defalias 'widget-read-event
(if (string-match "XEmacs" emacs-version
)
61 ;; The following should go away when bundled with Emacs.
66 (unless (and (featurep 'custom
) (fboundp 'custom-declare-variable
))
67 ;; We have the old custom-library, hack around it!
68 (defmacro defgroup
(&rest args
) nil
)
69 (defmacro defcustom
(var value doc
&rest args
)
70 (` (defvar (, var
) (, value
) (, doc
))))
71 (defmacro defface
(&rest args
) nil
)
72 (define-widget-keywords :prefix
:tag
:load
:link
:options
:type
:group
)
73 (when (fboundp 'copy-face
)
74 (copy-face 'default
'widget-documentation-face
)
75 (copy-face 'bold
'widget-button-face
)
76 (copy-face 'italic
'widget-field-face
)))
78 (unless (fboundp 'button-release-event-p
)
79 ;; XEmacs function missing from Emacs.
80 (defun button-release-event-p (event)
81 "Non-nil if EVENT is a mouse-button-release event object."
83 (memq (event-basic-type event
) '(mouse-1 mouse-2 mouse-3
))
84 (or (memq 'click
(event-modifiers event
))
85 (memq 'drag
(event-modifiers event
))))))
87 (unless (fboundp 'error-message-string
)
88 ;; Emacs function missing in XEmacs.
89 (defun error-message-string (obj)
90 "Convert an error value to an error message."
91 (let ((buf (get-buffer-create " *error-message*")))
93 (display-error obj buf
)
94 (buffer-string buf
)))))
96 (when (let ((a "foo"))
97 (put-text-property 1 2 'foo
1 a
)
98 (put-text-property 1 2 'bar
2 a
)
99 (set-text-properties 1 2 nil a
)
100 (text-properties-at 1 a
))
101 ;; XEmacs 20.2 and earlier had a buggy set-text-properties.
102 (defun set-text-properties (start end props
&optional buffer-or-string
)
103 "Completely replace properties of text from START to END.
104 The third argument PROPS is the new property list.
105 The optional fourth argument, BUFFER-OR-STRING,
106 is the string or buffer containing the text."
107 (map-extents #'(lambda (extent ignored
)
108 (remove-text-properties
110 (list (extent-property extent
'text-prop
)
114 buffer-or-string start end nil nil
'text-prop
)
115 (add-text-properties start end props buffer-or-string
)))
119 (defgroup widgets nil
120 "Customization support for the Widget Library."
121 :link
'(custom-manual "(widget)Top")
122 :link
'(url-link :tag
"Development Page"
123 "http://www.dina.kvl.dk/~abraham/custom/")
128 (defgroup widget-documentation nil
129 "Options controling the display of documentation strings."
132 (defgroup widget-faces nil
133 "Faces used by the widget library."
137 (defface widget-documentation-face
'((((class color
)
139 (:foreground
"lime green"))
142 (:foreground
"dark green"))
144 "Face used for documentation text."
145 :group
'widget-documentation
146 :group
'widget-faces
)
148 (defface widget-button-face
'((t (:bold t
)))
149 "Face used for widget buttons."
150 :group
'widget-faces
)
152 (defcustom widget-mouse-face
'highlight
153 "Face used for widget buttons when the mouse is above them."
155 :group
'widget-faces
)
157 (defface widget-field-face
'((((class grayscale color
)
159 (:background
"gray85"))
160 (((class grayscale color
)
162 (:background
"dim gray"))
165 "Face used for editable fields."
166 :group
'widget-faces
)
168 ;;; Utility functions.
170 ;; These are not really widget specific.
172 (defsubst widget-plist-member
(plist prop
)
173 ;; Return non-nil if PLIST has the property PROP.
174 ;; PLIST is a property list, which is a list of the form
175 ;; (PROP1 VALUE1 PROP2 VALUE2 ...). PROP is a symbol.
176 ;; Unlike `plist-get', this allows you to distinguish between a missing
177 ;; property and a property with the value nil.
178 ;; The value is actually the tail of PLIST whose car is PROP.
179 (while (and plist
(not (eq (car plist
) prop
)))
180 (setq plist
(cdr (cdr plist
))))
183 (defun widget-princ-to-string (object)
184 ;; Return string representation of OBJECT, any Lisp object.
185 ;; No quoting characters are used; no delimiters are printed around
186 ;; the contents of strings.
188 (set-buffer (get-buffer-create " *widget-tmp*"))
190 (let ((standard-output (current-buffer)))
194 (defun widget-clear-undo ()
195 "Clear all undo information."
196 (buffer-disable-undo (current-buffer))
197 (buffer-enable-undo))
199 (defcustom widget-menu-max-size
40
200 "Largest number of items allowed in a popup-menu.
201 Larger menus are read through the minibuffer."
205 (defun widget-choose (title items
&optional event
)
206 "Choose an item from a list.
208 First argument TITLE is the name of the list.
209 Second argument ITEMS is an list whose members are either
210 (NAME . VALUE), to indicate selectable items, or just strings to
211 indicate unselectable items.
212 Optional third argument EVENT is an input event.
214 The user is asked to choose between each NAME from the items alist,
215 and the VALUE of the chosen element will be returned. If EVENT is a
216 mouse event, and the number of elements in items is less than
217 `widget-menu-max-size', a popup menu will be used, otherwise the
219 (cond ((and (< (length items
) widget-menu-max-size
)
220 event
(fboundp 'x-popup-menu
) window-system
)
221 ;; We are in Emacs-19, pressed by the mouse
223 (list title
(cons "" items
))))
224 ((and (< (length items
) widget-menu-max-size
)
225 event
(fboundp 'popup-menu
) window-system
)
226 ;; We are in XEmacs, pressed by the mouse
227 (let ((val (get-popup-menu-response
234 (vector (car x
) (list (car x
)) t
))))
237 (listp (event-object val
))
238 (stringp (car-safe (event-object val
)))
239 (car (event-object val
))))
240 (cdr (assoc val items
))))
242 (setq items
(widget-remove-if 'stringp items
))
243 (let ((val (completing-read (concat title
": ") items nil t
)))
245 (let ((try (try-completion val items
)))
248 (cdr (assoc val items
)))
251 (defun widget-remove-if (predictate list
)
252 (let (result (tail list
))
254 (or (funcall predictate
(car tail
))
255 (setq result
(cons (car tail
) result
)))
256 (setq tail
(cdr tail
)))
259 ;;; Widget text specifications.
261 ;; These functions are for specifying text properties.
263 (defun widget-specify-none (from to
)
264 ;; Clear all text properties between FROM and TO.
265 (set-text-properties from to nil
))
267 (defun widget-specify-text (from to
)
268 ;; Default properties.
269 (add-text-properties from to
(list 'read-only t
275 (defcustom widget-field-add-space
276 (or (< emacs-major-version
20)
277 (and (eq emacs-major-version
20)
278 (< emacs-minor-version
3))
279 (not (string-match "XEmacs" emacs-version
)))
280 "Non-nil means add extra space at the end of editable text fields.
282 This is needed on all versions of Emacs, and on XEmacs before 20.3.
283 If you don't add the space, it will become impossible to edit a zero
288 (defun widget-specify-field (widget from to
)
289 "Specify editable button for WIDGET between FROM and TO."
290 (put-text-property from to
'read-only nil
)
291 ;; Terminating space is not part of the field, but necessary in
292 ;; order for local-map to work. Remove next sexp if local-map works
293 ;; at the end of the overlay.
296 (when widget-field-add-space
297 (insert-and-inherit " "))
299 (if widget-field-add-space
300 (add-text-properties (1- to
) to
301 '(front-sticky nil start-open t read-only to
))
302 (add-text-properties to
(1+ to
)
303 '(front-sticky nil start-open t read-only to
)))
304 (add-text-properties (1- from
) from
305 '(rear-nonsticky t end-open t read-only from
))
306 (let ((map (widget-get widget
:keymap
))
307 (face (or (widget-get widget
:value-face
) 'widget-field-face
))
308 (help-echo (widget-get widget
:help-echo
))
309 (overlay (make-overlay from to nil nil t
)))
310 (unless (or (stringp help-echo
) (null help-echo
))
311 (setq help-echo
'widget-mouse-help
))
312 (widget-put widget
:field-overlay overlay
)
313 (overlay-put overlay
'detachable nil
)
314 (overlay-put overlay
'field widget
)
315 (overlay-put overlay
'local-map map
)
316 (overlay-put overlay
'keymap map
)
317 (overlay-put overlay
'face face
)
318 (overlay-put overlay
'balloon-help help-echo
)
319 (overlay-put overlay
'help-echo help-echo
)))
321 (defun widget-specify-button (widget from to
)
322 "Specify button for WIDGET between FROM and TO."
323 (let ((face (widget-apply widget
:button-face-get
))
324 (help-echo (widget-get widget
:help-echo
))
325 (overlay (make-overlay from to nil t nil
)))
326 (widget-put widget
:button-overlay overlay
)
327 (unless (or (null help-echo
) (stringp help-echo
))
328 (setq help-echo
'widget-mouse-help
))
329 (overlay-put overlay
'button widget
)
330 (overlay-put overlay
'mouse-face widget-mouse-face
)
331 (overlay-put overlay
'balloon-help help-echo
)
332 (overlay-put overlay
'help-echo help-echo
)
333 (overlay-put overlay
'face face
)))
335 (defun widget-mouse-help (extent)
336 "Find mouse help string for button in extent."
337 (let* ((widget (widget-at (extent-start-position extent
)))
338 (help-echo (and widget
(widget-get widget
:help-echo
))))
339 (cond ((stringp help-echo
)
341 ((and (symbolp help-echo
) (fboundp help-echo
)
342 (stringp (setq help-echo
(funcall help-echo widget
))))
345 (format "(widget %S :help-echo %S)" widget help-echo
)))))
347 (defun widget-specify-sample (widget from to
)
348 ;; Specify sample for WIDGET between FROM and TO.
349 (let ((face (widget-apply widget
:sample-face-get
)))
351 (add-text-properties from to
(list 'start-open t
354 (defun widget-specify-doc (widget from to
)
355 ;; Specify documentation for WIDGET between FROM and TO.
356 (add-text-properties from to
(list 'widget-doc widget
357 'face
'widget-documentation-face
)))
359 (defmacro widget-specify-insert
(&rest form
)
360 ;; Execute FORM without inheriting any text properties.
363 (let ((inhibit-read-only t
)
365 after-change-functions
)
367 (narrow-to-region (- (point) 2) (point))
368 (widget-specify-none (point-min) (point-max))
369 (goto-char (1+ (point-min)))
370 (setq result
(progn (,@ form
)))
371 (delete-region (point-min) (1+ (point-min)))
372 (delete-region (1- (point-max)) (point-max))
373 (goto-char (point-max))
376 (defface widget-inactive-face
'((((class grayscale color
)
378 (:foreground
"light gray"))
379 (((class grayscale color
)
381 (:foreground
"dim gray"))
384 "Face used for inactive widgets."
385 :group
'widget-faces
)
387 (defun widget-specify-inactive (widget from to
)
388 "Make WIDGET inactive for user modifications."
389 (unless (widget-get widget
:inactive
)
390 (let ((overlay (make-overlay from to nil t nil
)))
391 (overlay-put overlay
'face
'widget-inactive-face
)
392 ;; This is disabled, as it makes the mouse cursor change shape.
393 ;; (overlay-put overlay 'mouse-face 'widget-inactive-face)
394 (overlay-put overlay
'evaporate t
)
395 (overlay-put overlay
'priority
100)
396 (overlay-put overlay
(if (string-match "XEmacs" emacs-version
)
398 'modification-hooks
) '(widget-overlay-inactive))
399 (widget-put widget
:inactive overlay
))))
401 (defun widget-overlay-inactive (&rest junk
)
402 "Ignoring the arguments, signal an error."
403 (unless inhibit-read-only
404 (error "Attempt to modify inactive widget")))
407 (defun widget-specify-active (widget)
408 "Make WIDGET active for user modifications."
409 (let ((inactive (widget-get widget
:inactive
)))
411 (delete-overlay inactive
)
412 (widget-put widget
:inactive nil
))))
414 ;;; Widget Properties.
416 (defsubst widget-type
(widget)
417 "Return the type of WIDGET, a symbol."
420 (defun widget-put (widget property value
)
421 "In WIDGET set PROPERTY to VALUE.
422 The value can later be retrived with `widget-get'."
423 (setcdr widget
(plist-put (cdr widget
) property value
)))
425 (defun widget-get (widget property
)
426 "In WIDGET, get the value of PROPERTY.
427 The value could either be specified when the widget was created, or
428 later with `widget-put'."
432 (cond ((setq tmp
(widget-plist-member (cdr widget
) property
))
433 (setq value
(car (cdr tmp
))
435 ((setq tmp
(car widget
))
436 (setq widget
(get tmp
'widget-type
)))
438 (setq missing nil
))))
441 (defun widget-member (widget property
)
442 "Non-nil iff there is a definition in WIDGET for PROPERTY."
443 (cond ((widget-plist-member (cdr widget
) property
)
446 (widget-member (get (car widget
) 'widget-type
) property
))
450 (defun widget-apply (widget property
&rest args
)
451 "Apply the value of WIDGET's PROPERTY to the widget itself.
452 ARGS are passed as extra arguments to the function."
453 (apply (widget-get widget property
) widget args
))
455 (defun widget-value (widget)
456 "Extract the current value of WIDGET."
458 :value-to-external
(widget-apply widget
:value-get
)))
460 (defun widget-value-set (widget value
)
461 "Set the current value of WIDGET to VALUE."
463 :value-set
(widget-apply widget
464 :value-to-internal value
)))
466 (defun widget-match-inline (widget vals
)
467 ;; In WIDGET, match the start of VALS.
468 (cond ((widget-get widget
:inline
)
469 (widget-apply widget
:match-inline vals
))
471 (widget-apply widget
:match
(car vals
)))
472 (cons (list (car vals
)) (cdr vals
)))
475 (defun widget-apply-action (widget &optional event
)
476 "Apply :action in WIDGET in response to EVENT."
477 (if (widget-apply widget
:active
)
478 (widget-apply widget
:action event
)
479 (error "Attempt to perform action on inactive widget")))
481 ;;; Helper functions.
483 ;; These are widget specific.
486 (defun widget-prompt-value (widget prompt
&optional value unbound
)
487 "Prompt for a value matching WIDGET, using PROMPT.
488 The current value is assumed to be VALUE, unless UNBOUND is non-nil."
489 (unless (listp widget
)
490 (setq widget
(list widget
)))
491 (setq prompt
(format "[%s] %s" (widget-type widget
) prompt
))
492 (setq widget
(widget-convert widget
))
493 (let ((answer (widget-apply widget
:prompt-value prompt value unbound
)))
494 (unless (widget-apply widget
:match answer
)
495 (error "Value does not match %S type." (car widget
)))
498 (defun widget-get-sibling (widget)
499 "Get the item WIDGET is assumed to toggle.
500 This is only meaningful for radio buttons or checkboxes in a list."
501 (let* ((parent (widget-get widget
:parent
))
502 (children (widget-get parent
:children
))
506 (setq child
(car children
)
507 children
(cdr children
))
508 (when (eq (widget-get child
:button
) widget
)
509 (throw 'child child
)))
512 (defun widget-map-buttons (function &optional buffer maparg
)
513 "Map FUNCTION over the buttons in BUFFER.
514 FUNCTION is called with the arguments WIDGET and MAPARG.
516 If FUNCTION returns non-nil, the walk is cancelled.
518 The arguments MAPARG, and BUFFER default to nil and (current-buffer),
520 (let ((cur (point-min))
524 (save-excursion (set-buffer buffer
) (overlay-lists))
526 (setq overlays
(append (car overlays
) (cdr overlays
)))
527 (while (setq cur
(pop overlays
))
528 (setq widget
(overlay-get cur
'button
))
529 (if (and widget
(funcall function widget maparg
))
530 (setq overlays nil
)))))
534 (defcustom widget-glyph-directory
(concat data-directory
"custom/")
535 "Where widget glyphs are located.
536 If this variable is nil, widget will try to locate the directory
541 (defcustom widget-glyph-enable t
542 "If non nil, use glyphs in images when available."
546 (defcustom widget-image-conversion
547 '((xpm ".xpm") (gif ".gif") (png ".png") (jpeg ".jpg" ".jpeg")
549 "Conversion alist from image formats to file name suffixes."
551 :type
'(repeat (cons :format
"%v"
552 (symbol :tag
"Image Format" unknown
)
553 (repeat :tag
"Suffixes"
554 (string :format
"%v")))))
556 (defun widget-glyph-find (image tag
)
557 "Create a glyph corresponding to IMAGE with string TAG as fallback.
558 IMAGE should either already be a glyph, or be a file name sans
559 extension (xpm, xbm, gif, jpg, or png) located in
560 `widget-glyph-directory'."
561 (cond ((not (and image
562 (string-match "XEmacs" emacs-version
)
564 (fboundp 'make-glyph
)
565 (fboundp 'locate-file
)
567 ;; We don't want or can't use glyphs.
569 ((and (fboundp 'glyphp
)
571 ;; Already a glyph. Use it.
574 ;; A string. Look it up in relevant directories.
575 (let* ((dirlist (list (or widget-glyph-directory
576 (concat data-directory
579 (formats widget-image-conversion
)
581 (while (and formats
(not file
))
582 (when (valid-image-instantiator-format-p (car (car formats
)))
583 (setq file
(locate-file image dirlist
588 (setq formats
(cdr formats
))))
590 ;; We create a glyph with the file as the default image
591 ;; instantiator, and the TAG fallback
592 (make-glyph (list (vector (car (car formats
)) ':file file
)
593 (vector 'string
':data tag
))))))
594 ((valid-instantiator-p image
'image
)
595 ;; A valid image instantiator (e.g. [gif :file "somefile"] etc.)
596 (make-glyph (list image
597 (vector 'string
':data tag
))))
599 ;; This could be virtually anything. Let `make-glyph' sort it out.
605 (defun widget-glyph-insert (widget tag image
&optional down inactive
)
606 "In WIDGET, insert the text TAG or, if supported, IMAGE.
607 IMAGE should either be a glyph, an image instantiator, or an image file
608 name sans extension (xpm, xbm, gif, jpg, or png) located in
609 `widget-glyph-directory'.
611 Optional arguments DOWN and INACTIVE is used instead of IMAGE when the
612 glyph is pressed or inactive, respectively.
614 WARNING: If you call this with a glyph, and you want the user to be
615 able to invoke the glyph, make sure it is unique. If you use the
616 same glyph for multiple widgets, invoking any of the glyphs will
617 cause the last created widget to be invoked.
619 Instead of an instantiator, you can also use a list of instantiators,
620 or whatever `make-glyph' will accept. However, in that case you must
621 provide the fallback TAG as a part of the instantiator yourself."
622 (let ((glyph (widget-glyph-find image tag
)))
624 (widget-glyph-insert-glyph widget
626 (widget-glyph-find down tag
)
627 (widget-glyph-find inactive tag
))
630 (defun widget-glyph-insert-glyph (widget glyph
&optional down inactive
)
631 "In WIDGET, insert GLYPH.
632 If optional arguments DOWN and INACTIVE are given, they should be
633 glyphs used when the widget is pushed and inactive, respectively."
634 (set-glyph-property glyph
'widget widget
)
636 (set-glyph-property down
'widget widget
))
638 (set-glyph-property inactive
'widget widget
))
640 (let ((ext (make-extent (point) (1- (point))))
641 (help-echo (widget-get widget
:help-echo
)))
642 (set-extent-property ext
'invisible t
)
643 (set-extent-property ext
'start-open t
)
644 (set-extent-property ext
'end-open t
)
645 (set-extent-end-glyph ext glyph
)
647 (set-extent-property ext
'balloon-help help-echo
)
648 (set-extent-property ext
'help-echo help-echo
)))
649 (widget-put widget
:glyph-up glyph
)
650 (when down
(widget-put widget
:glyph-down down
))
651 (when inactive
(widget-put widget
:glyph-inactive inactive
)))
655 (defgroup widget-button nil
656 "The look of various kinds of buttons."
659 (defcustom widget-button-prefix
""
660 "String used as prefix for buttons."
662 :group
'widget-button
)
664 (defcustom widget-button-suffix
""
665 "String used as suffix for buttons."
667 :group
'widget-button
)
669 (defun widget-button-insert-indirect (widget key
)
670 "Insert value of WIDGET's KEY property."
671 (let ((val (widget-get widget key
)))
672 (while (and val
(symbolp val
))
673 (setq val
(symbol-value val
)))
677 ;;; Creating Widgets.
680 (defun widget-create (type &rest args
)
681 "Create widget of TYPE.
682 The optional ARGS are additional keyword arguments."
683 (let ((widget (apply 'widget-convert type args
)))
684 (widget-apply widget
:create
)
687 (defun widget-create-child-and-convert (parent type
&rest args
)
688 "As part of the widget PARENT, create a child widget TYPE.
689 The child is converted, using the keyword arguments ARGS."
690 (let ((widget (apply 'widget-convert type args
)))
691 (widget-put widget
:parent parent
)
692 (unless (widget-get widget
:indent
)
693 (widget-put widget
:indent
(+ (or (widget-get parent
:indent
) 0)
694 (or (widget-get widget
:extra-offset
) 0)
695 (widget-get parent
:offset
))))
696 (widget-apply widget
:create
)
699 (defun widget-create-child (parent type
)
700 "Create widget of TYPE."
701 (let ((widget (copy-sequence type
)))
702 (widget-put widget
:parent parent
)
703 (unless (widget-get widget
:indent
)
704 (widget-put widget
:indent
(+ (or (widget-get parent
:indent
) 0)
705 (or (widget-get widget
:extra-offset
) 0)
706 (widget-get parent
:offset
))))
707 (widget-apply widget
:create
)
710 (defun widget-create-child-value (parent type value
)
711 "Create widget of TYPE with value VALUE."
712 (let ((widget (copy-sequence type
)))
713 (widget-put widget
:value
(widget-apply widget
:value-to-internal value
))
714 (widget-put widget
:parent parent
)
715 (unless (widget-get widget
:indent
)
716 (widget-put widget
:indent
(+ (or (widget-get parent
:indent
) 0)
717 (or (widget-get widget
:extra-offset
) 0)
718 (widget-get parent
:offset
))))
719 (widget-apply widget
:create
)
723 (defun widget-delete (widget)
725 (widget-apply widget
:delete
))
727 (defun widget-convert (type &rest args
)
728 "Convert TYPE to a widget without inserting it in the buffer.
729 The optional ARGS are additional keyword arguments."
730 ;; Don't touch the type.
731 (let* ((widget (if (symbolp type
)
733 (copy-sequence type
)))
736 ;; First set the :args keyword.
737 (while (cdr current
) ;Look in the type.
738 (let ((next (car (cdr current
))))
739 (if (and (symbolp next
) (eq (aref (symbol-name next
) 0) ?
:))
740 (setq current
(cdr (cdr current
)))
741 (setcdr current
(list :args
(cdr current
)))
742 (setq current nil
))))
743 (while args
;Look in the args.
744 (let ((next (nth 0 args
)))
745 (if (and (symbolp next
) (eq (aref (symbol-name next
) 0) ?
:))
746 (setq args
(nthcdr 2 args
))
747 (widget-put widget
:args args
)
749 ;; Then Convert the widget.
752 (let ((convert-widget (plist-get (cdr type
) :convert-widget
)))
754 (setq widget
(funcall convert-widget widget
))))
755 (setq type
(get (car type
) 'widget-type
)))
756 ;; Finally set the keyword args.
758 (let ((next (nth 0 keys
)))
759 (if (and (symbolp next
) (eq (aref (symbol-name next
) 0) ?
:))
761 (widget-put widget next
(nth 1 keys
))
762 (setq keys
(nthcdr 2 keys
)))
764 ;; Convert the :value to internal format.
765 (if (widget-member widget
:value
)
766 (let ((value (widget-get widget
:value
)))
768 :value
(widget-apply widget
:value-to-internal value
))))
769 ;; Return the newly create widget.
772 (defun widget-insert (&rest args
)
773 "Call `insert' with ARGS and make the text read only."
774 (let ((inhibit-read-only t
)
775 after-change-functions
778 (widget-specify-text from
(point))))
780 (defun widget-convert-text (type from to
781 &optional button-from button-to
783 "Return a widget of type TYPE with endpoint FROM TO.
784 Optional ARGS are extra keyword arguments for TYPE.
785 and TO will be used as the widgets end points. If optional arguments
786 BUTTON-FROM and BUTTON-TO are given, these will be used as the widgets
788 Optional ARGS are extra keyword arguments for TYPE."
789 (let ((widget (apply 'widget-convert type
:delete
'widget-leave-text args
))
790 (from (copy-marker from
))
791 (to (copy-marker to
)))
792 (widget-specify-text from to
)
793 (set-marker-insertion-type from t
)
794 (set-marker-insertion-type to nil
)
795 (widget-put widget
:from from
)
796 (widget-put widget
:to to
)
798 (widget-specify-button widget button-from button-to
))
801 (defun widget-convert-button (type from to
&rest args
)
802 "Return a widget of type TYPE with endpoint FROM TO.
803 Optional ARGS are extra keyword arguments for TYPE.
804 No text will be inserted to the buffer, instead the text between FROM
805 and TO will be used as the widgets end points, as well as the widgets
807 (apply 'widget-convert-text type from to from to args
))
809 (defun widget-leave-text (widget)
810 "Remove markers and overlays from WIDGET and its children."
811 (let ((from (widget-get widget
:from
))
812 (to (widget-get widget
:to
))
813 (button (widget-get widget
:button-overlay
))
814 (field (widget-get widget
:field-overlay
))
815 (children (widget-get widget
:children
)))
816 (set-marker from nil
)
818 (delete-overlay button
)
819 (delete-overlay field
)
820 (mapcar 'widget-leave-text children
)))
822 ;;; Keymap and Commands.
824 (defvar widget-keymap nil
825 "Keymap containing useful binding for buffers containing widgets.
826 Recommended as a parent keymap for modes using widgets.")
828 (unless widget-keymap
829 (setq widget-keymap
(make-sparse-keymap))
830 (define-key widget-keymap
"\t" 'widget-forward
)
831 (define-key widget-keymap
[(shift tab
)] 'widget-backward
)
832 (define-key widget-keymap
[backtab] 'widget-backward)
833 (if (string-match "XEmacs" emacs-version)
836 (define-key widget-keymap [button1] 'widget-button1-click)
837 (define-key widget-keymap [button2] 'widget-button-click))
838 (define-key widget-keymap [down-mouse-2] 'widget-button-click))
839 (define-key widget-keymap "\C-m" 'widget-button-press))
841 (defvar widget-global-map global-map
842 "Keymap used for events the widget does not handle themselves.")
843 (make-variable-buffer-local 'widget-global-map)
845 (defvar widget-field-keymap nil
846 "Keymap used inside an editable field.")
848 (unless widget-field-keymap
849 (setq widget-field-keymap (copy-keymap widget-keymap))
850 (unless (string-match "XEmacs" (emacs-version))
851 (define-key widget-field-keymap [menu-bar] 'nil))
852 (define-key widget-field-keymap "\C-k" 'widget-kill-line)
853 (define-key widget-field-keymap "\M-\t" 'widget-complete)
854 (define-key widget-field-keymap "\C-m" 'widget-field-activate)
855 (define-key widget-field-keymap "\C-a" 'widget-beginning-of-line)
856 (define-key widget-field-keymap "\C-e" 'widget-end-of-line)
857 (set-keymap-parent widget-field-keymap global-map))
859 (defvar widget-text-keymap nil
860 "Keymap used inside a text field.")
862 (unless widget-text-keymap
863 (setq widget-text-keymap (copy-keymap widget-keymap))
864 (unless (string-match "XEmacs" (emacs-version))
865 (define-key widget-text-keymap [menu-bar] 'nil))
866 (define-key widget-text-keymap "\C-a" 'widget-beginning-of-line)
867 (define-key widget-text-keymap "\C-e" 'widget-end-of-line)
868 (set-keymap-parent widget-text-keymap global-map))
870 (defun widget-field-activate (pos &optional event)
871 "Invoke the ediable field at point."
873 (let ((field (get-char-property pos 'field)))
875 (widget-apply-action field event)
877 (lookup-key widget-global-map (this-command-keys))))))
879 (defface widget-button-pressed-face
883 (:bold t :underline t)))
884 "Face used for pressed buttons."
885 :group 'widget-faces)
887 (defun widget-button-click (event)
888 "Invoke button below mouse pointer."
890 (cond ((and (fboundp 'event-glyph)
892 (widget-glyph-click event))
893 ((widget-event-point event)
894 (let* ((pos (widget-event-point event))
895 (button (get-char-property pos 'button)))
897 (let* ((overlay (widget-get button :button-overlay))
898 (face (overlay-get overlay 'face))
899 (mouse-face (overlay-get overlay 'mouse-face)))
901 (let ((track-mouse t))
903 'face 'widget-button-pressed-face)
905 'mouse-face 'widget-button-pressed-face)
906 (unless (widget-apply button :mouse-down-action event)
907 (while (not (button-release-event-p event))
908 (setq event (widget-read-event)
909 pos (widget-event-point event))
911 (eq (get-char-property pos 'button)
916 'widget-button-pressed-face)
919 'widget-button-pressed-face))
920 (overlay-put overlay 'face face)
921 (overlay-put overlay 'mouse-face mouse-face))))
923 (eq (get-char-property pos 'button) button))
924 (widget-apply-action button event)))
925 (overlay-put overlay 'face face)
926 (overlay-put overlay 'mouse-face mouse-face)))
928 ;; Find the global command to run, and check whether it
929 ;; is bound to an up event.
930 (cond ((setq command ;down event
931 (lookup-key widget-global-map [ button2 ])))
932 ((setq command ;down event
933 (lookup-key widget-global-map [ down-mouse-2 ])))
934 ((setq command ;up event
935 (lookup-key widget-global-map [ button2up ]))
937 ((setq command ;up event
938 (lookup-key widget-global-map [ mouse-2]))
941 ;; Don't execute up events twice.
943 (while (not (button-release-event-p event))
944 (setq event (widget-read-event))))
945 (call-interactively command))))))
947 (message "You clicked somewhere weird."))))
949 (defun widget-button1-click (event)
950 "Invoke glyph below mouse pointer."
952 (if (and (fboundp 'event-glyph)
954 (widget-glyph-click event)
955 (call-interactively (lookup-key widget-global-map (this-command-keys)))))
957 (defun widget-glyph-click (event)
958 "Handle click on a glyph."
959 (let* ((glyph (event-glyph event))
960 (widget (glyph-property glyph 'widget))
961 (extent (event-glyph-extent event))
962 (down-glyph (or (and widget (widget-get widget :glyph-down)) glyph))
963 (up-glyph (or (and widget (widget-get widget :glyph-up)) glyph))
965 ;; Wait for the release.
966 (while (not (button-release-event-p last))
967 (if (eq extent (event-glyph-extent last))
968 (set-extent-property extent 'end-glyph down-glyph)
969 (set-extent-property extent 'end-glyph up-glyph))
970 (setq last (next-event event)))
973 (set-extent-property extent 'end-glyph up-glyph))
974 ;; Apply widget action.
975 (when (eq extent (event-glyph-extent last))
976 (let ((widget (glyph-property (event-glyph event) 'widget)))
978 (message "You clicked on a glyph."))
979 ((not (widget-apply widget :active))
980 (message "This glyph is inactive."))
982 (widget-apply-action widget event)))))))
984 (defun widget-button-press (pos &optional event)
985 "Invoke button at POS."
987 (let ((button (get-char-property pos 'button)))
989 (widget-apply-action button event)
990 (let ((command (lookup-key widget-global-map (this-command-keys))))
991 (when (commandp command)
992 (call-interactively command))))))
994 (defun widget-tabable-at (&optional pos)
995 "Return the tabable widget at POS, or nil.
996 POS defaults to the value of (point)."
999 (let ((widget (or (get-char-property (point) 'button)
1000 (get-char-property (point) 'field))))
1002 (let ((order (widget-get widget :tab-order)))
1010 (defun widget-move (arg)
1011 "Move point to the ARG next field or button.
1012 ARG may be negative to move backward."
1013 (or (bobp) (> arg 0) (backward-char))
1016 (old (widget-tabable-at))
1021 (goto-char (point-min))
1023 (and (eq pos (point))
1025 (error "No buttons or fields found"))
1026 (let ((new (widget-tabable-at)))
1028 (unless (eq new old)
1034 (goto-char (point-max))
1036 (and (eq pos (point))
1038 (error "No buttons or fields found"))
1039 (let ((new (widget-tabable-at)))
1041 (unless (eq new old)
1042 (setq arg (1+ arg))))))
1043 (let ((new (widget-tabable-at)))
1044 (while (eq (widget-tabable-at) new)
1047 (widget-echo-help (point))
1048 (run-hooks 'widget-move-hook))
1050 (defun widget-forward (arg)
1051 "Move point to the next field or button.
1052 With optional ARG, move across that many fields."
1054 (run-hooks 'widget-forward-hook)
1057 (defun widget-backward (arg)
1058 "Move point to the previous field or button.
1059 With optional ARG, move across that many fields."
1061 (run-hooks 'widget-backward-hook)
1062 (widget-move (- arg)))
1064 (defun widget-beginning-of-line ()
1065 "Go to beginning of field or beginning of line, whichever is first."
1067 (let* ((field (widget-field-find (point)))
1068 (start (and field (widget-field-start field))))
1069 (if (and start (not (eq start (point))))
1071 (call-interactively 'beginning-of-line))))
1073 (defun widget-end-of-line ()
1074 "Go to end of field or end of line, whichever is first."
1076 (let* ((field (widget-field-find (point)))
1077 (end (and field (widget-field-end field))))
1078 (if (and end (not (eq end (point))))
1080 (call-interactively 'end-of-line))))
1082 (defun widget-kill-line ()
1083 "Kill to end of field or end of line, whichever is first."
1085 (let* ((field (widget-field-find (point)))
1086 (newline (save-excursion (forward-line 1) (point)))
1087 (end (and field (widget-field-end field))))
1088 (if (and field (> newline end))
1089 (kill-region (point) end)
1090 (call-interactively 'kill-line))))
1092 (defcustom widget-complete-field (lookup-key global-map "\M-\t")
1093 "Default function to call for completion inside fields."
1094 :options '(ispell-complete-word complete-tag lisp-complete-symbol)
1098 (defun widget-complete ()
1099 "Complete content of editable field from point.
1100 When not inside a field, move to the previous button or field."
1102 (let ((field (widget-field-find (point))))
1104 (widget-apply field :complete)
1105 (error "Not in an editable field"))))
1107 ;;; Setting up the buffer.
1109 (defvar widget-field-new nil)
1110 ;; List of all newly created editable fields in the buffer.
1111 (make-variable-buffer-local 'widget-field-new)
1113 (defvar widget-field-list nil)
1114 ;; List of all editable fields in the buffer.
1115 (make-variable-buffer-local 'widget-field-list)
1117 (defun widget-setup ()
1118 "Setup current buffer so editing string widgets works."
1119 (let ((inhibit-read-only t)
1120 (after-change-functions nil)
1122 (while widget-field-new
1123 (setq field (car widget-field-new)
1124 widget-field-new (cdr widget-field-new)
1125 widget-field-list (cons field widget-field-list))
1126 (let ((from (car (widget-get field :field-overlay)))
1127 (to (cdr (widget-get field :field-overlay))))
1128 (widget-specify-field field
1129 (marker-position from) (marker-position to))
1130 (set-marker from nil)
1131 (set-marker to nil))))
1133 ;; We need to maintain text properties and size of the editing fields.
1134 (make-local-variable 'after-change-functions)
1135 (if widget-field-list
1136 (setq after-change-functions '(widget-after-change))
1137 (setq after-change-functions nil)))
1139 (defvar widget-field-last nil)
1140 ;; Last field containing point.
1141 (make-variable-buffer-local 'widget-field-last)
1143 (defvar widget-field-was nil)
1144 ;; The widget data before the change.
1145 (make-variable-buffer-local 'widget-field-was)
1147 (defun widget-field-buffer (widget)
1148 "Return the start of WIDGET's editing field."
1149 (let ((overlay (widget-get widget :field-overlay)))
1150 (and overlay (overlay-buffer overlay))))
1152 (defun widget-field-start (widget)
1153 "Return the start of WIDGET's editing field."
1154 (let ((overlay (widget-get widget :field-overlay)))
1155 (and overlay (overlay-start overlay))))
1157 (defun widget-field-end (widget)
1158 "Return the end of WIDGET's editing field."
1159 (let ((overlay (widget-get widget :field-overlay)))
1160 ;; Don't subtract one if local-map works at the end of the overlay.
1161 (and overlay (if widget-field-add-space
1162 (1- (overlay-end overlay))
1163 (overlay-end overlay)))))
1165 (defun widget-field-find (pos)
1166 "Return the field at POS.
1167 Unlike (get-char-property POS 'field) this, works with empty fields too."
1168 (let ((fields widget-field-list)
1171 (setq field (car fields)
1172 fields (cdr fields))
1173 (let ((start (widget-field-start field))
1174 (end (widget-field-end field)))
1175 (when (and (<= start pos) (<= pos end))
1177 (debug "Overlapping fields"))
1178 (setq found field))))
1181 (defun widget-after-change (from to old)
1182 ;; Adjust field size and text properties.
1184 (let ((field (widget-field-find from))
1185 (other (widget-field-find to)))
1187 (unless (eq field other)
1188 (debug "Change in different fields"))
1189 (let ((size (widget-get field :size))
1190 (secret (widget-get field :secret)))
1192 (let ((begin (widget-field-start field))
1193 (end (widget-field-end field)))
1194 (cond ((< (- end begin) size)
1198 (insert-char ?\ (- (+ begin size) end))))
1199 ((> (- end begin) size)
1200 ;; Field too large and
1201 (if (or (< (point) (+ begin size))
1203 ;; Point is outside extra space.
1204 (setq begin (+ begin size))
1205 ;; Point is within the extra space.
1206 (setq begin (point)))
1209 (while (and (eq (preceding-char) ?\ )
1211 (delete-backward-char 1)))))))
1213 (let ((begin (widget-field-start field))
1214 (end (widget-field-end field)))
1216 (while (and (> end begin)
1217 (eq (char-after (1- end)) ?\ ))
1218 (setq end (1- end))))
1219 (while (< begin end)
1220 (let ((old (char-after begin)))
1221 (unless (eq old secret)
1222 (subst-char-in-region begin (1+ begin) old secret)
1223 (put-text-property begin (1+ begin) 'secret old))
1224 (setq begin (1+ begin)))))))
1225 (widget-apply field :notify field)))
1226 (error (debug "After Change"))))
1228 ;;; Widget Functions
1230 ;; These functions are used in the definition of multiple widgets.
1232 (defun widget-parent-action (widget &optional event)
1233 "Tell :parent of WIDGET to handle the :action.
1234 Optional EVENT is the event that triggered the action."
1235 (widget-apply (widget-get widget :parent) :action event))
1237 (defun widget-children-value-delete (widget)
1238 "Delete all :children and :buttons in WIDGET."
1239 (mapcar 'widget-delete (widget-get widget :children))
1240 (widget-put widget :children nil)
1241 (mapcar 'widget-delete (widget-get widget :buttons))
1242 (widget-put widget :buttons nil))
1244 (defun widget-children-validate (widget)
1245 "All the :children must be valid."
1246 (let ((children (widget-get widget :children))
1248 (while (and children (not found))
1249 (setq child (car children)
1250 children (cdr children)
1251 found (widget-apply child :validate)))
1254 (defun widget-types-convert-widget (widget)
1255 "Convert :args as widget types in WIDGET."
1256 (widget-put widget :args (mapcar 'widget-convert (widget-get widget :args)))
1259 (defun widget-value-convert-widget (widget)
1260 "Initialize :value from :args in WIDGET."
1261 (let ((args (widget-get widget :args)))
1263 (widget-put widget :value (car args))
1264 ;; Don't convert :value here, as this is done in `widget-convert'.
1265 ;; (widget-put widget :value (widget-apply widget
1266 ;; :value-to-internal (car args)))
1267 (widget-put widget :args nil)))
1270 (defun widget-value-value-get (widget)
1271 "Return the :value property of WIDGET."
1272 (widget-get widget :value))
1274 ;;; The `default' Widget.
1276 (define-widget 'default nil
1277 "Basic widget other widgets are derived from."
1278 :value-to-internal (lambda (widget value) value)
1279 :value-to-external (lambda (widget value) value)
1280 :button-prefix 'widget-button-prefix
1281 :button-suffix 'widget-button-suffix
1282 :complete 'widget-default-complete
1283 :create 'widget-default-create
1286 :format-handler 'widget-default-format-handler
1287 :button-face-get 'widget-default-button-face-get
1288 :sample-face-get 'widget-default-sample-face-get
1289 :delete 'widget-default-delete
1290 :value-set 'widget-default-value-set
1291 :value-inline 'widget-default-value-inline
1292 :menu-tag-get 'widget-default-menu-tag-get
1293 :validate (lambda (widget) nil)
1294 :active 'widget-default-active
1295 :activate 'widget-specify-active
1296 :deactivate 'widget-default-deactivate
1297 :mouse-down-action (lambda (widget event) nil)
1298 :action 'widget-default-action
1299 :notify 'widget-default-notify
1300 :prompt-value 'widget-default-prompt-value)
1302 (defun widget-default-complete (widget)
1303 "Call the value of the :complete-function property of WIDGET.
1304 If that does not exists, call the value of `widget-complete-field'."
1305 (let ((fun (widget-get widget :complete-function)))
1306 (call-interactively (or fun widget-complete-field))))
1308 (defun widget-default-create (widget)
1309 "Create WIDGET at point in the current buffer."
1310 (widget-specify-insert
1311 (let ((from (point))
1312 button-begin button-end
1313 sample-begin sample-end
1316 (insert (widget-get widget :format))
1318 ;; Parse escapes in format.
1319 (while (re-search-forward "%\\(.\\)" nil t)
1320 (let ((escape (aref (match-string 1) 0)))
1321 (replace-match "" t t)
1322 (cond ((eq escape ?%)
1325 (setq button-begin (point))
1326 (widget-button-insert-indirect widget :button-prefix))
1328 (widget-button-insert-indirect widget :button-suffix)
1329 (setq button-end (point)))
1331 (setq sample-begin (point)))
1333 (setq sample-end (point)))
1335 (when (widget-get widget :indent)
1337 (insert-char ? (widget-get widget :indent))))
1339 (let ((glyph (widget-get widget :tag-glyph))
1340 (tag (widget-get widget :tag)))
1342 (widget-glyph-insert widget (or tag "image") glyph))
1346 (let ((standard-output (current-buffer)))
1347 (princ (widget-get widget :value)))))))
1349 (let ((doc (widget-get widget :doc)))
1351 (setq doc-begin (point))
1353 (while (eq (preceding-char) ?\n)
1354 (delete-backward-char 1))
1356 (setq doc-end (point)))))
1358 (if (and button-begin (not button-end))
1359 (widget-apply widget :value-create)
1360 (setq value-pos (point))))
1362 (widget-apply widget :format-handler escape)))))
1363 ;; Specify button, sample, and doc, and insert value.
1364 (and button-begin button-end
1365 (widget-specify-button widget button-begin button-end))
1366 (and sample-begin sample-end
1367 (widget-specify-sample widget sample-begin sample-end))
1368 (and doc-begin doc-end
1369 (widget-specify-doc widget doc-begin doc-end))
1371 (goto-char value-pos)
1372 (widget-apply widget :value-create)))
1373 (let ((from (copy-marker (point-min)))
1374 (to (copy-marker (point-max))))
1375 (widget-specify-text from to)
1376 (set-marker-insertion-type from t)
1377 (set-marker-insertion-type to nil)
1378 (widget-put widget :from from)
1379 (widget-put widget :to to)))
1380 (widget-clear-undo))
1382 (defun widget-default-format-handler (widget escape)
1383 ;; We recognize the %h escape by default.
1384 (let* ((buttons (widget-get widget :buttons)))
1385 (cond ((eq escape ?h)
1386 (let* ((doc-property (widget-get widget :documentation-property))
1387 (doc-try (cond ((widget-get widget :doc))
1388 ((symbolp doc-property)
1389 (documentation-property
1390 (widget-get widget :value)
1393 (funcall doc-property
1394 (widget-get widget :value)))))
1395 (doc-text (and (stringp doc-try)
1396 (> (length doc-try) 1)
1398 (doc-indent (widget-get widget :documentation-indent)))
1400 (and (eq (preceding-char) ?\n)
1401 (widget-get widget :indent)
1402 (insert-char ? (widget-get widget :indent)))
1403 ;; The `*' in the beginning is redundant.
1404 (when (eq (aref doc-text 0) ?*)
1405 (setq doc-text (substring doc-text 1)))
1406 ;; Get rid of trailing newlines.
1407 (when (string-match "\n+\\'" doc-text)
1408 (setq doc-text (substring doc-text 0 (match-beginning 0))))
1409 (push (widget-create-child-and-convert
1410 widget 'documentation-string
1411 :indent (cond ((numberp doc-indent )
1419 (error "Unknown escape `%c'" escape)))
1420 (widget-put widget :buttons buttons)))
1422 (defun widget-default-button-face-get (widget)
1423 ;; Use :button-face or widget-button-face
1424 (or (widget-get widget :button-face) 'widget-button-face))
1426 (defun widget-default-sample-face-get (widget)
1427 ;; Use :sample-face.
1428 (widget-get widget :sample-face))
1430 (defun widget-default-delete (widget)
1431 ;; Remove widget from the buffer.
1432 (let ((from (widget-get widget :from))
1433 (to (widget-get widget :to))
1434 (inactive-overlay (widget-get widget :inactive))
1435 (button-overlay (widget-get widget :button-overlay))
1436 after-change-functions
1437 (inhibit-read-only t))
1438 (widget-apply widget :value-delete)
1439 (when inactive-overlay
1440 (delete-overlay inactive-overlay))
1441 (when button-overlay
1442 (delete-overlay button-overlay))
1444 ;; Kludge: this doesn't need to be true for empty formats.
1445 (delete-region from to))
1446 (set-marker from nil)
1447 (set-marker to nil))
1448 (widget-clear-undo))
1450 (defun widget-default-value-set (widget value)
1451 ;; Recreate widget with new value.
1453 (goto-char (widget-get widget :from))
1454 (widget-apply widget :delete)
1455 (widget-put widget :value value)
1456 (widget-apply widget :create)))
1458 (defun widget-default-value-inline (widget)
1459 ;; Wrap value in a list unless it is inline.
1460 (if (widget-get widget :inline)
1461 (widget-value widget)
1462 (list (widget-value widget))))
1464 (defun widget-default-menu-tag-get (widget)
1465 ;; Use tag or value for menus.
1466 (or (widget-get widget :menu-tag)
1467 (widget-get widget :tag)
1468 (widget-princ-to-string (widget-get widget :value))))
1470 (defun widget-default-active (widget)
1471 "Return t iff this widget active (user modifiable)."
1472 (and (not (widget-get widget :inactive))
1473 (let ((parent (widget-get widget :parent)))
1475 (widget-apply parent :active)))))
1477 (defun widget-default-deactivate (widget)
1478 "Make WIDGET inactive for user modifications."
1479 (widget-specify-inactive widget
1480 (widget-get widget :from)
1481 (widget-get widget :to)))
1483 (defun widget-default-action (widget &optional event)
1484 ;; Notify the parent when a widget change
1485 (let ((parent (widget-get widget :parent)))
1487 (widget-apply parent :notify widget event))))
1489 (defun widget-default-notify (widget child &optional event)
1490 ;; Pass notification to parent.
1491 (widget-default-action widget event))
1493 (defun widget-default-prompt-value (widget prompt value unbound)
1494 ;; Read an arbitrary value. Stolen from `set-variable'.
1495 ;; (let ((initial (if unbound
1497 ;; ;; It would be nice if we could do a `(cons val 1)' here.
1498 ;; (prin1-to-string (custom-quote value))))))
1499 (eval-minibuffer prompt ))
1501 ;;; The `item' Widget.
1503 (define-widget 'item 'default
1504 "Constant items for inclusion in other widgets."
1505 :convert-widget 'widget-value-convert-widget
1506 :value-create 'widget-item-value-create
1507 :value-delete 'ignore
1508 :value-get 'widget-value-value-get
1509 :match 'widget-item-match
1510 :match-inline 'widget-item-match-inline
1511 :action 'widget-item-action
1514 (defun widget-item-value-create (widget)
1515 ;; Insert the printed representation of the value.
1516 (let ((standard-output (current-buffer)))
1517 (princ (widget-get widget :value))))
1519 (defun widget-item-match (widget value)
1520 ;; Match if the value is the same.
1521 (equal (widget-get widget :value) value))
1523 (defun widget-item-match-inline (widget values)
1524 ;; Match if the value is the same.
1525 (let ((value (widget-get widget :value)))
1527 (<= (length value) (length values))
1528 (let ((head (widget-sublist values 0 (length value))))
1529 (and (equal head value)
1530 (cons head (widget-sublist values (length value))))))))
1532 (defun widget-sublist (list start &optional end)
1533 "Return the sublist of LIST from START to END.
1534 If END is omitted, it defaults to the length of LIST."
1535 (if (> start 0) (setq list (nthcdr start list)))
1539 (setq list (copy-sequence list))
1540 (setcdr (nthcdr (- end start 1) list) nil)
1542 (copy-sequence list)))
1544 (defun widget-item-action (widget &optional event)
1545 ;; Just notify itself.
1546 (widget-apply widget :notify widget event))
1548 ;;; The `push-button' Widget.
1550 (defcustom widget-push-button-gui t
1551 "If non nil, use GUI push buttons when available."
1555 ;; Cache already created GUI objects.
1556 (defvar widget-push-button-cache nil)
1558 (defcustom widget-push-button-prefix "["
1559 "String used as prefix for buttons."
1561 :group 'widget-button)
1563 (defcustom widget-push-button-suffix "]"
1564 "String used as suffix for buttons."
1566 :group 'widget-button)
1568 (define-widget 'push-button 'item
1569 "A pushable button."
1572 :value-create 'widget-push-button-value-create
1575 (defun widget-push-button-value-create (widget)
1576 ;; Insert text representing the `on' and `off' states.
1577 (let* ((tag (or (widget-get widget :tag)
1578 (widget-get widget :value)))
1579 (text (concat widget-push-button-prefix
1580 tag widget-push-button-suffix))
1581 (gui (cdr (assoc tag widget-push-button-cache))))
1582 (if (and (fboundp 'make-gui-button)
1583 (fboundp 'make-glyph)
1584 widget-push-button-gui
1585 (fboundp 'device-on-window-system-p)
1586 (device-on-window-system-p)
1587 (string-match "XEmacs" emacs-version))
1590 (setq gui (make-gui-button tag 'widget-gui-action widget))
1591 (push (cons tag gui) widget-push-button-cache))
1592 (widget-glyph-insert-glyph widget
1594 (list (nth 0 (aref gui 1))
1595 (vector 'string ':data text)))
1597 (list (nth 1 (aref gui 1))
1598 (vector 'string ':data text)))
1600 (list (nth 2 (aref gui 1))
1601 (vector 'string ':data text)))))
1604 (defun widget-gui-action (widget)
1605 "Apply :action for WIDGET."
1606 (widget-apply-action widget (this-command-keys)))
1608 ;;; The `link' Widget.
1610 (defcustom widget-link-prefix "["
1611 "String used as prefix for links."
1613 :group 'widget-button)
1615 (defcustom widget-link-suffix "]"
1616 "String used as suffix for links."
1618 :group 'widget-button)
1620 (define-widget 'link 'item
1622 :button-prefix 'widget-link-prefix
1623 :button-suffix 'widget-link-suffix
1624 :help-echo "Follow the link."
1627 ;;; The `info-link' Widget.
1629 (define-widget 'info-link 'link
1630 "A link to an info file."
1631 :action 'widget-info-link-action)
1633 (defun widget-info-link-action (widget &optional event)
1634 "Open the info node specified by WIDGET."
1635 (Info-goto-node (widget-value widget)))
1637 ;;; The `url-link' Widget.
1639 (define-widget 'url-link 'link
1640 "A link to an www page."
1641 :action 'widget-url-link-action)
1643 (defun widget-url-link-action (widget &optional event)
1644 "Open the url specified by WIDGET."
1645 (require 'browse-url)
1646 (funcall browse-url-browser-function (widget-value widget)))
1648 ;;; The `editable-field' Widget.
1650 (define-widget 'editable-field 'default
1651 "An editable text field."
1652 :convert-widget 'widget-value-convert-widget
1653 :keymap widget-field-keymap
1656 :prompt-internal 'widget-field-prompt-internal
1657 :prompt-history 'widget-field-history
1658 :prompt-value 'widget-field-prompt-value
1659 :action 'widget-field-action
1660 :validate 'widget-field-validate
1663 :value-create 'widget-field-value-create
1664 :value-delete 'widget-field-value-delete
1665 :value-get 'widget-field-value-get
1666 :match 'widget-field-match)
1668 (defvar widget-field-history nil
1669 "History of field minibuffer edits.")
1671 (defun widget-field-prompt-internal (widget prompt initial history)
1672 ;; Read string for WIDGET promptinhg with PROMPT.
1673 ;; INITIAL is the initial input and HISTORY is a symbol containing
1674 ;; the earlier input.
1675 (read-string prompt initial history))
1677 (defun widget-field-prompt-value (widget prompt value unbound)
1678 ;; Prompt for a string.
1679 (let ((initial (if unbound
1681 (cons (widget-apply widget :value-to-internal
1683 (history (widget-get widget :prompt-history)))
1684 (let ((answer (widget-apply widget
1685 :prompt-internal prompt initial history)))
1686 (widget-apply widget :value-to-external answer))))
1688 (defun widget-field-action (widget &optional event)
1689 ;; Edit the value in the minibuffer.
1690 (let ((invalid (widget-apply widget :validate)))
1691 (let ((prompt (concat (widget-apply widget :menu-tag-get) ": "))
1692 (value (unless invalid
1693 (widget-value widget))))
1694 (let ((answer (widget-apply widget :prompt-value prompt value invalid) ))
1695 (widget-value-set widget answer)))
1697 (widget-apply widget :notify widget event)))
1699 (defun widget-field-validate (widget)
1700 ;; Valid if the content matches `:valid-regexp'.
1702 (let ((value (widget-apply widget :value-get))
1703 (regexp (widget-get widget :valid-regexp)))
1704 (if (string-match regexp value)
1708 (defun widget-field-value-create (widget)
1709 ;; Create an editable text field.
1710 (let ((size (widget-get widget :size))
1711 (value (widget-get widget :value))
1713 (overlay (cons (make-marker) (make-marker))))
1714 (widget-put widget :field-overlay overlay)
1717 (< (length value) size)
1718 (insert-char ?\ (- size (length value))))
1719 (unless (memq widget widget-field-list)
1720 (setq widget-field-new (cons widget widget-field-new)))
1721 (move-marker (cdr overlay) (point))
1722 (set-marker-insertion-type (cdr overlay) nil)
1725 (move-marker (car overlay) from)
1726 (set-marker-insertion-type (car overlay) t)))
1728 (defun widget-field-value-delete (widget)
1729 ;; Remove the widget from the list of active editing fields.
1730 (setq widget-field-list (delq widget widget-field-list))
1731 ;; These are nil if the :format string doesn't contain `%v'.
1732 (let ((overlay (widget-get widget :field-overlay)))
1734 (delete-overlay overlay))))
1736 (defun widget-field-value-get (widget)
1737 ;; Return current text in editing field.
1738 (let ((from (widget-field-start widget))
1739 (to (widget-field-end widget))
1740 (buffer (widget-field-buffer widget))
1741 (size (widget-get widget :size))
1742 (secret (widget-get widget :secret))
1743 (old (current-buffer)))
1750 (eq (char-after (1- to)) ?\ ))
1752 (let ((result (buffer-substring-no-properties from to)))
1755 (while (< (+ from index) to)
1757 (get-char-property (+ from index) 'secret))
1758 (setq index (1+ index)))))
1761 (widget-get widget :value))))
1763 (defun widget-field-match (widget value)
1764 ;; Match any string.
1767 ;;; The `text' Widget.
1769 (define-widget 'text 'editable-field
1770 :keymap widget-text-keymap
1771 "A multiline text area.")
1773 ;;; The `menu-choice' Widget.
1775 (define-widget 'menu-choice 'default
1776 "A menu of options."
1777 :convert-widget 'widget-types-convert-widget
1778 :format "%[%t%]: %v"
1781 :void '(item :format "invalid (%t)\n")
1782 :value-create 'widget-choice-value-create
1783 :value-delete 'widget-children-value-delete
1784 :value-get 'widget-choice-value-get
1785 :value-inline 'widget-choice-value-inline
1786 :mouse-down-action 'widget-choice-mouse-down-action
1787 :action 'widget-choice-action
1788 :error "Make a choice"
1789 :validate 'widget-choice-validate
1790 :match 'widget-choice-match
1791 :match-inline 'widget-choice-match-inline)
1793 (defun widget-choice-value-create (widget)
1794 ;; Insert the first choice that matches the value.
1795 (let ((value (widget-get widget :value))
1796 (args (widget-get widget :args))
1799 (setq current (car args)
1801 (when (widget-apply current :match value)
1802 (widget-put widget :children (list (widget-create-child-value
1803 widget current value)))
1804 (widget-put widget :choice current)
1808 (let ((void (widget-get widget :void)))
1809 (widget-put widget :children (list (widget-create-child-and-convert
1810 widget void :value value)))
1811 (widget-put widget :choice void)))))
1813 (defun widget-choice-value-get (widget)
1814 ;; Get value of the child widget.
1815 (widget-value (car (widget-get widget :children))))
1817 (defun widget-choice-value-inline (widget)
1818 ;; Get value of the child widget.
1819 (widget-apply (car (widget-get widget :children)) :value-inline))
1821 (defcustom widget-choice-toggle nil
1822 "If non-nil, a binary choice will just toggle between the values.
1823 Otherwise, the user will explicitly have to choose between the values
1824 when he invoked the menu."
1828 (defun widget-choice-mouse-down-action (widget &optional event)
1829 ;; Return non-nil if we need a menu.
1830 (let ((args (widget-get widget :args))
1831 (old (widget-get widget :choice)))
1832 (cond ((not window-system)
1833 ;; No place to pop up a menu.
1835 ((not (or (fboundp 'x-popup-menu) (fboundp 'popup-menu)))
1836 ;; No way to pop up a menu.
1838 ((< (length args) 2)
1839 ;; Empty or singleton list, just return the value.
1841 ((> (length args) widget-menu-max-size)
1842 ;; Too long, prompt.
1844 ((> (length args) 2)
1845 ;; Reasonable sized list, use menu.
1847 ((and widget-choice-toggle (memq old args))
1851 ;; Ask which of the two.
1854 (defun widget-choice-action (widget &optional event)
1856 (let ((args (widget-get widget :args))
1857 (old (widget-get widget :choice))
1858 (tag (widget-apply widget :menu-tag-get))
1859 (completion-ignore-case (widget-get widget :case-fold))
1861 ;; Remember old value.
1862 (if (and old (not (widget-apply widget :validate)))
1863 (let* ((external (widget-value widget))
1864 (internal (widget-apply old :value-to-internal external)))
1865 (widget-put old :value internal)))
1868 (cond ((= (length args) 0)
1870 ((= (length args) 1)
1872 ((and widget-choice-toggle
1875 (if (eq old (nth 0 args))
1880 (setq current (car args)
1883 (cons (cons (widget-apply current :menu-tag-get)
1886 (widget-choose tag (reverse choices) event))))
1888 (widget-value-set widget
1889 (widget-apply current :value-to-external
1890 (widget-get current :value)))
1892 (widget-apply widget :notify widget event))))
1894 (defun widget-choice-validate (widget)
1895 ;; Valid if we have made a valid choice.
1896 (let ((void (widget-get widget :void))
1897 (choice (widget-get widget :choice))
1898 (child (car (widget-get widget :children))))
1899 (if (eq void choice)
1901 (widget-apply child :validate))))
1903 (defun widget-choice-match (widget value)
1904 ;; Matches if one of the choices matches.
1905 (let ((args (widget-get widget :args))
1907 (while (and args (not found))
1908 (setq current (car args)
1910 found (widget-apply current :match value)))
1913 (defun widget-choice-match-inline (widget values)
1914 ;; Matches if one of the choices matches.
1915 (let ((args (widget-get widget :args))
1917 (while (and args (null found))
1918 (setq current (car args)
1920 found (widget-match-inline current values)))
1923 ;;; The `toggle' Widget.
1925 (define-widget 'toggle 'item
1926 "Toggle between two states."
1928 :value-create 'widget-toggle-value-create
1929 :action 'widget-toggle-action
1930 :match (lambda (widget value) t)
1934 (defun widget-toggle-value-create (widget)
1935 ;; Insert text representing the `on' and `off' states.
1936 (if (widget-value widget)
1937 (widget-glyph-insert widget
1938 (widget-get widget :on)
1939 (widget-get widget :on-glyph))
1940 (widget-glyph-insert widget
1941 (widget-get widget :off)
1942 (widget-get widget :off-glyph))))
1944 (defun widget-toggle-action (widget &optional event)
1946 (widget-value-set widget (not (widget-value widget)))
1947 (widget-apply widget :notify widget event))
1949 ;;; The `checkbox' Widget.
1951 (define-widget 'checkbox 'toggle
1952 "A checkbox toggle."
1960 :action 'widget-checkbox-action)
1962 (defun widget-checkbox-action (widget &optional event)
1963 "Toggle checkbox, notify parent, and set active state of sibling."
1964 (widget-toggle-action widget event)
1965 (let ((sibling (widget-get-sibling widget)))
1967 (if (widget-value widget)
1968 (widget-apply sibling :activate)
1969 (widget-apply sibling :deactivate)))))
1971 ;;; The `checklist' Widget.
1973 (define-widget 'checklist 'default
1974 "A multiple choice widget."
1975 :convert-widget 'widget-types-convert-widget
1978 :entry-format "%b %v"
1979 :menu-tag "checklist"
1981 :value-create 'widget-checklist-value-create
1982 :value-delete 'widget-children-value-delete
1983 :value-get 'widget-checklist-value-get
1984 :validate 'widget-checklist-validate
1985 :match 'widget-checklist-match
1986 :match-inline 'widget-checklist-match-inline)
1988 (defun widget-checklist-value-create (widget)
1989 ;; Insert all values
1990 (let ((alist (widget-checklist-match-find widget (widget-get widget :value)))
1991 (args (widget-get widget :args)))
1993 (widget-checklist-add-item widget (car args) (assq (car args) alist))
1994 (setq args (cdr args)))
1995 (widget-put widget :children (nreverse (widget-get widget :children)))))
1997 (defun widget-checklist-add-item (widget type chosen)
1998 ;; Create checklist item in WIDGET of type TYPE.
1999 ;; If the item is checked, CHOSEN is a cons whose cdr is the value.
2000 (and (eq (preceding-char) ?\n)
2001 (widget-get widget :indent)
2002 (insert-char ? (widget-get widget :indent)))
2003 (widget-specify-insert
2004 (let* ((children (widget-get widget :children))
2005 (buttons (widget-get widget :buttons))
2006 (button-args (or (widget-get type :sibling-args)
2007 (widget-get widget :button-args)))
2010 (insert (widget-get widget :entry-format))
2012 ;; Parse % escapes in format.
2013 (while (re-search-forward "%\\([bv%]\\)" nil t)
2014 (let ((escape (aref (match-string 1) 0)))
2015 (replace-match "" t t)
2016 (cond ((eq escape ?%)
2019 (setq button (apply 'widget-create-child-and-convert
2021 :value (not (null chosen))
2026 (let ((child (widget-create-child widget type)))
2027 (widget-apply child :deactivate)
2029 ((widget-get type :inline)
2030 (widget-create-child-value
2031 widget type (cdr chosen)))
2033 (widget-create-child-value
2034 widget type (car (cdr chosen)))))))
2036 (error "Unknown escape `%c'" escape)))))
2037 ;; Update properties.
2038 (and button child (widget-put child :button button))
2039 (and button (widget-put widget :buttons (cons button buttons)))
2040 (and child (widget-put widget :children (cons child children))))))
2042 (defun widget-checklist-match (widget values)
2043 ;; All values must match a type in the checklist.
2045 (null (cdr (widget-checklist-match-inline widget values)))))
2047 (defun widget-checklist-match-inline (widget values)
2048 ;; Find the values which match a type in the checklist.
2049 (let ((greedy (widget-get widget :greedy))
2050 (args (copy-sequence (widget-get widget :args)))
2053 (let ((answer (widget-checklist-match-up args values)))
2055 (let ((vals (widget-match-inline answer values)))
2056 (setq found (append found (car vals))
2058 args (delq answer args))))
2060 (setq rest (append rest (list (car values)))
2061 values (cdr values)))
2063 (setq rest (append rest values)
2067 (defun widget-checklist-match-find (widget vals)
2068 ;; Find the vals which match a type in the checklist.
2069 ;; Return an alist of (TYPE MATCH).
2070 (let ((greedy (widget-get widget :greedy))
2071 (args (copy-sequence (widget-get widget :args)))
2074 (let ((answer (widget-checklist-match-up args vals)))
2076 (let ((match (widget-match-inline answer vals)))
2077 (setq found (cons (cons answer (car match)) found)
2079 args (delq answer args))))
2081 (setq vals (cdr vals)))
2086 (defun widget-checklist-match-up (args vals)
2087 ;; Rerturn the first type from ARGS that matches VALS.
2088 (let (current found)
2089 (while (and args (null found))
2090 (setq current (car args)
2092 found (widget-match-inline current vals)))
2097 (defun widget-checklist-value-get (widget)
2098 ;; The values of all selected items.
2099 (let ((children (widget-get widget :children))
2102 (setq child (car children)
2103 children (cdr children))
2104 (if (widget-value (widget-get child :button))
2105 (setq result (append result (widget-apply child :value-inline)))))
2108 (defun widget-checklist-validate (widget)
2109 ;; Ticked chilren must be valid.
2110 (let ((children (widget-get widget :children))
2112 (while (and children (not found))
2113 (setq child (car children)
2114 children (cdr children)
2115 button (widget-get child :button)
2116 found (and (widget-value button)
2117 (widget-apply child :validate))))
2120 ;;; The `option' Widget
2122 (define-widget 'option 'checklist
2123 "An widget with an optional item."
2126 ;;; The `choice-item' Widget.
2128 (define-widget 'choice-item 'item
2129 "Button items that delegate action events to their parents."
2130 :action 'widget-parent-action
2131 :format "%[%t%] \n")
2133 ;;; The `radio-button' Widget.
2135 (define-widget 'radio-button 'toggle
2136 "A radio button for use in the `radio' widget."
2137 :notify 'widget-radio-button-notify
2144 :off-glyph "radio0")
2146 (defun widget-radio-button-notify (widget child &optional event)
2148 (widget-apply (widget-get widget :parent) :action widget event))
2150 ;;; The `radio-button-choice' Widget.
2152 (define-widget 'radio-button-choice 'default
2153 "Select one of multiple options."
2154 :convert-widget 'widget-types-convert-widget
2157 :entry-format "%b %v"
2159 :value-create 'widget-radio-value-create
2160 :value-delete 'widget-children-value-delete
2161 :value-get 'widget-radio-value-get
2162 :value-inline 'widget-radio-value-inline
2163 :value-set 'widget-radio-value-set
2164 :error "You must push one of the buttons"
2165 :validate 'widget-radio-validate
2166 :match 'widget-choice-match
2167 :match-inline 'widget-choice-match-inline
2168 :action 'widget-radio-action)
2170 (defun widget-radio-value-create (widget)
2171 ;; Insert all values
2172 (let ((args (widget-get widget :args))
2175 (setq arg (car args)
2177 (widget-radio-add-item widget arg))))
2179 (defun widget-radio-add-item (widget type)
2180 "Add to radio widget WIDGET a new radio button item of type TYPE."
2181 ;; (setq type (widget-convert type))
2182 (and (eq (preceding-char) ?\n)
2183 (widget-get widget :indent)
2184 (insert-char ? (widget-get widget :indent)))
2185 (widget-specify-insert
2186 (let* ((value (widget-get widget :value))
2187 (children (widget-get widget :children))
2188 (buttons (widget-get widget :buttons))
2189 (button-args (or (widget-get type :sibling-args)
2190 (widget-get widget :button-args)))
2192 (chosen (and (null (widget-get widget :choice))
2193 (widget-apply type :match value)))
2195 (insert (widget-get widget :entry-format))
2197 ;; Parse % escapes in format.
2198 (while (re-search-forward "%\\([bv%]\\)" nil t)
2199 (let ((escape (aref (match-string 1) 0)))
2200 (replace-match "" t t)
2201 (cond ((eq escape ?%)
2204 (setq button (apply 'widget-create-child-and-convert
2205 widget 'radio-button
2206 :value (not (null chosen))
2209 (setq child (if chosen
2210 (widget-create-child-value
2212 (widget-create-child widget type)))
2214 (widget-apply child :deactivate)))
2216 (error "Unknown escape `%c'" escape)))))
2217 ;; Update properties.
2219 (widget-put widget :choice type))
2221 (widget-put child :button button)
2222 (widget-put widget :buttons (nconc buttons (list button))))
2224 (widget-put widget :children (nconc children (list child))))
2227 (defun widget-radio-value-get (widget)
2228 ;; Get value of the child widget.
2229 (let ((chosen (widget-radio-chosen widget)))
2230 (and chosen (widget-value chosen))))
2232 (defun widget-radio-chosen (widget)
2233 "Return the widget representing the chosen radio button."
2234 (let ((children (widget-get widget :children))
2237 (setq current (car children)
2238 children (cdr children))
2239 (let* ((button (widget-get current :button))
2240 (value (widget-apply button :value-get)))
2246 (defun widget-radio-value-inline (widget)
2247 ;; Get value of the child widget.
2248 (let ((children (widget-get widget :children))
2251 (setq current (car children)
2252 children (cdr children))
2253 (let* ((button (widget-get current :button))
2254 (value (widget-apply button :value-get)))
2256 (setq found (widget-apply current :value-inline)
2260 (defun widget-radio-value-set (widget value)
2261 ;; We can't just delete and recreate a radio widget, since children
2262 ;; can be added after the original creation and won't be recreated
2264 (let ((children (widget-get widget :children))
2267 (setq current (car children)
2268 children (cdr children))
2269 (let* ((button (widget-get current :button))
2270 (match (and (not found)
2271 (widget-apply current :match value))))
2272 (widget-value-set button match)
2275 (widget-value-set current value)
2276 (widget-apply current :activate))
2277 (widget-apply current :deactivate))
2278 (setq found (or found match))))))
2280 (defun widget-radio-validate (widget)
2281 ;; Valid if we have made a valid choice.
2282 (let ((children (widget-get widget :children))
2283 current found button)
2284 (while (and children (not found))
2285 (setq current (car children)
2286 children (cdr children)
2287 button (widget-get current :button)
2288 found (widget-apply button :value-get)))
2290 (widget-apply current :validate)
2293 (defun widget-radio-action (widget child event)
2294 ;; Check if a radio button was pressed.
2295 (let ((children (widget-get widget :children))
2296 (buttons (widget-get widget :buttons))
2298 (when (memq child buttons)
2300 (setq current (car children)
2301 children (cdr children))
2302 (let* ((button (widget-get current :button)))
2303 (cond ((eq child button)
2304 (widget-value-set button t)
2305 (widget-apply current :activate))
2306 ((widget-value button)
2307 (widget-value-set button nil)
2308 (widget-apply current :deactivate)))))))
2309 ;; Pass notification to parent.
2310 (widget-apply widget :notify child event))
2312 ;;; The `insert-button' Widget.
2314 (define-widget 'insert-button 'push-button
2315 "An insert button for the `editable-list' widget."
2317 :help-echo "Insert a new item into the list at this position."
2318 :action 'widget-insert-button-action)
2320 (defun widget-insert-button-action (widget &optional event)
2321 ;; Ask the parent to insert a new item.
2322 (widget-apply (widget-get widget :parent)
2323 :insert-before (widget-get widget :widget)))
2325 ;;; The `delete-button' Widget.
2327 (define-widget 'delete-button 'push-button
2328 "A delete button for the `editable-list' widget."
2330 :help-echo "Delete this item from the list."
2331 :action 'widget-delete-button-action)
2333 (defun widget-delete-button-action (widget &optional event)
2334 ;; Ask the parent to insert a new item.
2335 (widget-apply (widget-get widget :parent)
2336 :delete-at (widget-get widget :widget)))
2338 ;;; The `editable-list' Widget.
2340 (defcustom widget-editable-list-gui nil
2341 "If non nil, use GUI push-buttons in editable list when available."
2345 (define-widget 'editable-list 'default
2346 "A variable list of widgets of the same type."
2347 :convert-widget 'widget-types-convert-widget
2350 :format-handler 'widget-editable-list-format-handler
2351 :entry-format "%i %d %v"
2352 :menu-tag "editable-list"
2353 :value-create 'widget-editable-list-value-create
2354 :value-delete 'widget-children-value-delete
2355 :value-get 'widget-editable-list-value-get
2356 :validate 'widget-children-validate
2357 :match 'widget-editable-list-match
2358 :match-inline 'widget-editable-list-match-inline
2359 :insert-before 'widget-editable-list-insert-before
2360 :delete-at 'widget-editable-list-delete-at)
2362 (defun widget-editable-list-format-handler (widget escape)
2363 ;; We recognize the insert button.
2364 (let ((widget-push-button-gui widget-editable-list-gui))
2365 (cond ((eq escape ?i)
2366 (and (widget-get widget :indent)
2367 (insert-char ? (widget-get widget :indent)))
2368 (apply 'widget-create-child-and-convert
2369 widget 'insert-button
2370 (widget-get widget :append-button-args)))
2372 (widget-default-format-handler widget escape)))))
2374 (defun widget-editable-list-value-create (widget)
2375 ;; Insert all values
2376 (let* ((value (widget-get widget :value))
2377 (type (nth 0 (widget-get widget :args)))
2378 (inlinep (widget-get type :inline))
2380 (widget-put widget :value-pos (copy-marker (point)))
2381 (set-marker-insertion-type (widget-get widget :value-pos) t)
2383 (let ((answer (widget-match-inline type value)))
2385 (setq children (cons (widget-editable-list-entry-create
2394 (widget-put widget :children (nreverse children))))
2396 (defun widget-editable-list-value-get (widget)
2397 ;; Get value of the child widget.
2398 (apply 'append (mapcar (lambda (child) (widget-apply child :value-inline))
2399 (widget-get widget :children))))
2401 (defun widget-editable-list-match (widget value)
2402 ;; Value must be a list and all the members must match the type.
2404 (null (cdr (widget-editable-list-match-inline widget value)))))
2406 (defun widget-editable-list-match-inline (widget value)
2407 (let ((type (nth 0 (widget-get widget :args)))
2410 (while (and value ok)
2411 (let ((answer (widget-match-inline type value)))
2413 (setq found (append found (car answer))
2416 (cons found value)))
2418 (defun widget-editable-list-insert-before (widget before)
2419 ;; Insert a new child in the list of children.
2421 (let ((children (widget-get widget :children))
2422 (inhibit-read-only t)
2423 after-change-functions)
2425 (goto-char (widget-get before :entry-from)))
2427 (goto-char (widget-get widget :value-pos))))
2428 (let ((child (widget-editable-list-entry-create
2430 (when (< (widget-get child :entry-from) (widget-get widget :from))
2431 (set-marker (widget-get widget :from)
2432 (widget-get child :entry-from)))
2433 (widget-specify-text (widget-get child :entry-from)
2434 (widget-get child :entry-to))
2435 (if (eq (car children) before)
2436 (widget-put widget :children (cons child children))
2437 (while (not (eq (car (cdr children)) before))
2438 (setq children (cdr children)))
2439 (setcdr children (cons child (cdr children)))))))
2441 (widget-apply widget :notify widget))
2443 (defun widget-editable-list-delete-at (widget child)
2444 ;; Delete child from list of children.
2446 (let ((buttons (copy-sequence (widget-get widget :buttons)))
2448 (inhibit-read-only t)
2449 after-change-functions)
2451 (setq button (car buttons)
2452 buttons (cdr buttons))
2453 (when (eq (widget-get button :widget) child)
2455 :buttons (delq button (widget-get widget :buttons)))
2456 (widget-delete button))))
2457 (let ((entry-from (widget-get child :entry-from))
2458 (entry-to (widget-get child :entry-to))
2459 (inhibit-read-only t)
2460 after-change-functions)
2461 (widget-delete child)
2462 (delete-region entry-from entry-to)
2463 (set-marker entry-from nil)
2464 (set-marker entry-to nil))
2465 (widget-put widget :children (delq child (widget-get widget :children))))
2467 (widget-apply widget :notify widget))
2469 (defun widget-editable-list-entry-create (widget value conv)
2470 ;; Create a new entry to the list.
2471 (let ((type (nth 0 (widget-get widget :args)))
2472 (widget-push-button-gui widget-editable-list-gui)
2473 child delete insert)
2474 (widget-specify-insert
2476 (and (widget-get widget :indent)
2477 (insert-char ? (widget-get widget :indent)))
2478 (insert (widget-get widget :entry-format)))
2479 ;; Parse % escapes in format.
2480 (while (re-search-forward "%\\(.\\)" nil t)
2481 (let ((escape (aref (match-string 1) 0)))
2482 (replace-match "" t t)
2483 (cond ((eq escape ?%)
2486 (setq insert (apply 'widget-create-child-and-convert
2487 widget 'insert-button
2488 (widget-get widget :insert-button-args))))
2490 (setq delete (apply 'widget-create-child-and-convert
2491 widget 'delete-button
2492 (widget-get widget :delete-button-args))))
2495 (setq child (widget-create-child-value
2497 (setq child (widget-create-child widget type))))
2499 (error "Unknown escape `%c'" escape)))))
2501 :buttons (cons delete
2503 (widget-get widget :buttons))))
2504 (let ((entry-from (copy-marker (point-min)))
2505 (entry-to (copy-marker (point-max))))
2506 (widget-specify-text entry-from entry-to)
2507 (set-marker-insertion-type entry-from t)
2508 (set-marker-insertion-type entry-to nil)
2509 (widget-put child :entry-from entry-from)
2510 (widget-put child :entry-to entry-to)))
2511 (widget-put insert :widget child)
2512 (widget-put delete :widget child)
2515 ;;; The `group' Widget.
2517 (define-widget 'group 'default
2518 "A widget which group other widgets inside."
2519 :convert-widget 'widget-types-convert-widget
2521 :value-create 'widget-group-value-create
2522 :value-delete 'widget-children-value-delete
2523 :value-get 'widget-editable-list-value-get
2524 :validate 'widget-children-validate
2525 :match 'widget-group-match
2526 :match-inline 'widget-group-match-inline)
2528 (defun widget-group-value-create (widget)
2529 ;; Create each component.
2530 (let ((args (widget-get widget :args))
2531 (value (widget-get widget :value))
2532 arg answer children)
2534 (setq arg (car args)
2536 answer (widget-match-inline arg value)
2538 (and (eq (preceding-char) ?\n)
2539 (widget-get widget :indent)
2540 (insert-char ? (widget-get widget :indent)))
2541 (push (cond ((null answer)
2542 (widget-create-child widget arg))
2543 ((widget-get arg :inline)
2544 (widget-create-child-value widget arg (car answer)))
2546 (widget-create-child-value widget arg (car (car answer)))))
2548 (widget-put widget :children (nreverse children))))
2550 (defun widget-group-match (widget values)
2551 ;; Match if the components match.
2553 (let ((match (widget-group-match-inline widget values)))
2554 (and match (null (cdr match))))))
2556 (defun widget-group-match-inline (widget vals)
2557 ;; Match if the components match.
2558 (let ((args (widget-get widget :args))
2559 argument answer found)
2561 (setq argument (car args)
2563 answer (widget-match-inline argument vals))
2565 (setq vals (cdr answer)
2566 found (append found (car answer)))
2573 ;;; The `visibility' Widget.
2575 (define-widget 'visibility 'item
2576 "An indicator and manipulator for hidden items."
2582 :value-create 'widget-visibility-value-create
2583 :action 'widget-toggle-action
2584 :match (lambda (widget value) t))
2586 (defun widget-visibility-value-create (widget)
2587 ;; Insert text representing the `on' and `off' states.
2588 (let ((on (widget-get widget :on))
2589 (off (widget-get widget :off)))
2591 (setq on (concat widget-push-button-prefix
2593 widget-push-button-suffix))
2596 (setq off (concat widget-push-button-prefix
2598 widget-push-button-suffix))
2600 (if (widget-value widget)
2601 (widget-glyph-insert widget on "down" "down-pushed")
2602 (widget-glyph-insert widget off "right" "right-pushed")
2605 ;;; The `documentation-link' Widget.
2607 ;; This is a helper widget for `documentation-string'.
2609 (define-widget 'documentation-link 'link
2610 "Link type used in documentation strings."
2612 :help-echo 'widget-documentation-link-echo-help
2613 :action 'widget-documentation-link-action)
2615 (defun widget-documentation-link-echo-help (widget)
2616 "Tell what this link will describe."
2617 (concat "Describe the `" (widget-get widget :value) "' symbol."))
2619 (defun widget-documentation-link-action (widget &optional event)
2620 "Run apropos on WIDGET's value. Ignore optional argument EVENT."
2621 (apropos (concat "\\`" (regexp-quote (widget-get widget :value)) "\\'")))
2623 (defcustom widget-documentation-links t
2624 "Add hyperlinks to documentation strings when non-nil."
2626 :group 'widget-documentation)
2628 (defcustom widget-documentation-link-regexp "`\\([^\n`' ]+\\)'"
2629 "Regexp for matching potential links in documentation strings.
2630 The first group should be the link itself."
2632 :group 'widget-documentation)
2634 (defcustom widget-documentation-link-p 'intern-soft
2635 "Predicate used to test if a string is useful as a link.
2636 The value should be a function. The function will be called one
2637 argument, a string, and should return non-nil if there should be a
2638 link for that string."
2640 :options '(widget-documentation-link-p)
2641 :group 'widget-documentation)
2643 (defcustom widget-documentation-link-type 'documentation-link
2644 "Widget type used for links in documentation strings."
2646 :group 'widget-documentation)
2648 (defun widget-documentation-link-add (widget from to)
2649 (widget-specify-doc widget from to)
2650 (when widget-documentation-links
2651 (let ((regexp widget-documentation-link-regexp)
2652 (predicate widget-documentation-link-p)
2653 (type widget-documentation-link-type)
2654 (buttons (widget-get widget :buttons)))
2657 (while (re-search-forward regexp to t)
2658 (let ((name (match-string 1))
2659 (begin (match-beginning 1))
2660 (end (match-end 1)))
2661 (when (funcall predicate name)
2662 (push (widget-convert-button type begin end :value name)
2664 (widget-put widget :buttons buttons)))
2665 (let ((indent (widget-get widget :indent)))
2666 (when (and indent (not (zerop indent)))
2669 (narrow-to-region from to)
2670 (goto-char (point-min))
2671 (while (search-forward "\n" nil t)
2672 (insert-char ?\ indent)))))))
2674 ;;; The `documentation-string' Widget.
2676 (define-widget 'documentation-string 'item
2677 "A documentation string."
2679 :action 'widget-documentation-string-action
2680 :value-delete 'widget-children-value-delete
2681 :value-create 'widget-documentation-string-value-create)
2683 (defun widget-documentation-string-value-create (widget)
2684 ;; Insert documentation string.
2685 (let ((doc (widget-value widget))
2686 (indent (widget-get widget :indent))
2687 (shown (widget-get (widget-get widget :parent) :documentation-shown))
2689 (if (string-match "\n" doc)
2690 (let ((before (substring doc 0 (match-beginning 0)))
2691 (after (substring doc (match-beginning 0)))
2694 (widget-documentation-link-add widget start (point))
2695 (push (widget-create-child-and-convert
2697 :help-echo "Show or hide rest of the documentation."
2699 :action 'widget-parent-action
2703 (setq start (point))
2704 (when (and indent (not (zerop indent)))
2705 (insert-char ?\ indent))
2707 (widget-documentation-link-add widget start (point)))
2708 (widget-put widget :buttons buttons))
2710 (widget-documentation-link-add widget start (point))))
2713 (defun widget-documentation-string-action (widget &rest ignore)
2714 ;; Toggle documentation.
2715 (let ((parent (widget-get widget :parent)))
2716 (widget-put parent :documentation-shown
2717 (not (widget-get parent :documentation-shown))))
2719 (widget-value-set widget (widget-value widget)))
2721 ;;; The Sexp Widgets.
2723 (define-widget 'const 'item
2724 "An immutable sexp."
2725 :prompt-value 'widget-const-prompt-value
2728 (defun widget-const-prompt-value (widget prompt value unbound)
2729 ;; Return the value of the const.
2730 (widget-value widget))
2732 (define-widget 'function-item 'const
2733 "An immutable function name."
2735 :documentation-property (lambda (symbol)
2737 (documentation symbol t)
2740 (define-widget 'variable-item 'const
2741 "An immutable variable name."
2743 :documentation-property 'variable-documentation)
2745 (defvar widget-string-prompt-value-history nil
2746 "History of input to `widget-string-prompt-value'.")
2748 (define-widget 'string 'editable-field
2751 :format "%{%t%}: %v"
2752 :complete-function 'ispell-complete-word
2753 :prompt-history 'widget-string-prompt-value-history)
2755 (define-widget 'regexp 'string
2756 "A regular expression."
2757 :match 'widget-regexp-match
2758 :validate 'widget-regexp-validate
2761 (defun widget-regexp-match (widget value)
2762 ;; Match valid regexps.
2763 (and (stringp value)
2766 (string-match value ""))
2769 (defun widget-regexp-validate (widget)
2770 "Check that the value of WIDGET is a valid regexp."
2771 (let ((val (widget-value widget)))
2772 (condition-case data
2774 (string-match val ""))
2775 (error (widget-put widget :error (error-message-string data))
2778 (define-widget 'file 'string
2780 It will read a file name from the minibuffer when invoked."
2781 :prompt-value 'widget-file-prompt-value
2782 :format "%{%t%}: %v"
2784 :action 'widget-file-action)
2786 (defun widget-file-prompt-value (widget prompt value unbound)
2787 ;; Read file from minibuffer.
2788 (abbreviate-file-name
2790 (read-file-name prompt)
2791 (let ((prompt2 (format "%s (default %s) " prompt value))
2792 (dir (file-name-directory value))
2793 (file (file-name-nondirectory value))
2794 (must-match (widget-get widget :must-match)))
2795 (read-file-name prompt2 dir nil must-match file)))))
2797 (defun widget-file-action (widget &optional event)
2798 ;; Read a file name from the minibuffer.
2799 (let* ((value (widget-value widget))
2800 (dir (file-name-directory value))
2801 (file (file-name-nondirectory value))
2802 (menu-tag (widget-apply widget :menu-tag-get))
2803 (must-match (widget-get widget :must-match))
2804 (answer (read-file-name (concat menu-tag ": (default `" value "') ")
2805 dir nil must-match file)))
2806 (widget-value-set widget (abbreviate-file-name answer))
2808 (widget-apply widget :notify widget event)))
2810 (define-widget 'directory 'file
2811 "A directory widget.
2812 It will read a directory name from the minibuffer when invoked."
2815 (defvar widget-symbol-prompt-value-history nil
2816 "History of input to `widget-symbol-prompt-value'.")
2818 (define-widget 'symbol 'editable-field
2822 :format "%{%t%}: %v"
2823 :match (lambda (widget value) (symbolp value))
2824 :prompt-internal 'widget-symbol-prompt-internal
2825 :prompt-match 'symbolp
2826 :prompt-history 'widget-symbol-prompt-value-history
2827 :value-to-internal (lambda (widget value)
2831 :value-to-external (lambda (widget value)
2836 (defun widget-symbol-prompt-internal (widget prompt initial history)
2837 ;; Read file from minibuffer.
2838 (let ((answer (completing-read prompt obarray
2839 (widget-get widget :prompt-match)
2840 nil initial history)))
2841 (if (and (stringp answer)
2842 (not (zerop (length answer))))
2844 (error "No value"))))
2846 (defvar widget-function-prompt-value-history nil
2847 "History of input to `widget-function-prompt-value'.")
2849 (define-widget 'function 'sexp
2851 :complete-function 'lisp-complete-symbol
2852 :prompt-value 'widget-field-prompt-value
2853 :prompt-internal 'widget-symbol-prompt-internal
2854 :prompt-match 'fboundp
2855 :prompt-history 'widget-function-prompt-value-history
2856 :action 'widget-field-action
2859 (defvar widget-variable-prompt-value-history nil
2860 "History of input to `widget-variable-prompt-value'.")
2862 (define-widget 'variable 'symbol
2863 ;; Should complete on variables.
2865 :prompt-match 'boundp
2866 :prompt-history 'widget-variable-prompt-value-history
2869 (when (featurep 'mule)
2870 (defvar widget-coding-system-prompt-value-history nil
2871 "History of input to `widget-coding-system-prompt-value'.")
2873 (define-widget 'coding-system 'symbol
2874 "A MULE coding-system."
2875 :format "%{%t%}: %v"
2876 :tag "Coding system"
2877 :prompt-history 'widget-coding-system-prompt-value-history
2878 :prompt-value 'widget-coding-system-prompt-value
2879 :action 'widget-coding-system-action)
2881 (defun widget-coding-system-prompt-value (widget prompt value unbound)
2882 ;; Read coding-system from minibuffer.
2884 (completing-read (format "%s (default %s) " prompt value)
2887 (list (symbol-name sym))
2889 (coding-system-list)))))
2891 (defun widget-coding-system-action (widget &optional event)
2892 ;; Read a file name from the minibuffer.
2894 (widget-coding-system-prompt-value
2896 (widget-apply widget :menu-tag-get)
2897 (widget-value widget)
2899 (widget-value-set widget answer)
2900 (widget-apply widget :notify widget event)
2904 (define-widget 'sexp 'editable-field
2905 "An arbitrary lisp expression."
2906 :tag "Lisp expression"
2907 :format "%{%t%}: %v"
2909 :validate 'widget-sexp-validate
2910 :match (lambda (widget value) t)
2911 :value-to-internal 'widget-sexp-value-to-internal
2912 :value-to-external (lambda (widget value) (read value))
2913 :prompt-history 'widget-sexp-prompt-value-history
2914 :prompt-value 'widget-sexp-prompt-value)
2916 (defun widget-sexp-value-to-internal (widget value)
2917 ;; Use pp for printer representation.
2918 (let ((pp (if (symbolp value)
2919 (prin1-to-string value)
2920 (pp-to-string value))))
2921 (while (string-match "\n\\'" pp)
2922 (setq pp (substring pp 0 -1)))
2923 (if (or (string-match "\n\\'" pp)
2928 (defun widget-sexp-validate (widget)
2929 ;; Valid if we can read the string and there is no junk left after it.
2931 (let ((buffer (set-buffer (get-buffer-create " *Widget Scratch*"))))
2933 (insert (widget-apply widget :value-get))
2934 (goto-char (point-min))
2935 (condition-case data
2936 (let ((value (read buffer)))
2938 (if (widget-apply widget :match value)
2940 (widget-put widget :error (widget-get widget :type-error))
2943 :error (format "Junk at end of expression: %s"
2944 (buffer-substring (point)
2947 (error (widget-put widget :error (error-message-string data))
2950 (defvar widget-sexp-prompt-value-history nil
2951 "History of input to `widget-sexp-prompt-value'.")
2953 (defun widget-sexp-prompt-value (widget prompt value unbound)
2954 ;; Read an arbitrary sexp.
2955 (let ((found (read-string prompt
2956 (if unbound nil (cons (prin1-to-string value) 0))
2957 (widget-get widget :prompt-history))))
2959 (let ((buffer (set-buffer (get-buffer-create " *Widget Scratch*"))))
2962 (goto-char (point-min))
2963 (let ((answer (read buffer)))
2965 (error "Junk at end of expression: %s"
2966 (buffer-substring (point) (point-max))))
2969 (define-widget 'integer 'sexp
2973 :type-error "This field should contain an integer"
2974 :value-to-internal (lambda (widget value)
2975 (if (integerp value)
2976 (prin1-to-string value)
2978 :match (lambda (widget value) (integerp value)))
2980 (define-widget 'character 'editable-field
2985 :format "%{%t%}: %v\n"
2986 :valid-regexp "\\`.\\'"
2987 :error "This field should contain a single character"
2988 :value-to-internal (lambda (widget value)
2991 (char-to-string value)))
2992 :value-to-external (lambda (widget value)
2996 :match (lambda (widget value)
2997 (if (fboundp 'characterp)
3001 (define-widget 'number 'sexp
3002 "A floating point number."
3005 :type-error "This field should contain a number"
3006 :value-to-internal (lambda (widget value)
3008 (prin1-to-string value)
3010 :match (lambda (widget value) (numberp value)))
3012 (define-widget 'list 'group
3015 :format "%{%t%}:\n%v")
3017 (define-widget 'vector 'group
3020 :format "%{%t%}:\n%v"
3021 :match 'widget-vector-match
3022 :value-to-internal (lambda (widget value) (append value nil))
3023 :value-to-external (lambda (widget value) (apply 'vector value)))
3025 (defun widget-vector-match (widget value)
3026 (and (vectorp value)
3027 (widget-group-match widget
3028 (widget-apply widget :value-to-internal value))))
3030 (define-widget 'cons 'group
3033 :format "%{%t%}:\n%v"
3034 :match 'widget-cons-match
3035 :value-to-internal (lambda (widget value)
3036 (list (car value) (cdr value)))
3037 :value-to-external (lambda (widget value)
3038 (cons (nth 0 value) (nth 1 value))))
3040 (defun widget-cons-match (widget value)
3042 (widget-group-match widget
3043 (widget-apply widget :value-to-internal value))))
3045 (define-widget 'choice 'menu-choice
3046 "A union of several sexp types."
3048 :format "%{%t%}: %[value menu%] %v"
3049 :button-prefix 'widget-push-button-prefix
3050 :button-suffix 'widget-push-button-suffix
3051 :prompt-value 'widget-choice-prompt-value)
3053 (defun widget-choice-prompt-value (widget prompt value unbound)
3055 (let ((args (widget-get widget :args))
3056 (completion-ignore-case (widget-get widget :case-fold))
3057 current choices old)
3058 ;; Find the first arg that match VALUE.
3061 (if (widget-apply (car look) :match value)
3062 (setq old (car look)
3064 (setq look (cdr look)))))
3067 (cond ((= (length args) 0)
3069 ((= (length args) 1)
3071 ((and (= (length args) 2)
3073 (if (eq old (nth 0 args))
3078 (setq current (car args)
3081 (cons (cons (widget-apply current :menu-tag-get)
3084 (let ((val (completing-read prompt choices nil t)))
3086 (let ((try (try-completion val choices)))
3089 (cdr (assoc val choices)))
3092 (widget-prompt-value current prompt nil t)
3095 (define-widget 'radio 'radio-button-choice
3096 "A union of several sexp types."
3098 :format "%{%t%}:\n%v"
3099 :prompt-value 'widget-choice-prompt-value)
3101 (define-widget 'repeat 'editable-list
3102 "A variable length homogeneous list."
3104 :format "%{%t%}:\n%v%i\n")
3106 (define-widget 'set 'checklist
3107 "A list of members from a fixed set."
3109 :format "%{%t%}:\n%v")
3111 (define-widget 'boolean 'toggle
3112 "To be nil or non-nil, that is the question."
3114 :prompt-value 'widget-boolean-prompt-value
3115 :button-prefix 'widget-push-button-prefix
3116 :button-suffix 'widget-push-button-suffix
3117 :format "%{%t%}: %[toggle%] %v\n")
3119 (defun widget-boolean-prompt-value (widget prompt value unbound)
3120 ;; Toggle a boolean.
3123 ;;; The `color' Widget.
3125 (define-widget 'color-item 'choice-item
3126 "A color name (with sample)."
3127 :format "%v (%{sample%})\n"
3128 :sample-face-get 'widget-color-item-button-face-get)
3130 (defun widget-color-item-button-face-get (widget)
3131 (let ((symbol (intern (concat "fg:" (widget-value widget)))))
3132 (if (string-match "XEmacs" emacs-version)
3134 (or (find-face symbol)
3135 (set-face-foreground (make-face symbol) (widget-value widget))))
3137 (facemenu-get-face symbol)
3138 (error 'default)))))
3140 (define-widget 'color 'push-button
3141 "Choose a color name (with sample)."
3142 :format "%[%t%]: %v"
3145 :value-create 'widget-color-value-create
3146 :value-delete 'widget-children-value-delete
3147 :value-get 'widget-color-value-get
3148 :value-set 'widget-color-value-set
3149 :action 'widget-color-action
3150 :match 'widget-field-match
3153 (defvar widget-color-choice-list nil)
3154 ;; Variable holding the possible colors.
3156 (defun widget-color-choice-list ()
3157 (unless widget-color-choice-list
3158 (setq widget-color-choice-list
3159 (mapcar '(lambda (color) (list color))
3160 (x-defined-colors))))
3161 widget-color-choice-list)
3163 (defun widget-color-value-create (widget)
3164 (let ((child (widget-create-child-and-convert
3165 widget 'color-item (widget-get widget :value))))
3166 (widget-put widget :children (list child))))
3168 (defun widget-color-value-get (widget)
3169 ;; Pass command to first child.
3170 (widget-apply (car (widget-get widget :children)) :value-get))
3172 (defun widget-color-value-set (widget value)
3173 ;; Pass command to first child.
3174 (widget-apply (car (widget-get widget :children)) :value-set value))
3176 (defvar widget-color-history nil
3177 "History of entered colors")
3179 (defun widget-color-action (widget &optional event)
3180 ;; Prompt for a color.
3181 (let* ((tag (widget-apply widget :menu-tag-get))
3182 (prompt (concat tag ": "))
3183 (answer (cond ((string-match "XEmacs" emacs-version)
3184 (read-color prompt))
3185 ((fboundp 'x-defined-colors)
3186 (completing-read (concat tag ": ")
3187 (widget-color-choice-list)
3188 nil nil nil 'widget-color-history))
3190 (read-string prompt (widget-value widget))))))
3191 (unless (zerop (length answer))
3192 (widget-value-set widget answer)
3194 (widget-apply widget :notify widget event))))
3198 (defun widget-echo-help-mouse ()
3199 "Display the help message for the widget under the mouse.
3200 Enable with (run-with-idle-timer 1 t 'widget-echo-help-mouse)"
3201 (let* ((pos (mouse-position))
3205 (win (window-at x y frame))
3206 (where (coordinates-in-window-p (cons x y) win)))
3208 (save-window-excursion
3209 (progn ; save-excursion
3211 (let* ((result (compute-motion (window-start win)
3216 (cons (window-hscroll) 0)
3218 (when (and (eq (nth 1 result) x)
3219 (eq (nth 2 result) y))
3220 (widget-echo-help (nth 0 result))))))))
3222 (setq track-mouse t)
3223 (add-hook 'post-command-hook 'widget-stop-mouse-tracking)))
3225 (defun widget-stop-mouse-tracking (&rest args)
3226 "Stop the mouse tracking done while idle."
3227 (remove-hook 'post-command-hook 'widget-stop-mouse-tracking)
3228 (setq track-mouse nil))
3230 (defun widget-at (pos)
3231 "The button or field at POS."
3232 (or (get-char-property pos 'button)
3233 (get-char-property pos 'field)))
3235 (defun widget-echo-help (pos)
3236 "Display the help echo for widget at POS."
3237 (let* ((widget (widget-at pos))
3238 (help-echo (and widget (widget-get widget :help-echo))))
3239 (cond ((stringp help-echo)
3240 (message "%s" help-echo))
3241 ((and (symbolp help-echo) (fboundp help-echo)
3242 (stringp (setq help-echo (funcall help-echo widget))))
3243 (message "%s" help-echo)))))
3249 ;; wid-edit.el ends here