1 ;;; wid-edit.el --- Functions for creating and using widgets -*-byte-compile-dynamic: t;-*-
3 ;; Copyright (C) 1996, 1997, 1999, 2000 Free Software Foundation, Inc.
5 ;; Author: Per Abrahamsen <abraham@dina.kvl.dk>
7 ;; Keywords: extensions
9 ;; This file is part of GNU Emacs.
11 ;; GNU Emacs is free software; you can redistribute it and/or modify
12 ;; it under the terms of the GNU General Public License as published by
13 ;; the Free Software Foundation; either version 2, or (at your option)
16 ;; GNU Emacs is distributed in the hope that it will be useful,
17 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
18 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 ;; GNU General Public License for more details.
21 ;; You should have received a copy of the GNU General Public License
22 ;; along with GNU Emacs; see the file COPYING. If not, write to the
23 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
24 ;; Boston, MA 02111-1307, USA.
26 ;;; Wishlist items (from widget.texi):
28 ;; * The `menu-choice' tag should be prettier, something like the
29 ;; abbreviated menus in Open Look.
31 ;; * Finish `:tab-order'.
33 ;; * Make indentation work with glyphs and proportional fonts.
35 ;; * Add commands to show overview of object and class hierarchies to
38 ;; * Find a way to disable mouse highlight for inactive widgets.
40 ;; * Find a way to make glyphs look inactive.
42 ;; * Add `key-binding' widget.
44 ;; * Add `widget' widget for editing widget specifications.
46 ;; * Find clean way to implement variable length list. See
47 ;; `TeX-printer-list' for an explanation.
49 ;; * `C-h' in `widget-prompt-value' should give type specific help.
51 ;; * A mailto widget. [This should work OK as a url-link if with
52 ;; browse-url-browser-function' set up appropriately.]
62 (defun widget-event-point (event)
63 "Character position of the end of event if that exists, or nil."
64 (posn-point (event-end event
)))
66 (autoload 'pp-to-string
"pp")
67 (autoload 'Info-goto-node
"info")
69 (defun widget-button-release-event-p (event)
70 "Non-nil if EVENT is a mouse-button-release event object."
72 (memq (event-basic-type event
) '(mouse-1 mouse-2 mouse-3
))
73 (or (memq 'click
(event-modifiers event
))
74 (memq 'drag
(event-modifiers event
)))))
79 "Customization support for the Widget Library."
80 :link
'(custom-manual "(widget)Top")
81 :link
'(emacs-library-link :tag
"Lisp File" "widget.el")
86 (defgroup widget-documentation nil
87 "Options controling the display of documentation strings."
90 (defgroup widget-faces nil
91 "Faces used by the widget library."
95 (defvar widget-documentation-face
'widget-documentation-face
96 "Face used for documentation strings in widgets.
97 This exists as a variable so it can be set locally in certain buffers.")
99 (defface widget-documentation-face
'((((class color
)
101 (:foreground
"lime green"))
104 (:foreground
"dark green"))
106 "Face used for documentation text."
107 :group
'widget-documentation
108 :group
'widget-faces
)
110 (defvar widget-button-face
'widget-button-face
111 "Face used for buttons in widgets.
112 This exists as a variable so it can be set locally in certain buffers.")
114 (defface widget-button-face
'((t (:bold t
)))
115 "Face used for widget buttons."
116 :group
'widget-faces
)
118 (defcustom widget-mouse-face
'highlight
119 "Face used for widget buttons when the mouse is above them."
121 :group
'widget-faces
)
123 (defface widget-field-face
'((((class grayscale color
)
125 (:background
"gray85"))
126 (((class grayscale color
)
128 (:background
"dim gray"))
131 "Face used for editable fields."
132 :group
'widget-faces
)
134 (defface widget-single-line-field-face
'((((class grayscale color
)
136 (:background
"gray85"))
137 (((class grayscale color
)
139 (:background
"dim gray"))
142 "Face used for editable fields spanning only a single line."
143 :group
'widget-faces
)
145 ;;; This causes display-table to be loaded, and not usefully.
146 ;;;(defvar widget-single-line-display-table
147 ;;; (let ((table (make-display-table)))
148 ;;; (aset table 9 "^I")
149 ;;; (aset table 10 "^J")
151 ;;; "Display table used for single-line editable fields.")
153 ;;;(when (fboundp 'set-face-display-table)
154 ;;; (set-face-display-table 'widget-single-line-field-face
155 ;;; widget-single-line-display-table))
157 ;;; Utility functions.
159 ;; These are not really widget specific.
161 (defun widget-princ-to-string (object)
162 "Return string representation of OBJECT, any Lisp object.
163 No quoting characters are used; no delimiters are printed around
164 the contents of strings."
165 (with-output-to-string
168 (defun widget-clear-undo ()
169 "Clear all undo information."
170 (buffer-disable-undo (current-buffer))
171 (buffer-enable-undo))
173 (defcustom widget-menu-max-size
40
174 "Largest number of items allowed in a popup-menu.
175 Larger menus are read through the minibuffer."
179 (defcustom widget-menu-max-shortcuts
40
180 "Largest number of items for which it works to choose one with a character.
181 For a larger number of items, the minibuffer is used."
185 (defcustom widget-menu-minibuffer-flag nil
186 "*Control how to ask for a choice from the keyboard.
187 Non-nil means use the minibuffer;
188 nil means read a single character."
192 (defun widget-choose (title items
&optional event
)
193 "Choose an item from a list.
195 First argument TITLE is the name of the list.
196 Second argument ITEMS is an list whose members are either
197 (NAME . VALUE), to indicate selectable items, or just strings to
198 indicate unselectable items.
199 Optional third argument EVENT is an input event.
201 The user is asked to choose between each NAME from the items alist,
202 and the VALUE of the chosen element will be returned. If EVENT is a
203 mouse event, and the number of elements in items is less than
204 `widget-menu-max-size', a popup menu will be used, otherwise the
206 (cond ((and (< (length items
) widget-menu-max-size
)
207 event
(display-mouse-p))
210 (list title
(cons "" items
))))
211 ((or widget-menu-minibuffer-flag
212 (> (length items
) widget-menu-max-shortcuts
))
213 ;; Read the choice of name from the minibuffer.
214 (setq items
(widget-remove-if 'stringp items
))
215 (let ((val (completing-read (concat title
": ") items nil t
)))
217 (let ((try (try-completion val items
)))
220 (cdr (assoc val items
))))))
222 ;; Construct a menu of the choices
223 ;; and then use it for prompting for a single character.
224 (let* ((overriding-terminal-local-map (make-sparse-keymap))
226 map choice some-choice-enabled value
)
227 ;; Define SPC as a prefix char to get to this menu.
228 (define-key overriding-terminal-local-map
" "
229 (setq map
(make-sparse-keymap title
)))
231 (set-buffer (get-buffer-create " widget-choose"))
233 (insert "Available choices:\n\n")
235 (setq choice
(car items
) items
(cdr items
))
237 (let* ((name (car choice
))
238 (function (cdr choice
)))
239 (insert (format "%c = %s\n" next-digit name
))
240 (define-key map
(vector next-digit
) function
)
241 (setq some-choice-enabled t
)))
242 ;; Allocate digits to disabled alternatives
243 ;; so that the digit of a given alternative never varies.
244 (setq next-digit
(1+ next-digit
)))
245 (insert "\nC-g = Quit"))
246 (or some-choice-enabled
247 (error "None of the choices is currently meaningful"))
248 (define-key map
[?\C-g
] 'keyboard-quit
)
249 (define-key map
[t] 'keyboard-quit)
250 (define-key map [?\M-\C-v] 'scroll-other-window)
251 (define-key map [?\M--] 'negative-argument)
252 (setcdr map (nreverse (cdr map)))
253 ;; Read a char with the menu, and return the result
254 ;; that corresponds to it.
255 (save-window-excursion
256 (let ((buf (get-buffer " widget-choose")))
258 (let ((cursor-in-echo-area t)
262 (while (not (or (and (>= char ?0) (< char next-digit))
263 (eq value 'keyboard-quit)))
264 ;; Unread a SPC to lead to our new menu.
265 (setq unread-command-events (cons ?\ unread-command-events))
266 (setq keys (read-key-sequence title))
268 (lookup-key overriding-terminal-local-map keys t)
269 char (string-to-char (substring keys 1)))
270 (cond ((eq value 'scroll-other-window)
271 (let ((minibuffer-scroll-window
272 (get-buffer-window buf)))
274 (scroll-other-window-down
275 (window-height minibuffer-scroll-window))
276 (scroll-other-window))
278 ((eq value 'negative-argument)
282 (when (eq value 'keyboard-quit)
286 (defun widget-remove-if (predictate list)
287 (let (result (tail list))
289 (or (funcall predictate (car tail))
290 (setq result (cons (car tail) result)))
291 (setq tail (cdr tail)))
294 ;;; Widget text specifications.
296 ;; These functions are for specifying text properties.
298 (defvar widget-field-add-space t
299 "Non-nil means add extra space at the end of editable text fields.
300 If you don't add the space, it will become impossible to edit a zero
303 (defvar widget-field-use-before-change t
304 "Non-nil means use `before-change-functions' to track editable fields.
305 This enables the use of undo, but doesn't work on Emacs 19.34 and earlier.
306 Using before hooks also means that the :notify function can't know the
309 (defun widget-specify-field (widget from to)
310 "Specify editable button for WIDGET between FROM and TO."
311 ;; Terminating space is not part of the field, but necessary in
312 ;; order for local-map to work. Remove next sexp if local-map works
313 ;; at the end of the overlay.
316 (cond ((null (widget-get widget :size))
318 (widget-field-add-space
319 (insert-and-inherit " ")))
321 (let ((keymap (widget-get widget :keymap))
322 (face (or (widget-get widget :value-face) 'widget-field-face))
323 (help-echo (widget-get widget :help-echo))
325 (or (not widget-field-add-space) (widget-get widget :size))))
326 (if (functionp help-echo)
327 (setq help-echo 'widget-mouse-help))
328 (when (= (char-before to) ?\n)
329 ;; When the last character in the field is a newline, we want to
330 ;; give it a `field' char-property of `boundary', which helps the
331 ;; C-n/C-p act more naturally when entering/leaving the field. We
332 ;; do this by making a small secondary overlay to contain just that
334 (let ((overlay (make-overlay (1- to) to nil t nil)))
335 (overlay-put overlay 'field 'boundary)
336 ;; Use `local-map' here, not `keymap', so that normal editing
337 ;; works in the field when, say, Custom uses `suppress-keymap'.
338 (overlay-put overlay 'local-map keymap)
339 (overlay-put overlay 'face face)
340 (overlay-put overlay 'help-echo help-echo))
342 (setq rear-sticky t))
343 (let ((overlay (make-overlay from to nil nil rear-sticky)))
344 (widget-put widget :field-overlay overlay)
345 ;;(overlay-put overlay 'detachable nil)
346 (overlay-put overlay 'field widget)
347 (overlay-put overlay 'local-map keymap)
348 (overlay-put overlay 'face face)
349 (overlay-put overlay 'help-echo help-echo)))
350 (widget-specify-secret widget))
352 (defun widget-specify-secret (field)
353 "Replace text in FIELD with value of `:secret', if non-nil."
354 (let ((secret (widget-get field :secret))
355 (size (widget-get field :size)))
357 (let ((begin (widget-field-start field))
358 (end (widget-field-end field)))
360 (while (and (> end begin)
361 (eq (char-after (1- end)) ?\ ))
362 (setq end (1- end))))
364 (let ((old (char-after begin)))
365 (unless (eq old secret)
366 (subst-char-in-region begin (1+ begin) old secret)
367 (put-text-property begin (1+ begin) 'secret old))
368 (setq begin (1+ begin))))))))
370 (defun widget-specify-button (widget from to)
371 "Specify button for WIDGET between FROM and TO."
372 (let ((overlay (make-overlay from to nil t nil))
373 (help-echo (widget-get widget :help-echo)))
374 (widget-put widget :button-overlay overlay)
375 (if (functionp help-echo)
376 (setq help-echo 'widget-mouse-help))
377 (overlay-put overlay 'button widget)
378 (overlay-put overlay 'keymap (widget-get widget :keymap))
379 ;; We want to avoid the face with image buttons.
380 (unless (widget-get widget :suppress-face)
381 (overlay-put overlay 'face (widget-apply widget :button-face-get))
382 (overlay-put overlay 'mouse-face widget-mouse-face))
383 (overlay-put overlay 'help-echo help-echo)))
385 (defun widget-mouse-help (window overlay point)
386 "Help-echo callback for widgets whose :help-echo is a function."
387 (with-current-buffer (overlay-buffer overlay)
388 (let* ((widget (widget-at (overlay-start overlay)))
389 (help-echo (if widget (widget-get widget :help-echo))))
390 (if (functionp help-echo)
391 (funcall help-echo widget)
394 (defun widget-specify-sample (widget from to)
395 "Specify sample for WIDGET between FROM and TO."
396 (let ((overlay (make-overlay from to nil t nil)))
397 (overlay-put overlay 'face (widget-apply widget :sample-face-get))
398 (widget-put widget :sample-overlay overlay)))
400 (defun widget-specify-doc (widget from to)
401 "Specify documentation for WIDGET between FROM and TO."
402 (let ((overlay (make-overlay from to nil t nil)))
403 (overlay-put overlay 'widget-doc widget)
404 (overlay-put overlay 'face widget-documentation-face)
405 (widget-put widget :doc-overlay overlay)))
407 (defmacro widget-specify-insert (&rest form)
408 "Execute FORM without inheriting any text properties."
410 (let ((inhibit-read-only t)
411 (inhibit-modification-hooks t)
414 (narrow-to-region (- (point) 2) (point))
415 (goto-char (1+ (point-min)))
416 (setq result (progn ,@form))
417 (delete-region (point-min) (1+ (point-min)))
418 (delete-region (1- (point-max)) (point-max))
419 (goto-char (point-max))
422 (defface widget-inactive-face '((((class grayscale color)
424 (:foreground "light gray"))
425 (((class grayscale color)
427 (:foreground "dim gray"))
430 "Face used for inactive widgets."
431 :group 'widget-faces)
433 (defun widget-specify-inactive (widget from to)
434 "Make WIDGET inactive for user modifications."
435 (unless (widget-get widget :inactive)
436 (let ((overlay (make-overlay from to nil t nil)))
437 (overlay-put overlay 'face 'widget-inactive-face)
438 ;; This is disabled, as it makes the mouse cursor change shape.
439 ;; (overlay-put overlay 'mouse-face 'widget-inactive-face)
440 (overlay-put overlay 'evaporate t)
441 (overlay-put overlay 'priority 100)
442 (overlay-put overlay 'modification-hooks '(widget-overlay-inactive))
443 (widget-put widget :inactive overlay))))
445 (defun widget-overlay-inactive (&rest junk)
446 "Ignoring the arguments, signal an error."
447 (unless inhibit-read-only
448 (error "The widget here is not active")))
451 (defun widget-specify-active (widget)
452 "Make WIDGET active for user modifications."
453 (let ((inactive (widget-get widget :inactive)))
455 (delete-overlay inactive)
456 (widget-put widget :inactive nil))))
458 ;;; Widget Properties.
460 (defsubst widget-type (widget)
461 "Return the type of WIDGET, a symbol."
464 (defun widget-get-indirect (widget property)
465 "In WIDGET, get the value of PROPERTY.
466 If the value is a symbol, return its binding.
467 Otherwise, just return the value."
468 (let ((value (widget-get widget property)))
473 (defun widget-member (widget property)
474 "Non-nil iff there is a definition in WIDGET for PROPERTY."
475 (cond ((plist-member (cdr widget) property)
478 (widget-member (get (car widget) 'widget-type) property))
481 (defun widget-value (widget)
482 "Extract the current value of WIDGET."
484 :value-to-external (widget-apply widget :value-get)))
486 (defun widget-value-set (widget value)
487 "Set the current value of WIDGET to VALUE."
489 :value-set (widget-apply widget
490 :value-to-internal value)))
492 (defun widget-default-get (widget)
493 "Extract the default value of WIDGET."
494 (or (widget-get widget :value)
495 (widget-apply widget :default-get)))
497 (defun widget-match-inline (widget vals)
498 "In WIDGET, match the start of VALS."
499 (cond ((widget-get widget :inline)
500 (widget-apply widget :match-inline vals))
502 (widget-apply widget :match (car vals)))
503 (cons (list (car vals)) (cdr vals)))
506 (defun widget-apply-action (widget &optional event)
507 "Apply :action in WIDGET in response to EVENT."
508 (if (widget-apply widget :active)
509 (widget-apply widget :action event)
510 (error "Attempt to perform action on inactive widget")))
512 ;;; Helper functions.
514 ;; These are widget specific.
517 (defun widget-prompt-value (widget prompt &optional value unbound)
518 "Prompt for a value matching WIDGET, using PROMPT.
519 The current value is assumed to be VALUE, unless UNBOUND is non-nil."
520 (unless (listp widget)
521 (setq widget (list widget)))
522 (setq prompt (format "[%s] %s" (widget-type widget) prompt))
523 (setq widget (widget-convert widget))
524 (let ((answer (widget-apply widget :prompt-value prompt value unbound)))
525 (unless (widget-apply widget :match answer)
526 (error "Value does not match %S type" (car widget)))
529 (defun widget-get-sibling (widget)
530 "Get the item WIDGET is assumed to toggle.
531 This is only meaningful for radio buttons or checkboxes in a list."
532 (let* ((children (widget-get (widget-get widget :parent) :children))
536 (setq child (car children)
537 children (cdr children))
538 (when (eq (widget-get child :button) widget)
539 (throw 'child child)))
542 (defun widget-map-buttons (function &optional buffer maparg)
543 "Map FUNCTION over the buttons in BUFFER.
544 FUNCTION is called with the arguments WIDGET and MAPARG.
546 If FUNCTION returns non-nil, the walk is cancelled.
548 The arguments MAPARG, and BUFFER default to nil and (current-buffer),
550 (let ((cur (point-min))
554 (save-excursion (set-buffer buffer) (overlay-lists))
556 (setq overlays (append (car overlays) (cdr overlays)))
557 (while (setq cur (pop overlays))
558 (setq widget (overlay-get cur 'button))
559 (if (and widget (funcall function widget maparg))
560 (setq overlays nil)))))
564 (defcustom widget-image-directory (file-name-as-directory
565 (expand-file-name "custom" data-directory))
566 "Where widget button images are located.
567 If this variable is nil, widget will try to locate the directory
572 (defcustom widget-image-enable t
573 "If non nil, use image buttons in widgets when available."
578 (defcustom widget-image-conversion
579 '((xpm ".xpm") (gif ".gif") (png ".png") (jpeg ".jpg" ".jpeg")
581 "Conversion alist from image formats to file name suffixes."
583 :type '(repeat (cons :format "%v"
584 (symbol :tag "Image Format" unknown)
585 (repeat :tag "Suffixes"
586 (string :format "%v")))))
588 (defun widget-image-find (image)
589 "Create a graphical button from IMAGE.
590 IMAGE should either already be an image, or be a file name sans
591 extension (xpm, xbm, gif, jpg, or png) located in
592 `widget-image-directory' or otherwise where `find-image' will find it."
593 (cond ((not (and image widget-image-enable (display-graphic-p)))
594 ;; We don't want or can't use images.
597 (eq 'image (car image)))
598 ;; Already an image spec. Use it.
601 ;; A string. Look it up in relevant directories.
602 (let* ((load-path (cons widget-image-directory load-path))
604 (dolist (elt widget-image-conversion)
605 (dolist (ext (cdr elt))
606 (push (list :type (car elt) :file (concat image ext)) specs)))
607 (setq specs (nreverse specs))
613 (defvar widget-button-pressed-face 'widget-button-pressed-face
614 "Face used for pressed buttons in widgets.
615 This exists as a variable so it can be set locally in certain
618 (defun widget-image-insert (widget tag image &optional down inactive)
619 "In WIDGET, insert the text TAG or, if supported, IMAGE.
620 IMAGE should either be an image or an image file name sans extension
621 \(xpm, xbm, gif, jpg, or png) located in `widget-image-directory'.
623 Optional arguments DOWN and INACTIVE are used instead of IMAGE when the
624 button is pressed or inactive, respectively. These are currently ignored."
625 (if (and (display-graphic-p)
626 (setq image (widget-image-find image)))
627 (progn (widget-put widget :suppress-face t)
630 tag 'mouse-face widget-button-pressed-face)))
635 (defgroup widget-button nil
636 "The look of various kinds of buttons."
639 (defcustom widget-button-prefix ""
640 "String used as prefix for buttons."
642 :group 'widget-button)
644 (defcustom widget-button-suffix ""
645 "String used as suffix for buttons."
647 :group 'widget-button)
649 ;;; Creating Widgets.
652 (defun widget-create (type &rest args)
653 "Create widget of TYPE.
654 The optional ARGS are additional keyword arguments."
655 (let ((widget (apply 'widget-convert type args)))
656 (widget-apply widget :create)
659 (defun widget-create-child-and-convert (parent type &rest args)
660 "As part of the widget PARENT, create a child widget TYPE.
661 The child is converted, using the keyword arguments ARGS."
662 (let ((widget (apply 'widget-convert type args)))
663 (widget-put widget :parent parent)
664 (unless (widget-get widget :indent)
665 (widget-put widget :indent (+ (or (widget-get parent :indent) 0)
666 (or (widget-get widget :extra-offset) 0)
667 (widget-get parent :offset))))
668 (widget-apply widget :create)
671 (defun widget-create-child (parent type)
672 "Create widget of TYPE."
673 (let ((widget (copy-sequence type)))
674 (widget-put widget :parent parent)
675 (unless (widget-get widget :indent)
676 (widget-put widget :indent (+ (or (widget-get parent :indent) 0)
677 (or (widget-get widget :extra-offset) 0)
678 (widget-get parent :offset))))
679 (widget-apply widget :create)
682 (defun widget-create-child-value (parent type value)
683 "Create widget of TYPE with value VALUE."
684 (let ((widget (copy-sequence type)))
685 (widget-put widget :value (widget-apply widget :value-to-internal value))
686 (widget-put widget :parent parent)
687 (unless (widget-get widget :indent)
688 (widget-put widget :indent (+ (or (widget-get parent :indent) 0)
689 (or (widget-get widget :extra-offset) 0)
690 (widget-get parent :offset))))
691 (widget-apply widget :create)
695 (defun widget-delete (widget)
697 (widget-apply widget :delete))
699 (defun widget-convert (type &rest args)
700 "Convert TYPE to a widget without inserting it in the buffer.
701 The optional ARGS are additional keyword arguments."
702 ;; Don't touch the type.
703 (let* ((widget (if (symbolp type)
705 (copy-sequence type)))
708 ;; First set the :args keyword.
709 (while (cdr current) ;Look in the type.
710 (if (keywordp (car (cdr current)))
711 (setq current (cdr (cdr current)))
712 (setcdr current (list :args (cdr current)))
714 (while args ;Look in the args.
715 (if (keywordp (nth 0 args))
716 (setq args (nthcdr 2 args))
717 (widget-put widget :args args)
719 ;; Then Convert the widget.
722 (let ((convert-widget (plist-get (cdr type) :convert-widget)))
724 (setq widget (funcall convert-widget widget))))
725 (setq type (get (car type) 'widget-type)))
726 ;; Finally set the keyword args.
728 (let ((next (nth 0 keys)))
731 (widget-put widget next (nth 1 keys))
732 (setq keys (nthcdr 2 keys)))
734 ;; Convert the :value to internal format.
735 (if (widget-member widget :value)
737 :value (widget-apply widget
739 (widget-get widget :value))))
740 ;; Return the newly create widget.
743 (defun widget-insert (&rest args)
744 "Call `insert' with ARGS even if surrounding text is read only."
745 (let ((inhibit-read-only t)
746 (inhibit-modification-hooks t))
747 (apply 'insert args)))
749 (defun widget-convert-text (type from to
750 &optional button-from button-to
752 "Return a widget of type TYPE with endpoint FROM TO.
753 Optional ARGS are extra keyword arguments for TYPE.
754 and TO will be used as the widgets end points. If optional arguments
755 BUTTON-FROM and BUTTON-TO are given, these will be used as the widgets
757 Optional ARGS are extra keyword arguments for TYPE."
758 (let ((widget (apply 'widget-convert type :delete 'widget-leave-text args))
759 (from (copy-marker from))
760 (to (copy-marker to)))
761 (set-marker-insertion-type from t)
762 (set-marker-insertion-type to nil)
763 (widget-put widget :from from)
764 (widget-put widget :to to)
766 (widget-specify-button widget button-from button-to))
769 (defun widget-convert-button (type from to &rest args)
770 "Return a widget of type TYPE with endpoint FROM TO.
771 Optional ARGS are extra keyword arguments for TYPE.
772 No text will be inserted to the buffer, instead the text between FROM
773 and TO will be used as the widgets end points, as well as the widgets
775 (apply 'widget-convert-text type from to from to args))
777 (defun widget-leave-text (widget)
778 "Remove markers and overlays from WIDGET and its children."
779 (let ((button (widget-get widget :button-overlay))
780 (sample (widget-get widget :sample-overlay))
781 (doc (widget-get widget :doc-overlay))
782 (field (widget-get widget :field-overlay)))
783 (set-marker (widget-get widget :from) nil)
784 (set-marker (widget-get widget :to) nil)
786 (delete-overlay button))
788 (delete-overlay sample))
790 (delete-overlay doc))
792 (delete-overlay field))
793 (mapc 'widget-leave-text (widget-get widget :children))))
795 ;;; Keymap and Commands.
797 (defvar widget-keymap
798 (let ((map (make-sparse-keymap)))
799 (define-key map "\t" 'widget-forward)
800 (define-key map [(shift tab)] 'widget-backward)
801 (define-key map [backtab] 'widget-backward)
802 (define-key map [down-mouse-2] 'widget-button-click)
803 (define-key map "\C-m" 'widget-button-press)
805 "Keymap containing useful binding for buffers containing widgets.
806 Recommended as a parent keymap for modes using widgets.")
808 (defvar widget-global-map global-map
809 "Keymap used for events a widget does not handle itself.")
810 (make-variable-buffer-local 'widget-global-map)
812 (defvar widget-field-keymap
813 (let ((map (copy-keymap widget-keymap)))
814 (define-key map "\C-k" 'widget-kill-line)
815 (define-key map "\M-\t" 'widget-complete)
816 (define-key map "\C-m" 'widget-field-activate)
817 ;; Since the widget code uses a `field' property to identify fields,
818 ;; ordinary beginning-of-line does the right thing.
819 ;; (define-key map "\C-a" 'widget-beginning-of-line)
820 (define-key map "\C-e" 'widget-end-of-line)
822 "Keymap used inside an editable field.")
824 (defvar widget-text-keymap
825 (let ((map (copy-keymap widget-keymap)))
826 ;; Since the widget code uses a `field' property to identify fields,
827 ;; ordinary beginning-of-line does the right thing.
828 ;; (define-key map "\C-a" 'widget-beginning-of-line)
829 (define-key map "\C-e" 'widget-end-of-line)
831 "Keymap used inside a text field.")
833 (defun widget-field-activate (pos &optional event)
834 "Invoke the ediable field at point."
836 (let ((field (widget-field-at pos)))
838 (widget-apply-action field event)
840 (lookup-key widget-global-map (this-command-keys))))))
842 (defface widget-button-pressed-face
846 (:bold t :underline t)))
847 "Face used for pressed buttons."
848 :group 'widget-faces)
850 (defun widget-button-click (event)
851 "Invoke the button that the mouse is pointing at."
853 (if (widget-event-point event)
855 (mouse-set-point event)
856 (let* ((pos (widget-event-point event))
857 (button (get-char-property pos 'button)))
859 (let* ((overlay (widget-get button :button-overlay))
860 (face (overlay-get overlay 'face))
861 (mouse-face (overlay-get overlay 'mouse-face)))
863 (let ((track-mouse t))
865 (when face ; avoid changing around image
867 'face widget-button-pressed-face)
869 'mouse-face widget-button-pressed-face))
870 (unless (widget-apply button :mouse-down-action event)
871 (while (not (widget-button-release-event-p event))
872 (setq event (read-event)
873 pos (widget-event-point event))
875 (eq (get-char-property pos 'button)
880 widget-button-pressed-face)
883 widget-button-pressed-face))
884 (overlay-put overlay 'face face)
885 (overlay-put overlay 'mouse-face mouse-face))))
887 (eq (get-char-property pos 'button) button))
888 (widget-apply-action button event))))
889 (overlay-put overlay 'face face)
890 (overlay-put overlay 'mouse-face mouse-face)))
893 ;; Find the global command to run, and check whether it
894 ;; is bound to an up event.
895 (if (memq (event-basic-type event) '(mouse-1 down-mouse-1))
896 (cond ((setq command ;down event
897 (lookup-key widget-global-map [down-mouse-1]))
899 ((setq command ;up event
900 (lookup-key widget-global-map [mouse-1]))))
901 (cond ((setq command ;down event
902 (lookup-key widget-global-map [down-mouse-2]))
904 ((setq command ;up event
905 (lookup-key widget-global-map [mouse-2])))))
907 ;; Don't execute up events twice.
908 (while (not (widget-button-release-event-p event))
909 (setq event (read-event))))
911 (call-interactively command)))))
912 (unless (pos-visible-in-window-p (widget-event-point event))
913 (mouse-set-point event)
916 (message "You clicked somewhere weird.")))
918 (defun widget-button-press (pos &optional event)
919 "Invoke button at POS."
921 (let ((button (get-char-property pos 'button)))
923 (widget-apply-action button event)
924 (let ((command (lookup-key widget-global-map (this-command-keys))))
925 (when (commandp command)
926 (call-interactively command))))))
928 (defun widget-tabable-at (&optional pos)
929 "Return the tabable widget at POS, or nil.
930 POS defaults to the value of (point)."
931 (let ((widget (widget-at pos)))
933 (let ((order (widget-get widget :tab-order)))
939 (defvar widget-use-overlay-change t
940 "If non-nil, use overlay change functions to tab around in the buffer.
941 This is much faster, but doesn't work reliably on Emacs 19.34.")
943 (defun widget-move (arg)
944 "Move point to the ARG next field or button.
945 ARG may be negative to move backward."
946 (or (bobp) (> arg 0) (backward-char))
949 (old (widget-tabable-at))
954 (goto-char (point-min)))
955 (widget-use-overlay-change
956 (goto-char (next-overlay-change (point))))
959 (and (eq pos (point))
961 (error "No buttons or fields found"))
962 (let ((new (widget-tabable-at)))
970 (goto-char (point-max)))
971 (widget-use-overlay-change
972 (goto-char (previous-overlay-change (point))))
975 (and (eq pos (point))
977 (error "No buttons or fields found"))
978 (let ((new (widget-tabable-at)))
981 (setq arg (1+ arg))))))
982 (let ((new (widget-tabable-at)))
983 (while (eq (widget-tabable-at) new)
986 (widget-echo-help (point))
987 (run-hooks 'widget-move-hook))
989 (defun widget-forward (arg)
990 "Move point to the next field or button.
991 With optional ARG, move across that many fields."
993 (run-hooks 'widget-forward-hook)
996 (defun widget-backward (arg)
997 "Move point to the previous field or button.
998 With optional ARG, move across that many fields."
1000 (run-hooks 'widget-backward-hook)
1001 (widget-move (- arg)))
1003 ;; Since the widget code uses a `field' property to identify fields,
1004 ;; ordinary beginning-of-line does the right thing.
1005 (defalias 'widget-beginning-of-line 'beginning-of-line)
1007 (defun widget-end-of-line ()
1008 "Go to end of field or end of line, whichever is first.
1009 Trailing spaces at the end of padded fields are not considered part of
1012 ;; Ordinary end-of-line does the right thing, because we're inside
1013 ;; text with a `field' property.
1016 ;; ... except that we want to ignore trailing spaces in fields that
1017 ;; aren't terminated by a newline, because they are used as padding,
1018 ;; and ignored when extracting the entered value of the field.
1019 (skip-chars-backward " " (field-beginning (1- (point))))))
1021 (defun widget-kill-line ()
1022 "Kill to end of field or end of line, whichever is first."
1024 (let* ((field (widget-field-find (point)))
1025 (end (and field (widget-field-end field))))
1026 (if (and field (> (line-beginning-position 2) end))
1027 (kill-region (point) end)
1028 (call-interactively 'kill-line))))
1030 (defcustom widget-complete-field (lookup-key global-map "\M-\t")
1031 "Default function to call for completion inside fields."
1032 :options '(ispell-complete-word complete-tag lisp-complete-symbol)
1036 (defun widget-complete ()
1037 "Complete content of editable field from point.
1038 When not inside a field, move to the previous button or field."
1040 (let ((field (widget-field-find (point))))
1042 (widget-apply field :complete)
1043 (error "Not in an editable field"))))
1045 ;;; Setting up the buffer.
1047 (defvar widget-field-new nil)
1048 ;; List of all newly created editable fields in the buffer.
1049 (make-variable-buffer-local 'widget-field-new)
1051 (defvar widget-field-list nil)
1052 ;; List of all editable fields in the buffer.
1053 (make-variable-buffer-local 'widget-field-list)
1055 (defun widget-at (&optional pos)
1056 "The button or field at POS (default, point)."
1057 (or (get-char-property (or pos (point)) 'button)
1058 (widget-field-at pos)))
1060 (defun widget-setup ()
1061 "Setup current buffer so editing string widgets works."
1062 (let ((inhibit-read-only t)
1063 (inhibit-modification-hooks t)
1065 (while widget-field-new
1066 (setq field (car widget-field-new)
1067 widget-field-new (cdr widget-field-new)
1068 widget-field-list (cons field widget-field-list))
1069 (let ((from (car (widget-get field :field-overlay)))
1070 (to (cdr (widget-get field :field-overlay))))
1071 (widget-specify-field field
1072 (marker-position from) (marker-position to))
1073 (set-marker from nil)
1074 (set-marker to nil))))
1076 (widget-add-change))
1078 (defvar widget-field-last nil)
1079 ;; Last field containing point.
1080 (make-variable-buffer-local 'widget-field-last)
1082 (defvar widget-field-was nil)
1083 ;; The widget data before the change.
1084 (make-variable-buffer-local 'widget-field-was)
1086 (defun widget-field-at (pos)
1087 "Return the widget field at POS, or nil if none."
1088 (let ((field (get-char-property (or pos (point)) 'field)))
1089 (if (eq field 'boundary)
1093 (defun widget-field-buffer (widget)
1094 "Return the start of WIDGET's editing field."
1095 (let ((overlay (widget-get widget :field-overlay)))
1096 (and overlay (overlay-buffer overlay))))
1098 (defun widget-field-start (widget)
1099 "Return the start of WIDGET's editing field."
1100 (let ((overlay (widget-get widget :field-overlay)))
1101 (and overlay (overlay-start overlay))))
1103 (defun widget-field-end (widget)
1104 "Return the end of WIDGET's editing field."
1105 (let ((overlay (widget-get widget :field-overlay)))
1106 ;; Don't subtract one if local-map works at the end of the overlay,
1107 ;; or if a special `boundary' field has been added after the widget
1110 (if (and (not (eq (get-char-property (overlay-end overlay)
1112 (widget-field-buffer widget))
1114 (or widget-field-add-space
1115 (null (widget-get widget :size))))
1116 (1- (overlay-end overlay))
1117 (overlay-end overlay)))))
1119 (defun widget-field-find (pos)
1120 "Return the field at POS.
1121 Unlike (get-char-property POS 'field) this, works with empty fields too."
1122 (let ((fields widget-field-list)
1125 (setq field (car fields)
1126 fields (cdr fields))
1127 (when (and (<= (widget-field-start field) pos)
1128 (<= pos (widget-field-end field)))
1130 (error "Overlapping fields"))
1131 (setq found field)))
1134 (defun widget-before-change (from to)
1135 ;; This is how, for example, a variable changes its state to `modified'.
1136 ;; when it is being edited.
1137 (unless inhibit-read-only
1138 (let ((from-field (widget-field-find from))
1139 (to-field (widget-field-find to)))
1140 (cond ((not (eq from-field to-field))
1141 (add-hook 'post-command-hook 'widget-add-change nil t)
1142 (signal 'text-read-only
1143 '("Change should be restricted to a single field")))
1145 (add-hook 'post-command-hook 'widget-add-change nil t)
1146 (signal 'text-read-only
1147 '("Attempt to change text outside editable field")))
1148 (widget-field-use-before-change
1149 (widget-apply from-field :notify from-field))))))
1151 (defun widget-add-change ()
1152 (remove-hook 'post-command-hook 'widget-add-change t)
1153 (add-hook 'before-change-functions 'widget-before-change nil t)
1154 (add-hook 'after-change-functions 'widget-after-change nil t))
1156 (defun widget-after-change (from to old)
1157 "Adjust field size and text properties."
1158 (let ((field (widget-field-find from))
1159 (other (widget-field-find to)))
1161 (unless (eq field other)
1162 (error "Change in different fields"))
1163 (let ((size (widget-get field :size)))
1165 (let ((begin (widget-field-start field))
1166 (end (widget-field-end field)))
1167 (cond ((< (- end begin) size)
1171 (insert-char ?\ (- (+ begin size) end))))
1172 ((> (- end begin) size)
1173 ;; Field too large and
1174 (if (or (< (point) (+ begin size))
1176 ;; Point is outside extra space.
1177 (setq begin (+ begin size))
1178 ;; Point is within the extra space.
1179 (setq begin (point)))
1182 (while (and (eq (preceding-char) ?\ )
1184 (delete-backward-char 1)))))))
1185 (widget-specify-secret field))
1186 (widget-apply field :notify field))))
1188 ;;; Widget Functions
1190 ;; These functions are used in the definition of multiple widgets.
1192 (defun widget-parent-action (widget &optional event)
1193 "Tell :parent of WIDGET to handle the :action.
1194 Optional EVENT is the event that triggered the action."
1195 (widget-apply (widget-get widget :parent) :action event))
1197 (defun widget-children-value-delete (widget)
1198 "Delete all :children and :buttons in WIDGET."
1199 (mapc 'widget-delete (widget-get widget :children))
1200 (widget-put widget :children nil)
1201 (mapc 'widget-delete (widget-get widget :buttons))
1202 (widget-put widget :buttons nil))
1204 (defun widget-children-validate (widget)
1205 "All the :children must be valid."
1206 (let ((children (widget-get widget :children))
1208 (while (and children (not found))
1209 (setq child (car children)
1210 children (cdr children)
1211 found (widget-apply child :validate)))
1214 ;; Made defsubst to speed up face editor creation.
1215 (defsubst widget-types-convert-widget (widget)
1216 "Convert :args as widget types in WIDGET."
1217 (widget-put widget :args (mapcar 'widget-convert (widget-get widget :args)))
1220 (defun widget-value-convert-widget (widget)
1221 "Initialize :value from :args in WIDGET."
1222 (let ((args (widget-get widget :args)))
1224 (widget-put widget :value (car args))
1225 ;; Don't convert :value here, as this is done in `widget-convert'.
1226 ;; (widget-put widget :value (widget-apply widget
1227 ;; :value-to-internal (car args)))
1228 (widget-put widget :args nil)))
1231 (defun widget-value-value-get (widget)
1232 "Return the :value property of WIDGET."
1233 (widget-get widget :value))
1235 ;;; The `default' Widget.
1237 (define-widget 'default nil
1238 "Basic widget other widgets are derived from."
1239 :value-to-internal (lambda (widget value) value)
1240 :value-to-external (lambda (widget value) value)
1241 :button-prefix 'widget-button-prefix
1242 :button-suffix 'widget-button-suffix
1243 :complete 'widget-default-complete
1244 :create 'widget-default-create
1247 :format-handler 'widget-default-format-handler
1248 :button-face-get 'widget-default-button-face-get
1249 :sample-face-get 'widget-default-sample-face-get
1250 :delete 'widget-default-delete
1251 :value-set 'widget-default-value-set
1252 :value-inline 'widget-default-value-inline
1253 :default-get 'widget-default-default-get
1254 :menu-tag-get 'widget-default-menu-tag-get
1256 :active 'widget-default-active
1257 :activate 'widget-specify-active
1258 :deactivate 'widget-default-deactivate
1259 :mouse-down-action #'ignore
1260 :action 'widget-default-action
1261 :notify 'widget-default-notify
1262 :prompt-value 'widget-default-prompt-value)
1264 (defun widget-default-complete (widget)
1265 "Call the value of the :complete-function property of WIDGET.
1266 If that does not exists, call the value of `widget-complete-field'."
1267 (call-interactively (or (widget-get widget :complete-function)
1268 widget-complete-field)))
1270 (defun widget-default-create (widget)
1271 "Create WIDGET at point in the current buffer."
1272 (widget-specify-insert
1273 (let ((from (point))
1274 button-begin button-end
1275 sample-begin sample-end
1278 (insert (widget-get widget :format))
1280 ;; Parse escapes in format.
1281 (while (re-search-forward "%\\(.\\)" nil t)
1282 (let ((escape (char-after (match-beginning 1))))
1283 (delete-backward-char 2)
1284 (cond ((eq escape ?%)
1287 (setq button-begin (point))
1288 (insert (widget-get-indirect widget :button-prefix)))
1290 (insert (widget-get-indirect widget :button-suffix))
1291 (setq button-end (point)))
1293 (setq sample-begin (point)))
1295 (setq sample-end (point)))
1297 (when (widget-get widget :indent)
1299 (insert-char ? (widget-get widget :indent))))
1301 (let ((image (widget-get widget :tag-glyph))
1302 (tag (widget-get widget :tag)))
1304 (widget-image-insert widget (or tag "image") image))
1308 (princ (widget-get widget :value)
1309 (current-buffer))))))
1311 (let ((doc (widget-get widget :doc)))
1313 (setq doc-begin (point))
1315 (while (eq (preceding-char) ?\n)
1316 (delete-backward-char 1))
1318 (setq doc-end (point)))))
1320 (if (and button-begin (not button-end))
1321 (widget-apply widget :value-create)
1322 (setq value-pos (point))))
1324 (widget-apply widget :format-handler escape)))))
1325 ;; Specify button, sample, and doc, and insert value.
1326 (and button-begin button-end
1327 (widget-specify-button widget button-begin button-end))
1328 (and sample-begin sample-end
1329 (widget-specify-sample widget sample-begin sample-end))
1330 (and doc-begin doc-end
1331 (widget-specify-doc widget doc-begin doc-end))
1333 (goto-char value-pos)
1334 (widget-apply widget :value-create)))
1335 (let ((from (point-min-marker))
1336 (to (point-max-marker)))
1337 (set-marker-insertion-type from t)
1338 (set-marker-insertion-type to nil)
1339 (widget-put widget :from from)
1340 (widget-put widget :to to)))
1341 (widget-clear-undo))
1343 (defun widget-default-format-handler (widget escape)
1344 ;; We recognize the %h escape by default.
1345 (let* ((buttons (widget-get widget :buttons)))
1346 (cond ((eq escape ?h)
1347 (let* ((doc-property (widget-get widget :documentation-property))
1348 (doc-try (cond ((widget-get widget :doc))
1349 ((functionp doc-property)
1350 (funcall doc-property
1351 (widget-get widget :value)))
1352 ((symbolp doc-property)
1353 (documentation-property
1354 (widget-get widget :value)
1356 (doc-text (and (stringp doc-try)
1357 (> (length doc-try) 1)
1359 (doc-indent (widget-get widget :documentation-indent)))
1361 (and (eq (preceding-char) ?\n)
1362 (widget-get widget :indent)
1363 (insert-char ? (widget-get widget :indent)))
1364 ;; The `*' in the beginning is redundant.
1365 (when (eq (aref doc-text 0) ?*)
1366 (setq doc-text (substring doc-text 1)))
1367 ;; Get rid of trailing newlines.
1368 (when (string-match "\n+\\'" doc-text)
1369 (setq doc-text (substring doc-text 0 (match-beginning 0))))
1370 (push (widget-create-child-and-convert
1371 widget 'documentation-string
1372 :indent (cond ((numberp doc-indent )
1380 (error "Unknown escape `%c'" escape)))
1381 (widget-put widget :buttons buttons)))
1383 (defun widget-default-button-face-get (widget)
1384 ;; Use :button-face or widget-button-face
1385 (or (widget-get widget :button-face)
1386 (let ((parent (widget-get widget :parent)))
1388 (widget-apply parent :button-face-get)
1389 widget-button-face))))
1391 (defun widget-default-sample-face-get (widget)
1392 ;; Use :sample-face.
1393 (widget-get widget :sample-face))
1395 (defun widget-default-delete (widget)
1396 "Remove widget from the buffer."
1397 (let ((from (widget-get widget :from))
1398 (to (widget-get widget :to))
1399 (inactive-overlay (widget-get widget :inactive))
1400 (button-overlay (widget-get widget :button-overlay))
1401 (sample-overlay (widget-get widget :sample-overlay))
1402 (doc-overlay (widget-get widget :doc-overlay))
1403 (inhibit-modification-hooks t)
1404 (inhibit-read-only t))
1405 (widget-apply widget :value-delete)
1406 (when inactive-overlay
1407 (delete-overlay inactive-overlay))
1408 (when button-overlay
1409 (delete-overlay button-overlay))
1410 (when sample-overlay
1411 (delete-overlay sample-overlay))
1413 (delete-overlay doc-overlay))
1415 ;; Kludge: this doesn't need to be true for empty formats.
1416 (delete-region from to))
1417 (set-marker from nil)
1418 (set-marker to nil))
1419 (widget-clear-undo))
1421 (defun widget-default-value-set (widget value)
1422 "Recreate widget with new value."
1423 (let* ((old-pos (point))
1424 (from (copy-marker (widget-get widget :from)))
1425 (to (copy-marker (widget-get widget :to)))
1426 (offset (if (and (<= from old-pos) (<= old-pos to))
1427 (if (>= old-pos (1- to))
1429 (- old-pos from)))))
1430 ;;??? Bug: this ought to insert the new value before deleting the old one,
1431 ;; so that markers on either side of the value automatically
1432 ;; stay on the same side. -- rms.
1434 (goto-char (widget-get widget :from))
1435 (widget-apply widget :delete)
1436 (widget-put widget :value value)
1437 (widget-apply widget :create))
1440 (goto-char (+ (widget-get widget :to) offset 1))
1441 (goto-char (min (+ from offset) (1- (widget-get widget :to))))))))
1443 (defun widget-default-value-inline (widget)
1444 "Wrap value in a list unless it is inline."
1445 (if (widget-get widget :inline)
1446 (widget-value widget)
1447 (list (widget-value widget))))
1449 (defun widget-default-default-get (widget)
1451 (widget-get widget :value))
1453 (defun widget-default-menu-tag-get (widget)
1454 "Use tag or value for menus."
1455 (or (widget-get widget :menu-tag)
1456 (widget-get widget :tag)
1457 (widget-princ-to-string (widget-get widget :value))))
1459 (defun widget-default-active (widget)
1460 "Return t iff this widget active (user modifiable)."
1461 (or (widget-get widget :always-active)
1462 (and (not (widget-get widget :inactive))
1463 (let ((parent (widget-get widget :parent)))
1465 (widget-apply parent :active))))))
1467 (defun widget-default-deactivate (widget)
1468 "Make WIDGET inactive for user modifications."
1469 (widget-specify-inactive widget
1470 (widget-get widget :from)
1471 (widget-get widget :to)))
1473 (defun widget-default-action (widget &optional event)
1474 "Notify the parent when a widget changes."
1475 (let ((parent (widget-get widget :parent)))
1477 (widget-apply parent :notify widget event))))
1479 (defun widget-default-notify (widget child &optional event)
1480 "Pass notification to parent."
1481 (widget-default-action widget event))
1483 (defun widget-default-prompt-value (widget prompt value unbound)
1484 "Read an arbitrary value. Stolen from `set-variable'."
1485 ;; (let ((initial (if unbound
1487 ;; It would be nice if we could do a `(cons val 1)' here.
1488 ;; (prin1-to-string (custom-quote value))))))
1489 (eval-minibuffer prompt))
1491 ;;; The `item' Widget.
1493 (define-widget 'item 'default
1494 "Constant items for inclusion in other widgets."
1495 :convert-widget 'widget-value-convert-widget
1496 :value-create 'widget-item-value-create
1497 :value-delete 'ignore
1498 :value-get 'widget-value-value-get
1499 :match 'widget-item-match
1500 :match-inline 'widget-item-match-inline
1501 :action 'widget-item-action
1504 (defun widget-item-value-create (widget)
1505 "Insert the printed representation of the value."
1506 (princ (widget-get widget :value) (current-buffer)))
1508 (defun widget-item-match (widget value)
1509 ;; Match if the value is the same.
1510 (equal (widget-get widget :value) value))
1512 (defun widget-item-match-inline (widget values)
1513 ;; Match if the value is the same.
1514 (let ((value (widget-get widget :value)))
1516 (<= (length value) (length values))
1517 (let ((head (widget-sublist values 0 (length value))))
1518 (and (equal head value)
1519 (cons head (widget-sublist values (length value))))))))
1521 (defun widget-sublist (list start &optional end)
1522 "Return the sublist of LIST from START to END.
1523 If END is omitted, it defaults to the length of LIST."
1524 (if (> start 0) (setq list (nthcdr start list)))
1526 (unless (<= end start)
1527 (setq list (copy-sequence list))
1528 (setcdr (nthcdr (- end start 1) list) nil)
1530 (copy-sequence list)))
1532 (defun widget-item-action (widget &optional event)
1533 ;; Just notify itself.
1534 (widget-apply widget :notify widget event))
1536 ;;; The `push-button' Widget.
1538 ;; (defcustom widget-push-button-gui t
1539 ;; "If non nil, use GUI push buttons when available."
1543 ;; Cache already created GUI objects.
1544 ;; (defvar widget-push-button-cache nil)
1546 (defcustom widget-push-button-prefix "["
1547 "String used as prefix for buttons."
1549 :group 'widget-button)
1551 (defcustom widget-push-button-suffix "]"
1552 "String used as suffix for buttons."
1554 :group 'widget-button)
1556 (define-widget 'push-button 'item
1557 "A pushable button."
1560 :value-create 'widget-push-button-value-create
1563 (defun widget-push-button-value-create (widget)
1564 "Insert text representing the `on' and `off' states."
1565 (let* ((tag (or (widget-get widget :tag)
1566 (widget-get widget :value)))
1567 (tag-glyph (widget-get widget :tag-glyph))
1568 (text (concat widget-push-button-prefix
1569 tag widget-push-button-suffix)))
1571 (widget-image-insert widget text tag-glyph)
1574 ;; (defun widget-gui-action (widget)
1575 ;; "Apply :action for WIDGET."
1576 ;; (widget-apply-action widget (this-command-keys)))
1578 ;;; The `link' Widget.
1580 (defcustom widget-link-prefix "["
1581 "String used as prefix for links."
1583 :group 'widget-button)
1585 (defcustom widget-link-suffix "]"
1586 "String used as suffix for links."
1588 :group 'widget-button)
1590 (define-widget 'link 'item
1592 :button-prefix 'widget-link-prefix
1593 :button-suffix 'widget-link-suffix
1594 :help-echo "Follow the link."
1597 ;;; The `info-link' Widget.
1599 (define-widget 'info-link 'link
1600 "A link to an info file."
1601 :action 'widget-info-link-action)
1603 (defun widget-info-link-action (widget &optional event)
1604 "Open the info node specified by WIDGET."
1605 (Info-goto-node (widget-value widget)))
1607 ;;; The `url-link' Widget.
1609 (define-widget 'url-link 'link
1610 "A link to an www page."
1611 :action 'widget-url-link-action)
1613 (defun widget-url-link-action (widget &optional event)
1614 "Open the url specified by WIDGET."
1615 (browse-url (widget-value widget)))
1617 ;;; The `function-link' Widget.
1619 (define-widget 'function-link 'link
1620 "A link to an Emacs function."
1621 :action 'widget-function-link-action)
1623 (defun widget-function-link-action (widget &optional event)
1624 "Show the function specified by WIDGET."
1625 (describe-function (widget-value widget)))
1627 ;;; The `variable-link' Widget.
1629 (define-widget 'variable-link 'link
1630 "A link to an Emacs variable."
1631 :action 'widget-variable-link-action)
1633 (defun widget-variable-link-action (widget &optional event)
1634 "Show the variable specified by WIDGET."
1635 (describe-variable (widget-value widget)))
1637 ;;; The `file-link' Widget.
1639 (define-widget 'file-link 'link
1641 :action 'widget-file-link-action)
1643 (defun widget-file-link-action (widget &optional event)
1644 "Find the file specified by WIDGET."
1645 (find-file (widget-value widget)))
1647 ;;; The `emacs-library-link' Widget.
1649 (define-widget 'emacs-library-link 'link
1650 "A link to an Emacs Lisp library file."
1651 :action 'widget-emacs-library-link-action)
1653 (defun widget-emacs-library-link-action (widget &optional event)
1654 "Find the Emacs Library file specified by WIDGET."
1655 (find-file (locate-library (widget-value widget))))
1657 ;;; The `emacs-commentary-link' Widget.
1659 (define-widget 'emacs-commentary-link 'link
1660 "A link to Commentary in an Emacs Lisp library file."
1661 :action 'widget-emacs-commentary-link-action)
1663 (defun widget-emacs-commentary-link-action (widget &optional event)
1664 "Find the Commentary section of the Emacs file specified by WIDGET."
1665 (finder-commentary (widget-value widget)))
1667 ;;; The `editable-field' Widget.
1669 (define-widget 'editable-field 'default
1670 "An editable text field."
1671 :convert-widget 'widget-value-convert-widget
1672 :keymap widget-field-keymap
1674 :help-echo "M-TAB: complete field; RET: enter value"
1676 :prompt-internal 'widget-field-prompt-internal
1677 :prompt-history 'widget-field-history
1678 :prompt-value 'widget-field-prompt-value
1679 :action 'widget-field-action
1680 :validate 'widget-field-validate
1682 :error "Field's value doesn't match allowed forms"
1683 :value-create 'widget-field-value-create
1684 :value-delete 'widget-field-value-delete
1685 :value-get 'widget-field-value-get
1686 :match 'widget-field-match)
1688 (defvar widget-field-history nil
1689 "History of field minibuffer edits.")
1691 (defun widget-field-prompt-internal (widget prompt initial history)
1692 "Read string for WIDGET promptinhg with PROMPT.
1693 INITIAL is the initial input and HISTORY is a symbol containing
1695 (read-string prompt initial history))
1697 (defun widget-field-prompt-value (widget prompt value unbound)
1698 "Prompt for a string."
1699 (widget-apply widget
1701 (widget-apply widget
1702 :prompt-internal prompt
1704 (cons (widget-apply widget
1705 :value-to-internal value)
1707 (widget-get widget :prompt-history))))
1709 (defvar widget-edit-functions nil)
1711 (defun widget-field-action (widget &optional event)
1712 "Move to next field."
1714 (run-hook-with-args 'widget-edit-functions widget))
1716 (defun widget-field-validate (widget)
1717 "Valid if the content matches `:valid-regexp'."
1718 (unless (string-match (widget-get widget :valid-regexp)
1719 (widget-apply widget :value-get))
1722 (defun widget-field-value-create (widget)
1723 "Create an editable text field."
1724 (let ((size (widget-get widget :size))
1725 (value (widget-get widget :value))
1727 ;; This is changed to a real overlay in `widget-setup'. We
1728 ;; need the end points to behave differently until
1729 ;; `widget-setup' is called.
1730 (overlay (cons (make-marker) (make-marker))))
1731 (widget-put widget :field-overlay overlay)
1734 (< (length value) size)
1735 (insert-char ?\ (- size (length value))))
1736 (unless (memq widget widget-field-list)
1737 (setq widget-field-new (cons widget widget-field-new)))
1738 (move-marker (cdr overlay) (point))
1739 (set-marker-insertion-type (cdr overlay) nil)
1742 (move-marker (car overlay) from)
1743 (set-marker-insertion-type (car overlay) t)))
1745 (defun widget-field-value-delete (widget)
1746 "Remove the widget from the list of active editing fields."
1747 (setq widget-field-list (delq widget widget-field-list))
1748 ;; These are nil if the :format string doesn't contain `%v'.
1749 (let ((overlay (widget-get widget :field-overlay)))
1751 (delete-overlay overlay))))
1753 (defun widget-field-value-get (widget)
1754 "Return current text in editing field."
1755 (let ((from (widget-field-start widget))
1756 (to (widget-field-end widget))
1757 (buffer (widget-field-buffer widget))
1758 (size (widget-get widget :size))
1759 (secret (widget-get widget :secret))
1760 (old (current-buffer)))
1767 (eq (char-after (1- to)) ?\ ))
1769 (let ((result (buffer-substring-no-properties from to)))
1772 (while (< (+ from index) to)
1774 (get-char-property (+ from index) 'secret))
1775 (setq index (1+ index)))))
1778 (widget-get widget :value))))
1780 (defun widget-field-match (widget value)
1781 ;; Match any string.
1784 ;;; The `text' Widget.
1786 (define-widget 'text 'editable-field
1787 :keymap widget-text-keymap
1788 "A multiline text area.")
1790 ;;; The `menu-choice' Widget.
1792 (define-widget 'menu-choice 'default
1793 "A menu of options."
1794 :convert-widget 'widget-types-convert-widget
1795 :format "%[%t%]: %v"
1798 :void '(item :format "invalid (%t)\n")
1799 :value-create 'widget-choice-value-create
1800 :value-delete 'widget-children-value-delete
1801 :value-get 'widget-choice-value-get
1802 :value-inline 'widget-choice-value-inline
1803 :default-get 'widget-choice-default-get
1804 :mouse-down-action 'widget-choice-mouse-down-action
1805 :action 'widget-choice-action
1806 :error "Make a choice"
1807 :validate 'widget-choice-validate
1808 :match 'widget-choice-match
1809 :match-inline 'widget-choice-match-inline)
1811 (defun widget-choice-value-create (widget)
1812 "Insert the first choice that matches the value."
1813 (let ((value (widget-get widget :value))
1814 (args (widget-get widget :args))
1815 (explicit (widget-get widget :explicit-choice))
1817 (if (and explicit (equal value (widget-get widget :explicit-choice-value)))
1819 ;; If the user specified the choice for this value,
1820 ;; respect that choice as long as the value is the same.
1821 (widget-put widget :children (list (widget-create-child-value
1822 widget explicit value)))
1823 (widget-put widget :choice explicit))
1825 (setq current (car args)
1827 (when (widget-apply current :match value)
1828 (widget-put widget :children (list (widget-create-child-value
1829 widget current value)))
1830 (widget-put widget :choice current)
1834 (let ((void (widget-get widget :void)))
1835 (widget-put widget :children (list (widget-create-child-and-convert
1836 widget void :value value)))
1837 (widget-put widget :choice void))))))
1839 (defun widget-choice-value-get (widget)
1840 ;; Get value of the child widget.
1841 (widget-value (car (widget-get widget :children))))
1843 (defun widget-choice-value-inline (widget)
1844 ;; Get value of the child widget.
1845 (widget-apply (car (widget-get widget :children)) :value-inline))
1847 (defun widget-choice-default-get (widget)
1848 ;; Get default for the first choice.
1849 (widget-default-get (car (widget-get widget :args))))
1851 (defcustom widget-choice-toggle nil
1852 "If non-nil, a binary choice will just toggle between the values.
1853 Otherwise, the user will explicitly have to choose between the values
1854 when he invoked the menu."
1858 (defun widget-choice-mouse-down-action (widget &optional event)
1859 ;; Return non-nil if we need a menu.
1860 (let ((args (widget-get widget :args))
1861 (old (widget-get widget :choice)))
1862 (cond ((not (display-popup-menus-p))
1863 ;; No place to pop up a menu.
1865 ((< (length args) 2)
1866 ;; Empty or singleton list, just return the value.
1868 ((> (length args) widget-menu-max-size)
1869 ;; Too long, prompt.
1871 ((> (length args) 2)
1872 ;; Reasonable sized list, use menu.
1874 ((and widget-choice-toggle (memq old args))
1878 ;; Ask which of the two.
1881 (defun widget-choice-action (widget &optional event)
1883 (let ((args (widget-get widget :args))
1884 (old (widget-get widget :choice))
1885 (tag (widget-apply widget :menu-tag-get))
1886 (completion-ignore-case (widget-get widget :case-fold))
1889 ;; Remember old value.
1890 (if (and old (not (widget-apply widget :validate)))
1891 (let* ((external (widget-value widget))
1892 (internal (widget-apply old :value-to-internal external)))
1893 (widget-put old :value internal)))
1896 (cond ((= (length args) 0)
1898 ((= (length args) 1)
1900 ((and widget-choice-toggle
1903 (if (eq old (nth 0 args))
1908 (setq current (car args)
1911 (cons (cons (widget-apply current :menu-tag-get)
1914 (setq this-explicit t)
1915 (widget-choose tag (reverse choices) event))))
1917 ;; If this was an explicit user choice,
1918 ;; record the choice, and the record the value it was made for.
1919 ;; widget-choice-value-create will respect this choice,
1920 ;; as long as the value is the same.
1922 (widget-put widget :explicit-choice current)
1923 (widget-put widget :explicit-choice-value (widget-get widget :value)))
1925 widget (widget-apply current
1926 :value-to-external (widget-default-get current)))
1928 (widget-apply widget :notify widget event)))
1929 (run-hook-with-args 'widget-edit-functions widget))
1931 (defun widget-choice-validate (widget)
1932 ;; Valid if we have made a valid choice.
1933 (if (eq (widget-get widget :void) (widget-get widget :choice))
1935 (widget-apply (car (widget-get widget :children)) :validate)))
1937 (defun widget-choice-match (widget value)
1938 ;; Matches if one of the choices matches.
1939 (let ((args (widget-get widget :args))
1941 (while (and args (not found))
1942 (setq current (car args)
1944 found (widget-apply current :match value)))
1947 (defun widget-choice-match-inline (widget values)
1948 ;; Matches if one of the choices matches.
1949 (let ((args (widget-get widget :args))
1951 (while (and args (null found))
1952 (setq current (car args)
1954 found (widget-match-inline current values)))
1957 ;;; The `toggle' Widget.
1959 (define-widget 'toggle 'item
1960 "Toggle between two states."
1962 :value-create 'widget-toggle-value-create
1963 :action 'widget-toggle-action
1964 :match (lambda (widget value) t)
1968 (defun widget-toggle-value-create (widget)
1969 "Insert text representing the `on' and `off' states."
1970 (if (widget-value widget)
1971 (widget-image-insert widget
1972 (widget-get widget :on)
1973 (widget-get widget :on-glyph))
1974 (widget-image-insert widget
1975 (widget-get widget :off)
1976 (widget-get widget :off-glyph))))
1978 (defun widget-toggle-action (widget &optional event)
1980 (widget-value-set widget (not (widget-value widget)))
1981 (widget-apply widget :notify widget event)
1982 (run-hook-with-args 'widget-edit-functions widget))
1984 ;;; The `checkbox' Widget.
1986 (define-widget 'checkbox 'toggle
1987 "A checkbox toggle."
1992 ;; We could probably do the same job as the images using single
1993 ;; space characters in a boxed face with a stretch specification to
1994 ;; make them square.
1995 :on-glyph (create-image (make-bool-vector 49 1)
1996 'xbm t :width 7 :height 7
1997 :foreground "grey75" ; like default mode line
1998 :relief -3 :ascent 'center)
2000 :off-glyph (create-image (make-bool-vector 49 1)
2001 'xbm t :width 7 :height 7
2002 :foreground "grey75"
2003 :relief 3 :ascent 'center)
2004 :help-echo "Toggle this item."
2005 :action 'widget-checkbox-action)
2007 (defun widget-checkbox-action (widget &optional event)
2008 "Toggle checkbox, notify parent, and set active state of sibling."
2009 (widget-toggle-action widget event)
2010 (let ((sibling (widget-get-sibling widget)))
2012 (if (widget-value widget)
2013 (widget-apply sibling :activate)
2014 (widget-apply sibling :deactivate)))))
2016 ;;; The `checklist' Widget.
2018 (define-widget 'checklist 'default
2019 "A multiple choice widget."
2020 :convert-widget 'widget-types-convert-widget
2023 :entry-format "%b %v"
2024 :menu-tag "checklist"
2026 :value-create 'widget-checklist-value-create
2027 :value-delete 'widget-children-value-delete
2028 :value-get 'widget-checklist-value-get
2029 :validate 'widget-checklist-validate
2030 :match 'widget-checklist-match
2031 :match-inline 'widget-checklist-match-inline)
2033 (defun widget-checklist-value-create (widget)
2034 ;; Insert all values
2035 (let ((alist (widget-checklist-match-find widget (widget-get widget :value)))
2036 (args (widget-get widget :args)))
2038 (widget-checklist-add-item widget (car args) (assq (car args) alist))
2039 (setq args (cdr args)))
2040 (widget-put widget :children (nreverse (widget-get widget :children)))))
2042 (defun widget-checklist-add-item (widget type chosen)
2043 "Create checklist item in WIDGET of type TYPE.
2044 If the item is checked, CHOSEN is a cons whose cdr is the value."
2045 (and (eq (preceding-char) ?\n)
2046 (widget-get widget :indent)
2047 (insert-char ? (widget-get widget :indent)))
2048 (widget-specify-insert
2049 (let* ((children (widget-get widget :children))
2050 (buttons (widget-get widget :buttons))
2051 (button-args (or (widget-get type :sibling-args)
2052 (widget-get widget :button-args)))
2055 (insert (widget-get widget :entry-format))
2057 ;; Parse % escapes in format.
2058 (while (re-search-forward "%\\([bv%]\\)" nil t)
2059 (let ((escape (char-after (match-beginning 1))))
2060 (delete-backward-char 2)
2061 (cond ((eq escape ?%)
2064 (setq button (apply 'widget-create-child-and-convert
2066 :value (not (null chosen))
2071 (let ((child (widget-create-child widget type)))
2072 (widget-apply child :deactivate)
2074 ((widget-get type :inline)
2075 (widget-create-child-value
2076 widget type (cdr chosen)))
2078 (widget-create-child-value
2079 widget type (car (cdr chosen)))))))
2081 (error "Unknown escape `%c'" escape)))))
2082 ;; Update properties.
2083 (and button child (widget-put child :button button))
2084 (and button (widget-put widget :buttons (cons button buttons)))
2085 (and child (widget-put widget :children (cons child children))))))
2087 (defun widget-checklist-match (widget values)
2088 ;; All values must match a type in the checklist.
2090 (null (cdr (widget-checklist-match-inline widget values)))))
2092 (defun widget-checklist-match-inline (widget values)
2093 ;; Find the values which match a type in the checklist.
2094 (let ((greedy (widget-get widget :greedy))
2095 (args (copy-sequence (widget-get widget :args)))
2098 (let ((answer (widget-checklist-match-up args values)))
2100 (let ((vals (widget-match-inline answer values)))
2101 (setq found (append found (car vals))
2103 args (delq answer args))))
2105 (setq rest (append rest (list (car values)))
2106 values (cdr values)))
2108 (setq rest (append rest values)
2112 (defun widget-checklist-match-find (widget vals)
2113 "Find the vals which match a type in the checklist.
2114 Return an alist of (TYPE MATCH)."
2115 (let ((greedy (widget-get widget :greedy))
2116 (args (copy-sequence (widget-get widget :args)))
2119 (let ((answer (widget-checklist-match-up args vals)))
2121 (let ((match (widget-match-inline answer vals)))
2122 (setq found (cons (cons answer (car match)) found)
2124 args (delq answer args))))
2126 (setq vals (cdr vals)))
2131 (defun widget-checklist-match-up (args vals)
2132 "Return the first type from ARGS that matches VALS."
2133 (let (current found)
2134 (while (and args (null found))
2135 (setq current (car args)
2137 found (widget-match-inline current vals)))
2141 (defun widget-checklist-value-get (widget)
2142 ;; The values of all selected items.
2143 (let ((children (widget-get widget :children))
2146 (setq child (car children)
2147 children (cdr children))
2148 (if (widget-value (widget-get child :button))
2149 (setq result (append result (widget-apply child :value-inline)))))
2152 (defun widget-checklist-validate (widget)
2153 ;; Ticked chilren must be valid.
2154 (let ((children (widget-get widget :children))
2156 (while (and children (not found))
2157 (setq child (car children)
2158 children (cdr children)
2159 button (widget-get child :button)
2160 found (and (widget-value button)
2161 (widget-apply child :validate))))
2164 ;;; The `option' Widget
2166 (define-widget 'option 'checklist
2167 "An widget with an optional item."
2170 ;;; The `choice-item' Widget.
2172 (define-widget 'choice-item 'item
2173 "Button items that delegate action events to their parents."
2174 :action 'widget-parent-action
2175 :format "%[%t%] \n")
2177 ;;; The `radio-button' Widget.
2179 (define-widget 'radio-button 'toggle
2180 "A radio button for use in the `radio' widget."
2181 :notify 'widget-radio-button-notify
2188 :off-glyph "radio0")
2190 (defun widget-radio-button-notify (widget child &optional event)
2192 (widget-apply (widget-get widget :parent) :action widget event))
2194 ;;; The `radio-button-choice' Widget.
2196 (define-widget 'radio-button-choice 'default
2197 "Select one of multiple options."
2198 :convert-widget 'widget-types-convert-widget
2201 :entry-format "%b %v"
2203 :value-create 'widget-radio-value-create
2204 :value-delete 'widget-children-value-delete
2205 :value-get 'widget-radio-value-get
2206 :value-inline 'widget-radio-value-inline
2207 :value-set 'widget-radio-value-set
2208 :error "You must push one of the buttons"
2209 :validate 'widget-radio-validate
2210 :match 'widget-choice-match
2211 :match-inline 'widget-choice-match-inline
2212 :action 'widget-radio-action)
2214 (defun widget-radio-value-create (widget)
2215 ;; Insert all values
2216 (let ((args (widget-get widget :args))
2219 (setq arg (car args)
2221 (widget-radio-add-item widget arg))))
2223 (defun widget-radio-add-item (widget type)
2224 "Add to radio widget WIDGET a new radio button item of type TYPE."
2225 ;; (setq type (widget-convert type))
2226 (and (eq (preceding-char) ?\n)
2227 (widget-get widget :indent)
2228 (insert-char ? (widget-get widget :indent)))
2229 (widget-specify-insert
2230 (let* ((value (widget-get widget :value))
2231 (children (widget-get widget :children))
2232 (buttons (widget-get widget :buttons))
2233 (button-args (or (widget-get type :sibling-args)
2234 (widget-get widget :button-args)))
2236 (chosen (and (null (widget-get widget :choice))
2237 (widget-apply type :match value)))
2239 (insert (widget-get widget :entry-format))
2241 ;; Parse % escapes in format.
2242 (while (re-search-forward "%\\([bv%]\\)" nil t)
2243 (let ((escape (char-after (match-beginning 1))))
2244 (delete-backward-char 2)
2245 (cond ((eq escape ?%)
2248 (setq button (apply 'widget-create-child-and-convert
2249 widget 'radio-button
2250 :value (not (null chosen))
2253 (setq child (if chosen
2254 (widget-create-child-value
2256 (widget-create-child widget type)))
2258 (widget-apply child :deactivate)))
2260 (error "Unknown escape `%c'" escape)))))
2261 ;; Update properties.
2263 (widget-put widget :choice type))
2265 (widget-put child :button button)
2266 (widget-put widget :buttons (nconc buttons (list button))))
2268 (widget-put widget :children (nconc children (list child))))
2271 (defun widget-radio-value-get (widget)
2272 ;; Get value of the child widget.
2273 (let ((chosen (widget-radio-chosen widget)))
2274 (and chosen (widget-value chosen))))
2276 (defun widget-radio-chosen (widget)
2277 "Return the widget representing the chosen radio button."
2278 (let ((children (widget-get widget :children))
2281 (setq current (car children)
2282 children (cdr children))
2283 (when (widget-apply (widget-get current :button) :value-get)
2288 (defun widget-radio-value-inline (widget)
2289 ;; Get value of the child widget.
2290 (let ((children (widget-get widget :children))
2293 (setq current (car children)
2294 children (cdr children))
2295 (when (widget-apply (widget-get current :button) :value-get)
2296 (setq found (widget-apply current :value-inline)
2300 (defun widget-radio-value-set (widget value)
2301 ;; We can't just delete and recreate a radio widget, since children
2302 ;; can be added after the original creation and won't be recreated
2304 (let ((children (widget-get widget :children))
2307 (setq current (car children)
2308 children (cdr children))
2309 (let* ((button (widget-get current :button))
2310 (match (and (not found)
2311 (widget-apply current :match value))))
2312 (widget-value-set button match)
2315 (widget-value-set current value)
2316 (widget-apply current :activate))
2317 (widget-apply current :deactivate))
2318 (setq found (or found match))))))
2320 (defun widget-radio-validate (widget)
2321 ;; Valid if we have made a valid choice.
2322 (let ((children (widget-get widget :children))
2323 current found button)
2324 (while (and children (not found))
2325 (setq current (car children)
2326 children (cdr children)
2327 button (widget-get current :button)
2328 found (widget-apply button :value-get)))
2330 (widget-apply current :validate)
2333 (defun widget-radio-action (widget child event)
2334 ;; Check if a radio button was pressed.
2335 (let ((children (widget-get widget :children))
2336 (buttons (widget-get widget :buttons))
2338 (when (memq child buttons)
2340 (setq current (car children)
2341 children (cdr children))
2342 (let* ((button (widget-get current :button)))
2343 (cond ((eq child button)
2344 (widget-value-set button t)
2345 (widget-apply current :activate))
2346 ((widget-value button)
2347 (widget-value-set button nil)
2348 (widget-apply current :deactivate)))))))
2349 ;; Pass notification to parent.
2350 (widget-apply widget :notify child event))
2352 ;;; The `insert-button' Widget.
2354 (define-widget 'insert-button 'push-button
2355 "An insert button for the `editable-list' widget."
2357 :help-echo "Insert a new item into the list at this position."
2358 :action 'widget-insert-button-action)
2360 (defun widget-insert-button-action (widget &optional event)
2361 ;; Ask the parent to insert a new item.
2362 (widget-apply (widget-get widget :parent)
2363 :insert-before (widget-get widget :widget)))
2365 ;;; The `delete-button' Widget.
2367 (define-widget 'delete-button 'push-button
2368 "A delete button for the `editable-list' widget."
2370 :help-echo "Delete this item from the list."
2371 :action 'widget-delete-button-action)
2373 (defun widget-delete-button-action (widget &optional event)
2374 ;; Ask the parent to insert a new item.
2375 (widget-apply (widget-get widget :parent)
2376 :delete-at (widget-get widget :widget)))
2378 ;;; The `editable-list' Widget.
2380 ;; (defcustom widget-editable-list-gui nil
2381 ;; "If non nil, use GUI push-buttons in editable list when available."
2385 (define-widget 'editable-list 'default
2386 "A variable list of widgets of the same type."
2387 :convert-widget 'widget-types-convert-widget
2390 :format-handler 'widget-editable-list-format-handler
2391 :entry-format "%i %d %v"
2392 :menu-tag "editable-list"
2393 :value-create 'widget-editable-list-value-create
2394 :value-delete 'widget-children-value-delete
2395 :value-get 'widget-editable-list-value-get
2396 :validate 'widget-children-validate
2397 :match 'widget-editable-list-match
2398 :match-inline 'widget-editable-list-match-inline
2399 :insert-before 'widget-editable-list-insert-before
2400 :delete-at 'widget-editable-list-delete-at)
2402 (defun widget-editable-list-format-handler (widget escape)
2403 ;; We recognize the insert button.
2404 ;;; (let ((widget-push-button-gui widget-editable-list-gui))
2405 (cond ((eq escape ?i)
2406 (and (widget-get widget :indent)
2407 (insert-char ?\ (widget-get widget :indent)))
2408 (apply 'widget-create-child-and-convert
2409 widget 'insert-button
2410 (widget-get widget :append-button-args)))
2412 (widget-default-format-handler widget escape)))
2416 (defun widget-editable-list-value-create (widget)
2417 ;; Insert all values
2418 (let* ((value (widget-get widget :value))
2419 (type (nth 0 (widget-get widget :args)))
2421 (widget-put widget :value-pos (copy-marker (point)))
2422 (set-marker-insertion-type (widget-get widget :value-pos) t)
2424 (let ((answer (widget-match-inline type value)))
2426 (setq children (cons (widget-editable-list-entry-create
2428 (if (widget-get type :inline)
2435 (widget-put widget :children (nreverse children))))
2437 (defun widget-editable-list-value-get (widget)
2438 ;; Get value of the child widget.
2439 (apply 'append (mapcar (lambda (child) (widget-apply child :value-inline))
2440 (widget-get widget :children))))
2442 (defun widget-editable-list-match (widget value)
2443 ;; Value must be a list and all the members must match the type.
2445 (null (cdr (widget-editable-list-match-inline widget value)))))
2447 (defun widget-editable-list-match-inline (widget value)
2448 (let ((type (nth 0 (widget-get widget :args)))
2451 (while (and value ok)
2452 (let ((answer (widget-match-inline type value)))
2454 (setq found (append found (car answer))
2457 (cons found value)))
2459 (defun widget-editable-list-insert-before (widget before)
2460 ;; Insert a new child in the list of children.
2462 (let ((children (widget-get widget :children))
2463 (inhibit-read-only t)
2464 before-change-functions
2465 after-change-functions)
2467 (goto-char (widget-get before :entry-from)))
2469 (goto-char (widget-get widget :value-pos))))
2470 (let ((child (widget-editable-list-entry-create
2472 (when (< (widget-get child :entry-from) (widget-get widget :from))
2473 (set-marker (widget-get widget :from)
2474 (widget-get child :entry-from)))
2475 (if (eq (car children) before)
2476 (widget-put widget :children (cons child children))
2477 (while (not (eq (car (cdr children)) before))
2478 (setq children (cdr children)))
2479 (setcdr children (cons child (cdr children)))))))
2481 (widget-apply widget :notify widget))
2483 (defun widget-editable-list-delete-at (widget child)
2484 ;; Delete child from list of children.
2486 (let ((buttons (copy-sequence (widget-get widget :buttons)))
2488 (inhibit-read-only t)
2489 before-change-functions
2490 after-change-functions)
2492 (setq button (car buttons)
2493 buttons (cdr buttons))
2494 (when (eq (widget-get button :widget) child)
2496 :buttons (delq button (widget-get widget :buttons)))
2497 (widget-delete button))))
2498 (let ((entry-from (widget-get child :entry-from))
2499 (entry-to (widget-get child :entry-to))
2500 (inhibit-read-only t)
2501 before-change-functions
2502 after-change-functions)
2503 (widget-delete child)
2504 (delete-region entry-from entry-to)
2505 (set-marker entry-from nil)
2506 (set-marker entry-to nil))
2507 (widget-put widget :children (delq child (widget-get widget :children))))
2509 (widget-apply widget :notify widget))
2511 (defun widget-editable-list-entry-create (widget value conv)
2512 ;; Create a new entry to the list.
2513 (let ((type (nth 0 (widget-get widget :args)))
2514 ;;; (widget-push-button-gui widget-editable-list-gui)
2515 child delete insert)
2516 (widget-specify-insert
2518 (and (widget-get widget :indent)
2519 (insert-char ?\ (widget-get widget :indent)))
2520 (insert (widget-get widget :entry-format)))
2521 ;; Parse % escapes in format.
2522 (while (re-search-forward "%\\(.\\)" nil t)
2523 (let ((escape (char-after (match-beginning 1))))
2524 (delete-backward-char 2)
2525 (cond ((eq escape ?%)
2528 (setq insert (apply 'widget-create-child-and-convert
2529 widget 'insert-button
2530 (widget-get widget :insert-button-args))))
2532 (setq delete (apply 'widget-create-child-and-convert
2533 widget 'delete-button
2534 (widget-get widget :delete-button-args))))
2537 (setq child (widget-create-child-value
2539 (setq child (widget-create-child-value
2541 (widget-apply type :value-to-external
2542 (widget-default-get type))))))
2544 (error "Unknown escape `%c'" escape)))))
2546 :buttons (cons delete
2548 (widget-get widget :buttons))))
2549 (let ((entry-from (point-min-marker))
2550 (entry-to (point-max-marker)))
2551 (set-marker-insertion-type entry-from t)
2552 (set-marker-insertion-type entry-to nil)
2553 (widget-put child :entry-from entry-from)
2554 (widget-put child :entry-to entry-to)))
2555 (widget-put insert :widget child)
2556 (widget-put delete :widget child)
2559 ;;; The `group' Widget.
2561 (define-widget 'group 'default
2562 "A widget which groups other widgets inside."
2563 :convert-widget 'widget-types-convert-widget
2565 :value-create 'widget-group-value-create
2566 :value-delete 'widget-children-value-delete
2567 :value-get 'widget-editable-list-value-get
2568 :default-get 'widget-group-default-get
2569 :validate 'widget-children-validate
2570 :match 'widget-group-match
2571 :match-inline 'widget-group-match-inline)
2573 (defun widget-group-value-create (widget)
2574 ;; Create each component.
2575 (let ((args (widget-get widget :args))
2576 (value (widget-get widget :value))
2577 arg answer children)
2579 (setq arg (car args)
2581 answer (widget-match-inline arg value)
2583 (and (eq (preceding-char) ?\n)
2584 (widget-get widget :indent)
2585 (insert-char ?\ (widget-get widget :indent)))
2586 (push (cond ((null answer)
2587 (widget-create-child widget arg))
2588 ((widget-get arg :inline)
2589 (widget-create-child-value widget arg (car answer)))
2591 (widget-create-child-value widget arg (car (car answer)))))
2593 (widget-put widget :children (nreverse children))))
2595 (defun widget-group-default-get (widget)
2596 ;; Get the default of the components.
2597 (mapcar 'widget-default-get (widget-get widget :args)))
2599 (defun widget-group-match (widget values)
2600 ;; Match if the components match.
2602 (let ((match (widget-group-match-inline widget values)))
2603 (and match (null (cdr match))))))
2605 (defun widget-group-match-inline (widget vals)
2606 ;; Match if the components match.
2607 (let ((args (widget-get widget :args))
2608 argument answer found)
2610 (setq argument (car args)
2612 answer (widget-match-inline argument vals))
2614 (setq vals (cdr answer)
2615 found (append found (car answer)))
2619 (cons found vals))))
2621 ;;; The `visibility' Widget.
2623 (define-widget 'visibility 'item
2624 "An indicator and manipulator for hidden items."
2630 :value-create 'widget-visibility-value-create
2631 :action 'widget-toggle-action
2632 :match (lambda (widget value) t))
2634 (defun widget-visibility-value-create (widget)
2635 ;; Insert text representing the `on' and `off' states.
2636 (let ((on (widget-get widget :on))
2637 (off (widget-get widget :off)))
2639 (setq on (concat widget-push-button-prefix
2641 widget-push-button-suffix))
2644 (setq off (concat widget-push-button-prefix
2646 widget-push-button-suffix))
2648 (if (widget-value widget)
2649 (widget-image-insert widget on "down" "down-pushed")
2650 (widget-image-insert widget off "right" "right-pushed"))))
2652 ;;; The `documentation-link' Widget.
2654 ;; This is a helper widget for `documentation-string'.
2656 (define-widget 'documentation-link 'link
2657 "Link type used in documentation strings."
2659 :help-echo "Describe this symbol"
2660 :action 'widget-documentation-link-action)
2662 (defun widget-documentation-link-action (widget &optional event)
2663 "Display documentation for WIDGET's value. Ignore optional argument EVENT."
2664 (let* ((string (widget-get widget :value))
2665 (symbol (intern string)))
2666 (if (and (fboundp symbol) (boundp symbol))
2667 ;; If there are two doc strings, give the user a way to pick one.
2668 (apropos (concat "\\`" (regexp-quote string) "\\'"))
2669 (if (fboundp symbol)
2670 (describe-function symbol)
2671 (describe-variable symbol)))))
2673 (defcustom widget-documentation-links t
2674 "Add hyperlinks to documentation strings when non-nil."
2676 :group 'widget-documentation)
2678 (defcustom widget-documentation-link-regexp "`\\([^\n`' ]+\\)'"
2679 "Regexp for matching potential links in documentation strings.
2680 The first group should be the link itself."
2682 :group 'widget-documentation)
2684 (defcustom widget-documentation-link-p 'intern-soft
2685 "Predicate used to test if a string is useful as a link.
2686 The value should be a function. The function will be called one
2687 argument, a string, and should return non-nil if there should be a
2688 link for that string."
2690 :options '(widget-documentation-link-p)
2691 :group 'widget-documentation)
2693 (defcustom widget-documentation-link-type 'documentation-link
2694 "Widget type used for links in documentation strings."
2696 :group 'widget-documentation)
2698 (defun widget-documentation-link-add (widget from to)
2699 (widget-specify-doc widget from to)
2700 (when widget-documentation-links
2701 (let ((regexp widget-documentation-link-regexp)
2702 (buttons (widget-get widget :buttons))
2703 (widget-mouse-face (default-value 'widget-mouse-face))
2704 (widget-button-face widget-documentation-face)
2705 (widget-button-pressed-face widget-documentation-face))
2708 (while (re-search-forward regexp to t)
2709 (let ((name (match-string 1))
2710 (begin (match-beginning 1))
2711 (end (match-end 1)))
2712 (when (funcall widget-documentation-link-p name)
2713 (push (widget-convert-button widget-documentation-link-type
2714 begin end :value name)
2716 (widget-put widget :buttons buttons)))
2717 (let ((indent (widget-get widget :indent)))
2718 (when (and indent (not (zerop indent)))
2721 (narrow-to-region from to)
2722 (goto-char (point-min))
2723 (while (search-forward "\n" nil t)
2724 (insert-char ?\ indent)))))))
2726 ;;; The `documentation-string' Widget.
2728 (define-widget 'documentation-string 'item
2729 "A documentation string."
2731 :action 'widget-documentation-string-action
2732 :value-delete 'widget-children-value-delete
2733 :value-create 'widget-documentation-string-value-create)
2735 (defun widget-documentation-string-value-create (widget)
2736 ;; Insert documentation string.
2737 (let ((doc (widget-value widget))
2738 (indent (widget-get widget :indent))
2739 (shown (widget-get (widget-get widget :parent) :documentation-shown))
2741 (if (string-match "\n" doc)
2742 (let ((before (substring doc 0 (match-beginning 0)))
2743 (after (substring doc (match-beginning 0)))
2746 (widget-documentation-link-add widget start (point))
2748 (widget-create-child-and-convert
2750 :help-echo "Show or hide rest of the documentation."
2753 :action 'widget-parent-action
2756 (setq start (point))
2757 (when (and indent (not (zerop indent)))
2758 (insert-char ?\ indent))
2760 (widget-documentation-link-add widget start (point)))
2761 (widget-put widget :buttons (list button)))
2763 (widget-documentation-link-add widget start (point))))
2766 (defun widget-documentation-string-action (widget &rest ignore)
2767 ;; Toggle documentation.
2768 (let ((parent (widget-get widget :parent)))
2769 (widget-put parent :documentation-shown
2770 (not (widget-get parent :documentation-shown))))
2772 (widget-value-set widget (widget-value widget)))
2774 ;;; The Sexp Widgets.
2776 (define-widget 'const 'item
2777 "An immutable sexp."
2778 :prompt-value 'widget-const-prompt-value
2781 (defun widget-const-prompt-value (widget prompt value unbound)
2782 ;; Return the value of the const.
2783 (widget-value widget))
2785 (define-widget 'function-item 'const
2786 "An immutable function name."
2788 :documentation-property (lambda (symbol)
2790 (documentation symbol t)
2793 (define-widget 'variable-item 'const
2794 "An immutable variable name."
2796 :documentation-property 'variable-documentation)
2798 (define-widget 'other 'sexp
2799 "Matches any value, but doesn't let the user edit the value.
2800 This is useful as last item in a `choice' widget.
2801 You should use this widget type with a default value,
2802 as in (other DEFAULT) or (other :tag \"NAME\" DEFAULT).
2803 If the user selects this alternative, that specifies DEFAULT
2809 (defvar widget-string-prompt-value-history nil
2810 "History of input to `widget-string-prompt-value'.")
2812 (define-widget 'string 'editable-field
2815 :format "%{%t%}: %v"
2816 :complete-function 'ispell-complete-word
2817 :prompt-history 'widget-string-prompt-value-history)
2819 (define-widget 'regexp 'string
2820 "A regular expression."
2821 :match 'widget-regexp-match
2822 :validate 'widget-regexp-validate
2823 ;; Doesn't work well with terminating newline.
2824 ;; :value-face 'widget-single-line-field-face
2827 (defun widget-regexp-match (widget value)
2828 ;; Match valid regexps.
2829 (and (stringp value)
2832 (string-match value ""))
2835 (defun widget-regexp-validate (widget)
2836 "Check that the value of WIDGET is a valid regexp."
2837 (condition-case data
2839 (string-match (widget-value widget) ""))
2840 (error (widget-put widget :error (error-message-string data))
2843 (define-widget 'file 'string
2845 It will read a file name from the minibuffer when invoked."
2846 :complete-function 'widget-file-complete
2847 :prompt-value 'widget-file-prompt-value
2848 :format "%{%t%}: %v"
2849 ;; Doesn't work well with terminating newline.
2850 ;; :value-face 'widget-single-line-field-face
2853 (defun widget-file-complete ()
2854 "Perform completion on file name preceding point."
2856 (let* ((end (point))
2857 (beg (save-excursion
2858 (skip-chars-backward "^ ")
2860 (pattern (buffer-substring beg end))
2861 (name-part (file-name-nondirectory pattern))
2862 (directory (file-name-directory pattern))
2863 (completion (file-name-completion name-part directory)))
2864 (cond ((eq completion t))
2866 (message "Can't find completion for \"%s\"" pattern)
2868 ((not (string= name-part completion))
2869 (delete-region beg end)
2870 (insert (expand-file-name completion directory)))
2872 (message "Making completion list...")
2873 (with-output-to-temp-buffer "*Completions*"
2874 (display-completion-list
2875 (sort (file-name-all-completions name-part directory)
2877 (message "Making completion list...%s" "done")))))
2879 (defun widget-file-prompt-value (widget prompt value unbound)
2880 ;; Read file from minibuffer.
2881 (abbreviate-file-name
2883 (read-file-name prompt)
2884 (let ((prompt2 (format "%s (default %s) " prompt value))
2885 (dir (file-name-directory value))
2886 (file (file-name-nondirectory value))
2887 (must-match (widget-get widget :must-match)))
2888 (read-file-name prompt2 dir nil must-match file)))))
2890 ;;;(defun widget-file-action (widget &optional event)
2891 ;;; ;; Read a file name from the minibuffer.
2892 ;;; (let* ((value (widget-value widget))
2893 ;;; (dir (file-name-directory value))
2894 ;;; (file (file-name-nondirectory value))
2895 ;;; (menu-tag (widget-apply widget :menu-tag-get))
2896 ;;; (must-match (widget-get widget :must-match))
2897 ;;; (answer (read-file-name (concat menu-tag ": (default `" value "') ")
2898 ;;; dir nil must-match file)))
2899 ;;; (widget-value-set widget (abbreviate-file-name answer))
2901 ;;; (widget-apply widget :notify widget event)))
2903 (define-widget 'directory 'file
2904 "A directory widget.
2905 It will read a directory name from the minibuffer when invoked."
2908 (defvar widget-symbol-prompt-value-history nil
2909 "History of input to `widget-symbol-prompt-value'.")
2911 (define-widget 'symbol 'editable-field
2915 :format "%{%t%}: %v"
2916 :match (lambda (widget value) (symbolp value))
2917 :complete-function 'lisp-complete-symbol
2918 :prompt-internal 'widget-symbol-prompt-internal
2919 :prompt-match 'symbolp
2920 :prompt-history 'widget-symbol-prompt-value-history
2921 :value-to-internal (lambda (widget value)
2925 :value-to-external (lambda (widget value)
2930 (defun widget-symbol-prompt-internal (widget prompt initial history)
2931 ;; Read file from minibuffer.
2932 (let ((answer (completing-read prompt obarray
2933 (widget-get widget :prompt-match)
2934 nil initial history)))
2935 (if (and (stringp answer)
2936 (not (zerop (length answer))))
2938 (error "No value"))))
2940 (defvar widget-function-prompt-value-history nil
2941 "History of input to `widget-function-prompt-value'.")
2943 (define-widget 'function 'sexp
2945 :complete-function (lambda ()
2947 (lisp-complete-symbol 'fboundp))
2948 :prompt-value 'widget-field-prompt-value
2949 :prompt-internal 'widget-symbol-prompt-internal
2950 :prompt-match 'fboundp
2951 :prompt-history 'widget-function-prompt-value-history
2952 :action 'widget-field-action
2953 :validate (lambda (widget)
2954 (unless (functionp (widget-value widget))
2955 (widget-put widget :error (format "Invalid function: %S"
2956 (widget-value widget)))
2961 (defvar widget-variable-prompt-value-history nil
2962 "History of input to `widget-variable-prompt-value'.")
2964 (define-widget 'variable 'symbol
2965 ;; Should complete on variables.
2967 :prompt-match 'boundp
2968 :prompt-history 'widget-variable-prompt-value-history
2969 :complete-function (lambda ()
2971 (lisp-complete-symbol 'boundp))
2974 (defvar widget-coding-system-prompt-value-history nil
2975 "History of input to `widget-coding-system-prompt-value'.")
2977 (define-widget 'coding-system 'symbol
2978 "A MULE coding-system."
2979 :format "%{%t%}: %v"
2980 :tag "Coding system"
2982 :prompt-history 'widget-coding-system-prompt-value-history
2983 :prompt-value 'widget-coding-system-prompt-value
2984 :action 'widget-coding-system-action
2985 :complete-function (lambda ()
2987 (lisp-complete-symbol 'coding-system-p))
2988 :validate (lambda (widget)
2989 (unless (coding-system-p (widget-value widget))
2990 (widget-put widget :error (format "Invalid coding system: %S"
2991 (widget-value widget)))
2994 :prompt-match 'coding-system-p)
2996 (defun widget-coding-system-prompt-value (widget prompt value unbound)
2997 "Read coding-system from minibuffer."
2998 (if (widget-get widget :base-only)
3000 (completing-read (format "%s (default %s) " prompt value)
3001 (mapcar #'list (coding-system-list t)) nil nil nil
3002 coding-system-history))
3003 (read-coding-system (format "%s (default %s) " prompt value) value)))
3005 (defun widget-coding-system-action (widget &optional event)
3007 (widget-coding-system-prompt-value
3009 (widget-apply widget :menu-tag-get)
3010 (widget-value widget)
3012 (widget-value-set widget answer)
3013 (widget-apply widget :notify widget event)
3016 (define-widget 'sexp 'editable-field
3017 "An arbitrary Lisp expression."
3018 :tag "Lisp expression"
3019 :format "%{%t%}: %v"
3021 :validate 'widget-sexp-validate
3022 :match (lambda (widget value) t)
3023 :value-to-internal 'widget-sexp-value-to-internal
3024 :value-to-external (lambda (widget value) (read value))
3025 :prompt-history 'widget-sexp-prompt-value-history
3026 :prompt-value 'widget-sexp-prompt-value)
3028 (defun widget-sexp-value-to-internal (widget value)
3029 ;; Use pp for printer representation.
3030 (let ((pp (if (symbolp value)
3031 (prin1-to-string value)
3032 (pp-to-string value))))
3033 (while (string-match "\n\\'" pp)
3034 (setq pp (substring pp 0 -1)))
3035 (if (or (string-match "\n\\'" pp)
3040 (defun widget-sexp-validate (widget)
3041 ;; Valid if we can read the string and there is no junk left after it.
3043 (insert (widget-apply widget :value-get))
3044 (goto-char (point-min))
3046 (condition-case data
3048 ;; Avoid a confusing end-of-file error.
3049 (skip-syntax-forward "\\s-")
3051 (setq err "Empty sexp -- use `nil'?")
3052 (unless (widget-apply widget :match (read (current-buffer)))
3053 (setq err (widget-get widget :type-error))))
3054 (if (and (not (eobp))
3056 (setq err (format "Junk at end of expression: %s"
3057 (buffer-substring (point)
3059 (end-of-file ; Avoid confusing error message.
3060 (setq err "Unbalanced sexp"))
3061 (error (setq err (error-message-string data))))
3064 (widget-put widget :error err)
3067 (defvar widget-sexp-prompt-value-history nil
3068 "History of input to `widget-sexp-prompt-value'.")
3070 (defun widget-sexp-prompt-value (widget prompt value unbound)
3071 ;; Read an arbitrary sexp.
3072 (let ((found (read-string prompt
3073 (if unbound nil (cons (prin1-to-string value) 0))
3074 (widget-get widget :prompt-history))))
3075 (let ((answer (read-from-string found)))
3076 (unless (= (cdr answer) (length found))
3077 (error "Junk at end of expression: %s"
3078 (substring found (cdr answer))))
3081 (define-widget 'restricted-sexp 'sexp
3082 "A Lisp expression restricted to values that match.
3083 To use this type, you must define :match or :match-alternatives."
3084 :type-error "The specified value is not valid"
3085 :match 'widget-restricted-sexp-match
3086 :value-to-internal (lambda (widget value)
3087 (if (widget-apply widget :match value)
3088 (prin1-to-string value)
3091 (defun widget-restricted-sexp-match (widget value)
3092 (let ((alternatives (widget-get widget :match-alternatives))
3094 (while (and alternatives (not matched))
3095 (if (cond ((functionp (car alternatives))
3096 (funcall (car alternatives) value))
3097 ((and (consp (car alternatives))
3098 (eq (car (car alternatives)) 'quote))
3099 (eq value (nth 1 (car alternatives)))))
3101 (setq alternatives (cdr alternatives)))
3104 (define-widget 'integer 'restricted-sexp
3108 :type-error "This field should contain an integer"
3109 :match-alternatives '(integerp))
3111 (define-widget 'number 'restricted-sexp
3112 "A floating point number."
3115 :type-error "This field should contain a number"
3116 :match-alternatives '(numberp))
3118 (define-widget 'character 'editable-field
3123 :format "%{%t%}: %v\n"
3124 :valid-regexp "\\`.\\'"
3125 :error "This field should contain a single character"
3126 :value-to-internal (lambda (widget value)
3129 (char-to-string value)))
3130 :value-to-external (lambda (widget value)
3134 :match (lambda (widget value)
3135 (char-valid-p value)))
3137 (define-widget 'list 'group
3140 :format "%{%t%}:\n%v")
3142 (define-widget 'vector 'group
3145 :format "%{%t%}:\n%v"
3146 :match 'widget-vector-match
3147 :value-to-internal (lambda (widget value) (append value nil))
3148 :value-to-external (lambda (widget value) (apply 'vector value)))
3150 (defun widget-vector-match (widget value)
3151 (and (vectorp value)
3152 (widget-group-match widget
3153 (widget-apply widget :value-to-internal value))))
3155 (define-widget 'cons 'group
3158 :format "%{%t%}:\n%v"
3159 :match 'widget-cons-match
3160 :value-to-internal (lambda (widget value)
3161 (list (car value) (cdr value)))
3162 :value-to-external (lambda (widget value)
3163 (cons (nth 0 value) (nth 1 value))))
3165 (defun widget-cons-match (widget value)
3167 (widget-group-match widget
3168 (widget-apply widget :value-to-internal value))))
3170 ;;; The `plist' Widget.
3174 (define-widget 'plist 'list
3176 :key-type '(symbol :tag "Key")
3177 :value-type '(sexp :tag "Value")
3178 :convert-widget 'widget-plist-convert-widget
3181 (defvar widget-plist-value-type) ;Dynamic variable
3183 (defun widget-plist-convert-widget (widget)
3184 ;; Handle `:options'.
3185 (let* ((options (widget-get widget :options))
3186 (other `(editable-list :inline t
3188 ,(widget-get widget :key-type)
3189 ,(widget-get widget :value-type))))
3191 (list `(checklist :inline t
3193 ,@(mapcar 'widget-plist-convert-option
3197 (widget-put widget :args args)
3200 (defun widget-plist-convert-option (option)
3201 ;; Convert a single plist option.
3202 (let (key-type value-type)
3204 (let ((key (nth 0 option)))
3205 (setq value-type (nth 1 option))
3208 (setq key-type `(const ,key))))
3209 (setq key-type `(const ,option)
3210 value-type widget-plist-value-type))
3211 `(group :format "Key: %v" :inline t ,key-type ,value-type)))
3214 ;;; The `alist' Widget.
3216 ;; Association lists.
3218 (define-widget 'alist 'list
3219 "An association list."
3220 :key-type '(sexp :tag "Key")
3221 :value-type '(sexp :tag "Value")
3222 :convert-widget 'widget-alist-convert-widget
3225 (defvar widget-alist-value-type) ;Dynamic variable
3227 (defun widget-alist-convert-widget (widget)
3228 ;; Handle `:options'.
3229 (let* ((options (widget-get widget :options))
3230 (other `(editable-list :inline t
3232 ,(widget-get widget :key-type)
3233 ,(widget-get widget :value-type))))
3235 (list `(checklist :inline t
3237 ,@(mapcar 'widget-alist-convert-option
3241 (widget-put widget :args args)
3244 (defun widget-alist-convert-option (option)
3245 ;; Convert a single alist option.
3246 (let (key-type value-type)
3248 (let ((key (nth 0 option)))
3249 (setq value-type (nth 1 option))
3252 (setq key-type `(const ,key))))
3253 (setq key-type `(const ,option)
3254 value-type widget-alist-value-type))
3255 `(cons :format "Key: %v" ,key-type ,value-type)))
3257 (define-widget 'choice 'menu-choice
3258 "A union of several sexp types."
3260 :format "%{%t%}: %[Value Menu%] %v"
3261 :button-prefix 'widget-push-button-prefix
3262 :button-suffix 'widget-push-button-suffix
3263 :prompt-value 'widget-choice-prompt-value)
3265 (defun widget-choice-prompt-value (widget prompt value unbound)
3267 (let ((args (widget-get widget :args))
3268 (completion-ignore-case (widget-get widget :case-fold))
3269 current choices old)
3270 ;; Find the first arg that matches VALUE.
3273 (if (widget-apply (car look) :match value)
3274 (setq old (car look)
3276 (setq look (cdr look)))))
3279 (cond ((= (length args) 0)
3281 ((= (length args) 1)
3283 ((and (= (length args) 2)
3285 (if (eq old (nth 0 args))
3290 (setq current (car args)
3293 (cons (cons (widget-apply current :menu-tag-get)
3296 (let ((val (completing-read prompt choices nil t)))
3298 (let ((try (try-completion val choices)))
3301 (cdr (assoc val choices)))
3304 (widget-prompt-value current prompt nil t)
3307 (define-widget 'radio 'radio-button-choice
3308 "A union of several sexp types."
3310 :format "%{%t%}:\n%v"
3311 :prompt-value 'widget-choice-prompt-value)
3313 (define-widget 'repeat 'editable-list
3314 "A variable length homogeneous list."
3316 :format "%{%t%}:\n%v%i\n")
3318 (define-widget 'set 'checklist
3319 "A list of members from a fixed set."
3321 :format "%{%t%}:\n%v")
3323 (define-widget 'boolean 'toggle
3324 "To be nil or non-nil, that is the question."
3326 :prompt-value 'widget-boolean-prompt-value
3327 :button-prefix 'widget-push-button-prefix
3328 :button-suffix 'widget-push-button-suffix
3329 :format "%{%t%}: %[Toggle%] %v\n"
3333 (defun widget-boolean-prompt-value (widget prompt value unbound)
3334 ;; Toggle a boolean.
3337 ;;; The `color' Widget.
3339 (define-widget 'color 'editable-field
3340 "Choose a color name (with sample)."
3341 :format "%t: %v (%{sample%})\n"
3345 :complete 'widget-color-complete
3346 :sample-face-get 'widget-color-sample-face-get
3347 :notify 'widget-color-notify
3348 :action 'widget-color-action)
3350 (defun widget-color-complete (widget)
3351 "Complete the color in WIDGET."
3352 (require 'facemenu) ; for facemenu-color-alist
3353 (let* ((prefix (buffer-substring-no-properties (widget-field-start widget)
3355 (list (or facemenu-color-alist
3356 (mapcar 'list (defined-colors))))
3357 (completion (try-completion prefix list)))
3358 (cond ((eq completion t)
3359 (message "Exact match."))
3361 (error "Can't find completion for \"%s\"" prefix))
3362 ((not (string-equal prefix completion))
3363 (insert-and-inherit (substring completion (length prefix))))
3365 (message "Making completion list...")
3366 (with-output-to-temp-buffer "*Completions*"
3367 (display-completion-list (all-completions prefix list nil)))
3368 (message "Making completion list...done")))))
3370 (defun widget-color-sample-face-get (widget)
3371 (let* ((value (condition-case nil
3372 (widget-value widget)
3373 (error (widget-get widget :value))))
3374 (symbol (intern (concat "fg:" value))))
3376 (facemenu-get-face symbol)
3379 (defun widget-color-action (widget &optional event)
3380 ;; Prompt for a color.
3381 (let* ((tag (widget-apply widget :menu-tag-get))
3382 (prompt (concat tag ": "))
3383 (value (widget-value widget))
3384 (start (widget-field-start widget))
3385 (pos (cond ((< (point) start)
3387 ((> (point) (+ start (length value)))
3390 (- (point) start))))
3391 (answer (facemenu-read-color prompt)))
3392 (unless (zerop (length answer))
3393 (widget-value-set widget answer)
3395 (widget-apply widget :notify widget event))))
3397 (defun widget-color-notify (widget child &optional event)
3398 "Update the sample, and notofy the parent."
3399 (overlay-put (widget-get widget :sample-overlay)
3400 'face (widget-apply widget :sample-face-get))
3401 (widget-default-notify widget child event))
3405 (defun widget-echo-help (pos)
3406 "Display help-echo text for widget at POS."
3407 (let* ((widget (widget-at pos))
3408 (help-echo (and widget (widget-get widget :help-echo))))
3409 (if (functionp help-echo)
3410 (setq help-echo (funcall help-echo widget)))
3411 (if (stringp help-echo)
3412 (message "%s" help-echo))))
3418 ;;; wid-edit.el ends here