1 ;;; wid-edit.el --- Functions for creating and using widgets -*-byte-compile-dynamic: t;-*-
3 ;; Copyright (C) 1996, 1997, 1999, 2000 Free Software Foundation, Inc.
5 ;; Author: Per Abrahamsen <abraham@dina.kvl.dk>
7 ;; Keywords: extensions
9 ;; This file is part of GNU Emacs.
11 ;; GNU Emacs is free software; you can redistribute it and/or modify
12 ;; it under the terms of the GNU General Public License as published by
13 ;; the Free Software Foundation; either version 2, or (at your option)
16 ;; GNU Emacs is distributed in the hope that it will be useful,
17 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
18 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 ;; GNU General Public License for more details.
21 ;; You should have received a copy of the GNU General Public License
22 ;; along with GNU Emacs; see the file COPYING. If not, write to the
23 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
24 ;; Boston, MA 02111-1307, USA.
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-mouse-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")))
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 ((overlay (make-overlay from to nil
322 nil (or (not widget-field-add-space)
323 (widget-get widget :size)))))
324 (widget-put widget :field-overlay overlay)
325 ;;(overlay-put overlay 'detachable nil)
326 (overlay-put overlay 'field widget)
327 (overlay-put overlay 'keymap (widget-get widget :keymap))
328 (overlay-put overlay 'face (or (widget-get widget :value-face)
330 (overlay-put overlay 'help-echo (widget-get widget :help-echo)))
331 (widget-specify-secret widget))
333 (defun widget-specify-secret (field)
334 "Replace text in FIELD with value of `:secret', if non-nil."
335 (let ((secret (widget-get field :secret))
336 (size (widget-get field :size)))
338 (let ((begin (widget-field-start field))
339 (end (widget-field-end field)))
341 (while (and (> end begin)
342 (eq (char-after (1- end)) ?\ ))
343 (setq end (1- end))))
345 (let ((old (char-after begin)))
346 (unless (eq old secret)
347 (subst-char-in-region begin (1+ begin) old secret)
348 (put-text-property begin (1+ begin) 'secret old))
349 (setq begin (1+ begin))))))))
351 (defun widget-specify-button (widget from to)
352 "Specify button for WIDGET between FROM and TO."
353 (let ((overlay (make-overlay from to nil t nil)))
354 (widget-put widget :button-overlay overlay)
355 (overlay-put overlay 'button widget)
356 (overlay-put overlay 'keymap (widget-get widget :keymap))
357 ;; We want to avoid the face with image buttons.
358 (unless (widget-get widget :suppress-face)
359 (overlay-put overlay 'face (widget-apply widget :button-face-get))
360 (overlay-put overlay 'mouse-face widget-mouse-face))
361 (overlay-put overlay 'help-echo (widget-get widget :help-echo))))
363 (defun widget-specify-sample (widget from to)
364 "Specify sample for WIDGET between FROM and TO."
365 (let ((overlay (make-overlay from to nil t nil)))
366 (overlay-put overlay 'face (widget-apply widget :sample-face-get))
367 (widget-put widget :sample-overlay overlay)))
369 (defun widget-specify-doc (widget from to)
370 "Specify documentation for WIDGET between FROM and TO."
371 (let ((overlay (make-overlay from to nil t nil)))
372 (overlay-put overlay 'widget-doc widget)
373 (overlay-put overlay 'face widget-documentation-face)
374 (widget-put widget :doc-overlay overlay)))
376 (defmacro widget-specify-insert (&rest form)
377 "Execute FORM without inheriting any text properties."
379 (let ((inhibit-read-only t)
380 (inhibit-modification-hooks t)
383 (narrow-to-region (- (point) 2) (point))
384 (goto-char (1+ (point-min)))
385 (setq result (progn ,@form))
386 (delete-region (point-min) (1+ (point-min)))
387 (delete-region (1- (point-max)) (point-max))
388 (goto-char (point-max))
391 (defface widget-inactive-face '((((class grayscale color)
393 (:foreground "light gray"))
394 (((class grayscale color)
396 (:foreground "dim gray"))
399 "Face used for inactive widgets."
400 :group 'widget-faces)
402 (defun widget-specify-inactive (widget from to)
403 "Make WIDGET inactive for user modifications."
404 (unless (widget-get widget :inactive)
405 (let ((overlay (make-overlay from to nil t nil)))
406 (overlay-put overlay 'face 'widget-inactive-face)
407 ;; This is disabled, as it makes the mouse cursor change shape.
408 ;; (overlay-put overlay 'mouse-face 'widget-inactive-face)
409 (overlay-put overlay 'evaporate t)
410 (overlay-put overlay 'priority 100)
411 (overlay-put overlay 'modification-hooks '(widget-overlay-inactive))
412 (widget-put widget :inactive overlay))))
414 (defun widget-overlay-inactive (&rest junk)
415 "Ignoring the arguments, signal an error."
416 (unless inhibit-read-only
417 (error "The widget here is not active")))
420 (defun widget-specify-active (widget)
421 "Make WIDGET active for user modifications."
422 (let ((inactive (widget-get widget :inactive)))
424 (delete-overlay inactive)
425 (widget-put widget :inactive nil))))
427 ;;; Widget Properties.
429 (defsubst widget-type (widget)
430 "Return the type of WIDGET, a symbol."
433 (defun widget-get-indirect (widget property)
434 "In WIDGET, get the value of PROPERTY.
435 If the value is a symbol, return its binding.
436 Otherwise, just return the value."
437 (let ((value (widget-get widget property)))
442 (defun widget-member (widget property)
443 "Non-nil iff there is a definition in WIDGET for PROPERTY."
444 (cond ((plist-member (cdr widget) property)
447 (widget-member (get (car widget) 'widget-type) property))
450 (defun widget-value (widget)
451 "Extract the current value of WIDGET."
453 :value-to-external (widget-apply widget :value-get)))
455 (defun widget-value-set (widget value)
456 "Set the current value of WIDGET to VALUE."
458 :value-set (widget-apply widget
459 :value-to-internal value)))
461 (defun widget-default-get (widget)
462 "Extract the default value of WIDGET."
463 (or (widget-get widget :value)
464 (widget-apply widget :default-get)))
466 (defun widget-match-inline (widget vals)
467 "In WIDGET, match the start of VALS."
468 (cond ((widget-get widget :inline)
469 (widget-apply widget :match-inline vals))
471 (widget-apply widget :match (car vals)))
472 (cons (list (car vals)) (cdr vals)))
475 (defun widget-apply-action (widget &optional event)
476 "Apply :action in WIDGET in response to EVENT."
477 (if (widget-apply widget :active)
478 (widget-apply widget :action event)
479 (error "Attempt to perform action on inactive widget")))
481 ;;; Helper functions.
483 ;; These are widget specific.
486 (defun widget-prompt-value (widget prompt &optional value unbound)
487 "Prompt for a value matching WIDGET, using PROMPT.
488 The current value is assumed to be VALUE, unless UNBOUND is non-nil."
489 (unless (listp widget)
490 (setq widget (list widget)))
491 (setq prompt (format "[%s] %s" (widget-type widget) prompt))
492 (setq widget (widget-convert widget))
493 (let ((answer (widget-apply widget :prompt-value prompt value unbound)))
494 (unless (widget-apply widget :match answer)
495 (error "Value does not match %S type" (car widget)))
498 (defun widget-get-sibling (widget)
499 "Get the item WIDGET is assumed to toggle.
500 This is only meaningful for radio buttons or checkboxes in a list."
501 (let* ((children (widget-get (widget-get widget :parent) :children))
505 (setq child (car children)
506 children (cdr children))
507 (when (eq (widget-get child :button) widget)
508 (throw 'child child)))
511 (defun widget-map-buttons (function &optional buffer maparg)
512 "Map FUNCTION over the buttons in BUFFER.
513 FUNCTION is called with the arguments WIDGET and MAPARG.
515 If FUNCTION returns non-nil, the walk is cancelled.
517 The arguments MAPARG, and BUFFER default to nil and (current-buffer),
519 (let ((cur (point-min))
523 (save-excursion (set-buffer buffer) (overlay-lists))
525 (setq overlays (append (car overlays) (cdr overlays)))
526 (while (setq cur (pop overlays))
527 (setq widget (overlay-get cur 'button))
528 (if (and widget (funcall function widget maparg))
529 (setq overlays nil)))))
533 (defcustom widget-image-directory (file-name-as-directory
534 (expand-file-name "custom" data-directory))
535 "Where widget button images are located.
536 If this variable is nil, widget will try to locate the directory
541 (defcustom widget-image-enable t
542 "If non nil, use image buttons in widgets when available."
547 (defcustom widget-image-conversion
548 '((xpm ".xpm") (gif ".gif") (png ".png") (jpeg ".jpg" ".jpeg")
550 "Conversion alist from image formats to file name suffixes."
552 :type '(repeat (cons :format "%v"
553 (symbol :tag "Image Format" unknown)
554 (repeat :tag "Suffixes"
555 (string :format "%v")))))
557 (defun widget-image-find (image)
558 "Create a graphical button from IMAGE.
559 IMAGE should either already be an image, or be a file name sans
560 extension (xpm, xbm, gif, jpg, or png) located in
561 `widget-image-directory' or otherwise where `find-image' will find it."
562 (cond ((not (and image widget-image-enable (display-graphic-p)))
563 ;; We don't want or can't use images.
566 (eq 'image (car image)))
567 ;; Already an image spec. Use it.
570 ;; A string. Look it up in relevant directories.
571 (let* ((load-path (cons widget-image-directory load-path))
573 (dolist (elt widget-image-conversion)
574 (dolist (ext (cdr elt))
575 (push (list :type (car elt) :file (concat image ext)) specs)))
576 (setq specs (nreverse specs))
582 (defvar widget-button-pressed-face 'widget-button-pressed-face
583 "Face used for pressed buttons in widgets.
584 This exists as a variable so it can be set locally in certain
587 (defun widget-image-insert (widget tag image &optional down inactive)
588 "In WIDGET, insert the text TAG or, if supported, IMAGE.
589 IMAGE should either be an image or an image file name sans extension
590 \(xpm, xbm, gif, jpg, or png) located in `widget-image-directory'.
592 Optional arguments DOWN and INACTIVE are used instead of IMAGE when the
593 button is pressed or inactive, respectively. These are currently ignored."
594 (if (and (display-graphic-p)
595 (setq image (widget-image-find image)))
596 (progn (widget-put widget :suppress-face t)
599 tag 'mouse-face widget-button-pressed-face)))
604 (defgroup widget-button nil
605 "The look of various kinds of buttons."
608 (defcustom widget-button-prefix ""
609 "String used as prefix for buttons."
611 :group 'widget-button)
613 (defcustom widget-button-suffix ""
614 "String used as suffix for buttons."
616 :group 'widget-button)
618 ;;; Creating Widgets.
621 (defun widget-create (type &rest args)
622 "Create widget of TYPE.
623 The optional ARGS are additional keyword arguments."
624 (let ((widget (apply 'widget-convert type args)))
625 (widget-apply widget :create)
628 (defun widget-create-child-and-convert (parent type &rest args)
629 "As part of the widget PARENT, create a child widget TYPE.
630 The child is converted, using the keyword arguments ARGS."
631 (let ((widget (apply 'widget-convert type args)))
632 (widget-put widget :parent parent)
633 (unless (widget-get widget :indent)
634 (widget-put widget :indent (+ (or (widget-get parent :indent) 0)
635 (or (widget-get widget :extra-offset) 0)
636 (widget-get parent :offset))))
637 (widget-apply widget :create)
640 (defun widget-create-child (parent type)
641 "Create widget of TYPE."
642 (let ((widget (copy-sequence type)))
643 (widget-put widget :parent parent)
644 (unless (widget-get widget :indent)
645 (widget-put widget :indent (+ (or (widget-get parent :indent) 0)
646 (or (widget-get widget :extra-offset) 0)
647 (widget-get parent :offset))))
648 (widget-apply widget :create)
651 (defun widget-create-child-value (parent type value)
652 "Create widget of TYPE with value VALUE."
653 (let ((widget (copy-sequence type)))
654 (widget-put widget :value (widget-apply widget :value-to-internal value))
655 (widget-put widget :parent parent)
656 (unless (widget-get widget :indent)
657 (widget-put widget :indent (+ (or (widget-get parent :indent) 0)
658 (or (widget-get widget :extra-offset) 0)
659 (widget-get parent :offset))))
660 (widget-apply widget :create)
664 (defun widget-delete (widget)
666 (widget-apply widget :delete))
668 (defun widget-convert (type &rest args)
669 "Convert TYPE to a widget without inserting it in the buffer.
670 The optional ARGS are additional keyword arguments."
671 ;; Don't touch the type.
672 (let* ((widget (if (symbolp type)
674 (copy-sequence type)))
677 ;; First set the :args keyword.
678 (while (cdr current) ;Look in the type.
679 (if (keywordp (car (cdr current)))
680 (setq current (cdr (cdr current)))
681 (setcdr current (list :args (cdr current)))
683 (while args ;Look in the args.
684 (if (keywordp (nth 0 args))
685 (setq args (nthcdr 2 args))
686 (widget-put widget :args args)
688 ;; Then Convert the widget.
691 (let ((convert-widget (plist-get (cdr type) :convert-widget)))
693 (setq widget (funcall convert-widget widget))))
694 (setq type (get (car type) 'widget-type)))
695 ;; Finally set the keyword args.
697 (let ((next (nth 0 keys)))
700 (widget-put widget next (nth 1 keys))
701 (setq keys (nthcdr 2 keys)))
703 ;; Convert the :value to internal format.
704 (if (widget-member widget :value)
706 :value (widget-apply widget
708 (widget-get widget :value))))
709 ;; Return the newly create widget.
712 (defun widget-insert (&rest args)
713 "Call `insert' with ARGS even if surrounding text is read only."
714 (let ((inhibit-read-only t)
715 (inhibit-modification-hooks t))
716 (apply 'insert args)))
718 (defun widget-convert-text (type from to
719 &optional button-from button-to
721 "Return a widget of type TYPE with endpoint FROM TO.
722 Optional ARGS are extra keyword arguments for TYPE.
723 and TO will be used as the widgets end points. If optional arguments
724 BUTTON-FROM and BUTTON-TO are given, these will be used as the widgets
726 Optional ARGS are extra keyword arguments for TYPE."
727 (let ((widget (apply 'widget-convert type :delete 'widget-leave-text args))
728 (from (copy-marker from))
729 (to (copy-marker to)))
730 (set-marker-insertion-type from t)
731 (set-marker-insertion-type to nil)
732 (widget-put widget :from from)
733 (widget-put widget :to to)
735 (widget-specify-button widget button-from button-to))
738 (defun widget-convert-button (type from to &rest args)
739 "Return a widget of type TYPE with endpoint FROM TO.
740 Optional ARGS are extra keyword arguments for TYPE.
741 No text will be inserted to the buffer, instead the text between FROM
742 and TO will be used as the widgets end points, as well as the widgets
744 (apply 'widget-convert-text type from to from to args))
746 (defun widget-leave-text (widget)
747 "Remove markers and overlays from WIDGET and its children."
748 (let ((button (widget-get widget :button-overlay))
749 (sample (widget-get widget :sample-overlay))
750 (doc (widget-get widget :doc-overlay))
751 (field (widget-get widget :field-overlay)))
752 (set-marker (widget-get widget :from) nil)
753 (set-marker (widget-get widget :to) nil)
755 (delete-overlay button))
757 (delete-overlay sample))
759 (delete-overlay doc))
761 (delete-overlay field))
762 (mapc 'widget-leave-text (widget-get widget :children))))
764 ;;; Keymap and Commands.
766 (defvar widget-keymap
767 (let ((map (make-sparse-keymap)))
768 (define-key map "\t" 'widget-forward)
769 (define-key map [(shift tab)] 'widget-backward)
770 (define-key map [backtab] 'widget-backward)
771 (define-key map [down-mouse-2] 'widget-button-click)
772 (define-key map "\C-m" 'widget-button-press)
774 "Keymap containing useful binding for buffers containing widgets.
775 Recommended as a parent keymap for modes using widgets.")
777 (defvar widget-global-map global-map
778 "Keymap used for events the widget does not handle themselves.")
779 (make-variable-buffer-local 'widget-global-map)
781 (defvar widget-field-keymap
782 (let ((map (copy-keymap widget-keymap)))
783 (define-key map [menu-bar] nil)
784 (define-key map "\C-k" 'widget-kill-line)
785 (define-key map "\M-\t" 'widget-complete)
786 (define-key map "\C-m" 'widget-field-activate)
787 (define-key map "\C-a" 'widget-beginning-of-line)
788 (define-key map "\C-e" 'widget-end-of-line)
789 (set-keymap-parent map global-map)
791 "Keymap used inside an editable field.")
793 (defvar widget-text-keymap
794 (let ((map (copy-keymap widget-keymap)))
795 (define-key map [menu-bar] 'nil)
796 (define-key map "\C-a" 'widget-beginning-of-line)
797 (define-key map "\C-e" 'widget-end-of-line)
798 (set-keymap-parent map global-map)
800 "Keymap used inside a text field.")
802 (defun widget-field-activate (pos &optional event)
803 "Invoke the ediable field at point."
805 (let ((field (get-char-property pos 'field)))
807 (widget-apply-action field event)
809 (lookup-key widget-global-map (this-command-keys))))))
811 (defface widget-button-pressed-face
815 (:bold t :underline t)))
816 "Face used for pressed buttons."
817 :group 'widget-faces)
819 (defun widget-button-click (event)
820 "Invoke the button that the mouse is pointing at."
822 (if (widget-event-point event)
824 (mouse-set-point event)
825 (let* ((pos (widget-event-point event))
826 (button (get-char-property pos 'button)))
828 (let* ((overlay (widget-get button :button-overlay))
829 (face (overlay-get overlay 'face))
830 (mouse-face (overlay-get overlay 'mouse-face)))
832 (let ((track-mouse t))
834 (when face ; avoid changing around image
836 'face widget-button-pressed-face)
838 'mouse-face widget-button-pressed-face))
839 (unless (widget-apply button :mouse-down-action event)
840 (while (not (widget-button-release-event-p event))
841 (setq event (read-event)
842 pos (widget-event-point event))
844 (eq (get-char-property pos 'button)
849 widget-button-pressed-face)
852 widget-button-pressed-face))
853 (overlay-put overlay 'face face)
854 (overlay-put overlay 'mouse-face mouse-face))))
856 (eq (get-char-property pos 'button) button))
857 (widget-apply-action button event))))
858 (overlay-put overlay 'face face)
859 (overlay-put overlay 'mouse-face mouse-face)))
862 ;; Find the global command to run, and check whether it
863 ;; is bound to an up event.
864 (if (memq (event-basic-type event) '(mouse-1 down-mouse-1))
865 (cond ((setq command ;down event
866 (lookup-key widget-global-map [down-mouse-1]))
868 ((setq command ;up event
869 (lookup-key widget-global-map [mouse-1]))))
870 (cond ((setq command ;down event
871 (lookup-key widget-global-map [down-mouse-2]))
873 ((setq command ;up event
874 (lookup-key widget-global-map [mouse-2])))))
876 ;; Don't execute up events twice.
877 (while (not (widget-button-release-event-p event))
878 (setq event (read-event))))
880 (call-interactively command)))))
881 (unless (pos-visible-in-window-p (widget-event-point event))
882 (mouse-set-point event)
885 (message "You clicked somewhere weird.")))
887 (defun widget-button-press (pos &optional event)
888 "Invoke button at POS."
890 (let ((button (get-char-property pos 'button)))
892 (widget-apply-action button event)
893 (let ((command (lookup-key widget-global-map (this-command-keys))))
894 (when (commandp command)
895 (call-interactively command))))))
897 (defun widget-tabable-at (&optional pos)
898 "Return the tabable widget at POS, or nil.
899 POS defaults to the value of (point)."
902 (let ((widget (or (get-char-property pos 'button)
903 (get-char-property pos 'field))))
905 (let ((order (widget-get widget :tab-order)))
911 (defvar widget-use-overlay-change t
912 "If non-nil, use overlay change functions to tab around in the buffer.
913 This is much faster, but doesn't work reliably on Emacs 19.34.")
915 (defun widget-move (arg)
916 "Move point to the ARG next field or button.
917 ARG may be negative to move backward."
918 (or (bobp) (> arg 0) (backward-char))
921 (old (widget-tabable-at))
926 (goto-char (point-min)))
927 (widget-use-overlay-change
928 (goto-char (next-overlay-change (point))))
931 (and (eq pos (point))
933 (error "No buttons or fields found"))
934 (let ((new (widget-tabable-at)))
942 (goto-char (point-max)))
943 (widget-use-overlay-change
944 (goto-char (previous-overlay-change (point))))
947 (and (eq pos (point))
949 (error "No buttons or fields found"))
950 (let ((new (widget-tabable-at)))
953 (setq arg (1+ arg))))))
954 (let ((new (widget-tabable-at)))
955 (while (eq (widget-tabable-at) new)
958 (widget-echo-help (point))
959 (run-hooks 'widget-move-hook))
961 (defun widget-forward (arg)
962 "Move point to the next field or button.
963 With optional ARG, move across that many fields."
965 (run-hooks 'widget-forward-hook)
968 (defun widget-backward (arg)
969 "Move point to the previous field or button.
970 With optional ARG, move across that many fields."
972 (run-hooks 'widget-backward-hook)
973 (widget-move (- arg)))
975 (defun widget-beginning-of-line ()
976 "Go to beginning of field or beginning of line, whichever is first."
978 (let* ((field (widget-field-find (point)))
979 (start (and field (widget-field-start field))))
981 (max start (line-beginning-position))
982 (line-beginning-position)))))
984 (defun widget-end-of-line ()
985 "Go to end of field or end of line, whichever is first."
987 (let* ((field (widget-field-find (point)))
988 (end (and field (widget-field-end field))))
990 (min end (line-end-position))
991 (line-end-position)))))
993 (defun widget-kill-line ()
994 "Kill to end of field or end of line, whichever is first."
996 (let* ((field (widget-field-find (point)))
997 (end (and field (widget-field-end field))))
998 (if (and field (> (line-beginning-position 2) end))
999 (kill-region (point) end)
1000 (call-interactively 'kill-line))))
1002 (defcustom widget-complete-field (lookup-key global-map "\M-\t")
1003 "Default function to call for completion inside fields."
1004 :options '(ispell-complete-word complete-tag lisp-complete-symbol)
1008 (defun widget-complete ()
1009 "Complete content of editable field from point.
1010 When not inside a field, move to the previous button or field."
1012 (let ((field (widget-field-find (point))))
1014 (widget-apply field :complete)
1015 (error "Not in an editable field"))))
1017 ;;; Setting up the buffer.
1019 (defvar widget-field-new nil)
1020 ;; List of all newly created editable fields in the buffer.
1021 (make-variable-buffer-local 'widget-field-new)
1023 (defvar widget-field-list nil)
1024 ;; List of all editable fields in the buffer.
1025 (make-variable-buffer-local 'widget-field-list)
1027 (defun widget-setup ()
1028 "Setup current buffer so editing string widgets works."
1029 (let ((inhibit-read-only t)
1030 (inhibit-modification-hooks t)
1032 (while widget-field-new
1033 (setq field (car widget-field-new)
1034 widget-field-new (cdr widget-field-new)
1035 widget-field-list (cons field widget-field-list))
1036 (let ((from (car (widget-get field :field-overlay)))
1037 (to (cdr (widget-get field :field-overlay))))
1038 (widget-specify-field field
1039 (marker-position from) (marker-position to))
1040 (set-marker from nil)
1041 (set-marker to nil))))
1043 (widget-add-change))
1045 (defvar widget-field-last nil)
1046 ;; Last field containing point.
1047 (make-variable-buffer-local 'widget-field-last)
1049 (defvar widget-field-was nil)
1050 ;; The widget data before the change.
1051 (make-variable-buffer-local 'widget-field-was)
1053 (defun widget-field-buffer (widget)
1054 "Return the start of WIDGET's editing field."
1055 (let ((overlay (widget-get widget :field-overlay)))
1056 (and overlay (overlay-buffer overlay))))
1058 (defun widget-field-start (widget)
1059 "Return the start of WIDGET's editing field."
1060 (let ((overlay (widget-get widget :field-overlay)))
1061 (and overlay (overlay-start overlay))))
1063 (defun widget-field-end (widget)
1064 "Return the end of WIDGET's editing field."
1065 (let ((overlay (widget-get widget :field-overlay)))
1066 ;; Don't subtract one if local-map works at the end of the overlay.
1067 (and overlay (if (or widget-field-add-space
1068 (null (widget-get widget :size)))
1069 (1- (overlay-end overlay))
1070 (overlay-end overlay)))))
1072 (defun widget-field-find (pos)
1073 "Return the field at POS.
1074 Unlike (get-char-property POS 'field) this, works with empty fields too."
1075 (let ((fields widget-field-list)
1078 (setq field (car fields)
1079 fields (cdr fields))
1080 (when (and (<= (widget-field-start field) pos)
1081 (<= pos (widget-field-end field)))
1083 (error "Overlapping fields"))
1084 (setq found field)))
1087 (defun widget-before-change (from to)
1088 ;; This is how, for example, a variable changes its state to `modified'.
1089 ;; when it is being edited.
1090 (unless inhibit-read-only
1091 (let ((from-field (widget-field-find from))
1092 (to-field (widget-field-find to)))
1093 (cond ((not (eq from-field to-field))
1094 (add-hook 'post-command-hook 'widget-add-change nil t)
1095 (signal 'text-read-only
1096 '("Change should be restricted to a single field")))
1098 (add-hook 'post-command-hook 'widget-add-change nil t)
1099 (signal 'text-read-only
1100 '("Attempt to change text outside editable field")))
1101 (widget-field-use-before-change
1102 (widget-apply from-field :notify from-field))))))
1104 (defun widget-add-change ()
1105 (make-local-hook 'post-command-hook)
1106 (remove-hook 'post-command-hook 'widget-add-change t)
1107 (make-local-hook 'before-change-functions)
1108 (add-hook 'before-change-functions 'widget-before-change nil t)
1109 (make-local-hook 'after-change-functions)
1110 (add-hook 'after-change-functions 'widget-after-change nil t))
1112 (defun widget-after-change (from to old)
1113 "Adjust field size and text properties."
1114 (let ((field (widget-field-find from))
1115 (other (widget-field-find to)))
1117 (unless (eq field other)
1118 (error "Change in different fields"))
1119 (let ((size (widget-get field :size)))
1121 (let ((begin (widget-field-start field))
1122 (end (widget-field-end field)))
1123 (cond ((< (- end begin) size)
1127 (insert-char ?\ (- (+ begin size) end))))
1128 ((> (- end begin) size)
1129 ;; Field too large and
1130 (if (or (< (point) (+ begin size))
1132 ;; Point is outside extra space.
1133 (setq begin (+ begin size))
1134 ;; Point is within the extra space.
1135 (setq begin (point)))
1138 (while (and (eq (preceding-char) ?\ )
1140 (delete-backward-char 1)))))))
1141 (widget-specify-secret field))
1142 (widget-apply field :notify field))))
1144 ;;; Widget Functions
1146 ;; These functions are used in the definition of multiple widgets.
1148 (defun widget-parent-action (widget &optional event)
1149 "Tell :parent of WIDGET to handle the :action.
1150 Optional EVENT is the event that triggered the action."
1151 (widget-apply (widget-get widget :parent) :action event))
1153 (defun widget-children-value-delete (widget)
1154 "Delete all :children and :buttons in WIDGET."
1155 (mapc 'widget-delete (widget-get widget :children))
1156 (widget-put widget :children nil)
1157 (mapc 'widget-delete (widget-get widget :buttons))
1158 (widget-put widget :buttons nil))
1160 (defun widget-children-validate (widget)
1161 "All the :children must be valid."
1162 (let ((children (widget-get widget :children))
1164 (while (and children (not found))
1165 (setq child (car children)
1166 children (cdr children)
1167 found (widget-apply child :validate)))
1170 ;; Made defsubst to speed up face editor creation.
1171 (defsubst widget-types-convert-widget (widget)
1172 "Convert :args as widget types in WIDGET."
1173 (widget-put widget :args (mapcar 'widget-convert (widget-get widget :args)))
1176 (defun widget-value-convert-widget (widget)
1177 "Initialize :value from :args in WIDGET."
1178 (let ((args (widget-get widget :args)))
1180 (widget-put widget :value (car args))
1181 ;; Don't convert :value here, as this is done in `widget-convert'.
1182 ;; (widget-put widget :value (widget-apply widget
1183 ;; :value-to-internal (car args)))
1184 (widget-put widget :args nil)))
1187 (defun widget-value-value-get (widget)
1188 "Return the :value property of WIDGET."
1189 (widget-get widget :value))
1191 ;;; The `default' Widget.
1193 (define-widget 'default nil
1194 "Basic widget other widgets are derived from."
1195 :value-to-internal (lambda (widget value) value)
1196 :value-to-external (lambda (widget value) value)
1197 :button-prefix 'widget-button-prefix
1198 :button-suffix 'widget-button-suffix
1199 :complete 'widget-default-complete
1200 :create 'widget-default-create
1203 :format-handler 'widget-default-format-handler
1204 :button-face-get 'widget-default-button-face-get
1205 :sample-face-get 'widget-default-sample-face-get
1206 :delete 'widget-default-delete
1207 :value-set 'widget-default-value-set
1208 :value-inline 'widget-default-value-inline
1209 :default-get 'widget-default-default-get
1210 :menu-tag-get 'widget-default-menu-tag-get
1212 :active 'widget-default-active
1213 :activate 'widget-specify-active
1214 :deactivate 'widget-default-deactivate
1215 :mouse-down-action #'ignore
1216 :action 'widget-default-action
1217 :notify 'widget-default-notify
1218 :prompt-value 'widget-default-prompt-value)
1220 (defun widget-default-complete (widget)
1221 "Call the value of the :complete-function property of WIDGET.
1222 If that does not exists, call the value of `widget-complete-field'."
1223 (call-interactively (or (widget-get widget :complete-function)
1224 widget-complete-field)))
1226 (defun widget-default-create (widget)
1227 "Create WIDGET at point in the current buffer."
1228 (widget-specify-insert
1229 (let ((from (point))
1230 button-begin button-end
1231 sample-begin sample-end
1234 (insert (widget-get widget :format))
1236 ;; Parse escapes in format.
1237 (while (re-search-forward "%\\(.\\)" nil t)
1238 (let ((escape (char-after (match-beginning 1))))
1239 (delete-backward-char 2)
1240 (cond ((eq escape ?%)
1243 (setq button-begin (point))
1244 (insert (widget-get-indirect widget :button-prefix)))
1246 (insert (widget-get-indirect widget :button-suffix))
1247 (setq button-end (point)))
1249 (setq sample-begin (point)))
1251 (setq sample-end (point)))
1253 (when (widget-get widget :indent)
1255 (insert-char ? (widget-get widget :indent))))
1257 (let ((image (widget-get widget :tag-glyph))
1258 (tag (widget-get widget :tag)))
1260 (widget-image-insert widget (or tag "image") image))
1264 (princ (widget-get widget :value)
1265 (current-buffer))))))
1267 (let ((doc (widget-get widget :doc)))
1269 (setq doc-begin (point))
1271 (while (eq (preceding-char) ?\n)
1272 (delete-backward-char 1))
1274 (setq doc-end (point)))))
1276 (if (and button-begin (not button-end))
1277 (widget-apply widget :value-create)
1278 (setq value-pos (point))))
1280 (widget-apply widget :format-handler escape)))))
1281 ;; Specify button, sample, and doc, and insert value.
1282 (and button-begin button-end
1283 (widget-specify-button widget button-begin button-end))
1284 (and sample-begin sample-end
1285 (widget-specify-sample widget sample-begin sample-end))
1286 (and doc-begin doc-end
1287 (widget-specify-doc widget doc-begin doc-end))
1289 (goto-char value-pos)
1290 (widget-apply widget :value-create)))
1291 (let ((from (point-min-marker))
1292 (to (point-max-marker)))
1293 (set-marker-insertion-type from t)
1294 (set-marker-insertion-type to nil)
1295 (widget-put widget :from from)
1296 (widget-put widget :to to)))
1297 (widget-clear-undo))
1299 (defun widget-default-format-handler (widget escape)
1300 ;; We recognize the %h escape by default.
1301 (let* ((buttons (widget-get widget :buttons)))
1302 (cond ((eq escape ?h)
1303 (let* ((doc-property (widget-get widget :documentation-property))
1304 (doc-try (cond ((widget-get widget :doc))
1305 ((symbolp doc-property)
1306 (documentation-property
1307 (widget-get widget :value)
1310 (funcall doc-property
1311 (widget-get widget :value)))))
1312 (doc-text (and (stringp doc-try)
1313 (> (length doc-try) 1)
1315 (doc-indent (widget-get widget :documentation-indent)))
1317 (and (eq (preceding-char) ?\n)
1318 (widget-get widget :indent)
1319 (insert-char ? (widget-get widget :indent)))
1320 ;; The `*' in the beginning is redundant.
1321 (when (eq (aref doc-text 0) ?*)
1322 (setq doc-text (substring doc-text 1)))
1323 ;; Get rid of trailing newlines.
1324 (when (string-match "\n+\\'" doc-text)
1325 (setq doc-text (substring doc-text 0 (match-beginning 0))))
1326 (push (widget-create-child-and-convert
1327 widget 'documentation-string
1328 :indent (cond ((numberp doc-indent )
1336 (error "Unknown escape `%c'" escape)))
1337 (widget-put widget :buttons buttons)))
1339 (defun widget-default-button-face-get (widget)
1340 ;; Use :button-face or widget-button-face
1341 (or (widget-get widget :button-face)
1342 (let ((parent (widget-get widget :parent)))
1344 (widget-apply parent :button-face-get)
1345 widget-button-face))))
1347 (defun widget-default-sample-face-get (widget)
1348 ;; Use :sample-face.
1349 (widget-get widget :sample-face))
1351 (defun widget-default-delete (widget)
1352 "Remove widget from the buffer."
1353 (let ((from (widget-get widget :from))
1354 (to (widget-get widget :to))
1355 (inactive-overlay (widget-get widget :inactive))
1356 (button-overlay (widget-get widget :button-overlay))
1357 (sample-overlay (widget-get widget :sample-overlay))
1358 (doc-overlay (widget-get widget :doc-overlay))
1359 (inhibit-modification-hooks t)
1360 (inhibit-read-only t))
1361 (widget-apply widget :value-delete)
1362 (when inactive-overlay
1363 (delete-overlay inactive-overlay))
1364 (when button-overlay
1365 (delete-overlay button-overlay))
1366 (when sample-overlay
1367 (delete-overlay sample-overlay))
1369 (delete-overlay doc-overlay))
1371 ;; Kludge: this doesn't need to be true for empty formats.
1372 (delete-region from to))
1373 (set-marker from nil)
1374 (set-marker to nil))
1375 (widget-clear-undo))
1377 (defun widget-default-value-set (widget value)
1378 "Recreate widget with new value."
1379 (let* ((old-pos (point))
1380 (from (copy-marker (widget-get widget :from)))
1381 (to (copy-marker (widget-get widget :to)))
1382 (offset (if (and (<= from old-pos) (<= old-pos to))
1383 (if (>= old-pos (1- to))
1385 (- old-pos from)))))
1386 ;;??? Bug: this ought to insert the new value before deleting the old one,
1387 ;; so that markers on either side of the value automatically
1388 ;; stay on the same side. -- rms.
1390 (goto-char (widget-get widget :from))
1391 (widget-apply widget :delete)
1392 (widget-put widget :value value)
1393 (widget-apply widget :create))
1396 (goto-char (+ (widget-get widget :to) offset 1))
1397 (goto-char (min (+ from offset) (1- (widget-get widget :to))))))))
1399 (defun widget-default-value-inline (widget)
1400 "Wrap value in a list unless it is inline."
1401 (if (widget-get widget :inline)
1402 (widget-value widget)
1403 (list (widget-value widget))))
1405 (defun widget-default-default-get (widget)
1407 (widget-get widget :value))
1409 (defun widget-default-menu-tag-get (widget)
1410 "Use tag or value for menus."
1411 (or (widget-get widget :menu-tag)
1412 (widget-get widget :tag)
1413 (widget-princ-to-string (widget-get widget :value))))
1415 (defun widget-default-active (widget)
1416 "Return t iff this widget active (user modifiable)."
1417 (or (widget-get widget :always-active)
1418 (and (not (widget-get widget :inactive))
1419 (let ((parent (widget-get widget :parent)))
1421 (widget-apply parent :active))))))
1423 (defun widget-default-deactivate (widget)
1424 "Make WIDGET inactive for user modifications."
1425 (widget-specify-inactive widget
1426 (widget-get widget :from)
1427 (widget-get widget :to)))
1429 (defun widget-default-action (widget &optional event)
1430 "Notify the parent when a widget changes."
1431 (let ((parent (widget-get widget :parent)))
1433 (widget-apply parent :notify widget event))))
1435 (defun widget-default-notify (widget child &optional event)
1436 "Pass notification to parent."
1437 (widget-default-action widget event))
1439 (defun widget-default-prompt-value (widget prompt value unbound)
1440 "Read an arbitrary value. Stolen from `set-variable'."
1441 ;; (let ((initial (if unbound
1443 ;; It would be nice if we could do a `(cons val 1)' here.
1444 ;; (prin1-to-string (custom-quote value))))))
1445 (eval-minibuffer prompt))
1447 ;;; The `item' Widget.
1449 (define-widget 'item 'default
1450 "Constant items for inclusion in other widgets."
1451 :convert-widget 'widget-value-convert-widget
1452 :value-create 'widget-item-value-create
1453 :value-delete 'ignore
1454 :value-get 'widget-value-value-get
1455 :match 'widget-item-match
1456 :match-inline 'widget-item-match-inline
1457 :action 'widget-item-action
1460 (defun widget-item-value-create (widget)
1461 "Insert the printed representation of the value."
1462 (princ (widget-get widget :value) (current-buffer)))
1464 (defun widget-item-match (widget value)
1465 ;; Match if the value is the same.
1466 (equal (widget-get widget :value) value))
1468 (defun widget-item-match-inline (widget values)
1469 ;; Match if the value is the same.
1470 (let ((value (widget-get widget :value)))
1472 (<= (length value) (length values))
1473 (let ((head (widget-sublist values 0 (length value))))
1474 (and (equal head value)
1475 (cons head (widget-sublist values (length value))))))))
1477 (defun widget-sublist (list start &optional end)
1478 "Return the sublist of LIST from START to END.
1479 If END is omitted, it defaults to the length of LIST."
1480 (if (> start 0) (setq list (nthcdr start list)))
1482 (unless (<= end start)
1483 (setq list (copy-sequence list))
1484 (setcdr (nthcdr (- end start 1) list) nil)
1486 (copy-sequence list)))
1488 (defun widget-item-action (widget &optional event)
1489 ;; Just notify itself.
1490 (widget-apply widget :notify widget event))
1492 ;;; The `push-button' Widget.
1494 ;; (defcustom widget-push-button-gui t
1495 ;; "If non nil, use GUI push buttons when available."
1499 ;; Cache already created GUI objects.
1500 ;; (defvar widget-push-button-cache nil)
1502 (defcustom widget-push-button-prefix "["
1503 "String used as prefix for buttons."
1505 :group 'widget-button)
1507 (defcustom widget-push-button-suffix "]"
1508 "String used as suffix for buttons."
1510 :group 'widget-button)
1512 (define-widget 'push-button 'item
1513 "A pushable button."
1516 :value-create 'widget-push-button-value-create
1519 (defun widget-push-button-value-create (widget)
1520 "Insert text representing the `on' and `off' states."
1521 (let* ((tag (or (widget-get widget :tag)
1522 (widget-get widget :value)))
1523 (tag-glyph (widget-get widget :tag-glyph))
1524 (text (concat widget-push-button-prefix
1525 tag widget-push-button-suffix)))
1527 (widget-image-insert widget text tag-glyph)
1530 ;; (defun widget-gui-action (widget)
1531 ;; "Apply :action for WIDGET."
1532 ;; (widget-apply-action widget (this-command-keys)))
1534 ;;; The `link' Widget.
1536 (defcustom widget-link-prefix "["
1537 "String used as prefix for links."
1539 :group 'widget-button)
1541 (defcustom widget-link-suffix "]"
1542 "String used as suffix for links."
1544 :group 'widget-button)
1546 (define-widget 'link 'item
1548 :button-prefix 'widget-link-prefix
1549 :button-suffix 'widget-link-suffix
1550 :help-echo "Follow the link."
1553 ;;; The `info-link' Widget.
1555 (define-widget 'info-link 'link
1556 "A link to an info file."
1557 :action 'widget-info-link-action)
1559 (defun widget-info-link-action (widget &optional event)
1560 "Open the info node specified by WIDGET."
1561 (Info-goto-node (widget-value widget)))
1563 ;;; The `url-link' Widget.
1565 (define-widget 'url-link 'link
1566 "A link to an www page."
1567 :action 'widget-url-link-action)
1569 (defun widget-url-link-action (widget &optional event)
1570 "Open the url specified by WIDGET."
1571 (browse-url (widget-value widget)))
1573 ;;; The `function-link' Widget.
1575 (define-widget 'function-link 'link
1576 "A link to an Emacs function."
1577 :action 'widget-function-link-action)
1579 (defun widget-function-link-action (widget &optional event)
1580 "Show the function specified by WIDGET."
1581 (describe-function (widget-value widget)))
1583 ;;; The `variable-link' Widget.
1585 (define-widget 'variable-link 'link
1586 "A link to an Emacs variable."
1587 :action 'widget-variable-link-action)
1589 (defun widget-variable-link-action (widget &optional event)
1590 "Show the variable specified by WIDGET."
1591 (describe-variable (widget-value widget)))
1593 ;;; The `file-link' Widget.
1595 (define-widget 'file-link 'link
1597 :action 'widget-file-link-action)
1599 (defun widget-file-link-action (widget &optional event)
1600 "Find the file specified by WIDGET."
1601 (find-file (widget-value widget)))
1603 ;;; The `emacs-library-link' Widget.
1605 (define-widget 'emacs-library-link 'link
1606 "A link to an Emacs Lisp library file."
1607 :action 'widget-emacs-library-link-action)
1609 (defun widget-emacs-library-link-action (widget &optional event)
1610 "Find the Emacs Library file specified by WIDGET."
1611 (find-file (locate-library (widget-value widget))))
1613 ;;; The `emacs-commentary-link' Widget.
1615 (define-widget 'emacs-commentary-link 'link
1616 "A link to Commentary in an Emacs Lisp library file."
1617 :action 'widget-emacs-commentary-link-action)
1619 (defun widget-emacs-commentary-link-action (widget &optional event)
1620 "Find the Commentary section of the Emacs file specified by WIDGET."
1621 (finder-commentary (widget-value widget)))
1623 ;;; The `editable-field' Widget.
1625 (define-widget 'editable-field 'default
1626 "An editable text field."
1627 :convert-widget 'widget-value-convert-widget
1628 :keymap widget-field-keymap
1630 :help-echo "M-TAB: complete field; RET: enter value"
1632 :prompt-internal 'widget-field-prompt-internal
1633 :prompt-history 'widget-field-history
1634 :prompt-value 'widget-field-prompt-value
1635 :action 'widget-field-action
1636 :validate 'widget-field-validate
1639 :value-create 'widget-field-value-create
1640 :value-delete 'widget-field-value-delete
1641 :value-get 'widget-field-value-get
1642 :match 'widget-field-match)
1644 (defvar widget-field-history nil
1645 "History of field minibuffer edits.")
1647 (defun widget-field-prompt-internal (widget prompt initial history)
1648 "Read string for WIDGET promptinhg with PROMPT.
1649 INITIAL is the initial input and HISTORY is a symbol containing
1651 (read-string prompt initial history))
1653 (defun widget-field-prompt-value (widget prompt value unbound)
1654 "Prompt for a string."
1655 (widget-apply widget
1657 (widget-apply widget
1658 :prompt-internal prompt
1660 (cons (widget-apply widget
1661 :value-to-internal value)
1663 (widget-get widget :prompt-history))))
1665 (defvar widget-edit-functions nil)
1667 (defun widget-field-action (widget &optional event)
1668 "Move to next field."
1670 (run-hook-with-args 'widget-edit-functions widget))
1672 (defun widget-field-validate (widget)
1673 "Valid if the content matches `:valid-regexp'."
1674 (unless (string-match (widget-get widget :valid-regexp)
1675 (widget-apply widget :value-get))
1678 (defun widget-field-value-create (widget)
1679 "Create an editable text field."
1680 (let ((size (widget-get widget :size))
1681 (value (widget-get widget :value))
1683 ;; This is changed to a real overlay in `widget-setup'. We
1684 ;; need the end points to behave differently until
1685 ;; `widget-setup' is called.
1686 (overlay (cons (make-marker) (make-marker))))
1687 (widget-put widget :field-overlay overlay)
1690 (< (length value) size)
1691 (insert-char ?\ (- size (length value))))
1692 (unless (memq widget widget-field-list)
1693 (setq widget-field-new (cons widget widget-field-new)))
1694 (move-marker (cdr overlay) (point))
1695 (set-marker-insertion-type (cdr overlay) nil)
1698 (move-marker (car overlay) from)
1699 (set-marker-insertion-type (car overlay) t)))
1701 (defun widget-field-value-delete (widget)
1702 "Remove the widget from the list of active editing fields."
1703 (setq widget-field-list (delq widget widget-field-list))
1704 ;; These are nil if the :format string doesn't contain `%v'.
1705 (let ((overlay (widget-get widget :field-overlay)))
1707 (delete-overlay overlay))))
1709 (defun widget-field-value-get (widget)
1710 "Return current text in editing field."
1711 (let ((from (widget-field-start widget))
1712 (to (widget-field-end widget))
1713 (buffer (widget-field-buffer widget))
1714 (size (widget-get widget :size))
1715 (secret (widget-get widget :secret))
1716 (old (current-buffer)))
1723 (eq (char-after (1- to)) ?\ ))
1725 (let ((result (buffer-substring-no-properties from to)))
1728 (while (< (+ from index) to)
1730 (get-char-property (+ from index) 'secret))
1731 (setq index (1+ index)))))
1734 (widget-get widget :value))))
1736 (defun widget-field-match (widget value)
1737 ;; Match any string.
1740 ;;; The `text' Widget.
1742 (define-widget 'text 'editable-field
1743 :keymap widget-text-keymap
1744 "A multiline text area.")
1746 ;;; The `menu-choice' Widget.
1748 (define-widget 'menu-choice 'default
1749 "A menu of options."
1750 :convert-widget 'widget-types-convert-widget
1751 :format "%[%t%]: %v"
1754 :void '(item :format "invalid (%t)\n")
1755 :value-create 'widget-choice-value-create
1756 :value-delete 'widget-children-value-delete
1757 :value-get 'widget-choice-value-get
1758 :value-inline 'widget-choice-value-inline
1759 :default-get 'widget-choice-default-get
1760 :mouse-down-action 'widget-choice-mouse-down-action
1761 :action 'widget-choice-action
1762 :error "Make a choice"
1763 :validate 'widget-choice-validate
1764 :match 'widget-choice-match
1765 :match-inline 'widget-choice-match-inline)
1767 (defun widget-choice-value-create (widget)
1768 "Insert the first choice that matches the value."
1769 (let ((value (widget-get widget :value))
1770 (args (widget-get widget :args))
1771 (explicit (widget-get widget :explicit-choice))
1773 (if (and explicit (equal value (widget-get widget :explicit-choice-value)))
1775 ;; If the user specified the choice for this value,
1776 ;; respect that choice as long as the value is the same.
1777 (widget-put widget :children (list (widget-create-child-value
1778 widget explicit value)))
1779 (widget-put widget :choice explicit))
1781 (setq current (car args)
1783 (when (widget-apply current :match value)
1784 (widget-put widget :children (list (widget-create-child-value
1785 widget current value)))
1786 (widget-put widget :choice current)
1790 (let ((void (widget-get widget :void)))
1791 (widget-put widget :children (list (widget-create-child-and-convert
1792 widget void :value value)))
1793 (widget-put widget :choice void))))))
1795 (defun widget-choice-value-get (widget)
1796 ;; Get value of the child widget.
1797 (widget-value (car (widget-get widget :children))))
1799 (defun widget-choice-value-inline (widget)
1800 ;; Get value of the child widget.
1801 (widget-apply (car (widget-get widget :children)) :value-inline))
1803 (defun widget-choice-default-get (widget)
1804 ;; Get default for the first choice.
1805 (widget-default-get (car (widget-get widget :args))))
1807 (defcustom widget-choice-toggle nil
1808 "If non-nil, a binary choice will just toggle between the values.
1809 Otherwise, the user will explicitly have to choose between the values
1810 when he invoked the menu."
1814 (defun widget-choice-mouse-down-action (widget &optional event)
1815 ;; Return non-nil if we need a menu.
1816 (let ((args (widget-get widget :args))
1817 (old (widget-get widget :choice)))
1818 (cond ((not (display-popup-menus-p))
1819 ;; No place to pop up a menu.
1821 ((< (length args) 2)
1822 ;; Empty or singleton list, just return the value.
1824 ((> (length args) widget-menu-max-size)
1825 ;; Too long, prompt.
1827 ((> (length args) 2)
1828 ;; Reasonable sized list, use menu.
1830 ((and widget-choice-toggle (memq old args))
1834 ;; Ask which of the two.
1837 (defun widget-choice-action (widget &optional event)
1839 (let ((args (widget-get widget :args))
1840 (old (widget-get widget :choice))
1841 (tag (widget-apply widget :menu-tag-get))
1842 (completion-ignore-case (widget-get widget :case-fold))
1845 ;; Remember old value.
1846 (if (and old (not (widget-apply widget :validate)))
1847 (let* ((external (widget-value widget))
1848 (internal (widget-apply old :value-to-internal external)))
1849 (widget-put old :value internal)))
1852 (cond ((= (length args) 0)
1854 ((= (length args) 1)
1856 ((and widget-choice-toggle
1859 (if (eq old (nth 0 args))
1864 (setq current (car args)
1867 (cons (cons (widget-apply current :menu-tag-get)
1870 (setq this-explicit t)
1871 (widget-choose tag (reverse choices) event))))
1873 ;; If this was an explicit user choice,
1874 ;; record the choice, and the record the value it was made for.
1875 ;; widget-choice-value-create will respect this choice,
1876 ;; as long as the value is the same.
1878 (widget-put widget :explicit-choice current)
1879 (widget-put widget :explicit-choice-value (widget-get widget :value)))
1881 widget (widget-apply current
1882 :value-to-external (widget-default-get current)))
1884 (widget-apply widget :notify widget event)))
1885 (run-hook-with-args 'widget-edit-functions widget))
1887 (defun widget-choice-validate (widget)
1888 ;; Valid if we have made a valid choice.
1889 (if (eq (widget-get widget :void) (widget-get widget :choice))
1891 (widget-apply (car (widget-get widget :children)) :validate)))
1893 (defun widget-choice-match (widget value)
1894 ;; Matches if one of the choices matches.
1895 (let ((args (widget-get widget :args))
1897 (while (and args (not found))
1898 (setq current (car args)
1900 found (widget-apply current :match value)))
1903 (defun widget-choice-match-inline (widget values)
1904 ;; Matches if one of the choices matches.
1905 (let ((args (widget-get widget :args))
1907 (while (and args (null found))
1908 (setq current (car args)
1910 found (widget-match-inline current values)))
1913 ;;; The `toggle' Widget.
1915 (define-widget 'toggle 'item
1916 "Toggle between two states."
1918 :value-create 'widget-toggle-value-create
1919 :action 'widget-toggle-action
1920 :match (lambda (widget value) t)
1924 (defun widget-toggle-value-create (widget)
1925 "Insert text representing the `on' and `off' states."
1926 (if (widget-value widget)
1927 (widget-image-insert widget
1928 (widget-get widget :on)
1929 (widget-get widget :on-glyph))
1930 (widget-image-insert widget
1931 (widget-get widget :off)
1932 (widget-get widget :off-glyph))))
1934 (defun widget-toggle-action (widget &optional event)
1936 (widget-value-set widget (not (widget-value widget)))
1937 (widget-apply widget :notify widget event)
1938 (run-hook-with-args 'widget-edit-functions widget))
1940 ;;; The `checkbox' Widget.
1942 (define-widget 'checkbox 'toggle
1943 "A checkbox toggle."
1948 ;; We could probably do the same job as the images using single
1949 ;; space characters in a boxed face with a stretch specification to
1950 ;; make them square.
1951 :on-glyph (create-image (make-bool-vector 49 1)
1952 'xbm t :width 7 :height 7
1953 :foreground "grey75" ; like default mode line
1954 :relief -3 :ascent 'center)
1956 :off-glyph (create-image (make-bool-vector 49 1)
1957 'xbm t :width 7 :height 7
1958 :foreground "grey75"
1959 :relief 3 :ascent 'center)
1960 :help-echo "Toggle this item."
1961 :action 'widget-checkbox-action)
1963 (defun widget-checkbox-action (widget &optional event)
1964 "Toggle checkbox, notify parent, and set active state of sibling."
1965 (widget-toggle-action widget event)
1966 (let ((sibling (widget-get-sibling widget)))
1968 (if (widget-value widget)
1969 (widget-apply sibling :activate)
1970 (widget-apply sibling :deactivate)))))
1972 ;;; The `checklist' Widget.
1974 (define-widget 'checklist 'default
1975 "A multiple choice widget."
1976 :convert-widget 'widget-types-convert-widget
1979 :entry-format "%b %v"
1980 :menu-tag "checklist"
1982 :value-create 'widget-checklist-value-create
1983 :value-delete 'widget-children-value-delete
1984 :value-get 'widget-checklist-value-get
1985 :validate 'widget-checklist-validate
1986 :match 'widget-checklist-match
1987 :match-inline 'widget-checklist-match-inline)
1989 (defun widget-checklist-value-create (widget)
1990 ;; Insert all values
1991 (let ((alist (widget-checklist-match-find widget (widget-get widget :value)))
1992 (args (widget-get widget :args)))
1994 (widget-checklist-add-item widget (car args) (assq (car args) alist))
1995 (setq args (cdr args)))
1996 (widget-put widget :children (nreverse (widget-get widget :children)))))
1998 (defun widget-checklist-add-item (widget type chosen)
1999 "Create checklist item in WIDGET of type TYPE.
2000 If the item is checked, CHOSEN is a cons whose cdr is the value."
2001 (and (eq (preceding-char) ?\n)
2002 (widget-get widget :indent)
2003 (insert-char ? (widget-get widget :indent)))
2004 (widget-specify-insert
2005 (let* ((children (widget-get widget :children))
2006 (buttons (widget-get widget :buttons))
2007 (button-args (or (widget-get type :sibling-args)
2008 (widget-get widget :button-args)))
2011 (insert (widget-get widget :entry-format))
2013 ;; Parse % escapes in format.
2014 (while (re-search-forward "%\\([bv%]\\)" nil t)
2015 (let ((escape (char-after (match-beginning 1))))
2016 (delete-backward-char 2)
2017 (cond ((eq escape ?%)
2020 (setq button (apply 'widget-create-child-and-convert
2022 :value (not (null chosen))
2027 (let ((child (widget-create-child widget type)))
2028 (widget-apply child :deactivate)
2030 ((widget-get type :inline)
2031 (widget-create-child-value
2032 widget type (cdr chosen)))
2034 (widget-create-child-value
2035 widget type (car (cdr chosen)))))))
2037 (error "Unknown escape `%c'" escape)))))
2038 ;; Update properties.
2039 (and button child (widget-put child :button button))
2040 (and button (widget-put widget :buttons (cons button buttons)))
2041 (and child (widget-put widget :children (cons child children))))))
2043 (defun widget-checklist-match (widget values)
2044 ;; All values must match a type in the checklist.
2046 (null (cdr (widget-checklist-match-inline widget values)))))
2048 (defun widget-checklist-match-inline (widget values)
2049 ;; Find the values which match a type in the checklist.
2050 (let ((greedy (widget-get widget :greedy))
2051 (args (copy-sequence (widget-get widget :args)))
2054 (let ((answer (widget-checklist-match-up args values)))
2056 (let ((vals (widget-match-inline answer values)))
2057 (setq found (append found (car vals))
2059 args (delq answer args))))
2061 (setq rest (append rest (list (car values)))
2062 values (cdr values)))
2064 (setq rest (append rest values)
2068 (defun widget-checklist-match-find (widget vals)
2069 "Find the vals which match a type in the checklist.
2070 Return an alist of (TYPE MATCH)."
2071 (let ((greedy (widget-get widget :greedy))
2072 (args (copy-sequence (widget-get widget :args)))
2075 (let ((answer (widget-checklist-match-up args vals)))
2077 (let ((match (widget-match-inline answer vals)))
2078 (setq found (cons (cons answer (car match)) found)
2080 args (delq answer args))))
2082 (setq vals (cdr vals)))
2087 (defun widget-checklist-match-up (args vals)
2088 "Return the first type from ARGS that matches VALS."
2089 (let (current found)
2090 (while (and args (null found))
2091 (setq current (car args)
2093 found (widget-match-inline current vals)))
2097 (defun widget-checklist-value-get (widget)
2098 ;; The values of all selected items.
2099 (let ((children (widget-get widget :children))
2102 (setq child (car children)
2103 children (cdr children))
2104 (if (widget-value (widget-get child :button))
2105 (setq result (append result (widget-apply child :value-inline)))))
2108 (defun widget-checklist-validate (widget)
2109 ;; Ticked chilren must be valid.
2110 (let ((children (widget-get widget :children))
2112 (while (and children (not found))
2113 (setq child (car children)
2114 children (cdr children)
2115 button (widget-get child :button)
2116 found (and (widget-value button)
2117 (widget-apply child :validate))))
2120 ;;; The `option' Widget
2122 (define-widget 'option 'checklist
2123 "An widget with an optional item."
2126 ;;; The `choice-item' Widget.
2128 (define-widget 'choice-item 'item
2129 "Button items that delegate action events to their parents."
2130 :action 'widget-parent-action
2131 :format "%[%t%] \n")
2133 ;;; The `radio-button' Widget.
2135 (define-widget 'radio-button 'toggle
2136 "A radio button for use in the `radio' widget."
2137 :notify 'widget-radio-button-notify
2144 :off-glyph "radio0")
2146 (defun widget-radio-button-notify (widget child &optional event)
2148 (widget-apply (widget-get widget :parent) :action widget event))
2150 ;;; The `radio-button-choice' Widget.
2152 (define-widget 'radio-button-choice 'default
2153 "Select one of multiple options."
2154 :convert-widget 'widget-types-convert-widget
2157 :entry-format "%b %v"
2159 :value-create 'widget-radio-value-create
2160 :value-delete 'widget-children-value-delete
2161 :value-get 'widget-radio-value-get
2162 :value-inline 'widget-radio-value-inline
2163 :value-set 'widget-radio-value-set
2164 :error "You must push one of the buttons"
2165 :validate 'widget-radio-validate
2166 :match 'widget-choice-match
2167 :match-inline 'widget-choice-match-inline
2168 :action 'widget-radio-action)
2170 (defun widget-radio-value-create (widget)
2171 ;; Insert all values
2172 (let ((args (widget-get widget :args))
2175 (setq arg (car args)
2177 (widget-radio-add-item widget arg))))
2179 (defun widget-radio-add-item (widget type)
2180 "Add to radio widget WIDGET a new radio button item of type TYPE."
2181 ;; (setq type (widget-convert type))
2182 (and (eq (preceding-char) ?\n)
2183 (widget-get widget :indent)
2184 (insert-char ? (widget-get widget :indent)))
2185 (widget-specify-insert
2186 (let* ((value (widget-get widget :value))
2187 (children (widget-get widget :children))
2188 (buttons (widget-get widget :buttons))
2189 (button-args (or (widget-get type :sibling-args)
2190 (widget-get widget :button-args)))
2192 (chosen (and (null (widget-get widget :choice))
2193 (widget-apply type :match value)))
2195 (insert (widget-get widget :entry-format))
2197 ;; Parse % escapes in format.
2198 (while (re-search-forward "%\\([bv%]\\)" nil t)
2199 (let ((escape (char-after (match-beginning 1))))
2200 (delete-backward-char 2)
2201 (cond ((eq escape ?%)
2204 (setq button (apply 'widget-create-child-and-convert
2205 widget 'radio-button
2206 :value (not (null chosen))
2209 (setq child (if chosen
2210 (widget-create-child-value
2212 (widget-create-child widget type)))
2214 (widget-apply child :deactivate)))
2216 (error "Unknown escape `%c'" escape)))))
2217 ;; Update properties.
2219 (widget-put widget :choice type))
2221 (widget-put child :button button)
2222 (widget-put widget :buttons (nconc buttons (list button))))
2224 (widget-put widget :children (nconc children (list child))))
2227 (defun widget-radio-value-get (widget)
2228 ;; Get value of the child widget.
2229 (let ((chosen (widget-radio-chosen widget)))
2230 (and chosen (widget-value chosen))))
2232 (defun widget-radio-chosen (widget)
2233 "Return the widget representing the chosen radio button."
2234 (let ((children (widget-get widget :children))
2237 (setq current (car children)
2238 children (cdr children))
2239 (when (widget-apply (widget-get current :button) :value-get)
2244 (defun widget-radio-value-inline (widget)
2245 ;; Get value of the child widget.
2246 (let ((children (widget-get widget :children))
2249 (setq current (car children)
2250 children (cdr children))
2251 (when (widget-apply (widget-get current :button) :value-get)
2252 (setq found (widget-apply current :value-inline)
2256 (defun widget-radio-value-set (widget value)
2257 ;; We can't just delete and recreate a radio widget, since children
2258 ;; can be added after the original creation and won't be recreated
2260 (let ((children (widget-get widget :children))
2263 (setq current (car children)
2264 children (cdr children))
2265 (let* ((button (widget-get current :button))
2266 (match (and (not found)
2267 (widget-apply current :match value))))
2268 (widget-value-set button match)
2271 (widget-value-set current value)
2272 (widget-apply current :activate))
2273 (widget-apply current :deactivate))
2274 (setq found (or found match))))))
2276 (defun widget-radio-validate (widget)
2277 ;; Valid if we have made a valid choice.
2278 (let ((children (widget-get widget :children))
2279 current found button)
2280 (while (and children (not found))
2281 (setq current (car children)
2282 children (cdr children)
2283 button (widget-get current :button)
2284 found (widget-apply button :value-get)))
2286 (widget-apply current :validate)
2289 (defun widget-radio-action (widget child event)
2290 ;; Check if a radio button was pressed.
2291 (let ((children (widget-get widget :children))
2292 (buttons (widget-get widget :buttons))
2294 (when (memq child buttons)
2296 (setq current (car children)
2297 children (cdr children))
2298 (let* ((button (widget-get current :button)))
2299 (cond ((eq child button)
2300 (widget-value-set button t)
2301 (widget-apply current :activate))
2302 ((widget-value button)
2303 (widget-value-set button nil)
2304 (widget-apply current :deactivate)))))))
2305 ;; Pass notification to parent.
2306 (widget-apply widget :notify child event))
2308 ;;; The `insert-button' Widget.
2310 (define-widget 'insert-button 'push-button
2311 "An insert button for the `editable-list' widget."
2313 :help-echo "Insert a new item into the list at this position."
2314 :action 'widget-insert-button-action)
2316 (defun widget-insert-button-action (widget &optional event)
2317 ;; Ask the parent to insert a new item.
2318 (widget-apply (widget-get widget :parent)
2319 :insert-before (widget-get widget :widget)))
2321 ;;; The `delete-button' Widget.
2323 (define-widget 'delete-button 'push-button
2324 "A delete button for the `editable-list' widget."
2326 :help-echo "Delete this item from the list."
2327 :action 'widget-delete-button-action)
2329 (defun widget-delete-button-action (widget &optional event)
2330 ;; Ask the parent to insert a new item.
2331 (widget-apply (widget-get widget :parent)
2332 :delete-at (widget-get widget :widget)))
2334 ;;; The `editable-list' Widget.
2336 ;; (defcustom widget-editable-list-gui nil
2337 ;; "If non nil, use GUI push-buttons in editable list when available."
2341 (define-widget 'editable-list 'default
2342 "A variable list of widgets of the same type."
2343 :convert-widget 'widget-types-convert-widget
2346 :format-handler 'widget-editable-list-format-handler
2347 :entry-format "%i %d %v"
2348 :menu-tag "editable-list"
2349 :value-create 'widget-editable-list-value-create
2350 :value-delete 'widget-children-value-delete
2351 :value-get 'widget-editable-list-value-get
2352 :validate 'widget-children-validate
2353 :match 'widget-editable-list-match
2354 :match-inline 'widget-editable-list-match-inline
2355 :insert-before 'widget-editable-list-insert-before
2356 :delete-at 'widget-editable-list-delete-at)
2358 (defun widget-editable-list-format-handler (widget escape)
2359 ;; We recognize the insert button.
2360 ;;; (let ((widget-push-button-gui widget-editable-list-gui))
2361 (cond ((eq escape ?i)
2362 (and (widget-get widget :indent)
2363 (insert-char ?\ (widget-get widget :indent)))
2364 (apply 'widget-create-child-and-convert
2365 widget 'insert-button
2366 (widget-get widget :append-button-args)))
2368 (widget-default-format-handler widget escape)))
2372 (defun widget-editable-list-value-create (widget)
2373 ;; Insert all values
2374 (let* ((value (widget-get widget :value))
2375 (type (nth 0 (widget-get widget :args)))
2377 (widget-put widget :value-pos (copy-marker (point)))
2378 (set-marker-insertion-type (widget-get widget :value-pos) t)
2380 (let ((answer (widget-match-inline type value)))
2382 (setq children (cons (widget-editable-list-entry-create
2384 (if (widget-get type :inline)
2391 (widget-put widget :children (nreverse children))))
2393 (defun widget-editable-list-value-get (widget)
2394 ;; Get value of the child widget.
2395 (apply 'append (mapcar (lambda (child) (widget-apply child :value-inline))
2396 (widget-get widget :children))))
2398 (defun widget-editable-list-match (widget value)
2399 ;; Value must be a list and all the members must match the type.
2401 (null (cdr (widget-editable-list-match-inline widget value)))))
2403 (defun widget-editable-list-match-inline (widget value)
2404 (let ((type (nth 0 (widget-get widget :args)))
2407 (while (and value ok)
2408 (let ((answer (widget-match-inline type value)))
2410 (setq found (append found (car answer))
2413 (cons found value)))
2415 (defun widget-editable-list-insert-before (widget before)
2416 ;; Insert a new child in the list of children.
2418 (let ((children (widget-get widget :children))
2419 (inhibit-read-only t)
2420 before-change-functions
2421 after-change-functions)
2423 (goto-char (widget-get before :entry-from)))
2425 (goto-char (widget-get widget :value-pos))))
2426 (let ((child (widget-editable-list-entry-create
2428 (when (< (widget-get child :entry-from) (widget-get widget :from))
2429 (set-marker (widget-get widget :from)
2430 (widget-get child :entry-from)))
2431 (if (eq (car children) before)
2432 (widget-put widget :children (cons child children))
2433 (while (not (eq (car (cdr children)) before))
2434 (setq children (cdr children)))
2435 (setcdr children (cons child (cdr children)))))))
2437 (widget-apply widget :notify widget))
2439 (defun widget-editable-list-delete-at (widget child)
2440 ;; Delete child from list of children.
2442 (let ((buttons (copy-sequence (widget-get widget :buttons)))
2444 (inhibit-read-only t)
2445 before-change-functions
2446 after-change-functions)
2448 (setq button (car buttons)
2449 buttons (cdr buttons))
2450 (when (eq (widget-get button :widget) child)
2452 :buttons (delq button (widget-get widget :buttons)))
2453 (widget-delete button))))
2454 (let ((entry-from (widget-get child :entry-from))
2455 (entry-to (widget-get child :entry-to))
2456 (inhibit-read-only t)
2457 before-change-functions
2458 after-change-functions)
2459 (widget-delete child)
2460 (delete-region entry-from entry-to)
2461 (set-marker entry-from nil)
2462 (set-marker entry-to nil))
2463 (widget-put widget :children (delq child (widget-get widget :children))))
2465 (widget-apply widget :notify widget))
2467 (defun widget-editable-list-entry-create (widget value conv)
2468 ;; Create a new entry to the list.
2469 (let ((type (nth 0 (widget-get widget :args)))
2470 ;;; (widget-push-button-gui widget-editable-list-gui)
2471 child delete insert)
2472 (widget-specify-insert
2474 (and (widget-get widget :indent)
2475 (insert-char ?\ (widget-get widget :indent)))
2476 (insert (widget-get widget :entry-format)))
2477 ;; Parse % escapes in format.
2478 (while (re-search-forward "%\\(.\\)" nil t)
2479 (let ((escape (char-after (match-beginning 1))))
2480 (delete-backward-char 2)
2481 (cond ((eq escape ?%)
2484 (setq insert (apply 'widget-create-child-and-convert
2485 widget 'insert-button
2486 (widget-get widget :insert-button-args))))
2488 (setq delete (apply 'widget-create-child-and-convert
2489 widget 'delete-button
2490 (widget-get widget :delete-button-args))))
2493 (setq child (widget-create-child-value
2495 (setq child (widget-create-child-value
2497 (widget-apply type :value-to-external
2498 (widget-default-get type))))))
2500 (error "Unknown escape `%c'" escape)))))
2502 :buttons (cons delete
2504 (widget-get widget :buttons))))
2505 (let ((entry-from (point-min-marker))
2506 (entry-to (point-max-marker)))
2507 (set-marker-insertion-type entry-from t)
2508 (set-marker-insertion-type entry-to nil)
2509 (widget-put child :entry-from entry-from)
2510 (widget-put child :entry-to entry-to)))
2511 (widget-put insert :widget child)
2512 (widget-put delete :widget child)
2515 ;;; The `group' Widget.
2517 (define-widget 'group 'default
2518 "A widget which groups other widgets inside."
2519 :convert-widget 'widget-types-convert-widget
2521 :value-create 'widget-group-value-create
2522 :value-delete 'widget-children-value-delete
2523 :value-get 'widget-editable-list-value-get
2524 :default-get 'widget-group-default-get
2525 :validate 'widget-children-validate
2526 :match 'widget-group-match
2527 :match-inline 'widget-group-match-inline)
2529 (defun widget-group-value-create (widget)
2530 ;; Create each component.
2531 (let ((args (widget-get widget :args))
2532 (value (widget-get widget :value))
2533 arg answer children)
2535 (setq arg (car args)
2537 answer (widget-match-inline arg value)
2539 (and (eq (preceding-char) ?\n)
2540 (widget-get widget :indent)
2541 (insert-char ?\ (widget-get widget :indent)))
2542 (push (cond ((null answer)
2543 (widget-create-child widget arg))
2544 ((widget-get arg :inline)
2545 (widget-create-child-value widget arg (car answer)))
2547 (widget-create-child-value widget arg (car (car answer)))))
2549 (widget-put widget :children (nreverse children))))
2551 (defun widget-group-default-get (widget)
2552 ;; Get the default of the components.
2553 (mapcar 'widget-default-get (widget-get widget :args)))
2555 (defun widget-group-match (widget values)
2556 ;; Match if the components match.
2558 (let ((match (widget-group-match-inline widget values)))
2559 (and match (null (cdr match))))))
2561 (defun widget-group-match-inline (widget vals)
2562 ;; Match if the components match.
2563 (let ((args (widget-get widget :args))
2564 argument answer found)
2566 (setq argument (car args)
2568 answer (widget-match-inline argument vals))
2570 (setq vals (cdr answer)
2571 found (append found (car answer)))
2575 (cons found vals))))
2577 ;;; The `visibility' Widget.
2579 (define-widget 'visibility 'item
2580 "An indicator and manipulator for hidden items."
2586 :value-create 'widget-visibility-value-create
2587 :action 'widget-toggle-action
2588 :match (lambda (widget value) t))
2590 (defun widget-visibility-value-create (widget)
2591 ;; Insert text representing the `on' and `off' states.
2592 (let ((on (widget-get widget :on))
2593 (off (widget-get widget :off)))
2595 (setq on (concat widget-push-button-prefix
2597 widget-push-button-suffix))
2600 (setq off (concat widget-push-button-prefix
2602 widget-push-button-suffix))
2604 (if (widget-value widget)
2605 (widget-image-insert widget on "down" "down-pushed")
2606 (widget-image-insert widget off "right" "right-pushed"))))
2608 ;;; The `documentation-link' Widget.
2610 ;; This is a helper widget for `documentation-string'.
2612 (define-widget 'documentation-link 'link
2613 "Link type used in documentation strings."
2615 :help-echo "Describe this symbol"
2616 :action 'widget-documentation-link-action)
2618 (defun widget-documentation-link-action (widget &optional event)
2619 "Display documentation for WIDGET's value. Ignore optional argument EVENT."
2620 (let* ((string (widget-get widget :value))
2621 (symbol (intern string)))
2622 (if (and (fboundp symbol) (boundp symbol))
2623 ;; If there are two doc strings, give the user a way to pick one.
2624 (apropos (concat "\\`" (regexp-quote string) "\\'"))
2625 (if (fboundp symbol)
2626 (describe-function symbol)
2627 (describe-variable symbol)))))
2629 (defcustom widget-documentation-links t
2630 "Add hyperlinks to documentation strings when non-nil."
2632 :group 'widget-documentation)
2634 (defcustom widget-documentation-link-regexp "`\\([^\n`' ]+\\)'"
2635 "Regexp for matching potential links in documentation strings.
2636 The first group should be the link itself."
2638 :group 'widget-documentation)
2640 (defcustom widget-documentation-link-p 'intern-soft
2641 "Predicate used to test if a string is useful as a link.
2642 The value should be a function. The function will be called one
2643 argument, a string, and should return non-nil if there should be a
2644 link for that string."
2646 :options '(widget-documentation-link-p)
2647 :group 'widget-documentation)
2649 (defcustom widget-documentation-link-type 'documentation-link
2650 "Widget type used for links in documentation strings."
2652 :group 'widget-documentation)
2654 (defun widget-documentation-link-add (widget from to)
2655 (widget-specify-doc widget from to)
2656 (when widget-documentation-links
2657 (let ((regexp widget-documentation-link-regexp)
2658 (buttons (widget-get widget :buttons))
2659 (widget-mouse-face (default-value 'widget-mouse-face))
2660 (widget-button-face widget-documentation-face)
2661 (widget-button-pressed-face widget-documentation-face))
2664 (while (re-search-forward regexp to t)
2665 (let ((name (match-string 1))
2666 (begin (match-beginning 1))
2667 (end (match-end 1)))
2668 (when (funcall widget-documentation-link-p name)
2669 (push (widget-convert-button widget-documentation-link-type
2670 begin end :value name)
2672 (widget-put widget :buttons buttons)))
2673 (let ((indent (widget-get widget :indent)))
2674 (when (and indent (not (zerop indent)))
2677 (narrow-to-region from to)
2678 (goto-char (point-min))
2679 (while (search-forward "\n" nil t)
2680 (insert-char ?\ indent)))))))
2682 ;;; The `documentation-string' Widget.
2684 (define-widget 'documentation-string 'item
2685 "A documentation string."
2687 :action 'widget-documentation-string-action
2688 :value-delete 'widget-children-value-delete
2689 :value-create 'widget-documentation-string-value-create)
2691 (defun widget-documentation-string-value-create (widget)
2692 ;; Insert documentation string.
2693 (let ((doc (widget-value widget))
2694 (indent (widget-get widget :indent))
2695 (shown (widget-get (widget-get widget :parent) :documentation-shown))
2697 (if (string-match "\n" doc)
2698 (let ((before (substring doc 0 (match-beginning 0)))
2699 (after (substring doc (match-beginning 0)))
2702 (widget-documentation-link-add widget start (point))
2704 (widget-create-child-and-convert
2706 :help-echo "Show or hide rest of the documentation."
2709 :action 'widget-parent-action
2712 (setq start (point))
2713 (when (and indent (not (zerop indent)))
2714 (insert-char ?\ indent))
2716 (widget-documentation-link-add widget start (point)))
2717 (widget-put widget :buttons (list button)))
2719 (widget-documentation-link-add widget start (point))))
2722 (defun widget-documentation-string-action (widget &rest ignore)
2723 ;; Toggle documentation.
2724 (let ((parent (widget-get widget :parent)))
2725 (widget-put parent :documentation-shown
2726 (not (widget-get parent :documentation-shown))))
2728 (widget-value-set widget (widget-value widget)))
2730 ;;; The Sexp Widgets.
2732 (define-widget 'const 'item
2733 "An immutable sexp."
2734 :prompt-value 'widget-const-prompt-value
2737 (defun widget-const-prompt-value (widget prompt value unbound)
2738 ;; Return the value of the const.
2739 (widget-value widget))
2741 (define-widget 'function-item 'const
2742 "An immutable function name."
2744 :documentation-property (lambda (symbol)
2746 (documentation symbol t)
2749 (define-widget 'variable-item 'const
2750 "An immutable variable name."
2752 :documentation-property 'variable-documentation)
2754 (define-widget 'other 'sexp
2755 "Matches any value, but doesn't let the user edit the value.
2756 This is useful as last item in a `choice' widget.
2757 You should use this widget type with a default value,
2758 as in (other DEFAULT) or (other :tag \"NAME\" DEFAULT).
2759 If the user selects this alternative, that specifies DEFAULT
2765 (defvar widget-string-prompt-value-history nil
2766 "History of input to `widget-string-prompt-value'.")
2768 (define-widget 'string 'editable-field
2771 :format "%{%t%}: %v"
2772 :complete-function 'ispell-complete-word
2773 :prompt-history 'widget-string-prompt-value-history)
2775 (define-widget 'regexp 'string
2776 "A regular expression."
2777 :match 'widget-regexp-match
2778 :validate 'widget-regexp-validate
2779 ;; Doesn't work well with terminating newline.
2780 ;; :value-face 'widget-single-line-field-face
2783 (defun widget-regexp-match (widget value)
2784 ;; Match valid regexps.
2785 (and (stringp value)
2788 (string-match value ""))
2791 (defun widget-regexp-validate (widget)
2792 "Check that the value of WIDGET is a valid regexp."
2793 (condition-case data
2795 (string-match (widget-value widget) ""))
2796 (error (widget-put widget :error (error-message-string data))
2799 (define-widget 'file 'string
2801 It will read a file name from the minibuffer when invoked."
2802 :complete-function 'widget-file-complete
2803 :prompt-value 'widget-file-prompt-value
2804 :format "%{%t%}: %v"
2805 ;; Doesn't work well with terminating newline.
2806 ;; :value-face 'widget-single-line-field-face
2809 (defun widget-file-complete ()
2810 "Perform completion on file name preceding point."
2812 (let* ((end (point))
2813 (beg (save-excursion
2814 (skip-chars-backward "^ ")
2816 (pattern (buffer-substring beg end))
2817 (name-part (file-name-nondirectory pattern))
2818 (directory (file-name-directory pattern))
2819 (completion (file-name-completion name-part directory)))
2820 (cond ((eq completion t))
2822 (message "Can't find completion for \"%s\"" pattern)
2824 ((not (string= name-part completion))
2825 (delete-region beg end)
2826 (insert (expand-file-name completion directory)))
2828 (message "Making completion list...")
2829 (with-output-to-temp-buffer "*Completions*"
2830 (display-completion-list
2831 (sort (file-name-all-completions name-part directory)
2833 (message "Making completion list...%s" "done")))))
2835 (defun widget-file-prompt-value (widget prompt value unbound)
2836 ;; Read file from minibuffer.
2837 (abbreviate-file-name
2839 (read-file-name prompt)
2840 (let ((prompt2 (format "%s (default %s) " prompt value))
2841 (dir (file-name-directory value))
2842 (file (file-name-nondirectory value))
2843 (must-match (widget-get widget :must-match)))
2844 (read-file-name prompt2 dir nil must-match file)))))
2846 ;;;(defun widget-file-action (widget &optional event)
2847 ;;; ;; Read a file name from the minibuffer.
2848 ;;; (let* ((value (widget-value widget))
2849 ;;; (dir (file-name-directory value))
2850 ;;; (file (file-name-nondirectory value))
2851 ;;; (menu-tag (widget-apply widget :menu-tag-get))
2852 ;;; (must-match (widget-get widget :must-match))
2853 ;;; (answer (read-file-name (concat menu-tag ": (default `" value "') ")
2854 ;;; dir nil must-match file)))
2855 ;;; (widget-value-set widget (abbreviate-file-name answer))
2857 ;;; (widget-apply widget :notify widget event)))
2859 (define-widget 'directory 'file
2860 "A directory widget.
2861 It will read a directory name from the minibuffer when invoked."
2864 (defvar widget-symbol-prompt-value-history nil
2865 "History of input to `widget-symbol-prompt-value'.")
2867 (define-widget 'symbol 'editable-field
2871 :format "%{%t%}: %v"
2872 :match (lambda (widget value) (symbolp value))
2873 :complete-function 'lisp-complete-symbol
2874 :prompt-internal 'widget-symbol-prompt-internal
2875 :prompt-match 'symbolp
2876 :prompt-history 'widget-symbol-prompt-value-history
2877 :value-to-internal (lambda (widget value)
2881 :value-to-external (lambda (widget value)
2886 (defun widget-symbol-prompt-internal (widget prompt initial history)
2887 ;; Read file from minibuffer.
2888 (let ((answer (completing-read prompt obarray
2889 (widget-get widget :prompt-match)
2890 nil initial history)))
2891 (if (and (stringp answer)
2892 (not (zerop (length answer))))
2894 (error "No value"))))
2896 (defvar widget-function-prompt-value-history nil
2897 "History of input to `widget-function-prompt-value'.")
2899 (define-widget 'function 'sexp
2901 :complete-function (lambda ()
2903 (lisp-complete-symbol 'fboundp))
2904 :prompt-value 'widget-field-prompt-value
2905 :prompt-internal 'widget-symbol-prompt-internal
2906 :prompt-match 'fboundp
2907 :prompt-history 'widget-function-prompt-value-history
2908 :action 'widget-field-action
2909 :validate (lambda (widget)
2910 (unless (functionp (widget-value widget))
2911 (widget-put widget :error (format "Invalid function: %S"
2912 (widget-value widget)))
2917 (defvar widget-variable-prompt-value-history nil
2918 "History of input to `widget-variable-prompt-value'.")
2920 (define-widget 'variable 'symbol
2921 ;; Should complete on variables.
2923 :prompt-match 'boundp
2924 :prompt-history 'widget-variable-prompt-value-history
2925 :complete-function (lambda ()
2927 (lisp-complete-symbol 'boundp))
2930 (defvar widget-coding-system-prompt-value-history nil
2931 "History of input to `widget-coding-system-prompt-value'.")
2933 (define-widget 'coding-system 'symbol
2934 "A MULE coding-system."
2935 :format "%{%t%}: %v"
2936 :tag "Coding system"
2938 :prompt-history 'widget-coding-system-prompt-value-history
2939 :prompt-value 'widget-coding-system-prompt-value
2940 :action 'widget-coding-system-action
2941 :complete-function (lambda ()
2943 (lisp-complete-symbol 'coding-system-p))
2944 :validate (lambda (widget)
2945 (unless (coding-system-p (widget-value widget))
2946 (widget-put widget :error (format "Invalid coding system: %S"
2947 (widget-value widget)))
2950 :prompt-match 'coding-system-p)
2952 (defun widget-coding-system-prompt-value (widget prompt value unbound)
2953 "Read coding-system from minibuffer."
2954 (if (widget-get widget :base-only)
2956 (completing-read (format "%s (default %s) " prompt value)
2957 (mapcar #'list (coding-system-list t)) nil nil nil
2958 coding-system-history))
2959 (read-coding-system (format "%s (default %s) " prompt value) value)))
2961 (defun widget-coding-system-action (widget &optional event)
2963 (widget-coding-system-prompt-value
2965 (widget-apply widget :menu-tag-get)
2966 (widget-value widget)
2968 (widget-value-set widget answer)
2969 (widget-apply widget :notify widget event)
2972 (define-widget 'sexp 'editable-field
2973 "An arbitrary Lisp expression."
2974 :tag "Lisp expression"
2975 :format "%{%t%}: %v"
2977 :validate 'widget-sexp-validate
2978 :match (lambda (widget value) t)
2979 :value-to-internal 'widget-sexp-value-to-internal
2980 :value-to-external (lambda (widget value) (read value))
2981 :prompt-history 'widget-sexp-prompt-value-history
2982 :prompt-value 'widget-sexp-prompt-value)
2984 (defun widget-sexp-value-to-internal (widget value)
2985 ;; Use pp for printer representation.
2986 (let ((pp (if (symbolp value)
2987 (prin1-to-string value)
2988 (pp-to-string value))))
2989 (while (string-match "\n\\'" pp)
2990 (setq pp (substring pp 0 -1)))
2991 (if (or (string-match "\n\\'" pp)
2996 (defun widget-sexp-validate (widget)
2997 ;; Valid if we can read the string and there is no junk left after it.
2999 (insert (widget-apply widget :value-get))
3000 (goto-char (point-min))
3001 (condition-case data
3003 ;; Avoid a confusing end-of-file error.
3004 (skip-syntax-forward "\\s-")
3006 (error "Empty sexp -- use `nil'?"))
3008 (unless (widget-apply widget :match (read (current-buffer)))
3009 (widget-put widget :error (widget-get widget :type-error))
3012 :error (format "Junk at end of expression: %s"
3013 (buffer-substring (point)
3016 (end-of-file ; Avoid confusing error message.
3017 (widget-put widget :error "Unbalanced sexp")
3019 (error (widget-put widget :error (error-message-string data))
3022 (defvar widget-sexp-prompt-value-history nil
3023 "History of input to `widget-sexp-prompt-value'.")
3025 (defun widget-sexp-prompt-value (widget prompt value unbound)
3026 ;; Read an arbitrary sexp.
3027 (let ((found (read-string prompt
3028 (if unbound nil (cons (prin1-to-string value) 0))
3029 (widget-get widget :prompt-history))))
3030 (let ((answer (read-from-string found)))
3031 (unless (= (cdr answer) (length found))
3032 (error "Junk at end of expression: %s"
3033 (substring found (cdr answer))))
3036 (define-widget 'restricted-sexp 'sexp
3037 "A Lisp expression restricted to values that match.
3038 To use this type, you must define :match or :match-alternatives."
3039 :type-error "The specified value is not valid"
3040 :match 'widget-restricted-sexp-match
3041 :value-to-internal (lambda (widget value)
3042 (if (widget-apply widget :match value)
3043 (prin1-to-string value)
3046 (defun widget-restricted-sexp-match (widget value)
3047 (let ((alternatives (widget-get widget :match-alternatives))
3049 (while (and alternatives (not matched))
3050 (if (cond ((functionp (car alternatives))
3051 (funcall (car alternatives) value))
3052 ((and (consp (car alternatives))
3053 (eq (car (car alternatives)) 'quote))
3054 (eq value (nth 1 (car alternatives)))))
3056 (setq alternatives (cdr alternatives)))
3059 (define-widget 'integer 'restricted-sexp
3063 :type-error "This field should contain an integer"
3064 :match-alternatives '(integerp))
3066 (define-widget 'number 'restricted-sexp
3067 "A floating point number."
3070 :type-error "This field should contain a number"
3071 :match-alternatives '(numberp))
3073 (define-widget 'character 'editable-field
3078 :format "%{%t%}: %v\n"
3079 :valid-regexp "\\`.\\'"
3080 :error "This field should contain a single character"
3081 :value-to-internal (lambda (widget value)
3084 (char-to-string value)))
3085 :value-to-external (lambda (widget value)
3089 :match (lambda (widget value)
3090 (char-valid-p value)))
3092 (define-widget 'list 'group
3095 :format "%{%t%}:\n%v")
3097 (define-widget 'vector 'group
3100 :format "%{%t%}:\n%v"
3101 :match 'widget-vector-match
3102 :value-to-internal (lambda (widget value) (append value nil))
3103 :value-to-external (lambda (widget value) (apply 'vector value)))
3105 (defun widget-vector-match (widget value)
3106 (and (vectorp value)
3107 (widget-group-match widget
3108 (widget-apply widget :value-to-internal value))))
3110 (define-widget 'cons 'group
3113 :format "%{%t%}:\n%v"
3114 :match 'widget-cons-match
3115 :value-to-internal (lambda (widget value)
3116 (list (car value) (cdr value)))
3117 :value-to-external (lambda (widget value)
3118 (cons (nth 0 value) (nth 1 value))))
3120 (defun widget-cons-match (widget value)
3122 (widget-group-match widget
3123 (widget-apply widget :value-to-internal value))))
3125 ;;; The `plist' Widget.
3129 (define-widget 'plist 'list
3131 :key-type '(symbol :tag "Key")
3132 :value-type '(sexp :tag "Value")
3133 :convert-widget 'widget-plist-convert-widget
3136 (defvar widget-plist-value-type) ;Dynamic variable
3138 (defun widget-plist-convert-widget (widget)
3139 ;; Handle `:options'.
3140 (let* ((options (widget-get widget :options))
3141 (other `(editable-list :inline t
3143 ,(widget-get widget :key-type)
3144 ,(widget-get widget :value-type))))
3146 (list `(checklist :inline t
3148 ,@(mapcar 'widget-plist-convert-option
3152 (widget-put widget :args args)
3155 (defun widget-plist-convert-option (option)
3156 ;; Convert a single plist option.
3157 (let (key-type value-type)
3159 (let ((key (nth 0 option)))
3160 (setq value-type (nth 1 option))
3163 (setq key-type `(const ,key))))
3164 (setq key-type `(const ,option)
3165 value-type widget-plist-value-type))
3166 `(group :format "Key: %v" :inline t ,key-type ,value-type)))
3169 ;;; The `alist' Widget.
3171 ;; Association lists.
3173 (define-widget 'alist 'list
3174 "An association list."
3175 :key-type '(sexp :tag "Key")
3176 :value-type '(sexp :tag "Value")
3177 :convert-widget 'widget-alist-convert-widget
3180 (defvar widget-alist-value-type) ;Dynamic variable
3182 (defun widget-alist-convert-widget (widget)
3183 ;; Handle `:options'.
3184 (let* ((options (widget-get widget :options))
3185 (other `(editable-list :inline t
3187 ,(widget-get widget :key-type)
3188 ,(widget-get widget :value-type))))
3190 (list `(checklist :inline t
3192 ,@(mapcar 'widget-alist-convert-option
3196 (widget-put widget :args args)
3199 (defun widget-alist-convert-option (option)
3200 ;; Convert a single alist option.
3201 (let (key-type value-type)
3203 (let ((key (nth 0 option)))
3204 (setq value-type (nth 1 option))
3207 (setq key-type `(const ,key))))
3208 (setq key-type `(const ,option)
3209 value-type widget-alist-value-type))
3210 `(cons :format "Key: %v" ,key-type ,value-type)))
3212 (define-widget 'choice 'menu-choice
3213 "A union of several sexp types."
3215 :format "%{%t%}: %[Value Menu%] %v"
3216 :button-prefix 'widget-push-button-prefix
3217 :button-suffix 'widget-push-button-suffix
3218 :prompt-value 'widget-choice-prompt-value)
3220 (defun widget-choice-prompt-value (widget prompt value unbound)
3222 (let ((args (widget-get widget :args))
3223 (completion-ignore-case (widget-get widget :case-fold))
3224 current choices old)
3225 ;; Find the first arg that matches VALUE.
3228 (if (widget-apply (car look) :match value)
3229 (setq old (car look)
3231 (setq look (cdr look)))))
3234 (cond ((= (length args) 0)
3236 ((= (length args) 1)
3238 ((and (= (length args) 2)
3240 (if (eq old (nth 0 args))
3245 (setq current (car args)
3248 (cons (cons (widget-apply current :menu-tag-get)
3251 (let ((val (completing-read prompt choices nil t)))
3253 (let ((try (try-completion val choices)))
3256 (cdr (assoc val choices)))
3259 (widget-prompt-value current prompt nil t)
3262 (define-widget 'radio 'radio-button-choice
3263 "A union of several sexp types."
3265 :format "%{%t%}:\n%v"
3266 :prompt-value 'widget-choice-prompt-value)
3268 (define-widget 'repeat 'editable-list
3269 "A variable length homogeneous list."
3271 :format "%{%t%}:\n%v%i\n")
3273 (define-widget 'set 'checklist
3274 "A list of members from a fixed set."
3276 :format "%{%t%}:\n%v")
3278 (define-widget 'boolean 'toggle
3279 "To be nil or non-nil, that is the question."
3281 :prompt-value 'widget-boolean-prompt-value
3282 :button-prefix 'widget-push-button-prefix
3283 :button-suffix 'widget-push-button-suffix
3284 :format "%{%t%}: %[Toggle%] %v\n"
3288 (defun widget-boolean-prompt-value (widget prompt value unbound)
3289 ;; Toggle a boolean.
3292 ;;; The `color' Widget.
3294 (define-widget 'color 'editable-field
3295 "Choose a color name (with sample)."
3296 :format "%t: %v (%{sample%})\n"
3300 :complete 'widget-color-complete
3301 :sample-face-get 'widget-color-sample-face-get
3302 :notify 'widget-color-notify
3303 :action 'widget-color-action)
3305 (defun widget-color-complete (widget)
3306 "Complete the color in WIDGET."
3307 (require 'facemenu) ; for facemenu-color-alist
3308 (let* ((prefix (buffer-substring-no-properties (widget-field-start widget)
3310 (list (or facemenu-color-alist
3311 (mapcar 'list (defined-colors))))
3312 (completion (try-completion prefix list)))
3313 (cond ((eq completion t)
3314 (message "Exact match."))
3316 (error "Can't find completion for \"%s\"" prefix))
3317 ((not (string-equal prefix completion))
3318 (insert-and-inherit (substring completion (length prefix))))
3320 (message "Making completion list...")
3321 (with-output-to-temp-buffer "*Completions*"
3322 (display-completion-list (all-completions prefix list nil)))
3323 (message "Making completion list...done")))))
3325 (defun widget-color-sample-face-get (widget)
3326 (let* ((value (condition-case nil
3327 (widget-value widget)
3328 (error (widget-get widget :value))))
3329 (symbol (intern (concat "fg:" value))))
3331 (facemenu-get-face symbol)
3334 (defun widget-color-action (widget &optional event)
3335 ;; Prompt for a color.
3336 (let* ((tag (widget-apply widget :menu-tag-get))
3337 (prompt (concat tag ": "))
3338 (value (widget-value widget))
3339 (start (widget-field-start widget))
3340 (pos (cond ((< (point) start)
3342 ((> (point) (+ start (length value)))
3345 (- (point) start))))
3346 (answer (facemenu-read-color prompt)))
3347 (unless (zerop (length answer))
3348 (widget-value-set widget answer)
3350 (widget-apply widget :notify widget event))))
3352 (defun widget-color-notify (widget child &optional event)
3353 "Update the sample, and notofy the parent."
3354 (overlay-put (widget-get widget :sample-overlay)
3355 'face (widget-apply widget :sample-face-get))
3356 (widget-default-notify widget child event))
3360 (defun widget-at (&optional pos)
3361 "The button or field at POS (default, point)."
3364 (or (get-char-property pos 'button)
3365 (get-char-property pos 'field)))
3367 (defun widget-echo-help (pos)
3368 "Display the help echo for widget at POS."
3369 (let* ((widget (widget-at pos))
3370 (help-echo (and widget (widget-get widget :help-echo))))
3371 (if (or (stringp help-echo)
3372 (and (functionp help-echo)
3373 ;; Kluge: help-echo originally could be a function of
3374 ;; one arg -- the widget. It is more useful in Emacs
3375 ;; 21 to have it as a function usable also as a
3376 ;; help-echo property, when it can sort out its own
3377 ;; widget if necessary. Try both calling sequences
3378 ;; (rather than messing around to get the function's
3384 (selected-window) (current-buffer)
3386 (error (funcall help-echo widget))))))
3387 (stringp (eval help-echo)))
3388 (message "%s" help-echo))))
3394 ;;; wid-edit.el ends here