1 ;;; wid-edit.el --- Functions for creating and using widgets -*-byte-compile-dynamic: t;-*-
3 ;; Copyright (C) 1996, 1997, 1999, 2000, 2001 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.
26 ;;; Wishlist items (from widget.texi):
28 ;; * The `menu-choice' tag should be prettier, something like the
29 ;; abbreviated menus in Open Look.
31 ;; * Finish `:tab-order'.
33 ;; * Make indentation work with glyphs and proportional fonts.
35 ;; * Add commands to show overview of object and class hierarchies to
38 ;; * Find a way to disable mouse highlight for inactive widgets.
40 ;; * Find a way to make glyphs look inactive.
42 ;; * Add `key-binding' widget.
44 ;; * Add `widget' widget for editing widget specifications.
46 ;; * Find clean way to implement variable length list. See
47 ;; `TeX-printer-list' for an explanation.
49 ;; * `C-h' in `widget-prompt-value' should give type specific help.
51 ;; * A mailto widget. [This should work OK as a url-link if with
52 ;; browse-url-browser-function' set up appropriately.]
62 (defun widget-event-point (event)
63 "Character position of the end of event if that exists, or nil."
64 (posn-point (event-end event
)))
66 (autoload 'pp-to-string
"pp")
67 (autoload 'Info-goto-node
"info")
69 (defun widget-button-release-event-p (event)
70 "Non-nil if EVENT is a mouse-button-release event object."
72 (memq (event-basic-type event
) '(mouse-1 mouse-2 mouse-3
))
73 (or (memq 'click
(event-modifiers event
))
74 (memq 'drag
(event-modifiers event
)))))
79 "Customization support for the Widget Library."
80 :link
'(custom-manual "(widget)Top")
81 :link
'(emacs-library-link :tag
"Lisp File" "widget.el")
86 (defgroup widget-documentation nil
87 "Options controling the display of documentation strings."
90 (defgroup widget-faces nil
91 "Faces used by the widget library."
95 (defvar widget-documentation-face
'widget-documentation-face
96 "Face used for documentation strings in widgets.
97 This exists as a variable so it can be set locally in certain buffers.")
99 (defface widget-documentation-face
'((((class color
)
101 (:foreground
"lime green"))
104 (:foreground
"dark green"))
106 "Face used for documentation text."
107 :group
'widget-documentation
108 :group
'widget-faces
)
110 (defvar widget-button-face
'widget-button-face
111 "Face used for buttons in widgets.
112 This exists as a variable so it can be set locally in certain buffers.")
114 (defface widget-button-face
'((t (:bold t
)))
115 "Face used for widget buttons."
116 :group
'widget-faces
)
118 (defcustom widget-mouse-face
'highlight
119 "Face used for widget buttons when the mouse is above them."
121 :group
'widget-faces
)
123 (defface widget-field-face
'((((class grayscale color
)
125 (:background
"gray85"))
126 (((class grayscale color
)
128 (:background
"dim gray"))
131 "Face used for editable fields."
132 :group
'widget-faces
)
134 (defface widget-single-line-field-face
'((((class grayscale color
)
136 (:background
"gray85"))
137 (((class grayscale color
)
139 (:background
"dim gray"))
142 "Face used for editable fields spanning only a single line."
143 :group
'widget-faces
)
145 ;;; This causes display-table to be loaded, and not usefully.
146 ;;;(defvar widget-single-line-display-table
147 ;;; (let ((table (make-display-table)))
148 ;;; (aset table 9 "^I")
149 ;;; (aset table 10 "^J")
151 ;;; "Display table used for single-line editable fields.")
153 ;;;(when (fboundp 'set-face-display-table)
154 ;;; (set-face-display-table 'widget-single-line-field-face
155 ;;; widget-single-line-display-table))
157 ;;; Utility functions.
159 ;; These are not really widget specific.
161 (defun widget-princ-to-string (object)
162 "Return string representation of OBJECT, any Lisp object.
163 No quoting characters are used; no delimiters are printed around
164 the contents of strings."
165 (with-output-to-string
168 (defun widget-clear-undo ()
169 "Clear all undo information."
170 (buffer-disable-undo (current-buffer))
171 (buffer-enable-undo))
173 (defcustom widget-menu-max-size
40
174 "Largest number of items allowed in a popup-menu.
175 Larger menus are read through the minibuffer."
179 (defcustom widget-menu-max-shortcuts
40
180 "Largest number of items for which it works to choose one with a character.
181 For a larger number of items, the minibuffer is used."
185 (defcustom widget-menu-minibuffer-flag nil
186 "*Control how to ask for a choice from the keyboard.
187 Non-nil means use the minibuffer;
188 nil means read a single character."
192 (defun widget-choose (title items
&optional event
)
193 "Choose an item from a list.
195 First argument TITLE is the name of the list.
196 Second argument ITEMS is an list whose members are either
197 (NAME . VALUE), to indicate selectable items, or just strings to
198 indicate unselectable items.
199 Optional third argument EVENT is an input event.
201 The user is asked to choose between each NAME from the items alist,
202 and the VALUE of the chosen element will be returned. If EVENT is a
203 mouse event, and the number of elements in items is less than
204 `widget-menu-max-size', a popup menu will be used, otherwise the
206 (cond ((and (< (length items
) widget-menu-max-size
)
207 event
(display-popup-menus-p))
210 (list title
(cons "" items
))))
211 ((or widget-menu-minibuffer-flag
212 (> (length items
) widget-menu-max-shortcuts
))
213 ;; Read the choice of name from the minibuffer.
214 (setq items
(widget-remove-if 'stringp items
))
215 (let ((val (completing-read (concat title
": ") items nil t
)))
217 (let ((try (try-completion val items
)))
220 (cdr (assoc val items
))))))
222 ;; Construct a menu of the choices
223 ;; and then use it for prompting for a single character.
224 (let* ((overriding-terminal-local-map (make-sparse-keymap))
226 map choice some-choice-enabled value
)
227 ;; Define SPC as a prefix char to get to this menu.
228 (define-key overriding-terminal-local-map
" "
229 (setq map
(make-sparse-keymap title
)))
231 (set-buffer (get-buffer-create " widget-choose"))
233 (insert "Available choices:\n\n")
235 (setq choice
(car items
) items
(cdr items
))
237 (let* ((name (car choice
))
238 (function (cdr choice
)))
239 (insert (format "%c = %s\n" next-digit name
))
240 (define-key map
(vector next-digit
) function
)
241 (setq some-choice-enabled t
)))
242 ;; Allocate digits to disabled alternatives
243 ;; so that the digit of a given alternative never varies.
244 (setq next-digit
(1+ next-digit
)))
245 (insert "\nC-g = Quit"))
246 (or some-choice-enabled
247 (error "None of the choices is currently meaningful"))
248 (define-key map
[?\C-g
] 'keyboard-quit
)
249 (define-key map
[t] 'keyboard-quit)
250 (define-key map [?\M-\C-v] 'scroll-other-window)
251 (define-key map [?\M--] 'negative-argument)
252 (setcdr map (nreverse (cdr map)))
253 ;; Read a char with the menu, and return the result
254 ;; that corresponds to it.
255 (save-window-excursion
256 (let ((buf (get-buffer " widget-choose")))
257 (fit-window-to-buffer (display-buffer buf))
258 (let ((cursor-in-echo-area t)
262 (while (not (or (and (>= char ?0) (< char next-digit))
263 (eq value 'keyboard-quit)))
264 ;; Unread a SPC to lead to our new menu.
265 (setq unread-command-events (cons ?\ unread-command-events))
266 (setq keys (read-key-sequence title))
268 (lookup-key overriding-terminal-local-map keys t)
269 char (string-to-char (substring keys 1)))
270 (cond ((eq value 'scroll-other-window)
271 (let ((minibuffer-scroll-window
272 (get-buffer-window buf)))
274 (scroll-other-window-down
275 (window-height minibuffer-scroll-window))
276 (scroll-other-window))
278 ((eq value 'negative-argument)
282 (when (eq value 'keyboard-quit)
286 (defun widget-remove-if (predictate list)
287 (let (result (tail list))
289 (or (funcall predictate (car tail))
290 (setq result (cons (car tail) result)))
291 (setq tail (cdr tail)))
294 ;;; Widget text specifications.
296 ;; These functions are for specifying text properties.
298 (defvar widget-field-add-space t
299 "Non-nil means add extra space at the end of editable text fields.
300 If you don't add the space, it will become impossible to edit a zero
303 (defvar widget-field-use-before-change t
304 "Non-nil means use `before-change-functions' to track editable fields.
305 This enables the use of undo, but doesn't work on Emacs 19.34 and earlier.
306 Using before hooks also means that the :notify function can't know the
309 (defun widget-specify-field (widget from to)
310 "Specify editable button for WIDGET between FROM and TO."
311 ;; Terminating space is not part of the field, but necessary in
312 ;; order for local-map to work. Remove next sexp if local-map works
313 ;; at the end of the overlay.
316 (cond ((null (widget-get widget :size))
318 (widget-field-add-space
319 (insert-and-inherit " ")))
321 (let ((keymap (widget-get widget :keymap))
322 (face (or (widget-get widget :value-face) 'widget-field-face))
323 (help-echo (widget-get widget :help-echo))
325 (or (not widget-field-add-space) (widget-get widget :size))))
326 (if (functionp help-echo)
327 (setq help-echo 'widget-mouse-help))
328 (when (= (char-before to) ?\n)
329 ;; When the last character in the field is a newline, we want to
330 ;; give it a `field' char-property of `boundary', which helps the
331 ;; C-n/C-p act more naturally when entering/leaving the field. We
332 ;; do this by making a small secondary overlay to contain just that
334 (let ((overlay (make-overlay (1- to) to nil t nil)))
335 (overlay-put overlay 'field 'boundary)
336 ;; Use `local-map' here, not `keymap', so that normal editing
337 ;; works in the field when, say, Custom uses `suppress-keymap'.
338 (overlay-put overlay 'local-map keymap)
339 (overlay-put overlay 'face face)
340 (overlay-put overlay 'help-echo help-echo))
342 (setq rear-sticky t))
343 (let ((overlay (make-overlay from to nil nil rear-sticky)))
344 (widget-put widget :field-overlay overlay)
345 ;;(overlay-put overlay 'detachable nil)
346 (overlay-put overlay 'field widget)
347 (overlay-put overlay 'local-map keymap)
348 (overlay-put overlay 'face face)
349 (overlay-put overlay 'help-echo help-echo)))
350 (widget-specify-secret widget))
352 (defun widget-specify-secret (field)
353 "Replace text in FIELD with value of `:secret', if non-nil."
354 (let ((secret (widget-get field :secret))
355 (size (widget-get field :size)))
357 (let ((begin (widget-field-start field))
358 (end (widget-field-end field)))
360 (while (and (> end begin)
361 (eq (char-after (1- end)) ?\ ))
362 (setq end (1- end))))
364 (let ((old (char-after begin)))
365 (unless (eq old secret)
366 (subst-char-in-region begin (1+ begin) old secret)
367 (put-text-property begin (1+ begin) 'secret old))
368 (setq begin (1+ begin))))))))
370 (defun widget-specify-button (widget from to)
371 "Specify button for WIDGET between FROM and TO."
372 (let ((overlay (make-overlay from to nil t nil))
373 (help-echo (widget-get widget :help-echo)))
374 (widget-put widget :button-overlay overlay)
375 (if (functionp help-echo)
376 (setq help-echo 'widget-mouse-help))
377 (overlay-put overlay 'button widget)
378 (overlay-put overlay 'keymap (widget-get widget :keymap))
379 ;; We want to avoid the face with image buttons.
380 (unless (widget-get widget :suppress-face)
381 (overlay-put overlay 'face (widget-apply widget :button-face-get))
382 (overlay-put overlay 'mouse-face widget-mouse-face))
383 (overlay-put overlay 'help-echo help-echo)))
385 (defun widget-mouse-help (window overlay point)
386 "Help-echo callback for widgets whose :help-echo is a function."
387 (with-current-buffer (overlay-buffer overlay)
388 (let* ((widget (widget-at (overlay-start overlay)))
389 (help-echo (if widget (widget-get widget :help-echo))))
390 (if (functionp help-echo)
391 (funcall help-echo widget)
394 (defun widget-specify-sample (widget from to)
395 "Specify sample for WIDGET between FROM and TO."
396 (let ((overlay (make-overlay from to nil t nil)))
397 (overlay-put overlay 'face (widget-apply widget :sample-face-get))
398 (widget-put widget :sample-overlay overlay)))
400 (defun widget-specify-doc (widget from to)
401 "Specify documentation for WIDGET between FROM and TO."
402 (let ((overlay (make-overlay from to nil t nil)))
403 (overlay-put overlay 'widget-doc widget)
404 (overlay-put overlay 'face widget-documentation-face)
405 (widget-put widget :doc-overlay overlay)))
407 (defmacro widget-specify-insert (&rest form)
408 "Execute FORM without inheriting any text properties."
410 (let ((inhibit-read-only t)
411 (inhibit-modification-hooks t)
414 (narrow-to-region (- (point) 2) (point))
415 (goto-char (1+ (point-min)))
416 (setq result (progn ,@form))
417 (delete-region (point-min) (1+ (point-min)))
418 (delete-region (1- (point-max)) (point-max))
419 (goto-char (point-max))
422 (defface widget-inactive-face '((((class grayscale color)
424 (:foreground "light gray"))
425 (((class grayscale color)
427 (:foreground "dim gray"))
430 "Face used for inactive widgets."
431 :group 'widget-faces)
433 (defun widget-specify-inactive (widget from to)
434 "Make WIDGET inactive for user modifications."
435 (unless (widget-get widget :inactive)
436 (let ((overlay (make-overlay from to nil t nil)))
437 (overlay-put overlay 'face 'widget-inactive-face)
438 ;; This is disabled, as it makes the mouse cursor change shape.
439 ;; (overlay-put overlay 'mouse-face 'widget-inactive-face)
440 (overlay-put overlay 'evaporate t)
441 (overlay-put overlay 'priority 100)
442 (overlay-put overlay 'modification-hooks '(widget-overlay-inactive))
443 (widget-put widget :inactive overlay))))
445 (defun widget-overlay-inactive (&rest junk)
446 "Ignoring the arguments, signal an error."
447 (unless inhibit-read-only
448 (error "The widget here is not active")))
451 (defun widget-specify-active (widget)
452 "Make WIDGET active for user modifications."
453 (let ((inactive (widget-get widget :inactive)))
455 (delete-overlay inactive)
456 (widget-put widget :inactive nil))))
458 ;;; Widget Properties.
460 (defsubst widget-type (widget)
461 "Return the type of WIDGET, a symbol."
464 (defun widget-get-indirect (widget property)
465 "In WIDGET, get the value of PROPERTY.
466 If the value is a symbol, return its binding.
467 Otherwise, just return the value."
468 (let ((value (widget-get widget property)))
473 (defun widget-member (widget property)
474 "Non-nil iff there is a definition in WIDGET for PROPERTY."
475 (cond ((plist-member (cdr widget) property)
478 (widget-member (get (car widget) 'widget-type) property))
481 (defun widget-value (widget)
482 "Extract the current value of WIDGET."
484 :value-to-external (widget-apply widget :value-get)))
486 (defun widget-value-set (widget value)
487 "Set the current value of WIDGET to VALUE."
489 :value-set (widget-apply widget
490 :value-to-internal value)))
492 (defun widget-default-get (widget)
493 "Extract the default value of WIDGET."
494 (or (widget-get widget :value)
495 (widget-apply widget :default-get)))
497 (defun widget-match-inline (widget vals)
498 "In WIDGET, match the start of VALS."
499 (cond ((widget-get widget :inline)
500 (widget-apply widget :match-inline vals))
502 (widget-apply widget :match (car vals)))
503 (cons (list (car vals)) (cdr vals)))
506 (defun widget-apply-action (widget &optional event)
507 "Apply :action in WIDGET in response to EVENT."
508 (if (widget-apply widget :active)
509 (widget-apply widget :action event)
510 (error "Attempt to perform action on inactive widget")))
512 ;;; Helper functions.
514 ;; These are widget specific.
517 (defun widget-prompt-value (widget prompt &optional value unbound)
518 "Prompt for a value matching WIDGET, using PROMPT.
519 The current value is assumed to be VALUE, unless UNBOUND is non-nil."
520 (unless (listp widget)
521 (setq widget (list widget)))
522 (setq prompt (format "[%s] %s" (widget-type widget) prompt))
523 (setq widget (widget-convert widget))
524 (let ((answer (widget-apply widget :prompt-value prompt value unbound)))
525 (unless (widget-apply widget :match answer)
526 (error "Value does not match %S type" (car widget)))
529 (defun widget-get-sibling (widget)
530 "Get the item WIDGET is assumed to toggle.
531 This is only meaningful for radio buttons or checkboxes in a list."
532 (let* ((children (widget-get (widget-get widget :parent) :children))
536 (setq child (car children)
537 children (cdr children))
538 (when (eq (widget-get child :button) widget)
539 (throw 'child child)))
542 (defun widget-map-buttons (function &optional buffer maparg)
543 "Map FUNCTION over the buttons in BUFFER.
544 FUNCTION is called with the arguments WIDGET and MAPARG.
546 If FUNCTION returns non-nil, the walk is cancelled.
548 The arguments MAPARG, and BUFFER default to nil and (current-buffer),
550 (let ((cur (point-min))
554 (save-excursion (set-buffer buffer) (overlay-lists))
556 (setq overlays (append (car overlays) (cdr overlays)))
557 (while (setq cur (pop overlays))
558 (setq widget (overlay-get cur 'button))
559 (if (and widget (funcall function widget maparg))
560 (setq overlays nil)))))
564 (defcustom widget-image-directory (file-name-as-directory
565 (expand-file-name "custom" data-directory))
566 "Where widget button images are located.
567 If this variable is nil, widget will try to locate the directory
572 (defcustom widget-image-enable t
573 "If non nil, use image buttons in widgets when available."
578 (defcustom widget-image-conversion
579 '((xpm ".xpm") (gif ".gif") (png ".png") (jpeg ".jpg" ".jpeg")
581 "Conversion alist from image formats to file name suffixes."
583 :type '(repeat (cons :format "%v"
584 (symbol :tag "Image Format" unknown)
585 (repeat :tag "Suffixes"
586 (string :format "%v")))))
588 (defun widget-image-find (image)
589 "Create a graphical button from IMAGE.
590 IMAGE should either already be an image, or be a file name sans
591 extension (xpm, xbm, gif, jpg, or png) located in
592 `widget-image-directory' or otherwise where `find-image' will find it."
593 (cond ((not (and image widget-image-enable (display-graphic-p)))
594 ;; We don't want or can't use images.
597 (eq 'image (car image)))
598 ;; Already an image spec. Use it.
601 ;; A string. Look it up in relevant directories.
602 (let* ((load-path (cons widget-image-directory load-path))
604 (dolist (elt widget-image-conversion)
605 (dolist (ext (cdr elt))
606 (push (list :type (car elt) :file (concat image ext)) specs)))
607 (setq specs (nreverse specs))
613 (defvar widget-button-pressed-face 'widget-button-pressed-face
614 "Face used for pressed buttons in widgets.
615 This exists as a variable so it can be set locally in certain
618 (defun widget-image-insert (widget tag image &optional down inactive)
619 "In WIDGET, insert the text TAG or, if supported, IMAGE.
620 IMAGE should either be an image or an image file name sans extension
621 \(xpm, xbm, gif, jpg, or png) located in `widget-image-directory'.
623 Optional arguments DOWN and INACTIVE are used instead of IMAGE when the
624 button is pressed or inactive, respectively. These are currently ignored."
625 (if (and (display-graphic-p)
626 (setq image (widget-image-find image)))
627 (progn (widget-put widget :suppress-face t)
630 tag 'mouse-face widget-button-pressed-face)))
635 (defgroup widget-button nil
636 "The look of various kinds of buttons."
639 (defcustom widget-button-prefix ""
640 "String used as prefix for buttons."
642 :group 'widget-button)
644 (defcustom widget-button-suffix ""
645 "String used as suffix for buttons."
647 :group 'widget-button)
649 ;;; Creating Widgets.
652 (defun widget-create (type &rest args)
653 "Create widget of TYPE.
654 The optional ARGS are additional keyword arguments."
655 (let ((widget (apply 'widget-convert type args)))
656 (widget-apply widget :create)
659 (defun widget-create-child-and-convert (parent type &rest args)
660 "As part of the widget PARENT, create a child widget TYPE.
661 The child is converted, using the keyword arguments ARGS."
662 (let ((widget (apply 'widget-convert type args)))
663 (widget-put widget :parent parent)
664 (unless (widget-get widget :indent)
665 (widget-put widget :indent (+ (or (widget-get parent :indent) 0)
666 (or (widget-get widget :extra-offset) 0)
667 (widget-get parent :offset))))
668 (widget-apply widget :create)
671 (defun widget-create-child (parent type)
672 "Create widget of TYPE."
673 (let ((widget (copy-sequence type)))
674 (widget-put widget :parent parent)
675 (unless (widget-get widget :indent)
676 (widget-put widget :indent (+ (or (widget-get parent :indent) 0)
677 (or (widget-get widget :extra-offset) 0)
678 (widget-get parent :offset))))
679 (widget-apply widget :create)
682 (defun widget-create-child-value (parent type value)
683 "Create widget of TYPE with value VALUE."
684 (let ((widget (copy-sequence type)))
685 (widget-put widget :value (widget-apply widget :value-to-internal value))
686 (widget-put widget :parent parent)
687 (unless (widget-get widget :indent)
688 (widget-put widget :indent (+ (or (widget-get parent :indent) 0)
689 (or (widget-get widget :extra-offset) 0)
690 (widget-get parent :offset))))
691 (widget-apply widget :create)
695 (defun widget-delete (widget)
697 (widget-apply widget :delete))
699 (defun widget-convert (type &rest args)
700 "Convert TYPE to a widget without inserting it in the buffer.
701 The optional ARGS are additional keyword arguments."
702 ;; Don't touch the type.
703 (let* ((widget (if (symbolp type)
705 (copy-sequence type)))
708 ;; First set the :args keyword.
709 (while (cdr current) ;Look in the type.
710 (if (keywordp (car (cdr current)))
711 (setq current (cdr (cdr current)))
712 (setcdr current (list :args (cdr current)))
714 (while args ;Look in the args.
715 (if (keywordp (nth 0 args))
716 (setq args (nthcdr 2 args))
717 (widget-put widget :args args)
719 ;; Then Convert the widget.
722 (let ((convert-widget (plist-get (cdr type) :convert-widget)))
724 (setq widget (funcall convert-widget widget))))
725 (setq type (get (car type) 'widget-type)))
726 ;; Finally set the keyword args.
728 (let ((next (nth 0 keys)))
731 (widget-put widget next (nth 1 keys))
732 (setq keys (nthcdr 2 keys)))
734 ;; Convert the :value to internal format.
735 (if (widget-member widget :value)
737 :value (widget-apply widget
739 (widget-get widget :value))))
740 ;; Return the newly create widget.
743 (defun widget-insert (&rest args)
744 "Call `insert' with ARGS even if surrounding text is read only."
745 (let ((inhibit-read-only t)
746 (inhibit-modification-hooks t))
747 (apply 'insert args)))
749 (defun widget-convert-text (type from to
750 &optional button-from button-to
752 "Return a widget of type TYPE with endpoint FROM TO.
753 Optional ARGS are extra keyword arguments for TYPE.
754 and TO will be used as the widgets end points. If optional arguments
755 BUTTON-FROM and BUTTON-TO are given, these will be used as the widgets
757 Optional ARGS are extra keyword arguments for TYPE."
758 (let ((widget (apply 'widget-convert type :delete 'widget-leave-text args))
759 (from (copy-marker from))
760 (to (copy-marker to)))
761 (set-marker-insertion-type from t)
762 (set-marker-insertion-type to nil)
763 (widget-put widget :from from)
764 (widget-put widget :to to)
766 (widget-specify-button widget button-from button-to))
769 (defun widget-convert-button (type from to &rest args)
770 "Return a widget of type TYPE with endpoint FROM TO.
771 Optional ARGS are extra keyword arguments for TYPE.
772 No text will be inserted to the buffer, instead the text between FROM
773 and TO will be used as the widgets end points, as well as the widgets
775 (apply 'widget-convert-text type from to from to args))
777 (defun widget-leave-text (widget)
778 "Remove markers and overlays from WIDGET and its children."
779 (let ((button (widget-get widget :button-overlay))
780 (sample (widget-get widget :sample-overlay))
781 (doc (widget-get widget :doc-overlay))
782 (field (widget-get widget :field-overlay)))
783 (set-marker (widget-get widget :from) nil)
784 (set-marker (widget-get widget :to) nil)
786 (delete-overlay button))
788 (delete-overlay sample))
790 (delete-overlay doc))
792 (delete-overlay field))
793 (mapc 'widget-leave-text (widget-get widget :children))))
795 ;;; Keymap and Commands.
797 (defvar widget-keymap
798 (let ((map (make-sparse-keymap)))
799 (define-key map "\t" 'widget-forward)
800 (define-key map [(shift tab)] 'widget-backward)
801 (define-key map [backtab] 'widget-backward)
802 (define-key map [down-mouse-2] 'widget-button-click)
803 (define-key map "\C-m" 'widget-button-press)
805 "Keymap containing useful binding for buffers containing widgets.
806 Recommended as a parent keymap for modes using widgets.")
808 (defvar widget-global-map global-map
809 "Keymap used for events a widget does not handle itself.")
810 (make-variable-buffer-local 'widget-global-map)
812 (defvar widget-field-keymap
813 (let ((map (copy-keymap widget-keymap)))
814 (define-key map "\C-k" 'widget-kill-line)
815 (define-key map "\M-\t" 'widget-complete)
816 (define-key map "\C-m" 'widget-field-activate)
817 ;; Since the widget code uses a `field' property to identify fields,
818 ;; ordinary beginning-of-line does the right thing.
819 ;; (define-key map "\C-a" 'widget-beginning-of-line)
820 (define-key map "\C-e" 'widget-end-of-line)
822 "Keymap used inside an editable field.")
824 (defvar widget-text-keymap
825 (let ((map (copy-keymap widget-keymap)))
826 ;; Since the widget code uses a `field' property to identify fields,
827 ;; ordinary beginning-of-line does the right thing.
828 ;; (define-key map "\C-a" 'widget-beginning-of-line)
829 (define-key map "\C-e" 'widget-end-of-line)
831 "Keymap used inside a text field.")
833 (defun widget-field-activate (pos &optional event)
834 "Invoke the editable field at point."
836 (let ((field (widget-field-at pos)))
838 (widget-apply-action field event)
840 (lookup-key widget-global-map (this-command-keys))))))
842 (defface widget-button-pressed-face
846 (:bold t :underline t)))
847 "Face used for pressed buttons."
848 :group 'widget-faces)
850 (defun widget-button-click (event)
851 "Invoke the button that the mouse is pointing at."
853 (if (widget-event-point event)
854 (let* ((pos (widget-event-point event))
855 (button (get-char-property pos 'button)))
857 ;; Mouse click on a widget button. Do the following
858 ;; in a save-excursion so that the click on the button
859 ;; doesn't change point.
860 (save-selected-window
862 (mouse-set-point event)
863 (let* ((overlay (widget-get button :button-overlay))
864 (face (overlay-get overlay 'face))
865 (mouse-face (overlay-get overlay 'mouse-face)))
867 ;; Read events, including mouse-movement events
868 ;; until we receive a release event. Highlight/
869 ;; unhighlight the button the mouse was initially
870 ;; on when we move over it.
871 (let ((track-mouse t))
873 (when face ; avoid changing around image
875 'face widget-button-pressed-face)
877 'mouse-face widget-button-pressed-face))
878 (unless (widget-apply button :mouse-down-action event)
879 (while (not (widget-button-release-event-p event))
880 (setq event (read-event)
881 pos (widget-event-point event))
883 (eq (get-char-property pos 'button)
888 widget-button-pressed-face)
891 widget-button-pressed-face))
892 (overlay-put overlay 'face face)
893 (overlay-put overlay 'mouse-face mouse-face))))
895 ;; When mouse is released over the button, run
896 ;; its action function.
898 (eq (get-char-property pos 'button) button))
899 (widget-apply-action button event))))
900 (overlay-put overlay 'face face)
901 (overlay-put overlay 'mouse-face mouse-face))))
903 (unless (pos-visible-in-window-p (widget-event-point event))
904 (mouse-set-point event)
908 (let ((up t) command)
909 ;; Mouse click not on a widget button. Find the global
910 ;; command to run, and check whether it is bound to an
912 (mouse-set-point event)
913 (if (memq (event-basic-type event) '(mouse-1 down-mouse-1))
914 (cond ((setq command ;down event
915 (lookup-key widget-global-map [down-mouse-1]))
917 ((setq command ;up event
918 (lookup-key widget-global-map [mouse-1]))))
919 (cond ((setq command ;down event
920 (lookup-key widget-global-map [down-mouse-2]))
922 ((setq command ;up event
923 (lookup-key widget-global-map [mouse-2])))))
925 ;; Don't execute up events twice.
926 (while (not (widget-button-release-event-p event))
927 (setq event (read-event))))
929 (call-interactively command)))))
930 (message "You clicked somewhere weird.")))
932 (defun widget-button-press (pos &optional event)
933 "Invoke button at POS."
935 (let ((button (get-char-property pos 'button)))
937 (widget-apply-action button event)
938 (let ((command (lookup-key widget-global-map (this-command-keys))))
939 (when (commandp command)
940 (call-interactively command))))))
942 (defun widget-tabable-at (&optional pos)
943 "Return the tabable widget at POS, or nil.
944 POS defaults to the value of (point)."
945 (let ((widget (widget-at pos)))
947 (let ((order (widget-get widget :tab-order)))
953 (defvar widget-use-overlay-change t
954 "If non-nil, use overlay change functions to tab around in the buffer.
955 This is much faster, but doesn't work reliably on Emacs 19.34.")
957 (defun widget-move (arg)
958 "Move point to the ARG next field or button.
959 ARG may be negative to move backward."
960 (or (bobp) (> arg 0) (backward-char))
963 (old (widget-tabable-at))
968 (goto-char (point-min)))
969 (widget-use-overlay-change
970 (goto-char (next-overlay-change (point))))
973 (and (eq pos (point))
975 (error "No buttons or fields found"))
976 (let ((new (widget-tabable-at)))
984 (goto-char (point-max)))
985 (widget-use-overlay-change
986 (goto-char (previous-overlay-change (point))))
989 (and (eq pos (point))
991 (error "No buttons or fields found"))
992 (let ((new (widget-tabable-at)))
995 (setq arg (1+ arg))))))
996 (let ((new (widget-tabable-at)))
997 (while (eq (widget-tabable-at) new)
1000 (widget-echo-help (point))
1001 (run-hooks 'widget-move-hook))
1003 (defun widget-forward (arg)
1004 "Move point to the next field or button.
1005 With optional ARG, move across that many fields."
1007 (run-hooks 'widget-forward-hook)
1010 (defun widget-backward (arg)
1011 "Move point to the previous field or button.
1012 With optional ARG, move across that many fields."
1014 (run-hooks 'widget-backward-hook)
1015 (widget-move (- arg)))
1017 ;; Since the widget code uses a `field' property to identify fields,
1018 ;; ordinary beginning-of-line does the right thing.
1019 (defalias 'widget-beginning-of-line 'beginning-of-line)
1021 (defun widget-end-of-line ()
1022 "Go to end of field or end of line, whichever is first.
1023 Trailing spaces at the end of padded fields are not considered part of
1026 ;; Ordinary end-of-line does the right thing, because we're inside
1027 ;; text with a `field' property.
1030 ;; ... except that we want to ignore trailing spaces in fields that
1031 ;; aren't terminated by a newline, because they are used as padding,
1032 ;; and ignored when extracting the entered value of the field.
1033 (skip-chars-backward " " (field-beginning (1- (point))))))
1035 (defun widget-kill-line ()
1036 "Kill to end of field or end of line, whichever is first."
1038 (let* ((field (widget-field-find (point)))
1039 (end (and field (widget-field-end field))))
1040 (if (and field (> (line-beginning-position 2) end))
1041 (kill-region (point) end)
1042 (call-interactively 'kill-line))))
1044 (defcustom widget-complete-field (lookup-key global-map "\M-\t")
1045 "Default function to call for completion inside fields."
1046 :options '(ispell-complete-word complete-tag lisp-complete-symbol)
1050 (defun widget-complete ()
1051 "Complete content of editable field from point.
1052 When not inside a field, move to the previous button or field."
1054 (let ((field (widget-field-find (point))))
1056 (widget-apply field :complete)
1057 (error "Not in an editable field"))))
1059 ;;; Setting up the buffer.
1061 (defvar widget-field-new nil)
1062 ;; List of all newly created editable fields in the buffer.
1063 (make-variable-buffer-local 'widget-field-new)
1065 (defvar widget-field-list nil)
1066 ;; List of all editable fields in the buffer.
1067 (make-variable-buffer-local 'widget-field-list)
1069 (defun widget-at (&optional pos)
1070 "The button or field at POS (default, point)."
1071 (or (get-char-property (or pos (point)) 'button)
1072 (widget-field-at pos)))
1074 (defun widget-setup ()
1075 "Setup current buffer so editing string widgets works."
1076 (let ((inhibit-read-only t)
1077 (inhibit-modification-hooks t)
1079 (while widget-field-new
1080 (setq field (car widget-field-new)
1081 widget-field-new (cdr widget-field-new)
1082 widget-field-list (cons field widget-field-list))
1083 (let ((from (car (widget-get field :field-overlay)))
1084 (to (cdr (widget-get field :field-overlay))))
1085 (widget-specify-field field
1086 (marker-position from) (marker-position to))
1087 (set-marker from nil)
1088 (set-marker to nil))))
1090 (widget-add-change))
1092 (defvar widget-field-last nil)
1093 ;; Last field containing point.
1094 (make-variable-buffer-local 'widget-field-last)
1096 (defvar widget-field-was nil)
1097 ;; The widget data before the change.
1098 (make-variable-buffer-local 'widget-field-was)
1100 (defun widget-field-at (pos)
1101 "Return the widget field at POS, or nil if none."
1102 (let ((field (get-char-property (or pos (point)) 'field)))
1103 (if (eq field 'boundary)
1107 (defun widget-field-buffer (widget)
1108 "Return the start of WIDGET's editing field."
1109 (let ((overlay (widget-get widget :field-overlay)))
1110 (cond ((overlayp overlay)
1111 (overlay-buffer overlay))
1113 (marker-buffer (car overlay))))))
1115 (defun widget-field-start (widget)
1116 "Return the start of WIDGET's editing field."
1117 (let ((overlay (widget-get widget :field-overlay)))
1118 (if (overlayp overlay)
1119 (overlay-start overlay)
1122 (defun widget-field-end (widget)
1123 "Return the end of WIDGET's editing field."
1124 (let ((overlay (widget-get widget :field-overlay)))
1125 ;; Don't subtract one if local-map works at the end of the overlay,
1126 ;; or if a special `boundary' field has been added after the widget
1128 (if (overlayp overlay)
1129 (if (and (not (eq (get-char-property (overlay-end overlay)
1131 (widget-field-buffer widget))
1133 (or widget-field-add-space
1134 (null (widget-get widget :size))))
1135 (1- (overlay-end overlay))
1136 (overlay-end overlay))
1139 (defun widget-field-find (pos)
1140 "Return the field at POS.
1141 Unlike (get-char-property POS 'field) this, works with empty fields too."
1142 (let ((fields widget-field-list)
1145 (setq field (car fields)
1146 fields (cdr fields))
1147 (when (and (<= (widget-field-start field) pos)
1148 (<= pos (widget-field-end field)))
1150 (error "Overlapping fields"))
1151 (setq found field)))
1154 (defun widget-before-change (from to)
1155 ;; This is how, for example, a variable changes its state to `modified'.
1156 ;; when it is being edited.
1157 (unless inhibit-read-only
1158 (let ((from-field (widget-field-find from))
1159 (to-field (widget-field-find to)))
1160 (cond ((not (eq from-field to-field))
1161 (add-hook 'post-command-hook 'widget-add-change nil t)
1162 (signal 'text-read-only
1163 '("Change should be restricted to a single field")))
1165 (add-hook 'post-command-hook 'widget-add-change nil t)
1166 (signal 'text-read-only
1167 '("Attempt to change text outside editable field")))
1168 (widget-field-use-before-change
1169 (widget-apply from-field :notify from-field))))))
1171 (defun widget-add-change ()
1172 (remove-hook 'post-command-hook 'widget-add-change t)
1173 (add-hook 'before-change-functions 'widget-before-change nil t)
1174 (add-hook 'after-change-functions 'widget-after-change nil t))
1176 (defun widget-after-change (from to old)
1177 "Adjust field size and text properties."
1178 (let ((field (widget-field-find from))
1179 (other (widget-field-find to)))
1181 (unless (eq field other)
1182 (error "Change in different fields"))
1183 (let ((size (widget-get field :size)))
1185 (let ((begin (widget-field-start field))
1186 (end (widget-field-end field)))
1187 (cond ((< (- end begin) size)
1191 (insert-char ?\ (- (+ begin size) end))))
1192 ((> (- end begin) size)
1193 ;; Field too large and
1194 (if (or (< (point) (+ begin size))
1196 ;; Point is outside extra space.
1197 (setq begin (+ begin size))
1198 ;; Point is within the extra space.
1199 (setq begin (point)))
1202 (while (and (eq (preceding-char) ?\ )
1204 (delete-backward-char 1)))))))
1205 (widget-specify-secret field))
1206 (widget-apply field :notify field))))
1208 ;;; Widget Functions
1210 ;; These functions are used in the definition of multiple widgets.
1212 (defun widget-parent-action (widget &optional event)
1213 "Tell :parent of WIDGET to handle the :action.
1214 Optional EVENT is the event that triggered the action."
1215 (widget-apply (widget-get widget :parent) :action event))
1217 (defun widget-children-value-delete (widget)
1218 "Delete all :children and :buttons in WIDGET."
1219 (mapc 'widget-delete (widget-get widget :children))
1220 (widget-put widget :children nil)
1221 (mapc 'widget-delete (widget-get widget :buttons))
1222 (widget-put widget :buttons nil))
1224 (defun widget-children-validate (widget)
1225 "All the :children must be valid."
1226 (let ((children (widget-get widget :children))
1228 (while (and children (not found))
1229 (setq child (car children)
1230 children (cdr children)
1231 found (widget-apply child :validate)))
1234 ;; Made defsubst to speed up face editor creation.
1235 (defsubst widget-types-convert-widget (widget)
1236 "Convert :args as widget types in WIDGET."
1237 (widget-put widget :args (mapcar 'widget-convert (widget-get widget :args)))
1240 (defun widget-value-convert-widget (widget)
1241 "Initialize :value from :args in WIDGET."
1242 (let ((args (widget-get widget :args)))
1244 (widget-put widget :value (car args))
1245 ;; Don't convert :value here, as this is done in `widget-convert'.
1246 ;; (widget-put widget :value (widget-apply widget
1247 ;; :value-to-internal (car args)))
1248 (widget-put widget :args nil)))
1251 (defun widget-value-value-get (widget)
1252 "Return the :value property of WIDGET."
1253 (widget-get widget :value))
1255 ;;; The `default' Widget.
1257 (define-widget 'default nil
1258 "Basic widget other widgets are derived from."
1259 :value-to-internal (lambda (widget value) value)
1260 :value-to-external (lambda (widget value) value)
1261 :button-prefix 'widget-button-prefix
1262 :button-suffix 'widget-button-suffix
1263 :complete 'widget-default-complete
1264 :create 'widget-default-create
1267 :format-handler 'widget-default-format-handler
1268 :button-face-get 'widget-default-button-face-get
1269 :sample-face-get 'widget-default-sample-face-get
1270 :delete 'widget-default-delete
1271 :value-set 'widget-default-value-set
1272 :value-inline 'widget-default-value-inline
1273 :default-get 'widget-default-default-get
1274 :menu-tag-get 'widget-default-menu-tag-get
1276 :active 'widget-default-active
1277 :activate 'widget-specify-active
1278 :deactivate 'widget-default-deactivate
1279 :mouse-down-action #'ignore
1280 :action 'widget-default-action
1281 :notify 'widget-default-notify
1282 :prompt-value 'widget-default-prompt-value)
1284 (defun widget-default-complete (widget)
1285 "Call the value of the :complete-function property of WIDGET.
1286 If that does not exists, call the value of `widget-complete-field'."
1287 (call-interactively (or (widget-get widget :complete-function)
1288 widget-complete-field)))
1290 (defun widget-default-create (widget)
1291 "Create WIDGET at point in the current buffer."
1292 (widget-specify-insert
1293 (let ((from (point))
1294 button-begin button-end
1295 sample-begin sample-end
1298 (insert (widget-get widget :format))
1300 ;; Parse escapes in format.
1301 (while (re-search-forward "%\\(.\\)" nil t)
1302 (let ((escape (char-after (match-beginning 1))))
1303 (delete-backward-char 2)
1304 (cond ((eq escape ?%)
1307 (setq button-begin (point))
1308 (insert (widget-get-indirect widget :button-prefix)))
1310 (insert (widget-get-indirect widget :button-suffix))
1311 (setq button-end (point)))
1313 (setq sample-begin (point)))
1315 (setq sample-end (point)))
1317 (when (widget-get widget :indent)
1319 (insert-char ? (widget-get widget :indent))))
1321 (let ((image (widget-get widget :tag-glyph))
1322 (tag (widget-get widget :tag)))
1324 (widget-image-insert widget (or tag "image") image))
1328 (princ (widget-get widget :value)
1329 (current-buffer))))))
1331 (let ((doc (widget-get widget :doc)))
1333 (setq doc-begin (point))
1335 (while (eq (preceding-char) ?\n)
1336 (delete-backward-char 1))
1338 (setq doc-end (point)))))
1340 (if (and button-begin (not button-end))
1341 (widget-apply widget :value-create)
1342 (setq value-pos (point))))
1344 (widget-apply widget :format-handler escape)))))
1345 ;; Specify button, sample, and doc, and insert value.
1346 (and button-begin button-end
1347 (widget-specify-button widget button-begin button-end))
1348 (and sample-begin sample-end
1349 (widget-specify-sample widget sample-begin sample-end))
1350 (and doc-begin doc-end
1351 (widget-specify-doc widget doc-begin doc-end))
1353 (goto-char value-pos)
1354 (widget-apply widget :value-create)))
1355 (let ((from (point-min-marker))
1356 (to (point-max-marker)))
1357 (set-marker-insertion-type from t)
1358 (set-marker-insertion-type to nil)
1359 (widget-put widget :from from)
1360 (widget-put widget :to to)))
1361 (widget-clear-undo))
1363 (defun widget-default-format-handler (widget escape)
1364 ;; We recognize the %h escape by default.
1365 (let* ((buttons (widget-get widget :buttons)))
1366 (cond ((eq escape ?h)
1367 (let* ((doc-property (widget-get widget :documentation-property))
1368 (doc-try (cond ((widget-get widget :doc))
1369 ((functionp doc-property)
1370 (funcall doc-property
1371 (widget-get widget :value)))
1372 ((symbolp doc-property)
1373 (documentation-property
1374 (widget-get widget :value)
1376 (doc-text (and (stringp doc-try)
1377 (> (length doc-try) 1)
1379 (doc-indent (widget-get widget :documentation-indent)))
1381 (and (eq (preceding-char) ?\n)
1382 (widget-get widget :indent)
1383 (insert-char ? (widget-get widget :indent)))
1384 ;; The `*' in the beginning is redundant.
1385 (when (eq (aref doc-text 0) ?*)
1386 (setq doc-text (substring doc-text 1)))
1387 ;; Get rid of trailing newlines.
1388 (when (string-match "\n+\\'" doc-text)
1389 (setq doc-text (substring doc-text 0 (match-beginning 0))))
1390 (push (widget-create-child-and-convert
1391 widget 'documentation-string
1392 :indent (cond ((numberp doc-indent )
1400 (error "Unknown escape `%c'" escape)))
1401 (widget-put widget :buttons buttons)))
1403 (defun widget-default-button-face-get (widget)
1404 ;; Use :button-face or widget-button-face
1405 (or (widget-get widget :button-face)
1406 (let ((parent (widget-get widget :parent)))
1408 (widget-apply parent :button-face-get)
1409 widget-button-face))))
1411 (defun widget-default-sample-face-get (widget)
1412 ;; Use :sample-face.
1413 (widget-get widget :sample-face))
1415 (defun widget-default-delete (widget)
1416 "Remove widget from the buffer."
1417 (let ((from (widget-get widget :from))
1418 (to (widget-get widget :to))
1419 (inactive-overlay (widget-get widget :inactive))
1420 (button-overlay (widget-get widget :button-overlay))
1421 (sample-overlay (widget-get widget :sample-overlay))
1422 (doc-overlay (widget-get widget :doc-overlay))
1423 (inhibit-modification-hooks t)
1424 (inhibit-read-only t))
1425 (widget-apply widget :value-delete)
1426 (when inactive-overlay
1427 (delete-overlay inactive-overlay))
1428 (when button-overlay
1429 (delete-overlay button-overlay))
1430 (when sample-overlay
1431 (delete-overlay sample-overlay))
1433 (delete-overlay doc-overlay))
1435 ;; Kludge: this doesn't need to be true for empty formats.
1436 (delete-region from to))
1437 (set-marker from nil)
1438 (set-marker to nil))
1439 (widget-clear-undo))
1441 (defun widget-default-value-set (widget value)
1442 "Recreate widget with new value."
1443 (let* ((old-pos (point))
1444 (from (copy-marker (widget-get widget :from)))
1445 (to (copy-marker (widget-get widget :to)))
1446 (offset (if (and (<= from old-pos) (<= old-pos to))
1447 (if (>= old-pos (1- to))
1449 (- old-pos from)))))
1450 ;;??? Bug: this ought to insert the new value before deleting the old one,
1451 ;; so that markers on either side of the value automatically
1452 ;; stay on the same side. -- rms.
1454 (goto-char (widget-get widget :from))
1455 (widget-apply widget :delete)
1456 (widget-put widget :value value)
1457 (widget-apply widget :create))
1460 (goto-char (+ (widget-get widget :to) offset 1))
1461 (goto-char (min (+ from offset) (1- (widget-get widget :to))))))))
1463 (defun widget-default-value-inline (widget)
1464 "Wrap value in a list unless it is inline."
1465 (if (widget-get widget :inline)
1466 (widget-value widget)
1467 (list (widget-value widget))))
1469 (defun widget-default-default-get (widget)
1471 (widget-get widget :value))
1473 (defun widget-default-menu-tag-get (widget)
1474 "Use tag or value for menus."
1475 (or (widget-get widget :menu-tag)
1476 (widget-get widget :tag)
1477 (widget-princ-to-string (widget-get widget :value))))
1479 (defun widget-default-active (widget)
1480 "Return t iff this widget active (user modifiable)."
1481 (or (widget-get widget :always-active)
1482 (and (not (widget-get widget :inactive))
1483 (let ((parent (widget-get widget :parent)))
1485 (widget-apply parent :active))))))
1487 (defun widget-default-deactivate (widget)
1488 "Make WIDGET inactive for user modifications."
1489 (widget-specify-inactive widget
1490 (widget-get widget :from)
1491 (widget-get widget :to)))
1493 (defun widget-default-action (widget &optional event)
1494 "Notify the parent when a widget changes."
1495 (let ((parent (widget-get widget :parent)))
1497 (widget-apply parent :notify widget event))))
1499 (defun widget-default-notify (widget child &optional event)
1500 "Pass notification to parent."
1501 (widget-default-action widget event))
1503 (defun widget-default-prompt-value (widget prompt value unbound)
1504 "Read an arbitrary value. Stolen from `set-variable'."
1505 ;; (let ((initial (if unbound
1507 ;; It would be nice if we could do a `(cons val 1)' here.
1508 ;; (prin1-to-string (custom-quote value))))))
1509 (eval-minibuffer prompt))
1511 ;;; The `item' Widget.
1513 (define-widget 'item 'default
1514 "Constant items for inclusion in other widgets."
1515 :convert-widget 'widget-value-convert-widget
1516 :value-create 'widget-item-value-create
1517 :value-delete 'ignore
1518 :value-get 'widget-value-value-get
1519 :match 'widget-item-match
1520 :match-inline 'widget-item-match-inline
1521 :action 'widget-item-action
1524 (defun widget-item-value-create (widget)
1525 "Insert the printed representation of the value."
1526 (princ (widget-get widget :value) (current-buffer)))
1528 (defun widget-item-match (widget value)
1529 ;; Match if the value is the same.
1530 (equal (widget-get widget :value) value))
1532 (defun widget-item-match-inline (widget values)
1533 ;; Match if the value is the same.
1534 (let ((value (widget-get widget :value)))
1536 (<= (length value) (length values))
1537 (let ((head (widget-sublist values 0 (length value))))
1538 (and (equal head value)
1539 (cons head (widget-sublist values (length value))))))))
1541 (defun widget-sublist (list start &optional end)
1542 "Return the sublist of LIST from START to END.
1543 If END is omitted, it defaults to the length of LIST."
1544 (if (> start 0) (setq list (nthcdr start list)))
1546 (unless (<= end start)
1547 (setq list (copy-sequence list))
1548 (setcdr (nthcdr (- end start 1) list) nil)
1550 (copy-sequence list)))
1552 (defun widget-item-action (widget &optional event)
1553 ;; Just notify itself.
1554 (widget-apply widget :notify widget event))
1556 ;;; The `push-button' Widget.
1558 ;; (defcustom widget-push-button-gui t
1559 ;; "If non nil, use GUI push buttons when available."
1563 ;; Cache already created GUI objects.
1564 ;; (defvar widget-push-button-cache nil)
1566 (defcustom widget-push-button-prefix "["
1567 "String used as prefix for buttons."
1569 :group 'widget-button)
1571 (defcustom widget-push-button-suffix "]"
1572 "String used as suffix for buttons."
1574 :group 'widget-button)
1576 (define-widget 'push-button 'item
1577 "A pushable button."
1580 :value-create 'widget-push-button-value-create
1583 (defun widget-push-button-value-create (widget)
1584 "Insert text representing the `on' and `off' states."
1585 (let* ((tag (or (widget-get widget :tag)
1586 (widget-get widget :value)))
1587 (tag-glyph (widget-get widget :tag-glyph))
1588 (text (concat widget-push-button-prefix
1589 tag widget-push-button-suffix)))
1591 (widget-image-insert widget text tag-glyph)
1594 ;; (defun widget-gui-action (widget)
1595 ;; "Apply :action for WIDGET."
1596 ;; (widget-apply-action widget (this-command-keys)))
1598 ;;; The `link' Widget.
1600 (defcustom widget-link-prefix "["
1601 "String used as prefix for links."
1603 :group 'widget-button)
1605 (defcustom widget-link-suffix "]"
1606 "String used as suffix for links."
1608 :group 'widget-button)
1610 (define-widget 'link 'item
1612 :button-prefix 'widget-link-prefix
1613 :button-suffix 'widget-link-suffix
1614 :help-echo "Follow the link."
1617 ;;; The `info-link' Widget.
1619 (define-widget 'info-link 'link
1620 "A link to an info file."
1621 :action 'widget-info-link-action)
1623 (defun widget-info-link-action (widget &optional event)
1624 "Open the info node specified by WIDGET."
1625 (Info-goto-node (widget-value widget)))
1627 ;;; The `url-link' Widget.
1629 (define-widget 'url-link 'link
1630 "A link to an www page."
1631 :action 'widget-url-link-action)
1633 (defun widget-url-link-action (widget &optional event)
1634 "Open the url specified by WIDGET."
1635 (browse-url (widget-value widget)))
1637 ;;; The `function-link' Widget.
1639 (define-widget 'function-link 'link
1640 "A link to an Emacs function."
1641 :action 'widget-function-link-action)
1643 (defun widget-function-link-action (widget &optional event)
1644 "Show the function specified by WIDGET."
1645 (describe-function (widget-value widget)))
1647 ;;; The `variable-link' Widget.
1649 (define-widget 'variable-link 'link
1650 "A link to an Emacs variable."
1651 :action 'widget-variable-link-action)
1653 (defun widget-variable-link-action (widget &optional event)
1654 "Show the variable specified by WIDGET."
1655 (describe-variable (widget-value widget)))
1657 ;;; The `file-link' Widget.
1659 (define-widget 'file-link 'link
1661 :action 'widget-file-link-action)
1663 (defun widget-file-link-action (widget &optional event)
1664 "Find the file specified by WIDGET."
1665 (find-file (widget-value widget)))
1667 ;;; The `emacs-library-link' Widget.
1669 (define-widget 'emacs-library-link 'link
1670 "A link to an Emacs Lisp library file."
1671 :action 'widget-emacs-library-link-action)
1673 (defun widget-emacs-library-link-action (widget &optional event)
1674 "Find the Emacs Library file specified by WIDGET."
1675 (find-file (locate-library (widget-value widget))))
1677 ;;; The `emacs-commentary-link' Widget.
1679 (define-widget 'emacs-commentary-link 'link
1680 "A link to Commentary in an Emacs Lisp library file."
1681 :action 'widget-emacs-commentary-link-action)
1683 (defun widget-emacs-commentary-link-action (widget &optional event)
1684 "Find the Commentary section of the Emacs file specified by WIDGET."
1685 (finder-commentary (widget-value widget)))
1687 ;;; The `editable-field' Widget.
1689 (define-widget 'editable-field 'default
1690 "An editable text field."
1691 :convert-widget 'widget-value-convert-widget
1692 :keymap widget-field-keymap
1694 :help-echo "M-TAB: complete field; RET: enter value"
1696 :prompt-internal 'widget-field-prompt-internal
1697 :prompt-history 'widget-field-history
1698 :prompt-value 'widget-field-prompt-value
1699 :action 'widget-field-action
1700 :validate 'widget-field-validate
1702 :error "Field's value doesn't match allowed forms"
1703 :value-create 'widget-field-value-create
1704 :value-delete 'widget-field-value-delete
1705 :value-get 'widget-field-value-get
1706 :match 'widget-field-match)
1708 (defvar widget-field-history nil
1709 "History of field minibuffer edits.")
1711 (defun widget-field-prompt-internal (widget prompt initial history)
1712 "Read string for WIDGET promptinhg with PROMPT.
1713 INITIAL is the initial input and HISTORY is a symbol containing
1715 (read-string prompt initial history))
1717 (defun widget-field-prompt-value (widget prompt value unbound)
1718 "Prompt for a string."
1719 (widget-apply widget
1721 (widget-apply widget
1722 :prompt-internal prompt
1724 (cons (widget-apply widget
1725 :value-to-internal value)
1727 (widget-get widget :prompt-history))))
1729 (defvar widget-edit-functions nil)
1731 (defun widget-field-action (widget &optional event)
1732 "Move to next field."
1734 (run-hook-with-args 'widget-edit-functions widget))
1736 (defun widget-field-validate (widget)
1737 "Valid if the content matches `:valid-regexp'."
1738 (unless (string-match (widget-get widget :valid-regexp)
1739 (widget-apply widget :value-get))
1742 (defun widget-field-value-create (widget)
1743 "Create an editable text field."
1744 (let ((size (widget-get widget :size))
1745 (value (widget-get widget :value))
1747 ;; This is changed to a real overlay in `widget-setup'. We
1748 ;; need the end points to behave differently until
1749 ;; `widget-setup' is called.
1750 (overlay (cons (make-marker) (make-marker))))
1751 (widget-put widget :field-overlay overlay)
1754 (< (length value) size)
1755 (insert-char ?\ (- size (length value))))
1756 (unless (memq widget widget-field-list)
1757 (setq widget-field-new (cons widget widget-field-new)))
1758 (move-marker (cdr overlay) (point))
1759 (set-marker-insertion-type (cdr overlay) nil)
1762 (move-marker (car overlay) from)
1763 (set-marker-insertion-type (car overlay) t)))
1765 (defun widget-field-value-delete (widget)
1766 "Remove the widget from the list of active editing fields."
1767 (setq widget-field-list (delq widget widget-field-list))
1768 (setq widget-field-new (delq widget widget-field-new))
1769 ;; These are nil if the :format string doesn't contain `%v'.
1770 (let ((overlay (widget-get widget :field-overlay)))
1771 (when (overlayp overlay)
1772 (delete-overlay overlay))))
1774 (defun widget-field-value-get (widget)
1775 "Return current text in editing field."
1776 (let ((from (widget-field-start widget))
1777 (to (widget-field-end widget))
1778 (buffer (widget-field-buffer widget))
1779 (size (widget-get widget :size))
1780 (secret (widget-get widget :secret))
1781 (old (current-buffer)))
1788 (eq (char-after (1- to)) ?\ ))
1790 (let ((result (buffer-substring-no-properties from to)))
1793 (while (< (+ from index) to)
1795 (get-char-property (+ from index) 'secret))
1796 (setq index (1+ index)))))
1799 (widget-get widget :value))))
1801 (defun widget-field-match (widget value)
1802 ;; Match any string.
1805 ;;; The `text' Widget.
1807 (define-widget 'text 'editable-field
1808 "A multiline text area."
1809 :keymap widget-text-keymap)
1811 ;;; The `menu-choice' Widget.
1813 (define-widget 'menu-choice 'default
1814 "A menu of options."
1815 :convert-widget 'widget-types-convert-widget
1816 :format "%[%t%]: %v"
1819 :void '(item :format "invalid (%t)\n")
1820 :value-create 'widget-choice-value-create
1821 :value-delete 'widget-children-value-delete
1822 :value-get 'widget-choice-value-get
1823 :value-inline 'widget-choice-value-inline
1824 :default-get 'widget-choice-default-get
1825 :mouse-down-action 'widget-choice-mouse-down-action
1826 :action 'widget-choice-action
1827 :error "Make a choice"
1828 :validate 'widget-choice-validate
1829 :match 'widget-choice-match
1830 :match-inline 'widget-choice-match-inline)
1832 (defun widget-choice-value-create (widget)
1833 "Insert the first choice that matches the value."
1834 (let ((value (widget-get widget :value))
1835 (args (widget-get widget :args))
1836 (explicit (widget-get widget :explicit-choice))
1838 (if (and explicit (equal value (widget-get widget :explicit-choice-value)))
1840 ;; If the user specified the choice for this value,
1841 ;; respect that choice as long as the value is the same.
1842 (widget-put widget :children (list (widget-create-child-value
1843 widget explicit value)))
1844 (widget-put widget :choice explicit))
1846 (setq current (car args)
1848 (when (widget-apply current :match value)
1849 (widget-put widget :children (list (widget-create-child-value
1850 widget current value)))
1851 (widget-put widget :choice current)
1855 (let ((void (widget-get widget :void)))
1856 (widget-put widget :children (list (widget-create-child-and-convert
1857 widget void :value value)))
1858 (widget-put widget :choice void))))))
1860 (defun widget-choice-value-get (widget)
1861 ;; Get value of the child widget.
1862 (widget-value (car (widget-get widget :children))))
1864 (defun widget-choice-value-inline (widget)
1865 ;; Get value of the child widget.
1866 (widget-apply (car (widget-get widget :children)) :value-inline))
1868 (defun widget-choice-default-get (widget)
1869 ;; Get default for the first choice.
1870 (widget-default-get (car (widget-get widget :args))))
1872 (defcustom widget-choice-toggle nil
1873 "If non-nil, a binary choice will just toggle between the values.
1874 Otherwise, the user will explicitly have to choose between the values
1875 when he invoked the menu."
1879 (defun widget-choice-mouse-down-action (widget &optional event)
1880 ;; Return non-nil if we need a menu.
1881 (let ((args (widget-get widget :args))
1882 (old (widget-get widget :choice)))
1883 (cond ((not (display-popup-menus-p))
1884 ;; No place to pop up a menu.
1886 ((< (length args) 2)
1887 ;; Empty or singleton list, just return the value.
1889 ((> (length args) widget-menu-max-size)
1890 ;; Too long, prompt.
1892 ((> (length args) 2)
1893 ;; Reasonable sized list, use menu.
1895 ((and widget-choice-toggle (memq old args))
1899 ;; Ask which of the two.
1902 (defun widget-choice-action (widget &optional event)
1904 (let ((args (widget-get widget :args))
1905 (old (widget-get widget :choice))
1906 (tag (widget-apply widget :menu-tag-get))
1907 (completion-ignore-case (widget-get widget :case-fold))
1910 ;; Remember old value.
1911 (if (and old (not (widget-apply widget :validate)))
1912 (let* ((external (widget-value widget))
1913 (internal (widget-apply old :value-to-internal external)))
1914 (widget-put old :value internal)))
1917 (cond ((= (length args) 0)
1919 ((= (length args) 1)
1921 ((and widget-choice-toggle
1924 (if (eq old (nth 0 args))
1929 (setq current (car args)
1932 (cons (cons (widget-apply current :menu-tag-get)
1935 (setq this-explicit t)
1936 (widget-choose tag (reverse choices) event))))
1938 ;; If this was an explicit user choice,
1939 ;; record the choice, and the record the value it was made for.
1940 ;; widget-choice-value-create will respect this choice,
1941 ;; as long as the value is the same.
1943 (widget-put widget :explicit-choice current)
1944 (widget-put widget :explicit-choice-value (widget-get widget :value)))
1946 widget (widget-apply current
1947 :value-to-external (widget-default-get current)))
1949 (widget-apply widget :notify widget event)))
1950 (run-hook-with-args 'widget-edit-functions widget))
1952 (defun widget-choice-validate (widget)
1953 ;; Valid if we have made a valid choice.
1954 (if (eq (widget-get widget :void) (widget-get widget :choice))
1956 (widget-apply (car (widget-get widget :children)) :validate)))
1958 (defun widget-choice-match (widget value)
1959 ;; Matches if one of the choices matches.
1960 (let ((args (widget-get widget :args))
1962 (while (and args (not found))
1963 (setq current (car args)
1965 found (widget-apply current :match value)))
1968 (defun widget-choice-match-inline (widget values)
1969 ;; Matches if one of the choices matches.
1970 (let ((args (widget-get widget :args))
1972 (while (and args (null found))
1973 (setq current (car args)
1975 found (widget-match-inline current values)))
1978 ;;; The `toggle' Widget.
1980 (define-widget 'toggle 'item
1981 "Toggle between two states."
1983 :value-create 'widget-toggle-value-create
1984 :action 'widget-toggle-action
1985 :match (lambda (widget value) t)
1989 (defun widget-toggle-value-create (widget)
1990 "Insert text representing the `on' and `off' states."
1991 (if (widget-value widget)
1992 (widget-image-insert widget
1993 (widget-get widget :on)
1994 (widget-get widget :on-glyph))
1995 (widget-image-insert widget
1996 (widget-get widget :off)
1997 (widget-get widget :off-glyph))))
1999 (defun widget-toggle-action (widget &optional event)
2001 (widget-value-set widget (not (widget-value widget)))
2002 (widget-apply widget :notify widget event)
2003 (run-hook-with-args 'widget-edit-functions widget))
2005 ;;; The `checkbox' Widget.
2007 (define-widget 'checkbox 'toggle
2008 "A checkbox toggle."
2013 ;; We could probably do the same job as the images using single
2014 ;; space characters in a boxed face with a stretch specification to
2015 ;; make them square.
2016 :on-glyph (create-image "\377\311\301\343\301\311\377" ; this is an `X'
2017 'xbm t :width 7 :height 7
2018 :foreground "grey75" ; like default mode line
2023 :off-glyph (create-image (make-bool-vector 49 1)
2024 'xbm t :width 7 :height 7
2025 :foreground "grey75"
2028 :help-echo "Toggle this item."
2029 :action 'widget-checkbox-action)
2031 (defun widget-checkbox-action (widget &optional event)
2032 "Toggle checkbox, notify parent, and set active state of sibling."
2033 (widget-toggle-action widget event)
2034 (let ((sibling (widget-get-sibling widget)))
2036 (if (widget-value widget)
2037 (widget-apply sibling :activate)
2038 (widget-apply sibling :deactivate)))))
2040 ;;; The `checklist' Widget.
2042 (define-widget 'checklist 'default
2043 "A multiple choice widget."
2044 :convert-widget 'widget-types-convert-widget
2047 :entry-format "%b %v"
2048 :menu-tag "checklist"
2050 :value-create 'widget-checklist-value-create
2051 :value-delete 'widget-children-value-delete
2052 :value-get 'widget-checklist-value-get
2053 :validate 'widget-checklist-validate
2054 :match 'widget-checklist-match
2055 :match-inline 'widget-checklist-match-inline)
2057 (defun widget-checklist-value-create (widget)
2058 ;; Insert all values
2059 (let ((alist (widget-checklist-match-find widget (widget-get widget :value)))
2060 (args (widget-get widget :args)))
2062 (widget-checklist-add-item widget (car args) (assq (car args) alist))
2063 (setq args (cdr args)))
2064 (widget-put widget :children (nreverse (widget-get widget :children)))))
2066 (defun widget-checklist-add-item (widget type chosen)
2067 "Create checklist item in WIDGET of type TYPE.
2068 If the item is checked, CHOSEN is a cons whose cdr is the value."
2069 (and (eq (preceding-char) ?\n)
2070 (widget-get widget :indent)
2071 (insert-char ? (widget-get widget :indent)))
2072 (widget-specify-insert
2073 (let* ((children (widget-get widget :children))
2074 (buttons (widget-get widget :buttons))
2075 (button-args (or (widget-get type :sibling-args)
2076 (widget-get widget :button-args)))
2079 (insert (widget-get widget :entry-format))
2081 ;; Parse % escapes in format.
2082 (while (re-search-forward "%\\([bv%]\\)" nil t)
2083 (let ((escape (char-after (match-beginning 1))))
2084 (delete-backward-char 2)
2085 (cond ((eq escape ?%)
2088 (setq button (apply 'widget-create-child-and-convert
2090 :value (not (null chosen))
2095 (let ((child (widget-create-child widget type)))
2096 (widget-apply child :deactivate)
2098 ((widget-get type :inline)
2099 (widget-create-child-value
2100 widget type (cdr chosen)))
2102 (widget-create-child-value
2103 widget type (car (cdr chosen)))))))
2105 (error "Unknown escape `%c'" escape)))))
2106 ;; Update properties.
2107 (and button child (widget-put child :button button))
2108 (and button (widget-put widget :buttons (cons button buttons)))
2109 (and child (widget-put widget :children (cons child children))))))
2111 (defun widget-checklist-match (widget values)
2112 ;; All values must match a type in the checklist.
2114 (null (cdr (widget-checklist-match-inline widget values)))))
2116 (defun widget-checklist-match-inline (widget values)
2117 ;; Find the values which match a type in the checklist.
2118 (let ((greedy (widget-get widget :greedy))
2119 (args (copy-sequence (widget-get widget :args)))
2122 (let ((answer (widget-checklist-match-up args values)))
2124 (let ((vals (widget-match-inline answer values)))
2125 (setq found (append found (car vals))
2127 args (delq answer args))))
2129 (setq rest (append rest (list (car values)))
2130 values (cdr values)))
2132 (setq rest (append rest values)
2136 (defun widget-checklist-match-find (widget vals)
2137 "Find the vals which match a type in the checklist.
2138 Return an alist of (TYPE MATCH)."
2139 (let ((greedy (widget-get widget :greedy))
2140 (args (copy-sequence (widget-get widget :args)))
2143 (let ((answer (widget-checklist-match-up args vals)))
2145 (let ((match (widget-match-inline answer vals)))
2146 (setq found (cons (cons answer (car match)) found)
2148 args (delq answer args))))
2150 (setq vals (cdr vals)))
2155 (defun widget-checklist-match-up (args vals)
2156 "Return the first type from ARGS that matches VALS."
2157 (let (current found)
2158 (while (and args (null found))
2159 (setq current (car args)
2161 found (widget-match-inline current vals)))
2165 (defun widget-checklist-value-get (widget)
2166 ;; The values of all selected items.
2167 (let ((children (widget-get widget :children))
2170 (setq child (car children)
2171 children (cdr children))
2172 (if (widget-value (widget-get child :button))
2173 (setq result (append result (widget-apply child :value-inline)))))
2176 (defun widget-checklist-validate (widget)
2177 ;; Ticked chilren must be valid.
2178 (let ((children (widget-get widget :children))
2180 (while (and children (not found))
2181 (setq child (car children)
2182 children (cdr children)
2183 button (widget-get child :button)
2184 found (and (widget-value button)
2185 (widget-apply child :validate))))
2188 ;;; The `option' Widget
2190 (define-widget 'option 'checklist
2191 "An widget with an optional item."
2194 ;;; The `choice-item' Widget.
2196 (define-widget 'choice-item 'item
2197 "Button items that delegate action events to their parents."
2198 :action 'widget-parent-action
2199 :format "%[%t%] \n")
2201 ;;; The `radio-button' Widget.
2203 (define-widget 'radio-button 'toggle
2204 "A radio button for use in the `radio' widget."
2205 :notify 'widget-radio-button-notify
2212 :off-glyph "radio0")
2214 (defun widget-radio-button-notify (widget child &optional event)
2216 (widget-apply (widget-get widget :parent) :action widget event))
2218 ;;; The `radio-button-choice' Widget.
2220 (define-widget 'radio-button-choice 'default
2221 "Select one of multiple options."
2222 :convert-widget 'widget-types-convert-widget
2225 :entry-format "%b %v"
2227 :value-create 'widget-radio-value-create
2228 :value-delete 'widget-children-value-delete
2229 :value-get 'widget-radio-value-get
2230 :value-inline 'widget-radio-value-inline
2231 :value-set 'widget-radio-value-set
2232 :error "You must push one of the buttons"
2233 :validate 'widget-radio-validate
2234 :match 'widget-choice-match
2235 :match-inline 'widget-choice-match-inline
2236 :action 'widget-radio-action)
2238 (defun widget-radio-value-create (widget)
2239 ;; Insert all values
2240 (let ((args (widget-get widget :args))
2243 (setq arg (car args)
2245 (widget-radio-add-item widget arg))))
2247 (defun widget-radio-add-item (widget type)
2248 "Add to radio widget WIDGET a new radio button item of type TYPE."
2249 ;; (setq type (widget-convert type))
2250 (and (eq (preceding-char) ?\n)
2251 (widget-get widget :indent)
2252 (insert-char ? (widget-get widget :indent)))
2253 (widget-specify-insert
2254 (let* ((value (widget-get widget :value))
2255 (children (widget-get widget :children))
2256 (buttons (widget-get widget :buttons))
2257 (button-args (or (widget-get type :sibling-args)
2258 (widget-get widget :button-args)))
2260 (chosen (and (null (widget-get widget :choice))
2261 (widget-apply type :match value)))
2263 (insert (widget-get widget :entry-format))
2265 ;; Parse % escapes in format.
2266 (while (re-search-forward "%\\([bv%]\\)" nil t)
2267 (let ((escape (char-after (match-beginning 1))))
2268 (delete-backward-char 2)
2269 (cond ((eq escape ?%)
2272 (setq button (apply 'widget-create-child-and-convert
2273 widget 'radio-button
2274 :value (not (null chosen))
2277 (setq child (if chosen
2278 (widget-create-child-value
2280 (widget-create-child widget type)))
2282 (widget-apply child :deactivate)))
2284 (error "Unknown escape `%c'" escape)))))
2285 ;; Update properties.
2287 (widget-put widget :choice type))
2289 (widget-put child :button button)
2290 (widget-put widget :buttons (nconc buttons (list button))))
2292 (widget-put widget :children (nconc children (list child))))
2295 (defun widget-radio-value-get (widget)
2296 ;; Get value of the child widget.
2297 (let ((chosen (widget-radio-chosen widget)))
2298 (and chosen (widget-value chosen))))
2300 (defun widget-radio-chosen (widget)
2301 "Return the widget representing the chosen radio button."
2302 (let ((children (widget-get widget :children))
2305 (setq current (car children)
2306 children (cdr children))
2307 (when (widget-apply (widget-get current :button) :value-get)
2312 (defun widget-radio-value-inline (widget)
2313 ;; Get value of the child widget.
2314 (let ((children (widget-get widget :children))
2317 (setq current (car children)
2318 children (cdr children))
2319 (when (widget-apply (widget-get current :button) :value-get)
2320 (setq found (widget-apply current :value-inline)
2324 (defun widget-radio-value-set (widget value)
2325 ;; We can't just delete and recreate a radio widget, since children
2326 ;; can be added after the original creation and won't be recreated
2328 (let ((children (widget-get widget :children))
2331 (setq current (car children)
2332 children (cdr children))
2333 (let* ((button (widget-get current :button))
2334 (match (and (not found)
2335 (widget-apply current :match value))))
2336 (widget-value-set button match)
2339 (widget-value-set current value)
2340 (widget-apply current :activate))
2341 (widget-apply current :deactivate))
2342 (setq found (or found match))))))
2344 (defun widget-radio-validate (widget)
2345 ;; Valid if we have made a valid choice.
2346 (let ((children (widget-get widget :children))
2347 current found button)
2348 (while (and children (not found))
2349 (setq current (car children)
2350 children (cdr children)
2351 button (widget-get current :button)
2352 found (widget-apply button :value-get)))
2354 (widget-apply current :validate)
2357 (defun widget-radio-action (widget child event)
2358 ;; Check if a radio button was pressed.
2359 (let ((children (widget-get widget :children))
2360 (buttons (widget-get widget :buttons))
2362 (when (memq child buttons)
2364 (setq current (car children)
2365 children (cdr children))
2366 (let* ((button (widget-get current :button)))
2367 (cond ((eq child button)
2368 (widget-value-set button t)
2369 (widget-apply current :activate))
2370 ((widget-value button)
2371 (widget-value-set button nil)
2372 (widget-apply current :deactivate)))))))
2373 ;; Pass notification to parent.
2374 (widget-apply widget :notify child event))
2376 ;;; The `insert-button' Widget.
2378 (define-widget 'insert-button 'push-button
2379 "An insert button for the `editable-list' widget."
2381 :help-echo "Insert a new item into the list at this position."
2382 :action 'widget-insert-button-action)
2384 (defun widget-insert-button-action (widget &optional event)
2385 ;; Ask the parent to insert a new item.
2386 (widget-apply (widget-get widget :parent)
2387 :insert-before (widget-get widget :widget)))
2389 ;;; The `delete-button' Widget.
2391 (define-widget 'delete-button 'push-button
2392 "A delete button for the `editable-list' widget."
2394 :help-echo "Delete this item from the list."
2395 :action 'widget-delete-button-action)
2397 (defun widget-delete-button-action (widget &optional event)
2398 ;; Ask the parent to insert a new item.
2399 (widget-apply (widget-get widget :parent)
2400 :delete-at (widget-get widget :widget)))
2402 ;;; The `editable-list' Widget.
2404 ;; (defcustom widget-editable-list-gui nil
2405 ;; "If non nil, use GUI push-buttons in editable list when available."
2409 (define-widget 'editable-list 'default
2410 "A variable list of widgets of the same type."
2411 :convert-widget 'widget-types-convert-widget
2414 :format-handler 'widget-editable-list-format-handler
2415 :entry-format "%i %d %v"
2416 :menu-tag "editable-list"
2417 :value-create 'widget-editable-list-value-create
2418 :value-delete 'widget-children-value-delete
2419 :value-get 'widget-editable-list-value-get
2420 :validate 'widget-children-validate
2421 :match 'widget-editable-list-match
2422 :match-inline 'widget-editable-list-match-inline
2423 :insert-before 'widget-editable-list-insert-before
2424 :delete-at 'widget-editable-list-delete-at)
2426 (defun widget-editable-list-format-handler (widget escape)
2427 ;; We recognize the insert button.
2428 ;;; (let ((widget-push-button-gui widget-editable-list-gui))
2429 (cond ((eq escape ?i)
2430 (and (widget-get widget :indent)
2431 (insert-char ?\ (widget-get widget :indent)))
2432 (apply 'widget-create-child-and-convert
2433 widget 'insert-button
2434 (widget-get widget :append-button-args)))
2436 (widget-default-format-handler widget escape)))
2440 (defun widget-editable-list-value-create (widget)
2441 ;; Insert all values
2442 (let* ((value (widget-get widget :value))
2443 (type (nth 0 (widget-get widget :args)))
2445 (widget-put widget :value-pos (copy-marker (point)))
2446 (set-marker-insertion-type (widget-get widget :value-pos) t)
2448 (let ((answer (widget-match-inline type value)))
2450 (setq children (cons (widget-editable-list-entry-create
2452 (if (widget-get type :inline)
2459 (widget-put widget :children (nreverse children))))
2461 (defun widget-editable-list-value-get (widget)
2462 ;; Get value of the child widget.
2463 (apply 'append (mapcar (lambda (child) (widget-apply child :value-inline))
2464 (widget-get widget :children))))
2466 (defun widget-editable-list-match (widget value)
2467 ;; Value must be a list and all the members must match the type.
2469 (null (cdr (widget-editable-list-match-inline widget value)))))
2471 (defun widget-editable-list-match-inline (widget value)
2472 (let ((type (nth 0 (widget-get widget :args)))
2475 (while (and value ok)
2476 (let ((answer (widget-match-inline type value)))
2478 (setq found (append found (car answer))
2481 (cons found value)))
2483 (defun widget-editable-list-insert-before (widget before)
2484 ;; Insert a new child in the list of children.
2486 (let ((children (widget-get widget :children))
2487 (inhibit-read-only t)
2488 before-change-functions
2489 after-change-functions)
2491 (goto-char (widget-get before :entry-from)))
2493 (goto-char (widget-get widget :value-pos))))
2494 (let ((child (widget-editable-list-entry-create
2496 (when (< (widget-get child :entry-from) (widget-get widget :from))
2497 (set-marker (widget-get widget :from)
2498 (widget-get child :entry-from)))
2499 (if (eq (car children) before)
2500 (widget-put widget :children (cons child children))
2501 (while (not (eq (car (cdr children)) before))
2502 (setq children (cdr children)))
2503 (setcdr children (cons child (cdr children)))))))
2505 (widget-apply widget :notify widget))
2507 (defun widget-editable-list-delete-at (widget child)
2508 ;; Delete child from list of children.
2510 (let ((buttons (copy-sequence (widget-get widget :buttons)))
2512 (inhibit-read-only t)
2513 before-change-functions
2514 after-change-functions)
2516 (setq button (car buttons)
2517 buttons (cdr buttons))
2518 (when (eq (widget-get button :widget) child)
2520 :buttons (delq button (widget-get widget :buttons)))
2521 (widget-delete button))))
2522 (let ((entry-from (widget-get child :entry-from))
2523 (entry-to (widget-get child :entry-to))
2524 (inhibit-read-only t)
2525 before-change-functions
2526 after-change-functions)
2527 (widget-delete child)
2528 (delete-region entry-from entry-to)
2529 (set-marker entry-from nil)
2530 (set-marker entry-to nil))
2531 (widget-put widget :children (delq child (widget-get widget :children))))
2533 (widget-apply widget :notify widget))
2535 (defun widget-editable-list-entry-create (widget value conv)
2536 ;; Create a new entry to the list.
2537 (let ((type (nth 0 (widget-get widget :args)))
2538 ;;; (widget-push-button-gui widget-editable-list-gui)
2539 child delete insert)
2540 (widget-specify-insert
2542 (and (widget-get widget :indent)
2543 (insert-char ?\ (widget-get widget :indent)))
2544 (insert (widget-get widget :entry-format)))
2545 ;; Parse % escapes in format.
2546 (while (re-search-forward "%\\(.\\)" nil t)
2547 (let ((escape (char-after (match-beginning 1))))
2548 (delete-backward-char 2)
2549 (cond ((eq escape ?%)
2552 (setq insert (apply 'widget-create-child-and-convert
2553 widget 'insert-button
2554 (widget-get widget :insert-button-args))))
2556 (setq delete (apply 'widget-create-child-and-convert
2557 widget 'delete-button
2558 (widget-get widget :delete-button-args))))
2561 (setq child (widget-create-child-value
2563 (setq child (widget-create-child-value
2565 (widget-apply type :value-to-external
2566 (widget-default-get type))))))
2568 (error "Unknown escape `%c'" escape)))))
2570 :buttons (cons delete
2572 (widget-get widget :buttons))))
2573 (let ((entry-from (point-min-marker))
2574 (entry-to (point-max-marker)))
2575 (set-marker-insertion-type entry-from t)
2576 (set-marker-insertion-type entry-to nil)
2577 (widget-put child :entry-from entry-from)
2578 (widget-put child :entry-to entry-to)))
2579 (widget-put insert :widget child)
2580 (widget-put delete :widget child)
2583 ;;; The `group' Widget.
2585 (define-widget 'group 'default
2586 "A widget which groups other widgets inside."
2587 :convert-widget 'widget-types-convert-widget
2589 :value-create 'widget-group-value-create
2590 :value-delete 'widget-children-value-delete
2591 :value-get 'widget-editable-list-value-get
2592 :default-get 'widget-group-default-get
2593 :validate 'widget-children-validate
2594 :match 'widget-group-match
2595 :match-inline 'widget-group-match-inline)
2597 (defun widget-group-value-create (widget)
2598 ;; Create each component.
2599 (let ((args (widget-get widget :args))
2600 (value (widget-get widget :value))
2601 arg answer children)
2603 (setq arg (car args)
2605 answer (widget-match-inline arg value)
2607 (and (eq (preceding-char) ?\n)
2608 (widget-get widget :indent)
2609 (insert-char ?\ (widget-get widget :indent)))
2610 (push (cond ((null answer)
2611 (widget-create-child widget arg))
2612 ((widget-get arg :inline)
2613 (widget-create-child-value widget arg (car answer)))
2615 (widget-create-child-value widget arg (car (car answer)))))
2617 (widget-put widget :children (nreverse children))))
2619 (defun widget-group-default-get (widget)
2620 ;; Get the default of the components.
2621 (mapcar 'widget-default-get (widget-get widget :args)))
2623 (defun widget-group-match (widget values)
2624 ;; Match if the components match.
2626 (let ((match (widget-group-match-inline widget values)))
2627 (and match (null (cdr match))))))
2629 (defun widget-group-match-inline (widget vals)
2630 ;; Match if the components match.
2631 (let ((args (widget-get widget :args))
2632 argument answer found)
2634 (setq argument (car args)
2636 answer (widget-match-inline argument vals))
2638 (setq vals (cdr answer)
2639 found (append found (car answer)))
2643 (cons found vals))))
2645 ;;; The `visibility' Widget.
2647 (define-widget 'visibility 'item
2648 "An indicator and manipulator for hidden items."
2654 :value-create 'widget-visibility-value-create
2655 :action 'widget-toggle-action
2656 :match (lambda (widget value) t))
2658 (defun widget-visibility-value-create (widget)
2659 ;; Insert text representing the `on' and `off' states.
2660 (let ((on (widget-get widget :on))
2661 (off (widget-get widget :off)))
2663 (setq on (concat widget-push-button-prefix
2665 widget-push-button-suffix))
2668 (setq off (concat widget-push-button-prefix
2670 widget-push-button-suffix))
2672 (if (widget-value widget)
2673 (widget-image-insert widget on "down" "down-pushed")
2674 (widget-image-insert widget off "right" "right-pushed"))))
2676 ;;; The `documentation-link' Widget.
2678 ;; This is a helper widget for `documentation-string'.
2680 (define-widget 'documentation-link 'link
2681 "Link type used in documentation strings."
2683 :help-echo "Describe this symbol"
2684 :action 'widget-documentation-link-action)
2686 (defun widget-documentation-link-action (widget &optional event)
2687 "Display documentation for WIDGET's value. Ignore optional argument EVENT."
2688 (let* ((string (widget-get widget :value))
2689 (symbol (intern string)))
2690 (if (and (fboundp symbol) (boundp symbol))
2691 ;; If there are two doc strings, give the user a way to pick one.
2692 (apropos (concat "\\`" (regexp-quote string) "\\'"))
2693 (if (fboundp symbol)
2694 (describe-function symbol)
2695 (describe-variable symbol)))))
2697 (defcustom widget-documentation-links t
2698 "Add hyperlinks to documentation strings when non-nil."
2700 :group 'widget-documentation)
2702 (defcustom widget-documentation-link-regexp "`\\([^\n`' ]+\\)'"
2703 "Regexp for matching potential links in documentation strings.
2704 The first group should be the link itself."
2706 :group 'widget-documentation)
2708 (defcustom widget-documentation-link-p 'intern-soft
2709 "Predicate used to test if a string is useful as a link.
2710 The value should be a function. The function will be called one
2711 argument, a string, and should return non-nil if there should be a
2712 link for that string."
2714 :options '(widget-documentation-link-p)
2715 :group 'widget-documentation)
2717 (defcustom widget-documentation-link-type 'documentation-link
2718 "Widget type used for links in documentation strings."
2720 :group 'widget-documentation)
2722 (defun widget-documentation-link-add (widget from to)
2723 (widget-specify-doc widget from to)
2724 (when widget-documentation-links
2725 (let ((regexp widget-documentation-link-regexp)
2726 (buttons (widget-get widget :buttons))
2727 (widget-mouse-face (default-value 'widget-mouse-face))
2728 (widget-button-face widget-documentation-face)
2729 (widget-button-pressed-face widget-documentation-face))
2732 (while (re-search-forward regexp to t)
2733 (let ((name (match-string 1))
2734 (begin (match-beginning 1))
2735 (end (match-end 1)))
2736 (when (funcall widget-documentation-link-p name)
2737 (push (widget-convert-button widget-documentation-link-type
2738 begin end :value name)
2740 (widget-put widget :buttons buttons)))
2741 (let ((indent (widget-get widget :indent)))
2742 (when (and indent (not (zerop indent)))
2745 (narrow-to-region from to)
2746 (goto-char (point-min))
2747 (while (search-forward "\n" nil t)
2748 (insert-char ?\ indent)))))))
2750 ;;; The `documentation-string' Widget.
2752 (define-widget 'documentation-string 'item
2753 "A documentation string."
2755 :action 'widget-documentation-string-action
2756 :value-delete 'widget-children-value-delete
2757 :value-create 'widget-documentation-string-value-create)
2759 (defun widget-documentation-string-value-create (widget)
2760 ;; Insert documentation string.
2761 (let ((doc (widget-value widget))
2762 (indent (widget-get widget :indent))
2763 (shown (widget-get (widget-get widget :parent) :documentation-shown))
2765 (if (string-match "\n" doc)
2766 (let ((before (substring doc 0 (match-beginning 0)))
2767 (after (substring doc (match-beginning 0)))
2770 (widget-documentation-link-add widget start (point))
2772 (widget-create-child-and-convert
2774 :help-echo "Show or hide rest of the documentation."
2777 :action 'widget-parent-action
2780 (setq start (point))
2781 (when (and indent (not (zerop indent)))
2782 (insert-char ?\ indent))
2784 (widget-documentation-link-add widget start (point)))
2785 (widget-put widget :buttons (list button)))
2787 (widget-documentation-link-add widget start (point))))
2790 (defun widget-documentation-string-action (widget &rest ignore)
2791 ;; Toggle documentation.
2792 (let ((parent (widget-get widget :parent)))
2793 (widget-put parent :documentation-shown
2794 (not (widget-get parent :documentation-shown))))
2796 (widget-value-set widget (widget-value widget)))
2798 ;;; The Sexp Widgets.
2800 (define-widget 'const 'item
2801 "An immutable sexp."
2802 :prompt-value 'widget-const-prompt-value
2805 (defun widget-const-prompt-value (widget prompt value unbound)
2806 ;; Return the value of the const.
2807 (widget-value widget))
2809 (define-widget 'function-item 'const
2810 "An immutable function name."
2812 :documentation-property (lambda (symbol)
2814 (documentation symbol t)
2817 (define-widget 'variable-item 'const
2818 "An immutable variable name."
2820 :documentation-property 'variable-documentation)
2822 (define-widget 'other 'sexp
2823 "Matches any value, but doesn't let the user edit the value.
2824 This is useful as last item in a `choice' widget.
2825 You should use this widget type with a default value,
2826 as in (other DEFAULT) or (other :tag \"NAME\" DEFAULT).
2827 If the user selects this alternative, that specifies DEFAULT
2833 (defvar widget-string-prompt-value-history nil
2834 "History of input to `widget-string-prompt-value'.")
2836 (define-widget 'string 'editable-field
2839 :format "%{%t%}: %v"
2840 :complete-function 'ispell-complete-word
2841 :prompt-history 'widget-string-prompt-value-history)
2843 (define-widget 'regexp 'string
2844 "A regular expression."
2845 :match 'widget-regexp-match
2846 :validate 'widget-regexp-validate
2847 ;; Doesn't work well with terminating newline.
2848 ;; :value-face 'widget-single-line-field-face
2851 (defun widget-regexp-match (widget value)
2852 ;; Match valid regexps.
2853 (and (stringp value)
2856 (string-match value ""))
2859 (defun widget-regexp-validate (widget)
2860 "Check that the value of WIDGET is a valid regexp."
2861 (condition-case data
2863 (string-match (widget-value widget) ""))
2864 (error (widget-put widget :error (error-message-string data))
2867 (define-widget 'file 'string
2869 It will read a file name from the minibuffer when invoked."
2870 :complete-function 'widget-file-complete
2871 :prompt-value 'widget-file-prompt-value
2872 :format "%{%t%}: %v"
2873 ;; Doesn't work well with terminating newline.
2874 ;; :value-face 'widget-single-line-field-face
2877 (defun widget-file-complete ()
2878 "Perform completion on file name preceding point."
2880 (let* ((end (point))
2881 (beg (save-excursion
2882 (skip-chars-backward "^ ")
2884 (pattern (buffer-substring beg end))
2885 (name-part (file-name-nondirectory pattern))
2886 (directory (file-name-directory pattern))
2887 (completion (file-name-completion name-part directory)))
2888 (cond ((eq completion t))
2890 (message "Can't find completion for \"%s\"" pattern)
2892 ((not (string= name-part completion))
2893 (delete-region beg end)
2894 (insert (expand-file-name completion directory)))
2896 (message "Making completion list...")
2897 (with-output-to-temp-buffer "*Completions*"
2898 (display-completion-list
2899 (sort (file-name-all-completions name-part directory)
2901 (message "Making completion list...%s" "done")))))
2903 (defun widget-file-prompt-value (widget prompt value unbound)
2904 ;; Read file from minibuffer.
2905 (abbreviate-file-name
2907 (read-file-name prompt)
2908 (let ((prompt2 (format "%s (default %s) " prompt value))
2909 (dir (file-name-directory value))
2910 (file (file-name-nondirectory value))
2911 (must-match (widget-get widget :must-match)))
2912 (read-file-name prompt2 dir nil must-match file)))))
2914 ;;;(defun widget-file-action (widget &optional event)
2915 ;;; ;; Read a file name from the minibuffer.
2916 ;;; (let* ((value (widget-value widget))
2917 ;;; (dir (file-name-directory value))
2918 ;;; (file (file-name-nondirectory value))
2919 ;;; (menu-tag (widget-apply widget :menu-tag-get))
2920 ;;; (must-match (widget-get widget :must-match))
2921 ;;; (answer (read-file-name (concat menu-tag ": (default `" value "') ")
2922 ;;; dir nil must-match file)))
2923 ;;; (widget-value-set widget (abbreviate-file-name answer))
2925 ;;; (widget-apply widget :notify widget event)))
2927 ;; Fixme: use file-name-as-directory.
2928 (define-widget 'directory 'file
2929 "A directory widget.
2930 It will read a directory name from the minibuffer when invoked."
2933 (defvar widget-symbol-prompt-value-history nil
2934 "History of input to `widget-symbol-prompt-value'.")
2936 (define-widget 'symbol 'editable-field
2940 :format "%{%t%}: %v"
2941 :match (lambda (widget value) (symbolp value))
2942 :complete-function 'lisp-complete-symbol
2943 :prompt-internal 'widget-symbol-prompt-internal
2944 :prompt-match 'symbolp
2945 :prompt-history 'widget-symbol-prompt-value-history
2946 :value-to-internal (lambda (widget value)
2950 :value-to-external (lambda (widget value)
2955 (defun widget-symbol-prompt-internal (widget prompt initial history)
2956 ;; Read file from minibuffer.
2957 (let ((answer (completing-read prompt obarray
2958 (widget-get widget :prompt-match)
2959 nil initial history)))
2960 (if (and (stringp answer)
2961 (not (zerop (length answer))))
2963 (error "No value"))))
2965 (defvar widget-function-prompt-value-history nil
2966 "History of input to `widget-function-prompt-value'.")
2968 (define-widget 'function 'sexp
2970 :complete-function (lambda ()
2972 (lisp-complete-symbol 'fboundp))
2973 :prompt-value 'widget-field-prompt-value
2974 :prompt-internal 'widget-symbol-prompt-internal
2975 :prompt-match 'fboundp
2976 :prompt-history 'widget-function-prompt-value-history
2977 :action 'widget-field-action
2978 :match-alternatives '(functionp)
2979 :validate (lambda (widget)
2980 (unless (functionp (widget-value widget))
2981 (widget-put widget :error (format "Invalid function: %S"
2982 (widget-value widget)))
2987 (defvar widget-variable-prompt-value-history nil
2988 "History of input to `widget-variable-prompt-value'.")
2990 (define-widget 'variable 'symbol
2992 :prompt-match 'boundp
2993 :prompt-history 'widget-variable-prompt-value-history
2994 :complete-function (lambda ()
2996 (lisp-complete-symbol 'boundp))
2999 (defvar widget-coding-system-prompt-value-history nil
3000 "History of input to `widget-coding-system-prompt-value'.")
3002 (define-widget 'coding-system 'symbol
3003 "A MULE coding-system."
3004 :format "%{%t%}: %v"
3005 :tag "Coding system"
3007 :prompt-history 'widget-coding-system-prompt-value-history
3008 :prompt-value 'widget-coding-system-prompt-value
3009 :action 'widget-coding-system-action
3010 :complete-function (lambda ()
3012 (lisp-complete-symbol 'coding-system-p))
3013 :validate (lambda (widget)
3014 (unless (coding-system-p (widget-value widget))
3015 (widget-put widget :error (format "Invalid coding system: %S"
3016 (widget-value widget)))
3019 :prompt-match 'coding-system-p)
3021 (defun widget-coding-system-prompt-value (widget prompt value unbound)
3022 "Read coding-system from minibuffer."
3023 (if (widget-get widget :base-only)
3025 (completing-read (format "%s (default %s) " prompt value)
3026 (mapcar #'list (coding-system-list t)) nil nil nil
3027 coding-system-history))
3028 (read-coding-system (format "%s (default %s) " prompt value) value)))
3030 (defun widget-coding-system-action (widget &optional event)
3032 (widget-coding-system-prompt-value
3034 (widget-apply widget :menu-tag-get)
3035 (widget-value widget)
3037 (widget-value-set widget answer)
3038 (widget-apply widget :notify widget event)
3041 (define-widget 'sexp 'editable-field
3042 "An arbitrary Lisp expression."
3043 :tag "Lisp expression"
3044 :format "%{%t%}: %v"
3046 :validate 'widget-sexp-validate
3047 :match (lambda (widget value) t)
3048 :value-to-internal 'widget-sexp-value-to-internal
3049 :value-to-external (lambda (widget value) (read value))
3050 :prompt-history 'widget-sexp-prompt-value-history
3051 :prompt-value 'widget-sexp-prompt-value)
3053 (defun widget-sexp-value-to-internal (widget value)
3054 ;; Use pp for printer representation.
3055 (let ((pp (if (symbolp value)
3056 (prin1-to-string value)
3057 (pp-to-string value))))
3058 (while (string-match "\n\\'" pp)
3059 (setq pp (substring pp 0 -1)))
3060 (if (or (string-match "\n\\'" pp)
3065 (defun widget-sexp-validate (widget)
3066 ;; Valid if we can read the string and there is no junk left after it.
3068 (insert (widget-apply widget :value-get))
3069 (goto-char (point-min))
3071 (condition-case data
3073 ;; Avoid a confusing end-of-file error.
3074 (skip-syntax-forward "\\s-")
3076 (setq err "Empty sexp -- use `nil'?")
3077 (unless (widget-apply widget :match (read (current-buffer)))
3078 (setq err (widget-get widget :type-error))))
3079 (if (and (not (eobp))
3081 (setq err (format "Junk at end of expression: %s"
3082 (buffer-substring (point)
3084 (end-of-file ; Avoid confusing error message.
3085 (setq err "Unbalanced sexp"))
3086 (error (setq err (error-message-string data))))
3089 (widget-put widget :error err)
3092 (defvar widget-sexp-prompt-value-history nil
3093 "History of input to `widget-sexp-prompt-value'.")
3095 (defun widget-sexp-prompt-value (widget prompt value unbound)
3096 ;; Read an arbitrary sexp.
3097 (let ((found (read-string prompt
3098 (if unbound nil (cons (prin1-to-string value) 0))
3099 (widget-get widget :prompt-history))))
3100 (let ((answer (read-from-string found)))
3101 (unless (= (cdr answer) (length found))
3102 (error "Junk at end of expression: %s"
3103 (substring found (cdr answer))))
3106 (define-widget 'restricted-sexp 'sexp
3107 "A Lisp expression restricted to values that match.
3108 To use this type, you must define :match or :match-alternatives."
3109 :type-error "The specified value is not valid"
3110 :match 'widget-restricted-sexp-match
3111 :value-to-internal (lambda (widget value)
3112 (if (widget-apply widget :match value)
3113 (prin1-to-string value)
3116 (defun widget-restricted-sexp-match (widget value)
3117 (let ((alternatives (widget-get widget :match-alternatives))
3119 (while (and alternatives (not matched))
3120 (if (cond ((functionp (car alternatives))
3121 (funcall (car alternatives) value))
3122 ((and (consp (car alternatives))
3123 (eq (car (car alternatives)) 'quote))
3124 (eq value (nth 1 (car alternatives)))))
3126 (setq alternatives (cdr alternatives)))
3129 (define-widget 'integer 'restricted-sexp
3133 :type-error "This field should contain an integer"
3134 :match-alternatives '(integerp))
3136 (define-widget 'number 'restricted-sexp
3137 "A floating point number."
3140 :type-error "This field should contain a number"
3141 :match-alternatives '(numberp))
3143 (define-widget 'character 'editable-field
3148 :format "%{%t%}: %v\n"
3149 :valid-regexp "\\`.\\'"
3150 :error "This field should contain a single character"
3151 :value-to-internal (lambda (widget value)
3154 (char-to-string value)))
3155 :value-to-external (lambda (widget value)
3159 :match (lambda (widget value)
3160 (char-valid-p value)))
3162 (define-widget 'list 'group
3165 :format "%{%t%}:\n%v")
3167 (define-widget 'vector 'group
3170 :format "%{%t%}:\n%v"
3171 :match 'widget-vector-match
3172 :value-to-internal (lambda (widget value) (append value nil))
3173 :value-to-external (lambda (widget value) (apply 'vector value)))
3175 (defun widget-vector-match (widget value)
3176 (and (vectorp value)
3177 (widget-group-match widget
3178 (widget-apply widget :value-to-internal value))))
3180 (define-widget 'cons 'group
3183 :format "%{%t%}:\n%v"
3184 :match 'widget-cons-match
3185 :value-to-internal (lambda (widget value)
3186 (list (car value) (cdr value)))
3187 :value-to-external (lambda (widget value)
3188 (cons (nth 0 value) (nth 1 value))))
3190 (defun widget-cons-match (widget value)
3192 (widget-group-match widget
3193 (widget-apply widget :value-to-internal value))))
3195 ;;; The `plist' Widget.
3199 (define-widget 'plist 'list
3201 :key-type '(symbol :tag "Key")
3202 :value-type '(sexp :tag "Value")
3203 :convert-widget 'widget-plist-convert-widget
3206 (defvar widget-plist-value-type) ;Dynamic variable
3208 (defun widget-plist-convert-widget (widget)
3209 ;; Handle `:options'.
3210 (let* ((options (widget-get widget :options))
3211 (widget-plist-value-type (widget-get widget :value-type))
3212 (other `(editable-list :inline t
3214 ,(widget-get widget :key-type)
3215 ,widget-plist-value-type)))
3217 (list `(checklist :inline t
3219 ,@(mapcar 'widget-plist-convert-option
3223 (widget-put widget :args args)
3226 (defun widget-plist-convert-option (option)
3227 ;; Convert a single plist option.
3228 (let (key-type value-type)
3230 (let ((key (nth 0 option)))
3231 (setq value-type (nth 1 option))
3234 (setq key-type `(const ,key))))
3235 (setq key-type `(const ,option)
3236 value-type widget-plist-value-type))
3237 `(group :format "Key: %v" :inline t ,key-type ,value-type)))
3240 ;;; The `alist' Widget.
3242 ;; Association lists.
3244 (define-widget 'alist 'list
3245 "An association list."
3246 :key-type '(sexp :tag "Key")
3247 :value-type '(sexp :tag "Value")
3248 :convert-widget 'widget-alist-convert-widget
3251 (defvar widget-alist-value-type) ;Dynamic variable
3253 (defun widget-alist-convert-widget (widget)
3254 ;; Handle `:options'.
3255 (let* ((options (widget-get widget :options))
3256 (widget-alist-value-type (widget-get widget :value-type))
3257 (other `(editable-list :inline t
3259 ,(widget-get widget :key-type)
3260 ,widget-alist-value-type)))
3262 (list `(checklist :inline t
3264 ,@(mapcar 'widget-alist-convert-option
3268 (widget-put widget :args args)
3271 (defun widget-alist-convert-option (option)
3272 ;; Convert a single alist option.
3273 (let (key-type value-type)
3275 (let ((key (nth 0 option)))
3276 (setq value-type (nth 1 option))
3279 (setq key-type `(const ,key))))
3280 (setq key-type `(const ,option)
3281 value-type widget-alist-value-type))
3282 `(cons :format "Key: %v" ,key-type ,value-type)))
3284 (define-widget 'choice 'menu-choice
3285 "A union of several sexp types."
3287 :format "%{%t%}: %[Value Menu%] %v"
3288 :button-prefix 'widget-push-button-prefix
3289 :button-suffix 'widget-push-button-suffix
3290 :prompt-value 'widget-choice-prompt-value)
3292 (defun widget-choice-prompt-value (widget prompt value unbound)
3294 (let ((args (widget-get widget :args))
3295 (completion-ignore-case (widget-get widget :case-fold))
3296 current choices old)
3297 ;; Find the first arg that matches VALUE.
3300 (if (widget-apply (car look) :match value)
3301 (setq old (car look)
3303 (setq look (cdr look)))))
3306 (cond ((= (length args) 0)
3308 ((= (length args) 1)
3310 ((and (= (length args) 2)
3312 (if (eq old (nth 0 args))
3317 (setq current (car args)
3320 (cons (cons (widget-apply current :menu-tag-get)
3323 (let ((val (completing-read prompt choices nil t)))
3325 (let ((try (try-completion val choices)))
3328 (cdr (assoc val choices)))
3331 (widget-prompt-value current prompt nil t)
3334 (define-widget 'radio 'radio-button-choice
3335 "A union of several sexp types."
3337 :format "%{%t%}:\n%v"
3338 :prompt-value 'widget-choice-prompt-value)
3340 (define-widget 'repeat 'editable-list
3341 "A variable length homogeneous list."
3343 :format "%{%t%}:\n%v%i\n")
3345 (define-widget 'set 'checklist
3346 "A list of members from a fixed set."
3348 :format "%{%t%}:\n%v")
3350 (define-widget 'boolean 'toggle
3351 "To be nil or non-nil, that is the question."
3353 :prompt-value 'widget-boolean-prompt-value
3354 :button-prefix 'widget-push-button-prefix
3355 :button-suffix 'widget-push-button-suffix
3356 :format "%{%t%}: %[Toggle%] %v\n"
3360 (defun widget-boolean-prompt-value (widget prompt value unbound)
3361 ;; Toggle a boolean.
3364 ;;; The `color' Widget.
3367 (define-widget 'color 'editable-field
3368 "Choose a color name (with sample)."
3369 :format "%t: %v (%{sample%})\n"
3373 :complete 'widget-color-complete
3374 :sample-face-get 'widget-color-sample-face-get
3375 :notify 'widget-color-notify
3376 :action 'widget-color-action)
3378 (defun widget-color-complete (widget)
3379 "Complete the color in WIDGET."
3380 (require 'facemenu) ; for facemenu-color-alist
3381 (let* ((prefix (buffer-substring-no-properties (widget-field-start widget)
3383 (list (or facemenu-color-alist
3384 (mapcar 'list (defined-colors))))
3385 (completion (try-completion prefix list)))
3386 (cond ((eq completion t)
3387 (message "Exact match."))
3389 (error "Can't find completion for \"%s\"" prefix))
3390 ((not (string-equal prefix completion))
3391 (insert-and-inherit (substring completion (length prefix))))
3393 (message "Making completion list...")
3394 (with-output-to-temp-buffer "*Completions*"
3395 (display-completion-list (all-completions prefix list nil)))
3396 (message "Making completion list...done")))))
3398 (defun widget-color-sample-face-get (widget)
3399 (let* ((value (condition-case nil
3400 (widget-value widget)
3401 (error (widget-get widget :value)))))
3402 (if (color-defined-p value)
3403 (list (cons 'foreground-color value))
3406 (defun widget-color-action (widget &optional event)
3407 "Prompt for a color."
3408 (let* ((tag (widget-apply widget :menu-tag-get))
3409 (prompt (concat tag ": "))
3410 (value (widget-value widget))
3411 (start (widget-field-start widget))
3412 (pos (cond ((< (point) start)
3414 ((> (point) (+ start (length value)))
3417 (- (point) start))))
3418 (answer (facemenu-read-color prompt)))
3419 (unless (zerop (length answer))
3420 (widget-value-set widget answer)
3422 (widget-apply widget :notify widget event))))
3424 (defun widget-color-notify (widget child &optional event)
3425 "Update the sample, and notofy the parent."
3426 (overlay-put (widget-get widget :sample-overlay)
3427 'face (widget-apply widget :sample-face-get))
3428 (widget-default-notify widget child event))
3432 (defun widget-echo-help (pos)
3433 "Display help-echo text for widget at POS."
3434 (let* ((widget (widget-at pos))
3435 (help-echo (and widget (widget-get widget :help-echo))))
3436 (if (functionp help-echo)
3437 (setq help-echo (funcall help-echo widget)))
3438 (if (stringp help-echo)
3439 (message "%s" help-echo))))
3445 ;;; wid-edit.el ends here