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
) (display-mouse-p))
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 (overlay-put overlay 'help-echo help-echo))
308 (widget-specify-secret widget))
310 (defun widget-specify-secret (field)
311 "Replace text in FIELD with value of `:secret', if non-nil."
312 (let ((secret (widget-get field :secret))
313 (size (widget-get field :size)))
315 (let ((begin (widget-field-start field))
316 (end (widget-field-end field)))
318 (while (and (> end begin)
319 (eq (char-after (1- end)) ?\ ))
320 (setq end (1- end))))
322 (let ((old (char-after begin)))
323 (unless (eq old secret)
324 (subst-char-in-region begin (1+ begin) old secret)
325 (put-text-property begin (1+ begin) 'secret old))
326 (setq begin (1+ begin))))))))
328 (defun widget-specify-button (widget from to)
329 "Specify button for WIDGET between FROM and TO."
330 (let ((face (widget-apply widget :button-face-get))
331 (help-echo (widget-get widget :help-echo))
332 (overlay (make-overlay from to nil t nil)))
333 (widget-put widget :button-overlay overlay)
334 (overlay-put overlay 'button widget)
335 ;; We want to avoid the face with image buttons.
336 (unless (widget-get widget :suppress-face)
337 (overlay-put overlay 'face face)
338 (overlay-put overlay 'mouse-face widget-mouse-face))
339 ;;(overlay-put overlay 'balloon-help help-echo)
340 (overlay-put overlay 'help-echo help-echo)))
342 (defun widget-specify-sample (widget from to)
343 "Specify sample for WIDGET between FROM and TO."
344 (let ((face (widget-apply widget :sample-face-get))
345 (overlay (make-overlay from to nil t nil)))
346 (overlay-put overlay 'face face)
347 (widget-put widget :sample-overlay overlay)))
349 (defun widget-specify-doc (widget from to)
350 "Specify documentation for WIDGET between FROM and TO."
351 (let ((overlay (make-overlay from to nil t nil)))
352 (overlay-put overlay 'widget-doc widget)
353 (overlay-put overlay 'face widget-documentation-face)
354 (widget-put widget :doc-overlay overlay)))
356 (defmacro widget-specify-insert (&rest form)
357 "Execute FORM without inheriting any text properties."
359 (let ((inhibit-read-only t)
361 before-change-functions
362 after-change-functions)
364 (narrow-to-region (- (point) 2) (point))
365 (goto-char (1+ (point-min)))
366 (setq result (progn ,@form))
367 (delete-region (point-min) (1+ (point-min)))
368 (delete-region (1- (point-max)) (point-max))
369 (goto-char (point-max))
372 (defface widget-inactive-face '((((class grayscale color)
374 (:foreground "light gray"))
375 (((class grayscale color)
377 (:foreground "dim gray"))
380 "Face used for inactive widgets."
381 :group 'widget-faces)
383 (defun widget-specify-inactive (widget from to)
384 "Make WIDGET inactive for user modifications."
385 (unless (widget-get widget :inactive)
386 (let ((overlay (make-overlay from to nil t nil)))
387 (overlay-put overlay 'face 'widget-inactive-face)
388 ;; This is disabled, as it makes the mouse cursor change shape.
389 ;; (overlay-put overlay 'mouse-face 'widget-inactive-face)
390 (overlay-put overlay 'evaporate t)
391 (overlay-put overlay 'priority 100)
392 (overlay-put overlay 'modification-hooks '(widget-overlay-inactive))
393 (widget-put widget :inactive overlay))))
395 (defun widget-overlay-inactive (&rest junk)
396 "Ignoring the arguments, signal an error."
397 (unless inhibit-read-only
398 (error "The widget here is not active")))
401 (defun widget-specify-active (widget)
402 "Make WIDGET active for user modifications."
403 (let ((inactive (widget-get widget :inactive)))
405 (delete-overlay inactive)
406 (widget-put widget :inactive nil))))
408 ;;; Widget Properties.
410 (defsubst widget-type (widget)
411 "Return the type of WIDGET, a symbol."
414 (defun widget-get-indirect (widget property)
415 "In WIDGET, get the value of PROPERTY.
416 If the value is a symbol, return its binding.
417 Otherwise, just return the value."
418 (let ((value (widget-get widget property)))
423 (defun widget-member (widget property)
424 "Non-nil iff there is a definition in WIDGET for PROPERTY."
425 (cond ((plist-member (cdr widget) property)
428 (widget-member (get (car widget) 'widget-type) property))
431 (defun widget-value (widget)
432 "Extract the current value of WIDGET."
434 :value-to-external (widget-apply widget :value-get)))
436 (defun widget-value-set (widget value)
437 "Set the current value of WIDGET to VALUE."
439 :value-set (widget-apply widget
440 :value-to-internal value)))
442 (defun widget-default-get (widget)
443 "Extract the default value of WIDGET."
444 (or (widget-get widget :value)
445 (widget-apply widget :default-get)))
447 (defun widget-match-inline (widget vals)
448 "In WIDGET, match the start of VALS."
449 (cond ((widget-get widget :inline)
450 (widget-apply widget :match-inline vals))
452 (widget-apply widget :match (car vals)))
453 (cons (list (car vals)) (cdr vals)))
456 (defun widget-apply-action (widget &optional event)
457 "Apply :action in WIDGET in response to EVENT."
458 (if (widget-apply widget :active)
459 (widget-apply widget :action event)
460 (error "Attempt to perform action on inactive widget")))
462 ;;; Helper functions.
464 ;; These are widget specific.
467 (defun widget-prompt-value (widget prompt &optional value unbound)
468 "Prompt for a value matching WIDGET, using PROMPT.
469 The current value is assumed to be VALUE, unless UNBOUND is non-nil."
470 (unless (listp widget)
471 (setq widget (list widget)))
472 (setq prompt (format "[%s] %s" (widget-type widget) prompt))
473 (setq widget (widget-convert widget))
474 (let ((answer (widget-apply widget :prompt-value prompt value unbound)))
475 (unless (widget-apply widget :match answer)
476 (error "Value does not match %S type" (car widget)))
479 (defun widget-get-sibling (widget)
480 "Get the item WIDGET is assumed to toggle.
481 This is only meaningful for radio buttons or checkboxes in a list."
482 (let* ((parent (widget-get widget :parent))
483 (children (widget-get parent :children))
487 (setq child (car children)
488 children (cdr children))
489 (when (eq (widget-get child :button) widget)
490 (throw 'child child)))
493 (defun widget-map-buttons (function &optional buffer maparg)
494 "Map FUNCTION over the buttons in BUFFER.
495 FUNCTION is called with the arguments WIDGET and MAPARG.
497 If FUNCTION returns non-nil, the walk is cancelled.
499 The arguments MAPARG, and BUFFER default to nil and (current-buffer),
501 (let ((cur (point-min))
505 (save-excursion (set-buffer buffer) (overlay-lists))
507 (setq overlays (append (car overlays) (cdr overlays)))
508 (while (setq cur (pop overlays))
509 (setq widget (overlay-get cur 'button))
510 (if (and widget (funcall function widget maparg))
511 (setq overlays nil)))))
515 (defcustom widget-image-directory (file-name-as-directory
516 (expand-file-name "custom" data-directory))
517 "Where widget button images are located.
518 If this variable is nil, widget will try to locate the directory
523 (defcustom widget-image-enable t
524 "If non nil, use image buttons in widgets when available."
529 (defcustom widget-image-conversion
530 '((xpm ".xpm") (gif ".gif") (png ".png") (jpeg ".jpg" ".jpeg")
532 "Conversion alist from image formats to file name suffixes."
534 :type '(repeat (cons :format "%v"
535 (symbol :tag "Image Format" unknown)
536 (repeat :tag "Suffixes"
537 (string :format "%v")))))
539 (defun widget-image-find (image)
540 "Create a graphical button from IMAGE.
541 IMAGE should either already be an image, or be a file name sans
542 extension (xpm, xbm, gif, jpg, or png) located in
543 `widget-image-directory' or otherwise where `find-image' will find it."
544 (cond ((not (and image widget-image-enable (display-graphic-p)))
545 ;; We don't want or can't use images.
548 (eq 'image (car image)))
549 ;; Already an image spec. Use it.
552 ;; A string. Look it up in relevant directories.
553 (let* ((load-path (cons widget-image-directory load-path))
554 (formats widget-image-conversion)
556 (dolist (elt widget-image-conversion)
557 (dolist (ext (cdr elt))
558 (push (list :type (car elt) :file (concat image ext)) specs)))
559 (setq specs (nreverse specs))
565 (defvar widget-button-pressed-face 'widget-button-pressed-face
566 "Face used for pressed buttons in widgets.
567 This exists as a variable so it can be set locally in certain
570 (defun widget-image-insert (widget tag image &optional down inactive)
571 "In WIDGET, insert the text TAG or, if supported, IMAGE.
572 IMAGE should either be an image or an image file name sans extension
573 \(xpm, xbm, gif, jpg, or png) located in `widget-image-directory'.
575 Optional arguments DOWN and INACTIVE are used instead of IMAGE when the
576 button is pressed or inactive, respectively. These are currently ignored."
577 (if (and (display-graphic-p)
578 (setq image (widget-image-find image)))
579 (progn (widget-put widget :suppress-face t)
582 tag 'mouse-face widget-button-pressed-face)))
587 (defgroup widget-button nil
588 "The look of various kinds of buttons."
591 (defcustom widget-button-prefix ""
592 "String used as prefix for buttons."
594 :group 'widget-button)
596 (defcustom widget-button-suffix ""
597 "String used as suffix for buttons."
599 :group 'widget-button)
601 ;;; Creating Widgets.
604 (defun widget-create (type &rest args)
605 "Create widget of TYPE.
606 The optional ARGS are additional keyword arguments."
607 (let ((widget (apply 'widget-convert type args)))
608 (widget-apply widget :create)
611 (defun widget-create-child-and-convert (parent type &rest args)
612 "As part of the widget PARENT, create a child widget TYPE.
613 The child is converted, using the keyword arguments ARGS."
614 (let ((widget (apply 'widget-convert type args)))
615 (widget-put widget :parent parent)
616 (unless (widget-get widget :indent)
617 (widget-put widget :indent (+ (or (widget-get parent :indent) 0)
618 (or (widget-get widget :extra-offset) 0)
619 (widget-get parent :offset))))
620 (widget-apply widget :create)
623 (defun widget-create-child (parent type)
624 "Create widget of TYPE."
625 (let ((widget (copy-sequence type)))
626 (widget-put widget :parent parent)
627 (unless (widget-get widget :indent)
628 (widget-put widget :indent (+ (or (widget-get parent :indent) 0)
629 (or (widget-get widget :extra-offset) 0)
630 (widget-get parent :offset))))
631 (widget-apply widget :create)
634 (defun widget-create-child-value (parent type value)
635 "Create widget of TYPE with value VALUE."
636 (let ((widget (copy-sequence type)))
637 (widget-put widget :value (widget-apply widget :value-to-internal value))
638 (widget-put widget :parent parent)
639 (unless (widget-get widget :indent)
640 (widget-put widget :indent (+ (or (widget-get parent :indent) 0)
641 (or (widget-get widget :extra-offset) 0)
642 (widget-get parent :offset))))
643 (widget-apply widget :create)
647 (defun widget-delete (widget)
649 (widget-apply widget :delete))
651 (defun widget-convert (type &rest args)
652 "Convert TYPE to a widget without inserting it in the buffer.
653 The optional ARGS are additional keyword arguments."
654 ;; Don't touch the type.
655 (let* ((widget (if (symbolp type)
657 (copy-sequence type)))
660 ;; First set the :args keyword.
661 (while (cdr current) ;Look in the type.
662 (let ((next (car (cdr current))))
664 (setq current (cdr (cdr current)))
665 (setcdr current (list :args (cdr current)))
666 (setq current nil))))
667 (while args ;Look in the args.
668 (let ((next (nth 0 args)))
670 (setq args (nthcdr 2 args))
671 (widget-put widget :args args)
673 ;; Then Convert the widget.
676 (let ((convert-widget (plist-get (cdr type) :convert-widget)))
678 (setq widget (funcall convert-widget widget))))
679 (setq type (get (car type) 'widget-type)))
680 ;; Finally set the keyword args.
682 (let ((next (nth 0 keys)))
685 (widget-put widget next (nth 1 keys))
686 (setq keys (nthcdr 2 keys)))
688 ;; Convert the :value to internal format.
689 (if (widget-member widget :value)
690 (let ((value (widget-get widget :value)))
692 :value (widget-apply widget :value-to-internal value))))
693 ;; Return the newly create widget.
696 (defun widget-insert (&rest args)
697 "Call `insert' with ARGS and make the text read only."
698 (let ((inhibit-read-only t)
699 before-change-functions
700 after-change-functions
702 (apply 'insert args)))
704 (defun widget-convert-text (type from to
705 &optional button-from button-to
707 "Return a widget of type TYPE with endpoint FROM TO.
708 Optional ARGS are extra keyword arguments for TYPE.
709 and TO will be used as the widgets end points. If optional arguments
710 BUTTON-FROM and BUTTON-TO are given, these will be used as the widgets
712 Optional ARGS are extra keyword arguments for TYPE."
713 (let ((widget (apply 'widget-convert type :delete 'widget-leave-text args))
714 (from (copy-marker from))
715 (to (copy-marker to)))
716 (set-marker-insertion-type from t)
717 (set-marker-insertion-type to nil)
718 (widget-put widget :from from)
719 (widget-put widget :to to)
721 (widget-specify-button widget button-from button-to))
724 (defun widget-convert-button (type from to &rest args)
725 "Return a widget of type TYPE with endpoint FROM TO.
726 Optional ARGS are extra keyword arguments for TYPE.
727 No text will be inserted to the buffer, instead the text between FROM
728 and TO will be used as the widgets end points, as well as the widgets
730 (apply 'widget-convert-text type from to from to args))
732 (defun widget-leave-text (widget)
733 "Remove markers and overlays from WIDGET and its children."
734 (let ((from (widget-get widget :from))
735 (to (widget-get widget :to))
736 (button (widget-get widget :button-overlay))
737 (sample (widget-get widget :sample-overlay))
738 (doc (widget-get widget :doc-overlay))
739 (field (widget-get widget :field-overlay))
740 (children (widget-get widget :children)))
741 (set-marker from nil)
744 (delete-overlay button))
746 (delete-overlay sample))
748 (delete-overlay doc))
750 (delete-overlay field))
751 (mapc 'widget-leave-text children)))
753 ;;; Keymap and Commands.
755 (defvar widget-keymap
756 (let ((map (make-sparse-keymap)))
757 (define-key map "\t" 'widget-forward)
758 (define-key map [(shift tab)] 'widget-backward)
759 (define-key map [backtab] 'widget-backward)
760 (define-key map [down-mouse-2] 'widget-button-click)
761 (define-key map "\C-m" 'widget-button-press)
763 "Keymap containing useful binding for buffers containing widgets.
764 Recommended as a parent keymap for modes using widgets.")
766 (defvar widget-global-map global-map
767 "Keymap used for events the widget does not handle themselves.")
768 (make-variable-buffer-local 'widget-global-map)
770 (defvar widget-field-keymap
771 (let ((map (copy-keymap widget-keymap)))
772 (define-key map [menu-bar] nil)
773 (define-key map "\C-k" 'widget-kill-line)
774 (define-key map "\M-\t" 'widget-complete)
775 (define-key map "\C-m" 'widget-field-activate)
776 (define-key map "\C-a" 'widget-beginning-of-line)
777 (define-key map "\C-e" 'widget-end-of-line)
778 (set-keymap-parent map global-map)
780 "Keymap used inside an editable field.")
782 (defvar widget-text-keymap
783 (let ((map (copy-keymap widget-keymap)))
784 (define-key map [menu-bar] 'nil)
785 (define-key map "\C-a" 'widget-beginning-of-line)
786 (define-key map "\C-e" 'widget-end-of-line)
787 (set-keymap-parent map global-map)
789 "Keymap used inside a text field.")
791 (defun widget-field-activate (pos &optional event)
792 "Invoke the ediable field at point."
794 (let ((field (get-char-property pos 'field)))
796 (widget-apply-action field event)
798 (lookup-key widget-global-map (this-command-keys))))))
800 (defface widget-button-pressed-face
804 (:bold t :underline t)))
805 "Face used for pressed buttons."
806 :group 'widget-faces)
808 (defun widget-button-click (event)
809 "Invoke the button that the mouse is pointing at."
811 (if (widget-event-point event)
813 (mouse-set-point event)
814 (let* ((pos (widget-event-point event))
815 (button (get-char-property pos 'button)))
817 (let* ((overlay (widget-get button :button-overlay))
818 (face (overlay-get overlay 'face))
819 (mouse-face (overlay-get overlay 'mouse-face)))
821 (let ((track-mouse t))
823 (when face ; avoid changing around image
825 'face widget-button-pressed-face)
827 'mouse-face widget-button-pressed-face))
828 (unless (widget-apply button :mouse-down-action event)
829 (while (not (widget-button-release-event-p event))
830 (setq event (read-event)
831 pos (widget-event-point event))
833 (eq (get-char-property pos 'button)
838 widget-button-pressed-face)
841 widget-button-pressed-face))
842 (overlay-put overlay 'face face)
843 (overlay-put overlay 'mouse-face mouse-face))))
845 (eq (get-char-property pos 'button) button))
846 (widget-apply-action button event))))
847 (overlay-put overlay 'face face)
848 (overlay-put overlay 'mouse-face mouse-face)))
851 ;; Find the global command to run, and check whether it
852 ;; is bound to an up event.
853 (if (memq (event-basic-type event) '(mouse-1 down-mouse-1))
854 (cond ((setq command ;down event
855 (lookup-key widget-global-map [down-mouse-1]))
857 ((setq command ;up event
858 (lookup-key widget-global-map [mouse-1]))))
859 (cond ((setq command ;down event
860 (lookup-key widget-global-map [down-mouse-2]))
862 ((setq command ;up event
863 (lookup-key widget-global-map [mouse-2])))))
865 ;; Don't execute up events twice.
866 (while (not (widget-button-release-event-p event))
867 (setq event (read-event))))
869 (call-interactively command)))))
870 (unless (pos-visible-in-window-p (widget-event-point event))
871 (mouse-set-point event)
874 (message "You clicked somewhere weird.")))
876 (defun widget-button-press (pos &optional event)
877 "Invoke button at POS."
879 (let ((button (get-char-property pos 'button)))
881 (widget-apply-action button event)
882 (let ((command (lookup-key widget-global-map (this-command-keys))))
883 (when (commandp command)
884 (call-interactively command))))))
886 (defun widget-tabable-at (&optional pos)
887 "Return the tabable widget at POS, or nil.
888 POS defaults to the value of (point)."
891 (let ((widget (or (get-char-property pos 'button)
892 (get-char-property pos 'field))))
894 (let ((order (widget-get widget :tab-order)))
900 (defvar widget-use-overlay-change t
901 "If non-nil, use overlay change functions to tab around in the buffer.
902 This is much faster, but doesn't work reliably on Emacs 19.34.")
904 (defun widget-move (arg)
905 "Move point to the ARG next field or button.
906 ARG may be negative to move backward."
907 (or (bobp) (> arg 0) (backward-char))
910 (old (widget-tabable-at))
915 (goto-char (point-min)))
916 (widget-use-overlay-change
917 (goto-char (next-overlay-change (point))))
920 (and (eq pos (point))
922 (error "No buttons or fields found"))
923 (let ((new (widget-tabable-at)))
931 (goto-char (point-max)))
932 (widget-use-overlay-change
933 (goto-char (previous-overlay-change (point))))
936 (and (eq pos (point))
938 (error "No buttons or fields found"))
939 (let ((new (widget-tabable-at)))
942 (setq arg (1+ arg))))))
943 (let ((new (widget-tabable-at)))
944 (while (eq (widget-tabable-at) new)
947 (widget-echo-help (point))
948 (run-hooks 'widget-move-hook))
950 (defun widget-forward (arg)
951 "Move point to the next field or button.
952 With optional ARG, move across that many fields."
954 (run-hooks 'widget-forward-hook)
957 (defun widget-backward (arg)
958 "Move point to the previous field or button.
959 With optional ARG, move across that many fields."
961 (run-hooks 'widget-backward-hook)
962 (widget-move (- arg)))
964 (defun widget-beginning-of-line ()
965 "Go to beginning of field or beginning of line, whichever is first."
967 (let* ((field (widget-field-find (point)))
968 (start (and field (widget-field-start field)))
969 (bol (line-beginning-position)))
974 (defun widget-end-of-line ()
975 "Go to end of field or end of line, whichever is first."
977 (let* ((field (widget-field-find (point)))
978 (end (and field (widget-field-end field)))
979 (eol (line-end-position)))
984 (defun widget-kill-line ()
985 "Kill to end of field or end of line, whichever is first."
987 (let* ((field (widget-field-find (point)))
988 (newline (save-excursion (forward-line 1) (point)))
989 (end (and field (widget-field-end field))))
990 (if (and field (> newline end))
991 (kill-region (point) end)
992 (call-interactively 'kill-line))))
994 (defcustom widget-complete-field (lookup-key global-map "\M-\t")
995 "Default function to call for completion inside fields."
996 :options '(ispell-complete-word complete-tag lisp-complete-symbol)
1000 (defun widget-complete ()
1001 "Complete content of editable field from point.
1002 When not inside a field, move to the previous button or field."
1004 (let ((field (widget-field-find (point))))
1006 (widget-apply field :complete)
1007 (error "Not in an editable field"))))
1009 ;;; Setting up the buffer.
1011 (defvar widget-field-new nil)
1012 ;; List of all newly created editable fields in the buffer.
1013 (make-variable-buffer-local 'widget-field-new)
1015 (defvar widget-field-list nil)
1016 ;; List of all editable fields in the buffer.
1017 (make-variable-buffer-local 'widget-field-list)
1019 (defun widget-setup ()
1020 "Setup current buffer so editing string widgets works."
1021 (let ((inhibit-read-only t)
1022 (after-change-functions nil)
1023 before-change-functions
1025 (while widget-field-new
1026 (setq field (car widget-field-new)
1027 widget-field-new (cdr widget-field-new)
1028 widget-field-list (cons field widget-field-list))
1029 (let ((from (car (widget-get field :field-overlay)))
1030 (to (cdr (widget-get field :field-overlay))))
1031 (widget-specify-field field
1032 (marker-position from) (marker-position to))
1033 (set-marker from nil)
1034 (set-marker to nil))))
1036 (widget-add-change))
1038 (defvar widget-field-last nil)
1039 ;; Last field containing point.
1040 (make-variable-buffer-local 'widget-field-last)
1042 (defvar widget-field-was nil)
1043 ;; The widget data before the change.
1044 (make-variable-buffer-local 'widget-field-was)
1046 (defun widget-field-buffer (widget)
1047 "Return the start of WIDGET's editing field."
1048 (let ((overlay (widget-get widget :field-overlay)))
1049 (and overlay (overlay-buffer overlay))))
1051 (defun widget-field-start (widget)
1052 "Return the start of WIDGET's editing field."
1053 (let ((overlay (widget-get widget :field-overlay)))
1054 (and overlay (overlay-start overlay))))
1056 (defun widget-field-end (widget)
1057 "Return the end of WIDGET's editing field."
1058 (let ((overlay (widget-get widget :field-overlay)))
1059 ;; Don't subtract one if local-map works at the end of the overlay.
1060 (and overlay (if (or widget-field-add-space
1061 (null (widget-get widget :size)))
1062 (1- (overlay-end overlay))
1063 (overlay-end overlay)))))
1065 (defun widget-field-find (pos)
1066 "Return the field at POS.
1067 Unlike (get-char-property POS 'field) this, works with empty fields too."
1068 (let ((fields widget-field-list)
1071 (setq field (car fields)
1072 fields (cdr fields))
1073 (let ((start (widget-field-start field))
1074 (end (widget-field-end field)))
1075 (when (and (<= start pos) (<= pos end))
1077 (debug "Overlapping fields"))
1078 (setq found field))))
1081 (defun widget-before-change (from to)
1082 ;; This is how, for example, a variable changes its state to `modified'.
1083 ;; when it is being edited.
1084 (unless inhibit-read-only
1085 (let ((from-field (widget-field-find from))
1086 (to-field (widget-field-find to)))
1087 (cond ((not (eq from-field to-field))
1088 (add-hook 'post-command-hook 'widget-add-change nil t)
1089 (signal 'text-read-only
1090 '("Change should be restricted to a single field")))
1092 (add-hook 'post-command-hook 'widget-add-change nil t)
1093 (signal 'text-read-only
1094 '("Attempt to change text outside editable field")))
1095 (widget-field-use-before-change
1097 (widget-apply from-field :notify from-field)
1098 (error (debug "Before Change"))))))))
1100 (defun widget-add-change ()
1101 (make-local-hook 'post-command-hook)
1102 (remove-hook 'post-command-hook 'widget-add-change t)
1103 (make-local-hook 'before-change-functions)
1104 (add-hook 'before-change-functions 'widget-before-change nil t)
1105 (make-local-hook 'after-change-functions)
1106 (add-hook 'after-change-functions 'widget-after-change nil t))
1108 (defun widget-after-change (from to old)
1109 "Adjust field size and text properties."
1111 (let ((field (widget-field-find from))
1112 (other (widget-field-find to)))
1114 (unless (eq field other)
1115 (debug "Change in different fields"))
1116 (let ((size (widget-get field :size)))
1118 (let ((begin (widget-field-start field))
1119 (end (widget-field-end field)))
1120 (cond ((< (- end begin) size)
1124 (insert-char ?\ (- (+ begin size) end))))
1125 ((> (- end begin) size)
1126 ;; Field too large and
1127 (if (or (< (point) (+ begin size))
1129 ;; Point is outside extra space.
1130 (setq begin (+ begin size))
1131 ;; Point is within the extra space.
1132 (setq begin (point)))
1135 (while (and (eq (preceding-char) ?\ )
1137 (delete-backward-char 1)))))))
1138 (widget-specify-secret field))
1139 (widget-apply field :notify field)))
1140 (error (debug "After Change"))))
1142 ;;; Widget Functions
1144 ;; These functions are used in the definition of multiple widgets.
1146 (defun widget-parent-action (widget &optional event)
1147 "Tell :parent of WIDGET to handle the :action.
1148 Optional EVENT is the event that triggered the action."
1149 (widget-apply (widget-get widget :parent) :action event))
1151 (defun widget-children-value-delete (widget)
1152 "Delete all :children and :buttons in WIDGET."
1153 (mapc 'widget-delete (widget-get widget :children))
1154 (widget-put widget :children nil)
1155 (mapc 'widget-delete (widget-get widget :buttons))
1156 (widget-put widget :buttons nil))
1158 (defun widget-children-validate (widget)
1159 "All the :children must be valid."
1160 (let ((children (widget-get widget :children))
1162 (while (and children (not found))
1163 (setq child (car children)
1164 children (cdr children)
1165 found (widget-apply child :validate)))
1168 ;; Made defsubst to speed up face editor creation.
1169 (defsubst widget-types-convert-widget (widget)
1170 "Convert :args as widget types in WIDGET."
1171 (widget-put widget :args (mapcar 'widget-convert (widget-get widget :args)))
1174 (defun widget-value-convert-widget (widget)
1175 "Initialize :value from :args in WIDGET."
1176 (let ((args (widget-get widget :args)))
1178 (widget-put widget :value (car args))
1179 ;; Don't convert :value here, as this is done in `widget-convert'.
1180 ;; (widget-put widget :value (widget-apply widget
1181 ;; :value-to-internal (car args)))
1182 (widget-put widget :args nil)))
1185 (defun widget-value-value-get (widget)
1186 "Return the :value property of WIDGET."
1187 (widget-get widget :value))
1189 ;;; The `default' Widget.
1191 (define-widget 'default nil
1192 "Basic widget other widgets are derived from."
1193 :value-to-internal (lambda (widget value) value)
1194 :value-to-external (lambda (widget value) value)
1195 :button-prefix 'widget-button-prefix
1196 :button-suffix 'widget-button-suffix
1197 :complete 'widget-default-complete
1198 :create 'widget-default-create
1201 :format-handler 'widget-default-format-handler
1202 :button-face-get 'widget-default-button-face-get
1203 :sample-face-get 'widget-default-sample-face-get
1204 :delete 'widget-default-delete
1205 :value-set 'widget-default-value-set
1206 :value-inline 'widget-default-value-inline
1207 :default-get 'widget-default-default-get
1208 :menu-tag-get 'widget-default-menu-tag-get
1210 :active 'widget-default-active
1211 :activate 'widget-specify-active
1212 :deactivate 'widget-default-deactivate
1213 :mouse-down-action #'ignore
1214 :action 'widget-default-action
1215 :notify 'widget-default-notify
1216 :prompt-value 'widget-default-prompt-value)
1218 (defun widget-default-complete (widget)
1219 "Call the value of the :complete-function property of WIDGET.
1220 If that does not exists, call the value of `widget-complete-field'."
1221 (let ((fun (widget-get widget :complete-function)))
1222 (call-interactively (or fun widget-complete-field))))
1224 (defun widget-default-create (widget)
1225 "Create WIDGET at point in the current buffer."
1226 (widget-specify-insert
1227 (let ((from (point))
1228 button-begin button-end
1229 sample-begin sample-end
1232 (insert (widget-get widget :format))
1234 ;; Parse escapes in format.
1235 (while (re-search-forward "%\\(.\\)" nil t)
1236 (let ((escape (aref (match-string 1) 0)))
1237 (replace-match "" t t)
1238 (cond ((eq escape ?%)
1241 (setq button-begin (point))
1242 (insert (widget-get-indirect widget :button-prefix)))
1244 (insert (widget-get-indirect widget :button-suffix))
1245 (setq button-end (point)))
1247 (setq sample-begin (point)))
1249 (setq sample-end (point)))
1251 (when (widget-get widget :indent)
1253 (insert-char ? (widget-get widget :indent))))
1255 (let ((image (widget-get widget :tag-glyph))
1256 (tag (widget-get widget :tag)))
1258 (widget-image-insert widget (or tag "image") image))
1262 (princ (widget-get widget :value)
1263 (current-buffer))))))
1265 (let ((doc (widget-get widget :doc)))
1267 (setq doc-begin (point))
1269 (while (eq (preceding-char) ?\n)
1270 (delete-backward-char 1))
1272 (setq doc-end (point)))))
1274 (if (and button-begin (not button-end))
1275 (widget-apply widget :value-create)
1276 (setq value-pos (point))))
1278 (widget-apply widget :format-handler escape)))))
1279 ;; Specify button, sample, and doc, and insert value.
1280 (and button-begin button-end
1281 (widget-specify-button widget button-begin button-end))
1282 (and sample-begin sample-end
1283 (widget-specify-sample widget sample-begin sample-end))
1284 (and doc-begin doc-end
1285 (widget-specify-doc widget doc-begin doc-end))
1287 (goto-char value-pos)
1288 (widget-apply widget :value-create)))
1289 (let ((from (copy-marker (point-min)))
1290 (to (copy-marker (point-max))))
1291 (set-marker-insertion-type from t)
1292 (set-marker-insertion-type to nil)
1293 (widget-put widget :from from)
1294 (widget-put widget :to to)))
1295 (widget-clear-undo))
1297 (defun widget-default-format-handler (widget escape)
1298 ;; We recognize the %h escape by default.
1299 (let* ((buttons (widget-get widget :buttons)))
1300 (cond ((eq escape ?h)
1301 (let* ((doc-property (widget-get widget :documentation-property))
1302 (doc-try (cond ((widget-get widget :doc))
1303 ((symbolp doc-property)
1304 (documentation-property
1305 (widget-get widget :value)
1308 (funcall doc-property
1309 (widget-get widget :value)))))
1310 (doc-text (and (stringp doc-try)
1311 (> (length doc-try) 1)
1313 (doc-indent (widget-get widget :documentation-indent)))
1315 (and (eq (preceding-char) ?\n)
1316 (widget-get widget :indent)
1317 (insert-char ? (widget-get widget :indent)))
1318 ;; The `*' in the beginning is redundant.
1319 (when (eq (aref doc-text 0) ?*)
1320 (setq doc-text (substring doc-text 1)))
1321 ;; Get rid of trailing newlines.
1322 (when (string-match "\n+\\'" doc-text)
1323 (setq doc-text (substring doc-text 0 (match-beginning 0))))
1324 (push (widget-create-child-and-convert
1325 widget 'documentation-string
1326 :indent (cond ((numberp doc-indent )
1334 (error "Unknown escape `%c'" escape)))
1335 (widget-put widget :buttons buttons)))
1337 (defun widget-default-button-face-get (widget)
1338 ;; Use :button-face or widget-button-face
1339 (or (widget-get widget :button-face)
1340 (let ((parent (widget-get widget :parent)))
1342 (widget-apply parent :button-face-get)
1343 widget-button-face))))
1345 (defun widget-default-sample-face-get (widget)
1346 ;; Use :sample-face.
1347 (widget-get widget :sample-face))
1349 (defun widget-default-delete (widget)
1350 "Remove widget from the buffer."
1351 (let ((from (widget-get widget :from))
1352 (to (widget-get widget :to))
1353 (inactive-overlay (widget-get widget :inactive))
1354 (button-overlay (widget-get widget :button-overlay))
1355 (sample-overlay (widget-get widget :sample-overlay))
1356 (doc-overlay (widget-get widget :doc-overlay))
1357 before-change-functions
1358 after-change-functions
1359 (inhibit-read-only t))
1360 (widget-apply widget :value-delete)
1361 (when inactive-overlay
1362 (delete-overlay inactive-overlay))
1363 (when button-overlay
1364 (delete-overlay button-overlay))
1365 (when sample-overlay
1366 (delete-overlay sample-overlay))
1368 (delete-overlay doc-overlay))
1370 ;; Kludge: this doesn't need to be true for empty formats.
1371 (delete-region from to))
1372 (set-marker from nil)
1373 (set-marker to nil))
1374 (widget-clear-undo))
1376 (defun widget-default-value-set (widget value)
1377 "Recreate widget with new value."
1378 (let* ((old-pos (point))
1379 (from (copy-marker (widget-get widget :from)))
1380 (to (copy-marker (widget-get widget :to)))
1381 (offset (if (and (<= from old-pos) (<= old-pos to))
1382 (if (>= old-pos (1- to))
1384 (- old-pos from)))))
1385 ;;??? Bug: this ought to insert the new value before deleting the old one,
1386 ;; so that markers on either side of the value automatically
1387 ;; stay on the same side. -- rms.
1389 (goto-char (widget-get widget :from))
1390 (widget-apply widget :delete)
1391 (widget-put widget :value value)
1392 (widget-apply widget :create))
1395 (goto-char (+ (widget-get widget :to) offset 1))
1396 (goto-char (min (+ from offset) (1- (widget-get widget :to))))))))
1398 (defun widget-default-value-inline (widget)
1399 "Wrap value in a list unless it is inline."
1400 (if (widget-get widget :inline)
1401 (widget-value widget)
1402 (list (widget-value widget))))
1404 (defun widget-default-default-get (widget)
1406 (widget-get widget :value))
1408 (defun widget-default-menu-tag-get (widget)
1409 "Use tag or value for menus."
1410 (or (widget-get widget :menu-tag)
1411 (widget-get widget :tag)
1412 (widget-princ-to-string (widget-get widget :value))))
1414 (defun widget-default-active (widget)
1415 "Return t iff this widget active (user modifiable)."
1416 (or (widget-get widget :always-active)
1417 (and (not (widget-get widget :inactive))
1418 (let ((parent (widget-get widget :parent)))
1420 (widget-apply parent :active))))))
1422 (defun widget-default-deactivate (widget)
1423 "Make WIDGET inactive for user modifications."
1424 (widget-specify-inactive widget
1425 (widget-get widget :from)
1426 (widget-get widget :to)))
1428 (defun widget-default-action (widget &optional event)
1429 "Notify the parent when a widget changes."
1430 (let ((parent (widget-get widget :parent)))
1432 (widget-apply parent :notify widget event))))
1434 (defun widget-default-notify (widget child &optional event)
1435 "Pass notification to parent."
1436 (widget-default-action widget event))
1438 (defun widget-default-prompt-value (widget prompt value unbound)
1439 "Read an arbitrary value. Stolen from `set-variable'."
1440 ;; (let ((initial (if unbound
1442 ;; It would be nice if we could do a `(cons val 1)' here.
1443 ;; (prin1-to-string (custom-quote value))))))
1444 (eval-minibuffer prompt ))
1446 ;;; The `item' Widget.
1448 (define-widget 'item 'default
1449 "Constant items for inclusion in other widgets."
1450 :convert-widget 'widget-value-convert-widget
1451 :value-create 'widget-item-value-create
1452 :value-delete 'ignore
1453 :value-get 'widget-value-value-get
1454 :match 'widget-item-match
1455 :match-inline 'widget-item-match-inline
1456 :action 'widget-item-action
1459 (defun widget-item-value-create (widget)
1460 "Insert the printed representation of the value."
1461 (princ (widget-get widget :value) (current-buffer)))
1463 (defun widget-item-match (widget value)
1464 ;; Match if the value is the same.
1465 (equal (widget-get widget :value) value))
1467 (defun widget-item-match-inline (widget values)
1468 ;; Match if the value is the same.
1469 (let ((value (widget-get widget :value)))
1471 (<= (length value) (length values))
1472 (let ((head (widget-sublist values 0 (length value))))
1473 (and (equal head value)
1474 (cons head (widget-sublist values (length value))))))))
1476 (defun widget-sublist (list start &optional end)
1477 "Return the sublist of LIST from START to END.
1478 If END is omitted, it defaults to the length of LIST."
1479 (if (> start 0) (setq list (nthcdr start list)))
1481 (unless (<= end start)
1482 (setq list (copy-sequence list))
1483 (setcdr (nthcdr (- end start 1) list) nil)
1485 (copy-sequence list)))
1487 (defun widget-item-action (widget &optional event)
1488 ;; Just notify itself.
1489 (widget-apply widget :notify widget event))
1491 ;;; The `push-button' Widget.
1493 (defcustom widget-push-button-gui t
1494 "If non nil, use GUI push buttons when available."
1498 ;; Cache already created GUI objects.
1499 (defvar widget-push-button-cache nil)
1501 (defcustom widget-push-button-prefix "["
1502 "String used as prefix for buttons."
1504 :group 'widget-button)
1506 (defcustom widget-push-button-suffix "]"
1507 "String used as suffix for buttons."
1509 :group 'widget-button)
1511 (define-widget 'push-button 'item
1512 "A pushable button."
1515 :value-create 'widget-push-button-value-create
1518 (defun widget-push-button-value-create (widget)
1519 "Insert text representing the `on' and `off' states."
1520 (let* ((tag (or (widget-get widget :tag)
1521 (widget-get widget :value)))
1522 (tag-glyph (widget-get widget :tag-glyph))
1523 (text (concat widget-push-button-prefix
1524 tag widget-push-button-suffix))
1525 (gui (cdr (assoc tag widget-push-button-cache))))
1527 (widget-image-insert widget text tag-glyph))
1531 (defun widget-gui-action (widget)
1532 "Apply :action for WIDGET."
1533 (widget-apply-action widget (this-command-keys)))
1535 ;;; The `link' Widget.
1537 (defcustom widget-link-prefix "["
1538 "String used as prefix for links."
1540 :group 'widget-button)
1542 (defcustom widget-link-suffix "]"
1543 "String used as suffix for links."
1545 :group 'widget-button)
1547 (define-widget 'link 'item
1549 :button-prefix 'widget-link-prefix
1550 :button-suffix 'widget-link-suffix
1551 :help-echo "Follow the link."
1554 ;;; The `info-link' Widget.
1556 (define-widget 'info-link 'link
1557 "A link to an info file."
1558 :action 'widget-info-link-action)
1560 (defun widget-info-link-action (widget &optional event)
1561 "Open the info node specified by WIDGET."
1562 (Info-goto-node (widget-value widget)))
1564 ;;; The `url-link' Widget.
1566 (define-widget 'url-link 'link
1567 "A link to an www page."
1568 :action 'widget-url-link-action)
1570 (defun widget-url-link-action (widget &optional event)
1571 "Open the url specified by WIDGET."
1572 (browse-url (widget-value widget)))
1574 ;;; The `function-link' Widget.
1576 (define-widget 'function-link 'link
1577 "A link to an Emacs function."
1578 :action 'widget-function-link-action)
1580 (defun widget-function-link-action (widget &optional event)
1581 "Show the function specified by WIDGET."
1582 (describe-function (widget-value widget)))
1584 ;;; The `variable-link' Widget.
1586 (define-widget 'variable-link 'link
1587 "A link to an Emacs variable."
1588 :action 'widget-variable-link-action)
1590 (defun widget-variable-link-action (widget &optional event)
1591 "Show the variable specified by WIDGET."
1592 (describe-variable (widget-value widget)))
1594 ;;; The `file-link' Widget.
1596 (define-widget 'file-link 'link
1598 :action 'widget-file-link-action)
1600 (defun widget-file-link-action (widget &optional event)
1601 "Find the file specified by WIDGET."
1602 (find-file (widget-value widget)))
1604 ;;; The `emacs-library-link' Widget.
1606 (define-widget 'emacs-library-link 'link
1607 "A link to an Emacs Lisp library file."
1608 :action 'widget-emacs-library-link-action)
1610 (defun widget-emacs-library-link-action (widget &optional event)
1611 "Find the Emacs Library file specified by WIDGET."
1612 (find-file (locate-library (widget-value widget))))
1614 ;;; The `emacs-commentary-link' Widget.
1616 (define-widget 'emacs-commentary-link 'link
1617 "A link to Commentary in an Emacs Lisp library file."
1618 :action 'widget-emacs-commentary-link-action)
1620 (defun widget-emacs-commentary-link-action (widget &optional event)
1621 "Find the Commentary section of the Emacs file specified by WIDGET."
1622 (finder-commentary (widget-value widget)))
1624 ;;; The `editable-field' Widget.
1626 (define-widget 'editable-field 'default
1627 "An editable text field."
1628 :convert-widget 'widget-value-convert-widget
1629 :keymap widget-field-keymap
1632 :prompt-internal 'widget-field-prompt-internal
1633 :prompt-history 'widget-field-history
1634 :prompt-value 'widget-field-prompt-value
1635 :action 'widget-field-action
1636 :validate 'widget-field-validate
1639 :value-create 'widget-field-value-create
1640 :value-delete 'widget-field-value-delete
1641 :value-get 'widget-field-value-get
1642 :match 'widget-field-match)
1644 (defvar widget-field-history nil
1645 "History of field minibuffer edits.")
1647 (defun widget-field-prompt-internal (widget prompt initial history)
1648 "Read string for WIDGET promptinhg with PROMPT.
1649 INITIAL is the initial input and HISTORY is a symbol containing
1651 (read-string prompt initial history))
1653 (defun widget-field-prompt-value (widget prompt value unbound)
1654 "Prompt for a string."
1655 (let ((initial (if unbound
1657 (cons (widget-apply widget :value-to-internal
1659 (history (widget-get widget :prompt-history)))
1660 (let ((answer (widget-apply widget
1661 :prompt-internal prompt initial history)))
1662 (widget-apply widget :value-to-external answer))))
1664 (defvar widget-edit-functions nil)
1666 (defun widget-field-action (widget &optional event)
1667 "Move to next field."
1669 (run-hook-with-args 'widget-edit-functions widget))
1671 (defun widget-field-validate (widget)
1672 "Valid if the content matches `:valid-regexp'."
1674 (let ((value (widget-apply widget :value-get))
1675 (regexp (widget-get widget :valid-regexp)))
1676 (if (string-match regexp value)
1680 (defun widget-field-value-create (widget)
1681 "Create an editable text field."
1682 (let ((size (widget-get widget :size))
1683 (value (widget-get widget :value))
1685 ;; This is changed to a real overlay in `widget-setup'. We
1686 ;; need the end points to behave differently until
1687 ;; `widget-setup' is called.
1688 (overlay (cons (make-marker) (make-marker))))
1689 (widget-put widget :field-overlay overlay)
1692 (< (length value) size)
1693 (insert-char ?\ (- size (length value))))
1694 (unless (memq widget widget-field-list)
1695 (setq widget-field-new (cons widget widget-field-new)))
1696 (move-marker (cdr overlay) (point))
1697 (set-marker-insertion-type (cdr overlay) nil)
1700 (move-marker (car overlay) from)
1701 (set-marker-insertion-type (car overlay) t)))
1703 (defun widget-field-value-delete (widget)
1704 "Remove the widget from the list of active editing fields."
1705 (setq widget-field-list (delq widget widget-field-list))
1706 ;; These are nil if the :format string doesn't contain `%v'.
1707 (let ((overlay (widget-get widget :field-overlay)))
1709 (delete-overlay overlay))))
1711 (defun widget-field-value-get (widget)
1712 "Return current text in editing field."
1713 (let ((from (widget-field-start widget))
1714 (to (widget-field-end widget))
1715 (buffer (widget-field-buffer widget))
1716 (size (widget-get widget :size))
1717 (secret (widget-get widget :secret))
1718 (old (current-buffer)))
1725 (eq (char-after (1- to)) ?\ ))
1727 (let ((result (buffer-substring-no-properties from to)))
1730 (while (< (+ from index) to)
1732 (get-char-property (+ from index) 'secret))
1733 (setq index (1+ index)))))
1736 (widget-get widget :value))))
1738 (defun widget-field-match (widget value)
1739 ;; Match any string.
1742 ;;; The `text' Widget.
1744 (define-widget 'text 'editable-field
1745 :keymap widget-text-keymap
1746 "A multiline text area.")
1748 ;;; The `menu-choice' Widget.
1750 (define-widget 'menu-choice 'default
1751 "A menu of options."
1752 :convert-widget 'widget-types-convert-widget
1753 :format "%[%t%]: %v"
1756 :void '(item :format "invalid (%t)\n")
1757 :value-create 'widget-choice-value-create
1758 :value-delete 'widget-children-value-delete
1759 :value-get 'widget-choice-value-get
1760 :value-inline 'widget-choice-value-inline
1761 :default-get 'widget-choice-default-get
1762 :mouse-down-action 'widget-choice-mouse-down-action
1763 :action 'widget-choice-action
1764 :error "Make a choice"
1765 :validate 'widget-choice-validate
1766 :match 'widget-choice-match
1767 :match-inline 'widget-choice-match-inline)
1769 (defun widget-choice-value-create (widget)
1770 "Insert the first choice that matches the value."
1771 (let ((value (widget-get widget :value))
1772 (args (widget-get widget :args))
1773 (explicit (widget-get widget :explicit-choice))
1774 (explicit-value (widget-get widget :explicit-choice-value))
1776 (if (and explicit (equal value explicit-value))
1778 ;; If the user specified the choice for this value,
1779 ;; respect that choice as long as the value is the same.
1780 (widget-put widget :children (list (widget-create-child-value
1781 widget explicit value)))
1782 (widget-put widget :choice explicit))
1784 (setq current (car args)
1786 (when (widget-apply current :match value)
1787 (widget-put widget :children (list (widget-create-child-value
1788 widget current value)))
1789 (widget-put widget :choice current)
1793 (let ((void (widget-get widget :void)))
1794 (widget-put widget :children (list (widget-create-child-and-convert
1795 widget void :value value)))
1796 (widget-put widget :choice void))))))
1798 (defun widget-choice-value-get (widget)
1799 ;; Get value of the child widget.
1800 (widget-value (car (widget-get widget :children))))
1802 (defun widget-choice-value-inline (widget)
1803 ;; Get value of the child widget.
1804 (widget-apply (car (widget-get widget :children)) :value-inline))
1806 (defun widget-choice-default-get (widget)
1807 ;; Get default for the first choice.
1808 (widget-default-get (car (widget-get widget :args))))
1810 (defcustom widget-choice-toggle nil
1811 "If non-nil, a binary choice will just toggle between the values.
1812 Otherwise, the user will explicitly have to choose between the values
1813 when he invoked the menu."
1817 (defun widget-choice-mouse-down-action (widget &optional event)
1818 ;; Return non-nil if we need a menu.
1819 (let ((args (widget-get widget :args))
1820 (old (widget-get widget :choice)))
1821 (cond ((not (display-popup-menus-p))
1822 ;; No place to pop up a menu.
1824 ((not (or (fboundp 'x-popup-menu) (fboundp 'popup-menu)))
1825 ;; No way to pop up a menu.
1827 ((< (length args) 2)
1828 ;; Empty or singleton list, just return the value.
1830 ((> (length args) widget-menu-max-size)
1831 ;; Too long, prompt.
1833 ((> (length args) 2)
1834 ;; Reasonable sized list, use menu.
1836 ((and widget-choice-toggle (memq old args))
1840 ;; Ask which of the two.
1843 (defun widget-choice-action (widget &optional event)
1845 (let ((args (widget-get widget :args))
1846 (old (widget-get widget :choice))
1847 (tag (widget-apply widget :menu-tag-get))
1848 (completion-ignore-case (widget-get widget :case-fold))
1851 ;; Remember old value.
1852 (if (and old (not (widget-apply widget :validate)))
1853 (let* ((external (widget-value widget))
1854 (internal (widget-apply old :value-to-internal external)))
1855 (widget-put old :value internal)))
1858 (cond ((= (length args) 0)
1860 ((= (length args) 1)
1862 ((and widget-choice-toggle
1865 (if (eq old (nth 0 args))
1870 (setq current (car args)
1873 (cons (cons (widget-apply current :menu-tag-get)
1876 (setq this-explicit t)
1877 (widget-choose tag (reverse choices) event))))
1879 ;; If this was an explicit user choice,
1880 ;; record the choice, and the record the value it was made for.
1881 ;; widget-choice-value-create will respect this choice,
1882 ;; as long as the value is the same.
1884 (widget-put widget :explicit-choice current)
1885 (widget-put widget :explicit-choice-value (widget-get widget :value)))
1886 (let ((value (widget-default-get current)))
1887 (widget-value-set widget
1888 (widget-apply current :value-to-external value)))
1890 (widget-apply widget :notify widget event)))
1891 (run-hook-with-args 'widget-edit-functions widget))
1893 (defun widget-choice-validate (widget)
1894 ;; Valid if we have made a valid choice.
1895 (let ((void (widget-get widget :void))
1896 (choice (widget-get widget :choice))
1897 (child (car (widget-get widget :children))))
1898 (if (eq void choice)
1900 (widget-apply child :validate))))
1902 (defun widget-choice-match (widget value)
1903 ;; Matches if one of the choices matches.
1904 (let ((args (widget-get widget :args))
1906 (while (and args (not found))
1907 (setq current (car args)
1909 found (widget-apply current :match value)))
1912 (defun widget-choice-match-inline (widget values)
1913 ;; Matches if one of the choices matches.
1914 (let ((args (widget-get widget :args))
1916 (while (and args (null found))
1917 (setq current (car args)
1919 found (widget-match-inline current values)))
1922 ;;; The `toggle' Widget.
1924 (define-widget 'toggle 'item
1925 "Toggle between two states."
1927 :value-create 'widget-toggle-value-create
1928 :action 'widget-toggle-action
1929 :match (lambda (widget value) t)
1933 (defun widget-toggle-value-create (widget)
1934 "Insert text representing the `on' and `off' states."
1935 (if (widget-value widget)
1936 (widget-image-insert widget
1937 (widget-get widget :on)
1938 (widget-get widget :on-glyph))
1939 (widget-image-insert widget
1940 (widget-get widget :off)
1941 (widget-get widget :off-glyph))))
1943 (defun widget-toggle-action (widget &optional event)
1945 (widget-value-set widget (not (widget-value widget)))
1946 (widget-apply widget :notify widget event)
1947 (run-hook-with-args 'widget-edit-functions widget))
1949 ;;; The `checkbox' Widget.
1951 (define-widget 'checkbox 'toggle
1952 "A checkbox toggle."
1957 ;; We could probably do the same job as the images using single
1958 ;; space characters in a boxed face with a stretch specification to
1959 ;; make them square.
1960 :on-glyph (create-image (make-bool-vector 49 1)
1961 'xbm t :width 7 :height 7
1962 :foreground "grey75" ; like default mode line
1963 :relief -3 :ascent 'center)
1965 :off-glyph (create-image (make-bool-vector 49 1)
1966 'xbm t :width 7 :height 7
1967 :foreground "grey75"
1968 :relief 3 :ascent 'center)
1969 :help-echo "Toggle this item."
1970 :action 'widget-checkbox-action)
1972 (defun widget-checkbox-action (widget &optional event)
1973 "Toggle checkbox, notify parent, and set active state of sibling."
1974 (widget-toggle-action widget event)
1975 (let ((sibling (widget-get-sibling widget)))
1977 (if (widget-value widget)
1978 (widget-apply sibling :activate)
1979 (widget-apply sibling :deactivate)))))
1981 ;;; The `checklist' Widget.
1983 (define-widget 'checklist 'default
1984 "A multiple choice widget."
1985 :convert-widget 'widget-types-convert-widget
1988 :entry-format "%b %v"
1989 :menu-tag "checklist"
1991 :value-create 'widget-checklist-value-create
1992 :value-delete 'widget-children-value-delete
1993 :value-get 'widget-checklist-value-get
1994 :validate 'widget-checklist-validate
1995 :match 'widget-checklist-match
1996 :match-inline 'widget-checklist-match-inline)
1998 (defun widget-checklist-value-create (widget)
1999 ;; Insert all values
2000 (let ((alist (widget-checklist-match-find widget (widget-get widget :value)))
2001 (args (widget-get widget :args)))
2003 (widget-checklist-add-item widget (car args) (assq (car args) alist))
2004 (setq args (cdr args)))
2005 (widget-put widget :children (nreverse (widget-get widget :children)))))
2007 (defun widget-checklist-add-item (widget type chosen)
2008 "Create checklist item in WIDGET of type TYPE.
2009 If the item is checked, CHOSEN is a cons whose cdr is the value."
2010 (and (eq (preceding-char) ?\n)
2011 (widget-get widget :indent)
2012 (insert-char ? (widget-get widget :indent)))
2013 (widget-specify-insert
2014 (let* ((children (widget-get widget :children))
2015 (buttons (widget-get widget :buttons))
2016 (button-args (or (widget-get type :sibling-args)
2017 (widget-get widget :button-args)))
2020 (insert (widget-get widget :entry-format))
2022 ;; Parse % escapes in format.
2023 (while (re-search-forward "%\\([bv%]\\)" nil t)
2024 (let ((escape (aref (match-string 1) 0)))
2025 (replace-match "" t t)
2026 (cond ((eq escape ?%)
2029 (setq button (apply 'widget-create-child-and-convert
2031 :value (not (null chosen))
2036 (let ((child (widget-create-child widget type)))
2037 (widget-apply child :deactivate)
2039 ((widget-get type :inline)
2040 (widget-create-child-value
2041 widget type (cdr chosen)))
2043 (widget-create-child-value
2044 widget type (car (cdr chosen)))))))
2046 (error "Unknown escape `%c'" escape)))))
2047 ;; Update properties.
2048 (and button child (widget-put child :button button))
2049 (and button (widget-put widget :buttons (cons button buttons)))
2050 (and child (widget-put widget :children (cons child children))))))
2052 (defun widget-checklist-match (widget values)
2053 ;; All values must match a type in the checklist.
2055 (null (cdr (widget-checklist-match-inline widget values)))))
2057 (defun widget-checklist-match-inline (widget values)
2058 ;; Find the values which match a type in the checklist.
2059 (let ((greedy (widget-get widget :greedy))
2060 (args (copy-sequence (widget-get widget :args)))
2063 (let ((answer (widget-checklist-match-up args values)))
2065 (let ((vals (widget-match-inline answer values)))
2066 (setq found (append found (car vals))
2068 args (delq answer args))))
2070 (setq rest (append rest (list (car values)))
2071 values (cdr values)))
2073 (setq rest (append rest values)
2077 (defun widget-checklist-match-find (widget vals)
2078 "Find the vals which match a type in the checklist.
2079 Return an alist of (TYPE MATCH)."
2080 (let ((greedy (widget-get widget :greedy))
2081 (args (copy-sequence (widget-get widget :args)))
2084 (let ((answer (widget-checklist-match-up args vals)))
2086 (let ((match (widget-match-inline answer vals)))
2087 (setq found (cons (cons answer (car match)) found)
2089 args (delq answer args))))
2091 (setq vals (cdr vals)))
2096 (defun widget-checklist-match-up (args vals)
2097 "Return the first type from ARGS that matches VALS."
2098 (let (current found)
2099 (while (and args (null found))
2100 (setq current (car args)
2102 found (widget-match-inline current vals)))
2106 (defun widget-checklist-value-get (widget)
2107 ;; The values of all selected items.
2108 (let ((children (widget-get widget :children))
2111 (setq child (car children)
2112 children (cdr children))
2113 (if (widget-value (widget-get child :button))
2114 (setq result (append result (widget-apply child :value-inline)))))
2117 (defun widget-checklist-validate (widget)
2118 ;; Ticked chilren must be valid.
2119 (let ((children (widget-get widget :children))
2121 (while (and children (not found))
2122 (setq child (car children)
2123 children (cdr children)
2124 button (widget-get child :button)
2125 found (and (widget-value button)
2126 (widget-apply child :validate))))
2129 ;;; The `option' Widget
2131 (define-widget 'option 'checklist
2132 "An widget with an optional item."
2135 ;;; The `choice-item' Widget.
2137 (define-widget 'choice-item 'item
2138 "Button items that delegate action events to their parents."
2139 :action 'widget-parent-action
2140 :format "%[%t%] \n")
2142 ;;; The `radio-button' Widget.
2144 (define-widget 'radio-button 'toggle
2145 "A radio button for use in the `radio' widget."
2146 :notify 'widget-radio-button-notify
2153 :off-glyph "radio0")
2155 (defun widget-radio-button-notify (widget child &optional event)
2157 (widget-apply (widget-get widget :parent) :action widget event))
2159 ;;; The `radio-button-choice' Widget.
2161 (define-widget 'radio-button-choice 'default
2162 "Select one of multiple options."
2163 :convert-widget 'widget-types-convert-widget
2166 :entry-format "%b %v"
2168 :value-create 'widget-radio-value-create
2169 :value-delete 'widget-children-value-delete
2170 :value-get 'widget-radio-value-get
2171 :value-inline 'widget-radio-value-inline
2172 :value-set 'widget-radio-value-set
2173 :error "You must push one of the buttons"
2174 :validate 'widget-radio-validate
2175 :match 'widget-choice-match
2176 :match-inline 'widget-choice-match-inline
2177 :action 'widget-radio-action)
2179 (defun widget-radio-value-create (widget)
2180 ;; Insert all values
2181 (let ((args (widget-get widget :args))
2184 (setq arg (car args)
2186 (widget-radio-add-item widget arg))))
2188 (defun widget-radio-add-item (widget type)
2189 "Add to radio widget WIDGET a new radio button item of type TYPE."
2190 ;; (setq type (widget-convert type))
2191 (and (eq (preceding-char) ?\n)
2192 (widget-get widget :indent)
2193 (insert-char ? (widget-get widget :indent)))
2194 (widget-specify-insert
2195 (let* ((value (widget-get widget :value))
2196 (children (widget-get widget :children))
2197 (buttons (widget-get widget :buttons))
2198 (button-args (or (widget-get type :sibling-args)
2199 (widget-get widget :button-args)))
2201 (chosen (and (null (widget-get widget :choice))
2202 (widget-apply type :match value)))
2204 (insert (widget-get widget :entry-format))
2206 ;; Parse % escapes in format.
2207 (while (re-search-forward "%\\([bv%]\\)" nil t)
2208 (let ((escape (aref (match-string 1) 0)))
2209 (replace-match "" t t)
2210 (cond ((eq escape ?%)
2213 (setq button (apply 'widget-create-child-and-convert
2214 widget 'radio-button
2215 :value (not (null chosen))
2218 (setq child (if chosen
2219 (widget-create-child-value
2221 (widget-create-child widget type)))
2223 (widget-apply child :deactivate)))
2225 (error "Unknown escape `%c'" escape)))))
2226 ;; Update properties.
2228 (widget-put widget :choice type))
2230 (widget-put child :button button)
2231 (widget-put widget :buttons (nconc buttons (list button))))
2233 (widget-put widget :children (nconc children (list child))))
2236 (defun widget-radio-value-get (widget)
2237 ;; Get value of the child widget.
2238 (let ((chosen (widget-radio-chosen widget)))
2239 (and chosen (widget-value chosen))))
2241 (defun widget-radio-chosen (widget)
2242 "Return the widget representing the chosen radio button."
2243 (let ((children (widget-get widget :children))
2246 (setq current (car children)
2247 children (cdr children))
2248 (let* ((button (widget-get current :button))
2249 (value (widget-apply button :value-get)))
2255 (defun widget-radio-value-inline (widget)
2256 ;; Get value of the child widget.
2257 (let ((children (widget-get widget :children))
2260 (setq current (car children)
2261 children (cdr children))
2262 (let* ((button (widget-get current :button))
2263 (value (widget-apply button :value-get)))
2265 (setq found (widget-apply current :value-inline)
2269 (defun widget-radio-value-set (widget value)
2270 ;; We can't just delete and recreate a radio widget, since children
2271 ;; can be added after the original creation and won't be recreated
2273 (let ((children (widget-get widget :children))
2276 (setq current (car children)
2277 children (cdr children))
2278 (let* ((button (widget-get current :button))
2279 (match (and (not found)
2280 (widget-apply current :match value))))
2281 (widget-value-set button match)
2284 (widget-value-set current value)
2285 (widget-apply current :activate))
2286 (widget-apply current :deactivate))
2287 (setq found (or found match))))))
2289 (defun widget-radio-validate (widget)
2290 ;; Valid if we have made a valid choice.
2291 (let ((children (widget-get widget :children))
2292 current found button)
2293 (while (and children (not found))
2294 (setq current (car children)
2295 children (cdr children)
2296 button (widget-get current :button)
2297 found (widget-apply button :value-get)))
2299 (widget-apply current :validate)
2302 (defun widget-radio-action (widget child event)
2303 ;; Check if a radio button was pressed.
2304 (let ((children (widget-get widget :children))
2305 (buttons (widget-get widget :buttons))
2307 (when (memq child buttons)
2309 (setq current (car children)
2310 children (cdr children))
2311 (let* ((button (widget-get current :button)))
2312 (cond ((eq child button)
2313 (widget-value-set button t)
2314 (widget-apply current :activate))
2315 ((widget-value button)
2316 (widget-value-set button nil)
2317 (widget-apply current :deactivate)))))))
2318 ;; Pass notification to parent.
2319 (widget-apply widget :notify child event))
2321 ;;; The `insert-button' Widget.
2323 (define-widget 'insert-button 'push-button
2324 "An insert button for the `editable-list' widget."
2326 :help-echo "Insert a new item into the list at this position."
2327 :action 'widget-insert-button-action)
2329 (defun widget-insert-button-action (widget &optional event)
2330 ;; Ask the parent to insert a new item.
2331 (widget-apply (widget-get widget :parent)
2332 :insert-before (widget-get widget :widget)))
2334 ;;; The `delete-button' Widget.
2336 (define-widget 'delete-button 'push-button
2337 "A delete button for the `editable-list' widget."
2339 :help-echo "Delete this item from the list."
2340 :action 'widget-delete-button-action)
2342 (defun widget-delete-button-action (widget &optional event)
2343 ;; Ask the parent to insert a new item.
2344 (widget-apply (widget-get widget :parent)
2345 :delete-at (widget-get widget :widget)))
2347 ;;; The `editable-list' Widget.
2349 (defcustom widget-editable-list-gui nil
2350 "If non nil, use GUI push-buttons in editable list when available."
2354 (define-widget 'editable-list 'default
2355 "A variable list of widgets of the same type."
2356 :convert-widget 'widget-types-convert-widget
2359 :format-handler 'widget-editable-list-format-handler
2360 :entry-format "%i %d %v"
2361 :menu-tag "editable-list"
2362 :value-create 'widget-editable-list-value-create
2363 :value-delete 'widget-children-value-delete
2364 :value-get 'widget-editable-list-value-get
2365 :validate 'widget-children-validate
2366 :match 'widget-editable-list-match
2367 :match-inline 'widget-editable-list-match-inline
2368 :insert-before 'widget-editable-list-insert-before
2369 :delete-at 'widget-editable-list-delete-at)
2371 (defun widget-editable-list-format-handler (widget escape)
2372 ;; We recognize the insert button.
2373 (let ((widget-push-button-gui widget-editable-list-gui))
2374 (cond ((eq escape ?i)
2375 (and (widget-get widget :indent)
2376 (insert-char ? (widget-get widget :indent)))
2377 (apply 'widget-create-child-and-convert
2378 widget 'insert-button
2379 (widget-get widget :append-button-args)))
2381 (widget-default-format-handler widget escape)))))
2383 (defun widget-editable-list-value-create (widget)
2384 ;; Insert all values
2385 (let* ((value (widget-get widget :value))
2386 (type (nth 0 (widget-get widget :args)))
2387 (inlinep (widget-get type :inline))
2389 (widget-put widget :value-pos (copy-marker (point)))
2390 (set-marker-insertion-type (widget-get widget :value-pos) t)
2392 (let ((answer (widget-match-inline type value)))
2394 (setq children (cons (widget-editable-list-entry-create
2403 (widget-put widget :children (nreverse children))))
2405 (defun widget-editable-list-value-get (widget)
2406 ;; Get value of the child widget.
2407 (apply 'append (mapcar (lambda (child) (widget-apply child :value-inline))
2408 (widget-get widget :children))))
2410 (defun widget-editable-list-match (widget value)
2411 ;; Value must be a list and all the members must match the type.
2413 (null (cdr (widget-editable-list-match-inline widget value)))))
2415 (defun widget-editable-list-match-inline (widget value)
2416 (let ((type (nth 0 (widget-get widget :args)))
2419 (while (and value ok)
2420 (let ((answer (widget-match-inline type value)))
2422 (setq found (append found (car answer))
2425 (cons found value)))
2427 (defun widget-editable-list-insert-before (widget before)
2428 ;; Insert a new child in the list of children.
2430 (let ((children (widget-get widget :children))
2431 (inhibit-read-only t)
2432 before-change-functions
2433 after-change-functions)
2435 (goto-char (widget-get before :entry-from)))
2437 (goto-char (widget-get widget :value-pos))))
2438 (let ((child (widget-editable-list-entry-create
2440 (when (< (widget-get child :entry-from) (widget-get widget :from))
2441 (set-marker (widget-get widget :from)
2442 (widget-get child :entry-from)))
2443 (if (eq (car children) before)
2444 (widget-put widget :children (cons child children))
2445 (while (not (eq (car (cdr children)) before))
2446 (setq children (cdr children)))
2447 (setcdr children (cons child (cdr children)))))))
2449 (widget-apply widget :notify widget))
2451 (defun widget-editable-list-delete-at (widget child)
2452 ;; Delete child from list of children.
2454 (let ((buttons (copy-sequence (widget-get widget :buttons)))
2456 (inhibit-read-only t)
2457 before-change-functions
2458 after-change-functions)
2460 (setq button (car buttons)
2461 buttons (cdr buttons))
2462 (when (eq (widget-get button :widget) child)
2464 :buttons (delq button (widget-get widget :buttons)))
2465 (widget-delete button))))
2466 (let ((entry-from (widget-get child :entry-from))
2467 (entry-to (widget-get child :entry-to))
2468 (inhibit-read-only t)
2469 before-change-functions
2470 after-change-functions)
2471 (widget-delete child)
2472 (delete-region entry-from entry-to)
2473 (set-marker entry-from nil)
2474 (set-marker entry-to nil))
2475 (widget-put widget :children (delq child (widget-get widget :children))))
2477 (widget-apply widget :notify widget))
2479 (defun widget-editable-list-entry-create (widget value conv)
2480 ;; Create a new entry to the list.
2481 (let ((type (nth 0 (widget-get widget :args)))
2482 (widget-push-button-gui widget-editable-list-gui)
2483 child delete insert)
2484 (widget-specify-insert
2486 (and (widget-get widget :indent)
2487 (insert-char ? (widget-get widget :indent)))
2488 (insert (widget-get widget :entry-format)))
2489 ;; Parse % escapes in format.
2490 (while (re-search-forward "%\\(.\\)" nil t)
2491 (let ((escape (aref (match-string 1) 0)))
2492 (replace-match "" t t)
2493 (cond ((eq escape ?%)
2496 (setq insert (apply 'widget-create-child-and-convert
2497 widget 'insert-button
2498 (widget-get widget :insert-button-args))))
2500 (setq delete (apply 'widget-create-child-and-convert
2501 widget 'delete-button
2502 (widget-get widget :delete-button-args))))
2505 (setq child (widget-create-child-value
2507 (setq child (widget-create-child-value
2509 (widget-apply type :value-to-external
2510 (widget-default-get type))))))
2512 (error "Unknown escape `%c'" escape)))))
2514 :buttons (cons delete
2516 (widget-get widget :buttons))))
2517 (let ((entry-from (copy-marker (point-min)))
2518 (entry-to (copy-marker (point-max))))
2519 (set-marker-insertion-type entry-from t)
2520 (set-marker-insertion-type entry-to nil)
2521 (widget-put child :entry-from entry-from)
2522 (widget-put child :entry-to entry-to)))
2523 (widget-put insert :widget child)
2524 (widget-put delete :widget child)
2527 ;;; The `group' Widget.
2529 (define-widget 'group 'default
2530 "A widget which groups other widgets inside."
2531 :convert-widget 'widget-types-convert-widget
2533 :value-create 'widget-group-value-create
2534 :value-delete 'widget-children-value-delete
2535 :value-get 'widget-editable-list-value-get
2536 :default-get 'widget-group-default-get
2537 :validate 'widget-children-validate
2538 :match 'widget-group-match
2539 :match-inline 'widget-group-match-inline)
2541 (defun widget-group-value-create (widget)
2542 ;; Create each component.
2543 (let ((args (widget-get widget :args))
2544 (value (widget-get widget :value))
2545 arg answer children)
2547 (setq arg (car args)
2549 answer (widget-match-inline arg value)
2551 (and (eq (preceding-char) ?\n)
2552 (widget-get widget :indent)
2553 (insert-char ? (widget-get widget :indent)))
2554 (push (cond ((null answer)
2555 (widget-create-child widget arg))
2556 ((widget-get arg :inline)
2557 (widget-create-child-value widget arg (car answer)))
2559 (widget-create-child-value widget arg (car (car answer)))))
2561 (widget-put widget :children (nreverse children))))
2563 (defun widget-group-default-get (widget)
2564 ;; Get the default of the components.
2565 (mapcar 'widget-default-get (widget-get widget :args)))
2567 (defun widget-group-match (widget values)
2568 ;; Match if the components match.
2570 (let ((match (widget-group-match-inline widget values)))
2571 (and match (null (cdr match))))))
2573 (defun widget-group-match-inline (widget vals)
2574 ;; Match if the components match.
2575 (let ((args (widget-get widget :args))
2576 argument answer found)
2578 (setq argument (car args)
2580 answer (widget-match-inline argument vals))
2582 (setq vals (cdr answer)
2583 found (append found (car answer)))
2587 (cons found vals))))
2589 ;;; The `visibility' Widget.
2591 (define-widget 'visibility 'item
2592 "An indicator and manipulator for hidden items."
2598 :value-create 'widget-visibility-value-create
2599 :action 'widget-toggle-action
2600 :match (lambda (widget value) t))
2602 (defun widget-visibility-value-create (widget)
2603 ;; Insert text representing the `on' and `off' states.
2604 (let ((on (widget-get widget :on))
2605 (off (widget-get widget :off)))
2607 (setq on (concat widget-push-button-prefix
2609 widget-push-button-suffix))
2612 (setq off (concat widget-push-button-prefix
2614 widget-push-button-suffix))
2616 (if (widget-value widget)
2617 (widget-image-insert widget on "down" "down-pushed")
2618 (widget-image-insert widget off "right" "right-pushed"))))
2620 ;;; The `documentation-link' Widget.
2622 ;; This is a helper widget for `documentation-string'.
2624 (define-widget 'documentation-link 'link
2625 "Link type used in documentation strings."
2627 :help-echo "Describe this symbol"
2628 :action 'widget-documentation-link-action)
2630 (defun widget-documentation-link-action (widget &optional event)
2631 "Display documentation for WIDGET's value. Ignore optional argument EVENT."
2632 (let* ((string (widget-get widget :value))
2633 (symbol (intern string)))
2634 (if (and (fboundp symbol) (boundp symbol))
2635 ;; If there are two doc strings, give the user a way to pick one.
2636 (apropos (concat "\\`" (regexp-quote string) "\\'"))
2637 (if (fboundp symbol)
2638 (describe-function symbol)
2639 (describe-variable symbol)))))
2641 (defcustom widget-documentation-links t
2642 "Add hyperlinks to documentation strings when non-nil."
2644 :group 'widget-documentation)
2646 (defcustom widget-documentation-link-regexp "`\\([^\n`' ]+\\)'"
2647 "Regexp for matching potential links in documentation strings.
2648 The first group should be the link itself."
2650 :group 'widget-documentation)
2652 (defcustom widget-documentation-link-p 'intern-soft
2653 "Predicate used to test if a string is useful as a link.
2654 The value should be a function. The function will be called one
2655 argument, a string, and should return non-nil if there should be a
2656 link for that string."
2658 :options '(widget-documentation-link-p)
2659 :group 'widget-documentation)
2661 (defcustom widget-documentation-link-type 'documentation-link
2662 "Widget type used for links in documentation strings."
2664 :group 'widget-documentation)
2666 (defun widget-documentation-link-add (widget from to)
2667 (widget-specify-doc widget from to)
2668 (when widget-documentation-links
2669 (let ((regexp widget-documentation-link-regexp)
2670 (predicate widget-documentation-link-p)
2671 (type widget-documentation-link-type)
2672 (buttons (widget-get widget :buttons))
2673 (widget-mouse-face (default-value 'widget-mouse-face))
2674 (widget-button-face widget-documentation-face)
2675 (widget-button-pressed-face widget-documentation-face))
2678 (while (re-search-forward regexp to t)
2679 (let ((name (match-string 1))
2680 (begin (match-beginning 1))
2681 (end (match-end 1)))
2682 (when (funcall predicate name)
2683 (push (widget-convert-button type begin end :value name)
2685 (widget-put widget :buttons buttons)))
2686 (let ((indent (widget-get widget :indent)))
2687 (when (and indent (not (zerop indent)))
2690 (narrow-to-region from to)
2691 (goto-char (point-min))
2692 (while (search-forward "\n" nil t)
2693 (insert-char ?\ indent)))))))
2695 ;;; The `documentation-string' Widget.
2697 (define-widget 'documentation-string 'item
2698 "A documentation string."
2700 :action 'widget-documentation-string-action
2701 :value-delete 'widget-children-value-delete
2702 :value-create 'widget-documentation-string-value-create)
2704 (defun widget-documentation-string-value-create (widget)
2705 ;; Insert documentation string.
2706 (let ((doc (widget-value widget))
2707 (indent (widget-get widget :indent))
2708 (shown (widget-get (widget-get widget :parent) :documentation-shown))
2710 (if (string-match "\n" doc)
2711 (let ((before (substring doc 0 (match-beginning 0)))
2712 (after (substring doc (match-beginning 0)))
2715 (widget-documentation-link-add widget start (point))
2716 (push (widget-create-child-and-convert
2718 :help-echo "Show or hide rest of the documentation."
2721 :action 'widget-parent-action
2725 (setq start (point))
2726 (when (and indent (not (zerop indent)))
2727 (insert-char ?\ indent))
2729 (widget-documentation-link-add widget start (point)))
2730 (widget-put widget :buttons buttons))
2732 (widget-documentation-link-add widget start (point))))
2735 (defun widget-documentation-string-action (widget &rest ignore)
2736 ;; Toggle documentation.
2737 (let ((parent (widget-get widget :parent)))
2738 (widget-put parent :documentation-shown
2739 (not (widget-get parent :documentation-shown))))
2741 (widget-value-set widget (widget-value widget)))
2743 ;;; The Sexp Widgets.
2745 (define-widget 'const 'item
2746 "An immutable sexp."
2747 :prompt-value 'widget-const-prompt-value
2750 (defun widget-const-prompt-value (widget prompt value unbound)
2751 ;; Return the value of the const.
2752 (widget-value widget))
2754 (define-widget 'function-item 'const
2755 "An immutable function name."
2757 :documentation-property (lambda (symbol)
2759 (documentation symbol t)
2762 (define-widget 'variable-item 'const
2763 "An immutable variable name."
2765 :documentation-property 'variable-documentation)
2767 (define-widget 'other 'sexp
2768 "Matches any value, but doesn't let the user edit the value.
2769 This is useful as last item in a `choice' widget.
2770 You should use this widget type with a default value,
2771 as in (other DEFAULT) or (other :tag \"NAME\" DEFAULT).
2772 If the user selects this alternative, that specifies DEFAULT
2778 (defvar widget-string-prompt-value-history nil
2779 "History of input to `widget-string-prompt-value'.")
2781 (define-widget 'string 'editable-field
2784 :format "%{%t%}: %v"
2785 :complete-function 'ispell-complete-word
2786 :prompt-history 'widget-string-prompt-value-history)
2788 (define-widget 'regexp 'string
2789 "A regular expression."
2790 :match 'widget-regexp-match
2791 :validate 'widget-regexp-validate
2792 ;; Doesn't work well with terminating newline.
2793 ;; :value-face 'widget-single-line-field-face
2796 (defun widget-regexp-match (widget value)
2797 ;; Match valid regexps.
2798 (and (stringp value)
2801 (string-match value ""))
2804 (defun widget-regexp-validate (widget)
2805 "Check that the value of WIDGET is a valid regexp."
2806 (let ((val (widget-value widget)))
2807 (condition-case data
2809 (string-match val ""))
2810 (error (widget-put widget :error (error-message-string data))
2813 (define-widget 'file 'string
2815 It will read a file name from the minibuffer when invoked."
2816 :complete-function 'widget-file-complete
2817 :prompt-value 'widget-file-prompt-value
2818 :format "%{%t%}: %v"
2819 ;; Doesn't work well with terminating newline.
2820 ;; :value-face 'widget-single-line-field-face
2823 (defun widget-file-complete ()
2824 "Perform completion on file name preceding point."
2826 (let* ((end (point))
2827 (beg (save-excursion
2828 (skip-chars-backward "^ ")
2830 (pattern (buffer-substring beg end))
2831 (name-part (file-name-nondirectory pattern))
2832 (directory (file-name-directory pattern))
2833 (completion (file-name-completion name-part directory)))
2834 (cond ((eq completion t))
2836 (message "Can't find completion for \"%s\"" pattern)
2838 ((not (string= name-part completion))
2839 (delete-region beg end)
2840 (insert (expand-file-name completion directory)))
2842 (message "Making completion list...")
2843 (let ((list (file-name-all-completions name-part directory)))
2844 (setq list (sort list 'string<))
2845 (with-output-to-temp-buffer "*Completions*"
2846 (display-completion-list list)))
2847 (message "Making completion list...%s" "done")))))
2849 (defun widget-file-prompt-value (widget prompt value unbound)
2850 ;; Read file from minibuffer.
2851 (abbreviate-file-name
2853 (read-file-name prompt)
2854 (let ((prompt2 (format "%s (default %s) " prompt value))
2855 (dir (file-name-directory value))
2856 (file (file-name-nondirectory value))
2857 (must-match (widget-get widget :must-match)))
2858 (read-file-name prompt2 dir nil must-match file)))))
2860 ;;;(defun widget-file-action (widget &optional event)
2861 ;;; ;; Read a file name from the minibuffer.
2862 ;;; (let* ((value (widget-value widget))
2863 ;;; (dir (file-name-directory value))
2864 ;;; (file (file-name-nondirectory value))
2865 ;;; (menu-tag (widget-apply widget :menu-tag-get))
2866 ;;; (must-match (widget-get widget :must-match))
2867 ;;; (answer (read-file-name (concat menu-tag ": (default `" value "') ")
2868 ;;; dir nil must-match file)))
2869 ;;; (widget-value-set widget (abbreviate-file-name answer))
2871 ;;; (widget-apply widget :notify widget event)))
2873 (define-widget 'directory 'file
2874 "A directory widget.
2875 It will read a directory name from the minibuffer when invoked."
2878 (defvar widget-symbol-prompt-value-history nil
2879 "History of input to `widget-symbol-prompt-value'.")
2881 (define-widget 'symbol 'editable-field
2885 :format "%{%t%}: %v"
2886 :match (lambda (widget value) (symbolp value))
2887 :complete-function 'lisp-complete-symbol
2888 :prompt-internal 'widget-symbol-prompt-internal
2889 :prompt-match 'symbolp
2890 :prompt-history 'widget-symbol-prompt-value-history
2891 :value-to-internal (lambda (widget value)
2895 :value-to-external (lambda (widget value)
2900 (defun widget-symbol-prompt-internal (widget prompt initial history)
2901 ;; Read file from minibuffer.
2902 (let ((answer (completing-read prompt obarray
2903 (widget-get widget :prompt-match)
2904 nil initial history)))
2905 (if (and (stringp answer)
2906 (not (zerop (length answer))))
2908 (error "No value"))))
2910 (defvar widget-function-prompt-value-history nil
2911 "History of input to `widget-function-prompt-value'.")
2913 (define-widget 'function 'sexp
2915 :complete-function 'lisp-complete-symbol
2916 :prompt-value 'widget-field-prompt-value
2917 :prompt-internal 'widget-symbol-prompt-internal
2918 :prompt-match 'fboundp
2919 :prompt-history 'widget-function-prompt-value-history
2920 :action 'widget-field-action
2923 (defvar widget-variable-prompt-value-history nil
2924 "History of input to `widget-variable-prompt-value'.")
2926 (define-widget 'variable 'symbol
2927 ;; Should complete on variables.
2929 :prompt-match 'boundp
2930 :prompt-history 'widget-variable-prompt-value-history
2933 (defvar widget-coding-system-prompt-value-history nil
2934 "History of input to `widget-coding-system-prompt-value'.")
2936 (define-widget 'coding-system 'symbol
2937 "A MULE coding-system."
2938 :format "%{%t%}: %v"
2939 :tag "Coding system"
2940 :prompt-history 'widget-coding-system-prompt-value-history
2941 :prompt-value 'widget-coding-system-prompt-value
2942 :action 'widget-coding-system-action)
2944 (defun widget-coding-system-prompt-value (widget prompt value unbound)
2945 ;; Read coding-system from minibuffer.
2947 (completing-read (format "%s (default %s) " prompt value)
2948 (mapcar (lambda (sym)
2949 (list (symbol-name sym)))
2950 (coding-system-list)))))
2952 (defun widget-coding-system-action (widget &optional event)
2953 ;; Read a file name from the minibuffer.
2955 (widget-coding-system-prompt-value
2957 (widget-apply widget :menu-tag-get)
2958 (widget-value widget)
2960 (widget-value-set widget answer)
2961 (widget-apply widget :notify widget event)
2964 (define-widget 'sexp 'editable-field
2965 "An arbitrary Lisp expression."
2966 :tag "Lisp expression"
2967 :format "%{%t%}: %v"
2969 :validate 'widget-sexp-validate
2970 :match (lambda (widget value) t)
2971 :value-to-internal 'widget-sexp-value-to-internal
2972 :value-to-external (lambda (widget value) (read value))
2973 :prompt-history 'widget-sexp-prompt-value-history
2974 :prompt-value 'widget-sexp-prompt-value)
2976 (defun widget-sexp-value-to-internal (widget value)
2977 ;; Use pp for printer representation.
2978 (let ((pp (if (symbolp value)
2979 (prin1-to-string value)
2980 (pp-to-string value))))
2981 (while (string-match "\n\\'" pp)
2982 (setq pp (substring pp 0 -1)))
2983 (if (or (string-match "\n\\'" pp)
2988 (defun widget-sexp-validate (widget)
2989 ;; Valid if we can read the string and there is no junk left after it.
2991 (insert (widget-apply widget :value-get))
2992 (goto-char (point-min))
2993 (condition-case data
2995 ;; Avoid a confusing end-of-file error.
2996 (skip-syntax-forward "\\s-")
2998 (error "Empty sexp -- use `nil'?"))
2999 (let ((value (read (current-buffer))))
3001 (if (widget-apply widget :match value)
3003 (widget-put widget :error (widget-get widget :type-error))
3006 :error (format "Junk at end of expression: %s"
3007 (buffer-substring (point)
3010 (end-of-file ; Avoid confusing error message.
3011 (widget-put widget :error "Unbalanced sexp")
3013 (error (widget-put widget :error (error-message-string data))
3016 (defvar widget-sexp-prompt-value-history nil
3017 "History of input to `widget-sexp-prompt-value'.")
3019 (defun widget-sexp-prompt-value (widget prompt value unbound)
3020 ;; Read an arbitrary sexp.
3021 (let ((found (read-string prompt
3022 (if unbound nil (cons (prin1-to-string value) 0))
3023 (widget-get widget :prompt-history))))
3024 (let ((answer (read-from-string found)))
3025 (unless (= (cdr answer) (length found))
3026 (error "Junk at end of expression: %s"
3027 (substring found (cdr answer))))
3030 (define-widget 'restricted-sexp 'sexp
3031 "A Lisp expression restricted to values that match.
3032 To use this type, you must define :match or :match-alternatives."
3033 :type-error "The specified value is not valid"
3034 :match 'widget-restricted-sexp-match
3035 :value-to-internal (lambda (widget value)
3036 (if (widget-apply widget :match value)
3037 (prin1-to-string value)
3040 (defun widget-restricted-sexp-match (widget value)
3041 (let ((alternatives (widget-get widget :match-alternatives))
3043 (while (and alternatives (not matched))
3044 (if (cond ((functionp (car alternatives))
3045 (funcall (car alternatives) value))
3046 ((and (consp (car alternatives))
3047 (eq (car (car alternatives)) 'quote))
3048 (eq value (nth 1 (car alternatives)))))
3050 (setq alternatives (cdr alternatives)))
3053 (define-widget 'integer 'restricted-sexp
3057 :type-error "This field should contain an integer"
3058 :match-alternatives '(integerp))
3060 (define-widget 'number 'restricted-sexp
3061 "A floating point number."
3064 :type-error "This field should contain a number"
3065 :match-alternatives '(numberp))
3067 (define-widget 'character 'editable-field
3072 :format "%{%t%}: %v\n"
3073 :valid-regexp "\\`.\\'"
3074 :error "This field should contain a single character"
3075 :value-to-internal (lambda (widget value)
3078 (char-to-string value)))
3079 :value-to-external (lambda (widget value)
3083 :match (lambda (widget value)
3084 (char-valid-p value)))
3086 (define-widget 'list 'group
3089 :format "%{%t%}:\n%v")
3091 (define-widget 'vector 'group
3094 :format "%{%t%}:\n%v"
3095 :match 'widget-vector-match
3096 :value-to-internal (lambda (widget value) (append value nil))
3097 :value-to-external (lambda (widget value) (apply 'vector value)))
3099 (defun widget-vector-match (widget value)
3100 (and (vectorp value)
3101 (widget-group-match widget
3102 (widget-apply widget :value-to-internal value))))
3104 (define-widget 'cons 'group
3107 :format "%{%t%}:\n%v"
3108 :match 'widget-cons-match
3109 :value-to-internal (lambda (widget value)
3110 (list (car value) (cdr value)))
3111 :value-to-external (lambda (widget value)
3112 (cons (nth 0 value) (nth 1 value))))
3114 (defun widget-cons-match (widget value)
3116 (widget-group-match widget
3117 (widget-apply widget :value-to-internal value))))
3119 ;;; The `plist' Widget.
3123 (define-widget 'plist 'list
3125 :key-type '(symbol :tag "Key")
3126 :value-type '(sexp :tag "Value")
3127 :convert-widget 'widget-plist-convert-widget
3130 (defvar widget-plist-value-type) ;Dynamic variable
3132 (defun widget-plist-convert-widget (widget)
3133 ;; Handle `:options'.
3134 (let* ((options (widget-get widget :options))
3135 (key-type (widget-get widget :key-type))
3136 (widget-plist-value-type (widget-get widget :value-type))
3137 (other `(editable-list :inline t
3140 ,widget-plist-value-type)))
3142 (list `(checklist :inline t
3144 ,@(mapcar 'widget-plist-convert-option
3148 (widget-put widget :args args)
3151 (defun widget-plist-convert-option (option)
3152 ;; Convert a single plist option.
3153 (let (key-type value-type)
3155 (let ((key (nth 0 option)))
3156 (setq value-type (nth 1 option))
3159 (setq key-type `(const ,key))))
3160 (setq key-type `(const ,option)
3161 value-type widget-plist-value-type))
3162 `(group :format "Key: %v" :inline t ,key-type ,value-type)))
3165 ;;; The `alist' Widget.
3167 ;; Association lists.
3169 (define-widget 'alist 'list
3170 "An association list."
3171 :key-type '(sexp :tag "Key")
3172 :value-type '(sexp :tag "Value")
3173 :convert-widget 'widget-alist-convert-widget
3176 (defvar widget-alist-value-type) ;Dynamic variable
3178 (defun widget-alist-convert-widget (widget)
3179 ;; Handle `:options'.
3180 (let* ((options (widget-get widget :options))
3181 (key-type (widget-get widget :key-type))
3182 (widget-alist-value-type (widget-get widget :value-type))
3183 (other `(editable-list :inline t
3186 ,widget-alist-value-type)))
3188 (list `(checklist :inline t
3190 ,@(mapcar 'widget-alist-convert-option
3194 (widget-put widget :args args)
3197 (defun widget-alist-convert-option (option)
3198 ;; Convert a single alist option.
3199 (let (key-type value-type)
3201 (let ((key (nth 0 option)))
3202 (setq value-type (nth 1 option))
3205 (setq key-type `(const ,key))))
3206 (setq key-type `(const ,option)
3207 value-type widget-alist-value-type))
3208 `(cons :format "Key: %v" ,key-type ,value-type)))
3210 (define-widget 'choice 'menu-choice
3211 "A union of several sexp types."
3213 :format "%{%t%}: %[Value Menu%] %v"
3214 :button-prefix 'widget-push-button-prefix
3215 :button-suffix 'widget-push-button-suffix
3216 :prompt-value 'widget-choice-prompt-value)
3218 (defun widget-choice-prompt-value (widget prompt value unbound)
3220 (let ((args (widget-get widget :args))
3221 (completion-ignore-case (widget-get widget :case-fold))
3222 current choices old)
3223 ;; Find the first arg that match VALUE.
3226 (if (widget-apply (car look) :match value)
3227 (setq old (car look)
3229 (setq look (cdr look)))))
3232 (cond ((= (length args) 0)
3234 ((= (length args) 1)
3236 ((and (= (length args) 2)
3238 (if (eq old (nth 0 args))
3243 (setq current (car args)
3246 (cons (cons (widget-apply current :menu-tag-get)
3249 (let ((val (completing-read prompt choices nil t)))
3251 (let ((try (try-completion val choices)))
3254 (cdr (assoc val choices)))
3257 (widget-prompt-value current prompt nil t)
3260 (define-widget 'radio 'radio-button-choice
3261 "A union of several sexp types."
3263 :format "%{%t%}:\n%v"
3264 :prompt-value 'widget-choice-prompt-value)
3266 (define-widget 'repeat 'editable-list
3267 "A variable length homogeneous list."
3269 :format "%{%t%}:\n%v%i\n")
3271 (define-widget 'set 'checklist
3272 "A list of members from a fixed set."
3274 :format "%{%t%}:\n%v")
3276 (define-widget 'boolean 'toggle
3277 "To be nil or non-nil, that is the question."
3279 :prompt-value 'widget-boolean-prompt-value
3280 :button-prefix 'widget-push-button-prefix
3281 :button-suffix 'widget-push-button-suffix
3282 :format "%{%t%}: %[Toggle%] %v\n"
3286 (defun widget-boolean-prompt-value (widget prompt value unbound)
3287 ;; Toggle a boolean.
3290 ;;; The `color' Widget.
3292 (define-widget 'color 'editable-field
3293 "Choose a color name (with sample)."
3294 :format "%t: %v (%{sample%})\n"
3298 :complete 'widget-color-complete
3299 :sample-face-get 'widget-color-sample-face-get
3300 :notify 'widget-color-notify
3301 :action 'widget-color-action)
3303 (defun widget-color-complete (widget)
3304 "Complete the color in WIDGET."
3305 (require 'facemenu) ; for facemenu-color-alist
3306 (let* ((prefix (buffer-substring-no-properties (widget-field-start widget)
3308 (list (or facemenu-color-alist
3309 (mapcar 'list (defined-colors))))
3310 (completion (try-completion prefix list)))
3311 (cond ((eq completion t)
3312 (message "Exact match."))
3314 (error "Can't find completion for \"%s\"" prefix))
3315 ((not (string-equal prefix completion))
3316 (insert-and-inherit (substring completion (length prefix))))
3318 (message "Making completion list...")
3319 (let ((list (all-completions prefix list nil)))
3320 (with-output-to-temp-buffer "*Completions*"
3321 (display-completion-list list)))
3322 (message "Making completion list...done")))))
3324 (defun widget-color-sample-face-get (widget)
3325 (let* ((value (condition-case nil
3326 (widget-value widget)
3327 (error (widget-get widget :value))))
3328 (symbol (intern (concat "fg:" value))))
3330 (facemenu-get-face symbol)
3333 (defun widget-color-action (widget &optional event)
3334 ;; Prompt for a color.
3335 (let* ((tag (widget-apply widget :menu-tag-get))
3336 (prompt (concat tag ": "))
3337 (value (widget-value widget))
3338 (start (widget-field-start widget))
3339 (pos (cond ((< (point) start)
3341 ((> (point) (+ start (length value)))
3344 (- (point) start))))
3345 (answer (facemenu-read-color prompt)))
3346 (unless (zerop (length answer))
3347 (widget-value-set widget answer)
3349 (widget-apply widget :notify widget event))))
3351 (defun widget-color-notify (widget child &optional event)
3352 "Update the sample, and notofy the parent."
3353 (overlay-put (widget-get widget :sample-overlay)
3354 'face (widget-apply widget :sample-face-get))
3355 (widget-default-notify widget child event))
3359 (defun widget-at (pos)
3360 "The button or field at POS."
3361 (or (get-char-property pos 'button)
3362 (get-char-property pos 'field)))
3364 (defun widget-echo-help (pos)
3365 "Display the help echo for widget at POS."
3366 (let* ((widget (widget-at pos))
3367 (help-echo (and widget (widget-get widget :help-echo))))
3368 (if (or (stringp help-echo)
3369 (and (functionp help-echo)
3370 ;; Kluge: help-echo originally could be a function of
3371 ;; one arg -- the widget. It is more useful in Emacs
3372 ;; 21 to have it as a function usable also as a
3373 ;; help-echo property, when it can sort out its own
3374 ;; widget if necessary. Try both calling sequences
3375 ;; (rather than messing around to get the function's
3380 (funcall help-echo (current-buffer) (point))
3381 (error (funcall help-echo widget))))))
3382 (stringp (eval help-echo)))
3383 (message "%s" help-echo))))
3389 ;;; wid-edit.el ends here