1 ;;; wid-edit.el --- Functions for creating and using widgets -*-byte-compile-dynamic: t;-*-
3 ;; Copyright (C) 1996, 1997, 1999, 2000, 2001, 2002 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 (:weight bold
)))
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 ;; TTY gets special definitions here and in the next defface, because
124 ;; the gray colors defined for other displays cause black text on a black
125 ;; background, at least on light-background TTYs.
126 (defface widget-field-face
'((((type tty
))
127 :background
"yellow3"
129 (((class grayscale color
)
131 :background
"gray85")
132 (((class grayscale color
)
134 :background
"dim gray")
137 "Face used for editable fields."
138 :group
'widget-faces
)
140 (defface widget-single-line-field-face
'((((type tty
))
143 (((class grayscale color
)
145 :background
"gray85")
146 (((class grayscale color
)
148 :background
"dim gray")
151 "Face used for editable fields spanning only a single line."
152 :group
'widget-faces
)
154 ;;; This causes display-table to be loaded, and not usefully.
155 ;;;(defvar widget-single-line-display-table
156 ;;; (let ((table (make-display-table)))
157 ;;; (aset table 9 "^I")
158 ;;; (aset table 10 "^J")
160 ;;; "Display table used for single-line editable fields.")
162 ;;;(when (fboundp 'set-face-display-table)
163 ;;; (set-face-display-table 'widget-single-line-field-face
164 ;;; widget-single-line-display-table))
166 ;;; Utility functions.
168 ;; These are not really widget specific.
170 (defun widget-princ-to-string (object)
171 "Return string representation of OBJECT, any Lisp object.
172 No quoting characters are used; no delimiters are printed around
173 the contents of strings."
174 (with-output-to-string
177 (defun widget-clear-undo ()
178 "Clear all undo information."
179 (buffer-disable-undo (current-buffer))
180 (buffer-enable-undo))
182 (defcustom widget-menu-max-size
40
183 "Largest number of items allowed in a popup-menu.
184 Larger menus are read through the minibuffer."
188 (defcustom widget-menu-max-shortcuts
40
189 "Largest number of items for which it works to choose one with a character.
190 For a larger number of items, the minibuffer is used."
194 (defcustom widget-menu-minibuffer-flag nil
195 "*Control how to ask for a choice from the keyboard.
196 Non-nil means use the minibuffer;
197 nil means read a single character."
201 (defun widget-choose (title items
&optional event
)
202 "Choose an item from a list.
204 First argument TITLE is the name of the list.
205 Second argument ITEMS is an list whose members are either
206 (NAME . VALUE), to indicate selectable items, or just strings to
207 indicate unselectable items.
208 Optional third argument EVENT is an input event.
210 The user is asked to choose between each NAME from the items alist,
211 and the VALUE of the chosen element will be returned. If EVENT is a
212 mouse event, and the number of elements in items is less than
213 `widget-menu-max-size', a popup menu will be used, otherwise the
215 (cond ((and (< (length items
) widget-menu-max-size
)
216 event
(display-popup-menus-p))
219 (list title
(cons "" items
))))
220 ((or widget-menu-minibuffer-flag
221 (> (length items
) widget-menu-max-shortcuts
))
222 ;; Read the choice of name from the minibuffer.
223 (setq items
(widget-remove-if 'stringp items
))
224 (let ((val (completing-read (concat title
": ") items nil t
)))
226 (let ((try (try-completion val items
)))
229 (cdr (assoc val items
))))))
231 ;; Construct a menu of the choices
232 ;; and then use it for prompting for a single character.
233 (let* ((overriding-terminal-local-map (make-sparse-keymap))
235 map choice some-choice-enabled value
)
236 ;; Define SPC as a prefix char to get to this menu.
237 (define-key overriding-terminal-local-map
" "
238 (setq map
(make-sparse-keymap title
)))
240 (set-buffer (get-buffer-create " widget-choose"))
242 (insert "Available choices:\n\n")
244 (setq choice
(car items
) items
(cdr items
))
246 (let* ((name (car choice
))
247 (function (cdr choice
)))
248 (insert (format "%c = %s\n" next-digit name
))
249 (define-key map
(vector next-digit
) function
)
250 (setq some-choice-enabled t
)))
251 ;; Allocate digits to disabled alternatives
252 ;; so that the digit of a given alternative never varies.
253 (setq next-digit
(1+ next-digit
)))
254 (insert "\nC-g = Quit"))
255 (or some-choice-enabled
256 (error "None of the choices is currently meaningful"))
257 (define-key map
[?\C-g
] 'keyboard-quit
)
258 (define-key map
[t] 'keyboard-quit)
259 (define-key map [?\M-\C-v] 'scroll-other-window)
260 (define-key map [?\M--] 'negative-argument)
261 (setcdr map (nreverse (cdr map)))
262 ;; Read a char with the menu, and return the result
263 ;; that corresponds to it.
264 (save-window-excursion
265 (let ((buf (get-buffer " widget-choose")))
266 (fit-window-to-buffer (display-buffer buf))
267 (let ((cursor-in-echo-area t)
271 (while (not (or (and (>= char ?0) (< char next-digit))
272 (eq value 'keyboard-quit)))
273 ;; Unread a SPC to lead to our new menu.
274 (setq unread-command-events (cons ?\ unread-command-events))
275 (setq keys (read-key-sequence title))
277 (lookup-key overriding-terminal-local-map keys t)
278 char (string-to-char (substring keys 1)))
279 (cond ((eq value 'scroll-other-window)
280 (let ((minibuffer-scroll-window
281 (get-buffer-window buf)))
283 (scroll-other-window-down
284 (window-height minibuffer-scroll-window))
285 (scroll-other-window))
287 ((eq value 'negative-argument)
291 (when (eq value 'keyboard-quit)
295 (defun widget-remove-if (predictate list)
296 (let (result (tail list))
298 (or (funcall predictate (car tail))
299 (setq result (cons (car tail) result)))
300 (setq tail (cdr tail)))
303 ;;; Widget text specifications.
305 ;; These functions are for specifying text properties.
307 (defvar widget-field-add-space t
308 "Non-nil means add extra space at the end of editable text fields.
309 If you don't add the space, it will become impossible to edit a zero
312 (defvar widget-field-use-before-change t
313 "Non-nil means use `before-change-functions' to track editable fields.
314 This enables the use of undo, but doesn't work on Emacs 19.34 and earlier.
315 Using before hooks also means that the :notify function can't know the
318 (defun widget-specify-field (widget from to)
319 "Specify editable button for WIDGET between FROM and TO."
320 ;; Terminating space is not part of the field, but necessary in
321 ;; order for local-map to work. Remove next sexp if local-map works
322 ;; at the end of the overlay.
325 (cond ((null (widget-get widget :size))
327 (widget-field-add-space
328 (insert-and-inherit " ")))
330 (let ((keymap (widget-get widget :keymap))
331 (face (or (widget-get widget :value-face) 'widget-field-face))
332 (help-echo (widget-get widget :help-echo))
334 (or (not widget-field-add-space) (widget-get widget :size))))
335 (if (functionp help-echo)
336 (setq help-echo 'widget-mouse-help))
337 (when (= (char-before to) ?\n)
338 ;; When the last character in the field is a newline, we want to
339 ;; give it a `field' char-property of `boundary', which helps the
340 ;; C-n/C-p act more naturally when entering/leaving the field. We
341 ;; do this by making a small secondary overlay to contain just that
343 (let ((overlay (make-overlay (1- to) to nil t nil)))
344 (overlay-put overlay 'field 'boundary)
345 ;; Use `local-map' here, not `keymap', so that normal editing
346 ;; works in the field when, say, Custom uses `suppress-keymap'.
347 (overlay-put overlay 'local-map keymap)
348 (overlay-put overlay 'face face)
349 (overlay-put overlay 'help-echo help-echo))
351 (setq rear-sticky t))
352 (let ((overlay (make-overlay from to nil nil rear-sticky)))
353 (widget-put widget :field-overlay overlay)
354 ;;(overlay-put overlay 'detachable nil)
355 (overlay-put overlay 'field widget)
356 (overlay-put overlay 'local-map keymap)
357 (overlay-put overlay 'face face)
358 (overlay-put overlay 'help-echo help-echo)))
359 (widget-specify-secret widget))
361 (defun widget-specify-secret (field)
362 "Replace text in FIELD with value of `:secret', if non-nil."
363 (let ((secret (widget-get field :secret))
364 (size (widget-get field :size)))
366 (let ((begin (widget-field-start field))
367 (end (widget-field-end field)))
369 (while (and (> end begin)
370 (eq (char-after (1- end)) ?\ ))
371 (setq end (1- end))))
373 (let ((old (char-after begin)))
374 (unless (eq old secret)
375 (subst-char-in-region begin (1+ begin) old secret)
376 (put-text-property begin (1+ begin) 'secret old))
377 (setq begin (1+ begin))))))))
379 (defun widget-specify-button (widget from to)
380 "Specify button for WIDGET between FROM and TO."
381 (let ((overlay (make-overlay from to nil t nil))
382 (help-echo (widget-get widget :help-echo)))
383 (widget-put widget :button-overlay overlay)
384 (if (functionp help-echo)
385 (setq help-echo 'widget-mouse-help))
386 (overlay-put overlay 'button widget)
387 (overlay-put overlay 'keymap (widget-get widget :keymap))
388 ;; We want to avoid the face with image buttons.
389 (unless (widget-get widget :suppress-face)
390 (overlay-put overlay 'face (widget-apply widget :button-face-get))
391 (overlay-put overlay 'mouse-face widget-mouse-face))
392 (overlay-put overlay 'help-echo help-echo)))
394 (defun widget-mouse-help (window overlay point)
395 "Help-echo callback for widgets whose :help-echo is a function."
396 (with-current-buffer (overlay-buffer overlay)
397 (let* ((widget (widget-at (overlay-start overlay)))
398 (help-echo (if widget (widget-get widget :help-echo))))
399 (if (functionp help-echo)
400 (funcall help-echo widget)
403 (defun widget-specify-sample (widget from to)
404 "Specify sample for WIDGET between FROM and TO."
405 (let ((overlay (make-overlay from to nil t nil)))
406 (overlay-put overlay 'face (widget-apply widget :sample-face-get))
407 (widget-put widget :sample-overlay overlay)))
409 (defun widget-specify-doc (widget from to)
410 "Specify documentation for WIDGET between FROM and TO."
411 (let ((overlay (make-overlay from to nil t nil)))
412 (overlay-put overlay 'widget-doc widget)
413 (overlay-put overlay 'face widget-documentation-face)
414 (widget-put widget :doc-overlay overlay)))
416 (defmacro widget-specify-insert (&rest form)
417 "Execute FORM without inheriting any text properties."
419 (let ((inhibit-read-only t)
420 (inhibit-modification-hooks t)
423 (narrow-to-region (- (point) 2) (point))
424 (goto-char (1+ (point-min)))
425 (setq result (progn ,@form))
426 (delete-region (point-min) (1+ (point-min)))
427 (delete-region (1- (point-max)) (point-max))
428 (goto-char (point-max))
431 (defface widget-inactive-face '((((class grayscale color)
433 (:foreground "light gray"))
434 (((class grayscale color)
436 (:foreground "dim gray"))
439 "Face used for inactive widgets."
440 :group 'widget-faces)
442 (defun widget-specify-inactive (widget from to)
443 "Make WIDGET inactive for user modifications."
444 (unless (widget-get widget :inactive)
445 (let ((overlay (make-overlay from to nil t nil)))
446 (overlay-put overlay 'face 'widget-inactive-face)
447 ;; This is disabled, as it makes the mouse cursor change shape.
448 ;; (overlay-put overlay 'mouse-face 'widget-inactive-face)
449 (overlay-put overlay 'evaporate t)
450 (overlay-put overlay 'priority 100)
451 (overlay-put overlay 'modification-hooks '(widget-overlay-inactive))
452 (widget-put widget :inactive overlay))))
454 (defun widget-overlay-inactive (&rest junk)
455 "Ignoring the arguments, signal an error."
456 (unless inhibit-read-only
457 (error "The widget here is not active")))
460 (defun widget-specify-active (widget)
461 "Make WIDGET active for user modifications."
462 (let ((inactive (widget-get widget :inactive)))
464 (delete-overlay inactive)
465 (widget-put widget :inactive nil))))
467 ;;; Widget Properties.
469 (defsubst widget-type (widget)
470 "Return the type of WIDGET, a symbol."
474 (defun widgetp (widget)
475 "Return non-nil iff WIDGET is a widget."
477 (get widget 'widget-type)
479 (symbolp (car widget))
480 (get (car widget) 'widget-type))))
482 (defun widget-get-indirect (widget property)
483 "In WIDGET, get the value of PROPERTY.
484 If the value is a symbol, return its binding.
485 Otherwise, just return the value."
486 (let ((value (widget-get widget property)))
491 (defun widget-member (widget property)
492 "Non-nil iff there is a definition in WIDGET for PROPERTY."
493 (cond ((plist-member (cdr widget) property)
496 (widget-member (get (car widget) 'widget-type) property))
499 (defun widget-value (widget)
500 "Extract the current value of WIDGET."
502 :value-to-external (widget-apply widget :value-get)))
504 (defun widget-value-set (widget value)
505 "Set the current value of WIDGET to VALUE."
507 :value-set (widget-apply widget
508 :value-to-internal value)))
510 (defun widget-default-get (widget)
511 "Extract the default value of WIDGET."
512 (or (widget-get widget :value)
513 (widget-apply widget :default-get)))
515 (defun widget-match-inline (widget vals)
516 "In WIDGET, match the start of VALS."
517 (cond ((widget-get widget :inline)
518 (widget-apply widget :match-inline vals))
520 (widget-apply widget :match (car vals)))
521 (cons (list (car vals)) (cdr vals)))
524 (defun widget-apply-action (widget &optional event)
525 "Apply :action in WIDGET in response to EVENT."
526 (if (widget-apply widget :active)
527 (widget-apply widget :action event)
528 (error "Attempt to perform action on inactive widget")))
530 ;;; Helper functions.
532 ;; These are widget specific.
535 (defun widget-prompt-value (widget prompt &optional value unbound)
536 "Prompt for a value matching WIDGET, using PROMPT.
537 The current value is assumed to be VALUE, unless UNBOUND is non-nil."
538 (unless (listp widget)
539 (setq widget (list widget)))
540 (setq prompt (format "[%s] %s" (widget-type widget) prompt))
541 (setq widget (widget-convert widget))
542 (let ((answer (widget-apply widget :prompt-value prompt value unbound)))
543 (unless (widget-apply widget :match answer)
544 (error "Value does not match %S type" (car widget)))
547 (defun widget-get-sibling (widget)
548 "Get the item WIDGET is assumed to toggle.
549 This is only meaningful for radio buttons or checkboxes in a list."
550 (let* ((children (widget-get (widget-get widget :parent) :children))
554 (setq child (car children)
555 children (cdr children))
556 (when (eq (widget-get child :button) widget)
557 (throw 'child child)))
560 (defun widget-map-buttons (function &optional buffer maparg)
561 "Map FUNCTION over the buttons in BUFFER.
562 FUNCTION is called with the arguments WIDGET and MAPARG.
564 If FUNCTION returns non-nil, the walk is cancelled.
566 The arguments MAPARG, and BUFFER default to nil and (current-buffer),
568 (let ((cur (point-min))
572 (save-excursion (set-buffer buffer) (overlay-lists))
574 (setq overlays (append (car overlays) (cdr overlays)))
575 (while (setq cur (pop overlays))
576 (setq widget (overlay-get cur 'button))
577 (if (and widget (funcall function widget maparg))
578 (setq overlays nil)))))
582 (defcustom widget-image-directory (file-name-as-directory
583 (expand-file-name "custom" data-directory))
584 "Where widget button images are located.
585 If this variable is nil, widget will try to locate the directory
590 (defcustom widget-image-enable t
591 "If non nil, use image buttons in widgets when available."
596 (defcustom widget-image-conversion
597 '((xpm ".xpm") (gif ".gif") (png ".png") (jpeg ".jpg" ".jpeg")
599 "Conversion alist from image formats to file name suffixes."
601 :type '(repeat (cons :format "%v"
602 (symbol :tag "Image Format" unknown)
603 (repeat :tag "Suffixes"
604 (string :format "%v")))))
606 (defun widget-image-find (image)
607 "Create a graphical button from IMAGE.
608 IMAGE should either already be an image, or be a file name sans
609 extension (xpm, xbm, gif, jpg, or png) located in
610 `widget-image-directory' or otherwise where `find-image' will find it."
611 (cond ((not (and image widget-image-enable (display-graphic-p)))
612 ;; We don't want or can't use images.
615 (eq 'image (car image)))
616 ;; Already an image spec. Use it.
619 ;; A string. Look it up in relevant directories.
620 (let* ((load-path (cons widget-image-directory load-path))
622 (dolist (elt widget-image-conversion)
623 (dolist (ext (cdr elt))
624 (push (list :type (car elt) :file (concat image ext)) specs)))
625 (setq specs (nreverse specs))
631 (defvar widget-button-pressed-face 'widget-button-pressed-face
632 "Face used for pressed buttons in widgets.
633 This exists as a variable so it can be set locally in certain
636 (defun widget-image-insert (widget tag image &optional down inactive)
637 "In WIDGET, insert the text TAG or, if supported, IMAGE.
638 IMAGE should either be an image or an image file name sans extension
639 \(xpm, xbm, gif, jpg, or png) located in `widget-image-directory'.
641 Optional arguments DOWN and INACTIVE are used instead of IMAGE when the
642 button is pressed or inactive, respectively. These are currently ignored."
643 (if (and (display-graphic-p)
644 (setq image (widget-image-find image)))
645 (progn (widget-put widget :suppress-face t)
648 tag 'mouse-face widget-button-pressed-face)))
653 (defgroup widget-button nil
654 "The look of various kinds of buttons."
657 (defcustom widget-button-prefix ""
658 "String used as prefix for buttons."
660 :group 'widget-button)
662 (defcustom widget-button-suffix ""
663 "String used as suffix for buttons."
665 :group 'widget-button)
667 ;;; Creating Widgets.
670 (defun widget-create (type &rest args)
671 "Create widget of TYPE.
672 The optional ARGS are additional keyword arguments."
673 (let ((widget (apply 'widget-convert type args)))
674 (widget-apply widget :create)
677 (defun widget-create-child-and-convert (parent type &rest args)
678 "As part of the widget PARENT, create a child widget TYPE.
679 The child is converted, using the keyword arguments ARGS."
680 (let ((widget (apply 'widget-convert type args)))
681 (widget-put widget :parent parent)
682 (unless (widget-get widget :indent)
683 (widget-put widget :indent (+ (or (widget-get parent :indent) 0)
684 (or (widget-get widget :extra-offset) 0)
685 (widget-get parent :offset))))
686 (widget-apply widget :create)
689 (defun widget-create-child (parent type)
690 "Create widget of TYPE."
691 (let ((widget (copy-sequence type)))
692 (widget-put widget :parent parent)
693 (unless (widget-get widget :indent)
694 (widget-put widget :indent (+ (or (widget-get parent :indent) 0)
695 (or (widget-get widget :extra-offset) 0)
696 (widget-get parent :offset))))
697 (widget-apply widget :create)
700 (defun widget-create-child-value (parent type value)
701 "Create widget of TYPE with value VALUE."
702 (let ((widget (copy-sequence type)))
703 (widget-put widget :value (widget-apply widget :value-to-internal value))
704 (widget-put widget :parent parent)
705 (unless (widget-get widget :indent)
706 (widget-put widget :indent (+ (or (widget-get parent :indent) 0)
707 (or (widget-get widget :extra-offset) 0)
708 (widget-get parent :offset))))
709 (widget-apply widget :create)
713 (defun widget-delete (widget)
715 (widget-apply widget :delete))
717 (defun widget-convert (type &rest args)
718 "Convert TYPE to a widget without inserting it in the buffer.
719 The optional ARGS are additional keyword arguments."
720 ;; Don't touch the type.
721 (let* ((widget (if (symbolp type)
723 (copy-sequence type)))
727 ;; First set the :args keyword.
728 (while (cdr current) ;Look in the type.
729 (if (and (keywordp (cadr current))
730 ;; If the last element is a keyword,
731 ;; it is still the :args element,
732 ;; even though it is a keyword.
734 (if (eq (cadr current) :args)
735 ;; If :args is explicitly specified, obey it.
737 ;; Some other irrelevant keyword.
738 (setq current (cdr (cdr current))))
739 (setcdr current (list :args (cdr current)))
741 (while (and args (not done)) ;Look in ARGS.
742 (cond ((eq (car args) :args)
743 ;; Handle explicit specification of :args.
744 (setq args (cadr args)
746 ((keywordp (car args))
747 (setq args (cddr args)))
750 (widget-put widget :args args))
751 ;; Then Convert the widget.
754 (let ((convert-widget (plist-get (cdr type) :convert-widget)))
756 (setq widget (funcall convert-widget widget))))
757 (setq type (get (car type) 'widget-type)))
758 ;; Finally set the keyword args.
760 (let ((next (nth 0 keys)))
763 (widget-put widget next (nth 1 keys))
764 (setq keys (nthcdr 2 keys)))
766 ;; Convert the :value to internal format.
767 (if (widget-member widget :value)
769 :value (widget-apply widget
771 (widget-get widget :value))))
772 ;; Return the newly create widget.
776 (defun widget-insert (&rest args)
777 "Call `insert' with ARGS even if surrounding text is read only."
778 (let ((inhibit-read-only t)
779 (inhibit-modification-hooks t))
780 (apply 'insert args)))
782 (defun widget-convert-text (type from to
783 &optional button-from button-to
785 "Return a widget of type TYPE with endpoint FROM TO.
786 Optional ARGS are extra keyword arguments for TYPE.
787 and TO will be used as the widgets end points. If optional arguments
788 BUTTON-FROM and BUTTON-TO are given, these will be used as the widgets
790 Optional ARGS are extra keyword arguments for TYPE."
791 (let ((widget (apply 'widget-convert type :delete 'widget-leave-text args))
792 (from (copy-marker from))
793 (to (copy-marker to)))
794 (set-marker-insertion-type from t)
795 (set-marker-insertion-type to nil)
796 (widget-put widget :from from)
797 (widget-put widget :to to)
799 (widget-specify-button widget button-from button-to))
802 (defun widget-convert-button (type from to &rest args)
803 "Return a widget of type TYPE with endpoint FROM TO.
804 Optional ARGS are extra keyword arguments for TYPE.
805 No text will be inserted to the buffer, instead the text between FROM
806 and TO will be used as the widgets end points, as well as the widgets
808 (apply 'widget-convert-text type from to from to args))
810 (defun widget-leave-text (widget)
811 "Remove markers and overlays from WIDGET and its children."
812 (let ((button (widget-get widget :button-overlay))
813 (sample (widget-get widget :sample-overlay))
814 (doc (widget-get widget :doc-overlay))
815 (field (widget-get widget :field-overlay)))
816 (set-marker (widget-get widget :from) nil)
817 (set-marker (widget-get widget :to) nil)
819 (delete-overlay button))
821 (delete-overlay sample))
823 (delete-overlay doc))
825 (delete-overlay field))
826 (mapc 'widget-leave-text (widget-get widget :children))))
828 ;;; Keymap and Commands.
831 (defvar widget-keymap
832 (let ((map (make-sparse-keymap)))
833 (define-key map "\t" 'widget-forward)
834 (define-key map [(shift tab)] 'widget-backward)
835 (define-key map [backtab] 'widget-backward)
836 (define-key map [down-mouse-2] 'widget-button-click)
837 (define-key map "\C-m" 'widget-button-press)
839 "Keymap containing useful binding for buffers containing widgets.
840 Recommended as a parent keymap for modes using widgets.")
842 (defvar widget-global-map global-map
843 "Keymap used for events a widget does not handle itself.")
844 (make-variable-buffer-local 'widget-global-map)
846 (defvar widget-field-keymap
847 (let ((map (copy-keymap widget-keymap)))
848 (define-key map "\C-k" 'widget-kill-line)
849 (define-key map "\M-\t" 'widget-complete)
850 (define-key map "\C-m" 'widget-field-activate)
851 ;; Since the widget code uses a `field' property to identify fields,
852 ;; ordinary beginning-of-line does the right thing.
853 ;; (define-key map "\C-a" 'widget-beginning-of-line)
854 (define-key map "\C-e" 'widget-end-of-line)
856 "Keymap used inside an editable field.")
858 (defvar widget-text-keymap
859 (let ((map (copy-keymap widget-keymap)))
860 ;; Since the widget code uses a `field' property to identify fields,
861 ;; ordinary beginning-of-line does the right thing.
862 ;; (define-key map "\C-a" 'widget-beginning-of-line)
863 (define-key map "\C-e" 'widget-end-of-line)
865 "Keymap used inside a text field.")
867 (defun widget-field-activate (pos &optional event)
868 "Invoke the editable field at point."
870 (let ((field (widget-field-at pos)))
872 (widget-apply-action field event)
874 (lookup-key widget-global-map (this-command-keys))))))
876 (defface widget-button-pressed-face
880 (:weight bold :underline t)))
881 "Face used for pressed buttons."
882 :group 'widget-faces)
884 (defun widget-button-click (event)
885 "Invoke the button that the mouse is pointing at."
887 (if (widget-event-point event)
888 (let* ((pos (widget-event-point event))
889 (start (event-start event))
890 (button (get-char-property
891 pos 'button (and (windowp (posn-window start))
892 (window-buffer (posn-window start))))))
894 ;; Mouse click on a widget button. Do the following
895 ;; in a save-excursion so that the click on the button
896 ;; doesn't change point.
897 (save-selected-window
898 (select-window (posn-window (event-start event)))
900 (goto-char (posn-point (event-start event)))
901 (let* ((overlay (widget-get button :button-overlay))
902 (face (overlay-get overlay 'face))
903 (mouse-face (overlay-get overlay 'mouse-face)))
905 ;; Read events, including mouse-movement events
906 ;; until we receive a release event. Highlight/
907 ;; unhighlight the button the mouse was initially
908 ;; on when we move over it.
909 (let ((track-mouse t))
911 (when face ; avoid changing around image
913 'face widget-button-pressed-face)
915 'mouse-face widget-button-pressed-face))
916 (unless (widget-apply button :mouse-down-action event)
917 (while (not (widget-button-release-event-p event))
918 (setq event (read-event)
919 pos (widget-event-point event))
921 (eq (get-char-property pos 'button)
926 widget-button-pressed-face)
929 widget-button-pressed-face))
930 (overlay-put overlay 'face face)
931 (overlay-put overlay 'mouse-face mouse-face))))
933 ;; When mouse is released over the button, run
934 ;; its action function.
936 (eq (get-char-property pos 'button) button))
937 (widget-apply-action button event))))
938 (overlay-put overlay 'face face)
939 (overlay-put overlay 'mouse-face mouse-face))))
941 (unless (pos-visible-in-window-p (widget-event-point event))
942 (mouse-set-point event)
947 (let ((up t) command)
948 ;; Mouse click not on a widget button. Find the global
949 ;; command to run, and check whether it is bound to an
951 (mouse-set-point event)
952 (if (memq (event-basic-type event) '(mouse-1 down-mouse-1))
953 (cond ((setq command ;down event
954 (lookup-key widget-global-map [down-mouse-1]))
956 ((setq command ;up event
957 (lookup-key widget-global-map [mouse-1]))))
958 (cond ((setq command ;down event
959 (lookup-key widget-global-map [down-mouse-2]))
961 ((setq command ;up event
962 (lookup-key widget-global-map [mouse-2])))))
964 ;; Don't execute up events twice.
965 (while (not (widget-button-release-event-p event))
966 (setq event (read-event))))
968 (call-interactively command)))))
969 (message "You clicked somewhere weird.")))
971 (defun widget-button-press (pos &optional event)
972 "Invoke button at POS."
974 (let ((button (get-char-property pos 'button)))
976 (widget-apply-action button event)
977 (let ((command (lookup-key widget-global-map (this-command-keys))))
978 (when (commandp command)
979 (call-interactively command))))))
981 (defun widget-tabable-at (&optional pos)
982 "Return the tabable widget at POS, or nil.
983 POS defaults to the value of (point)."
984 (let ((widget (widget-at pos)))
986 (let ((order (widget-get widget :tab-order)))
992 (defvar widget-use-overlay-change t
993 "If non-nil, use overlay change functions to tab around in the buffer.
994 This is much faster, but doesn't work reliably on Emacs 19.34.")
996 (defun widget-move (arg)
997 "Move point to the ARG next field or button.
998 ARG may be negative to move backward."
999 (or (bobp) (> arg 0) (backward-char))
1002 (old (widget-tabable-at))
1007 (goto-char (point-min)))
1008 (widget-use-overlay-change
1009 (goto-char (next-overlay-change (point))))
1012 (and (eq pos (point))
1014 (error "No buttons or fields found"))
1015 (let ((new (widget-tabable-at)))
1017 (unless (eq new old)
1023 (goto-char (point-max)))
1024 (widget-use-overlay-change
1025 (goto-char (previous-overlay-change (point))))
1028 (and (eq pos (point))
1030 (error "No buttons or fields found"))
1031 (let ((new (widget-tabable-at)))
1033 (unless (eq new old)
1034 (setq arg (1+ arg))))))
1035 (let ((new (widget-tabable-at)))
1036 (while (eq (widget-tabable-at) new)
1039 (widget-echo-help (point))
1040 (run-hooks 'widget-move-hook))
1042 (defun widget-forward (arg)
1043 "Move point to the next field or button.
1044 With optional ARG, move across that many fields."
1046 (run-hooks 'widget-forward-hook)
1049 (defun widget-backward (arg)
1050 "Move point to the previous field or button.
1051 With optional ARG, move across that many fields."
1053 (run-hooks 'widget-backward-hook)
1054 (widget-move (- arg)))
1056 ;; Since the widget code uses a `field' property to identify fields,
1057 ;; ordinary beginning-of-line does the right thing.
1058 (defalias 'widget-beginning-of-line 'beginning-of-line)
1060 (defun widget-end-of-line ()
1061 "Go to end of field or end of line, whichever is first.
1062 Trailing spaces at the end of padded fields are not considered part of
1065 ;; Ordinary end-of-line does the right thing, because we're inside
1066 ;; text with a `field' property.
1069 ;; ... except that we want to ignore trailing spaces in fields that
1070 ;; aren't terminated by a newline, because they are used as padding,
1071 ;; and ignored when extracting the entered value of the field.
1072 (skip-chars-backward " " (field-beginning (1- (point))))))
1074 (defun widget-kill-line ()
1075 "Kill to end of field or end of line, whichever is first."
1077 (let* ((field (widget-field-find (point)))
1078 (end (and field (widget-field-end field))))
1079 (if (and field (> (line-beginning-position 2) end))
1080 (kill-region (point) end)
1081 (call-interactively 'kill-line))))
1083 (defcustom widget-complete-field (lookup-key global-map "\M-\t")
1084 "Default function to call for completion inside fields."
1085 :options '(ispell-complete-word complete-tag lisp-complete-symbol)
1089 (defun widget-complete ()
1090 "Complete content of editable field from point.
1091 When not inside a field, move to the previous button or field."
1093 (let ((field (widget-field-find (point))))
1095 (widget-apply field :complete)
1096 (error "Not in an editable field"))))
1098 ;;; Setting up the buffer.
1100 (defvar widget-field-new nil)
1101 ;; List of all newly created editable fields in the buffer.
1102 (make-variable-buffer-local 'widget-field-new)
1104 (defvar widget-field-list nil)
1105 ;; List of all editable fields in the buffer.
1106 (make-variable-buffer-local 'widget-field-list)
1108 (defun widget-at (&optional pos)
1109 "The button or field at POS (default, point)."
1110 (or (get-char-property (or pos (point)) 'button)
1111 (widget-field-at pos)))
1114 (defun widget-setup ()
1115 "Setup current buffer so editing string widgets works."
1116 (let ((inhibit-read-only t)
1117 (inhibit-modification-hooks t)
1119 (while widget-field-new
1120 (setq field (car widget-field-new)
1121 widget-field-new (cdr widget-field-new)
1122 widget-field-list (cons field widget-field-list))
1123 (let ((from (car (widget-get field :field-overlay)))
1124 (to (cdr (widget-get field :field-overlay))))
1125 (widget-specify-field field
1126 (marker-position from) (marker-position to))
1127 (set-marker from nil)
1128 (set-marker to nil))))
1130 (widget-add-change))
1132 (defvar widget-field-last nil)
1133 ;; Last field containing point.
1134 (make-variable-buffer-local 'widget-field-last)
1136 (defvar widget-field-was nil)
1137 ;; The widget data before the change.
1138 (make-variable-buffer-local 'widget-field-was)
1140 (defun widget-field-at (pos)
1141 "Return the widget field at POS, or nil if none."
1142 (let ((field (get-char-property (or pos (point)) 'field)))
1143 (if (eq field 'boundary)
1147 (defun widget-field-buffer (widget)
1148 "Return the start of WIDGET's editing field."
1149 (let ((overlay (widget-get widget :field-overlay)))
1150 (cond ((overlayp overlay)
1151 (overlay-buffer overlay))
1153 (marker-buffer (car overlay))))))
1155 (defun widget-field-start (widget)
1156 "Return the start of WIDGET's editing field."
1157 (let ((overlay (widget-get widget :field-overlay)))
1158 (if (overlayp overlay)
1159 (overlay-start overlay)
1162 (defun widget-field-end (widget)
1163 "Return the end of WIDGET's editing field."
1164 (let ((overlay (widget-get widget :field-overlay)))
1165 ;; Don't subtract one if local-map works at the end of the overlay,
1166 ;; or if a special `boundary' field has been added after the widget
1168 (if (overlayp overlay)
1169 (if (and (not (eq (get-char-property (overlay-end overlay)
1171 (widget-field-buffer widget))
1173 (or widget-field-add-space
1174 (null (widget-get widget :size))))
1175 (1- (overlay-end overlay))
1176 (overlay-end overlay))
1179 (defun widget-field-find (pos)
1180 "Return the field at POS.
1181 Unlike (get-char-property POS 'field) this, works with empty fields too."
1182 (let ((fields widget-field-list)
1185 (setq field (car fields)
1186 fields (cdr fields))
1187 (when (and (<= (widget-field-start field) pos)
1188 (<= pos (widget-field-end field)))
1190 (error "Overlapping fields"))
1191 (setq found field)))
1194 (defun widget-before-change (from to)
1195 ;; This is how, for example, a variable changes its state to `modified'.
1196 ;; when it is being edited.
1197 (unless inhibit-read-only
1198 (let ((from-field (widget-field-find from))
1199 (to-field (widget-field-find to)))
1200 (cond ((not (eq from-field to-field))
1201 (add-hook 'post-command-hook 'widget-add-change nil t)
1202 (signal 'text-read-only
1203 '("Change should be restricted to a single field")))
1205 (add-hook 'post-command-hook 'widget-add-change nil t)
1206 (signal 'text-read-only
1207 '("Attempt to change text outside editable field")))
1208 (widget-field-use-before-change
1209 (widget-apply from-field :notify from-field))))))
1211 (defun widget-add-change ()
1212 (remove-hook 'post-command-hook 'widget-add-change t)
1213 (add-hook 'before-change-functions 'widget-before-change nil t)
1214 (add-hook 'after-change-functions 'widget-after-change nil t))
1216 (defun widget-after-change (from to old)
1217 "Adjust field size and text properties."
1218 (let ((field (widget-field-find from))
1219 (other (widget-field-find to)))
1221 (unless (eq field other)
1222 (error "Change in different fields"))
1223 (let ((size (widget-get field :size)))
1225 (let ((begin (widget-field-start field))
1226 (end (widget-field-end field)))
1227 (cond ((< (- end begin) size)
1231 (insert-char ?\ (- (+ begin size) end))))
1232 ((> (- end begin) size)
1233 ;; Field too large and
1234 (if (or (< (point) (+ begin size))
1236 ;; Point is outside extra space.
1237 (setq begin (+ begin size))
1238 ;; Point is within the extra space.
1239 (setq begin (point)))
1242 (while (and (eq (preceding-char) ?\ )
1244 (delete-backward-char 1)))))))
1245 (widget-specify-secret field))
1246 (widget-apply field :notify field))))
1248 ;;; Widget Functions
1250 ;; These functions are used in the definition of multiple widgets.
1252 (defun widget-parent-action (widget &optional event)
1253 "Tell :parent of WIDGET to handle the :action.
1254 Optional EVENT is the event that triggered the action."
1255 (widget-apply (widget-get widget :parent) :action event))
1257 (defun widget-children-value-delete (widget)
1258 "Delete all :children and :buttons in WIDGET."
1259 (mapc 'widget-delete (widget-get widget :children))
1260 (widget-put widget :children nil)
1261 (mapc 'widget-delete (widget-get widget :buttons))
1262 (widget-put widget :buttons nil))
1264 (defun widget-children-validate (widget)
1265 "All the :children must be valid."
1266 (let ((children (widget-get widget :children))
1268 (while (and children (not found))
1269 (setq child (car children)
1270 children (cdr children)
1271 found (widget-apply child :validate)))
1274 ;; Made defsubst to speed up face editor creation.
1275 (defsubst widget-types-convert-widget (widget)
1276 "Convert :args as widget types in WIDGET."
1277 (widget-put widget :args (mapcar 'widget-convert (widget-get widget :args)))
1280 (defun widget-value-convert-widget (widget)
1281 "Initialize :value from :args in WIDGET."
1282 (let ((args (widget-get widget :args)))
1284 (widget-put widget :value (car args))
1285 ;; Don't convert :value here, as this is done in `widget-convert'.
1286 ;; (widget-put widget :value (widget-apply widget
1287 ;; :value-to-internal (car args)))
1288 (widget-put widget :args nil)))
1291 (defun widget-value-value-get (widget)
1292 "Return the :value property of WIDGET."
1293 (widget-get widget :value))
1295 ;;; The `default' Widget.
1297 (define-widget 'default nil
1298 "Basic widget other widgets are derived from."
1299 :value-to-internal (lambda (widget value) value)
1300 :value-to-external (lambda (widget value) value)
1301 :button-prefix 'widget-button-prefix
1302 :button-suffix 'widget-button-suffix
1303 :complete 'widget-default-complete
1304 :create 'widget-default-create
1307 :format-handler 'widget-default-format-handler
1308 :button-face-get 'widget-default-button-face-get
1309 :sample-face-get 'widget-default-sample-face-get
1310 :delete 'widget-default-delete
1311 :value-set 'widget-default-value-set
1312 :value-inline 'widget-default-value-inline
1313 :default-get 'widget-default-default-get
1314 :menu-tag-get 'widget-default-menu-tag-get
1316 :active 'widget-default-active
1317 :activate 'widget-specify-active
1318 :deactivate 'widget-default-deactivate
1319 :mouse-down-action #'ignore
1320 :action 'widget-default-action
1321 :notify 'widget-default-notify
1322 :prompt-value 'widget-default-prompt-value)
1324 (defun widget-default-complete (widget)
1325 "Call the value of the :complete-function property of WIDGET.
1326 If that does not exists, call the value of `widget-complete-field'."
1327 (call-interactively (or (widget-get widget :complete-function)
1328 widget-complete-field)))
1330 (defun widget-default-create (widget)
1331 "Create WIDGET at point in the current buffer."
1332 (widget-specify-insert
1333 (let ((from (point))
1334 button-begin button-end
1335 sample-begin sample-end
1338 (insert (widget-get widget :format))
1340 ;; Parse escapes in format.
1341 (while (re-search-forward "%\\(.\\)" nil t)
1342 (let ((escape (char-after (match-beginning 1))))
1343 (delete-backward-char 2)
1344 (cond ((eq escape ?%)
1347 (setq button-begin (point))
1348 (insert (widget-get-indirect widget :button-prefix)))
1350 (insert (widget-get-indirect widget :button-suffix))
1351 (setq button-end (point)))
1353 (setq sample-begin (point)))
1355 (setq sample-end (point)))
1357 (when (widget-get widget :indent)
1359 (insert-char ? (widget-get widget :indent))))
1361 (let ((image (widget-get widget :tag-glyph))
1362 (tag (widget-get widget :tag)))
1364 (widget-image-insert widget (or tag "image") image))
1368 (princ (widget-get widget :value)
1369 (current-buffer))))))
1371 (let ((doc (widget-get widget :doc)))
1373 (setq doc-begin (point))
1375 (while (eq (preceding-char) ?\n)
1376 (delete-backward-char 1))
1378 (setq doc-end (point)))))
1380 (if (and button-begin (not button-end))
1381 (widget-apply widget :value-create)
1382 (setq value-pos (point))))
1384 (widget-apply widget :format-handler escape)))))
1385 ;; Specify button, sample, and doc, and insert value.
1386 (and button-begin button-end
1387 (widget-specify-button widget button-begin button-end))
1388 (and sample-begin sample-end
1389 (widget-specify-sample widget sample-begin sample-end))
1390 (and doc-begin doc-end
1391 (widget-specify-doc widget doc-begin doc-end))
1393 (goto-char value-pos)
1394 (widget-apply widget :value-create)))
1395 (let ((from (point-min-marker))
1396 (to (point-max-marker)))
1397 (set-marker-insertion-type from t)
1398 (set-marker-insertion-type to nil)
1399 (widget-put widget :from from)
1400 (widget-put widget :to to)))
1401 (widget-clear-undo))
1403 (defun widget-default-format-handler (widget escape)
1404 ;; We recognize the %h escape by default.
1405 (let* ((buttons (widget-get widget :buttons)))
1406 (cond ((eq escape ?h)
1407 (let* ((doc-property (widget-get widget :documentation-property))
1408 (doc-try (cond ((widget-get widget :doc))
1409 ((functionp doc-property)
1410 (funcall doc-property
1411 (widget-get widget :value)))
1412 ((symbolp doc-property)
1413 (documentation-property
1414 (widget-get widget :value)
1416 (doc-text (and (stringp doc-try)
1417 (> (length doc-try) 1)
1419 (doc-indent (widget-get widget :documentation-indent)))
1421 (and (eq (preceding-char) ?\n)
1422 (widget-get widget :indent)
1423 (insert-char ? (widget-get widget :indent)))
1424 ;; The `*' in the beginning is redundant.
1425 (when (eq (aref doc-text 0) ?*)
1426 (setq doc-text (substring doc-text 1)))
1427 ;; Get rid of trailing newlines.
1428 (when (string-match "\n+\\'" doc-text)
1429 (setq doc-text (substring doc-text 0 (match-beginning 0))))
1430 (push (widget-create-child-and-convert
1431 widget 'documentation-string
1432 :indent (cond ((numberp doc-indent )
1440 (error "Unknown escape `%c'" escape)))
1441 (widget-put widget :buttons buttons)))
1443 (defun widget-default-button-face-get (widget)
1444 ;; Use :button-face or widget-button-face
1445 (or (widget-get widget :button-face)
1446 (let ((parent (widget-get widget :parent)))
1448 (widget-apply parent :button-face-get)
1449 widget-button-face))))
1451 (defun widget-default-sample-face-get (widget)
1452 ;; Use :sample-face.
1453 (widget-get widget :sample-face))
1455 (defun widget-default-delete (widget)
1456 "Remove widget from the buffer."
1457 (let ((from (widget-get widget :from))
1458 (to (widget-get widget :to))
1459 (inactive-overlay (widget-get widget :inactive))
1460 (button-overlay (widget-get widget :button-overlay))
1461 (sample-overlay (widget-get widget :sample-overlay))
1462 (doc-overlay (widget-get widget :doc-overlay))
1463 (inhibit-modification-hooks t)
1464 (inhibit-read-only t))
1465 (widget-apply widget :value-delete)
1466 (when inactive-overlay
1467 (delete-overlay inactive-overlay))
1468 (when button-overlay
1469 (delete-overlay button-overlay))
1470 (when sample-overlay
1471 (delete-overlay sample-overlay))
1473 (delete-overlay doc-overlay))
1475 ;; Kludge: this doesn't need to be true for empty formats.
1476 (delete-region from to))
1477 (set-marker from nil)
1478 (set-marker to nil))
1479 (widget-clear-undo))
1481 (defun widget-default-value-set (widget value)
1482 "Recreate widget with new value."
1483 (let* ((old-pos (point))
1484 (from (copy-marker (widget-get widget :from)))
1485 (to (copy-marker (widget-get widget :to)))
1486 (offset (if (and (<= from old-pos) (<= old-pos to))
1487 (if (>= old-pos (1- to))
1489 (- old-pos from)))))
1490 ;;??? Bug: this ought to insert the new value before deleting the old one,
1491 ;; so that markers on either side of the value automatically
1492 ;; stay on the same side. -- rms.
1494 (goto-char (widget-get widget :from))
1495 (widget-apply widget :delete)
1496 (widget-put widget :value value)
1497 (widget-apply widget :create))
1500 (goto-char (+ (widget-get widget :to) offset 1))
1501 (goto-char (min (+ from offset) (1- (widget-get widget :to))))))))
1503 (defun widget-default-value-inline (widget)
1504 "Wrap value in a list unless it is inline."
1505 (if (widget-get widget :inline)
1506 (widget-value widget)
1507 (list (widget-value widget))))
1509 (defun widget-default-default-get (widget)
1511 (widget-get widget :value))
1513 (defun widget-default-menu-tag-get (widget)
1514 "Use tag or value for menus."
1515 (or (widget-get widget :menu-tag)
1516 (widget-get widget :tag)
1517 (widget-princ-to-string (widget-get widget :value))))
1519 (defun widget-default-active (widget)
1520 "Return t iff this widget active (user modifiable)."
1521 (or (widget-get widget :always-active)
1522 (and (not (widget-get widget :inactive))
1523 (let ((parent (widget-get widget :parent)))
1525 (widget-apply parent :active))))))
1527 (defun widget-default-deactivate (widget)
1528 "Make WIDGET inactive for user modifications."
1529 (widget-specify-inactive widget
1530 (widget-get widget :from)
1531 (widget-get widget :to)))
1533 (defun widget-default-action (widget &optional event)
1534 "Notify the parent when a widget changes."
1535 (let ((parent (widget-get widget :parent)))
1537 (widget-apply parent :notify widget event))))
1539 (defun widget-default-notify (widget child &optional event)
1540 "Pass notification to parent."
1541 (widget-default-action widget event))
1543 (defun widget-default-prompt-value (widget prompt value unbound)
1544 "Read an arbitrary value. Stolen from `set-variable'."
1545 ;; (let ((initial (if unbound
1547 ;; It would be nice if we could do a `(cons val 1)' here.
1548 ;; (prin1-to-string (custom-quote value))))))
1549 (eval-minibuffer prompt))
1551 ;;; The `item' Widget.
1553 (define-widget 'item 'default
1554 "Constant items for inclusion in other widgets."
1555 :convert-widget 'widget-value-convert-widget
1556 :value-create 'widget-item-value-create
1557 :value-delete 'ignore
1558 :value-get 'widget-value-value-get
1559 :match 'widget-item-match
1560 :match-inline 'widget-item-match-inline
1561 :action 'widget-item-action
1564 (defun widget-item-value-create (widget)
1565 "Insert the printed representation of the value."
1566 (princ (widget-get widget :value) (current-buffer)))
1568 (defun widget-item-match (widget value)
1569 ;; Match if the value is the same.
1570 (equal (widget-get widget :value) value))
1572 (defun widget-item-match-inline (widget values)
1573 ;; Match if the value is the same.
1574 (let ((value (widget-get widget :value)))
1576 (<= (length value) (length values))
1577 (let ((head (widget-sublist values 0 (length value))))
1578 (and (equal head value)
1579 (cons head (widget-sublist values (length value))))))))
1581 (defun widget-sublist (list start &optional end)
1582 "Return the sublist of LIST from START to END.
1583 If END is omitted, it defaults to the length of LIST."
1584 (if (> start 0) (setq list (nthcdr start list)))
1586 (unless (<= end start)
1587 (setq list (copy-sequence list))
1588 (setcdr (nthcdr (- end start 1) list) nil)
1590 (copy-sequence list)))
1592 (defun widget-item-action (widget &optional event)
1593 ;; Just notify itself.
1594 (widget-apply widget :notify widget event))
1596 ;;; The `push-button' Widget.
1598 ;; (defcustom widget-push-button-gui t
1599 ;; "If non nil, use GUI push buttons when available."
1603 ;; Cache already created GUI objects.
1604 ;; (defvar widget-push-button-cache nil)
1606 (defcustom widget-push-button-prefix "["
1607 "String used as prefix for buttons."
1609 :group 'widget-button)
1611 (defcustom widget-push-button-suffix "]"
1612 "String used as suffix for buttons."
1614 :group 'widget-button)
1616 (define-widget 'push-button 'item
1617 "A pushable button."
1620 :value-create 'widget-push-button-value-create
1623 (defun widget-push-button-value-create (widget)
1624 "Insert text representing the `on' and `off' states."
1625 (let* ((tag (or (widget-get widget :tag)
1626 (widget-get widget :value)))
1627 (tag-glyph (widget-get widget :tag-glyph))
1628 (text (concat widget-push-button-prefix
1629 tag widget-push-button-suffix)))
1631 (widget-image-insert widget text tag-glyph)
1634 ;; (defun widget-gui-action (widget)
1635 ;; "Apply :action for WIDGET."
1636 ;; (widget-apply-action widget (this-command-keys)))
1638 ;;; The `link' Widget.
1640 (defcustom widget-link-prefix "["
1641 "String used as prefix for links."
1643 :group 'widget-button)
1645 (defcustom widget-link-suffix "]"
1646 "String used as suffix for links."
1648 :group 'widget-button)
1650 (define-widget 'link 'item
1652 :button-prefix 'widget-link-prefix
1653 :button-suffix 'widget-link-suffix
1654 :help-echo "Follow the link."
1657 ;;; The `info-link' Widget.
1659 (define-widget 'info-link 'link
1660 "A link to an info file."
1661 :action 'widget-info-link-action)
1663 (defun widget-info-link-action (widget &optional event)
1664 "Open the info node specified by WIDGET."
1665 (Info-goto-node (widget-value widget)))
1667 ;;; The `url-link' Widget.
1669 (define-widget 'url-link 'link
1670 "A link to an www page."
1671 :action 'widget-url-link-action)
1673 (defun widget-url-link-action (widget &optional event)
1674 "Open the url specified by WIDGET."
1675 (browse-url (widget-value widget)))
1677 ;;; The `function-link' Widget.
1679 (define-widget 'function-link 'link
1680 "A link to an Emacs function."
1681 :action 'widget-function-link-action)
1683 (defun widget-function-link-action (widget &optional event)
1684 "Show the function specified by WIDGET."
1685 (describe-function (widget-value widget)))
1687 ;;; The `variable-link' Widget.
1689 (define-widget 'variable-link 'link
1690 "A link to an Emacs variable."
1691 :action 'widget-variable-link-action)
1693 (defun widget-variable-link-action (widget &optional event)
1694 "Show the variable specified by WIDGET."
1695 (describe-variable (widget-value widget)))
1697 ;;; The `file-link' Widget.
1699 (define-widget 'file-link 'link
1701 :action 'widget-file-link-action)
1703 (defun widget-file-link-action (widget &optional event)
1704 "Find the file specified by WIDGET."
1705 (find-file (widget-value widget)))
1707 ;;; The `emacs-library-link' Widget.
1709 (define-widget 'emacs-library-link 'link
1710 "A link to an Emacs Lisp library file."
1711 :action 'widget-emacs-library-link-action)
1713 (defun widget-emacs-library-link-action (widget &optional event)
1714 "Find the Emacs Library file specified by WIDGET."
1715 (find-file (locate-library (widget-value widget))))
1717 ;;; The `emacs-commentary-link' Widget.
1719 (define-widget 'emacs-commentary-link 'link
1720 "A link to Commentary in an Emacs Lisp library file."
1721 :action 'widget-emacs-commentary-link-action)
1723 (defun widget-emacs-commentary-link-action (widget &optional event)
1724 "Find the Commentary section of the Emacs file specified by WIDGET."
1725 (finder-commentary (widget-value widget)))
1727 ;;; The `editable-field' Widget.
1729 (define-widget 'editable-field 'default
1730 "An editable text field."
1731 :convert-widget 'widget-value-convert-widget
1732 :keymap widget-field-keymap
1734 :help-echo "M-TAB: complete field; RET: enter value"
1736 :prompt-internal 'widget-field-prompt-internal
1737 :prompt-history 'widget-field-history
1738 :prompt-value 'widget-field-prompt-value
1739 :action 'widget-field-action
1740 :validate 'widget-field-validate
1742 :error "Field's value doesn't match allowed forms"
1743 :value-create 'widget-field-value-create
1744 :value-delete 'widget-field-value-delete
1745 :value-get 'widget-field-value-get
1746 :match 'widget-field-match)
1748 (defvar widget-field-history nil
1749 "History of field minibuffer edits.")
1751 (defun widget-field-prompt-internal (widget prompt initial history)
1752 "Read string for WIDGET promptinhg with PROMPT.
1753 INITIAL is the initial input and HISTORY is a symbol containing
1755 (read-string prompt initial history))
1757 (defun widget-field-prompt-value (widget prompt value unbound)
1758 "Prompt for a string."
1759 (widget-apply widget
1761 (widget-apply widget
1762 :prompt-internal prompt
1764 (cons (widget-apply widget
1765 :value-to-internal value)
1767 (widget-get widget :prompt-history))))
1769 (defvar widget-edit-functions nil)
1771 (defun widget-field-action (widget &optional event)
1772 "Move to next field."
1774 (run-hook-with-args 'widget-edit-functions widget))
1776 (defun widget-field-validate (widget)
1777 "Valid if the content matches `:valid-regexp'."
1778 (unless (string-match (widget-get widget :valid-regexp)
1779 (widget-apply widget :value-get))
1782 (defun widget-field-value-create (widget)
1783 "Create an editable text field."
1784 (let ((size (widget-get widget :size))
1785 (value (widget-get widget :value))
1787 ;; This is changed to a real overlay in `widget-setup'. We
1788 ;; need the end points to behave differently until
1789 ;; `widget-setup' is called.
1790 (overlay (cons (make-marker) (make-marker))))
1791 (widget-put widget :field-overlay overlay)
1794 (< (length value) size)
1795 (insert-char ?\ (- size (length value))))
1796 (unless (memq widget widget-field-list)
1797 (setq widget-field-new (cons widget widget-field-new)))
1798 (move-marker (cdr overlay) (point))
1799 (set-marker-insertion-type (cdr overlay) nil)
1802 (move-marker (car overlay) from)
1803 (set-marker-insertion-type (car overlay) t)))
1805 (defun widget-field-value-delete (widget)
1806 "Remove the widget from the list of active editing fields."
1807 (setq widget-field-list (delq widget widget-field-list))
1808 (setq widget-field-new (delq widget widget-field-new))
1809 ;; These are nil if the :format string doesn't contain `%v'.
1810 (let ((overlay (widget-get widget :field-overlay)))
1811 (when (overlayp overlay)
1812 (delete-overlay overlay))))
1814 (defun widget-field-value-get (widget)
1815 "Return current text in editing field."
1816 (let ((from (widget-field-start widget))
1817 (to (widget-field-end widget))
1818 (buffer (widget-field-buffer widget))
1819 (size (widget-get widget :size))
1820 (secret (widget-get widget :secret))
1821 (old (current-buffer)))
1828 (eq (char-after (1- to)) ?\ ))
1830 (let ((result (buffer-substring-no-properties from to)))
1833 (while (< (+ from index) to)
1835 (get-char-property (+ from index) 'secret))
1836 (setq index (1+ index)))))
1839 (widget-get widget :value))))
1841 (defun widget-field-match (widget value)
1842 ;; Match any string.
1845 ;;; The `text' Widget.
1847 (define-widget 'text 'editable-field
1848 "A multiline text area."
1849 :keymap widget-text-keymap)
1851 ;;; The `menu-choice' Widget.
1853 (define-widget 'menu-choice 'default
1854 "A menu of options."
1855 :convert-widget 'widget-types-convert-widget
1856 :format "%[%t%]: %v"
1859 :void '(item :format "invalid (%t)\n")
1860 :value-create 'widget-choice-value-create
1861 :value-delete 'widget-children-value-delete
1862 :value-get 'widget-choice-value-get
1863 :value-inline 'widget-choice-value-inline
1864 :default-get 'widget-choice-default-get
1865 :mouse-down-action 'widget-choice-mouse-down-action
1866 :action 'widget-choice-action
1867 :error "Make a choice"
1868 :validate 'widget-choice-validate
1869 :match 'widget-choice-match
1870 :match-inline 'widget-choice-match-inline)
1872 (defun widget-choice-value-create (widget)
1873 "Insert the first choice that matches the value."
1874 (let ((value (widget-get widget :value))
1875 (args (widget-get widget :args))
1876 (explicit (widget-get widget :explicit-choice))
1878 (if (and explicit (equal value (widget-get widget :explicit-choice-value)))
1880 ;; If the user specified the choice for this value,
1881 ;; respect that choice as long as the value is the same.
1882 (widget-put widget :children (list (widget-create-child-value
1883 widget explicit value)))
1884 (widget-put widget :choice explicit))
1886 (setq current (car args)
1888 (when (widget-apply current :match value)
1889 (widget-put widget :children (list (widget-create-child-value
1890 widget current value)))
1891 (widget-put widget :choice current)
1895 (let ((void (widget-get widget :void)))
1896 (widget-put widget :children (list (widget-create-child-and-convert
1897 widget void :value value)))
1898 (widget-put widget :choice void))))))
1900 (defun widget-choice-value-get (widget)
1901 ;; Get value of the child widget.
1902 (widget-value (car (widget-get widget :children))))
1904 (defun widget-choice-value-inline (widget)
1905 ;; Get value of the child widget.
1906 (widget-apply (car (widget-get widget :children)) :value-inline))
1908 (defun widget-choice-default-get (widget)
1909 ;; Get default for the first choice.
1910 (widget-default-get (car (widget-get widget :args))))
1912 (defcustom widget-choice-toggle nil
1913 "If non-nil, a binary choice will just toggle between the values.
1914 Otherwise, the user will explicitly have to choose between the values
1915 when he invoked the menu."
1919 (defun widget-choice-mouse-down-action (widget &optional event)
1920 ;; Return non-nil if we need a menu.
1921 (let ((args (widget-get widget :args))
1922 (old (widget-get widget :choice)))
1923 (cond ((not (display-popup-menus-p))
1924 ;; No place to pop up a menu.
1926 ((< (length args) 2)
1927 ;; Empty or singleton list, just return the value.
1929 ((> (length args) widget-menu-max-size)
1930 ;; Too long, prompt.
1932 ((> (length args) 2)
1933 ;; Reasonable sized list, use menu.
1935 ((and widget-choice-toggle (memq old args))
1939 ;; Ask which of the two.
1942 (defun widget-choice-action (widget &optional event)
1944 (let ((args (widget-get widget :args))
1945 (old (widget-get widget :choice))
1946 (tag (widget-apply widget :menu-tag-get))
1947 (completion-ignore-case (widget-get widget :case-fold))
1950 ;; Remember old value.
1951 (if (and old (not (widget-apply widget :validate)))
1952 (let* ((external (widget-value widget))
1953 (internal (widget-apply old :value-to-internal external)))
1954 (widget-put old :value internal)))
1957 (cond ((= (length args) 0)
1959 ((= (length args) 1)
1961 ((and widget-choice-toggle
1964 (if (eq old (nth 0 args))
1969 (setq current (car args)
1972 (cons (cons (widget-apply current :menu-tag-get)
1975 (setq this-explicit t)
1976 (widget-choose tag (reverse choices) event))))
1978 ;; If this was an explicit user choice,
1979 ;; record the choice, and the record the value it was made for.
1980 ;; widget-choice-value-create will respect this choice,
1981 ;; as long as the value is the same.
1983 (widget-put widget :explicit-choice current)
1984 (widget-put widget :explicit-choice-value (widget-get widget :value)))
1986 widget (widget-apply current
1987 :value-to-external (widget-default-get current)))
1989 (widget-apply widget :notify widget event)))
1990 (run-hook-with-args 'widget-edit-functions widget))
1992 (defun widget-choice-validate (widget)
1993 ;; Valid if we have made a valid choice.
1994 (if (eq (widget-get widget :void) (widget-get widget :choice))
1996 (widget-apply (car (widget-get widget :children)) :validate)))
1998 (defun widget-choice-match (widget value)
1999 ;; Matches if one of the choices matches.
2000 (let ((args (widget-get widget :args))
2002 (while (and args (not found))
2003 (setq current (car args)
2005 found (widget-apply current :match value)))
2008 (defun widget-choice-match-inline (widget values)
2009 ;; Matches if one of the choices matches.
2010 (let ((args (widget-get widget :args))
2012 (while (and args (null found))
2013 (setq current (car args)
2015 found (widget-match-inline current values)))
2018 ;;; The `toggle' Widget.
2020 (define-widget 'toggle 'item
2021 "Toggle between two states."
2023 :value-create 'widget-toggle-value-create
2024 :action 'widget-toggle-action
2025 :match (lambda (widget value) t)
2029 (defun widget-toggle-value-create (widget)
2030 "Insert text representing the `on' and `off' states."
2031 (if (widget-value widget)
2032 (let ((image (widget-get widget :on-glyph)))
2033 (and (display-graphic-p)
2035 (not (eq (car image) 'image))
2036 (widget-put widget :on-glyph (setq image (eval image))))
2037 (widget-image-insert widget
2038 (widget-get widget :on)
2040 (let ((image (widget-get widget :off-glyph)))
2041 (and (display-graphic-p)
2043 (not (eq (car image) 'image))
2044 (widget-put widget :off-glyph (setq image (eval image))))
2045 (widget-image-insert widget (widget-get widget :off) image))))
2047 (defun widget-toggle-action (widget &optional event)
2049 (widget-value-set widget (not (widget-value widget)))
2050 (widget-apply widget :notify widget event)
2051 (run-hook-with-args 'widget-edit-functions widget))
2053 ;;; The `checkbox' Widget.
2055 (define-widget 'checkbox 'toggle
2056 "A checkbox toggle."
2061 ;; We could probably do the same job as the images using single
2062 ;; space characters in a boxed face with a stretch specification to
2063 ;; make them square.
2064 :on-glyph '(create-image "\300\300\141\143\067\076\034\030"
2065 'xbm t :width 8 :height 8
2066 :background "grey75" ; like default mode line
2071 :off-glyph '(create-image (make-string 8 0)
2072 'xbm t :width 8 :height 8
2073 :background "grey75"
2077 :help-echo "Toggle this item."
2078 :action 'widget-checkbox-action)
2080 (defun widget-checkbox-action (widget &optional event)
2081 "Toggle checkbox, notify parent, and set active state of sibling."
2082 (widget-toggle-action widget event)
2083 (let ((sibling (widget-get-sibling widget)))
2085 (if (widget-value widget)
2086 (widget-apply sibling :activate)
2087 (widget-apply sibling :deactivate)))))
2089 ;;; The `checklist' Widget.
2091 (define-widget 'checklist 'default
2092 "A multiple choice widget."
2093 :convert-widget 'widget-types-convert-widget
2096 :entry-format "%b %v"
2098 :value-create 'widget-checklist-value-create
2099 :value-delete 'widget-children-value-delete
2100 :value-get 'widget-checklist-value-get
2101 :validate 'widget-checklist-validate
2102 :match 'widget-checklist-match
2103 :match-inline 'widget-checklist-match-inline)
2105 (defun widget-checklist-value-create (widget)
2106 ;; Insert all values
2107 (let ((alist (widget-checklist-match-find widget (widget-get widget :value)))
2108 (args (widget-get widget :args)))
2110 (widget-checklist-add-item widget (car args) (assq (car args) alist))
2111 (setq args (cdr args)))
2112 (widget-put widget :children (nreverse (widget-get widget :children)))))
2114 (defun widget-checklist-add-item (widget type chosen)
2115 "Create checklist item in WIDGET of type TYPE.
2116 If the item is checked, CHOSEN is a cons whose cdr is the value."
2117 (and (eq (preceding-char) ?\n)
2118 (widget-get widget :indent)
2119 (insert-char ? (widget-get widget :indent)))
2120 (widget-specify-insert
2121 (let* ((children (widget-get widget :children))
2122 (buttons (widget-get widget :buttons))
2123 (button-args (or (widget-get type :sibling-args)
2124 (widget-get widget :button-args)))
2127 (insert (widget-get widget :entry-format))
2129 ;; Parse % escapes in format.
2130 (while (re-search-forward "%\\([bv%]\\)" nil t)
2131 (let ((escape (char-after (match-beginning 1))))
2132 (delete-backward-char 2)
2133 (cond ((eq escape ?%)
2136 (setq button (apply 'widget-create-child-and-convert
2138 :value (not (null chosen))
2143 (let ((child (widget-create-child widget type)))
2144 (widget-apply child :deactivate)
2146 ((widget-get type :inline)
2147 (widget-create-child-value
2148 widget type (cdr chosen)))
2150 (widget-create-child-value
2151 widget type (car (cdr chosen)))))))
2153 (error "Unknown escape `%c'" escape)))))
2154 ;; Update properties.
2155 (and button child (widget-put child :button button))
2156 (and button (widget-put widget :buttons (cons button buttons)))
2157 (and child (widget-put widget :children (cons child children))))))
2159 (defun widget-checklist-match (widget values)
2160 ;; All values must match a type in the checklist.
2162 (null (cdr (widget-checklist-match-inline widget values)))))
2164 (defun widget-checklist-match-inline (widget values)
2165 ;; Find the values which match a type in the checklist.
2166 (let ((greedy (widget-get widget :greedy))
2167 (args (copy-sequence (widget-get widget :args)))
2170 (let ((answer (widget-checklist-match-up args values)))
2172 (let ((vals (widget-match-inline answer values)))
2173 (setq found (append found (car vals))
2175 args (delq answer args))))
2177 (setq rest (append rest (list (car values)))
2178 values (cdr values)))
2180 (setq rest (append rest values)
2184 (defun widget-checklist-match-find (widget vals)
2185 "Find the vals which match a type in the checklist.
2186 Return an alist of (TYPE MATCH)."
2187 (let ((greedy (widget-get widget :greedy))
2188 (args (copy-sequence (widget-get widget :args)))
2191 (let ((answer (widget-checklist-match-up args vals)))
2193 (let ((match (widget-match-inline answer vals)))
2194 (setq found (cons (cons answer (car match)) found)
2196 args (delq answer args))))
2198 (setq vals (cdr vals)))
2203 (defun widget-checklist-match-up (args vals)
2204 "Return the first type from ARGS that matches VALS."
2205 (let (current found)
2206 (while (and args (null found))
2207 (setq current (car args)
2209 found (widget-match-inline current vals)))
2213 (defun widget-checklist-value-get (widget)
2214 ;; The values of all selected items.
2215 (let ((children (widget-get widget :children))
2218 (setq child (car children)
2219 children (cdr children))
2220 (if (widget-value (widget-get child :button))
2221 (setq result (append result (widget-apply child :value-inline)))))
2224 (defun widget-checklist-validate (widget)
2225 ;; Ticked chilren must be valid.
2226 (let ((children (widget-get widget :children))
2228 (while (and children (not found))
2229 (setq child (car children)
2230 children (cdr children)
2231 button (widget-get child :button)
2232 found (and (widget-value button)
2233 (widget-apply child :validate))))
2236 ;;; The `option' Widget
2238 (define-widget 'option 'checklist
2239 "An widget with an optional item."
2242 ;;; The `choice-item' Widget.
2244 (define-widget 'choice-item 'item
2245 "Button items that delegate action events to their parents."
2246 :action 'widget-parent-action
2247 :format "%[%t%] \n")
2249 ;;; The `radio-button' Widget.
2251 (define-widget 'radio-button 'toggle
2252 "A radio button for use in the `radio' widget."
2253 :notify 'widget-radio-button-notify
2260 :off-glyph "radio0")
2262 (defun widget-radio-button-notify (widget child &optional event)
2264 (widget-apply (widget-get widget :parent) :action widget event))
2266 ;;; The `radio-button-choice' Widget.
2268 (define-widget 'radio-button-choice 'default
2269 "Select one of multiple options."
2270 :convert-widget 'widget-types-convert-widget
2273 :entry-format "%b %v"
2274 :value-create 'widget-radio-value-create
2275 :value-delete 'widget-children-value-delete
2276 :value-get 'widget-radio-value-get
2277 :value-inline 'widget-radio-value-inline
2278 :value-set 'widget-radio-value-set
2279 :error "You must push one of the buttons"
2280 :validate 'widget-radio-validate
2281 :match 'widget-choice-match
2282 :match-inline 'widget-choice-match-inline
2283 :action 'widget-radio-action)
2285 (defun widget-radio-value-create (widget)
2286 ;; Insert all values
2287 (let ((args (widget-get widget :args))
2290 (setq arg (car args)
2292 (widget-radio-add-item widget arg))))
2294 (defun widget-radio-add-item (widget type)
2295 "Add to radio widget WIDGET a new radio button item of type TYPE."
2296 ;; (setq type (widget-convert type))
2297 (and (eq (preceding-char) ?\n)
2298 (widget-get widget :indent)
2299 (insert-char ? (widget-get widget :indent)))
2300 (widget-specify-insert
2301 (let* ((value (widget-get widget :value))
2302 (children (widget-get widget :children))
2303 (buttons (widget-get widget :buttons))
2304 (button-args (or (widget-get type :sibling-args)
2305 (widget-get widget :button-args)))
2307 (chosen (and (null (widget-get widget :choice))
2308 (widget-apply type :match value)))
2310 (insert (widget-get widget :entry-format))
2312 ;; Parse % escapes in format.
2313 (while (re-search-forward "%\\([bv%]\\)" nil t)
2314 (let ((escape (char-after (match-beginning 1))))
2315 (delete-backward-char 2)
2316 (cond ((eq escape ?%)
2319 (setq button (apply 'widget-create-child-and-convert
2320 widget 'radio-button
2321 :value (not (null chosen))
2324 (setq child (if chosen
2325 (widget-create-child-value
2327 (widget-create-child widget type)))
2329 (widget-apply child :deactivate)))
2331 (error "Unknown escape `%c'" escape)))))
2332 ;; Update properties.
2334 (widget-put widget :choice type))
2336 (widget-put child :button button)
2337 (widget-put widget :buttons (nconc buttons (list button))))
2339 (widget-put widget :children (nconc children (list child))))
2342 (defun widget-radio-value-get (widget)
2343 ;; Get value of the child widget.
2344 (let ((chosen (widget-radio-chosen widget)))
2345 (and chosen (widget-value chosen))))
2347 (defun widget-radio-chosen (widget)
2348 "Return the widget representing the chosen radio button."
2349 (let ((children (widget-get widget :children))
2352 (setq current (car children)
2353 children (cdr children))
2354 (when (widget-apply (widget-get current :button) :value-get)
2359 (defun widget-radio-value-inline (widget)
2360 ;; Get value of the child widget.
2361 (let ((children (widget-get widget :children))
2364 (setq current (car children)
2365 children (cdr children))
2366 (when (widget-apply (widget-get current :button) :value-get)
2367 (setq found (widget-apply current :value-inline)
2371 (defun widget-radio-value-set (widget value)
2372 ;; We can't just delete and recreate a radio widget, since children
2373 ;; can be added after the original creation and won't be recreated
2375 (let ((children (widget-get widget :children))
2378 (setq current (car children)
2379 children (cdr children))
2380 (let* ((button (widget-get current :button))
2381 (match (and (not found)
2382 (widget-apply current :match value))))
2383 (widget-value-set button match)
2386 (widget-value-set current value)
2387 (widget-apply current :activate))
2388 (widget-apply current :deactivate))
2389 (setq found (or found match))))))
2391 (defun widget-radio-validate (widget)
2392 ;; Valid if we have made a valid choice.
2393 (let ((children (widget-get widget :children))
2394 current found button)
2395 (while (and children (not found))
2396 (setq current (car children)
2397 children (cdr children)
2398 button (widget-get current :button)
2399 found (widget-apply button :value-get)))
2401 (widget-apply current :validate)
2404 (defun widget-radio-action (widget child event)
2405 ;; Check if a radio button was pressed.
2406 (let ((children (widget-get widget :children))
2407 (buttons (widget-get widget :buttons))
2409 (when (memq child buttons)
2411 (setq current (car children)
2412 children (cdr children))
2413 (let* ((button (widget-get current :button)))
2414 (cond ((eq child button)
2415 (widget-value-set button t)
2416 (widget-apply current :activate))
2417 ((widget-value button)
2418 (widget-value-set button nil)
2419 (widget-apply current :deactivate)))))))
2420 ;; Pass notification to parent.
2421 (widget-apply widget :notify child event))
2423 ;;; The `insert-button' Widget.
2425 (define-widget 'insert-button 'push-button
2426 "An insert button for the `editable-list' widget."
2428 :help-echo "Insert a new item into the list at this position."
2429 :action 'widget-insert-button-action)
2431 (defun widget-insert-button-action (widget &optional event)
2432 ;; Ask the parent to insert a new item.
2433 (widget-apply (widget-get widget :parent)
2434 :insert-before (widget-get widget :widget)))
2436 ;;; The `delete-button' Widget.
2438 (define-widget 'delete-button 'push-button
2439 "A delete button for the `editable-list' widget."
2441 :help-echo "Delete this item from the list."
2442 :action 'widget-delete-button-action)
2444 (defun widget-delete-button-action (widget &optional event)
2445 ;; Ask the parent to insert a new item.
2446 (widget-apply (widget-get widget :parent)
2447 :delete-at (widget-get widget :widget)))
2449 ;;; The `editable-list' Widget.
2451 ;; (defcustom widget-editable-list-gui nil
2452 ;; "If non nil, use GUI push-buttons in editable list when available."
2456 (define-widget 'editable-list 'default
2457 "A variable list of widgets of the same type."
2458 :convert-widget 'widget-types-convert-widget
2461 :format-handler 'widget-editable-list-format-handler
2462 :entry-format "%i %d %v"
2463 :value-create 'widget-editable-list-value-create
2464 :value-delete 'widget-children-value-delete
2465 :value-get 'widget-editable-list-value-get
2466 :validate 'widget-children-validate
2467 :match 'widget-editable-list-match
2468 :match-inline 'widget-editable-list-match-inline
2469 :insert-before 'widget-editable-list-insert-before
2470 :delete-at 'widget-editable-list-delete-at)
2472 (defun widget-editable-list-format-handler (widget escape)
2473 ;; We recognize the insert button.
2474 ;;; (let ((widget-push-button-gui widget-editable-list-gui))
2475 (cond ((eq escape ?i)
2476 (and (widget-get widget :indent)
2477 (insert-char ?\ (widget-get widget :indent)))
2478 (apply 'widget-create-child-and-convert
2479 widget 'insert-button
2480 (widget-get widget :append-button-args)))
2482 (widget-default-format-handler widget escape)))
2486 (defun widget-editable-list-value-create (widget)
2487 ;; Insert all values
2488 (let* ((value (widget-get widget :value))
2489 (type (nth 0 (widget-get widget :args)))
2491 (widget-put widget :value-pos (copy-marker (point)))
2492 (set-marker-insertion-type (widget-get widget :value-pos) t)
2494 (let ((answer (widget-match-inline type value)))
2496 (setq children (cons (widget-editable-list-entry-create
2498 (if (widget-get type :inline)
2505 (widget-put widget :children (nreverse children))))
2507 (defun widget-editable-list-value-get (widget)
2508 ;; Get value of the child widget.
2509 (apply 'append (mapcar (lambda (child) (widget-apply child :value-inline))
2510 (widget-get widget :children))))
2512 (defun widget-editable-list-match (widget value)
2513 ;; Value must be a list and all the members must match the type.
2515 (null (cdr (widget-editable-list-match-inline widget value)))))
2517 (defun widget-editable-list-match-inline (widget value)
2518 (let ((type (nth 0 (widget-get widget :args)))
2521 (while (and value ok)
2522 (let ((answer (widget-match-inline type value)))
2524 (setq found (append found (car answer))
2527 (cons found value)))
2529 (defun widget-editable-list-insert-before (widget before)
2530 ;; Insert a new child in the list of children.
2532 (let ((children (widget-get widget :children))
2533 (inhibit-read-only t)
2534 before-change-functions
2535 after-change-functions)
2537 (goto-char (widget-get before :entry-from)))
2539 (goto-char (widget-get widget :value-pos))))
2540 (let ((child (widget-editable-list-entry-create
2542 (when (< (widget-get child :entry-from) (widget-get widget :from))
2543 (set-marker (widget-get widget :from)
2544 (widget-get child :entry-from)))
2545 (if (eq (car children) before)
2546 (widget-put widget :children (cons child children))
2547 (while (not (eq (car (cdr children)) before))
2548 (setq children (cdr children)))
2549 (setcdr children (cons child (cdr children)))))))
2551 (widget-apply widget :notify widget))
2553 (defun widget-editable-list-delete-at (widget child)
2554 ;; Delete child from list of children.
2556 (let ((buttons (copy-sequence (widget-get widget :buttons)))
2558 (inhibit-read-only t)
2559 before-change-functions
2560 after-change-functions)
2562 (setq button (car buttons)
2563 buttons (cdr buttons))
2564 (when (eq (widget-get button :widget) child)
2566 :buttons (delq button (widget-get widget :buttons)))
2567 (widget-delete button))))
2568 (let ((entry-from (widget-get child :entry-from))
2569 (entry-to (widget-get child :entry-to))
2570 (inhibit-read-only t)
2571 before-change-functions
2572 after-change-functions)
2573 (widget-delete child)
2574 (delete-region entry-from entry-to)
2575 (set-marker entry-from nil)
2576 (set-marker entry-to nil))
2577 (widget-put widget :children (delq child (widget-get widget :children))))
2579 (widget-apply widget :notify widget))
2581 (defun widget-editable-list-entry-create (widget value conv)
2582 ;; Create a new entry to the list.
2583 (let ((type (nth 0 (widget-get widget :args)))
2584 ;;; (widget-push-button-gui widget-editable-list-gui)
2585 child delete insert)
2586 (widget-specify-insert
2588 (and (widget-get widget :indent)
2589 (insert-char ?\ (widget-get widget :indent)))
2590 (insert (widget-get widget :entry-format)))
2591 ;; Parse % escapes in format.
2592 (while (re-search-forward "%\\(.\\)" nil t)
2593 (let ((escape (char-after (match-beginning 1))))
2594 (delete-backward-char 2)
2595 (cond ((eq escape ?%)
2598 (setq insert (apply 'widget-create-child-and-convert
2599 widget 'insert-button
2600 (widget-get widget :insert-button-args))))
2602 (setq delete (apply 'widget-create-child-and-convert
2603 widget 'delete-button
2604 (widget-get widget :delete-button-args))))
2607 (setq child (widget-create-child-value
2609 (setq child (widget-create-child-value
2611 (widget-apply type :value-to-external
2612 (widget-default-get type))))))
2614 (error "Unknown escape `%c'" escape)))))
2616 :buttons (cons delete
2618 (widget-get widget :buttons))))
2619 (let ((entry-from (point-min-marker))
2620 (entry-to (point-max-marker)))
2621 (set-marker-insertion-type entry-from t)
2622 (set-marker-insertion-type entry-to nil)
2623 (widget-put child :entry-from entry-from)
2624 (widget-put child :entry-to entry-to)))
2625 (widget-put insert :widget child)
2626 (widget-put delete :widget child)
2629 ;;; The `group' Widget.
2631 (define-widget 'group 'default
2632 "A widget which groups other widgets inside."
2633 :convert-widget 'widget-types-convert-widget
2635 :value-create 'widget-group-value-create
2636 :value-delete 'widget-children-value-delete
2637 :value-get 'widget-editable-list-value-get
2638 :default-get 'widget-group-default-get
2639 :validate 'widget-children-validate
2640 :match 'widget-group-match
2641 :match-inline 'widget-group-match-inline)
2643 (defun widget-group-value-create (widget)
2644 ;; Create each component.
2645 (let ((args (widget-get widget :args))
2646 (value (widget-get widget :value))
2647 arg answer children)
2649 (setq arg (car args)
2651 answer (widget-match-inline arg value)
2653 (and (eq (preceding-char) ?\n)
2654 (widget-get widget :indent)
2655 (insert-char ?\ (widget-get widget :indent)))
2656 (push (cond ((null answer)
2657 (widget-create-child widget arg))
2658 ((widget-get arg :inline)
2659 (widget-create-child-value widget arg (car answer)))
2661 (widget-create-child-value widget arg (car (car answer)))))
2663 (widget-put widget :children (nreverse children))))
2665 (defun widget-group-default-get (widget)
2666 ;; Get the default of the components.
2667 (mapcar 'widget-default-get (widget-get widget :args)))
2669 (defun widget-group-match (widget values)
2670 ;; Match if the components match.
2672 (let ((match (widget-group-match-inline widget values)))
2673 (and match (null (cdr match))))))
2675 (defun widget-group-match-inline (widget vals)
2676 ;; Match if the components match.
2677 (let ((args (widget-get widget :args))
2678 argument answer found)
2680 (setq argument (car args)
2682 answer (widget-match-inline argument vals))
2684 (setq vals (cdr answer)
2685 found (append found (car answer)))
2689 (cons found vals))))
2691 ;;; The `visibility' Widget.
2693 (define-widget 'visibility 'item
2694 "An indicator and manipulator for hidden items."
2700 :value-create 'widget-visibility-value-create
2701 :action 'widget-toggle-action
2702 :match (lambda (widget value) t))
2704 (defun widget-visibility-value-create (widget)
2705 ;; Insert text representing the `on' and `off' states.
2706 (let ((on (widget-get widget :on))
2707 (off (widget-get widget :off)))
2709 (setq on (concat widget-push-button-prefix
2711 widget-push-button-suffix))
2714 (setq off (concat widget-push-button-prefix
2716 widget-push-button-suffix))
2718 (if (widget-value widget)
2719 (widget-image-insert widget on "down" "down-pushed")
2720 (widget-image-insert widget off "right" "right-pushed"))))
2722 ;;; The `documentation-link' Widget.
2724 ;; This is a helper widget for `documentation-string'.
2726 (define-widget 'documentation-link 'link
2727 "Link type used in documentation strings."
2729 :help-echo "Describe this symbol"
2730 :action 'widget-documentation-link-action)
2732 (defun widget-documentation-link-action (widget &optional event)
2733 "Display documentation for WIDGET's value. Ignore optional argument EVENT."
2734 (let* ((string (widget-get widget :value))
2735 (symbol (intern string)))
2736 (if (and (fboundp symbol) (boundp symbol))
2737 ;; If there are two doc strings, give the user a way to pick one.
2738 (apropos (concat "\\`" (regexp-quote string) "\\'"))
2739 (if (fboundp symbol)
2740 (describe-function symbol)
2741 (describe-variable symbol)))))
2743 (defcustom widget-documentation-links t
2744 "Add hyperlinks to documentation strings when non-nil."
2746 :group 'widget-documentation)
2748 (defcustom widget-documentation-link-regexp "`\\([^\n`' ]+\\)'"
2749 "Regexp for matching potential links in documentation strings.
2750 The first group should be the link itself."
2752 :group 'widget-documentation)
2754 (defcustom widget-documentation-link-p 'intern-soft
2755 "Predicate used to test if a string is useful as a link.
2756 The value should be a function. The function will be called one
2757 argument, a string, and should return non-nil if there should be a
2758 link for that string."
2760 :options '(widget-documentation-link-p)
2761 :group 'widget-documentation)
2763 (defcustom widget-documentation-link-type 'documentation-link
2764 "Widget type used for links in documentation strings."
2766 :group 'widget-documentation)
2768 (defun widget-documentation-link-add (widget from to)
2769 (widget-specify-doc widget from to)
2770 (when widget-documentation-links
2771 (let ((regexp widget-documentation-link-regexp)
2772 (buttons (widget-get widget :buttons))
2773 (widget-mouse-face (default-value 'widget-mouse-face))
2774 (widget-button-face widget-documentation-face)
2775 (widget-button-pressed-face widget-documentation-face))
2778 (while (re-search-forward regexp to t)
2779 (let ((name (match-string 1))
2780 (begin (match-beginning 1))
2781 (end (match-end 1)))
2782 (when (funcall widget-documentation-link-p name)
2783 (push (widget-convert-button widget-documentation-link-type
2784 begin end :value name)
2786 (widget-put widget :buttons buttons)))
2787 (let ((indent (widget-get widget :indent)))
2788 (when (and indent (not (zerop indent)))
2791 (narrow-to-region from to)
2792 (goto-char (point-min))
2793 (while (search-forward "\n" nil t)
2794 (insert-char ?\ indent)))))))
2796 ;;; The `documentation-string' Widget.
2798 (define-widget 'documentation-string 'item
2799 "A documentation string."
2801 :action 'widget-documentation-string-action
2802 :value-delete 'widget-children-value-delete
2803 :value-create 'widget-documentation-string-value-create)
2805 (defun widget-documentation-string-value-create (widget)
2806 ;; Insert documentation string.
2807 (let ((doc (widget-value widget))
2808 (indent (widget-get widget :indent))
2809 (shown (widget-get (widget-get widget :parent) :documentation-shown))
2811 (if (string-match "\n" doc)
2812 (let ((before (substring doc 0 (match-beginning 0)))
2813 (after (substring doc (match-beginning 0)))
2816 (widget-documentation-link-add widget start (point))
2818 (widget-create-child-and-convert
2820 :help-echo "Show or hide rest of the documentation."
2824 :action 'widget-parent-action
2827 (setq start (point))
2828 (when (and indent (not (zerop indent)))
2829 (insert-char ?\ indent))
2831 (widget-documentation-link-add widget start (point)))
2832 (widget-put widget :buttons (list button)))
2834 (widget-documentation-link-add widget start (point))))
2837 (defun widget-documentation-string-action (widget &rest ignore)
2838 ;; Toggle documentation.
2839 (let ((parent (widget-get widget :parent)))
2840 (widget-put parent :documentation-shown
2841 (not (widget-get parent :documentation-shown))))
2843 (widget-value-set widget (widget-value widget)))
2845 ;;; The Sexp Widgets.
2847 (define-widget 'const 'item
2848 "An immutable sexp."
2849 :prompt-value 'widget-const-prompt-value
2852 (defun widget-const-prompt-value (widget prompt value unbound)
2853 ;; Return the value of the const.
2854 (widget-value widget))
2856 (define-widget 'function-item 'const
2857 "An immutable function name."
2859 :documentation-property (lambda (symbol)
2861 (documentation symbol t)
2864 (define-widget 'variable-item 'const
2865 "An immutable variable name."
2867 :documentation-property 'variable-documentation)
2869 (define-widget 'other 'sexp
2870 "Matches any value, but doesn't let the user edit the value.
2871 This is useful as last item in a `choice' widget.
2872 You should use this widget type with a default value,
2873 as in (other DEFAULT) or (other :tag \"NAME\" DEFAULT).
2874 If the user selects this alternative, that specifies DEFAULT
2880 (defvar widget-string-prompt-value-history nil
2881 "History of input to `widget-string-prompt-value'.")
2883 (define-widget 'string 'editable-field
2886 :format "%{%t%}: %v"
2887 :complete-function 'ispell-complete-word
2888 :prompt-history 'widget-string-prompt-value-history)
2890 (define-widget 'regexp 'string
2891 "A regular expression."
2892 :match 'widget-regexp-match
2893 :validate 'widget-regexp-validate
2894 ;; Doesn't work well with terminating newline.
2895 ;; :value-face 'widget-single-line-field-face
2898 (defun widget-regexp-match (widget value)
2899 ;; Match valid regexps.
2900 (and (stringp value)
2903 (string-match value ""))
2906 (defun widget-regexp-validate (widget)
2907 "Check that the value of WIDGET is a valid regexp."
2908 (condition-case data
2910 (string-match (widget-value widget) ""))
2911 (error (widget-put widget :error (error-message-string data))
2914 (define-widget 'file 'string
2916 It will read a file name from the minibuffer when invoked."
2917 :complete-function 'widget-file-complete
2918 :prompt-value 'widget-file-prompt-value
2919 :format "%{%t%}: %v"
2920 ;; Doesn't work well with terminating newline.
2921 ;; :value-face 'widget-single-line-field-face
2924 (defun widget-file-complete ()
2925 "Perform completion on file name preceding point."
2927 (let* ((end (point))
2928 (beg (save-excursion
2929 (skip-chars-backward "^ ")
2931 (pattern (buffer-substring beg end))
2932 (name-part (file-name-nondirectory pattern))
2933 (directory (file-name-directory pattern))
2934 (completion (file-name-completion name-part directory)))
2935 (cond ((eq completion t))
2937 (message "Can't find completion for \"%s\"" pattern)
2939 ((not (string= name-part completion))
2940 (delete-region beg end)
2941 (insert (expand-file-name completion directory)))
2943 (message "Making completion list...")
2944 (with-output-to-temp-buffer "*Completions*"
2945 (display-completion-list
2946 (sort (file-name-all-completions name-part directory)
2948 (message "Making completion list...%s" "done")))))
2950 (defun widget-file-prompt-value (widget prompt value unbound)
2951 ;; Read file from minibuffer.
2952 (abbreviate-file-name
2954 (read-file-name prompt)
2955 (let ((prompt2 (format "%s (default %s) " prompt value))
2956 (dir (file-name-directory value))
2957 (file (file-name-nondirectory value))
2958 (must-match (widget-get widget :must-match)))
2959 (read-file-name prompt2 dir nil must-match file)))))
2961 ;;;(defun widget-file-action (widget &optional event)
2962 ;;; ;; Read a file name from the minibuffer.
2963 ;;; (let* ((value (widget-value widget))
2964 ;;; (dir (file-name-directory value))
2965 ;;; (file (file-name-nondirectory value))
2966 ;;; (menu-tag (widget-apply widget :menu-tag-get))
2967 ;;; (must-match (widget-get widget :must-match))
2968 ;;; (answer (read-file-name (concat menu-tag ": (default `" value "') ")
2969 ;;; dir nil must-match file)))
2970 ;;; (widget-value-set widget (abbreviate-file-name answer))
2972 ;;; (widget-apply widget :notify widget event)))
2974 ;; Fixme: use file-name-as-directory.
2975 (define-widget 'directory 'file
2976 "A directory widget.
2977 It will read a directory name from the minibuffer when invoked."
2980 (defvar widget-symbol-prompt-value-history nil
2981 "History of input to `widget-symbol-prompt-value'.")
2983 (define-widget 'symbol 'editable-field
2987 :format "%{%t%}: %v"
2988 :match (lambda (widget value) (symbolp value))
2989 :complete-function 'lisp-complete-symbol
2990 :prompt-internal 'widget-symbol-prompt-internal
2991 :prompt-match 'symbolp
2992 :prompt-history 'widget-symbol-prompt-value-history
2993 :value-to-internal (lambda (widget value)
2997 :value-to-external (lambda (widget value)
3002 (defun widget-symbol-prompt-internal (widget prompt initial history)
3003 ;; Read file from minibuffer.
3004 (let ((answer (completing-read prompt obarray
3005 (widget-get widget :prompt-match)
3006 nil initial history)))
3007 (if (and (stringp answer)
3008 (not (zerop (length answer))))
3010 (error "No value"))))
3012 (defvar widget-function-prompt-value-history nil
3013 "History of input to `widget-function-prompt-value'.")
3015 (define-widget 'function 'sexp
3017 :complete-function (lambda ()
3019 (lisp-complete-symbol 'fboundp))
3020 :prompt-value 'widget-field-prompt-value
3021 :prompt-internal 'widget-symbol-prompt-internal
3022 :prompt-match 'fboundp
3023 :prompt-history 'widget-function-prompt-value-history
3024 :action 'widget-field-action
3025 :match-alternatives '(functionp)
3026 :validate (lambda (widget)
3027 (unless (functionp (widget-value widget))
3028 (widget-put widget :error (format "Invalid function: %S"
3029 (widget-value widget)))
3034 (defvar widget-variable-prompt-value-history nil
3035 "History of input to `widget-variable-prompt-value'.")
3037 (define-widget 'variable 'symbol
3039 :prompt-match 'boundp
3040 :prompt-history 'widget-variable-prompt-value-history
3041 :complete-function (lambda ()
3043 (lisp-complete-symbol 'boundp))
3046 (defvar widget-coding-system-prompt-value-history nil
3047 "History of input to `widget-coding-system-prompt-value'.")
3049 (define-widget 'coding-system 'symbol
3050 "A MULE coding-system."
3051 :format "%{%t%}: %v"
3052 :tag "Coding system"
3054 :prompt-history 'widget-coding-system-prompt-value-history
3055 :prompt-value 'widget-coding-system-prompt-value
3056 :action 'widget-coding-system-action
3057 :complete-function (lambda ()
3059 (lisp-complete-symbol 'coding-system-p))
3060 :validate (lambda (widget)
3061 (unless (coding-system-p (widget-value widget))
3062 (widget-put widget :error (format "Invalid coding system: %S"
3063 (widget-value widget)))
3066 :prompt-match 'coding-system-p)
3068 (defun widget-coding-system-prompt-value (widget prompt value unbound)
3069 "Read coding-system from minibuffer."
3070 (if (widget-get widget :base-only)
3072 (completing-read (format "%s (default %s) " prompt value)
3073 (mapcar #'list (coding-system-list t)) nil nil nil
3074 coding-system-history))
3075 (read-coding-system (format "%s (default %s) " prompt value) value)))
3077 (defun widget-coding-system-action (widget &optional event)
3079 (widget-coding-system-prompt-value
3081 (widget-apply widget :menu-tag-get)
3082 (widget-value widget)
3084 (widget-value-set widget answer)
3085 (widget-apply widget :notify widget event)
3088 (define-widget 'sexp 'editable-field
3089 "An arbitrary Lisp expression."
3090 :tag "Lisp expression"
3091 :format "%{%t%}: %v"
3093 :validate 'widget-sexp-validate
3094 :match (lambda (widget value) t)
3095 :value-to-internal 'widget-sexp-value-to-internal
3096 :value-to-external (lambda (widget value) (read value))
3097 :prompt-history 'widget-sexp-prompt-value-history
3098 :prompt-value 'widget-sexp-prompt-value)
3100 (defun widget-sexp-value-to-internal (widget value)
3101 ;; Use pp for printer representation.
3102 (let ((pp (if (symbolp value)
3103 (prin1-to-string value)
3104 (pp-to-string value))))
3105 (while (string-match "\n\\'" pp)
3106 (setq pp (substring pp 0 -1)))
3107 (if (or (string-match "\n\\'" pp)
3112 (defun widget-sexp-validate (widget)
3113 ;; Valid if we can read the string and there is no junk left after it.
3115 (insert (widget-apply widget :value-get))
3116 (goto-char (point-min))
3118 (condition-case data
3120 ;; Avoid a confusing end-of-file error.
3121 (skip-syntax-forward "\\s-")
3123 (setq err "Empty sexp -- use `nil'?")
3124 (unless (widget-apply widget :match (read (current-buffer)))
3125 (setq err (widget-get widget :type-error))))
3126 (if (and (not (eobp))
3128 (setq err (format "Junk at end of expression: %s"
3129 (buffer-substring (point)
3131 (end-of-file ; Avoid confusing error message.
3132 (setq err "Unbalanced sexp"))
3133 (error (setq err (error-message-string data))))
3136 (widget-put widget :error err)
3139 (defvar widget-sexp-prompt-value-history nil
3140 "History of input to `widget-sexp-prompt-value'.")
3142 (defun widget-sexp-prompt-value (widget prompt value unbound)
3143 ;; Read an arbitrary sexp.
3144 (let ((found (read-string prompt
3145 (if unbound nil (cons (prin1-to-string value) 0))
3146 (widget-get widget :prompt-history))))
3147 (let ((answer (read-from-string found)))
3148 (unless (= (cdr answer) (length found))
3149 (error "Junk at end of expression: %s"
3150 (substring found (cdr answer))))
3153 (define-widget 'restricted-sexp 'sexp
3154 "A Lisp expression restricted to values that match.
3155 To use this type, you must define :match or :match-alternatives."
3156 :type-error "The specified value is not valid"
3157 :match 'widget-restricted-sexp-match
3158 :value-to-internal (lambda (widget value)
3159 (if (widget-apply widget :match value)
3160 (prin1-to-string value)
3163 (defun widget-restricted-sexp-match (widget value)
3164 (let ((alternatives (widget-get widget :match-alternatives))
3166 (while (and alternatives (not matched))
3167 (if (cond ((functionp (car alternatives))
3168 (funcall (car alternatives) value))
3169 ((and (consp (car alternatives))
3170 (eq (car (car alternatives)) 'quote))
3171 (eq value (nth 1 (car alternatives)))))
3173 (setq alternatives (cdr alternatives)))
3176 (define-widget 'integer 'restricted-sexp
3180 :type-error "This field should contain an integer"
3181 :match-alternatives '(integerp))
3183 (define-widget 'number 'restricted-sexp
3184 "A floating point number."
3187 :type-error "This field should contain a number"
3188 :match-alternatives '(numberp))
3190 (define-widget 'character 'editable-field
3195 :format "%{%t%}: %v\n"
3196 :valid-regexp "\\`.\\'"
3197 :error "This field should contain a single character"
3198 :value-to-internal (lambda (widget value)
3201 (char-to-string value)))
3202 :value-to-external (lambda (widget value)
3206 :match (lambda (widget value)
3207 (char-valid-p value)))
3209 (define-widget 'list 'group
3212 :format "%{%t%}:\n%v")
3214 (define-widget 'vector 'group
3217 :format "%{%t%}:\n%v"
3218 :match 'widget-vector-match
3219 :value-to-internal (lambda (widget value) (append value nil))
3220 :value-to-external (lambda (widget value) (apply 'vector value)))
3222 (defun widget-vector-match (widget value)
3223 (and (vectorp value)
3224 (widget-group-match widget
3225 (widget-apply widget :value-to-internal value))))
3227 (define-widget 'cons 'group
3230 :format "%{%t%}:\n%v"
3231 :match 'widget-cons-match
3232 :value-to-internal (lambda (widget value)
3233 (list (car value) (cdr value)))
3234 :value-to-external (lambda (widget value)
3235 (cons (nth 0 value) (nth 1 value))))
3237 (defun widget-cons-match (widget value)
3239 (widget-group-match widget
3240 (widget-apply widget :value-to-internal value))))
3242 ;;; The `plist' Widget.
3246 (define-widget 'plist 'list
3248 :key-type '(symbol :tag "Key")
3249 :value-type '(sexp :tag "Value")
3250 :convert-widget 'widget-plist-convert-widget
3253 (defvar widget-plist-value-type) ;Dynamic variable
3255 (defun widget-plist-convert-widget (widget)
3256 ;; Handle `:options'.
3257 (let* ((options (widget-get widget :options))
3258 (widget-plist-value-type (widget-get widget :value-type))
3259 (other `(editable-list :inline t
3261 ,(widget-get widget :key-type)
3262 ,widget-plist-value-type)))
3264 (list `(checklist :inline t
3266 ,@(mapcar 'widget-plist-convert-option
3270 (widget-put widget :args args)
3273 (defun widget-plist-convert-option (option)
3274 ;; Convert a single plist option.
3275 (let (key-type value-type)
3277 (let ((key (nth 0 option)))
3278 (setq value-type (nth 1 option))
3281 (setq key-type `(const ,key))))
3282 (setq key-type `(const ,option)
3283 value-type widget-plist-value-type))
3284 `(group :format "Key: %v" :inline t ,key-type ,value-type)))
3287 ;;; The `alist' Widget.
3289 ;; Association lists.
3291 (define-widget 'alist 'list
3292 "An association list."
3293 :key-type '(sexp :tag "Key")
3294 :value-type '(sexp :tag "Value")
3295 :convert-widget 'widget-alist-convert-widget
3298 (defvar widget-alist-value-type) ;Dynamic variable
3300 (defun widget-alist-convert-widget (widget)
3301 ;; Handle `:options'.
3302 (let* ((options (widget-get widget :options))
3303 (widget-alist-value-type (widget-get widget :value-type))
3304 (other `(editable-list :inline t
3306 ,(widget-get widget :key-type)
3307 ,widget-alist-value-type)))
3309 (list `(checklist :inline t
3311 ,@(mapcar 'widget-alist-convert-option
3315 (widget-put widget :args args)
3318 (defun widget-alist-convert-option (option)
3319 ;; Convert a single alist option.
3320 (let (key-type value-type)
3322 (let ((key (nth 0 option)))
3323 (setq value-type (nth 1 option))
3326 (setq key-type `(const ,key))))
3327 (setq key-type `(const ,option)
3328 value-type widget-alist-value-type))
3329 `(cons :format "Key: %v" ,key-type ,value-type)))
3331 (define-widget 'choice 'menu-choice
3332 "A union of several sexp types."
3334 :format "%{%t%}: %[Value Menu%] %v"
3335 :button-prefix 'widget-push-button-prefix
3336 :button-suffix 'widget-push-button-suffix
3337 :prompt-value 'widget-choice-prompt-value)
3339 (defun widget-choice-prompt-value (widget prompt value unbound)
3341 (let ((args (widget-get widget :args))
3342 (completion-ignore-case (widget-get widget :case-fold))
3343 current choices old)
3344 ;; Find the first arg that matches VALUE.
3347 (if (widget-apply (car look) :match value)
3348 (setq old (car look)
3350 (setq look (cdr look)))))
3353 (cond ((= (length args) 0)
3355 ((= (length args) 1)
3357 ((and (= (length args) 2)
3359 (if (eq old (nth 0 args))
3364 (setq current (car args)
3367 (cons (cons (widget-apply current :menu-tag-get)
3370 (let ((val (completing-read prompt choices nil t)))
3372 (let ((try (try-completion val choices)))
3375 (cdr (assoc val choices)))
3378 (widget-prompt-value current prompt nil t)
3381 (define-widget 'radio 'radio-button-choice
3382 "A union of several sexp types."
3384 :format "%{%t%}:\n%v"
3385 :prompt-value 'widget-choice-prompt-value)
3387 (define-widget 'repeat 'editable-list
3388 "A variable length homogeneous list."
3390 :format "%{%t%}:\n%v%i\n")
3392 (define-widget 'set 'checklist
3393 "A list of members from a fixed set."
3395 :format "%{%t%}:\n%v")
3397 (define-widget 'boolean 'toggle
3398 "To be nil or non-nil, that is the question."
3400 :prompt-value 'widget-boolean-prompt-value
3401 :button-prefix 'widget-push-button-prefix
3402 :button-suffix 'widget-push-button-suffix
3403 :format "%{%t%}: %[Toggle%] %v\n"
3407 (defun widget-boolean-prompt-value (widget prompt value unbound)
3408 ;; Toggle a boolean.
3411 ;;; The `color' Widget.
3414 (define-widget 'color 'editable-field
3415 "Choose a color name (with sample)."
3416 :format "%t: %v (%{sample%})\n"
3420 :complete 'widget-color-complete
3421 :sample-face-get 'widget-color-sample-face-get
3422 :notify 'widget-color-notify
3423 :action 'widget-color-action)
3425 (defun widget-color-complete (widget)
3426 "Complete the color in WIDGET."
3427 (require 'facemenu) ; for facemenu-color-alist
3428 (let* ((prefix (buffer-substring-no-properties (widget-field-start widget)
3430 (list (or facemenu-color-alist (defined-colors)))
3431 (completion (try-completion prefix list)))
3432 (cond ((eq completion t)
3433 (message "Exact match."))
3435 (error "Can't find completion for \"%s\"" prefix))
3436 ((not (string-equal prefix completion))
3437 (insert-and-inherit (substring completion (length prefix))))
3439 (message "Making completion list...")
3440 (with-output-to-temp-buffer "*Completions*"
3441 (display-completion-list (all-completions prefix list nil)))
3442 (message "Making completion list...done")))))
3444 (defun widget-color-sample-face-get (widget)
3445 (let* ((value (condition-case nil
3446 (widget-value widget)
3447 (error (widget-get widget :value)))))
3448 (if (color-defined-p value)
3449 (list (cons 'foreground-color value))
3452 (defun widget-color-action (widget &optional event)
3453 "Prompt for a color."
3454 (let* ((tag (widget-apply widget :menu-tag-get))
3455 (prompt (concat tag ": "))
3456 (value (widget-value widget))
3457 (start (widget-field-start widget))
3458 (pos (cond ((< (point) start)
3460 ((> (point) (+ start (length value)))
3463 (- (point) start))))
3464 (answer (facemenu-read-color prompt)))
3465 (unless (zerop (length answer))
3466 (widget-value-set widget answer)
3468 (widget-apply widget :notify widget event))))
3470 (defun widget-color-notify (widget child &optional event)
3471 "Update the sample, and notofy the parent."
3472 (overlay-put (widget-get widget :sample-overlay)
3473 'face (widget-apply widget :sample-face-get))
3474 (widget-default-notify widget child event))
3478 (defun widget-echo-help (pos)
3479 "Display help-echo text for widget at POS."
3480 (let* ((widget (widget-at pos))
3481 (help-echo (and widget (widget-get widget :help-echo))))
3482 (if (functionp help-echo)
3483 (setq help-echo (funcall help-echo widget)))
3484 (if (stringp help-echo)
3485 (message "%s" help-echo))))
3491 ;;; wid-edit.el ends here