1 ;;; wid-edit.el --- Functions for creating and using widgets -*-byte-compile-dynamic: t;-*-
3 ;; Copyright (C) 1996, 1997, 1999, 2000 Free Software Foundation, Inc.
5 ;; Author: Per Abrahamsen <abraham@dina.kvl.dk>
7 ;; Keywords: extensions
9 ;; This file is part of GNU Emacs.
11 ;; GNU Emacs is free software; you can redistribute it and/or modify
12 ;; it under the terms of the GNU General Public License as published by
13 ;; the Free Software Foundation; either version 2, or (at your option)
16 ;; GNU Emacs is distributed in the hope that it will be useful,
17 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
18 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 ;; GNU General Public License for more details.
21 ;; You should have received a copy of the GNU General Public License
22 ;; along with GNU Emacs; see the file COPYING. If not, write to the
23 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
24 ;; Boston, MA 02111-1307, USA.
34 (defun widget-event-point (event)
35 "Character position of the end of event if that exists, or nil."
36 (posn-point (event-end event
)))
38 (autoload 'pp-to-string
"pp")
39 (autoload 'Info-goto-node
"info")
41 (defun widget-button-release-event-p (event)
42 "Non-nil if EVENT is a mouse-button-release event object."
44 (memq (event-basic-type event
) '(mouse-1 mouse-2 mouse-3
))
45 (or (memq 'click
(event-modifiers event
))
46 (memq 'drag
(event-modifiers event
)))))
51 "Customization support for the Widget Library."
52 :link
'(custom-manual "(widget)Top")
53 :link
'(emacs-library-link :tag
"Lisp File" "widget.el")
58 (defgroup widget-documentation nil
59 "Options controling the display of documentation strings."
62 (defgroup widget-faces nil
63 "Faces used by the widget library."
67 (defvar widget-documentation-face
'widget-documentation-face
68 "Face used for documentation strings in widgets.
69 This exists as a variable so it can be set locally in certain buffers.")
71 (defface widget-documentation-face
'((((class color
)
73 (:foreground
"lime green"))
76 (:foreground
"dark green"))
78 "Face used for documentation text."
79 :group
'widget-documentation
82 (defvar widget-button-face
'widget-button-face
83 "Face used for buttons in widgets.
84 This exists as a variable so it can be set locally in certain buffers.")
86 (defface widget-button-face
'((t (:bold t
)))
87 "Face used for widget buttons."
90 (defcustom widget-mouse-face
'highlight
91 "Face used for widget buttons when the mouse is above them."
95 (defface widget-field-face
'((((class grayscale color
)
97 (:background
"gray85"))
98 (((class grayscale color
)
100 (:background
"dim gray"))
103 "Face used for editable fields."
104 :group
'widget-faces
)
106 (defface widget-single-line-field-face
'((((class grayscale color
)
108 (:background
"gray85"))
109 (((class grayscale color
)
111 (:background
"dim gray"))
114 "Face used for editable fields spanning only a single line."
115 :group
'widget-faces
)
117 ;;; This causes display-table to be loaded, and not usefully.
118 ;;;(defvar widget-single-line-display-table
119 ;;; (let ((table (make-display-table)))
120 ;;; (aset table 9 "^I")
121 ;;; (aset table 10 "^J")
123 ;;; "Display table used for single-line editable fields.")
125 ;;;(when (fboundp 'set-face-display-table)
126 ;;; (set-face-display-table 'widget-single-line-field-face
127 ;;; widget-single-line-display-table))
129 ;;; Utility functions.
131 ;; These are not really widget specific.
133 (defun widget-princ-to-string (object)
134 "Return string representation of OBJECT, any Lisp object.
135 No quoting characters are used; no delimiters are printed around
136 the contents of strings."
137 (with-output-to-string
140 (defun widget-clear-undo ()
141 "Clear all undo information."
142 (buffer-disable-undo (current-buffer))
143 (buffer-enable-undo))
145 (defcustom widget-menu-max-size
40
146 "Largest number of items allowed in a popup-menu.
147 Larger menus are read through the minibuffer."
151 (defcustom widget-menu-max-shortcuts
40
152 "Largest number of items for which it works to choose one with a character.
153 For a larger number of items, the minibuffer is used."
157 (defcustom widget-menu-minibuffer-flag nil
158 "*Control how to ask for a choice from the keyboard.
159 Non-nil means use the minibuffer;
160 nil means read a single character."
164 (defun widget-choose (title items
&optional event
)
165 "Choose an item from a list.
167 First argument TITLE is the name of the list.
168 Second argument ITEMS is an list whose members are either
169 (NAME . VALUE), to indicate selectable items, or just strings to
170 indicate unselectable items.
171 Optional third argument EVENT is an input event.
173 The user is asked to choose between each NAME from the items alist,
174 and the VALUE of the chosen element will be returned. If EVENT is a
175 mouse event, and the number of elements in items is less than
176 `widget-menu-max-size', a popup menu will be used, otherwise the
178 (cond ((and (< (length items
) widget-menu-max-size
)
179 event
(fboundp 'x-popup-menu
) window-system
)
180 ;; We are in Emacs-19, pressed by the mouse
182 (list title
(cons "" items
))))
183 ((or widget-menu-minibuffer-flag
184 (> (length items
) widget-menu-max-shortcuts
))
185 ;; Read the choice of name from the minibuffer.
186 (setq items
(widget-remove-if 'stringp items
))
187 (let ((val (completing-read (concat title
": ") items nil t
)))
189 (let ((try (try-completion val items
)))
192 (cdr (assoc val items
))))))
194 ;; Construct a menu of the choices
195 ;; and then use it for prompting for a single character.
196 (let* ((overriding-terminal-local-map
197 (make-sparse-keymap))
198 map choice
(next-digit ?
0)
201 ;; Define SPC as a prefix char to get to this menu.
202 (define-key overriding-terminal-local-map
" "
203 (setq map
(make-sparse-keymap title
)))
205 (set-buffer (get-buffer-create " widget-choose"))
207 (insert "Available choices:\n\n")
209 (setq choice
(car items
) items
(cdr items
))
211 (let* ((name (car choice
))
212 (function (cdr choice
)))
213 (insert (format "%c = %s\n" next-digit name
))
214 (define-key map
(vector next-digit
) function
)
215 (setq some-choice-enabled t
)))
216 ;; Allocate digits to disabled alternatives
217 ;; so that the digit of a given alternative never varies.
218 (setq next-digit
(1+ next-digit
)))
219 (insert "\nC-g = Quit"))
220 (or some-choice-enabled
221 (error "None of the choices is currently meaningful"))
222 (define-key map
[?\C-g
] 'keyboard-quit
)
223 (define-key map
[t] 'keyboard-quit)
224 (define-key map [?\M-\C-v] 'scroll-other-window)
225 (define-key map [?\M--] 'negative-argument)
226 (setcdr map (nreverse (cdr map)))
227 ;; Read a char with the menu, and return the result
228 ;; that corresponds to it.
229 (save-window-excursion
230 (let ((buf (get-buffer " widget-choose")))
232 (let ((cursor-in-echo-area t)
236 (while (not (or (and (>= char ?0) (< char next-digit))
237 (eq value 'keyboard-quit)))
238 ;; Unread a SPC to lead to our new menu.
239 (setq unread-command-events (cons ?\ unread-command-events))
240 (setq keys (read-key-sequence title))
242 (lookup-key overriding-terminal-local-map keys t)
243 char (string-to-char (substring keys 1)))
244 (cond ((eq value 'scroll-other-window)
245 (let ((minibuffer-scroll-window
246 (get-buffer-window buf)))
248 (scroll-other-window-down
249 (window-height minibuffer-scroll-window))
250 (scroll-other-window))
252 ((eq value 'negative-argument)
256 (when (eq value 'keyboard-quit)
260 (defun widget-remove-if (predictate list)
261 (let (result (tail list))
263 (or (funcall predictate (car tail))
264 (setq result (cons (car tail) result)))
265 (setq tail (cdr tail)))
268 ;;; Widget text specifications.
270 ;; These functions are for specifying text properties.
272 (defvar widget-field-add-space t
273 "Non-nil means add extra space at the end of editable text fields.
274 If you don't add the space, it will become impossible to edit a zero
277 (defvar widget-field-use-before-change t
278 "Non-nil means use `before-change-functions' to track editable fields.
279 This enables the use of undo, but doesn't work on Emacs 19.34 and earlier.
280 Using before hooks also means that the :notify function can't know the
283 (defun widget-specify-field (widget from to)
284 "Specify editable button for WIDGET between FROM and TO."
285 ;; Terminating space is not part of the field, but necessary in
286 ;; order for local-map to work. Remove next sexp if local-map works
287 ;; at the end of the overlay.
290 (cond ((null (widget-get widget :size))
292 (widget-field-add-space
293 (insert-and-inherit " ")))
295 (let ((map (widget-get widget :keymap))
296 (face (or (widget-get widget :value-face) 'widget-field-face))
297 (help-echo (widget-get widget :help-echo))
298 (overlay (make-overlay from to nil
299 nil (or (not widget-field-add-space)
300 (widget-get widget :size)))))
301 (widget-put widget :field-overlay overlay)
302 ;;(overlay-put overlay 'detachable nil)
303 (overlay-put overlay 'field widget)
304 (overlay-put overlay 'keymap map)
305 (overlay-put overlay 'face face)
306 ;;(overlay-put overlay 'balloon-help help-echo)
307 (if (stringp help-echo)
308 (overlay-put overlay 'help-echo help-echo)))
309 (widget-specify-secret widget))
311 (defun widget-specify-secret (field)
312 "Replace text in FIELD with value of `:secret', if non-nil."
313 (let ((secret (widget-get field :secret))
314 (size (widget-get field :size)))
316 (let ((begin (widget-field-start field))
317 (end (widget-field-end field)))
319 (while (and (> end begin)
320 (eq (char-after (1- end)) ?\ ))
321 (setq end (1- end))))
323 (let ((old (char-after begin)))
324 (unless (eq old secret)
325 (subst-char-in-region begin (1+ begin) old secret)
326 (put-text-property begin (1+ begin) 'secret old))
327 (setq begin (1+ begin))))))))
329 (defun widget-specify-button (widget from to)
330 "Specify button for WIDGET between FROM and TO."
331 (let ((face (widget-apply widget :button-face-get))
332 (help-echo (widget-get widget :help-echo))
333 (overlay (make-overlay from to nil t nil)))
334 (widget-put widget :button-overlay overlay)
335 (overlay-put overlay 'button widget)
336 ;; We want to avoid the face with image buttons.
337 (unless (widget-get widget :suppress-face)
338 (overlay-put overlay 'face face)
339 (overlay-put overlay 'mouse-face widget-mouse-face))
340 ;;(overlay-put overlay 'balloon-help help-echo)
341 (if (stringp help-echo)
342 (overlay-put overlay 'help-echo help-echo))))
344 (defun widget-specify-sample (widget from to)
345 "Specify sample for WIDGET between FROM and TO."
346 (let ((face (widget-apply widget :sample-face-get))
347 (overlay (make-overlay from to nil t nil)))
348 (overlay-put overlay 'face face)
349 (widget-put widget :sample-overlay overlay)))
351 (defun widget-specify-doc (widget from to)
352 "Specify documentation for WIDGET between FROM and TO."
353 (let ((overlay (make-overlay from to nil t nil)))
354 (overlay-put overlay 'widget-doc widget)
355 (overlay-put overlay 'face widget-documentation-face)
356 (widget-put widget :doc-overlay overlay)))
358 (defmacro widget-specify-insert (&rest form)
359 "Execute FORM without inheriting any text properties."
361 (let ((inhibit-read-only t)
363 before-change-functions
364 after-change-functions)
366 (narrow-to-region (- (point) 2) (point))
367 (goto-char (1+ (point-min)))
368 (setq result (progn ,@form))
369 (delete-region (point-min) (1+ (point-min)))
370 (delete-region (1- (point-max)) (point-max))
371 (goto-char (point-max))
374 (defface widget-inactive-face '((((class grayscale color)
376 (:foreground "light gray"))
377 (((class grayscale color)
379 (:foreground "dim gray"))
382 "Face used for inactive widgets."
383 :group 'widget-faces)
385 (defun widget-specify-inactive (widget from to)
386 "Make WIDGET inactive for user modifications."
387 (unless (widget-get widget :inactive)
388 (let ((overlay (make-overlay from to nil t nil)))
389 (overlay-put overlay 'face 'widget-inactive-face)
390 ;; This is disabled, as it makes the mouse cursor change shape.
391 ;; (overlay-put overlay 'mouse-face 'widget-inactive-face)
392 (overlay-put overlay 'evaporate t)
393 (overlay-put overlay 'priority 100)
394 (overlay-put overlay 'modification-hooks '(widget-overlay-inactive))
395 (widget-put widget :inactive overlay))))
397 (defun widget-overlay-inactive (&rest junk)
398 "Ignoring the arguments, signal an error."
399 (unless inhibit-read-only
400 (error "The widget here is not active")))
403 (defun widget-specify-active (widget)
404 "Make WIDGET active for user modifications."
405 (let ((inactive (widget-get widget :inactive)))
407 (delete-overlay inactive)
408 (widget-put widget :inactive nil))))
410 ;;; Widget Properties.
412 (defsubst widget-type (widget)
413 "Return the type of WIDGET, a symbol."
416 (defun widget-get-indirect (widget property)
417 "In WIDGET, get the value of PROPERTY.
418 If the value is a symbol, return its binding.
419 Otherwise, just return the value."
420 (let ((value (widget-get widget property)))
425 (defun widget-member (widget property)
426 "Non-nil iff there is a definition in WIDGET for PROPERTY."
427 (cond ((plist-member (cdr widget) property)
430 (widget-member (get (car widget) 'widget-type) property))
433 (defun widget-value (widget)
434 "Extract the current value of WIDGET."
436 :value-to-external (widget-apply widget :value-get)))
438 (defun widget-value-set (widget value)
439 "Set the current value of WIDGET to VALUE."
441 :value-set (widget-apply widget
442 :value-to-internal value)))
444 (defun widget-default-get (widget)
445 "Extract the default value of WIDGET."
446 (or (widget-get widget :value)
447 (widget-apply widget :default-get)))
449 (defun widget-match-inline (widget vals)
450 "In WIDGET, match the start of VALS."
451 (cond ((widget-get widget :inline)
452 (widget-apply widget :match-inline vals))
454 (widget-apply widget :match (car vals)))
455 (cons (list (car vals)) (cdr vals)))
458 (defun widget-apply-action (widget &optional event)
459 "Apply :action in WIDGET in response to EVENT."
460 (if (widget-apply widget :active)
461 (widget-apply widget :action event)
462 (error "Attempt to perform action on inactive widget")))
464 ;;; Helper functions.
466 ;; These are widget specific.
469 (defun widget-prompt-value (widget prompt &optional value unbound)
470 "Prompt for a value matching WIDGET, using PROMPT.
471 The current value is assumed to be VALUE, unless UNBOUND is non-nil."
472 (unless (listp widget)
473 (setq widget (list widget)))
474 (setq prompt (format "[%s] %s" (widget-type widget) prompt))
475 (setq widget (widget-convert widget))
476 (let ((answer (widget-apply widget :prompt-value prompt value unbound)))
477 (unless (widget-apply widget :match answer)
478 (error "Value does not match %S type" (car widget)))
481 (defun widget-get-sibling (widget)
482 "Get the item WIDGET is assumed to toggle.
483 This is only meaningful for radio buttons or checkboxes in a list."
484 (let* ((parent (widget-get widget :parent))
485 (children (widget-get parent :children))
489 (setq child (car children)
490 children (cdr children))
491 (when (eq (widget-get child :button) widget)
492 (throw 'child child)))
495 (defun widget-map-buttons (function &optional buffer maparg)
496 "Map FUNCTION over the buttons in BUFFER.
497 FUNCTION is called with the arguments WIDGET and MAPARG.
499 If FUNCTION returns non-nil, the walk is cancelled.
501 The arguments MAPARG, and BUFFER default to nil and (current-buffer),
503 (let ((cur (point-min))
507 (save-excursion (set-buffer buffer) (overlay-lists))
509 (setq overlays (append (car overlays) (cdr overlays)))
510 (while (setq cur (pop overlays))
511 (setq widget (overlay-get cur 'button))
512 (if (and widget (funcall function widget maparg))
513 (setq overlays nil)))))
517 (defcustom widget-image-directory (file-name-as-directory
518 (expand-file-name "custom" data-directory))
519 "Where widget button images are located.
520 If this variable is nil, widget will try to locate the directory
525 (defcustom widget-image-enable t
526 "If non nil, use image buttons in widgets when available."
531 (defcustom widget-image-conversion
532 '((xpm ".xpm") (gif ".gif") (png ".png") (jpeg ".jpg" ".jpeg")
534 "Conversion alist from image formats to file name suffixes."
536 :type '(repeat (cons :format "%v"
537 (symbol :tag "Image Format" unknown)
538 (repeat :tag "Suffixes"
539 (string :format "%v")))))
541 (defun widget-image-find (image)
542 "Create a graphical button from IMAGE.
543 IMAGE should either already be an image, or be a file name sans
544 extension (xpm, xbm, gif, jpg, or png) located in
545 `widget-image-directory' or otherwise where `find-image' will find it."
546 (cond ((not (and image widget-image-enable (display-graphic-p)))
547 ;; We don't want or can't use images.
550 (eq 'image (car image)))
551 ;; Already an image spec. Use it.
554 ;; A string. Look it up in relevant directories.
555 (let* ((load-path (cons widget-image-directory load-path))
556 (formats widget-image-conversion)
558 (dolist (elt widget-image-conversion)
559 (dolist (ext (cdr elt))
560 (push (list :type (car elt) :file (concat image ext)) specs)))
561 (setq specs (nreverse specs))
567 (defvar widget-button-pressed-face 'widget-button-pressed-face
568 "Face used for pressed buttons in widgets.
569 This exists as a variable so it can be set locally in certain
572 (defun widget-image-insert (widget tag image &optional down inactive)
573 "In WIDGET, insert the text TAG or, if supported, IMAGE.
574 IMAGE should either be an image or an image file name sans extension
575 \(xpm, xbm, gif, jpg, or png) located in `widget-image-directory'.
577 Optional arguments DOWN and INACTIVE are used instead of IMAGE when the
578 button is pressed or inactive, respectively. These are currently ignored."
579 (if (and (display-graphic-p)
580 (setq image (widget-image-find image)))
581 (progn (widget-put widget :suppress-face t)
584 tag 'mouse-face widget-button-pressed-face)))
589 (defgroup widget-button nil
590 "The look of various kinds of buttons."
593 (defcustom widget-button-prefix ""
594 "String used as prefix for buttons."
596 :group 'widget-button)
598 (defcustom widget-button-suffix ""
599 "String used as suffix for buttons."
601 :group 'widget-button)
603 ;;; Creating Widgets.
606 (defun widget-create (type &rest args)
607 "Create widget of TYPE.
608 The optional ARGS are additional keyword arguments."
609 (let ((widget (apply 'widget-convert type args)))
610 (widget-apply widget :create)
613 (defun widget-create-child-and-convert (parent type &rest args)
614 "As part of the widget PARENT, create a child widget TYPE.
615 The child is converted, using the keyword arguments ARGS."
616 (let ((widget (apply 'widget-convert type args)))
617 (widget-put widget :parent parent)
618 (unless (widget-get widget :indent)
619 (widget-put widget :indent (+ (or (widget-get parent :indent) 0)
620 (or (widget-get widget :extra-offset) 0)
621 (widget-get parent :offset))))
622 (widget-apply widget :create)
625 (defun widget-create-child (parent type)
626 "Create widget of TYPE."
627 (let ((widget (copy-sequence type)))
628 (widget-put widget :parent parent)
629 (unless (widget-get widget :indent)
630 (widget-put widget :indent (+ (or (widget-get parent :indent) 0)
631 (or (widget-get widget :extra-offset) 0)
632 (widget-get parent :offset))))
633 (widget-apply widget :create)
636 (defun widget-create-child-value (parent type value)
637 "Create widget of TYPE with value VALUE."
638 (let ((widget (copy-sequence type)))
639 (widget-put widget :value (widget-apply widget :value-to-internal value))
640 (widget-put widget :parent parent)
641 (unless (widget-get widget :indent)
642 (widget-put widget :indent (+ (or (widget-get parent :indent) 0)
643 (or (widget-get widget :extra-offset) 0)
644 (widget-get parent :offset))))
645 (widget-apply widget :create)
649 (defun widget-delete (widget)
651 (widget-apply widget :delete))
653 (defun widget-convert (type &rest args)
654 "Convert TYPE to a widget without inserting it in the buffer.
655 The optional ARGS are additional keyword arguments."
656 ;; Don't touch the type.
657 (let* ((widget (if (symbolp type)
659 (copy-sequence type)))
662 ;; First set the :args keyword.
663 (while (cdr current) ;Look in the type.
664 (let ((next (car (cdr current))))
666 (setq current (cdr (cdr current)))
667 (setcdr current (list :args (cdr current)))
668 (setq current nil))))
669 (while args ;Look in the args.
670 (let ((next (nth 0 args)))
672 (setq args (nthcdr 2 args))
673 (widget-put widget :args args)
675 ;; Then Convert the widget.
678 (let ((convert-widget (plist-get (cdr type) :convert-widget)))
680 (setq widget (funcall convert-widget widget))))
681 (setq type (get (car type) 'widget-type)))
682 ;; Finally set the keyword args.
684 (let ((next (nth 0 keys)))
687 (widget-put widget next (nth 1 keys))
688 (setq keys (nthcdr 2 keys)))
690 ;; Convert the :value to internal format.
691 (if (widget-member widget :value)
692 (let ((value (widget-get widget :value)))
694 :value (widget-apply widget :value-to-internal value))))
695 ;; Return the newly create widget.
698 (defun widget-insert (&rest args)
699 "Call `insert' with ARGS and make the text read only."
700 (let ((inhibit-read-only t)
701 before-change-functions
702 after-change-functions
704 (apply 'insert args)))
706 (defun widget-convert-text (type from to
707 &optional button-from button-to
709 "Return a widget of type TYPE with endpoint FROM TO.
710 Optional ARGS are extra keyword arguments for TYPE.
711 and TO will be used as the widgets end points. If optional arguments
712 BUTTON-FROM and BUTTON-TO are given, these will be used as the widgets
714 Optional ARGS are extra keyword arguments for TYPE."
715 (let ((widget (apply 'widget-convert type :delete 'widget-leave-text args))
716 (from (copy-marker from))
717 (to (copy-marker to)))
718 (set-marker-insertion-type from t)
719 (set-marker-insertion-type to nil)
720 (widget-put widget :from from)
721 (widget-put widget :to to)
723 (widget-specify-button widget button-from button-to))
726 (defun widget-convert-button (type from to &rest args)
727 "Return a widget of type TYPE with endpoint FROM TO.
728 Optional ARGS are extra keyword arguments for TYPE.
729 No text will be inserted to the buffer, instead the text between FROM
730 and TO will be used as the widgets end points, as well as the widgets
732 (apply 'widget-convert-text type from to from to args))
734 (defun widget-leave-text (widget)
735 "Remove markers and overlays from WIDGET and its children."
736 (let ((from (widget-get widget :from))
737 (to (widget-get widget :to))
738 (button (widget-get widget :button-overlay))
739 (sample (widget-get widget :sample-overlay))
740 (doc (widget-get widget :doc-overlay))
741 (field (widget-get widget :field-overlay))
742 (children (widget-get widget :children)))
743 (set-marker from nil)
746 (delete-overlay button))
748 (delete-overlay sample))
750 (delete-overlay doc))
752 (delete-overlay field))
753 (mapc 'widget-leave-text children)))
755 ;;; Keymap and Commands.
757 (defvar widget-keymap
758 (let ((map (make-sparse-keymap)))
759 (define-key map "\t" 'widget-forward)
760 (define-key map [(shift tab)] 'widget-backward)
761 (define-key map [backtab] 'widget-backward)
762 (define-key map [down-mouse-2] 'widget-button-click)
763 (define-key map "\C-m" 'widget-button-press)
765 "Keymap containing useful binding for buffers containing widgets.
766 Recommended as a parent keymap for modes using widgets.")
768 (defvar widget-global-map global-map
769 "Keymap used for events the widget does not handle themselves.")
770 (make-variable-buffer-local 'widget-global-map)
772 (defvar widget-field-keymap
773 (let ((map (copy-keymap widget-keymap)))
774 (define-key map [menu-bar] nil)
775 (define-key map "\C-k" 'widget-kill-line)
776 (define-key map "\M-\t" 'widget-complete)
777 (define-key map "\C-m" 'widget-field-activate)
778 (define-key map "\C-a" 'widget-beginning-of-line)
779 (define-key map "\C-e" 'widget-end-of-line)
780 (set-keymap-parent map global-map)
782 "Keymap used inside an editable field.")
784 (defvar widget-text-keymap
785 (let ((map (copy-keymap widget-keymap)))
786 (define-key map [menu-bar] 'nil)
787 (define-key map "\C-a" 'widget-beginning-of-line)
788 (define-key map "\C-e" 'widget-end-of-line)
789 (set-keymap-parent map global-map)
791 "Keymap used inside a text field.")
793 (defun widget-field-activate (pos &optional event)
794 "Invoke the ediable field at point."
796 (let ((field (get-char-property pos 'field)))
798 (widget-apply-action field event)
800 (lookup-key widget-global-map (this-command-keys))))))
802 (defface widget-button-pressed-face
806 (:bold t :underline t)))
807 "Face used for pressed buttons."
808 :group 'widget-faces)
810 (defun widget-button-click (event)
811 "Invoke the button that the mouse is pointing at."
813 (if (widget-event-point event)
815 (mouse-set-point event)
816 (let* ((pos (widget-event-point event))
817 (button (get-char-property pos 'button)))
819 (let* ((overlay (widget-get button :button-overlay))
820 (face (overlay-get overlay 'face))
821 (mouse-face (overlay-get overlay 'mouse-face)))
823 (let ((track-mouse t))
825 (when face ; avoid changing around image
827 'face widget-button-pressed-face)
829 'mouse-face widget-button-pressed-face))
830 (unless (widget-apply button :mouse-down-action event)
831 (while (not (widget-button-release-event-p event))
832 (setq event (read-event)
833 pos (widget-event-point event))
835 (eq (get-char-property pos 'button)
840 widget-button-pressed-face)
843 widget-button-pressed-face))
844 (overlay-put overlay 'face face)
845 (overlay-put overlay 'mouse-face mouse-face))))
847 (eq (get-char-property pos 'button) button))
848 (widget-apply-action button event))))
849 (overlay-put overlay 'face face)
850 (overlay-put overlay 'mouse-face mouse-face)))
853 ;; Find the global command to run, and check whether it
854 ;; is bound to an up event.
855 (if (memq (event-basic-type event) '(mouse-1 down-mouse-1))
856 (cond ((setq command ;down event
857 (lookup-key widget-global-map [down-mouse-1]))
859 ((setq command ;up event
860 (lookup-key widget-global-map [mouse-1]))))
861 (cond ((setq command ;down event
862 (lookup-key widget-global-map [down-mouse-2]))
864 ((setq command ;up event
865 (lookup-key widget-global-map [mouse-2])))))
867 ;; Don't execute up events twice.
868 (while (not (widget-button-release-event-p event))
869 (setq event (read-event))))
871 (call-interactively command)))))
872 (unless (pos-visible-in-window-p (widget-event-point event))
873 (mouse-set-point event)
876 (message "You clicked somewhere weird.")))
878 (defun widget-button-press (pos &optional event)
879 "Invoke button at POS."
881 (let ((button (get-char-property pos 'button)))
883 (widget-apply-action button event)
884 (let ((command (lookup-key widget-global-map (this-command-keys))))
885 (when (commandp command)
886 (call-interactively command))))))
888 (defun widget-tabable-at (&optional pos)
889 "Return the tabable widget at POS, or nil.
890 POS defaults to the value of (point)."
893 (let ((widget (or (get-char-property pos 'button)
894 (get-char-property pos 'field))))
896 (let ((order (widget-get widget :tab-order)))
902 (defvar widget-use-overlay-change t
903 "If non-nil, use overlay change functions to tab around in the buffer.
904 This is much faster, but doesn't work reliably on Emacs 19.34.")
906 (defun widget-move (arg)
907 "Move point to the ARG next field or button.
908 ARG may be negative to move backward."
909 (or (bobp) (> arg 0) (backward-char))
912 (old (widget-tabable-at))
917 (goto-char (point-min)))
918 (widget-use-overlay-change
919 (goto-char (next-overlay-change (point))))
922 (and (eq pos (point))
924 (error "No buttons or fields found"))
925 (let ((new (widget-tabable-at)))
933 (goto-char (point-max)))
934 (widget-use-overlay-change
935 (goto-char (previous-overlay-change (point))))
938 (and (eq pos (point))
940 (error "No buttons or fields found"))
941 (let ((new (widget-tabable-at)))
944 (setq arg (1+ arg))))))
945 (let ((new (widget-tabable-at)))
946 (while (eq (widget-tabable-at) new)
949 (widget-echo-help (point))
950 (run-hooks 'widget-move-hook))
952 (defun widget-forward (arg)
953 "Move point to the next field or button.
954 With optional ARG, move across that many fields."
956 (run-hooks 'widget-forward-hook)
959 (defun widget-backward (arg)
960 "Move point to the previous field or button.
961 With optional ARG, move across that many fields."
963 (run-hooks 'widget-backward-hook)
964 (widget-move (- arg)))
966 (defun widget-beginning-of-line ()
967 "Go to beginning of field or beginning of line, whichever is first."
969 (let* ((field (widget-field-find (point)))
970 (start (and field (widget-field-start field)))
971 (bol (line-beginning-position)))
976 (defun widget-end-of-line ()
977 "Go to end of field or end of line, whichever is first."
979 (let* ((field (widget-field-find (point)))
980 (end (and field (widget-field-end field)))
981 (eol (line-end-position)))
986 (defun widget-kill-line ()
987 "Kill to end of field or end of line, whichever is first."
989 (let* ((field (widget-field-find (point)))
990 (newline (save-excursion (forward-line 1) (point)))
991 (end (and field (widget-field-end field))))
992 (if (and field (> newline end))
993 (kill-region (point) end)
994 (call-interactively 'kill-line))))
996 (defcustom widget-complete-field (lookup-key global-map "\M-\t")
997 "Default function to call for completion inside fields."
998 :options '(ispell-complete-word complete-tag lisp-complete-symbol)
1002 (defun widget-complete ()
1003 "Complete content of editable field from point.
1004 When not inside a field, move to the previous button or field."
1006 (let ((field (widget-field-find (point))))
1008 (widget-apply field :complete)
1009 (error "Not in an editable field"))))
1011 ;;; Setting up the buffer.
1013 (defvar widget-field-new nil)
1014 ;; List of all newly created editable fields in the buffer.
1015 (make-variable-buffer-local 'widget-field-new)
1017 (defvar widget-field-list nil)
1018 ;; List of all editable fields in the buffer.
1019 (make-variable-buffer-local 'widget-field-list)
1021 (defun widget-setup ()
1022 "Setup current buffer so editing string widgets works."
1023 (let ((inhibit-read-only t)
1024 (after-change-functions nil)
1025 before-change-functions
1027 (while widget-field-new
1028 (setq field (car widget-field-new)
1029 widget-field-new (cdr widget-field-new)
1030 widget-field-list (cons field widget-field-list))
1031 (let ((from (car (widget-get field :field-overlay)))
1032 (to (cdr (widget-get field :field-overlay))))
1033 (widget-specify-field field
1034 (marker-position from) (marker-position to))
1035 (set-marker from nil)
1036 (set-marker to nil))))
1038 (widget-add-change))
1040 (defvar widget-field-last nil)
1041 ;; Last field containing point.
1042 (make-variable-buffer-local 'widget-field-last)
1044 (defvar widget-field-was nil)
1045 ;; The widget data before the change.
1046 (make-variable-buffer-local 'widget-field-was)
1048 (defun widget-field-buffer (widget)
1049 "Return the start of WIDGET's editing field."
1050 (let ((overlay (widget-get widget :field-overlay)))
1051 (and overlay (overlay-buffer overlay))))
1053 (defun widget-field-start (widget)
1054 "Return the start of WIDGET's editing field."
1055 (let ((overlay (widget-get widget :field-overlay)))
1056 (and overlay (overlay-start overlay))))
1058 (defun widget-field-end (widget)
1059 "Return the end of WIDGET's editing field."
1060 (let ((overlay (widget-get widget :field-overlay)))
1061 ;; Don't subtract one if local-map works at the end of the overlay.
1062 (and overlay (if (or widget-field-add-space
1063 (null (widget-get widget :size)))
1064 (1- (overlay-end overlay))
1065 (overlay-end overlay)))))
1067 (defun widget-field-find (pos)
1068 "Return the field at POS.
1069 Unlike (get-char-property POS 'field) this, works with empty fields too."
1070 (let ((fields widget-field-list)
1073 (setq field (car fields)
1074 fields (cdr fields))
1075 (let ((start (widget-field-start field))
1076 (end (widget-field-end field)))
1077 (when (and (<= start pos) (<= pos end))
1079 (debug "Overlapping fields"))
1080 (setq found field))))
1083 (defun widget-before-change (from to)
1084 ;; This is how, for example, a variable changes its state to `modified'.
1085 ;; when it is being edited.
1086 (unless inhibit-read-only
1087 (let ((from-field (widget-field-find from))
1088 (to-field (widget-field-find to)))
1089 (cond ((not (eq from-field to-field))
1090 (add-hook 'post-command-hook 'widget-add-change nil t)
1091 (signal 'text-read-only
1092 '("Change should be restricted to a single field")))
1094 (add-hook 'post-command-hook 'widget-add-change nil t)
1095 (signal 'text-read-only
1096 '("Attempt to change text outside editable field")))
1097 (widget-field-use-before-change
1099 (widget-apply from-field :notify from-field)
1100 (error (debug "Before Change"))))))))
1102 (defun widget-add-change ()
1103 (make-local-hook 'post-command-hook)
1104 (remove-hook 'post-command-hook 'widget-add-change t)
1105 (make-local-hook 'before-change-functions)
1106 (add-hook 'before-change-functions 'widget-before-change nil t)
1107 (make-local-hook 'after-change-functions)
1108 (add-hook 'after-change-functions 'widget-after-change nil t))
1110 (defun widget-after-change (from to old)
1111 "Adjust field size and text properties."
1113 (let ((field (widget-field-find from))
1114 (other (widget-field-find to)))
1116 (unless (eq field other)
1117 (debug "Change in different fields"))
1118 (let ((size (widget-get field :size)))
1120 (let ((begin (widget-field-start field))
1121 (end (widget-field-end field)))
1122 (cond ((< (- end begin) size)
1126 (insert-char ?\ (- (+ begin size) end))))
1127 ((> (- end begin) size)
1128 ;; Field too large and
1129 (if (or (< (point) (+ begin size))
1131 ;; Point is outside extra space.
1132 (setq begin (+ begin size))
1133 ;; Point is within the extra space.
1134 (setq begin (point)))
1137 (while (and (eq (preceding-char) ?\ )
1139 (delete-backward-char 1)))))))
1140 (widget-specify-secret field))
1141 (widget-apply field :notify field)))
1142 (error (debug "After Change"))))
1144 ;;; Widget Functions
1146 ;; These functions are used in the definition of multiple widgets.
1148 (defun widget-parent-action (widget &optional event)
1149 "Tell :parent of WIDGET to handle the :action.
1150 Optional EVENT is the event that triggered the action."
1151 (widget-apply (widget-get widget :parent) :action event))
1153 (defun widget-children-value-delete (widget)
1154 "Delete all :children and :buttons in WIDGET."
1155 (mapc 'widget-delete (widget-get widget :children))
1156 (widget-put widget :children nil)
1157 (mapc 'widget-delete (widget-get widget :buttons))
1158 (widget-put widget :buttons nil))
1160 (defun widget-children-validate (widget)
1161 "All the :children must be valid."
1162 (let ((children (widget-get widget :children))
1164 (while (and children (not found))
1165 (setq child (car children)
1166 children (cdr children)
1167 found (widget-apply child :validate)))
1170 (defun widget-types-convert-widget (widget)
1171 "Convert :args as widget types in WIDGET."
1172 (widget-put widget :args (mapcar 'widget-convert (widget-get widget :args)))
1175 (defun widget-value-convert-widget (widget)
1176 "Initialize :value from :args in WIDGET."
1177 (let ((args (widget-get widget :args)))
1179 (widget-put widget :value (car args))
1180 ;; Don't convert :value here, as this is done in `widget-convert'.
1181 ;; (widget-put widget :value (widget-apply widget
1182 ;; :value-to-internal (car args)))
1183 (widget-put widget :args nil)))
1186 (defun widget-value-value-get (widget)
1187 "Return the :value property of WIDGET."
1188 (widget-get widget :value))
1190 ;;; The `default' Widget.
1192 (define-widget 'default nil
1193 "Basic widget other widgets are derived from."
1194 :value-to-internal (lambda (widget value) value)
1195 :value-to-external (lambda (widget value) value)
1196 :button-prefix 'widget-button-prefix
1197 :button-suffix 'widget-button-suffix
1198 :complete 'widget-default-complete
1199 :create 'widget-default-create
1202 :format-handler 'widget-default-format-handler
1203 :button-face-get 'widget-default-button-face-get
1204 :sample-face-get 'widget-default-sample-face-get
1205 :delete 'widget-default-delete
1206 :value-set 'widget-default-value-set
1207 :value-inline 'widget-default-value-inline
1208 :default-get 'widget-default-default-get
1209 :menu-tag-get 'widget-default-menu-tag-get
1211 :active 'widget-default-active
1212 :activate 'widget-specify-active
1213 :deactivate 'widget-default-deactivate
1214 :mouse-down-action #'ignore
1215 :action 'widget-default-action
1216 :notify 'widget-default-notify
1217 :prompt-value 'widget-default-prompt-value)
1219 (defun widget-default-complete (widget)
1220 "Call the value of the :complete-function property of WIDGET.
1221 If that does not exists, call the value of `widget-complete-field'."
1222 (let ((fun (widget-get widget :complete-function)))
1223 (call-interactively (or fun widget-complete-field))))
1225 (defun widget-default-create (widget)
1226 "Create WIDGET at point in the current buffer."
1227 (widget-specify-insert
1228 (let ((from (point))
1229 button-begin button-end
1230 sample-begin sample-end
1233 (insert (widget-get widget :format))
1235 ;; Parse escapes in format.
1236 (while (re-search-forward "%\\(.\\)" nil t)
1237 (let ((escape (aref (match-string 1) 0)))
1238 (replace-match "" t t)
1239 (cond ((eq escape ?%)
1242 (setq button-begin (point))
1243 (insert (widget-get-indirect widget :button-prefix)))
1245 (insert (widget-get-indirect widget :button-suffix))
1246 (setq button-end (point)))
1248 (setq sample-begin (point)))
1250 (setq sample-end (point)))
1252 (when (widget-get widget :indent)
1254 (insert-char ? (widget-get widget :indent))))
1256 (let ((image (widget-get widget :tag-glyph))
1257 (tag (widget-get widget :tag)))
1259 (widget-image-insert widget (or tag "image") image))
1263 (princ (widget-get widget :value)
1264 (current-buffer))))))
1266 (let ((doc (widget-get widget :doc)))
1268 (setq doc-begin (point))
1270 (while (eq (preceding-char) ?\n)
1271 (delete-backward-char 1))
1273 (setq doc-end (point)))))
1275 (if (and button-begin (not button-end))
1276 (widget-apply widget :value-create)
1277 (setq value-pos (point))))
1279 (widget-apply widget :format-handler escape)))))
1280 ;; Specify button, sample, and doc, and insert value.
1281 (and button-begin button-end
1282 (widget-specify-button widget button-begin button-end))
1283 (and sample-begin sample-end
1284 (widget-specify-sample widget sample-begin sample-end))
1285 (and doc-begin doc-end
1286 (widget-specify-doc widget doc-begin doc-end))
1288 (goto-char value-pos)
1289 (widget-apply widget :value-create)))
1290 (let ((from (copy-marker (point-min)))
1291 (to (copy-marker (point-max))))
1292 (set-marker-insertion-type from t)
1293 (set-marker-insertion-type to nil)
1294 (widget-put widget :from from)
1295 (widget-put widget :to to)))
1296 (widget-clear-undo))
1298 (defun widget-default-format-handler (widget escape)
1299 ;; We recognize the %h escape by default.
1300 (let* ((buttons (widget-get widget :buttons)))
1301 (cond ((eq escape ?h)
1302 (let* ((doc-property (widget-get widget :documentation-property))
1303 (doc-try (cond ((widget-get widget :doc))
1304 ((symbolp doc-property)
1305 (documentation-property
1306 (widget-get widget :value)
1309 (funcall doc-property
1310 (widget-get widget :value)))))
1311 (doc-text (and (stringp doc-try)
1312 (> (length doc-try) 1)
1314 (doc-indent (widget-get widget :documentation-indent)))
1316 (and (eq (preceding-char) ?\n)
1317 (widget-get widget :indent)
1318 (insert-char ? (widget-get widget :indent)))
1319 ;; The `*' in the beginning is redundant.
1320 (when (eq (aref doc-text 0) ?*)
1321 (setq doc-text (substring doc-text 1)))
1322 ;; Get rid of trailing newlines.
1323 (when (string-match "\n+\\'" doc-text)
1324 (setq doc-text (substring doc-text 0 (match-beginning 0))))
1325 (push (widget-create-child-and-convert
1326 widget 'documentation-string
1327 :indent (cond ((numberp doc-indent )
1335 (error "Unknown escape `%c'" escape)))
1336 (widget-put widget :buttons buttons)))
1338 (defun widget-default-button-face-get (widget)
1339 ;; Use :button-face or widget-button-face
1340 (or (widget-get widget :button-face)
1341 (let ((parent (widget-get widget :parent)))
1343 (widget-apply parent :button-face-get)
1344 widget-button-face))))
1346 (defun widget-default-sample-face-get (widget)
1347 ;; Use :sample-face.
1348 (widget-get widget :sample-face))
1350 (defun widget-default-delete (widget)
1351 "Remove widget from the buffer."
1352 (let ((from (widget-get widget :from))
1353 (to (widget-get widget :to))
1354 (inactive-overlay (widget-get widget :inactive))
1355 (button-overlay (widget-get widget :button-overlay))
1356 (sample-overlay (widget-get widget :sample-overlay))
1357 (doc-overlay (widget-get widget :doc-overlay))
1358 before-change-functions
1359 after-change-functions
1360 (inhibit-read-only t))
1361 (widget-apply widget :value-delete)
1362 (when inactive-overlay
1363 (delete-overlay inactive-overlay))
1364 (when button-overlay
1365 (delete-overlay button-overlay))
1366 (when sample-overlay
1367 (delete-overlay sample-overlay))
1369 (delete-overlay doc-overlay))
1371 ;; Kludge: this doesn't need to be true for empty formats.
1372 (delete-region from to))
1373 (set-marker from nil)
1374 (set-marker to nil))
1375 (widget-clear-undo))
1377 (defun widget-default-value-set (widget value)
1378 "Recreate widget with new value."
1379 (let* ((old-pos (point))
1380 (from (copy-marker (widget-get widget :from)))
1381 (to (copy-marker (widget-get widget :to)))
1382 (offset (if (and (<= from old-pos) (<= old-pos to))
1383 (if (>= old-pos (1- to))
1385 (- old-pos from)))))
1386 ;;??? Bug: this ought to insert the new value before deleting the old one,
1387 ;; so that markers on either side of the value automatically
1388 ;; stay on the same side. -- rms.
1390 (goto-char (widget-get widget :from))
1391 (widget-apply widget :delete)
1392 (widget-put widget :value value)
1393 (widget-apply widget :create))
1396 (goto-char (+ (widget-get widget :to) offset 1))
1397 (goto-char (min (+ from offset) (1- (widget-get widget :to))))))))
1399 (defun widget-default-value-inline (widget)
1400 "Wrap value in a list unless it is inline."
1401 (if (widget-get widget :inline)
1402 (widget-value widget)
1403 (list (widget-value widget))))
1405 (defun widget-default-default-get (widget)
1407 (widget-get widget :value))
1409 (defun widget-default-menu-tag-get (widget)
1410 "Use tag or value for menus."
1411 (or (widget-get widget :menu-tag)
1412 (widget-get widget :tag)
1413 (widget-princ-to-string (widget-get widget :value))))
1415 (defun widget-default-active (widget)
1416 "Return t iff this widget active (user modifiable)."
1417 (or (widget-get widget :always-active)
1418 (and (not (widget-get widget :inactive))
1419 (let ((parent (widget-get widget :parent)))
1421 (widget-apply parent :active))))))
1423 (defun widget-default-deactivate (widget)
1424 "Make WIDGET inactive for user modifications."
1425 (widget-specify-inactive widget
1426 (widget-get widget :from)
1427 (widget-get widget :to)))
1429 (defun widget-default-action (widget &optional event)
1430 "Notify the parent when a widget changes."
1431 (let ((parent (widget-get widget :parent)))
1433 (widget-apply parent :notify widget event))))
1435 (defun widget-default-notify (widget child &optional event)
1436 "Pass notification to parent."
1437 (widget-default-action widget event))
1439 (defun widget-default-prompt-value (widget prompt value unbound)
1440 "Read an arbitrary value. Stolen from `set-variable'."
1441 ;; (let ((initial (if unbound
1443 ;; It would be nice if we could do a `(cons val 1)' here.
1444 ;; (prin1-to-string (custom-quote value))))))
1445 (eval-minibuffer prompt ))
1447 ;;; The `item' Widget.
1449 (define-widget 'item 'default
1450 "Constant items for inclusion in other widgets."
1451 :convert-widget 'widget-value-convert-widget
1452 :value-create 'widget-item-value-create
1453 :value-delete 'ignore
1454 :value-get 'widget-value-value-get
1455 :match 'widget-item-match
1456 :match-inline 'widget-item-match-inline
1457 :action 'widget-item-action
1460 (defun widget-item-value-create (widget)
1461 "Insert the printed representation of the value."
1462 (princ (widget-get widget :value) (current-buffer)))
1464 (defun widget-item-match (widget value)
1465 ;; Match if the value is the same.
1466 (equal (widget-get widget :value) value))
1468 (defun widget-item-match-inline (widget values)
1469 ;; Match if the value is the same.
1470 (let ((value (widget-get widget :value)))
1472 (<= (length value) (length values))
1473 (let ((head (widget-sublist values 0 (length value))))
1474 (and (equal head value)
1475 (cons head (widget-sublist values (length value))))))))
1477 (defun widget-sublist (list start &optional end)
1478 "Return the sublist of LIST from START to END.
1479 If END is omitted, it defaults to the length of LIST."
1480 (if (> start 0) (setq list (nthcdr start list)))
1482 (unless (<= end start)
1483 (setq list (copy-sequence list))
1484 (setcdr (nthcdr (- end start 1) list) nil)
1486 (copy-sequence list)))
1488 (defun widget-item-action (widget &optional event)
1489 ;; Just notify itself.
1490 (widget-apply widget :notify widget event))
1492 ;;; The `push-button' Widget.
1494 (defcustom widget-push-button-gui t
1495 "If non nil, use GUI push buttons when available."
1499 ;; Cache already created GUI objects.
1500 (defvar widget-push-button-cache nil)
1502 (defcustom widget-push-button-prefix "["
1503 "String used as prefix for buttons."
1505 :group 'widget-button)
1507 (defcustom widget-push-button-suffix "]"
1508 "String used as suffix for buttons."
1510 :group 'widget-button)
1512 (define-widget 'push-button 'item
1513 "A pushable button."
1516 :value-create 'widget-push-button-value-create
1519 (defun widget-push-button-value-create (widget)
1520 "Insert text representing the `on' and `off' states."
1521 (let* ((tag (or (widget-get widget :tag)
1522 (widget-get widget :value)))
1523 (tag-glyph (widget-get widget :tag-glyph))
1524 (text (concat widget-push-button-prefix
1525 tag widget-push-button-suffix))
1526 (gui (cdr (assoc tag widget-push-button-cache))))
1528 (widget-image-insert widget text tag-glyph))
1532 (defun widget-gui-action (widget)
1533 "Apply :action for WIDGET."
1534 (widget-apply-action widget (this-command-keys)))
1536 ;;; The `link' Widget.
1538 (defcustom widget-link-prefix "["
1539 "String used as prefix for links."
1541 :group 'widget-button)
1543 (defcustom widget-link-suffix "]"
1544 "String used as suffix for links."
1546 :group 'widget-button)
1548 (define-widget 'link 'item
1550 :button-prefix 'widget-link-prefix
1551 :button-suffix 'widget-link-suffix
1552 :help-echo "Follow the link."
1555 ;;; The `info-link' Widget.
1557 (define-widget 'info-link 'link
1558 "A link to an info file."
1559 :action 'widget-info-link-action)
1561 (defun widget-info-link-action (widget &optional event)
1562 "Open the info node specified by WIDGET."
1563 (Info-goto-node (widget-value widget)))
1565 ;;; The `url-link' Widget.
1567 (define-widget 'url-link 'link
1568 "A link to an www page."
1569 :action 'widget-url-link-action)
1571 (defun widget-url-link-action (widget &optional event)
1572 "Open the url specified by WIDGET."
1573 (browse-url (widget-value widget)))
1575 ;;; The `function-link' Widget.
1577 (define-widget 'function-link 'link
1578 "A link to an Emacs function."
1579 :action 'widget-function-link-action)
1581 (defun widget-function-link-action (widget &optional event)
1582 "Show the function specified by WIDGET."
1583 (describe-function (widget-value widget)))
1585 ;;; The `variable-link' Widget.
1587 (define-widget 'variable-link 'link
1588 "A link to an Emacs variable."
1589 :action 'widget-variable-link-action)
1591 (defun widget-variable-link-action (widget &optional event)
1592 "Show the variable specified by WIDGET."
1593 (describe-variable (widget-value widget)))
1595 ;;; The `file-link' Widget.
1597 (define-widget 'file-link 'link
1599 :action 'widget-file-link-action)
1601 (defun widget-file-link-action (widget &optional event)
1602 "Find the file specified by WIDGET."
1603 (find-file (widget-value widget)))
1605 ;;; The `emacs-library-link' Widget.
1607 (define-widget 'emacs-library-link 'link
1608 "A link to an Emacs Lisp library file."
1609 :action 'widget-emacs-library-link-action)
1611 (defun widget-emacs-library-link-action (widget &optional event)
1612 "Find the Emacs Library file specified by WIDGET."
1613 (find-file (locate-library (widget-value widget))))
1615 ;;; The `emacs-commentary-link' Widget.
1617 (define-widget 'emacs-commentary-link 'link
1618 "A link to Commentary in an Emacs Lisp library file."
1619 :action 'widget-emacs-commentary-link-action)
1621 (defun widget-emacs-commentary-link-action (widget &optional event)
1622 "Find the Commentary section of the Emacs file specified by WIDGET."
1623 (finder-commentary (widget-value widget)))
1625 ;;; The `editable-field' Widget.
1627 (define-widget 'editable-field 'default
1628 "An editable text field."
1629 :convert-widget 'widget-value-convert-widget
1630 :keymap widget-field-keymap
1633 :prompt-internal 'widget-field-prompt-internal
1634 :prompt-history 'widget-field-history
1635 :prompt-value 'widget-field-prompt-value
1636 :action 'widget-field-action
1637 :validate 'widget-field-validate
1640 :value-create 'widget-field-value-create
1641 :value-delete 'widget-field-value-delete
1642 :value-get 'widget-field-value-get
1643 :match 'widget-field-match)
1645 (defvar widget-field-history nil
1646 "History of field minibuffer edits.")
1648 (defun widget-field-prompt-internal (widget prompt initial history)
1649 "Read string for WIDGET promptinhg with PROMPT.
1650 INITIAL is the initial input and HISTORY is a symbol containing
1652 (read-string prompt initial history))
1654 (defun widget-field-prompt-value (widget prompt value unbound)
1655 "Prompt for a string."
1656 (let ((initial (if unbound
1658 (cons (widget-apply widget :value-to-internal
1660 (history (widget-get widget :prompt-history)))
1661 (let ((answer (widget-apply widget
1662 :prompt-internal prompt initial history)))
1663 (widget-apply widget :value-to-external answer))))
1665 (defvar widget-edit-functions nil)
1667 (defun widget-field-action (widget &optional event)
1668 "Move to next field."
1670 (run-hook-with-args 'widget-edit-functions widget))
1672 (defun widget-field-validate (widget)
1673 "Valid if the content matches `:valid-regexp'."
1675 (let ((value (widget-apply widget :value-get))
1676 (regexp (widget-get widget :valid-regexp)))
1677 (if (string-match regexp value)
1681 (defun widget-field-value-create (widget)
1682 "Create an editable text field."
1683 (let ((size (widget-get widget :size))
1684 (value (widget-get widget :value))
1686 ;; This is changed to a real overlay in `widget-setup'. We
1687 ;; need the end points to behave differently until
1688 ;; `widget-setup' is called.
1689 (overlay (cons (make-marker) (make-marker))))
1690 (widget-put widget :field-overlay overlay)
1693 (< (length value) size)
1694 (insert-char ?\ (- size (length value))))
1695 (unless (memq widget widget-field-list)
1696 (setq widget-field-new (cons widget widget-field-new)))
1697 (move-marker (cdr overlay) (point))
1698 (set-marker-insertion-type (cdr overlay) nil)
1701 (move-marker (car overlay) from)
1702 (set-marker-insertion-type (car overlay) t)))
1704 (defun widget-field-value-delete (widget)
1705 "Remove the widget from the list of active editing fields."
1706 (setq widget-field-list (delq widget widget-field-list))
1707 ;; These are nil if the :format string doesn't contain `%v'.
1708 (let ((overlay (widget-get widget :field-overlay)))
1710 (delete-overlay overlay))))
1712 (defun widget-field-value-get (widget)
1713 "Return current text in editing field."
1714 (let ((from (widget-field-start widget))
1715 (to (widget-field-end widget))
1716 (buffer (widget-field-buffer widget))
1717 (size (widget-get widget :size))
1718 (secret (widget-get widget :secret))
1719 (old (current-buffer)))
1726 (eq (char-after (1- to)) ?\ ))
1728 (let ((result (buffer-substring-no-properties from to)))
1731 (while (< (+ from index) to)
1733 (get-char-property (+ from index) 'secret))
1734 (setq index (1+ index)))))
1737 (widget-get widget :value))))
1739 (defun widget-field-match (widget value)
1740 ;; Match any string.
1743 ;;; The `text' Widget.
1745 (define-widget 'text 'editable-field
1746 :keymap widget-text-keymap
1747 "A multiline text area.")
1749 ;;; The `menu-choice' Widget.
1751 (define-widget 'menu-choice 'default
1752 "A menu of options."
1753 :convert-widget 'widget-types-convert-widget
1754 :format "%[%t%]: %v"
1757 :void '(item :format "invalid (%t)\n")
1758 :value-create 'widget-choice-value-create
1759 :value-delete 'widget-children-value-delete
1760 :value-get 'widget-choice-value-get
1761 :value-inline 'widget-choice-value-inline
1762 :default-get 'widget-choice-default-get
1763 :mouse-down-action 'widget-choice-mouse-down-action
1764 :action 'widget-choice-action
1765 :error "Make a choice"
1766 :validate 'widget-choice-validate
1767 :match 'widget-choice-match
1768 :match-inline 'widget-choice-match-inline)
1770 (defun widget-choice-value-create (widget)
1771 "Insert the first choice that matches the value."
1772 (let ((value (widget-get widget :value))
1773 (args (widget-get widget :args))
1774 (explicit (widget-get widget :explicit-choice))
1775 (explicit-value (widget-get widget :explicit-choice-value))
1777 (if (and explicit (equal value explicit-value))
1779 ;; If the user specified the choice for this value,
1780 ;; respect that choice as long as the value is the same.
1781 (widget-put widget :children (list (widget-create-child-value
1782 widget explicit value)))
1783 (widget-put widget :choice explicit))
1785 (setq current (car args)
1787 (when (widget-apply current :match value)
1788 (widget-put widget :children (list (widget-create-child-value
1789 widget current value)))
1790 (widget-put widget :choice current)
1794 (let ((void (widget-get widget :void)))
1795 (widget-put widget :children (list (widget-create-child-and-convert
1796 widget void :value value)))
1797 (widget-put widget :choice void))))))
1799 (defun widget-choice-value-get (widget)
1800 ;; Get value of the child widget.
1801 (widget-value (car (widget-get widget :children))))
1803 (defun widget-choice-value-inline (widget)
1804 ;; Get value of the child widget.
1805 (widget-apply (car (widget-get widget :children)) :value-inline))
1807 (defun widget-choice-default-get (widget)
1808 ;; Get default for the first choice.
1809 (widget-default-get (car (widget-get widget :args))))
1811 (defcustom widget-choice-toggle nil
1812 "If non-nil, a binary choice will just toggle between the values.
1813 Otherwise, the user will explicitly have to choose between the values
1814 when he invoked the menu."
1818 (defun widget-choice-mouse-down-action (widget &optional event)
1819 ;; Return non-nil if we need a menu.
1820 (let ((args (widget-get widget :args))
1821 (old (widget-get widget :choice)))
1822 (cond ((not window-system)
1823 ;; No place to pop up a menu.
1825 ((not (or (fboundp 'x-popup-menu) (fboundp 'popup-menu)))
1826 ;; No way to pop up a menu.
1828 ((< (length args) 2)
1829 ;; Empty or singleton list, just return the value.
1831 ((> (length args) widget-menu-max-size)
1832 ;; Too long, prompt.
1834 ((> (length args) 2)
1835 ;; Reasonable sized list, use menu.
1837 ((and widget-choice-toggle (memq old args))
1841 ;; Ask which of the two.
1844 (defun widget-choice-action (widget &optional event)
1846 (let ((args (widget-get widget :args))
1847 (old (widget-get widget :choice))
1848 (tag (widget-apply widget :menu-tag-get))
1849 (completion-ignore-case (widget-get widget :case-fold))
1852 ;; Remember old value.
1853 (if (and old (not (widget-apply widget :validate)))
1854 (let* ((external (widget-value widget))
1855 (internal (widget-apply old :value-to-internal external)))
1856 (widget-put old :value internal)))
1859 (cond ((= (length args) 0)
1861 ((= (length args) 1)
1863 ((and widget-choice-toggle
1866 (if (eq old (nth 0 args))
1871 (setq current (car args)
1874 (cons (cons (widget-apply current :menu-tag-get)
1877 (setq this-explicit t)
1878 (widget-choose tag (reverse choices) event))))
1880 ;; If this was an explicit user choice,
1881 ;; record the choice, and the record the value it was made for.
1882 ;; widget-choice-value-create will respect this choice,
1883 ;; as long as the value is the same.
1885 (widget-put widget :explicit-choice current)
1886 (widget-put widget :explicit-choice-value (widget-get widget :value)))
1887 (let ((value (widget-default-get current)))
1888 (widget-value-set widget
1889 (widget-apply current :value-to-external value)))
1891 (widget-apply widget :notify widget event)))
1892 (run-hook-with-args 'widget-edit-functions widget))
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-image-insert widget
1938 (widget-get widget :on)
1939 (widget-get widget :on-glyph))
1940 (widget-image-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)
1948 (run-hook-with-args 'widget-edit-functions widget))
1950 ;;; The `checkbox' Widget.
1952 (define-widget 'checkbox 'toggle
1953 "A checkbox toggle."
1958 ;; We could probably do the same job as the images using single
1959 ;; space characters in a boxed face with a stretch specification to
1960 ;; make them square.
1961 :on-glyph (create-image (make-bool-vector 49 1)
1962 'xbm t :width 7 :height 7
1963 :foreground "grey75" ; like default mode line
1964 :relief -3 :ascent 'center)
1966 :off-glyph (create-image (make-bool-vector 49 1)
1967 'xbm t :width 7 :height 7
1968 :foreground "grey75"
1969 :relief 3 :ascent 'center)
1970 :help-echo "Toggle this item."
1971 :action 'widget-checkbox-action)
1973 (defun widget-checkbox-action (widget &optional event)
1974 "Toggle checkbox, notify parent, and set active state of sibling."
1975 (widget-toggle-action widget event)
1976 (let ((sibling (widget-get-sibling widget)))
1978 (if (widget-value widget)
1979 (widget-apply sibling :activate)
1980 (widget-apply sibling :deactivate)))))
1982 ;;; The `checklist' Widget.
1984 (define-widget 'checklist 'default
1985 "A multiple choice widget."
1986 :convert-widget 'widget-types-convert-widget
1989 :entry-format "%b %v"
1990 :menu-tag "checklist"
1992 :value-create 'widget-checklist-value-create
1993 :value-delete 'widget-children-value-delete
1994 :value-get 'widget-checklist-value-get
1995 :validate 'widget-checklist-validate
1996 :match 'widget-checklist-match
1997 :match-inline 'widget-checklist-match-inline)
1999 (defun widget-checklist-value-create (widget)
2000 ;; Insert all values
2001 (let ((alist (widget-checklist-match-find widget (widget-get widget :value)))
2002 (args (widget-get widget :args)))
2004 (widget-checklist-add-item widget (car args) (assq (car args) alist))
2005 (setq args (cdr args)))
2006 (widget-put widget :children (nreverse (widget-get widget :children)))))
2008 (defun widget-checklist-add-item (widget type chosen)
2009 "Create checklist item in WIDGET of type TYPE.
2010 If the item is checked, CHOSEN is a cons whose cdr is the value."
2011 (and (eq (preceding-char) ?\n)
2012 (widget-get widget :indent)
2013 (insert-char ? (widget-get widget :indent)))
2014 (widget-specify-insert
2015 (let* ((children (widget-get widget :children))
2016 (buttons (widget-get widget :buttons))
2017 (button-args (or (widget-get type :sibling-args)
2018 (widget-get widget :button-args)))
2021 (insert (widget-get widget :entry-format))
2023 ;; Parse % escapes in format.
2024 (while (re-search-forward "%\\([bv%]\\)" nil t)
2025 (let ((escape (aref (match-string 1) 0)))
2026 (replace-match "" t t)
2027 (cond ((eq escape ?%)
2030 (setq button (apply 'widget-create-child-and-convert
2032 :value (not (null chosen))
2037 (let ((child (widget-create-child widget type)))
2038 (widget-apply child :deactivate)
2040 ((widget-get type :inline)
2041 (widget-create-child-value
2042 widget type (cdr chosen)))
2044 (widget-create-child-value
2045 widget type (car (cdr chosen)))))))
2047 (error "Unknown escape `%c'" escape)))))
2048 ;; Update properties.
2049 (and button child (widget-put child :button button))
2050 (and button (widget-put widget :buttons (cons button buttons)))
2051 (and child (widget-put widget :children (cons child children))))))
2053 (defun widget-checklist-match (widget values)
2054 ;; All values must match a type in the checklist.
2056 (null (cdr (widget-checklist-match-inline widget values)))))
2058 (defun widget-checklist-match-inline (widget values)
2059 ;; Find the values which match a type in the checklist.
2060 (let ((greedy (widget-get widget :greedy))
2061 (args (copy-sequence (widget-get widget :args)))
2064 (let ((answer (widget-checklist-match-up args values)))
2066 (let ((vals (widget-match-inline answer values)))
2067 (setq found (append found (car vals))
2069 args (delq answer args))))
2071 (setq rest (append rest (list (car values)))
2072 values (cdr values)))
2074 (setq rest (append rest values)
2078 (defun widget-checklist-match-find (widget vals)
2079 "Find the vals which match a type in the checklist.
2080 Return an alist of (TYPE MATCH)."
2081 (let ((greedy (widget-get widget :greedy))
2082 (args (copy-sequence (widget-get widget :args)))
2085 (let ((answer (widget-checklist-match-up args vals)))
2087 (let ((match (widget-match-inline answer vals)))
2088 (setq found (cons (cons answer (car match)) found)
2090 args (delq answer args))))
2092 (setq vals (cdr vals)))
2097 (defun widget-checklist-match-up (args vals)
2098 "Return the first type from ARGS that matches VALS."
2099 (let (current found)
2100 (while (and args (null found))
2101 (setq current (car args)
2103 found (widget-match-inline current vals)))
2107 (defun widget-checklist-value-get (widget)
2108 ;; The values of all selected items.
2109 (let ((children (widget-get widget :children))
2112 (setq child (car children)
2113 children (cdr children))
2114 (if (widget-value (widget-get child :button))
2115 (setq result (append result (widget-apply child :value-inline)))))
2118 (defun widget-checklist-validate (widget)
2119 ;; Ticked chilren must be valid.
2120 (let ((children (widget-get widget :children))
2122 (while (and children (not found))
2123 (setq child (car children)
2124 children (cdr children)
2125 button (widget-get child :button)
2126 found (and (widget-value button)
2127 (widget-apply child :validate))))
2130 ;;; The `option' Widget
2132 (define-widget 'option 'checklist
2133 "An widget with an optional item."
2136 ;;; The `choice-item' Widget.
2138 (define-widget 'choice-item 'item
2139 "Button items that delegate action events to their parents."
2140 :action 'widget-parent-action
2141 :format "%[%t%] \n")
2143 ;;; The `radio-button' Widget.
2145 (define-widget 'radio-button 'toggle
2146 "A radio button for use in the `radio' widget."
2147 :notify 'widget-radio-button-notify
2154 :off-glyph "radio0")
2156 (defun widget-radio-button-notify (widget child &optional event)
2158 (widget-apply (widget-get widget :parent) :action widget event))
2160 ;;; The `radio-button-choice' Widget.
2162 (define-widget 'radio-button-choice 'default
2163 "Select one of multiple options."
2164 :convert-widget 'widget-types-convert-widget
2167 :entry-format "%b %v"
2169 :value-create 'widget-radio-value-create
2170 :value-delete 'widget-children-value-delete
2171 :value-get 'widget-radio-value-get
2172 :value-inline 'widget-radio-value-inline
2173 :value-set 'widget-radio-value-set
2174 :error "You must push one of the buttons"
2175 :validate 'widget-radio-validate
2176 :match 'widget-choice-match
2177 :match-inline 'widget-choice-match-inline
2178 :action 'widget-radio-action)
2180 (defun widget-radio-value-create (widget)
2181 ;; Insert all values
2182 (let ((args (widget-get widget :args))
2185 (setq arg (car args)
2187 (widget-radio-add-item widget arg))))
2189 (defun widget-radio-add-item (widget type)
2190 "Add to radio widget WIDGET a new radio button item of type TYPE."
2191 ;; (setq type (widget-convert type))
2192 (and (eq (preceding-char) ?\n)
2193 (widget-get widget :indent)
2194 (insert-char ? (widget-get widget :indent)))
2195 (widget-specify-insert
2196 (let* ((value (widget-get widget :value))
2197 (children (widget-get widget :children))
2198 (buttons (widget-get widget :buttons))
2199 (button-args (or (widget-get type :sibling-args)
2200 (widget-get widget :button-args)))
2202 (chosen (and (null (widget-get widget :choice))
2203 (widget-apply type :match value)))
2205 (insert (widget-get widget :entry-format))
2207 ;; Parse % escapes in format.
2208 (while (re-search-forward "%\\([bv%]\\)" nil t)
2209 (let ((escape (aref (match-string 1) 0)))
2210 (replace-match "" t t)
2211 (cond ((eq escape ?%)
2214 (setq button (apply 'widget-create-child-and-convert
2215 widget 'radio-button
2216 :value (not (null chosen))
2219 (setq child (if chosen
2220 (widget-create-child-value
2222 (widget-create-child widget type)))
2224 (widget-apply child :deactivate)))
2226 (error "Unknown escape `%c'" escape)))))
2227 ;; Update properties.
2229 (widget-put widget :choice type))
2231 (widget-put child :button button)
2232 (widget-put widget :buttons (nconc buttons (list button))))
2234 (widget-put widget :children (nconc children (list child))))
2237 (defun widget-radio-value-get (widget)
2238 ;; Get value of the child widget.
2239 (let ((chosen (widget-radio-chosen widget)))
2240 (and chosen (widget-value chosen))))
2242 (defun widget-radio-chosen (widget)
2243 "Return the widget representing the chosen radio button."
2244 (let ((children (widget-get widget :children))
2247 (setq current (car children)
2248 children (cdr children))
2249 (let* ((button (widget-get current :button))
2250 (value (widget-apply button :value-get)))
2256 (defun widget-radio-value-inline (widget)
2257 ;; Get value of the child widget.
2258 (let ((children (widget-get widget :children))
2261 (setq current (car children)
2262 children (cdr children))
2263 (let* ((button (widget-get current :button))
2264 (value (widget-apply button :value-get)))
2266 (setq found (widget-apply current :value-inline)
2270 (defun widget-radio-value-set (widget value)
2271 ;; We can't just delete and recreate a radio widget, since children
2272 ;; can be added after the original creation and won't be recreated
2274 (let ((children (widget-get widget :children))
2277 (setq current (car children)
2278 children (cdr children))
2279 (let* ((button (widget-get current :button))
2280 (match (and (not found)
2281 (widget-apply current :match value))))
2282 (widget-value-set button match)
2285 (widget-value-set current value)
2286 (widget-apply current :activate))
2287 (widget-apply current :deactivate))
2288 (setq found (or found match))))))
2290 (defun widget-radio-validate (widget)
2291 ;; Valid if we have made a valid choice.
2292 (let ((children (widget-get widget :children))
2293 current found button)
2294 (while (and children (not found))
2295 (setq current (car children)
2296 children (cdr children)
2297 button (widget-get current :button)
2298 found (widget-apply button :value-get)))
2300 (widget-apply current :validate)
2303 (defun widget-radio-action (widget child event)
2304 ;; Check if a radio button was pressed.
2305 (let ((children (widget-get widget :children))
2306 (buttons (widget-get widget :buttons))
2308 (when (memq child buttons)
2310 (setq current (car children)
2311 children (cdr children))
2312 (let* ((button (widget-get current :button)))
2313 (cond ((eq child button)
2314 (widget-value-set button t)
2315 (widget-apply current :activate))
2316 ((widget-value button)
2317 (widget-value-set button nil)
2318 (widget-apply current :deactivate)))))))
2319 ;; Pass notification to parent.
2320 (widget-apply widget :notify child event))
2322 ;;; The `insert-button' Widget.
2324 (define-widget 'insert-button 'push-button
2325 "An insert button for the `editable-list' widget."
2327 :help-echo "Insert a new item into the list at this position."
2328 :action 'widget-insert-button-action)
2330 (defun widget-insert-button-action (widget &optional event)
2331 ;; Ask the parent to insert a new item.
2332 (widget-apply (widget-get widget :parent)
2333 :insert-before (widget-get widget :widget)))
2335 ;;; The `delete-button' Widget.
2337 (define-widget 'delete-button 'push-button
2338 "A delete button for the `editable-list' widget."
2340 :help-echo "Delete this item from the list."
2341 :action 'widget-delete-button-action)
2343 (defun widget-delete-button-action (widget &optional event)
2344 ;; Ask the parent to insert a new item.
2345 (widget-apply (widget-get widget :parent)
2346 :delete-at (widget-get widget :widget)))
2348 ;;; The `editable-list' Widget.
2350 (defcustom widget-editable-list-gui nil
2351 "If non nil, use GUI push-buttons in editable list when available."
2355 (define-widget 'editable-list 'default
2356 "A variable list of widgets of the same type."
2357 :convert-widget 'widget-types-convert-widget
2360 :format-handler 'widget-editable-list-format-handler
2361 :entry-format "%i %d %v"
2362 :menu-tag "editable-list"
2363 :value-create 'widget-editable-list-value-create
2364 :value-delete 'widget-children-value-delete
2365 :value-get 'widget-editable-list-value-get
2366 :validate 'widget-children-validate
2367 :match 'widget-editable-list-match
2368 :match-inline 'widget-editable-list-match-inline
2369 :insert-before 'widget-editable-list-insert-before
2370 :delete-at 'widget-editable-list-delete-at)
2372 (defun widget-editable-list-format-handler (widget escape)
2373 ;; We recognize the insert button.
2374 (let ((widget-push-button-gui widget-editable-list-gui))
2375 (cond ((eq escape ?i)
2376 (and (widget-get widget :indent)
2377 (insert-char ? (widget-get widget :indent)))
2378 (apply 'widget-create-child-and-convert
2379 widget 'insert-button
2380 (widget-get widget :append-button-args)))
2382 (widget-default-format-handler widget escape)))))
2384 (defun widget-editable-list-value-create (widget)
2385 ;; Insert all values
2386 (let* ((value (widget-get widget :value))
2387 (type (nth 0 (widget-get widget :args)))
2388 (inlinep (widget-get type :inline))
2390 (widget-put widget :value-pos (copy-marker (point)))
2391 (set-marker-insertion-type (widget-get widget :value-pos) t)
2393 (let ((answer (widget-match-inline type value)))
2395 (setq children (cons (widget-editable-list-entry-create
2404 (widget-put widget :children (nreverse children))))
2406 (defun widget-editable-list-value-get (widget)
2407 ;; Get value of the child widget.
2408 (apply 'append (mapcar (lambda (child) (widget-apply child :value-inline))
2409 (widget-get widget :children))))
2411 (defun widget-editable-list-match (widget value)
2412 ;; Value must be a list and all the members must match the type.
2414 (null (cdr (widget-editable-list-match-inline widget value)))))
2416 (defun widget-editable-list-match-inline (widget value)
2417 (let ((type (nth 0 (widget-get widget :args)))
2420 (while (and value ok)
2421 (let ((answer (widget-match-inline type value)))
2423 (setq found (append found (car answer))
2426 (cons found value)))
2428 (defun widget-editable-list-insert-before (widget before)
2429 ;; Insert a new child in the list of children.
2431 (let ((children (widget-get widget :children))
2432 (inhibit-read-only t)
2433 before-change-functions
2434 after-change-functions)
2436 (goto-char (widget-get before :entry-from)))
2438 (goto-char (widget-get widget :value-pos))))
2439 (let ((child (widget-editable-list-entry-create
2441 (when (< (widget-get child :entry-from) (widget-get widget :from))
2442 (set-marker (widget-get widget :from)
2443 (widget-get child :entry-from)))
2444 (if (eq (car children) before)
2445 (widget-put widget :children (cons child children))
2446 (while (not (eq (car (cdr children)) before))
2447 (setq children (cdr children)))
2448 (setcdr children (cons child (cdr children)))))))
2450 (widget-apply widget :notify widget))
2452 (defun widget-editable-list-delete-at (widget child)
2453 ;; Delete child from list of children.
2455 (let ((buttons (copy-sequence (widget-get widget :buttons)))
2457 (inhibit-read-only t)
2458 before-change-functions
2459 after-change-functions)
2461 (setq button (car buttons)
2462 buttons (cdr buttons))
2463 (when (eq (widget-get button :widget) child)
2465 :buttons (delq button (widget-get widget :buttons)))
2466 (widget-delete button))))
2467 (let ((entry-from (widget-get child :entry-from))
2468 (entry-to (widget-get child :entry-to))
2469 (inhibit-read-only t)
2470 before-change-functions
2471 after-change-functions)
2472 (widget-delete child)
2473 (delete-region entry-from entry-to)
2474 (set-marker entry-from nil)
2475 (set-marker entry-to nil))
2476 (widget-put widget :children (delq child (widget-get widget :children))))
2478 (widget-apply widget :notify widget))
2480 (defun widget-editable-list-entry-create (widget value conv)
2481 ;; Create a new entry to the list.
2482 (let ((type (nth 0 (widget-get widget :args)))
2483 (widget-push-button-gui widget-editable-list-gui)
2484 child delete insert)
2485 (widget-specify-insert
2487 (and (widget-get widget :indent)
2488 (insert-char ? (widget-get widget :indent)))
2489 (insert (widget-get widget :entry-format)))
2490 ;; Parse % escapes in format.
2491 (while (re-search-forward "%\\(.\\)" nil t)
2492 (let ((escape (aref (match-string 1) 0)))
2493 (replace-match "" t t)
2494 (cond ((eq escape ?%)
2497 (setq insert (apply 'widget-create-child-and-convert
2498 widget 'insert-button
2499 (widget-get widget :insert-button-args))))
2501 (setq delete (apply 'widget-create-child-and-convert
2502 widget 'delete-button
2503 (widget-get widget :delete-button-args))))
2506 (setq child (widget-create-child-value
2508 (setq child (widget-create-child-value
2510 (widget-apply type :value-to-external
2511 (widget-default-get type))))))
2513 (error "Unknown escape `%c'" escape)))))
2515 :buttons (cons delete
2517 (widget-get widget :buttons))))
2518 (let ((entry-from (copy-marker (point-min)))
2519 (entry-to (copy-marker (point-max))))
2520 (set-marker-insertion-type entry-from t)
2521 (set-marker-insertion-type entry-to nil)
2522 (widget-put child :entry-from entry-from)
2523 (widget-put child :entry-to entry-to)))
2524 (widget-put insert :widget child)
2525 (widget-put delete :widget child)
2528 ;;; The `group' Widget.
2530 (define-widget 'group 'default
2531 "A widget which groups other widgets inside."
2532 :convert-widget 'widget-types-convert-widget
2534 :value-create 'widget-group-value-create
2535 :value-delete 'widget-children-value-delete
2536 :value-get 'widget-editable-list-value-get
2537 :default-get 'widget-group-default-get
2538 :validate 'widget-children-validate
2539 :match 'widget-group-match
2540 :match-inline 'widget-group-match-inline)
2542 (defun widget-group-value-create (widget)
2543 ;; Create each component.
2544 (let ((args (widget-get widget :args))
2545 (value (widget-get widget :value))
2546 arg answer children)
2548 (setq arg (car args)
2550 answer (widget-match-inline arg value)
2552 (and (eq (preceding-char) ?\n)
2553 (widget-get widget :indent)
2554 (insert-char ? (widget-get widget :indent)))
2555 (push (cond ((null answer)
2556 (widget-create-child widget arg))
2557 ((widget-get arg :inline)
2558 (widget-create-child-value widget arg (car answer)))
2560 (widget-create-child-value widget arg (car (car answer)))))
2562 (widget-put widget :children (nreverse children))))
2564 (defun widget-group-default-get (widget)
2565 ;; Get the default of the components.
2566 (mapcar 'widget-default-get (widget-get widget :args)))
2568 (defun widget-group-match (widget values)
2569 ;; Match if the components match.
2571 (let ((match (widget-group-match-inline widget values)))
2572 (and match (null (cdr match))))))
2574 (defun widget-group-match-inline (widget vals)
2575 ;; Match if the components match.
2576 (let ((args (widget-get widget :args))
2577 argument answer found)
2579 (setq argument (car args)
2581 answer (widget-match-inline argument vals))
2583 (setq vals (cdr answer)
2584 found (append found (car answer)))
2588 (cons found vals))))
2590 ;;; The `visibility' Widget.
2592 (define-widget 'visibility 'item
2593 "An indicator and manipulator for hidden items."
2599 :value-create 'widget-visibility-value-create
2600 :action 'widget-toggle-action
2601 :match (lambda (widget value) t))
2603 (defun widget-visibility-value-create (widget)
2604 ;; Insert text representing the `on' and `off' states.
2605 (let ((on (widget-get widget :on))
2606 (off (widget-get widget :off)))
2608 (setq on (concat widget-push-button-prefix
2610 widget-push-button-suffix))
2613 (setq off (concat widget-push-button-prefix
2615 widget-push-button-suffix))
2617 (if (widget-value widget)
2618 (widget-image-insert widget on "down" "down-pushed")
2619 (widget-image-insert widget off "right" "right-pushed"))))
2621 ;;; The `documentation-link' Widget.
2623 ;; This is a helper widget for `documentation-string'.
2625 (define-widget 'documentation-link 'link
2626 "Link type used in documentation strings."
2628 :help-echo "Describe this symbol"
2629 :action 'widget-documentation-link-action)
2631 (defun widget-documentation-link-action (widget &optional event)
2632 "Display documentation for WIDGET's value. Ignore optional argument EVENT."
2633 (let* ((string (widget-get widget :value))
2634 (symbol (intern string)))
2635 (if (and (fboundp symbol) (boundp symbol))
2636 ;; If there are two doc strings, give the user a way to pick one.
2637 (apropos (concat "\\`" (regexp-quote string) "\\'"))
2638 (if (fboundp symbol)
2639 (describe-function symbol)
2640 (describe-variable symbol)))))
2642 (defcustom widget-documentation-links t
2643 "Add hyperlinks to documentation strings when non-nil."
2645 :group 'widget-documentation)
2647 (defcustom widget-documentation-link-regexp "`\\([^\n`' ]+\\)'"
2648 "Regexp for matching potential links in documentation strings.
2649 The first group should be the link itself."
2651 :group 'widget-documentation)
2653 (defcustom widget-documentation-link-p 'intern-soft
2654 "Predicate used to test if a string is useful as a link.
2655 The value should be a function. The function will be called one
2656 argument, a string, and should return non-nil if there should be a
2657 link for that string."
2659 :options '(widget-documentation-link-p)
2660 :group 'widget-documentation)
2662 (defcustom widget-documentation-link-type 'documentation-link
2663 "Widget type used for links in documentation strings."
2665 :group 'widget-documentation)
2667 (defun widget-documentation-link-add (widget from to)
2668 (widget-specify-doc widget from to)
2669 (when widget-documentation-links
2670 (let ((regexp widget-documentation-link-regexp)
2671 (predicate widget-documentation-link-p)
2672 (type widget-documentation-link-type)
2673 (buttons (widget-get widget :buttons))
2674 (widget-mouse-face (default-value 'widget-mouse-face))
2675 (widget-button-face widget-documentation-face)
2676 (widget-button-pressed-face widget-documentation-face))
2679 (while (re-search-forward regexp to t)
2680 (let ((name (match-string 1))
2681 (begin (match-beginning 1))
2682 (end (match-end 1)))
2683 (when (funcall predicate name)
2684 (push (widget-convert-button type begin end :value name)
2686 (widget-put widget :buttons buttons)))
2687 (let ((indent (widget-get widget :indent)))
2688 (when (and indent (not (zerop indent)))
2691 (narrow-to-region from to)
2692 (goto-char (point-min))
2693 (while (search-forward "\n" nil t)
2694 (insert-char ?\ indent)))))))
2696 ;;; The `documentation-string' Widget.
2698 (define-widget 'documentation-string 'item
2699 "A documentation string."
2701 :action 'widget-documentation-string-action
2702 :value-delete 'widget-children-value-delete
2703 :value-create 'widget-documentation-string-value-create)
2705 (defun widget-documentation-string-value-create (widget)
2706 ;; Insert documentation string.
2707 (let ((doc (widget-value widget))
2708 (indent (widget-get widget :indent))
2709 (shown (widget-get (widget-get widget :parent) :documentation-shown))
2711 (if (string-match "\n" doc)
2712 (let ((before (substring doc 0 (match-beginning 0)))
2713 (after (substring doc (match-beginning 0)))
2716 (widget-documentation-link-add widget start (point))
2717 (push (widget-create-child-and-convert
2719 :help-echo "Show or hide rest of the documentation."
2722 :action 'widget-parent-action
2726 (setq start (point))
2727 (when (and indent (not (zerop indent)))
2728 (insert-char ?\ indent))
2730 (widget-documentation-link-add widget start (point)))
2731 (widget-put widget :buttons buttons))
2733 (widget-documentation-link-add widget start (point))))
2736 (defun widget-documentation-string-action (widget &rest ignore)
2737 ;; Toggle documentation.
2738 (let ((parent (widget-get widget :parent)))
2739 (widget-put parent :documentation-shown
2740 (not (widget-get parent :documentation-shown))))
2742 (widget-value-set widget (widget-value widget)))
2744 ;;; The Sexp Widgets.
2746 (define-widget 'const 'item
2747 "An immutable sexp."
2748 :prompt-value 'widget-const-prompt-value
2751 (defun widget-const-prompt-value (widget prompt value unbound)
2752 ;; Return the value of the const.
2753 (widget-value widget))
2755 (define-widget 'function-item 'const
2756 "An immutable function name."
2758 :documentation-property (lambda (symbol)
2760 (documentation symbol t)
2763 (define-widget 'variable-item 'const
2764 "An immutable variable name."
2766 :documentation-property 'variable-documentation)
2768 (define-widget 'other 'sexp
2769 "Matches any value, but doesn't let the user edit the value.
2770 This is useful as last item in a `choice' widget.
2771 You should use this widget type with a default value,
2772 as in (other DEFAULT) or (other :tag \"NAME\" DEFAULT).
2773 If the user selects this alternative, that specifies DEFAULT
2779 (defvar widget-string-prompt-value-history nil
2780 "History of input to `widget-string-prompt-value'.")
2782 (define-widget 'string 'editable-field
2785 :format "%{%t%}: %v"
2786 :complete-function 'ispell-complete-word
2787 :prompt-history 'widget-string-prompt-value-history)
2789 (define-widget 'regexp 'string
2790 "A regular expression."
2791 :match 'widget-regexp-match
2792 :validate 'widget-regexp-validate
2793 ;; Doesn't work well with terminating newline.
2794 ;; :value-face 'widget-single-line-field-face
2797 (defun widget-regexp-match (widget value)
2798 ;; Match valid regexps.
2799 (and (stringp value)
2802 (string-match value ""))
2805 (defun widget-regexp-validate (widget)
2806 "Check that the value of WIDGET is a valid regexp."
2807 (let ((val (widget-value widget)))
2808 (condition-case data
2810 (string-match val ""))
2811 (error (widget-put widget :error (error-message-string data))
2814 (define-widget 'file 'string
2816 It will read a file name from the minibuffer when invoked."
2817 :complete-function 'widget-file-complete
2818 :prompt-value 'widget-file-prompt-value
2819 :format "%{%t%}: %v"
2820 ;; Doesn't work well with terminating newline.
2821 ;; :value-face 'widget-single-line-field-face
2824 (defun widget-file-complete ()
2825 "Perform completion on file name preceding point."
2827 (let* ((end (point))
2828 (beg (save-excursion
2829 (skip-chars-backward "^ ")
2831 (pattern (buffer-substring beg end))
2832 (name-part (file-name-nondirectory pattern))
2833 (directory (file-name-directory pattern))
2834 (completion (file-name-completion name-part directory)))
2835 (cond ((eq completion t))
2837 (message "Can't find completion for \"%s\"" pattern)
2839 ((not (string= name-part completion))
2840 (delete-region beg end)
2841 (insert (expand-file-name completion directory)))
2843 (message "Making completion list...")
2844 (let ((list (file-name-all-completions name-part directory)))
2845 (setq list (sort list 'string<))
2846 (with-output-to-temp-buffer "*Completions*"
2847 (display-completion-list list)))
2848 (message "Making completion list...%s" "done")))))
2850 (defun widget-file-prompt-value (widget prompt value unbound)
2851 ;; Read file from minibuffer.
2852 (abbreviate-file-name
2854 (read-file-name prompt)
2855 (let ((prompt2 (format "%s (default %s) " prompt value))
2856 (dir (file-name-directory value))
2857 (file (file-name-nondirectory value))
2858 (must-match (widget-get widget :must-match)))
2859 (read-file-name prompt2 dir nil must-match file)))))
2861 ;;;(defun widget-file-action (widget &optional event)
2862 ;;; ;; Read a file name from the minibuffer.
2863 ;;; (let* ((value (widget-value widget))
2864 ;;; (dir (file-name-directory value))
2865 ;;; (file (file-name-nondirectory value))
2866 ;;; (menu-tag (widget-apply widget :menu-tag-get))
2867 ;;; (must-match (widget-get widget :must-match))
2868 ;;; (answer (read-file-name (concat menu-tag ": (default `" value "') ")
2869 ;;; dir nil must-match file)))
2870 ;;; (widget-value-set widget (abbreviate-file-name answer))
2872 ;;; (widget-apply widget :notify widget event)))
2874 (define-widget 'directory 'file
2875 "A directory widget.
2876 It will read a directory name from the minibuffer when invoked."
2879 (defvar widget-symbol-prompt-value-history nil
2880 "History of input to `widget-symbol-prompt-value'.")
2882 (define-widget 'symbol 'editable-field
2886 :format "%{%t%}: %v"
2887 :match (lambda (widget value) (symbolp value))
2888 :complete-function 'lisp-complete-symbol
2889 :prompt-internal 'widget-symbol-prompt-internal
2890 :prompt-match 'symbolp
2891 :prompt-history 'widget-symbol-prompt-value-history
2892 :value-to-internal (lambda (widget value)
2896 :value-to-external (lambda (widget value)
2901 (defun widget-symbol-prompt-internal (widget prompt initial history)
2902 ;; Read file from minibuffer.
2903 (let ((answer (completing-read prompt obarray
2904 (widget-get widget :prompt-match)
2905 nil initial history)))
2906 (if (and (stringp answer)
2907 (not (zerop (length answer))))
2909 (error "No value"))))
2911 (defvar widget-function-prompt-value-history nil
2912 "History of input to `widget-function-prompt-value'.")
2914 (define-widget 'function 'sexp
2916 :complete-function 'lisp-complete-symbol
2917 :prompt-value 'widget-field-prompt-value
2918 :prompt-internal 'widget-symbol-prompt-internal
2919 :prompt-match 'fboundp
2920 :prompt-history 'widget-function-prompt-value-history
2921 :action 'widget-field-action
2924 (defvar widget-variable-prompt-value-history nil
2925 "History of input to `widget-variable-prompt-value'.")
2927 (define-widget 'variable 'symbol
2928 ;; Should complete on variables.
2930 :prompt-match 'boundp
2931 :prompt-history 'widget-variable-prompt-value-history
2934 (defvar widget-coding-system-prompt-value-history nil
2935 "History of input to `widget-coding-system-prompt-value'.")
2937 (define-widget 'coding-system 'symbol
2938 "A MULE coding-system."
2939 :format "%{%t%}: %v"
2940 :tag "Coding system"
2941 :prompt-history 'widget-coding-system-prompt-value-history
2942 :prompt-value 'widget-coding-system-prompt-value
2943 :action 'widget-coding-system-action)
2945 (defun widget-coding-system-prompt-value (widget prompt value unbound)
2946 ;; Read coding-system from minibuffer.
2948 (completing-read (format "%s (default %s) " prompt value)
2949 (mapcar (lambda (sym)
2950 (list (symbol-name sym)))
2951 (coding-system-list)))))
2953 (defun widget-coding-system-action (widget &optional event)
2954 ;; Read a file name from the minibuffer.
2956 (widget-coding-system-prompt-value
2958 (widget-apply widget :menu-tag-get)
2959 (widget-value widget)
2961 (widget-value-set widget answer)
2962 (widget-apply widget :notify widget event)
2965 (define-widget 'sexp 'editable-field
2966 "An arbitrary Lisp expression."
2967 :tag "Lisp expression"
2968 :format "%{%t%}: %v"
2970 :validate 'widget-sexp-validate
2971 :match (lambda (widget value) t)
2972 :value-to-internal 'widget-sexp-value-to-internal
2973 :value-to-external (lambda (widget value) (read value))
2974 :prompt-history 'widget-sexp-prompt-value-history
2975 :prompt-value 'widget-sexp-prompt-value)
2977 (defun widget-sexp-value-to-internal (widget value)
2978 ;; Use pp for printer representation.
2979 (let ((pp (if (symbolp value)
2980 (prin1-to-string value)
2981 (pp-to-string value))))
2982 (while (string-match "\n\\'" pp)
2983 (setq pp (substring pp 0 -1)))
2984 (if (or (string-match "\n\\'" pp)
2989 (defun widget-sexp-validate (widget)
2990 ;; Valid if we can read the string and there is no junk left after it.
2992 (insert (widget-apply widget :value-get))
2993 (goto-char (point-min))
2994 (condition-case data
2996 ;; Avoid a confusing end-of-file error.
2997 (skip-syntax-forward "\\s-")
2999 (error "Empty sexp -- use `nil'?"))
3000 (let ((value (read (current-buffer))))
3002 (if (widget-apply widget :match value)
3004 (widget-put widget :error (widget-get widget :type-error))
3007 :error (format "Junk at end of expression: %s"
3008 (buffer-substring (point)
3011 (end-of-file ; Avoid confusing error message.
3012 (widget-put widget :error "Unbalanced sexp")
3014 (error (widget-put widget :error (error-message-string data))
3017 (defvar widget-sexp-prompt-value-history nil
3018 "History of input to `widget-sexp-prompt-value'.")
3020 (defun widget-sexp-prompt-value (widget prompt value unbound)
3021 ;; Read an arbitrary sexp.
3022 (let ((found (read-string prompt
3023 (if unbound nil (cons (prin1-to-string value) 0))
3024 (widget-get widget :prompt-history))))
3025 (let ((answer (read-from-string found)))
3026 (unless (= (cdr answer) (length found))
3027 (error "Junk at end of expression: %s"
3028 (substring found (cdr answer))))
3031 (define-widget 'restricted-sexp 'sexp
3032 "A Lisp expression restricted to values that match.
3033 To use this type, you must define :match or :match-alternatives."
3034 :type-error "The specified value is not valid"
3035 :match 'widget-restricted-sexp-match
3036 :value-to-internal (lambda (widget value)
3037 (if (widget-apply widget :match value)
3038 (prin1-to-string value)
3041 (defun widget-restricted-sexp-match (widget value)
3042 (let ((alternatives (widget-get widget :match-alternatives))
3044 (while (and alternatives (not matched))
3045 (if (cond ((functionp (car alternatives))
3046 (funcall (car alternatives) value))
3047 ((and (consp (car alternatives))
3048 (eq (car (car alternatives)) 'quote))
3049 (eq value (nth 1 (car alternatives)))))
3051 (setq alternatives (cdr alternatives)))
3054 (define-widget 'integer 'restricted-sexp
3058 :type-error "This field should contain an integer"
3059 :match-alternatives '(integerp))
3061 (define-widget 'number 'restricted-sexp
3062 "A floating point number."
3065 :type-error "This field should contain a number"
3066 :match-alternatives '(numberp))
3068 (define-widget 'character 'editable-field
3073 :format "%{%t%}: %v\n"
3074 :valid-regexp "\\`.\\'"
3075 :error "This field should contain a single character"
3076 :value-to-internal (lambda (widget value)
3079 (char-to-string value)))
3080 :value-to-external (lambda (widget value)
3084 :match (lambda (widget value)
3085 (char-valid-p value)))
3087 (define-widget 'list 'group
3090 :format "%{%t%}:\n%v")
3092 (define-widget 'vector 'group
3095 :format "%{%t%}:\n%v"
3096 :match 'widget-vector-match
3097 :value-to-internal (lambda (widget value) (append value nil))
3098 :value-to-external (lambda (widget value) (apply 'vector value)))
3100 (defun widget-vector-match (widget value)
3101 (and (vectorp value)
3102 (widget-group-match widget
3103 (widget-apply widget :value-to-internal value))))
3105 (define-widget 'cons 'group
3108 :format "%{%t%}:\n%v"
3109 :match 'widget-cons-match
3110 :value-to-internal (lambda (widget value)
3111 (list (car value) (cdr value)))
3112 :value-to-external (lambda (widget value)
3113 (cons (nth 0 value) (nth 1 value))))
3115 (defun widget-cons-match (widget value)
3117 (widget-group-match widget
3118 (widget-apply widget :value-to-internal value))))
3120 ;;; The `plist' Widget.
3124 (define-widget 'plist 'list
3126 :key-type '(symbol :tag "Key")
3127 :value-type '(sexp :tag "Value")
3128 :convert-widget 'widget-plist-convert-widget
3131 (defvar widget-plist-value-type) ;Dynamic variable
3133 (defun widget-plist-convert-widget (widget)
3134 ;; Handle `:options'.
3135 (let* ((options (widget-get widget :options))
3136 (key-type (widget-get widget :key-type))
3137 (widget-plist-value-type (widget-get widget :value-type))
3138 (other `(editable-list :inline t
3141 ,widget-plist-value-type)))
3143 (list `(checklist :inline t
3145 ,@(mapcar 'widget-plist-convert-option
3149 (widget-put widget :args args)
3152 (defun widget-plist-convert-option (option)
3153 ;; Convert a single plist option.
3154 (let (key-type value-type)
3156 (let ((key (nth 0 option)))
3157 (setq value-type (nth 1 option))
3160 (setq key-type `(const ,key))))
3161 (setq key-type `(const ,option)
3162 value-type widget-plist-value-type))
3163 `(group :format "Key: %v" :inline t ,key-type ,value-type)))
3166 ;;; The `alist' Widget.
3168 ;; Association lists.
3170 (define-widget 'alist 'list
3171 "An association list."
3172 :key-type '(sexp :tag "Key")
3173 :value-type '(sexp :tag "Value")
3174 :convert-widget 'widget-alist-convert-widget
3177 (defvar widget-alist-value-type) ;Dynamic variable
3179 (defun widget-alist-convert-widget (widget)
3180 ;; Handle `:options'.
3181 (let* ((options (widget-get widget :options))
3182 (key-type (widget-get widget :key-type))
3183 (widget-alist-value-type (widget-get widget :value-type))
3184 (other `(editable-list :inline t
3187 ,widget-alist-value-type)))
3189 (list `(checklist :inline t
3191 ,@(mapcar 'widget-alist-convert-option
3195 (widget-put widget :args args)
3198 (defun widget-alist-convert-option (option)
3199 ;; Convert a single alist option.
3200 (let (key-type value-type)
3202 (let ((key (nth 0 option)))
3203 (setq value-type (nth 1 option))
3206 (setq key-type `(const ,key))))
3207 (setq key-type `(const ,option)
3208 value-type widget-alist-value-type))
3209 `(cons :format "Key: %v" ,key-type ,value-type)))
3211 (define-widget 'choice 'menu-choice
3212 "A union of several sexp types."
3214 :format "%{%t%}: %[Value Menu%] %v"
3215 :button-prefix 'widget-push-button-prefix
3216 :button-suffix 'widget-push-button-suffix
3217 :prompt-value 'widget-choice-prompt-value)
3219 (defun widget-choice-prompt-value (widget prompt value unbound)
3221 (let ((args (widget-get widget :args))
3222 (completion-ignore-case (widget-get widget :case-fold))
3223 current choices old)
3224 ;; Find the first arg that match VALUE.
3227 (if (widget-apply (car look) :match value)
3228 (setq old (car look)
3230 (setq look (cdr look)))))
3233 (cond ((= (length args) 0)
3235 ((= (length args) 1)
3237 ((and (= (length args) 2)
3239 (if (eq old (nth 0 args))
3244 (setq current (car args)
3247 (cons (cons (widget-apply current :menu-tag-get)
3250 (let ((val (completing-read prompt choices nil t)))
3252 (let ((try (try-completion val choices)))
3255 (cdr (assoc val choices)))
3258 (widget-prompt-value current prompt nil t)
3261 (define-widget 'radio 'radio-button-choice
3262 "A union of several sexp types."
3264 :format "%{%t%}:\n%v"
3265 :prompt-value 'widget-choice-prompt-value)
3267 (define-widget 'repeat 'editable-list
3268 "A variable length homogeneous list."
3270 :format "%{%t%}:\n%v%i\n")
3272 (define-widget 'set 'checklist
3273 "A list of members from a fixed set."
3275 :format "%{%t%}:\n%v")
3277 (define-widget 'boolean 'toggle
3278 "To be nil or non-nil, that is the question."
3280 :prompt-value 'widget-boolean-prompt-value
3281 :button-prefix 'widget-push-button-prefix
3282 :button-suffix 'widget-push-button-suffix
3283 :format "%{%t%}: %[Toggle%] %v\n"
3287 (defun widget-boolean-prompt-value (widget prompt value unbound)
3288 ;; Toggle a boolean.
3291 ;;; The `color' Widget.
3293 (define-widget 'color 'editable-field
3294 "Choose a color name (with sample)."
3295 :format "%t: %v (%{sample%})\n"
3299 :complete 'widget-color-complete
3300 :sample-face-get 'widget-color-sample-face-get
3301 :notify 'widget-color-notify
3302 :action 'widget-color-action)
3304 (defun widget-color-complete (widget)
3305 "Complete the color in WIDGET."
3306 (require 'facemenu) ; for facemenu-color-alist
3307 (let* ((prefix (buffer-substring-no-properties (widget-field-start widget)
3309 (list (or facemenu-color-alist
3310 (mapcar 'list (defined-colors))))
3311 (completion (try-completion prefix list)))
3312 (cond ((eq completion t)
3313 (message "Exact match."))
3315 (error "Can't find completion for \"%s\"" prefix))
3316 ((not (string-equal prefix completion))
3317 (insert-and-inherit (substring completion (length prefix))))
3319 (message "Making completion list...")
3320 (let ((list (all-completions prefix list nil)))
3321 (with-output-to-temp-buffer "*Completions*"
3322 (display-completion-list list)))
3323 (message "Making completion list...done")))))
3325 (defun widget-color-sample-face-get (widget)
3326 (let* ((value (condition-case nil
3327 (widget-value widget)
3328 (error (widget-get widget :value))))
3329 (symbol (intern (concat "fg:" value))))
3331 (facemenu-get-face symbol)
3334 (defun widget-color-action (widget &optional event)
3335 ;; Prompt for a color.
3336 (let* ((tag (widget-apply widget :menu-tag-get))
3337 (prompt (concat tag ": "))
3338 (value (widget-value widget))
3339 (start (widget-field-start widget))
3340 (pos (cond ((< (point) start)
3342 ((> (point) (+ start (length value)))
3345 (- (point) start))))
3346 (answer (facemenu-read-color prompt)))
3347 (unless (zerop (length answer))
3348 (widget-value-set widget answer)
3350 (widget-apply widget :notify widget event))))
3352 (defun widget-color-notify (widget child &optional event)
3353 "Update the sample, and notofy the parent."
3354 (overlay-put (widget-get widget :sample-overlay)
3355 'face (widget-apply widget :sample-face-get))
3356 (widget-default-notify widget child event))
3360 (defun widget-at (pos)
3361 "The button or field at POS."
3362 (or (get-char-property pos 'button)
3363 (get-char-property pos 'field)))
3365 (defun widget-echo-help (pos)
3366 "Display the help echo for widget at POS."
3367 (let* ((widget (widget-at pos))
3368 (help-echo (and widget (widget-get widget :help-echo))))
3369 (if (or (stringp help-echo)
3370 (and (symbolp help-echo) (fboundp help-echo)
3371 (stringp (setq help-echo (funcall help-echo widget)))))
3372 (message "%s" help-echo))))
3378 ;; wid-edit.el ends here