1 ;;; wid-edit.el --- Functions for creating and using widgets -*-byte-compile-dynamic: t;-*-
3 ;; Copyright (C) 1996, 1997, 1999, 2000, 2001, 2002, 2003,
4 ;; 2004, 2005 Free Software Foundation, Inc.
6 ;; Author: Per Abrahamsen <abraham@dina.kvl.dk>
8 ;; Keywords: extensions
10 ;; This file is part of GNU Emacs.
12 ;; GNU Emacs is free software; you can redistribute it and/or modify
13 ;; it under the terms of the GNU General Public License as published by
14 ;; the Free Software Foundation; either version 2, or (at your option)
17 ;; GNU Emacs is distributed in the hope that it will be useful,
18 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
19 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 ;; GNU General Public License for more details.
22 ;; You should have received a copy of the GNU General Public License
23 ;; along with GNU Emacs; see the file COPYING. If not, write to the
24 ;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
25 ;; Boston, MA 02110-1301, USA.
27 ;;; Wishlist items (from widget.texi):
29 ;; * The `menu-choice' tag should be prettier, something like the
30 ;; abbreviated menus in Open Look.
32 ;; * Finish `:tab-order'.
34 ;; * Make indentation work with glyphs and proportional fonts.
36 ;; * Add commands to show overview of object and class hierarchies to
39 ;; * Find a way to disable mouse highlight for inactive widgets.
41 ;; * Find a way to make glyphs look inactive.
43 ;; * Add `key-binding' widget.
45 ;; * Add `widget' widget for editing widget specifications.
47 ;; * Find clean way to implement variable length list. See
48 ;; `TeX-printer-list' for an explanation.
50 ;; * `C-h' in `widget-prompt-value' should give type specific help.
52 ;; * A mailto widget. [This should work OK as a url-link if with
53 ;; browse-url-browser-function' set up appropriately.]
65 (defun widget-event-point (event)
66 "Character position of the end of event if that exists, or nil."
67 (posn-point (event-end event
)))
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
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
'((((class color
)
101 (:foreground
"lime green"))
104 (:foreground
"dark green"))
106 "Face used for documentation text."
107 :group
'widget-documentation
108 :group
'widget-faces
)
109 ;; backward compatibility alias
110 (put 'widget-documentation-face
'face-alias
'widget-documentation
)
112 (defvar widget-button-face
'widget-button
113 "Face used for buttons in widgets.
114 This exists as a variable so it can be set locally in certain buffers.")
116 (defface widget-button
'((t (:weight bold
)))
117 "Face used for widget buttons."
118 :group
'widget-faces
)
119 ;; backward compatibility alias
120 (put 'widget-button-face
'face-alias
'widget-button
)
122 (defcustom widget-mouse-face
'highlight
123 "Face used for widget buttons when the mouse is above them."
125 :group
'widget-faces
)
127 ;; TTY gets special definitions here and in the next defface, because
128 ;; the gray colors defined for other displays cause black text on a black
129 ;; background, at least on light-background TTYs.
130 (defface widget-field
'((((type tty
))
131 :background
"yellow3"
133 (((class grayscale color
)
135 :background
"gray85")
136 (((class grayscale color
)
138 :background
"dim gray")
141 "Face used for editable fields."
142 :group
'widget-faces
)
143 ;; backward-compatibility alias
144 (put 'widget-field-face
'face-alias
'widget-field
)
146 (defface widget-single-line-field
'((((type tty
))
149 (((class grayscale color
)
151 :background
"gray85")
152 (((class grayscale color
)
154 :background
"dim gray")
157 "Face used for editable fields spanning only a single line."
158 :group
'widget-faces
)
159 ;; backward-compatibility alias
160 (put 'widget-single-line-field-face
'face-alias
'widget-single-line-field
)
162 ;;; This causes display-table to be loaded, and not usefully.
163 ;;;(defvar widget-single-line-display-table
164 ;;; (let ((table (make-display-table)))
165 ;;; (aset table 9 "^I")
166 ;;; (aset table 10 "^J")
168 ;;; "Display table used for single-line editable fields.")
170 ;;;(when (fboundp 'set-face-display-table)
171 ;;; (set-face-display-table 'widget-single-line-field-face
172 ;;; widget-single-line-display-table))
174 ;;; Utility functions.
176 ;; These are not really widget specific.
178 (defun widget-princ-to-string (object)
179 "Return string representation of OBJECT, any Lisp object.
180 No quoting characters are used; no delimiters are printed around
181 the contents of strings."
182 (with-output-to-string
185 (defun widget-clear-undo ()
186 "Clear all undo information."
187 (buffer-disable-undo (current-buffer))
188 (buffer-enable-undo))
190 (defcustom widget-menu-max-size
40
191 "Largest number of items allowed in a popup-menu.
192 Larger menus are read through the minibuffer."
196 (defcustom widget-menu-max-shortcuts
40
197 "Largest number of items for which it works to choose one with a character.
198 For a larger number of items, the minibuffer is used."
202 (defcustom widget-menu-minibuffer-flag nil
203 "*Control how to ask for a choice from the keyboard.
204 Non-nil means use the minibuffer;
205 nil means read a single character."
209 (defun widget-choose (title items
&optional event
)
210 "Choose an item from a list.
212 First argument TITLE is the name of the list.
213 Second argument ITEMS is a list whose members are either
214 (NAME . VALUE), to indicate selectable items, or just strings to
215 indicate unselectable items.
216 Optional third argument EVENT is an input event.
218 The user is asked to choose between each NAME from the items alist,
219 and the VALUE of the chosen element will be returned. If EVENT is a
220 mouse event, and the number of elements in items is less than
221 `widget-menu-max-size', a popup menu will be used, otherwise the
223 (cond ((and (< (length items
) widget-menu-max-size
)
224 event
(display-popup-menus-p))
227 (list title
(cons "" items
))))
228 ((or widget-menu-minibuffer-flag
229 (> (length items
) widget-menu-max-shortcuts
))
230 ;; Read the choice of name from the minibuffer.
231 (setq items
(widget-remove-if 'stringp items
))
232 (let ((val (completing-read (concat title
": ") items nil t
)))
234 (let ((try (try-completion val items
)))
237 (cdr (assoc val items
))))))
239 ;; Construct a menu of the choices
240 ;; and then use it for prompting for a single character.
241 (let* ((overriding-terminal-local-map (make-sparse-keymap))
243 map choice some-choice-enabled value
)
244 ;; Define SPC as a prefix char to get to this menu.
245 (define-key overriding-terminal-local-map
" "
246 (setq map
(make-sparse-keymap title
)))
247 (with-current-buffer (get-buffer-create " widget-choose")
249 (insert "Available choices:\n\n")
251 (setq choice
(car items
) items
(cdr items
))
253 (let* ((name (car choice
))
254 (function (cdr choice
)))
255 (insert (format "%c = %s\n" next-digit name
))
256 (define-key map
(vector next-digit
) function
)
257 (setq some-choice-enabled t
)))
258 ;; Allocate digits to disabled alternatives
259 ;; so that the digit of a given alternative never varies.
260 (setq next-digit
(1+ next-digit
)))
261 (insert "\nC-g = Quit"))
262 (or some-choice-enabled
263 (error "None of the choices is currently meaningful"))
264 (define-key map
[?\C-g
] 'keyboard-quit
)
265 (define-key map
[t] 'keyboard-quit)
266 (define-key map [?\M-\C-v] 'scroll-other-window)
267 (define-key map [?\M--] 'negative-argument)
268 (setcdr map (nreverse (cdr map)))
269 ;; Read a char with the menu, and return the result
270 ;; that corresponds to it.
271 (save-window-excursion
272 (let ((buf (get-buffer " widget-choose")))
273 (fit-window-to-buffer (display-buffer buf))
274 (let ((cursor-in-echo-area t)
278 (while (not (or (and (>= char ?0) (< char next-digit))
279 (eq value 'keyboard-quit)))
280 ;; Unread a SPC to lead to our new menu.
281 (setq unread-command-events (cons ?\s unread-command-events))
282 (setq keys (read-key-sequence title))
284 (lookup-key overriding-terminal-local-map keys t)
285 char (string-to-char (substring keys 1)))
286 (cond ((eq value 'scroll-other-window)
287 (let ((minibuffer-scroll-window
288 (get-buffer-window buf)))
290 (scroll-other-window-down
291 (window-height minibuffer-scroll-window))
292 (scroll-other-window))
294 ((eq value 'negative-argument)
298 (when (eq value 'keyboard-quit)
302 (defun widget-remove-if (predictate list)
303 (let (result (tail list))
305 (or (funcall predictate (car tail))
306 (setq result (cons (car tail) result)))
307 (setq tail (cdr tail)))
310 ;;; Widget text specifications.
312 ;; These functions are for specifying text properties.
314 ;; We can set it to nil now that get_local_map uses get_pos_property.
315 (defconst widget-field-add-space nil
316 "Non-nil means add extra space at the end of editable text fields.
317 If you don't add the space, it will become impossible to edit a zero
320 (defvar widget-field-use-before-change t
321 "Non-nil means use `before-change-functions' to track editable fields.
322 This enables the use of undo, but doesn't work on Emacs 19.34 and earlier.
323 Using before hooks also means that the :notify function can't know the
326 (defun widget-specify-field (widget from to)
327 "Specify editable button for WIDGET between FROM and TO."
328 ;; Terminating space is not part of the field, but necessary in
329 ;; order for local-map to work. Remove next sexp if local-map works
330 ;; at the end of the overlay.
333 (cond ((null (widget-get widget :size))
335 (widget-field-add-space
336 (insert-and-inherit " ")))
338 (let ((keymap (widget-get widget :keymap))
339 (face (or (widget-get widget :value-face) 'widget-field))
340 (help-echo (widget-get widget :help-echo))
341 (follow-link (widget-get widget :follow-link))
343 (or (not widget-field-add-space) (widget-get widget :size))))
344 (if (functionp help-echo)
345 (setq help-echo 'widget-mouse-help))
346 (when (= (char-before to) ?\n)
347 ;; When the last character in the field is a newline, we want to
348 ;; give it a `field' char-property of `boundary', which helps the
349 ;; C-n/C-p act more naturally when entering/leaving the field. We
350 ;; do this by making a small secondary overlay to contain just that
352 (let ((overlay (make-overlay (1- to) to nil t nil)))
353 (overlay-put overlay 'field 'boundary)
354 ;; We need the real field for tabbing.
355 (overlay-put overlay 'real-field widget)
356 ;; Use `local-map' here, not `keymap', so that normal editing
357 ;; works in the field when, say, Custom uses `suppress-keymap'.
358 (overlay-put overlay 'local-map keymap)
359 (overlay-put overlay 'face face)
360 (overlay-put overlay 'follow-link follow-link)
361 (overlay-put overlay 'help-echo help-echo))
363 (setq rear-sticky t))
364 (let ((overlay (make-overlay from to nil nil rear-sticky)))
365 (widget-put widget :field-overlay overlay)
366 ;;(overlay-put overlay 'detachable nil)
367 (overlay-put overlay 'field widget)
368 (overlay-put overlay 'local-map keymap)
369 (overlay-put overlay 'face face)
370 (overlay-put overlay 'follow-link follow-link)
371 (overlay-put overlay 'help-echo help-echo)))
372 (widget-specify-secret widget))
374 (defun widget-specify-secret (field)
375 "Replace text in FIELD with value of `:secret', if non-nil."
376 (let ((secret (widget-get field :secret))
377 (size (widget-get field :size)))
379 (let ((begin (widget-field-start field))
380 (end (widget-field-end field)))
382 (while (and (> end begin)
383 (eq (char-after (1- end)) ?\s))
384 (setq end (1- end))))
386 (let ((old (char-after begin)))
387 (unless (eq old secret)
388 (subst-char-in-region begin (1+ begin) old secret)
389 (put-text-property begin (1+ begin) 'secret old))
390 (setq begin (1+ begin))))))))
392 (defun widget-specify-button (widget from to)
393 "Specify button for WIDGET between FROM and TO."
394 (let ((overlay (make-overlay from to nil t nil))
395 (follow-link (widget-get widget :follow-link))
396 (help-echo (widget-get widget :help-echo)))
397 (widget-put widget :button-overlay overlay)
398 (if (functionp help-echo)
399 (setq help-echo 'widget-mouse-help))
400 (overlay-put overlay 'button widget)
401 (overlay-put overlay 'keymap (widget-get widget :keymap))
402 (overlay-put overlay 'evaporate t)
403 ;; We want to avoid the face with image buttons.
404 (unless (widget-get widget :suppress-face)
405 (overlay-put overlay 'face (widget-apply widget :button-face-get))
406 (overlay-put overlay 'mouse-face widget-mouse-face))
407 (overlay-put overlay 'pointer 'hand)
408 (overlay-put overlay 'follow-link follow-link)
409 (overlay-put overlay 'help-echo help-echo)))
411 (defun widget-mouse-help (window overlay point)
412 "Help-echo callback for widgets whose :help-echo is a function."
413 (with-current-buffer (overlay-buffer overlay)
414 (let* ((widget (widget-at (overlay-start overlay)))
415 (help-echo (if widget (widget-get widget :help-echo))))
416 (if (functionp help-echo)
417 (funcall help-echo widget)
420 (defun widget-specify-sample (widget from to)
421 "Specify sample for WIDGET between FROM and TO."
422 (let ((overlay (make-overlay from to nil t nil)))
423 (overlay-put overlay 'face (widget-apply widget :sample-face-get))
424 (overlay-put overlay 'evaporate t)
425 (widget-put widget :sample-overlay overlay)))
427 (defun widget-specify-doc (widget from to)
428 "Specify documentation for WIDGET between FROM and TO."
429 (let ((overlay (make-overlay from to nil t nil)))
430 (overlay-put overlay 'widget-doc widget)
431 (overlay-put overlay 'face widget-documentation-face)
432 (overlay-put overlay 'evaporate t)
433 (widget-put widget :doc-overlay overlay)))
435 (defmacro widget-specify-insert (&rest form)
436 "Execute FORM without inheriting any text properties."
438 (let ((inhibit-read-only t)
439 (inhibit-modification-hooks t))
440 (narrow-to-region (point) (point))
441 (prog1 (progn ,@form)
442 (goto-char (point-max))))))
444 (defface widget-inactive
445 '((t :inherit shadow))
446 "Face used for inactive widgets."
447 :group 'widget-faces)
448 ;; backward-compatibility alias
449 (put 'widget-inactive-face 'face-alias 'widget-inactive)
451 (defun widget-specify-inactive (widget from to)
452 "Make WIDGET inactive for user modifications."
453 (unless (widget-get widget :inactive)
454 (let ((overlay (make-overlay from to nil t nil)))
455 (overlay-put overlay 'face 'widget-inactive)
456 ;; This is disabled, as it makes the mouse cursor change shape.
457 ;; (overlay-put overlay 'mouse-face 'widget-inactive)
458 (overlay-put overlay 'evaporate t)
459 (overlay-put overlay 'priority 100)
460 (overlay-put overlay 'modification-hooks '(widget-overlay-inactive))
461 (widget-put widget :inactive overlay))))
463 (defun widget-overlay-inactive (&rest junk)
464 "Ignoring the arguments, signal an error."
465 (unless inhibit-read-only
466 (error "The widget here is not active")))
469 (defun widget-specify-active (widget)
470 "Make WIDGET active for user modifications."
471 (let ((inactive (widget-get widget :inactive)))
473 (delete-overlay inactive)
474 (widget-put widget :inactive nil))))
476 ;;; Widget Properties.
478 (defsubst widget-type (widget)
479 "Return the type of WIDGET, a symbol."
483 (defun widgetp (widget)
484 "Return non-nil iff WIDGET is a widget."
486 (get widget 'widget-type)
488 (symbolp (car widget))
489 (get (car widget) 'widget-type))))
491 (defun widget-get-indirect (widget property)
492 "In WIDGET, get the value of PROPERTY.
493 If the value is a symbol, return its binding.
494 Otherwise, just return the value."
495 (let ((value (widget-get widget property)))
500 (defun widget-member (widget property)
501 "Non-nil iff there is a definition in WIDGET for PROPERTY."
502 (cond ((plist-member (cdr widget) property)
505 (widget-member (get (car widget) 'widget-type) property))
508 (defun widget-value (widget)
509 "Extract the current value of WIDGET."
511 :value-to-external (widget-apply widget :value-get)))
513 (defun widget-value-set (widget value)
514 "Set the current value of WIDGET to VALUE."
516 :value-set (widget-apply widget
517 :value-to-internal value)))
519 (defun widget-default-get (widget)
520 "Extract the default external value of WIDGET."
521 (widget-apply widget :value-to-external
522 (or (widget-get widget :value)
523 (widget-apply widget :default-get))))
525 (defun widget-match-inline (widget vals)
526 "In WIDGET, match the start of VALS."
527 (cond ((widget-get widget :inline)
528 (widget-apply widget :match-inline vals))
530 (widget-apply widget :match (car vals)))
531 (cons (list (car vals)) (cdr vals)))
534 (defun widget-apply-action (widget &optional event)
535 "Apply :action in WIDGET in response to EVENT."
536 (if (widget-apply widget :active)
537 (widget-apply widget :action event)
538 (error "Attempt to perform action on inactive widget")))
540 ;;; Helper functions.
542 ;; These are widget specific.
545 (defun widget-prompt-value (widget prompt &optional value unbound)
546 "Prompt for a value matching WIDGET, using PROMPT.
547 The current value is assumed to be VALUE, unless UNBOUND is non-nil."
548 (unless (listp widget)
549 (setq widget (list widget)))
550 (setq prompt (format "[%s] %s" (widget-type widget) prompt))
551 (setq widget (widget-convert widget))
552 (let ((answer (widget-apply widget :prompt-value prompt value unbound)))
553 (unless (widget-apply widget :match answer)
554 (error "Value does not match %S type" (car widget)))
557 (defun widget-get-sibling (widget)
558 "Get the item WIDGET is assumed to toggle.
559 This is only meaningful for radio buttons or checkboxes in a list."
560 (let* ((children (widget-get (widget-get widget :parent) :children))
564 (setq child (car children)
565 children (cdr children))
566 (when (eq (widget-get child :button) widget)
567 (throw 'child child)))
570 (defun widget-map-buttons (function &optional buffer maparg)
571 "Map FUNCTION over the buttons in BUFFER.
572 FUNCTION is called with the arguments WIDGET and MAPARG.
574 If FUNCTION returns non-nil, the walk is cancelled.
576 The arguments MAPARG, and BUFFER default to nil and (current-buffer),
578 (let ((cur (point-min))
581 (with-current-buffer buffer (overlay-lists))
583 (setq overlays (append (car overlays) (cdr overlays)))
584 (while (setq cur (pop overlays))
585 (setq widget (overlay-get cur 'button))
586 (if (and widget (funcall function widget maparg))
587 (setq overlays nil)))))
591 (defcustom widget-image-directory (file-name-as-directory
592 (expand-file-name "custom" data-directory))
593 "Where widget button images are located.
594 If this variable is nil, widget will try to locate the directory
599 (defcustom widget-image-enable t
600 "If non nil, use image buttons in widgets when available."
605 (defcustom widget-image-conversion
606 '((xpm ".xpm") (gif ".gif") (png ".png") (jpeg ".jpg" ".jpeg")
608 "Conversion alist from image formats to file name suffixes."
610 :type '(repeat (cons :format "%v"
611 (symbol :tag "Image Format" unknown)
612 (repeat :tag "Suffixes"
613 (string :format "%v")))))
615 (defun widget-image-find (image)
616 "Create a graphical button from IMAGE.
617 IMAGE should either already be an image, or be a file name sans
618 extension (xpm, xbm, gif, jpg, or png) located in
619 `widget-image-directory' or otherwise where `find-image' will find it."
620 (cond ((not (and image widget-image-enable (display-graphic-p)))
621 ;; We don't want or can't use images.
624 (eq 'image (car image)))
625 ;; Already an image spec. Use it.
628 ;; A string. Look it up in relevant directories.
629 (let* ((load-path (cons widget-image-directory load-path))
631 (dolist (elt widget-image-conversion)
632 (dolist (ext (cdr elt))
633 (push (list :type (car elt) :file (concat image ext)) specs)))
634 (setq specs (nreverse specs))
640 (defvar widget-button-pressed-face 'widget-button-pressed
641 "Face used for pressed buttons in widgets.
642 This exists as a variable so it can be set locally in certain
645 (defun widget-image-insert (widget tag image &optional down inactive)
646 "In WIDGET, insert the text TAG or, if supported, IMAGE.
647 IMAGE should either be an image or an image file name sans extension
648 \(xpm, xbm, gif, jpg, or png) located in `widget-image-directory'.
650 Optional arguments DOWN and INACTIVE are used instead of IMAGE when the
651 button is pressed or inactive, respectively. These are currently ignored."
652 (if (and (display-graphic-p)
653 (setq image (widget-image-find image)))
654 (progn (widget-put widget :suppress-face t)
657 tag 'mouse-face widget-button-pressed-face)))
660 (defun widget-move-and-invoke (event)
661 "Move to where you click, and if it is an active field, invoke it."
663 (mouse-set-point event)
664 (let ((pos (widget-event-point event)))
665 (if (and pos (get-char-property pos 'button))
666 (widget-button-click event))))
670 (defgroup widget-button nil
671 "The look of various kinds of buttons."
674 (defcustom widget-button-prefix ""
675 "String used as prefix for buttons."
677 :group 'widget-button)
679 (defcustom widget-button-suffix ""
680 "String used as suffix for buttons."
682 :group 'widget-button)
684 ;;; Creating Widgets.
687 (defun widget-create (type &rest args)
688 "Create widget of TYPE.
689 The optional ARGS are additional keyword arguments."
690 (let ((widget (apply 'widget-convert type args)))
691 (widget-apply widget :create)
694 (defun widget-create-child-and-convert (parent type &rest args)
695 "As part of the widget PARENT, create a child widget TYPE.
696 The child is converted, using the keyword arguments ARGS."
697 (let ((widget (apply 'widget-convert type args)))
698 (widget-put widget :parent parent)
699 (unless (widget-get widget :indent)
700 (widget-put widget :indent (+ (or (widget-get parent :indent) 0)
701 (or (widget-get widget :extra-offset) 0)
702 (widget-get parent :offset))))
703 (widget-apply widget :create)
706 (defun widget-create-child (parent type)
707 "Create widget of TYPE."
708 (let ((widget (widget-copy type)))
709 (widget-put widget :parent parent)
710 (unless (widget-get widget :indent)
711 (widget-put widget :indent (+ (or (widget-get parent :indent) 0)
712 (or (widget-get widget :extra-offset) 0)
713 (widget-get parent :offset))))
714 (widget-apply widget :create)
717 (defun widget-create-child-value (parent type value)
718 "Create widget of TYPE with value VALUE."
719 (let ((widget (widget-copy type)))
720 (widget-put widget :value (widget-apply widget :value-to-internal value))
721 (widget-put widget :parent parent)
722 (unless (widget-get widget :indent)
723 (widget-put widget :indent (+ (or (widget-get parent :indent) 0)
724 (or (widget-get widget :extra-offset) 0)
725 (widget-get parent :offset))))
726 (widget-apply widget :create)
730 (defun widget-delete (widget)
732 (widget-apply widget :delete))
734 (defun widget-copy (widget)
735 "Make a deep copy of WIDGET."
736 (widget-apply (copy-sequence widget) :copy))
738 (defun widget-convert (type &rest args)
739 "Convert TYPE to a widget without inserting it in the buffer.
740 The optional ARGS are additional keyword arguments."
741 ;; Don't touch the type.
742 (let* ((widget (if (symbolp type)
744 (copy-sequence type)))
748 ;; First set the :args keyword.
749 (while (cdr current) ;Look in the type.
750 (if (and (keywordp (cadr current))
751 ;; If the last element is a keyword,
752 ;; it is still the :args element,
753 ;; even though it is a keyword.
755 (if (eq (cadr current) :args)
756 ;; If :args is explicitly specified, obey it.
758 ;; Some other irrelevant keyword.
759 (setq current (cdr (cdr current))))
760 (setcdr current (list :args (cdr current)))
762 (while (and args (not done)) ;Look in ARGS.
763 (cond ((eq (car args) :args)
764 ;; Handle explicit specification of :args.
765 (setq args (cadr args)
767 ((keywordp (car args))
768 (setq args (cddr args)))
771 (widget-put widget :args args))
772 ;; Then Convert the widget.
775 (let ((convert-widget (plist-get (cdr type) :convert-widget)))
777 (setq widget (funcall convert-widget widget))))
778 (setq type (get (car type) 'widget-type)))
779 ;; Finally set the keyword args.
781 (let ((next (nth 0 keys)))
784 (widget-put widget next (nth 1 keys))
785 (setq keys (nthcdr 2 keys)))
787 ;; Convert the :value to internal format.
788 (if (widget-member widget :value)
790 :value (widget-apply widget
792 (widget-get widget :value))))
793 ;; Return the newly create widget.
797 (defun widget-insert (&rest args)
798 "Call `insert' with ARGS even if surrounding text is read only."
799 (let ((inhibit-read-only t)
800 (inhibit-modification-hooks t))
801 (apply 'insert args)))
803 (defun widget-convert-text (type from to
804 &optional button-from button-to
806 "Return a widget of type TYPE with endpoint FROM TO.
807 No text will be inserted to the buffer, instead the text between FROM
808 and TO will be used as the widgets end points. If optional arguments
809 BUTTON-FROM and BUTTON-TO are given, these will be used as the widgets
811 Optional ARGS are extra keyword arguments for TYPE."
812 (let ((widget (apply 'widget-convert type :delete 'widget-leave-text args))
813 (from (copy-marker from))
814 (to (copy-marker to)))
815 (set-marker-insertion-type from t)
816 (set-marker-insertion-type to nil)
817 (widget-put widget :from from)
818 (widget-put widget :to to)
820 (widget-specify-button widget button-from button-to))
823 (defun widget-convert-button (type from to &rest args)
824 "Return a widget of type TYPE with endpoint FROM TO.
825 Optional ARGS are extra keyword arguments for TYPE.
826 No text will be inserted to the buffer, instead the text between FROM
827 and TO will be used as the widgets end points, as well as the widgets
829 (apply 'widget-convert-text type from to from to args))
831 (defun widget-leave-text (widget)
832 "Remove markers and overlays from WIDGET and its children."
833 (let ((button (widget-get widget :button-overlay))
834 (sample (widget-get widget :sample-overlay))
835 (doc (widget-get widget :doc-overlay))
836 (field (widget-get widget :field-overlay)))
837 (set-marker (widget-get widget :from) nil)
838 (set-marker (widget-get widget :to) nil)
840 (delete-overlay button))
842 (delete-overlay sample))
844 (delete-overlay doc))
846 (delete-overlay field))
847 (mapc 'widget-leave-text (widget-get widget :children))))
849 ;;; Keymap and Commands.
852 (defvar widget-keymap
853 (let ((map (make-sparse-keymap)))
854 (define-key map "\t" 'widget-forward)
855 (define-key map "\e\t" 'widget-backward)
856 (define-key map [(shift tab)] 'widget-backward)
857 (define-key map [backtab] 'widget-backward)
858 (define-key map [down-mouse-2] 'widget-button-click)
859 (define-key map "\C-m" 'widget-button-press)
861 "Keymap containing useful binding for buffers containing widgets.
862 Recommended as a parent keymap for modes using widgets.")
864 (defvar widget-global-map global-map
865 "Keymap used for events a widget does not handle itself.")
866 (make-variable-buffer-local 'widget-global-map)
868 (defvar widget-field-keymap
869 (let ((map (copy-keymap widget-keymap)))
870 (define-key map "\C-k" 'widget-kill-line)
871 (define-key map "\M-\t" 'widget-complete)
872 (define-key map "\C-m" 'widget-field-activate)
873 ;; Since the widget code uses a `field' property to identify fields,
874 ;; ordinary beginning-of-line does the right thing.
875 ;; (define-key map "\C-a" 'widget-beginning-of-line)
876 (define-key map "\C-e" 'widget-end-of-line)
878 "Keymap used inside an editable field.")
880 (defvar widget-text-keymap
881 (let ((map (copy-keymap widget-keymap)))
882 ;; Since the widget code uses a `field' property to identify fields,
883 ;; ordinary beginning-of-line does the right thing.
884 ;; (define-key map "\C-a" 'widget-beginning-of-line)
885 (define-key map "\C-e" 'widget-end-of-line)
887 "Keymap used inside a text field.")
889 (defun widget-field-activate (pos &optional event)
890 "Invoke the editable field at point."
892 (let ((field (widget-field-at pos)))
894 (widget-apply-action field event)
896 (lookup-key widget-global-map (this-command-keys))))))
898 (defface widget-button-pressed
899 '((((min-colors 88) (class color))
900 (:foreground "red1"))
904 (:weight bold :underline t)))
905 "Face used for pressed buttons."
906 :group 'widget-faces)
907 ;; backward-compatibility alias
908 (put 'widget-button-pressed-face 'face-alias 'widget-button-pressed)
910 (defun widget-button-click (event)
911 "Invoke the button that the mouse is pointing at."
913 (if (widget-event-point event)
914 (let* ((pos (widget-event-point event))
915 (start (event-start event))
916 (button (get-char-property
917 pos 'button (and (windowp (posn-window start))
918 (window-buffer (posn-window start))))))
920 ;; Mouse click on a widget button. Do the following
921 ;; in a save-excursion so that the click on the button
922 ;; doesn't change point.
923 (save-selected-window
924 (select-window (posn-window (event-start event)))
926 (goto-char (posn-point (event-start event)))
927 (let* ((overlay (widget-get button :button-overlay))
928 (face (overlay-get overlay 'face))
929 (mouse-face (overlay-get overlay 'mouse-face)))
931 ;; Read events, including mouse-movement events
932 ;; until we receive a release event. Highlight/
933 ;; unhighlight the button the mouse was initially
934 ;; on when we move over it.
936 (when face ; avoid changing around image
938 'face widget-button-pressed-face)
940 'mouse-face widget-button-pressed-face))
941 (unless (widget-apply button :mouse-down-action event)
942 (let ((track-mouse t))
943 (while (not (widget-button-release-event-p event))
944 (setq event (read-event)
945 pos (widget-event-point event))
947 (eq (get-char-property pos 'button)
952 widget-button-pressed-face)
955 widget-button-pressed-face))
956 (overlay-put overlay 'face face)
957 (overlay-put overlay 'mouse-face mouse-face)))))
959 ;; When mouse is released over the button, run
960 ;; its action function.
962 (eq (get-char-property pos 'button) button))
963 (widget-apply-action button event)))
964 (overlay-put overlay 'face face)
965 (overlay-put overlay 'mouse-face mouse-face))))
967 (unless (pos-visible-in-window-p (widget-event-point event))
968 (mouse-set-point event)
973 (let ((up t) command)
974 ;; Mouse click not on a widget button. Find the global
975 ;; command to run, and check whether it is bound to an
977 (mouse-set-point event)
978 (if (memq (event-basic-type event) '(mouse-1 down-mouse-1))
979 (cond ((setq command ;down event
980 (lookup-key widget-global-map [down-mouse-1]))
982 ((setq command ;up event
983 (lookup-key widget-global-map [mouse-1]))))
984 (cond ((setq command ;down event
985 (lookup-key widget-global-map [down-mouse-2]))
987 ((setq command ;up event
988 (lookup-key widget-global-map [mouse-2])))))
990 ;; Don't execute up events twice.
991 (while (not (widget-button-release-event-p event))
992 (setq event (read-event))))
994 (call-interactively command)))))
995 (message "You clicked somewhere weird.")))
997 (defun widget-button-press (pos &optional event)
998 "Invoke button at POS."
1000 (let ((button (get-char-property pos 'button)))
1002 (widget-apply-action button event)
1003 (let ((command (lookup-key widget-global-map (this-command-keys))))
1004 (when (commandp command)
1005 (call-interactively command))))))
1007 (defun widget-tabable-at (&optional pos)
1008 "Return the tabable widget at POS, or nil.
1009 POS defaults to the value of (point)."
1010 (let ((widget (widget-at pos)))
1012 (let ((order (widget-get widget :tab-order)))
1018 (defvar widget-use-overlay-change t
1019 "If non-nil, use overlay change functions to tab around in the buffer.
1020 This is much faster, but doesn't work reliably on Emacs 19.34.")
1022 (defun widget-move (arg)
1023 "Move point to the ARG next field or button.
1024 ARG may be negative to move backward."
1025 (or (bobp) (> arg 0) (backward-char))
1028 (old (widget-tabable-at)))
1032 (goto-char (point-min))
1033 (setq wrapped (1+ wrapped)))
1034 (widget-use-overlay-change
1035 (goto-char (next-overlay-change (point))))
1040 (error "No buttons or fields found"))
1041 (let ((new (widget-tabable-at)))
1043 (unless (eq new old)
1049 (goto-char (point-max))
1050 (setq wrapped (1+ wrapped)))
1051 (widget-use-overlay-change
1052 (goto-char (previous-overlay-change (point))))
1057 (error "No buttons or fields found"))
1058 (let ((new (widget-tabable-at)))
1060 (unless (eq new old)
1061 (setq arg (1+ arg))))))
1062 (let ((new (widget-tabable-at)))
1063 (while (eq (widget-tabable-at) new)
1066 (widget-echo-help (point))
1067 (run-hooks 'widget-move-hook))
1069 (defun widget-forward (arg)
1070 "Move point to the next field or button.
1071 With optional ARG, move across that many fields."
1073 (run-hooks 'widget-forward-hook)
1076 (defun widget-backward (arg)
1077 "Move point to the previous field or button.
1078 With optional ARG, move across that many fields."
1080 (run-hooks 'widget-backward-hook)
1081 (widget-move (- arg)))
1083 ;; Since the widget code uses a `field' property to identify fields,
1084 ;; ordinary beginning-of-line does the right thing.
1085 (defalias 'widget-beginning-of-line 'beginning-of-line)
1087 (defun widget-end-of-line ()
1088 "Go to end of field or end of line, whichever is first.
1089 Trailing spaces at the end of padded fields are not considered part of
1092 ;; Ordinary end-of-line does the right thing, because we're inside
1093 ;; text with a `field' property.
1096 ;; ... except that we want to ignore trailing spaces in fields that
1097 ;; aren't terminated by a newline, because they are used as padding,
1098 ;; and ignored when extracting the entered value of the field.
1099 (skip-chars-backward " " (field-beginning (1- (point))))))
1101 (defun widget-kill-line ()
1102 "Kill to end of field or end of line, whichever is first."
1104 (let* ((field (widget-field-find (point)))
1105 (end (and field (widget-field-end field))))
1106 (if (and field (> (line-beginning-position 2) end))
1107 (kill-region (point) end)
1108 (call-interactively 'kill-line))))
1110 (defcustom widget-complete-field (lookup-key global-map "\M-\t")
1111 "Default function to call for completion inside fields."
1112 :options '(ispell-complete-word complete-tag lisp-complete-symbol)
1116 (defun widget-narrow-to-field ()
1119 (let ((field (widget-field-find (point))))
1121 (narrow-to-region (line-beginning-position) (line-end-position)))))
1123 (defun widget-complete ()
1124 "Complete content of editable field from point.
1125 When not inside a field, move to the previous button or field."
1127 (let ((field (widget-field-find (point))))
1130 (widget-narrow-to-field)
1131 (widget-apply field :complete))
1132 (error "Not in an editable field"))))
1134 ;;; Setting up the buffer.
1136 (defvar widget-field-new nil
1137 "List of all newly created editable fields in the buffer.")
1138 (make-variable-buffer-local 'widget-field-new)
1140 (defvar widget-field-list nil
1141 "List of all editable fields in the buffer.")
1142 (make-variable-buffer-local 'widget-field-list)
1144 (defun widget-at (&optional pos)
1145 "The button or field at POS (default, point)."
1146 (or (get-char-property (or pos (point)) 'button)
1147 (widget-field-at pos)))
1150 (defun widget-setup ()
1151 "Setup current buffer so editing string widgets works."
1152 (let ((inhibit-read-only t)
1153 (inhibit-modification-hooks t)
1155 (while widget-field-new
1156 (setq field (car widget-field-new)
1157 widget-field-new (cdr widget-field-new)
1158 widget-field-list (cons field widget-field-list))
1159 (let ((from (car (widget-get field :field-overlay)))
1160 (to (cdr (widget-get field :field-overlay))))
1161 (widget-specify-field field
1162 (marker-position from) (marker-position to))
1163 (set-marker from nil)
1164 (set-marker to nil))))
1166 (widget-add-change))
1168 (defvar widget-field-last nil)
1169 ;; Last field containing point.
1170 (make-variable-buffer-local 'widget-field-last)
1172 (defvar widget-field-was nil)
1173 ;; The widget data before the change.
1174 (make-variable-buffer-local 'widget-field-was)
1176 (defun widget-field-at (pos)
1177 "Return the widget field at POS, or nil if none."
1178 (let ((field (get-char-property (or pos (point)) 'field)))
1179 (if (eq field 'boundary)
1180 (get-char-property (or pos (point)) 'real-field)
1183 (defun widget-field-buffer (widget)
1184 "Return the buffer of WIDGET's editing field."
1185 (let ((overlay (widget-get widget :field-overlay)))
1186 (cond ((overlayp overlay)
1187 (overlay-buffer overlay))
1189 (marker-buffer (car overlay))))))
1191 (defun widget-field-start (widget)
1192 "Return the start of WIDGET's editing field."
1193 (let ((overlay (widget-get widget :field-overlay)))
1194 (if (overlayp overlay)
1195 (overlay-start overlay)
1198 (defun widget-field-end (widget)
1199 "Return the end of WIDGET's editing field."
1200 (let ((overlay (widget-get widget :field-overlay)))
1201 ;; Don't subtract one if local-map works at the end of the overlay,
1202 ;; or if a special `boundary' field has been added after the widget
1204 (if (overlayp overlay)
1205 (if (and (not (eq (with-current-buffer
1206 (widget-field-buffer widget)
1208 ;; `widget-narrow-to-field' can be
1209 ;; active when this function is called
1210 ;; from an change-functions hook. So
1211 ;; temporarily remove field narrowing
1212 ;; before to call `get-char-property'.
1214 (get-char-property (overlay-end overlay)
1217 (or widget-field-add-space
1218 (null (widget-get widget :size))))
1219 (1- (overlay-end overlay))
1220 (overlay-end overlay))
1223 (defun widget-field-find (pos)
1224 "Return the field at POS.
1225 Unlike (get-char-property POS 'field), this works with empty fields too."
1226 (let ((fields widget-field-list)
1229 (setq field (car fields)
1230 fields (cdr fields))
1231 (when (and (<= (widget-field-start field) pos)
1232 (<= pos (widget-field-end field)))
1234 (error "Overlapping fields"))
1235 (setq found field)))
1238 (defun widget-before-change (from to)
1239 ;; This is how, for example, a variable changes its state to `modified'.
1240 ;; when it is being edited.
1241 (unless inhibit-read-only
1242 (let ((from-field (widget-field-find from))
1243 (to-field (widget-field-find to)))
1244 (cond ((not (eq from-field to-field))
1245 (add-hook 'post-command-hook 'widget-add-change nil t)
1246 (signal 'text-read-only
1247 '("Change should be restricted to a single field")))
1249 (add-hook 'post-command-hook 'widget-add-change nil t)
1250 (signal 'text-read-only
1251 '("Attempt to change text outside editable field")))
1252 (widget-field-use-before-change
1253 (widget-apply from-field :notify from-field))))))
1255 (defun widget-add-change ()
1256 (remove-hook 'post-command-hook 'widget-add-change t)
1257 (add-hook 'before-change-functions 'widget-before-change nil t)
1258 (add-hook 'after-change-functions 'widget-after-change nil t))
1260 (defun widget-after-change (from to old)
1261 "Adjust field size and text properties."
1262 (let ((field (widget-field-find from))
1263 (other (widget-field-find to)))
1265 (unless (eq field other)
1266 (error "Change in different fields"))
1267 (let ((size (widget-get field :size)))
1269 (let ((begin (widget-field-start field))
1270 (end (widget-field-end field)))
1271 (cond ((< (- end begin) size)
1275 (insert-char ?\s (- (+ begin size) end))))
1276 ((> (- end begin) size)
1277 ;; Field too large and
1278 (if (or (< (point) (+ begin size))
1280 ;; Point is outside extra space.
1281 (setq begin (+ begin size))
1282 ;; Point is within the extra space.
1283 (setq begin (point)))
1286 (while (and (eq (preceding-char) ?\s)
1288 (delete-backward-char 1)))))))
1289 (widget-specify-secret field))
1290 (widget-apply field :notify field))))
1292 ;;; Widget Functions
1294 ;; These functions are used in the definition of multiple widgets.
1296 (defun widget-parent-action (widget &optional event)
1297 "Tell :parent of WIDGET to handle the :action.
1298 Optional EVENT is the event that triggered the action."
1299 (widget-apply (widget-get widget :parent) :action event))
1301 (defun widget-children-value-delete (widget)
1302 "Delete all :children and :buttons in WIDGET."
1303 (mapc 'widget-delete (widget-get widget :children))
1304 (widget-put widget :children nil)
1305 (mapc 'widget-delete (widget-get widget :buttons))
1306 (widget-put widget :buttons nil))
1308 (defun widget-children-validate (widget)
1309 "All the :children must be valid."
1310 (let ((children (widget-get widget :children))
1312 (while (and children (not found))
1313 (setq child (car children)
1314 children (cdr children)
1315 found (widget-apply child :validate)))
1318 (defun widget-child-value-get (widget)
1319 "Get the value of the first member of :children in WIDGET."
1320 (widget-value (car (widget-get widget :children))))
1322 (defun widget-child-value-inline (widget)
1323 "Get the inline value of the first member of :children in WIDGET."
1324 (widget-apply (car (widget-get widget :children)) :value-inline))
1326 (defun widget-child-validate (widget)
1327 "The result of validating the first member of :children in WIDGET."
1328 (widget-apply (car (widget-get widget :children)) :validate))
1330 (defun widget-type-value-create (widget)
1331 "Convert and instantiate the value of the :type attribute of WIDGET.
1332 Store the newly created widget in the :children attribute.
1334 The value of the :type attribute should be an unconverted widget type."
1335 (let ((value (widget-get widget :value))
1336 (type (widget-get widget :type)))
1337 (widget-put widget :children
1338 (list (widget-create-child-value widget
1339 (widget-convert type)
1342 (defun widget-type-default-get (widget)
1343 "Get default value from the :type attribute of WIDGET.
1345 The value of the :type attribute should be an unconverted widget type."
1346 (widget-default-get (widget-convert (widget-get widget :type))))
1348 (defun widget-type-match (widget value)
1349 "Non-nil if the :type value of WIDGET matches VALUE.
1351 The value of the :type attribute should be an unconverted widget type."
1352 (widget-apply (widget-convert (widget-get widget :type)) :match value))
1354 (defun widget-types-copy (widget)
1355 "Copy :args as widget types in WIDGET."
1356 (widget-put widget :args (mapcar 'widget-copy (widget-get widget :args)))
1359 ;; Made defsubst to speed up face editor creation.
1360 (defsubst widget-types-convert-widget (widget)
1361 "Convert :args as widget types in WIDGET."
1362 (widget-put widget :args (mapcar 'widget-convert (widget-get widget :args)))
1365 (defun widget-value-convert-widget (widget)
1366 "Initialize :value from :args in WIDGET."
1367 (let ((args (widget-get widget :args)))
1369 (widget-put widget :value (car args))
1370 ;; Don't convert :value here, as this is done in `widget-convert'.
1371 ;; (widget-put widget :value (widget-apply widget
1372 ;; :value-to-internal (car args)))
1373 (widget-put widget :args nil)))
1376 (defun widget-value-value-get (widget)
1377 "Return the :value property of WIDGET."
1378 (widget-get widget :value))
1380 ;;; The `default' Widget.
1382 (define-widget 'default nil
1383 "Basic widget other widgets are derived from."
1384 :value-to-internal (lambda (widget value) value)
1385 :value-to-external (lambda (widget value) value)
1386 :button-prefix 'widget-button-prefix
1387 :button-suffix 'widget-button-suffix
1388 :complete 'widget-default-complete
1389 :create 'widget-default-create
1392 :format-handler 'widget-default-format-handler
1393 :button-face-get 'widget-default-button-face-get
1394 :sample-face-get 'widget-default-sample-face-get
1395 :delete 'widget-default-delete
1397 :value-set 'widget-default-value-set
1398 :value-inline 'widget-default-value-inline
1399 :value-delete 'ignore
1400 :default-get 'widget-default-default-get
1401 :menu-tag-get 'widget-default-menu-tag-get
1403 :active 'widget-default-active
1404 :activate 'widget-specify-active
1405 :deactivate 'widget-default-deactivate
1406 :mouse-down-action #'ignore
1407 :action 'widget-default-action
1408 :notify 'widget-default-notify
1409 :prompt-value 'widget-default-prompt-value)
1411 (defun widget-default-complete (widget)
1412 "Call the value of the :complete-function property of WIDGET.
1413 If that does not exists, call the value of `widget-complete-field'."
1414 (call-interactively (or (widget-get widget :complete-function)
1415 widget-complete-field)))
1417 (defun widget-default-create (widget)
1418 "Create WIDGET at point in the current buffer."
1419 (widget-specify-insert
1420 (let ((from (point))
1421 button-begin button-end
1422 sample-begin sample-end
1425 (insert (widget-get widget :format))
1427 ;; Parse escapes in format.
1428 (while (re-search-forward "%\\(.\\)" nil t)
1429 (let ((escape (char-after (match-beginning 1))))
1430 (delete-backward-char 2)
1431 (cond ((eq escape ?%)
1434 (setq button-begin (point))
1435 (insert (widget-get-indirect widget :button-prefix)))
1437 (insert (widget-get-indirect widget :button-suffix))
1438 (setq button-end (point)))
1440 (setq sample-begin (point)))
1442 (setq sample-end (point)))
1444 (when (widget-get widget :indent)
1446 (insert-char ?\s (widget-get widget :indent))))
1448 (let ((image (widget-get widget :tag-glyph))
1449 (tag (widget-get widget :tag)))
1451 (widget-image-insert widget (or tag "image") image))
1455 (princ (widget-get widget :value)
1456 (current-buffer))))))
1458 (let ((doc (widget-get widget :doc)))
1460 (setq doc-begin (point))
1462 (while (eq (preceding-char) ?\n)
1463 (delete-backward-char 1))
1465 (setq doc-end (point)))))
1467 (if (and button-begin (not button-end))
1468 (widget-apply widget :value-create)
1469 (setq value-pos (point))))
1471 (widget-apply widget :format-handler escape)))))
1472 ;; Specify button, sample, and doc, and insert value.
1473 (and button-begin button-end
1474 (widget-specify-button widget button-begin button-end))
1475 (and sample-begin sample-end
1476 (widget-specify-sample widget sample-begin sample-end))
1477 (and doc-begin doc-end
1478 (widget-specify-doc widget doc-begin doc-end))
1480 (goto-char value-pos)
1481 (widget-apply widget :value-create)))
1482 (let ((from (point-min-marker))
1483 (to (point-max-marker)))
1484 (set-marker-insertion-type from t)
1485 (set-marker-insertion-type to nil)
1486 (widget-put widget :from from)
1487 (widget-put widget :to to)))
1488 (widget-clear-undo))
1490 (defun widget-default-format-handler (widget escape)
1491 ;; We recognize the %h escape by default.
1492 (let* ((buttons (widget-get widget :buttons)))
1493 (cond ((eq escape ?h)
1494 (let* ((doc-property (widget-get widget :documentation-property))
1495 (doc-try (cond ((widget-get widget :doc))
1496 ((functionp doc-property)
1497 (funcall doc-property
1498 (widget-get widget :value)))
1499 ((symbolp doc-property)
1500 (documentation-property
1501 (widget-get widget :value)
1503 (doc-text (and (stringp doc-try)
1504 (> (length doc-try) 1)
1506 (doc-indent (widget-get widget :documentation-indent)))
1508 (and (eq (preceding-char) ?\n)
1509 (widget-get widget :indent)
1510 (insert-char ?\s (widget-get widget :indent)))
1511 ;; The `*' in the beginning is redundant.
1512 (when (eq (aref doc-text 0) ?*)
1513 (setq doc-text (substring doc-text 1)))
1514 ;; Get rid of trailing newlines.
1515 (when (string-match "\n+\\'" doc-text)
1516 (setq doc-text (substring doc-text 0 (match-beginning 0))))
1517 (push (widget-create-child-and-convert
1518 widget 'documentation-string
1519 :indent (cond ((numberp doc-indent )
1527 (error "Unknown escape `%c'" escape)))
1528 (widget-put widget :buttons buttons)))
1530 (defun widget-default-button-face-get (widget)
1531 ;; Use :button-face or widget-button-face
1532 (or (widget-get widget :button-face)
1533 (let ((parent (widget-get widget :parent)))
1535 (widget-apply parent :button-face-get)
1536 widget-button-face))))
1538 (defun widget-default-sample-face-get (widget)
1539 ;; Use :sample-face.
1540 (widget-get widget :sample-face))
1542 (defun widget-default-delete (widget)
1543 "Remove widget from the buffer."
1544 (let ((from (widget-get widget :from))
1545 (to (widget-get widget :to))
1546 (inactive-overlay (widget-get widget :inactive))
1547 (button-overlay (widget-get widget :button-overlay))
1548 (sample-overlay (widget-get widget :sample-overlay))
1549 (doc-overlay (widget-get widget :doc-overlay))
1550 (inhibit-modification-hooks t)
1551 (inhibit-read-only t))
1552 (widget-apply widget :value-delete)
1553 (widget-children-value-delete widget)
1554 (when inactive-overlay
1555 (delete-overlay inactive-overlay))
1556 (when button-overlay
1557 (delete-overlay button-overlay))
1558 (when sample-overlay
1559 (delete-overlay sample-overlay))
1561 (delete-overlay doc-overlay))
1563 ;; Kludge: this doesn't need to be true for empty formats.
1564 (delete-region from to))
1565 (set-marker from nil)
1566 (set-marker to nil))
1567 (widget-clear-undo))
1569 (defun widget-default-value-set (widget value)
1570 "Recreate widget with new value."
1571 (let* ((old-pos (point))
1572 (from (copy-marker (widget-get widget :from)))
1573 (to (copy-marker (widget-get widget :to)))
1574 (offset (if (and (<= from old-pos) (<= old-pos to))
1575 (if (>= old-pos (1- to))
1577 (- old-pos from)))))
1578 ;;??? Bug: this ought to insert the new value before deleting the old one,
1579 ;; so that markers on either side of the value automatically
1580 ;; stay on the same side. -- rms.
1582 (goto-char (widget-get widget :from))
1583 (widget-apply widget :delete)
1584 (widget-put widget :value value)
1585 (widget-apply widget :create))
1588 (goto-char (+ (widget-get widget :to) offset 1))
1589 (goto-char (min (+ from offset) (1- (widget-get widget :to))))))))
1591 (defun widget-default-value-inline (widget)
1592 "Wrap value in a list unless it is inline."
1593 (if (widget-get widget :inline)
1594 (widget-value widget)
1595 (list (widget-value widget))))
1597 (defun widget-default-default-get (widget)
1599 (widget-get widget :value))
1601 (defun widget-default-menu-tag-get (widget)
1602 "Use tag or value for menus."
1603 (or (widget-get widget :menu-tag)
1604 (widget-get widget :tag)
1605 (widget-princ-to-string (widget-get widget :value))))
1607 (defun widget-default-active (widget)
1608 "Return t iff this widget active (user modifiable)."
1609 (or (widget-get widget :always-active)
1610 (and (not (widget-get widget :inactive))
1611 (let ((parent (widget-get widget :parent)))
1613 (widget-apply parent :active))))))
1615 (defun widget-default-deactivate (widget)
1616 "Make WIDGET inactive for user modifications."
1617 (widget-specify-inactive widget
1618 (widget-get widget :from)
1619 (widget-get widget :to)))
1621 (defun widget-default-action (widget &optional event)
1622 "Notify the parent when a widget changes."
1623 (let ((parent (widget-get widget :parent)))
1625 (widget-apply parent :notify widget event))))
1627 (defun widget-default-notify (widget child &optional event)
1628 "Pass notification to parent."
1629 (widget-default-action widget event))
1631 (defun widget-default-prompt-value (widget prompt value unbound)
1632 "Read an arbitrary value. Stolen from `set-variable'."
1633 ;; (let ((initial (if unbound
1635 ;; It would be nice if we could do a `(cons val 1)' here.
1636 ;; (prin1-to-string (custom-quote value))))))
1637 (eval-minibuffer prompt))
1639 ;;; The `item' Widget.
1641 (define-widget 'item 'default
1642 "Constant items for inclusion in other widgets."
1643 :convert-widget 'widget-value-convert-widget
1644 :value-create 'widget-item-value-create
1645 :value-delete 'ignore
1646 :value-get 'widget-value-value-get
1647 :match 'widget-item-match
1648 :match-inline 'widget-item-match-inline
1649 :action 'widget-item-action
1652 (defun widget-item-value-create (widget)
1653 "Insert the printed representation of the value."
1654 (princ (widget-get widget :value) (current-buffer)))
1656 (defun widget-item-match (widget value)
1657 ;; Match if the value is the same.
1658 (equal (widget-get widget :value) value))
1660 (defun widget-item-match-inline (widget values)
1661 ;; Match if the value is the same.
1662 (let ((value (widget-get widget :value)))
1664 (<= (length value) (length values))
1665 (let ((head (widget-sublist values 0 (length value))))
1666 (and (equal head value)
1667 (cons head (widget-sublist values (length value))))))))
1669 (defun widget-sublist (list start &optional end)
1670 "Return the sublist of LIST from START to END.
1671 If END is omitted, it defaults to the length of LIST."
1672 (if (> start 0) (setq list (nthcdr start list)))
1674 (unless (<= end start)
1675 (setq list (copy-sequence list))
1676 (setcdr (nthcdr (- end start 1) list) nil)
1678 (copy-sequence list)))
1680 (defun widget-item-action (widget &optional event)
1681 ;; Just notify itself.
1682 (widget-apply widget :notify widget event))
1684 ;;; The `push-button' Widget.
1686 ;; (defcustom widget-push-button-gui t
1687 ;; "If non nil, use GUI push buttons when available."
1691 ;; Cache already created GUI objects.
1692 ;; (defvar widget-push-button-cache nil)
1694 (defcustom widget-push-button-prefix "["
1695 "String used as prefix for buttons."
1697 :group 'widget-button)
1699 (defcustom widget-push-button-suffix "]"
1700 "String used as suffix for buttons."
1702 :group 'widget-button)
1704 (define-widget 'push-button 'item
1705 "A pushable button."
1708 :value-create 'widget-push-button-value-create
1711 (defun widget-push-button-value-create (widget)
1712 "Insert text representing the `on' and `off' states."
1713 (let* ((tag (or (widget-get widget :tag)
1714 (widget-get widget :value)))
1715 (tag-glyph (widget-get widget :tag-glyph))
1716 (text (concat widget-push-button-prefix
1717 tag widget-push-button-suffix)))
1719 (widget-image-insert widget text tag-glyph)
1722 ;; (defun widget-gui-action (widget)
1723 ;; "Apply :action for WIDGET."
1724 ;; (widget-apply-action widget (this-command-keys)))
1726 ;;; The `link' Widget.
1728 (defcustom widget-link-prefix "["
1729 "String used as prefix for links."
1731 :group 'widget-button)
1733 (defcustom widget-link-suffix "]"
1734 "String used as suffix for links."
1736 :group 'widget-button)
1738 (define-widget 'link 'item
1740 :button-prefix 'widget-link-prefix
1741 :button-suffix 'widget-link-suffix
1743 :help-echo "Follow the link."
1746 ;;; The `info-link' Widget.
1748 (define-widget 'info-link 'link
1749 "A link to an info file."
1750 :action 'widget-info-link-action)
1752 (defun widget-info-link-action (widget &optional event)
1753 "Open the info node specified by WIDGET."
1754 (info (widget-value widget)))
1756 ;;; The `url-link' Widget.
1758 (define-widget 'url-link 'link
1759 "A link to an www page."
1760 :action 'widget-url-link-action)
1762 (defun widget-url-link-action (widget &optional event)
1763 "Open the URL specified by WIDGET."
1764 (browse-url (widget-value widget)))
1766 ;;; The `function-link' Widget.
1768 (define-widget 'function-link 'link
1769 "A link to an Emacs function."
1770 :action 'widget-function-link-action)
1772 (defun widget-function-link-action (widget &optional event)
1773 "Show the function specified by WIDGET."
1774 (describe-function (widget-value widget)))
1776 ;;; The `variable-link' Widget.
1778 (define-widget 'variable-link 'link
1779 "A link to an Emacs variable."
1780 :action 'widget-variable-link-action)
1782 (defun widget-variable-link-action (widget &optional event)
1783 "Show the variable specified by WIDGET."
1784 (describe-variable (widget-value widget)))
1786 ;;; The `file-link' Widget.
1788 (define-widget 'file-link 'link
1790 :action 'widget-file-link-action)
1792 (defun widget-file-link-action (widget &optional event)
1793 "Find the file specified by WIDGET."
1794 (find-file (widget-value widget)))
1796 ;;; The `emacs-library-link' Widget.
1798 (define-widget 'emacs-library-link 'link
1799 "A link to an Emacs Lisp library file."
1800 :action 'widget-emacs-library-link-action)
1802 (defun widget-emacs-library-link-action (widget &optional event)
1803 "Find the Emacs library file specified by WIDGET."
1804 (find-file (locate-library (widget-value widget))))
1806 ;;; The `emacs-commentary-link' Widget.
1808 (define-widget 'emacs-commentary-link 'link
1809 "A link to Commentary in an Emacs Lisp library file."
1810 :action 'widget-emacs-commentary-link-action)
1812 (defun widget-emacs-commentary-link-action (widget &optional event)
1813 "Find the Commentary section of the Emacs file specified by WIDGET."
1814 (finder-commentary (widget-value widget)))
1816 ;;; The `editable-field' Widget.
1818 (define-widget 'editable-field 'default
1819 "An editable text field."
1820 :convert-widget 'widget-value-convert-widget
1821 :keymap widget-field-keymap
1823 :help-echo "M-TAB: complete field; RET: enter value"
1825 :prompt-internal 'widget-field-prompt-internal
1826 :prompt-history 'widget-field-history
1827 :prompt-value 'widget-field-prompt-value
1828 :action 'widget-field-action
1829 :validate 'widget-field-validate
1831 :error "Field's value doesn't match allowed forms"
1832 :value-create 'widget-field-value-create
1833 :value-delete 'widget-field-value-delete
1834 :value-get 'widget-field-value-get
1835 :match 'widget-field-match)
1837 (defvar widget-field-history nil
1838 "History of field minibuffer edits.")
1840 (defun widget-field-prompt-internal (widget prompt initial history)
1841 "Read string for WIDGET promptinhg with PROMPT.
1842 INITIAL is the initial input and HISTORY is a symbol containing
1844 (read-string prompt initial history))
1846 (defun widget-field-prompt-value (widget prompt value unbound)
1847 "Prompt for a string."
1848 (widget-apply widget
1850 (widget-apply widget
1851 :prompt-internal prompt
1853 (cons (widget-apply widget
1854 :value-to-internal value)
1856 (widget-get widget :prompt-history))))
1858 (defvar widget-edit-functions nil)
1860 (defun widget-field-action (widget &optional event)
1861 "Move to next field."
1863 (run-hook-with-args 'widget-edit-functions widget))
1865 (defun widget-field-validate (widget)
1866 "Valid if the content matches `:valid-regexp'."
1867 (unless (string-match (widget-get widget :valid-regexp)
1868 (widget-apply widget :value-get))
1871 (defun widget-field-value-create (widget)
1872 "Create an editable text field."
1873 (let ((size (widget-get widget :size))
1874 (value (widget-get widget :value))
1876 ;; This is changed to a real overlay in `widget-setup'. We
1877 ;; need the end points to behave differently until
1878 ;; `widget-setup' is called.
1879 (overlay (cons (make-marker) (make-marker))))
1880 (widget-put widget :field-overlay overlay)
1883 (< (length value) size)
1884 (insert-char ?\s (- size (length value))))
1885 (unless (memq widget widget-field-list)
1886 (setq widget-field-new (cons widget widget-field-new)))
1887 (move-marker (cdr overlay) (point))
1888 (set-marker-insertion-type (cdr overlay) nil)
1891 (move-marker (car overlay) from)
1892 (set-marker-insertion-type (car overlay) t)))
1894 (defun widget-field-value-delete (widget)
1895 "Remove the widget from the list of active editing fields."
1896 (setq widget-field-list (delq widget widget-field-list))
1897 (setq widget-field-new (delq widget widget-field-new))
1898 ;; These are nil if the :format string doesn't contain `%v'.
1899 (let ((overlay (widget-get widget :field-overlay)))
1900 (when (overlayp overlay)
1901 (delete-overlay overlay))))
1903 (defun widget-field-value-get (widget)
1904 "Return current text in editing field."
1905 (let ((from (widget-field-start widget))
1906 (to (widget-field-end widget))
1907 (buffer (widget-field-buffer widget))
1908 (size (widget-get widget :size))
1909 (secret (widget-get widget :secret))
1910 (old (current-buffer)))
1917 (eq (char-after (1- to)) ?\s))
1919 (let ((result (buffer-substring-no-properties from to)))
1922 (while (< (+ from index) to)
1924 (get-char-property (+ from index) 'secret))
1925 (setq index (1+ index)))))
1928 (widget-get widget :value))))
1930 (defun widget-field-match (widget value)
1931 ;; Match any string.
1934 ;;; The `text' Widget.
1936 (define-widget 'text 'editable-field
1937 "A multiline text area."
1938 :keymap widget-text-keymap)
1940 ;;; The `menu-choice' Widget.
1942 (define-widget 'menu-choice 'default
1943 "A menu of options."
1944 :convert-widget 'widget-types-convert-widget
1945 :copy 'widget-types-copy
1946 :format "%[%t%]: %v"
1949 :void '(item :format "invalid (%t)\n")
1950 :value-create 'widget-choice-value-create
1951 :value-get 'widget-child-value-get
1952 :value-inline 'widget-child-value-inline
1953 :default-get 'widget-choice-default-get
1954 :mouse-down-action 'widget-choice-mouse-down-action
1955 :action 'widget-choice-action
1956 :error "Make a choice"
1957 :validate 'widget-choice-validate
1958 :match 'widget-choice-match
1959 :match-inline 'widget-choice-match-inline)
1961 (defun widget-choice-value-create (widget)
1962 "Insert the first choice that matches the value."
1963 (let ((value (widget-get widget :value))
1964 (args (widget-get widget :args))
1965 (explicit (widget-get widget :explicit-choice))
1969 ;; If the user specified the choice for this value,
1970 ;; respect that choice.
1971 (widget-put widget :children (list (widget-create-child-value
1972 widget explicit value)))
1973 (widget-put widget :choice explicit)
1974 (widget-put widget :explicit-choice nil))
1976 (setq current (car args)
1978 (when (widget-apply current :match value)
1979 (widget-put widget :children (list (widget-create-child-value
1980 widget current value)))
1981 (widget-put widget :choice current)
1985 (let ((void (widget-get widget :void)))
1986 (widget-put widget :children (list (widget-create-child-and-convert
1987 widget void :value value)))
1988 (widget-put widget :choice void))))))
1990 (defun widget-choice-default-get (widget)
1991 ;; Get default for the first choice.
1992 (widget-default-get (car (widget-get widget :args))))
1994 (defcustom widget-choice-toggle nil
1995 "If non-nil, a binary choice will just toggle between the values.
1996 Otherwise, the user will explicitly have to choose between the values
1997 when he invoked the menu."
2001 (defun widget-choice-mouse-down-action (widget &optional event)
2002 ;; Return non-nil if we need a menu.
2003 (let ((args (widget-get widget :args))
2004 (old (widget-get widget :choice)))
2005 (cond ((not (display-popup-menus-p))
2006 ;; No place to pop up a menu.
2008 ((< (length args) 2)
2009 ;; Empty or singleton list, just return the value.
2011 ((> (length args) widget-menu-max-size)
2012 ;; Too long, prompt.
2014 ((> (length args) 2)
2015 ;; Reasonable sized list, use menu.
2017 ((and widget-choice-toggle (memq old args))
2021 ;; Ask which of the two.
2024 (defun widget-choice-action (widget &optional event)
2026 (let ((args (widget-get widget :args))
2027 (old (widget-get widget :choice))
2028 (tag (widget-apply widget :menu-tag-get))
2029 (completion-ignore-case (widget-get widget :case-fold))
2032 ;; Remember old value.
2033 (if (and old (not (widget-apply widget :validate)))
2034 (let* ((external (widget-value widget))
2035 (internal (widget-apply old :value-to-internal external)))
2036 (widget-put old :value internal)))
2039 (cond ((= (length args) 0)
2041 ((= (length args) 1)
2043 ((and widget-choice-toggle
2046 (if (eq old (nth 0 args))
2051 (setq current (car args)
2054 (cons (cons (widget-apply current :menu-tag-get)
2057 (setq this-explicit t)
2058 (widget-choose tag (reverse choices) event))))
2060 ;; If this was an explicit user choice, record the choice,
2061 ;; so that widget-choice-value-create will respect it.
2063 (widget-put widget :explicit-choice current))
2064 (widget-value-set widget (widget-default-get current))
2066 (widget-apply widget :notify widget event)))
2067 (run-hook-with-args 'widget-edit-functions widget))
2069 (defun widget-choice-validate (widget)
2070 ;; Valid if we have made a valid choice.
2071 (if (eq (widget-get widget :void) (widget-get widget :choice))
2073 (widget-apply (car (widget-get widget :children)) :validate)))
2075 (defun widget-choice-match (widget value)
2076 ;; Matches if one of the choices matches.
2077 (let ((args (widget-get widget :args))
2079 (while (and args (not found))
2080 (setq current (car args)
2082 found (widget-apply current :match value)))
2085 (defun widget-choice-match-inline (widget values)
2086 ;; Matches if one of the choices matches.
2087 (let ((args (widget-get widget :args))
2089 (while (and args (null found))
2090 (setq current (car args)
2092 found (widget-match-inline current values)))
2095 ;;; The `toggle' Widget.
2097 (define-widget 'toggle 'item
2098 "Toggle between two states."
2100 :value-create 'widget-toggle-value-create
2101 :action 'widget-toggle-action
2102 :match (lambda (widget value) t)
2106 (defun widget-toggle-value-create (widget)
2107 "Insert text representing the `on' and `off' states."
2108 (if (widget-value widget)
2109 (let ((image (widget-get widget :on-glyph)))
2110 (and (display-graphic-p)
2112 (not (eq (car image) 'image))
2113 (widget-put widget :on-glyph (setq image (eval image))))
2114 (widget-image-insert widget
2115 (widget-get widget :on)
2117 (let ((image (widget-get widget :off-glyph)))
2118 (and (display-graphic-p)
2120 (not (eq (car image) 'image))
2121 (widget-put widget :off-glyph (setq image (eval image))))
2122 (widget-image-insert widget (widget-get widget :off) image))))
2124 (defun widget-toggle-action (widget &optional event)
2126 (widget-value-set widget (not (widget-value widget)))
2127 (widget-apply widget :notify widget event)
2128 (run-hook-with-args 'widget-edit-functions widget))
2130 ;;; The `checkbox' Widget.
2132 (define-widget 'checkbox 'toggle
2133 "A checkbox toggle."
2138 ;; We could probably do the same job as the images using single
2139 ;; space characters in a boxed face with a stretch specification to
2140 ;; make them square.
2141 :on-glyph '(create-image "\300\300\141\143\067\076\034\030"
2142 'xbm t :width 8 :height 8
2143 :background "grey75" ; like default mode line
2148 :off-glyph '(create-image (make-string 8 0)
2149 'xbm t :width 8 :height 8
2150 :background "grey75"
2154 :help-echo "Toggle this item."
2155 :action 'widget-checkbox-action)
2157 (defun widget-checkbox-action (widget &optional event)
2158 "Toggle checkbox, notify parent, and set active state of sibling."
2159 (widget-toggle-action widget event)
2160 (let ((sibling (widget-get-sibling widget)))
2162 (if (widget-value widget)
2163 (widget-apply sibling :activate)
2164 (widget-apply sibling :deactivate))
2165 (widget-clear-undo))))
2167 ;;; The `checklist' Widget.
2169 (define-widget 'checklist 'default
2170 "A multiple choice widget."
2171 :convert-widget 'widget-types-convert-widget
2172 :copy 'widget-types-copy
2175 :entry-format "%b %v"
2177 :value-create 'widget-checklist-value-create
2178 :value-get 'widget-checklist-value-get
2179 :validate 'widget-checklist-validate
2180 :match 'widget-checklist-match
2181 :match-inline 'widget-checklist-match-inline)
2183 (defun widget-checklist-value-create (widget)
2184 ;; Insert all values
2185 (let ((alist (widget-checklist-match-find widget (widget-get widget :value)))
2186 (args (widget-get widget :args)))
2188 (widget-checklist-add-item widget (car args) (assq (car args) alist))
2189 (setq args (cdr args)))
2190 (widget-put widget :children (nreverse (widget-get widget :children)))))
2192 (defun widget-checklist-add-item (widget type chosen)
2193 "Create checklist item in WIDGET of type TYPE.
2194 If the item is checked, CHOSEN is a cons whose cdr is the value."
2195 (and (eq (preceding-char) ?\n)
2196 (widget-get widget :indent)
2197 (insert-char ?\s (widget-get widget :indent)))
2198 (widget-specify-insert
2199 (let* ((children (widget-get widget :children))
2200 (buttons (widget-get widget :buttons))
2201 (button-args (or (widget-get type :sibling-args)
2202 (widget-get widget :button-args)))
2205 (insert (widget-get widget :entry-format))
2207 ;; Parse % escapes in format.
2208 (while (re-search-forward "%\\([bv%]\\)" nil t)
2209 (let ((escape (char-after (match-beginning 1))))
2210 (delete-backward-char 2)
2211 (cond ((eq escape ?%)
2214 (setq button (apply 'widget-create-child-and-convert
2216 :value (not (null chosen))
2221 (let ((child (widget-create-child widget type)))
2222 (widget-apply child :deactivate)
2224 ((widget-get type :inline)
2225 (widget-create-child-value
2226 widget type (cdr chosen)))
2228 (widget-create-child-value
2229 widget type (car (cdr chosen)))))))
2231 (error "Unknown escape `%c'" escape)))))
2232 ;; Update properties.
2233 (and button child (widget-put child :button button))
2234 (and button (widget-put widget :buttons (cons button buttons)))
2235 (and child (widget-put widget :children (cons child children))))))
2237 (defun widget-checklist-match (widget values)
2238 ;; All values must match a type in the checklist.
2240 (null (cdr (widget-checklist-match-inline widget values)))))
2242 (defun widget-checklist-match-inline (widget values)
2243 ;; Find the values which match a type in the checklist.
2244 (let ((greedy (widget-get widget :greedy))
2245 (args (copy-sequence (widget-get widget :args)))
2248 (let ((answer (widget-checklist-match-up args values)))
2250 (let ((vals (widget-match-inline answer values)))
2251 (setq found (append found (car vals))
2253 args (delq answer args))))
2255 (setq rest (append rest (list (car values)))
2256 values (cdr values)))
2258 (setq rest (append rest values)
2262 (defun widget-checklist-match-find (widget vals)
2263 "Find the vals which match a type in the checklist.
2264 Return an alist of (TYPE MATCH)."
2265 (let ((greedy (widget-get widget :greedy))
2266 (args (copy-sequence (widget-get widget :args)))
2269 (let ((answer (widget-checklist-match-up args vals)))
2271 (let ((match (widget-match-inline answer vals)))
2272 (setq found (cons (cons answer (car match)) found)
2274 args (delq answer args))))
2276 (setq vals (cdr vals)))
2281 (defun widget-checklist-match-up (args vals)
2282 "Return the first type from ARGS that matches VALS."
2283 (let (current found)
2284 (while (and args (null found))
2285 (setq current (car args)
2287 found (widget-match-inline current vals)))
2291 (defun widget-checklist-value-get (widget)
2292 ;; The values of all selected items.
2293 (let ((children (widget-get widget :children))
2296 (setq child (car children)
2297 children (cdr children))
2298 (if (widget-value (widget-get child :button))
2299 (setq result (append result (widget-apply child :value-inline)))))
2302 (defun widget-checklist-validate (widget)
2303 ;; Ticked chilren must be valid.
2304 (let ((children (widget-get widget :children))
2306 (while (and children (not found))
2307 (setq child (car children)
2308 children (cdr children)
2309 button (widget-get child :button)
2310 found (and (widget-value button)
2311 (widget-apply child :validate))))
2314 ;;; The `option' Widget
2316 (define-widget 'option 'checklist
2317 "An widget with an optional item."
2320 ;;; The `choice-item' Widget.
2322 (define-widget 'choice-item 'item
2323 "Button items that delegate action events to their parents."
2324 :action 'widget-parent-action
2325 :format "%[%t%] \n")
2327 ;;; The `radio-button' Widget.
2329 (define-widget 'radio-button 'toggle
2330 "A radio button for use in the `radio' widget."
2331 :notify 'widget-radio-button-notify
2338 :off-glyph "radio0")
2340 (defun widget-radio-button-notify (widget child &optional event)
2342 (widget-apply (widget-get widget :parent) :action widget event))
2344 ;;; The `radio-button-choice' Widget.
2346 (define-widget 'radio-button-choice 'default
2347 "Select one of multiple options."
2348 :convert-widget 'widget-types-convert-widget
2349 :copy 'widget-types-copy
2352 :entry-format "%b %v"
2353 :value-create 'widget-radio-value-create
2354 :value-get 'widget-radio-value-get
2355 :value-inline 'widget-radio-value-inline
2356 :value-set 'widget-radio-value-set
2357 :error "You must push one of the buttons"
2358 :validate 'widget-radio-validate
2359 :match 'widget-choice-match
2360 :match-inline 'widget-choice-match-inline
2361 :action 'widget-radio-action)
2363 (defun widget-radio-value-create (widget)
2364 ;; Insert all values
2365 (let ((args (widget-get widget :args))
2368 (setq arg (car args)
2370 (widget-radio-add-item widget arg))))
2372 (defun widget-radio-add-item (widget type)
2373 "Add to radio widget WIDGET a new radio button item of type TYPE."
2374 ;; (setq type (widget-convert type))
2375 (and (eq (preceding-char) ?\n)
2376 (widget-get widget :indent)
2377 (insert-char ?\s (widget-get widget :indent)))
2378 (widget-specify-insert
2379 (let* ((value (widget-get widget :value))
2380 (children (widget-get widget :children))
2381 (buttons (widget-get widget :buttons))
2382 (button-args (or (widget-get type :sibling-args)
2383 (widget-get widget :button-args)))
2385 (chosen (and (null (widget-get widget :choice))
2386 (widget-apply type :match value)))
2388 (insert (widget-get widget :entry-format))
2390 ;; Parse % escapes in format.
2391 (while (re-search-forward "%\\([bv%]\\)" nil t)
2392 (let ((escape (char-after (match-beginning 1))))
2393 (delete-backward-char 2)
2394 (cond ((eq escape ?%)
2397 (setq button (apply 'widget-create-child-and-convert
2398 widget 'radio-button
2399 :value (not (null chosen))
2402 (setq child (if chosen
2403 (widget-create-child-value
2405 (widget-create-child widget type)))
2407 (widget-apply child :deactivate)))
2409 (error "Unknown escape `%c'" escape)))))
2410 ;; Update properties.
2412 (widget-put widget :choice type))
2414 (widget-put child :button button)
2415 (widget-put widget :buttons (nconc buttons (list button))))
2417 (widget-put widget :children (nconc children (list child))))
2420 (defun widget-radio-value-get (widget)
2421 ;; Get value of the child widget.
2422 (let ((chosen (widget-radio-chosen widget)))
2423 (and chosen (widget-value chosen))))
2425 (defun widget-radio-chosen (widget)
2426 "Return the widget representing the chosen radio button."
2427 (let ((children (widget-get widget :children))
2430 (setq current (car children)
2431 children (cdr children))
2432 (when (widget-apply (widget-get current :button) :value-get)
2437 (defun widget-radio-value-inline (widget)
2438 ;; Get value of the child widget.
2439 (let ((children (widget-get widget :children))
2442 (setq current (car children)
2443 children (cdr children))
2444 (when (widget-apply (widget-get current :button) :value-get)
2445 (setq found (widget-apply current :value-inline)
2449 (defun widget-radio-value-set (widget value)
2450 ;; We can't just delete and recreate a radio widget, since children
2451 ;; can be added after the original creation and won't be recreated
2453 (let ((children (widget-get widget :children))
2456 (setq current (car children)
2457 children (cdr children))
2458 (let* ((button (widget-get current :button))
2459 (match (and (not found)
2460 (widget-apply current :match value))))
2461 (widget-value-set button match)
2464 (widget-value-set current value)
2465 (widget-apply current :activate))
2466 (widget-apply current :deactivate))
2467 (setq found (or found match))))))
2469 (defun widget-radio-validate (widget)
2470 ;; Valid if we have made a valid choice.
2471 (let ((children (widget-get widget :children))
2472 current found button)
2473 (while (and children (not found))
2474 (setq current (car children)
2475 children (cdr children)
2476 button (widget-get current :button)
2477 found (widget-apply button :value-get)))
2479 (widget-apply current :validate)
2482 (defun widget-radio-action (widget child event)
2483 ;; Check if a radio button was pressed.
2484 (let ((children (widget-get widget :children))
2485 (buttons (widget-get widget :buttons))
2487 (when (memq child buttons)
2489 (setq current (car children)
2490 children (cdr children))
2491 (let* ((button (widget-get current :button)))
2492 (cond ((eq child button)
2493 (widget-value-set button t)
2494 (widget-apply current :activate))
2495 ((widget-value button)
2496 (widget-value-set button nil)
2497 (widget-apply current :deactivate)))))))
2498 ;; Pass notification to parent.
2499 (widget-apply widget :notify child event))
2501 ;;; The `insert-button' Widget.
2503 (define-widget 'insert-button 'push-button
2504 "An insert button for the `editable-list' widget."
2506 :help-echo "Insert a new item into the list at this position."
2507 :action 'widget-insert-button-action)
2509 (defun widget-insert-button-action (widget &optional event)
2510 ;; Ask the parent to insert a new item.
2511 (widget-apply (widget-get widget :parent)
2512 :insert-before (widget-get widget :widget)))
2514 ;;; The `delete-button' Widget.
2516 (define-widget 'delete-button 'push-button
2517 "A delete button for the `editable-list' widget."
2519 :help-echo "Delete this item from the list."
2520 :action 'widget-delete-button-action)
2522 (defun widget-delete-button-action (widget &optional event)
2523 ;; Ask the parent to insert a new item.
2524 (widget-apply (widget-get widget :parent)
2525 :delete-at (widget-get widget :widget)))
2527 ;;; The `editable-list' Widget.
2529 ;; (defcustom widget-editable-list-gui nil
2530 ;; "If non nil, use GUI push-buttons in editable list when available."
2534 (define-widget 'editable-list 'default
2535 "A variable list of widgets of the same type."
2536 :convert-widget 'widget-types-convert-widget
2537 :copy 'widget-types-copy
2540 :format-handler 'widget-editable-list-format-handler
2541 :entry-format "%i %d %v"
2542 :value-create 'widget-editable-list-value-create
2543 :value-get 'widget-editable-list-value-get
2544 :validate 'widget-children-validate
2545 :match 'widget-editable-list-match
2546 :match-inline 'widget-editable-list-match-inline
2547 :insert-before 'widget-editable-list-insert-before
2548 :delete-at 'widget-editable-list-delete-at)
2550 (defun widget-editable-list-format-handler (widget escape)
2551 ;; We recognize the insert button.
2552 ;; (let ((widget-push-button-gui widget-editable-list-gui))
2553 (cond ((eq escape ?i)
2554 (and (widget-get widget :indent)
2555 (insert-char ?\s (widget-get widget :indent)))
2556 (apply 'widget-create-child-and-convert
2557 widget 'insert-button
2558 (widget-get widget :append-button-args)))
2560 (widget-default-format-handler widget escape)))
2564 (defun widget-editable-list-value-create (widget)
2565 ;; Insert all values
2566 (let* ((value (widget-get widget :value))
2567 (type (nth 0 (widget-get widget :args)))
2569 (widget-put widget :value-pos (copy-marker (point)))
2570 (set-marker-insertion-type (widget-get widget :value-pos) t)
2572 (let ((answer (widget-match-inline type value)))
2574 (setq children (cons (widget-editable-list-entry-create
2576 (if (widget-get type :inline)
2583 (widget-put widget :children (nreverse children))))
2585 (defun widget-editable-list-value-get (widget)
2586 ;; Get value of the child widget.
2587 (apply 'append (mapcar (lambda (child) (widget-apply child :value-inline))
2588 (widget-get widget :children))))
2590 (defun widget-editable-list-match (widget value)
2591 ;; Value must be a list and all the members must match the type.
2593 (null (cdr (widget-editable-list-match-inline widget value)))))
2595 (defun widget-editable-list-match-inline (widget value)
2596 (let ((type (nth 0 (widget-get widget :args)))
2599 (while (and value ok)
2600 (let ((answer (widget-match-inline type value)))
2602 (setq found (append found (car answer))
2605 (cons found value)))
2607 (defun widget-editable-list-insert-before (widget before)
2608 ;; Insert a new child in the list of children.
2610 (let ((children (widget-get widget :children))
2611 (inhibit-read-only t)
2612 before-change-functions
2613 after-change-functions)
2615 (goto-char (widget-get before :entry-from)))
2617 (goto-char (widget-get widget :value-pos))))
2618 (let ((child (widget-editable-list-entry-create
2620 (when (< (widget-get child :entry-from) (widget-get widget :from))
2621 (set-marker (widget-get widget :from)
2622 (widget-get child :entry-from)))
2623 (if (eq (car children) before)
2624 (widget-put widget :children (cons child children))
2625 (while (not (eq (car (cdr children)) before))
2626 (setq children (cdr children)))
2627 (setcdr children (cons child (cdr children)))))))
2629 (widget-apply widget :notify widget))
2631 (defun widget-editable-list-delete-at (widget child)
2632 ;; Delete child from list of children.
2634 (let ((buttons (copy-sequence (widget-get widget :buttons)))
2636 (inhibit-read-only t)
2637 before-change-functions
2638 after-change-functions)
2640 (setq button (car buttons)
2641 buttons (cdr buttons))
2642 (when (eq (widget-get button :widget) child)
2644 :buttons (delq button (widget-get widget :buttons)))
2645 (widget-delete button))))
2646 (let ((entry-from (widget-get child :entry-from))
2647 (entry-to (widget-get child :entry-to))
2648 (inhibit-read-only t)
2649 before-change-functions
2650 after-change-functions)
2651 (widget-delete child)
2652 (delete-region entry-from entry-to)
2653 (set-marker entry-from nil)
2654 (set-marker entry-to nil))
2655 (widget-put widget :children (delq child (widget-get widget :children))))
2657 (widget-apply widget :notify widget))
2659 (defun widget-editable-list-entry-create (widget value conv)
2660 ;; Create a new entry to the list.
2661 (let ((type (nth 0 (widget-get widget :args)))
2662 ;; (widget-push-button-gui widget-editable-list-gui)
2663 child delete insert)
2664 (widget-specify-insert
2666 (and (widget-get widget :indent)
2667 (insert-char ?\s (widget-get widget :indent)))
2668 (insert (widget-get widget :entry-format)))
2669 ;; Parse % escapes in format.
2670 (while (re-search-forward "%\\(.\\)" nil t)
2671 (let ((escape (char-after (match-beginning 1))))
2672 (delete-backward-char 2)
2673 (cond ((eq escape ?%)
2676 (setq insert (apply 'widget-create-child-and-convert
2677 widget 'insert-button
2678 (widget-get widget :insert-button-args))))
2680 (setq delete (apply 'widget-create-child-and-convert
2681 widget 'delete-button
2682 (widget-get widget :delete-button-args))))
2685 (setq child (widget-create-child-value
2687 (setq child (widget-create-child-value
2688 widget type (widget-default-get type)))))
2690 (error "Unknown escape `%c'" escape)))))
2691 (let ((buttons (widget-get widget :buttons)))
2692 (if insert (push insert buttons))
2693 (if delete (push delete buttons))
2694 (widget-put widget :buttons buttons))
2695 (let ((entry-from (point-min-marker))
2696 (entry-to (point-max-marker)))
2697 (set-marker-insertion-type entry-from t)
2698 (set-marker-insertion-type entry-to nil)
2699 (widget-put child :entry-from entry-from)
2700 (widget-put child :entry-to entry-to)))
2701 (if insert (widget-put insert :widget child))
2702 (if delete (widget-put delete :widget child))
2705 ;;; The `group' Widget.
2707 (define-widget 'group 'default
2708 "A widget which groups other widgets inside."
2709 :convert-widget 'widget-types-convert-widget
2710 :copy 'widget-types-copy
2712 :value-create 'widget-group-value-create
2713 :value-get 'widget-editable-list-value-get
2714 :default-get 'widget-group-default-get
2715 :validate 'widget-children-validate
2716 :match 'widget-group-match
2717 :match-inline 'widget-group-match-inline)
2719 (defun widget-group-value-create (widget)
2720 ;; Create each component.
2721 (let ((args (widget-get widget :args))
2722 (value (widget-get widget :value))
2723 arg answer children)
2725 (setq arg (car args)
2727 answer (widget-match-inline arg value)
2729 (and (eq (preceding-char) ?\n)
2730 (widget-get widget :indent)
2731 (insert-char ?\s (widget-get widget :indent)))
2732 (push (cond ((null answer)
2733 (widget-create-child widget arg))
2734 ((widget-get arg :inline)
2735 (widget-create-child-value widget arg (car answer)))
2737 (widget-create-child-value widget arg (car (car answer)))))
2739 (widget-put widget :children (nreverse children))))
2741 (defun widget-group-default-get (widget)
2742 ;; Get the default of the components.
2743 (mapcar 'widget-default-get (widget-get widget :args)))
2745 (defun widget-group-match (widget values)
2746 ;; Match if the components match.
2748 (let ((match (widget-group-match-inline widget values)))
2749 (and match (null (cdr match))))))
2751 (defun widget-group-match-inline (widget vals)
2752 ;; Match if the components match.
2753 (let ((args (widget-get widget :args))
2754 argument answer found)
2756 (setq argument (car args)
2758 answer (widget-match-inline argument vals))
2760 (setq vals (cdr answer)
2761 found (append found (car answer)))
2765 (cons found vals))))
2767 ;;; The `visibility' Widget.
2769 (define-widget 'visibility 'item
2770 "An indicator and manipulator for hidden items."
2776 :value-create 'widget-visibility-value-create
2777 :action 'widget-toggle-action
2778 :match (lambda (widget value) t))
2780 (defun widget-visibility-value-create (widget)
2781 ;; Insert text representing the `on' and `off' states.
2782 (let ((on (widget-get widget :on))
2783 (off (widget-get widget :off)))
2785 (setq on (concat widget-push-button-prefix
2787 widget-push-button-suffix))
2790 (setq off (concat widget-push-button-prefix
2792 widget-push-button-suffix))
2794 (if (widget-value widget)
2795 (widget-image-insert widget on "down" "down-pushed")
2796 (widget-image-insert widget off "right" "right-pushed"))))
2798 ;;; The `documentation-link' Widget.
2800 ;; This is a helper widget for `documentation-string'.
2802 (define-widget 'documentation-link 'link
2803 "Link type used in documentation strings."
2805 :help-echo "Describe this symbol"
2806 :action 'widget-documentation-link-action)
2808 (defun widget-documentation-link-action (widget &optional event)
2809 "Display documentation for WIDGET's value. Ignore optional argument EVENT."
2810 (let* ((string (widget-get widget :value))
2811 (symbol (intern string)))
2812 (if (and (fboundp symbol) (boundp symbol))
2813 ;; If there are two doc strings, give the user a way to pick one.
2814 (apropos (concat "\\`" (regexp-quote string) "\\'"))
2815 (if (fboundp symbol)
2816 (describe-function symbol)
2817 (describe-variable symbol)))))
2819 (defcustom widget-documentation-links t
2820 "Add hyperlinks to documentation strings when non-nil."
2822 :group 'widget-documentation)
2824 (defcustom widget-documentation-link-regexp "`\\([^\n`' ]+\\)'"
2825 "Regexp for matching potential links in documentation strings.
2826 The first group should be the link itself."
2828 :group 'widget-documentation)
2830 (defcustom widget-documentation-link-p 'intern-soft
2831 "Predicate used to test if a string is useful as a link.
2832 The value should be a function. The function will be called one
2833 argument, a string, and should return non-nil if there should be a
2834 link for that string."
2836 :options '(widget-documentation-link-p)
2837 :group 'widget-documentation)
2839 (defcustom widget-documentation-link-type 'documentation-link
2840 "Widget type used for links in documentation strings."
2842 :group 'widget-documentation)
2844 (defun widget-documentation-link-add (widget from to)
2845 (widget-specify-doc widget from to)
2846 (when widget-documentation-links
2847 (let ((regexp widget-documentation-link-regexp)
2848 (buttons (widget-get widget :buttons))
2849 (widget-mouse-face (default-value 'widget-mouse-face))
2850 (widget-button-face widget-documentation-face)
2851 (widget-button-pressed-face widget-documentation-face))
2854 (while (re-search-forward regexp to t)
2855 (let ((name (match-string 1))
2856 (begin (match-beginning 1))
2857 (end (match-end 1)))
2858 (when (funcall widget-documentation-link-p name)
2859 (push (widget-convert-button widget-documentation-link-type
2860 begin end :value name)
2862 (widget-put widget :buttons buttons)))
2863 (let ((indent (widget-get widget :indent)))
2864 (when (and indent (not (zerop indent)))
2867 (narrow-to-region from to)
2868 (goto-char (point-min))
2869 (while (search-forward "\n" nil t)
2870 (insert-char ?\s indent)))))))
2872 ;;; The `documentation-string' Widget.
2874 (define-widget 'documentation-string 'item
2875 "A documentation string."
2877 :action 'widget-documentation-string-action
2878 :value-create 'widget-documentation-string-value-create)
2880 (defun widget-documentation-string-value-create (widget)
2881 ;; Insert documentation string.
2882 (let ((doc (widget-value widget))
2883 (indent (widget-get widget :indent))
2884 (shown (widget-get (widget-get widget :parent) :documentation-shown))
2886 (if (string-match "\n" doc)
2887 (let ((before (substring doc 0 (match-beginning 0)))
2888 (after (substring doc (match-beginning 0)))
2891 (widget-documentation-link-add widget start (point))
2893 (widget-create-child-and-convert
2895 :help-echo "Show or hide rest of the documentation."
2899 :action 'widget-parent-action
2902 (setq start (point))
2903 (when (and indent (not (zerop indent)))
2904 (insert-char ?\s indent))
2906 (widget-documentation-link-add widget start (point)))
2907 (widget-put widget :buttons (list button)))
2909 (widget-documentation-link-add widget start (point))))
2912 (defun widget-documentation-string-action (widget &rest ignore)
2913 ;; Toggle documentation.
2914 (let ((parent (widget-get widget :parent)))
2915 (widget-put parent :documentation-shown
2916 (not (widget-get parent :documentation-shown))))
2918 (widget-value-set widget (widget-value widget)))
2920 ;;; The Sexp Widgets.
2922 (define-widget 'const 'item
2923 "An immutable sexp."
2924 :prompt-value 'widget-const-prompt-value
2927 (defun widget-const-prompt-value (widget prompt value unbound)
2928 ;; Return the value of the const.
2929 (widget-value widget))
2931 (define-widget 'function-item 'const
2932 "An immutable function name."
2934 :documentation-property (lambda (symbol)
2936 (documentation symbol t)
2939 (define-widget 'variable-item 'const
2940 "An immutable variable name."
2942 :documentation-property 'variable-documentation)
2944 (define-widget 'other 'sexp
2945 "Matches any value, but doesn't let the user edit the value.
2946 This is useful as last item in a `choice' widget.
2947 You should use this widget type with a default value,
2948 as in (other DEFAULT) or (other :tag \"NAME\" DEFAULT).
2949 If the user selects this alternative, that specifies DEFAULT
2955 (defvar widget-string-prompt-value-history nil
2956 "History of input to `widget-string-prompt-value'.")
2958 (define-widget 'string 'editable-field
2961 :format "%{%t%}: %v"
2962 :complete-function 'ispell-complete-word
2963 :prompt-history 'widget-string-prompt-value-history)
2965 (define-widget 'regexp 'string
2966 "A regular expression."
2967 :match 'widget-regexp-match
2968 :validate 'widget-regexp-validate
2969 ;; Doesn't work well with terminating newline.
2970 ;; :value-face 'widget-single-line-field
2973 (defun widget-regexp-match (widget value)
2974 ;; Match valid regexps.
2975 (and (stringp value)
2978 (string-match value ""))
2981 (defun widget-regexp-validate (widget)
2982 "Check that the value of WIDGET is a valid regexp."
2983 (condition-case data
2985 (string-match (widget-value widget) ""))
2986 (error (widget-put widget :error (error-message-string data))
2989 (define-widget 'file 'string
2991 It reads a file name from an editable text field."
2992 :complete-function 'widget-file-complete
2993 :prompt-value 'widget-file-prompt-value
2994 :format "%{%t%}: %v"
2995 ;; Doesn't work well with terminating newline.
2996 ;; :value-face 'widget-single-line-field
2999 (defun widget-file-complete ()
3000 "Perform completion on file name preceding point."
3002 (let* ((end (point))
3003 (beg (widget-field-start widget))
3004 (pattern (buffer-substring beg end))
3005 (name-part (file-name-nondirectory pattern))
3006 ;; I think defaulting to root is right
3007 ;; because these really should be absolute file names.
3008 (directory (or (file-name-directory pattern) "/"))
3009 (completion (file-name-completion name-part directory)))
3010 (cond ((eq completion t))
3012 (message "Can't find completion for \"%s\"" pattern)
3014 ((not (string= name-part completion))
3015 (delete-region beg end)
3016 (insert (expand-file-name completion directory)))
3018 (message "Making completion list...")
3019 (with-output-to-temp-buffer "*Completions*"
3020 (display-completion-list
3021 (sort (file-name-all-completions name-part directory)
3024 (message "Making completion list...%s" "done")))))
3026 (defun widget-file-prompt-value (widget prompt value unbound)
3027 ;; Read file from minibuffer.
3028 (abbreviate-file-name
3030 (read-file-name prompt)
3031 (let ((prompt2 (format "%s (default %s): " prompt value))
3032 (dir (file-name-directory value))
3033 (file (file-name-nondirectory value))
3034 (must-match (widget-get widget :must-match)))
3035 (read-file-name prompt2 dir nil must-match file)))))
3037 ;;;(defun widget-file-action (widget &optional event)
3038 ;;; ;; Read a file name from the minibuffer.
3039 ;;; (let* ((value (widget-value widget))
3040 ;;; (dir (file-name-directory value))
3041 ;;; (file (file-name-nondirectory value))
3042 ;;; (menu-tag (widget-apply widget :menu-tag-get))
3043 ;;; (must-match (widget-get widget :must-match))
3044 ;;; (answer (read-file-name (concat menu-tag " (default " value "): ")
3045 ;;; dir nil must-match file)))
3046 ;;; (widget-value-set widget (abbreviate-file-name answer))
3048 ;;; (widget-apply widget :notify widget event)))
3050 ;; Fixme: use file-name-as-directory.
3051 (define-widget 'directory 'file
3052 "A directory widget.
3053 It reads a directory name from an editable text field."
3056 (defvar widget-symbol-prompt-value-history nil
3057 "History of input to `widget-symbol-prompt-value'.")
3059 (define-widget 'symbol 'editable-field
3063 :format "%{%t%}: %v"
3064 :match (lambda (widget value) (symbolp value))
3065 :complete-function 'lisp-complete-symbol
3066 :prompt-internal 'widget-symbol-prompt-internal
3067 :prompt-match 'symbolp
3068 :prompt-history 'widget-symbol-prompt-value-history
3069 :value-to-internal (lambda (widget value)
3073 :value-to-external (lambda (widget value)
3078 (defun widget-symbol-prompt-internal (widget prompt initial history)
3079 ;; Read file from minibuffer.
3080 (let ((answer (completing-read prompt obarray
3081 (widget-get widget :prompt-match)
3082 nil initial history)))
3083 (if (and (stringp answer)
3084 (not (zerop (length answer))))
3086 (error "No value"))))
3088 (defvar widget-function-prompt-value-history nil
3089 "History of input to `widget-function-prompt-value'.")
3091 (define-widget 'function 'restricted-sexp
3093 :complete-function (lambda ()
3095 (lisp-complete-symbol 'fboundp))
3096 :prompt-value 'widget-field-prompt-value
3097 :prompt-internal 'widget-symbol-prompt-internal
3098 :prompt-match 'fboundp
3099 :prompt-history 'widget-function-prompt-value-history
3100 :action 'widget-field-action
3101 :match-alternatives '(functionp)
3102 :validate (lambda (widget)
3103 (unless (functionp (widget-value widget))
3104 (widget-put widget :error (format "Invalid function: %S"
3105 (widget-value widget)))
3110 (defvar widget-variable-prompt-value-history nil
3111 "History of input to `widget-variable-prompt-value'.")
3113 (define-widget 'variable 'symbol
3115 :prompt-match 'boundp
3116 :prompt-history 'widget-variable-prompt-value-history
3117 :complete-function (lambda ()
3119 (lisp-complete-symbol 'boundp))
3122 (defvar widget-coding-system-prompt-value-history nil
3123 "History of input to `widget-coding-system-prompt-value'.")
3125 (define-widget 'coding-system 'symbol
3126 "A MULE coding-system."
3127 :format "%{%t%}: %v"
3128 :tag "Coding system"
3130 :prompt-history 'widget-coding-system-prompt-value-history
3131 :prompt-value 'widget-coding-system-prompt-value
3132 :action 'widget-coding-system-action
3133 :complete-function (lambda ()
3135 (lisp-complete-symbol 'coding-system-p))
3136 :validate (lambda (widget)
3137 (unless (coding-system-p (widget-value widget))
3138 (widget-put widget :error (format "Invalid coding system: %S"
3139 (widget-value widget)))
3142 :prompt-match 'coding-system-p)
3144 (defun widget-coding-system-prompt-value (widget prompt value unbound)
3145 "Read coding-system from minibuffer."
3146 (if (widget-get widget :base-only)
3148 (completing-read (format "%s (default %s): " prompt value)
3149 (mapcar #'list (coding-system-list t)) nil nil nil
3150 coding-system-history))
3151 (read-coding-system (format "%s (default %s): " prompt value) value)))
3153 (defun widget-coding-system-action (widget &optional event)
3155 (widget-coding-system-prompt-value
3157 (widget-apply widget :menu-tag-get)
3158 (widget-value widget)
3160 (widget-value-set widget answer)
3161 (widget-apply widget :notify widget event)
3164 (defvar widget-key-sequence-prompt-value-history nil
3165 "History of input to `widget-key-sequence-prompt-value'.")
3167 ;; This mostly works, but I am pretty sure it needs more change
3168 ;; to be 100% correct. I don't know what the change should be -- rms.
3170 (define-widget 'key-sequence 'restricted-sexp
3172 :prompt-value 'widget-field-prompt-value
3173 :prompt-internal 'widget-symbol-prompt-internal
3174 :prompt-match 'fboundp
3175 :prompt-history 'widget-key-sequence-prompt-value-history
3176 :action 'widget-field-action
3177 :match-alternatives '(stringp vectorp)
3178 :validate (lambda (widget)
3179 (unless (or (stringp (widget-value widget))
3180 (vectorp (widget-value widget)))
3181 (widget-put widget :error (format "Invalid key sequence: %S"
3182 (widget-value widget)))
3185 :tag "Key sequence")
3187 (define-widget 'sexp 'editable-field
3188 "An arbitrary Lisp expression."
3189 :tag "Lisp expression"
3190 :format "%{%t%}: %v"
3192 :validate 'widget-sexp-validate
3193 :match (lambda (widget value) t)
3194 :value-to-internal 'widget-sexp-value-to-internal
3195 :value-to-external (lambda (widget value) (read value))
3196 :prompt-history 'widget-sexp-prompt-value-history
3197 :prompt-value 'widget-sexp-prompt-value)
3199 (defun widget-sexp-value-to-internal (widget value)
3200 ;; Use pp for printer representation.
3201 (let ((pp (if (symbolp value)
3202 (prin1-to-string value)
3203 (pp-to-string value))))
3204 (while (string-match "\n\\'" pp)
3205 (setq pp (substring pp 0 -1)))
3206 (if (or (string-match "\n\\'" pp)
3211 (defun widget-sexp-validate (widget)
3212 ;; Valid if we can read the string and there is no junk left after it.
3214 (insert (widget-apply widget :value-get))
3215 (goto-char (point-min))
3217 (condition-case data
3219 ;; Avoid a confusing end-of-file error.
3220 (skip-syntax-forward "\\s-")
3222 (setq err "Empty sexp -- use `nil'?")
3223 (unless (widget-apply widget :match (read (current-buffer)))
3224 (setq err (widget-get widget :type-error))))
3225 ;; Allow whitespace after expression.
3226 (skip-syntax-forward "\\s-")
3227 (if (and (not (eobp))
3229 (setq err (format "Junk at end of expression: %s"
3230 (buffer-substring (point)
3232 (end-of-file ; Avoid confusing error message.
3233 (setq err "Unbalanced sexp"))
3234 (error (setq err (error-message-string data))))
3237 (widget-put widget :error err)
3240 (defvar widget-sexp-prompt-value-history nil
3241 "History of input to `widget-sexp-prompt-value'.")
3243 (defun widget-sexp-prompt-value (widget prompt value unbound)
3244 ;; Read an arbitrary sexp.
3245 (let ((found (read-string prompt
3246 (if unbound nil (cons (prin1-to-string value) 0))
3247 (widget-get widget :prompt-history))))
3248 (let ((answer (read-from-string found)))
3249 (unless (= (cdr answer) (length found))
3250 (error "Junk at end of expression: %s"
3251 (substring found (cdr answer))))
3254 (define-widget 'restricted-sexp 'sexp
3255 "A Lisp expression restricted to values that match.
3256 To use this type, you must define :match or :match-alternatives."
3257 :type-error "The specified value is not valid"
3258 :match 'widget-restricted-sexp-match
3259 :value-to-internal (lambda (widget value)
3260 (if (widget-apply widget :match value)
3261 (prin1-to-string value)
3264 (defun widget-restricted-sexp-match (widget value)
3265 (let ((alternatives (widget-get widget :match-alternatives))
3267 (while (and alternatives (not matched))
3268 (if (cond ((functionp (car alternatives))
3269 (funcall (car alternatives) value))
3270 ((and (consp (car alternatives))
3271 (eq (car (car alternatives)) 'quote))
3272 (eq value (nth 1 (car alternatives)))))
3274 (setq alternatives (cdr alternatives)))
3277 (define-widget 'integer 'restricted-sexp
3281 :type-error "This field should contain an integer"
3282 :match-alternatives '(integerp))
3284 (define-widget 'number 'restricted-sexp
3285 "A number (floating point or integer)."
3288 :type-error "This field should contain a number (floating point or integer)"
3289 :match-alternatives '(numberp))
3291 (define-widget 'float 'restricted-sexp
3292 "A floating point number."
3293 :tag "Floating point number"
3295 :type-error "This field should contain a floating point number"
3296 :match-alternatives '(floatp))
3298 (define-widget 'character 'editable-field
3303 :format "%{%t%}: %v\n"
3304 :valid-regexp "\\`.\\'"
3305 :error "This field should contain a single character"
3306 :value-to-internal (lambda (widget value)
3309 (char-to-string value)))
3310 :value-to-external (lambda (widget value)
3314 :match (lambda (widget value)
3315 (char-valid-p value)))
3317 (define-widget 'list 'group
3320 :format "%{%t%}:\n%v")
3322 (define-widget 'vector 'group
3325 :format "%{%t%}:\n%v"
3326 :match 'widget-vector-match
3327 :value-to-internal (lambda (widget value) (append value nil))
3328 :value-to-external (lambda (widget value) (apply 'vector value)))
3330 (defun widget-vector-match (widget value)
3331 (and (vectorp value)
3332 (widget-group-match widget
3333 (widget-apply widget :value-to-internal value))))
3335 (define-widget 'cons 'group
3338 :format "%{%t%}:\n%v"
3339 :match 'widget-cons-match
3340 :value-to-internal (lambda (widget value)
3341 (list (car value) (cdr value)))
3342 :value-to-external (lambda (widget value)
3343 (apply 'cons value)))
3345 (defun widget-cons-match (widget value)
3347 (widget-group-match widget
3348 (widget-apply widget :value-to-internal value))))
3350 ;;; The `lazy' Widget.
3352 ;; Recursive datatypes.
3354 (define-widget 'lazy 'default
3355 "Base widget for recursive datastructures.
3357 The `lazy' widget will, when instantiated, contain a single inferior
3358 widget, of the widget type specified by the :type parameter. The
3359 value of the `lazy' widget is the same as the value of the inferior
3360 widget. When deriving a new widget from the 'lazy' widget, the :type
3361 parameter is allowed to refer to the widget currently being defined,
3362 thus allowing recursive datastructures to be described.
3364 The :type parameter takes the same arguments as the defcustom
3365 parameter with the same name.
3367 Most composite widgets, i.e. widgets containing other widgets, does
3368 not allow recursion. That is, when you define a new widget type, none
3369 of the inferior widgets may be of the same type you are currently
3372 In Lisp, however, it is custom to define datastructures in terms of
3373 themselves. A list, for example, is defined as either nil, or a cons
3374 cell whose cdr itself is a list. The obvious way to translate this
3375 into a widget type would be
3377 (define-widget 'my-list 'choice
3378 \"A list of sexps.\"
3380 :args '((const nil) (cons :value (nil) sexp my-list)))
3382 Here we attempt to define my-list as a choice of either the constant
3383 nil, or a cons-cell containing a sexp and my-lisp. This will not work
3384 because the `choice' widget does not allow recursion.
3386 Using the `lazy' widget you can overcome this problem, as in this
3389 (define-widget 'sexp-list 'lazy
3390 \"A list of sexps.\"
3392 :type '(choice (const nil) (cons :value (nil) sexp sexp-list)))"
3393 :format "%{%t%}: %v"
3394 ;; We don't convert :type because we want to allow recursive
3395 ;; datastructures. This is slow, so we should not create speed
3396 ;; critical widgets by deriving from this.
3397 :convert-widget 'widget-value-convert-widget
3398 :value-create 'widget-type-value-create
3399 :value-get 'widget-child-value-get
3400 :value-inline 'widget-child-value-inline
3401 :default-get 'widget-type-default-get
3402 :match 'widget-type-match
3403 :validate 'widget-child-validate)
3406 ;;; The `plist' Widget.
3410 (define-widget 'plist 'list
3412 :key-type '(symbol :tag "Key")
3413 :value-type '(sexp :tag "Value")
3414 :convert-widget 'widget-plist-convert-widget
3417 (defvar widget-plist-value-type) ;Dynamic variable
3419 (defun widget-plist-convert-widget (widget)
3420 ;; Handle `:options'.
3421 (let* ((options (widget-get widget :options))
3422 (widget-plist-value-type (widget-get widget :value-type))
3423 (other `(editable-list :inline t
3425 ,(widget-get widget :key-type)
3426 ,widget-plist-value-type)))
3428 (list `(checklist :inline t
3430 ,@(mapcar 'widget-plist-convert-option
3434 (widget-put widget :args args)
3437 (defun widget-plist-convert-option (option)
3438 ;; Convert a single plist option.
3439 (let (key-type value-type)
3441 (let ((key (nth 0 option)))
3442 (setq value-type (nth 1 option))
3445 (setq key-type `(const ,key))))
3446 (setq key-type `(const ,option)
3447 value-type widget-plist-value-type))
3448 `(group :format "Key: %v" :inline t ,key-type ,value-type)))
3451 ;;; The `alist' Widget.
3453 ;; Association lists.
3455 (define-widget 'alist 'list
3456 "An association list."
3457 :key-type '(sexp :tag "Key")
3458 :value-type '(sexp :tag "Value")
3459 :convert-widget 'widget-alist-convert-widget
3462 (defvar widget-alist-value-type) ;Dynamic variable
3464 (defun widget-alist-convert-widget (widget)
3465 ;; Handle `:options'.
3466 (let* ((options (widget-get widget :options))
3467 (widget-alist-value-type (widget-get widget :value-type))
3468 (other `(editable-list :inline t
3470 ,(widget-get widget :key-type)
3471 ,widget-alist-value-type)))
3473 (list `(checklist :inline t
3475 ,@(mapcar 'widget-alist-convert-option
3479 (widget-put widget :args args)
3482 (defun widget-alist-convert-option (option)
3483 ;; Convert a single alist option.
3484 (let (key-type value-type)
3486 (let ((key (nth 0 option)))
3487 (setq value-type (nth 1 option))
3490 (setq key-type `(const ,key))))
3491 (setq key-type `(const ,option)
3492 value-type widget-alist-value-type))
3493 `(cons :format "Key: %v" ,key-type ,value-type)))
3495 (define-widget 'choice 'menu-choice
3496 "A union of several sexp types."
3498 :format "%{%t%}: %[Value Menu%] %v"
3499 :button-prefix 'widget-push-button-prefix
3500 :button-suffix 'widget-push-button-suffix
3501 :prompt-value 'widget-choice-prompt-value)
3503 (defun widget-choice-prompt-value (widget prompt value unbound)
3505 (let ((args (widget-get widget :args))
3506 (completion-ignore-case (widget-get widget :case-fold))
3507 current choices old)
3508 ;; Find the first arg that matches VALUE.
3511 (if (widget-apply (car look) :match value)
3512 (setq old (car look)
3514 (setq look (cdr look)))))
3517 (cond ((= (length args) 0)
3519 ((= (length args) 1)
3521 ((and (= (length args) 2)
3523 (if (eq old (nth 0 args))
3528 (setq current (car args)
3531 (cons (cons (widget-apply current :menu-tag-get)
3534 (let ((val (completing-read prompt choices nil t)))
3536 (let ((try (try-completion val choices)))
3539 (cdr (assoc val choices)))
3542 (widget-prompt-value current prompt nil t)
3545 (define-widget 'radio 'radio-button-choice
3546 "A union of several sexp types."
3548 :format "%{%t%}:\n%v"
3549 :prompt-value 'widget-choice-prompt-value)
3551 (define-widget 'repeat 'editable-list
3552 "A variable length homogeneous list."
3554 :format "%{%t%}:\n%v%i\n")
3556 (define-widget 'set 'checklist
3557 "A list of members from a fixed set."
3559 :format "%{%t%}:\n%v")
3561 (define-widget 'boolean 'toggle
3562 "To be nil or non-nil, that is the question."
3564 :prompt-value 'widget-boolean-prompt-value
3565 :button-prefix 'widget-push-button-prefix
3566 :button-suffix 'widget-push-button-suffix
3567 :format "%{%t%}: %[Toggle%] %v\n"
3571 (defun widget-boolean-prompt-value (widget prompt value unbound)
3572 ;; Toggle a boolean.
3575 ;;; The `color' Widget.
3578 (define-widget 'color 'editable-field
3579 "Choose a color name (with sample)."
3580 :format "%{%t%}: %v (%{sample%})\n"
3584 :complete 'widget-color-complete
3585 :sample-face-get 'widget-color-sample-face-get
3586 :notify 'widget-color-notify
3587 :action 'widget-color-action)
3589 (defun widget-color-complete (widget)
3590 "Complete the color in WIDGET."
3591 (require 'facemenu) ; for facemenu-color-alist
3592 (let* ((prefix (buffer-substring-no-properties (widget-field-start widget)
3594 (list (or facemenu-color-alist (defined-colors)))
3595 (completion (try-completion prefix list)))
3596 (cond ((eq completion t)
3597 (message "Exact match."))
3599 (error "Can't find completion for \"%s\"" prefix))
3600 ((not (string-equal prefix completion))
3601 (insert-and-inherit (substring completion (length prefix))))
3603 (message "Making completion list...")
3604 (with-output-to-temp-buffer "*Completions*"
3605 (display-completion-list (all-completions prefix list nil)
3607 (message "Making completion list...done")))))
3609 (defun widget-color-sample-face-get (widget)
3610 (let* ((value (condition-case nil
3611 (widget-value widget)
3612 (error (widget-get widget :value)))))
3613 (if (color-defined-p value)
3614 (list (cons 'foreground-color value))
3617 (defun widget-color-action (widget &optional event)
3618 "Prompt for a color."
3619 (let* ((tag (widget-apply widget :menu-tag-get))
3620 (prompt (concat tag ": "))
3621 (value (widget-value widget))
3622 (start (widget-field-start widget))
3623 (answer (facemenu-read-color prompt)))
3624 (unless (zerop (length answer))
3625 (widget-value-set widget answer)
3627 (widget-apply widget :notify widget event))))
3629 (defun widget-color-notify (widget child &optional event)
3630 "Update the sample, and notify the parent."
3631 (overlay-put (widget-get widget :sample-overlay)
3632 'face (widget-apply widget :sample-face-get))
3633 (widget-default-notify widget child event))
3637 (defun widget-echo-help (pos)
3638 "Display help-echo text for widget at POS."
3639 (let* ((widget (widget-at pos))
3640 (help-echo (and widget (widget-get widget :help-echo))))
3641 (if (functionp help-echo)
3642 (setq help-echo (funcall help-echo widget)))
3643 (if help-echo (message "%s" (eval help-echo)))))
3649 ;;; arch-tag: a076e75e-18a1-4b46-8be5-3f317bcbc707
3650 ;;; wid-edit.el ends here