1 ;;; wid-edit.el --- Functions for creating and using widgets -*-byte-compile-dynamic: t;-*-
3 ;; Copyright (C) 1996,97,1999,2000,01,02,2003, 2004, 2005 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., 51 Franklin Street, Fifth Floor,
24 ;; Boston, MA 02110-1301, 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 (defun widget-button-release-event-p (event)
67 "Non-nil if EVENT is a mouse-button-release event object."
69 (memq (event-basic-type event
) '(mouse-1 mouse-2 mouse-3
))
70 (or (memq 'click
(event-modifiers event
))
71 (memq 'drag
(event-modifiers event
)))))
76 "Customization support for the Widget Library."
77 :link
'(custom-manual "(widget)Top")
78 :link
'(emacs-library-link :tag
"Lisp File" "widget.el")
83 (defgroup widget-documentation nil
84 "Options controling the display of documentation strings."
87 (defgroup widget-faces nil
88 "Faces used by the widget library."
92 (defvar widget-documentation-face
'widget-documentation
93 "Face used for documentation strings in widgets.
94 This exists as a variable so it can be set locally in certain buffers.")
96 (defface widget-documentation
'((((class color
)
98 (:foreground
"lime green"))
101 (:foreground
"dark green"))
103 "Face used for documentation text."
104 :group
'widget-documentation
105 :group
'widget-faces
)
106 ;; backward compatibility alias
107 (put 'widget-documentation-face
'face-alias
'widget-documentation
)
109 (defvar widget-button-face
'widget-button
110 "Face used for buttons in widgets.
111 This exists as a variable so it can be set locally in certain buffers.")
113 (defface widget-button
'((t (:weight bold
)))
114 "Face used for widget buttons."
115 :group
'widget-faces
)
116 ;; backward compatibility alias
117 (put 'widget-button-face
'face-alias
'widget-button
)
119 (defcustom widget-mouse-face
'highlight
120 "Face used for widget buttons when the mouse is above them."
122 :group
'widget-faces
)
124 ;; TTY gets special definitions here and in the next defface, because
125 ;; the gray colors defined for other displays cause black text on a black
126 ;; background, at least on light-background TTYs.
127 (defface widget-field
'((((type tty
))
128 :background
"yellow3"
130 (((class grayscale color
)
132 :background
"gray85")
133 (((class grayscale color
)
135 :background
"dim gray")
138 "Face used for editable fields."
139 :group
'widget-faces
)
140 ;; backward-compatibility alias
141 (put 'widget-field-face
'face-alias
'widget-field
)
143 (defface widget-single-line-field
'((((type tty
))
146 (((class grayscale color
)
148 :background
"gray85")
149 (((class grayscale color
)
151 :background
"dim gray")
154 "Face used for editable fields spanning only a single line."
155 :group
'widget-faces
)
156 ;; backward-compatibility alias
157 (put 'widget-single-line-field-face
'face-alias
'widget-single-line-field
)
159 ;;; This causes display-table to be loaded, and not usefully.
160 ;;;(defvar widget-single-line-display-table
161 ;;; (let ((table (make-display-table)))
162 ;;; (aset table 9 "^I")
163 ;;; (aset table 10 "^J")
165 ;;; "Display table used for single-line editable fields.")
167 ;;;(when (fboundp 'set-face-display-table)
168 ;;; (set-face-display-table 'widget-single-line-field-face
169 ;;; widget-single-line-display-table))
171 ;;; Utility functions.
173 ;; These are not really widget specific.
175 (defun widget-princ-to-string (object)
176 "Return string representation of OBJECT, any Lisp object.
177 No quoting characters are used; no delimiters are printed around
178 the contents of strings."
179 (with-output-to-string
182 (defun widget-clear-undo ()
183 "Clear all undo information."
184 (buffer-disable-undo (current-buffer))
185 (buffer-enable-undo))
187 (defcustom widget-menu-max-size
40
188 "Largest number of items allowed in a popup-menu.
189 Larger menus are read through the minibuffer."
193 (defcustom widget-menu-max-shortcuts
40
194 "Largest number of items for which it works to choose one with a character.
195 For a larger number of items, the minibuffer is used."
199 (defcustom widget-menu-minibuffer-flag nil
200 "*Control how to ask for a choice from the keyboard.
201 Non-nil means use the minibuffer;
202 nil means read a single character."
206 (defun widget-choose (title items
&optional event
)
207 "Choose an item from a list.
209 First argument TITLE is the name of the list.
210 Second argument ITEMS is a list whose members are either
211 (NAME . VALUE), to indicate selectable items, or just strings to
212 indicate unselectable items.
213 Optional third argument EVENT is an input event.
215 The user is asked to choose between each NAME from the items alist,
216 and the VALUE of the chosen element will be returned. If EVENT is a
217 mouse event, and the number of elements in items is less than
218 `widget-menu-max-size', a popup menu will be used, otherwise the
220 (cond ((and (< (length items
) widget-menu-max-size
)
221 event
(display-popup-menus-p))
224 (list title
(cons "" items
))))
225 ((or widget-menu-minibuffer-flag
226 (> (length items
) widget-menu-max-shortcuts
))
227 ;; Read the choice of name from the minibuffer.
228 (setq items
(widget-remove-if 'stringp items
))
229 (let ((val (completing-read (concat title
": ") items nil t
)))
231 (let ((try (try-completion val items
)))
234 (cdr (assoc val items
))))))
236 ;; Construct a menu of the choices
237 ;; and then use it for prompting for a single character.
238 (let* ((overriding-terminal-local-map (make-sparse-keymap))
240 map choice some-choice-enabled value
)
241 ;; Define SPC as a prefix char to get to this menu.
242 (define-key overriding-terminal-local-map
" "
243 (setq map
(make-sparse-keymap title
)))
244 (with-current-buffer (get-buffer-create " widget-choose")
246 (insert "Available choices:\n\n")
248 (setq choice
(car items
) items
(cdr items
))
250 (let* ((name (car choice
))
251 (function (cdr choice
)))
252 (insert (format "%c = %s\n" next-digit name
))
253 (define-key map
(vector next-digit
) function
)
254 (setq some-choice-enabled t
)))
255 ;; Allocate digits to disabled alternatives
256 ;; so that the digit of a given alternative never varies.
257 (setq next-digit
(1+ next-digit
)))
258 (insert "\nC-g = Quit"))
259 (or some-choice-enabled
260 (error "None of the choices is currently meaningful"))
261 (define-key map
[?\C-g
] 'keyboard-quit
)
262 (define-key map
[t] 'keyboard-quit)
263 (define-key map [?\M-\C-v] 'scroll-other-window)
264 (define-key map [?\M--] 'negative-argument)
265 (setcdr map (nreverse (cdr map)))
266 ;; Read a char with the menu, and return the result
267 ;; that corresponds to it.
268 (save-window-excursion
269 (let ((buf (get-buffer " widget-choose")))
270 (fit-window-to-buffer (display-buffer buf))
271 (let ((cursor-in-echo-area t)
275 (while (not (or (and (>= char ?0) (< char next-digit))
276 (eq value 'keyboard-quit)))
277 ;; Unread a SPC to lead to our new menu.
278 (setq unread-command-events (cons ?\s unread-command-events))
279 (setq keys (read-key-sequence title))
281 (lookup-key overriding-terminal-local-map keys t)
282 char (string-to-char (substring keys 1)))
283 (cond ((eq value 'scroll-other-window)
284 (let ((minibuffer-scroll-window
285 (get-buffer-window buf)))
287 (scroll-other-window-down
288 (window-height minibuffer-scroll-window))
289 (scroll-other-window))
291 ((eq value 'negative-argument)
295 (when (eq value 'keyboard-quit)
299 (defun widget-remove-if (predictate list)
300 (let (result (tail list))
302 (or (funcall predictate (car tail))
303 (setq result (cons (car tail) result)))
304 (setq tail (cdr tail)))
307 ;;; Widget text specifications.
309 ;; These functions are for specifying text properties.
311 ;; We can set it to nil now that get_local_map uses get_pos_property.
312 (defconst widget-field-add-space nil
313 "Non-nil means add extra space at the end of editable text fields.
314 If you don't add the space, it will become impossible to edit a zero
317 (defvar widget-field-use-before-change t
318 "Non-nil means use `before-change-functions' to track editable fields.
319 This enables the use of undo, but doesn't work on Emacs 19.34 and earlier.
320 Using before hooks also means that the :notify function can't know the
323 (defun widget-specify-field (widget from to)
324 "Specify editable button for WIDGET between FROM and TO."
325 ;; Terminating space is not part of the field, but necessary in
326 ;; order for local-map to work. Remove next sexp if local-map works
327 ;; at the end of the overlay.
330 (cond ((null (widget-get widget :size))
332 (widget-field-add-space
333 (insert-and-inherit " ")))
335 (let ((keymap (widget-get widget :keymap))
336 (face (or (widget-get widget :value-face) 'widget-field))
337 (help-echo (widget-get widget :help-echo))
338 (follow-link (widget-get widget :follow-link))
340 (or (not widget-field-add-space) (widget-get widget :size))))
341 (if (functionp help-echo)
342 (setq help-echo 'widget-mouse-help))
343 (when (= (char-before to) ?\n)
344 ;; When the last character in the field is a newline, we want to
345 ;; give it a `field' char-property of `boundary', which helps the
346 ;; C-n/C-p act more naturally when entering/leaving the field. We
347 ;; do this by making a small secondary overlay to contain just that
349 (let ((overlay (make-overlay (1- to) to nil t nil)))
350 (overlay-put overlay 'field 'boundary)
351 ;; We need the real field for tabbing.
352 (overlay-put overlay 'real-field widget)
353 ;; Use `local-map' here, not `keymap', so that normal editing
354 ;; works in the field when, say, Custom uses `suppress-keymap'.
355 (overlay-put overlay 'local-map keymap)
356 (overlay-put overlay 'face face)
357 (overlay-put overlay 'follow-link follow-link)
358 (overlay-put overlay 'help-echo help-echo))
360 (setq rear-sticky t))
361 (let ((overlay (make-overlay from to nil nil rear-sticky)))
362 (widget-put widget :field-overlay overlay)
363 ;;(overlay-put overlay 'detachable nil)
364 (overlay-put overlay 'field widget)
365 (overlay-put overlay 'local-map keymap)
366 (overlay-put overlay 'face face)
367 (overlay-put overlay 'follow-link follow-link)
368 (overlay-put overlay 'help-echo help-echo)))
369 (widget-specify-secret widget))
371 (defun widget-specify-secret (field)
372 "Replace text in FIELD with value of `:secret', if non-nil."
373 (let ((secret (widget-get field :secret))
374 (size (widget-get field :size)))
376 (let ((begin (widget-field-start field))
377 (end (widget-field-end field)))
379 (while (and (> end begin)
380 (eq (char-after (1- end)) ?\s))
381 (setq end (1- end))))
383 (let ((old (char-after begin)))
384 (unless (eq old secret)
385 (subst-char-in-region begin (1+ begin) old secret)
386 (put-text-property begin (1+ begin) 'secret old))
387 (setq begin (1+ begin))))))))
389 (defun widget-specify-button (widget from to)
390 "Specify button for WIDGET between FROM and TO."
391 (let ((overlay (make-overlay from to nil t nil))
392 (follow-link (widget-get widget :follow-link))
393 (help-echo (widget-get widget :help-echo)))
394 (widget-put widget :button-overlay overlay)
395 (if (functionp help-echo)
396 (setq help-echo 'widget-mouse-help))
397 (overlay-put overlay 'button widget)
398 (overlay-put overlay 'keymap (widget-get widget :keymap))
399 (overlay-put overlay 'evaporate t)
400 ;; We want to avoid the face with image buttons.
401 (unless (widget-get widget :suppress-face)
402 (overlay-put overlay 'face (widget-apply widget :button-face-get))
403 ; Text terminals cannot change mouse pointer shape, so use mouse
405 (or (display-graphic-p)
406 (overlay-put overlay 'mouse-face widget-mouse-face)))
407 (overlay-put overlay 'pointer 'hand)
408 (overlay-put overlay 'follow-link follow-link)
409 (overlay-put overlay 'help-echo help-echo)))
411 (defun widget-mouse-help (window overlay point)
412 "Help-echo callback for widgets whose :help-echo is a function."
413 (with-current-buffer (overlay-buffer overlay)
414 (let* ((widget (widget-at (overlay-start overlay)))
415 (help-echo (if widget (widget-get widget :help-echo))))
416 (if (functionp help-echo)
417 (funcall help-echo widget)
420 (defun widget-specify-sample (widget from to)
421 "Specify sample for WIDGET between FROM and TO."
422 (let ((overlay (make-overlay from to nil t nil)))
423 (overlay-put overlay 'face (widget-apply widget :sample-face-get))
424 (overlay-put overlay 'evaporate t)
425 (widget-put widget :sample-overlay overlay)))
427 (defun widget-specify-doc (widget from to)
428 "Specify documentation for WIDGET between FROM and TO."
429 (let ((overlay (make-overlay from to nil t nil)))
430 (overlay-put overlay 'widget-doc widget)
431 (overlay-put overlay 'face widget-documentation-face)
432 (overlay-put overlay 'evaporate t)
433 (widget-put widget :doc-overlay overlay)))
435 (defmacro widget-specify-insert (&rest form)
436 "Execute FORM without inheriting any text properties."
438 (let ((inhibit-read-only t)
439 (inhibit-modification-hooks t))
440 (narrow-to-region (point) (point))
441 (prog1 (progn ,@form)
442 (goto-char (point-max))))))
444 (defface widget-inactive
445 '((t :inherit shadow))
446 "Face used for inactive widgets."
447 :group 'widget-faces)
448 ;; backward-compatibility alias
449 (put 'widget-inactive-face 'face-alias 'widget-inactive)
451 (defun widget-specify-inactive (widget from to)
452 "Make WIDGET inactive for user modifications."
453 (unless (widget-get widget :inactive)
454 (let ((overlay (make-overlay from to nil t nil)))
455 (overlay-put overlay 'face 'widget-inactive)
456 ;; This is disabled, as it makes the mouse cursor change shape.
457 ;; (overlay-put overlay 'mouse-face 'widget-inactive)
458 (overlay-put overlay 'evaporate t)
459 (overlay-put overlay 'priority 100)
460 (overlay-put overlay 'modification-hooks '(widget-overlay-inactive))
461 (widget-put widget :inactive overlay))))
463 (defun widget-overlay-inactive (&rest junk)
464 "Ignoring the arguments, signal an error."
465 (unless inhibit-read-only
466 (error "The widget here is not active")))
469 (defun widget-specify-active (widget)
470 "Make WIDGET active for user modifications."
471 (let ((inactive (widget-get widget :inactive)))
473 (delete-overlay inactive)
474 (widget-put widget :inactive nil))))
476 ;;; Widget Properties.
478 (defsubst widget-type (widget)
479 "Return the type of WIDGET, a symbol."
483 (defun widgetp (widget)
484 "Return non-nil iff WIDGET is a widget."
486 (get widget 'widget-type)
488 (symbolp (car widget))
489 (get (car widget) 'widget-type))))
491 (defun widget-get-indirect (widget property)
492 "In WIDGET, get the value of PROPERTY.
493 If the value is a symbol, return its binding.
494 Otherwise, just return the value."
495 (let ((value (widget-get widget property)))
500 (defun widget-member (widget property)
501 "Non-nil iff there is a definition in WIDGET for PROPERTY."
502 (cond ((plist-member (cdr widget) property)
505 (widget-member (get (car widget) 'widget-type) property))
508 (defun widget-value (widget)
509 "Extract the current value of WIDGET."
511 :value-to-external (widget-apply widget :value-get)))
513 (defun widget-value-set (widget value)
514 "Set the current value of WIDGET to VALUE."
516 :value-set (widget-apply widget
517 :value-to-internal value)))
519 (defun widget-default-get (widget)
520 "Extract the default external value of WIDGET."
521 (widget-apply widget :value-to-external
522 (or (widget-get widget :value)
523 (widget-apply widget :default-get))))
525 (defun widget-match-inline (widget vals)
526 "In WIDGET, match the start of VALS."
527 (cond ((widget-get widget :inline)
528 (widget-apply widget :match-inline vals))
530 (widget-apply widget :match (car vals)))
531 (cons (list (car vals)) (cdr vals)))
534 (defun widget-apply-action (widget &optional event)
535 "Apply :action in WIDGET in response to EVENT."
536 (if (widget-apply widget :active)
537 (widget-apply widget :action event)
538 (error "Attempt to perform action on inactive widget")))
540 ;;; Helper functions.
542 ;; These are widget specific.
545 (defun widget-prompt-value (widget prompt &optional value unbound)
546 "Prompt for a value matching WIDGET, using PROMPT.
547 The current value is assumed to be VALUE, unless UNBOUND is non-nil."
548 (unless (listp widget)
549 (setq widget (list widget)))
550 (setq prompt (format "[%s] %s" (widget-type widget) prompt))
551 (setq widget (widget-convert widget))
552 (let ((answer (widget-apply widget :prompt-value prompt value unbound)))
553 (unless (widget-apply widget :match answer)
554 (error "Value does not match %S type" (car widget)))
557 (defun widget-get-sibling (widget)
558 "Get the item WIDGET is assumed to toggle.
559 This is only meaningful for radio buttons or checkboxes in a list."
560 (let* ((children (widget-get (widget-get widget :parent) :children))
564 (setq child (car children)
565 children (cdr children))
566 (when (eq (widget-get child :button) widget)
567 (throw 'child child)))
570 (defun widget-map-buttons (function &optional buffer maparg)
571 "Map FUNCTION over the buttons in BUFFER.
572 FUNCTION is called with the arguments WIDGET and MAPARG.
574 If FUNCTION returns non-nil, the walk is cancelled.
576 The arguments MAPARG, and BUFFER default to nil and (current-buffer),
578 (let ((cur (point-min))
581 (with-current-buffer buffer (overlay-lists))
583 (setq overlays (append (car overlays) (cdr overlays)))
584 (while (setq cur (pop overlays))
585 (setq widget (overlay-get cur 'button))
586 (if (and widget (funcall function widget maparg))
587 (setq overlays nil)))))
591 (defcustom widget-image-directory (file-name-as-directory
592 (expand-file-name "custom" data-directory))
593 "Where widget button images are located.
594 If this variable is nil, widget will try to locate the directory
599 (defcustom widget-image-enable t
600 "If non nil, use image buttons in widgets when available."
605 (defcustom widget-image-conversion
606 '((xpm ".xpm") (gif ".gif") (png ".png") (jpeg ".jpg" ".jpeg")
608 "Conversion alist from image formats to file name suffixes."
610 :type '(repeat (cons :format "%v"
611 (symbol :tag "Image Format" unknown)
612 (repeat :tag "Suffixes"
613 (string :format "%v")))))
615 (defun widget-image-find (image)
616 "Create a graphical button from IMAGE.
617 IMAGE should either already be an image, or be a file name sans
618 extension (xpm, xbm, gif, jpg, or png) located in
619 `widget-image-directory' or otherwise where `find-image' will find it."
620 (cond ((not (and image widget-image-enable (display-graphic-p)))
621 ;; We don't want or can't use images.
624 (eq 'image (car image)))
625 ;; Already an image spec. Use it.
628 ;; A string. Look it up in relevant directories.
629 (let* ((load-path (cons widget-image-directory load-path))
631 (dolist (elt widget-image-conversion)
632 (dolist (ext (cdr elt))
633 (push (list :type (car elt) :file (concat image ext)) specs)))
634 (setq specs (nreverse specs))
640 (defvar widget-button-pressed-face 'widget-button-pressed
641 "Face used for pressed buttons in widgets.
642 This exists as a variable so it can be set locally in certain
645 (defun widget-image-insert (widget tag image &optional down inactive)
646 "In WIDGET, insert the text TAG or, if supported, IMAGE.
647 IMAGE should either be an image or an image file name sans extension
648 \(xpm, xbm, gif, jpg, or png) located in `widget-image-directory'.
650 Optional arguments DOWN and INACTIVE are used instead of IMAGE when the
651 button is pressed or inactive, respectively. These are currently ignored."
652 (if (and (display-graphic-p)
653 (setq image (widget-image-find image)))
654 (progn (widget-put widget :suppress-face t)
657 tag 'mouse-face widget-button-pressed-face)))
662 (defgroup widget-button nil
663 "The look of various kinds of buttons."
666 (defcustom widget-button-prefix ""
667 "String used as prefix for buttons."
669 :group 'widget-button)
671 (defcustom widget-button-suffix ""
672 "String used as suffix for buttons."
674 :group 'widget-button)
676 ;;; Creating Widgets.
679 (defun widget-create (type &rest args)
680 "Create widget of TYPE.
681 The optional ARGS are additional keyword arguments."
682 (let ((widget (apply 'widget-convert type args)))
683 (widget-apply widget :create)
686 (defun widget-create-child-and-convert (parent type &rest args)
687 "As part of the widget PARENT, create a child widget TYPE.
688 The child is converted, using the keyword arguments ARGS."
689 (let ((widget (apply 'widget-convert type args)))
690 (widget-put widget :parent parent)
691 (unless (widget-get widget :indent)
692 (widget-put widget :indent (+ (or (widget-get parent :indent) 0)
693 (or (widget-get widget :extra-offset) 0)
694 (widget-get parent :offset))))
695 (widget-apply widget :create)
698 (defun widget-create-child (parent type)
699 "Create widget of TYPE."
700 (let ((widget (widget-copy type)))
701 (widget-put widget :parent parent)
702 (unless (widget-get widget :indent)
703 (widget-put widget :indent (+ (or (widget-get parent :indent) 0)
704 (or (widget-get widget :extra-offset) 0)
705 (widget-get parent :offset))))
706 (widget-apply widget :create)
709 (defun widget-create-child-value (parent type value)
710 "Create widget of TYPE with value VALUE."
711 (let ((widget (widget-copy type)))
712 (widget-put widget :value (widget-apply widget :value-to-internal value))
713 (widget-put widget :parent parent)
714 (unless (widget-get widget :indent)
715 (widget-put widget :indent (+ (or (widget-get parent :indent) 0)
716 (or (widget-get widget :extra-offset) 0)
717 (widget-get parent :offset))))
718 (widget-apply widget :create)
722 (defun widget-delete (widget)
724 (widget-apply widget :delete))
726 (defun widget-copy (widget)
727 "Make a deep copy of WIDGET."
728 (widget-apply (copy-sequence widget) :copy))
730 (defun widget-convert (type &rest args)
731 "Convert TYPE to a widget without inserting it in the buffer.
732 The optional ARGS are additional keyword arguments."
733 ;; Don't touch the type.
734 (let* ((widget (if (symbolp type)
736 (copy-sequence type)))
740 ;; First set the :args keyword.
741 (while (cdr current) ;Look in the type.
742 (if (and (keywordp (cadr current))
743 ;; If the last element is a keyword,
744 ;; it is still the :args element,
745 ;; even though it is a keyword.
747 (if (eq (cadr current) :args)
748 ;; If :args is explicitly specified, obey it.
750 ;; Some other irrelevant keyword.
751 (setq current (cdr (cdr current))))
752 (setcdr current (list :args (cdr current)))
754 (while (and args (not done)) ;Look in ARGS.
755 (cond ((eq (car args) :args)
756 ;; Handle explicit specification of :args.
757 (setq args (cadr args)
759 ((keywordp (car args))
760 (setq args (cddr args)))
763 (widget-put widget :args args))
764 ;; Then Convert the widget.
767 (let ((convert-widget (plist-get (cdr type) :convert-widget)))
769 (setq widget (funcall convert-widget widget))))
770 (setq type (get (car type) 'widget-type)))
771 ;; Finally set the keyword args.
773 (let ((next (nth 0 keys)))
776 (widget-put widget next (nth 1 keys))
777 (setq keys (nthcdr 2 keys)))
779 ;; Convert the :value to internal format.
780 (if (widget-member widget :value)
782 :value (widget-apply widget
784 (widget-get widget :value))))
785 ;; Return the newly create widget.
789 (defun widget-insert (&rest args)
790 "Call `insert' with ARGS even if surrounding text is read only."
791 (let ((inhibit-read-only t)
792 (inhibit-modification-hooks t))
793 (apply 'insert args)))
795 (defun widget-convert-text (type from to
796 &optional button-from button-to
798 "Return a widget of type TYPE with endpoint FROM TO.
799 No text will be inserted to the buffer, instead the text between FROM
800 and TO will be used as the widgets end points. If optional arguments
801 BUTTON-FROM and BUTTON-TO are given, these will be used as the widgets
803 Optional ARGS are extra keyword arguments for TYPE."
804 (let ((widget (apply 'widget-convert type :delete 'widget-leave-text args))
805 (from (copy-marker from))
806 (to (copy-marker to)))
807 (set-marker-insertion-type from t)
808 (set-marker-insertion-type to nil)
809 (widget-put widget :from from)
810 (widget-put widget :to to)
812 (widget-specify-button widget button-from button-to))
815 (defun widget-convert-button (type from to &rest args)
816 "Return a widget of type TYPE with endpoint FROM TO.
817 Optional ARGS are extra keyword arguments for TYPE.
818 No text will be inserted to the buffer, instead the text between FROM
819 and TO will be used as the widgets end points, as well as the widgets
821 (apply 'widget-convert-text type from to from to args))
823 (defun widget-leave-text (widget)
824 "Remove markers and overlays from WIDGET and its children."
825 (let ((button (widget-get widget :button-overlay))
826 (sample (widget-get widget :sample-overlay))
827 (doc (widget-get widget :doc-overlay))
828 (field (widget-get widget :field-overlay)))
829 (set-marker (widget-get widget :from) nil)
830 (set-marker (widget-get widget :to) nil)
832 (delete-overlay button))
834 (delete-overlay sample))
836 (delete-overlay doc))
838 (delete-overlay field))
839 (mapc 'widget-leave-text (widget-get widget :children))))
841 ;;; Keymap and Commands.
844 (defvar widget-keymap
845 (let ((map (make-sparse-keymap)))
846 (define-key map "\t" 'widget-forward)
847 (define-key map [(shift tab)] 'widget-backward)
848 (define-key map [backtab] 'widget-backward)
849 (define-key map [down-mouse-2] 'widget-button-click)
850 (define-key map "\C-m" 'widget-button-press)
852 "Keymap containing useful binding for buffers containing widgets.
853 Recommended as a parent keymap for modes using widgets.")
855 (defvar widget-global-map global-map
856 "Keymap used for events a widget does not handle itself.")
857 (make-variable-buffer-local 'widget-global-map)
859 (defvar widget-field-keymap
860 (let ((map (copy-keymap widget-keymap)))
861 (define-key map "\C-k" 'widget-kill-line)
862 (define-key map "\M-\t" 'widget-complete)
863 (define-key map "\C-m" 'widget-field-activate)
864 ;; Since the widget code uses a `field' property to identify fields,
865 ;; ordinary beginning-of-line does the right thing.
866 ;; (define-key map "\C-a" 'widget-beginning-of-line)
867 (define-key map "\C-e" 'widget-end-of-line)
869 "Keymap used inside an editable field.")
871 (defvar widget-text-keymap
872 (let ((map (copy-keymap widget-keymap)))
873 ;; Since the widget code uses a `field' property to identify fields,
874 ;; ordinary beginning-of-line does the right thing.
875 ;; (define-key map "\C-a" 'widget-beginning-of-line)
876 (define-key map "\C-e" 'widget-end-of-line)
878 "Keymap used inside a text field.")
880 (defun widget-field-activate (pos &optional event)
881 "Invoke the editable field at point."
883 (let ((field (widget-field-at pos)))
885 (widget-apply-action field event)
887 (lookup-key widget-global-map (this-command-keys))))))
889 (defface widget-button-pressed
890 '((((min-colors 88) (class color))
891 (:foreground "red1"))
895 (:weight bold :underline t)))
896 "Face used for pressed buttons."
897 :group 'widget-faces)
898 ;; backward-compatibility alias
899 (put 'widget-button-pressed-face 'face-alias 'widget-button-pressed)
901 (defun widget-button-click (event)
902 "Invoke the button that the mouse is pointing at."
904 (if (widget-event-point event)
905 (let* ((pos (widget-event-point event))
906 (start (event-start event))
907 (button (get-char-property
908 pos 'button (and (windowp (posn-window start))
909 (window-buffer (posn-window start))))))
911 ;; Mouse click on a widget button. Do the following
912 ;; in a save-excursion so that the click on the button
913 ;; doesn't change point.
914 (save-selected-window
915 (select-window (posn-window (event-start event)))
917 (goto-char (posn-point (event-start event)))
918 (let* ((overlay (widget-get button :button-overlay))
919 (face (overlay-get overlay 'face))
920 (mouse-face (overlay-get overlay 'mouse-face)))
922 ;; Read events, including mouse-movement events
923 ;; until we receive a release event. Highlight/
924 ;; unhighlight the button the mouse was initially
925 ;; on when we move over it.
927 (when face ; avoid changing around image
929 'face widget-button-pressed-face)
931 'mouse-face widget-button-pressed-face))
932 (unless (widget-apply button :mouse-down-action event)
933 (let ((track-mouse t))
934 (while (not (widget-button-release-event-p event))
935 (setq event (read-event)
936 pos (widget-event-point event))
938 (eq (get-char-property pos 'button)
943 widget-button-pressed-face)
946 widget-button-pressed-face))
947 (overlay-put overlay 'face face)
948 (overlay-put overlay 'mouse-face mouse-face)))))
950 ;; When mouse is released over the button, run
951 ;; its action function.
953 (eq (get-char-property pos 'button) button))
954 (widget-apply-action button event)))
955 (overlay-put overlay 'face face)
956 (overlay-put overlay 'mouse-face mouse-face))))
958 (unless (pos-visible-in-window-p (widget-event-point event))
959 (mouse-set-point event)
964 (let ((up t) command)
965 ;; Mouse click not on a widget button. Find the global
966 ;; command to run, and check whether it is bound to an
968 (mouse-set-point event)
969 (if (memq (event-basic-type event) '(mouse-1 down-mouse-1))
970 (cond ((setq command ;down event
971 (lookup-key widget-global-map [down-mouse-1]))
973 ((setq command ;up event
974 (lookup-key widget-global-map [mouse-1]))))
975 (cond ((setq command ;down event
976 (lookup-key widget-global-map [down-mouse-2]))
978 ((setq command ;up event
979 (lookup-key widget-global-map [mouse-2])))))
981 ;; Don't execute up events twice.
982 (while (not (widget-button-release-event-p event))
983 (setq event (read-event))))
985 (call-interactively command)))))
986 (message "You clicked somewhere weird.")))
988 (defun widget-button-press (pos &optional event)
989 "Invoke button at POS."
991 (let ((button (get-char-property pos 'button)))
993 (widget-apply-action button event)
994 (let ((command (lookup-key widget-global-map (this-command-keys))))
995 (when (commandp command)
996 (call-interactively command))))))
998 (defun widget-tabable-at (&optional pos)
999 "Return the tabable widget at POS, or nil.
1000 POS defaults to the value of (point)."
1001 (let ((widget (widget-at pos)))
1003 (let ((order (widget-get widget :tab-order)))
1009 (defvar widget-use-overlay-change t
1010 "If non-nil, use overlay change functions to tab around in the buffer.
1011 This is much faster, but doesn't work reliably on Emacs 19.34.")
1013 (defun widget-move (arg)
1014 "Move point to the ARG next field or button.
1015 ARG may be negative to move backward."
1016 (or (bobp) (> arg 0) (backward-char))
1019 (old (widget-tabable-at)))
1023 (goto-char (point-min))
1024 (setq wrapped (1+ wrapped)))
1025 (widget-use-overlay-change
1026 (goto-char (next-overlay-change (point))))
1031 (error "No buttons or fields found"))
1032 (let ((new (widget-tabable-at)))
1034 (unless (eq new old)
1040 (goto-char (point-max))
1041 (setq wrapped (1+ wrapped)))
1042 (widget-use-overlay-change
1043 (goto-char (previous-overlay-change (point))))
1048 (error "No buttons or fields found"))
1049 (let ((new (widget-tabable-at)))
1051 (unless (eq new old)
1052 (setq arg (1+ arg))))))
1053 (let ((new (widget-tabable-at)))
1054 (while (eq (widget-tabable-at) new)
1057 (widget-echo-help (point))
1058 (run-hooks 'widget-move-hook))
1060 (defun widget-forward (arg)
1061 "Move point to the next field or button.
1062 With optional ARG, move across that many fields."
1064 (run-hooks 'widget-forward-hook)
1067 (defun widget-backward (arg)
1068 "Move point to the previous field or button.
1069 With optional ARG, move across that many fields."
1071 (run-hooks 'widget-backward-hook)
1072 (widget-move (- arg)))
1074 ;; Since the widget code uses a `field' property to identify fields,
1075 ;; ordinary beginning-of-line does the right thing.
1076 (defalias 'widget-beginning-of-line 'beginning-of-line)
1078 (defun widget-end-of-line ()
1079 "Go to end of field or end of line, whichever is first.
1080 Trailing spaces at the end of padded fields are not considered part of
1083 ;; Ordinary end-of-line does the right thing, because we're inside
1084 ;; text with a `field' property.
1087 ;; ... except that we want to ignore trailing spaces in fields that
1088 ;; aren't terminated by a newline, because they are used as padding,
1089 ;; and ignored when extracting the entered value of the field.
1090 (skip-chars-backward " " (field-beginning (1- (point))))))
1092 (defun widget-kill-line ()
1093 "Kill to end of field or end of line, whichever is first."
1095 (let* ((field (widget-field-find (point)))
1096 (end (and field (widget-field-end field))))
1097 (if (and field (> (line-beginning-position 2) end))
1098 (kill-region (point) end)
1099 (call-interactively 'kill-line))))
1101 (defcustom widget-complete-field (lookup-key global-map "\M-\t")
1102 "Default function to call for completion inside fields."
1103 :options '(ispell-complete-word complete-tag lisp-complete-symbol)
1107 (defun widget-narrow-to-field ()
1110 (let ((field (widget-field-find (point))))
1112 (narrow-to-region (line-beginning-position) (line-end-position)))))
1114 (defun widget-complete ()
1115 "Complete content of editable field from point.
1116 When not inside a field, move to the previous button or field."
1118 (let ((field (widget-field-find (point))))
1121 (widget-narrow-to-field)
1122 (widget-apply field :complete))
1123 (error "Not in an editable field"))))
1125 ;;; Setting up the buffer.
1127 (defvar widget-field-new nil
1128 "List of all newly created editable fields in the buffer.")
1129 (make-variable-buffer-local 'widget-field-new)
1131 (defvar widget-field-list nil
1132 "List of all editable fields in the buffer.")
1133 (make-variable-buffer-local 'widget-field-list)
1135 (defun widget-at (&optional pos)
1136 "The button or field at POS (default, point)."
1137 (or (get-char-property (or pos (point)) 'button)
1138 (widget-field-at pos)))
1141 (defun widget-setup ()
1142 "Setup current buffer so editing string widgets works."
1143 (let ((inhibit-read-only t)
1144 (inhibit-modification-hooks t)
1146 (while widget-field-new
1147 (setq field (car widget-field-new)
1148 widget-field-new (cdr widget-field-new)
1149 widget-field-list (cons field widget-field-list))
1150 (let ((from (car (widget-get field :field-overlay)))
1151 (to (cdr (widget-get field :field-overlay))))
1152 (widget-specify-field field
1153 (marker-position from) (marker-position to))
1154 (set-marker from nil)
1155 (set-marker to nil))))
1157 (widget-add-change))
1159 (defvar widget-field-last nil)
1160 ;; Last field containing point.
1161 (make-variable-buffer-local 'widget-field-last)
1163 (defvar widget-field-was nil)
1164 ;; The widget data before the change.
1165 (make-variable-buffer-local 'widget-field-was)
1167 (defun widget-field-at (pos)
1168 "Return the widget field at POS, or nil if none."
1169 (let ((field (get-char-property (or pos (point)) 'field)))
1170 (if (eq field 'boundary)
1171 (get-char-property (or pos (point)) 'real-field)
1174 (defun widget-field-buffer (widget)
1175 "Return the buffer of WIDGET's editing field."
1176 (let ((overlay (widget-get widget :field-overlay)))
1177 (cond ((overlayp overlay)
1178 (overlay-buffer overlay))
1180 (marker-buffer (car overlay))))))
1182 (defun widget-field-start (widget)
1183 "Return the start of WIDGET's editing field."
1184 (let ((overlay (widget-get widget :field-overlay)))
1185 (if (overlayp overlay)
1186 (overlay-start overlay)
1189 (defun widget-field-end (widget)
1190 "Return the end of WIDGET's editing field."
1191 (let ((overlay (widget-get widget :field-overlay)))
1192 ;; Don't subtract one if local-map works at the end of the overlay,
1193 ;; or if a special `boundary' field has been added after the widget
1195 (if (overlayp overlay)
1196 (if (and (not (eq (with-current-buffer
1197 (widget-field-buffer widget)
1199 ;; `widget-narrow-to-field' can be
1200 ;; active when this function is called
1201 ;; from an change-functions hook. So
1202 ;; temporarily remove field narrowing
1203 ;; before to call `get-char-property'.
1205 (get-char-property (overlay-end overlay)
1208 (or widget-field-add-space
1209 (null (widget-get widget :size))))
1210 (1- (overlay-end overlay))
1211 (overlay-end overlay))
1214 (defun widget-field-find (pos)
1215 "Return the field at POS.
1216 Unlike (get-char-property POS 'field), this works with empty fields too."
1217 (let ((fields widget-field-list)
1220 (setq field (car fields)
1221 fields (cdr fields))
1222 (when (and (<= (widget-field-start field) pos)
1223 (<= pos (widget-field-end field)))
1225 (error "Overlapping fields"))
1226 (setq found field)))
1229 (defun widget-before-change (from to)
1230 ;; This is how, for example, a variable changes its state to `modified'.
1231 ;; when it is being edited.
1232 (unless inhibit-read-only
1233 (let ((from-field (widget-field-find from))
1234 (to-field (widget-field-find to)))
1235 (cond ((not (eq from-field to-field))
1236 (add-hook 'post-command-hook 'widget-add-change nil t)
1237 (signal 'text-read-only
1238 '("Change should be restricted to a single field")))
1240 (add-hook 'post-command-hook 'widget-add-change nil t)
1241 (signal 'text-read-only
1242 '("Attempt to change text outside editable field")))
1243 (widget-field-use-before-change
1244 (widget-apply from-field :notify from-field))))))
1246 (defun widget-add-change ()
1247 (remove-hook 'post-command-hook 'widget-add-change t)
1248 (add-hook 'before-change-functions 'widget-before-change nil t)
1249 (add-hook 'after-change-functions 'widget-after-change nil t))
1251 (defun widget-after-change (from to old)
1252 "Adjust field size and text properties."
1253 (let ((field (widget-field-find from))
1254 (other (widget-field-find to)))
1256 (unless (eq field other)
1257 (error "Change in different fields"))
1258 (let ((size (widget-get field :size)))
1260 (let ((begin (widget-field-start field))
1261 (end (widget-field-end field)))
1262 (cond ((< (- end begin) size)
1266 (insert-char ?\s (- (+ begin size) end))))
1267 ((> (- end begin) size)
1268 ;; Field too large and
1269 (if (or (< (point) (+ begin size))
1271 ;; Point is outside extra space.
1272 (setq begin (+ begin size))
1273 ;; Point is within the extra space.
1274 (setq begin (point)))
1277 (while (and (eq (preceding-char) ?\s)
1279 (delete-backward-char 1)))))))
1280 (widget-specify-secret field))
1281 (widget-apply field :notify field))))
1283 ;;; Widget Functions
1285 ;; These functions are used in the definition of multiple widgets.
1287 (defun widget-parent-action (widget &optional event)
1288 "Tell :parent of WIDGET to handle the :action.
1289 Optional EVENT is the event that triggered the action."
1290 (widget-apply (widget-get widget :parent) :action event))
1292 (defun widget-children-value-delete (widget)
1293 "Delete all :children and :buttons in WIDGET."
1294 (mapc 'widget-delete (widget-get widget :children))
1295 (widget-put widget :children nil)
1296 (mapc 'widget-delete (widget-get widget :buttons))
1297 (widget-put widget :buttons nil))
1299 (defun widget-children-validate (widget)
1300 "All the :children must be valid."
1301 (let ((children (widget-get widget :children))
1303 (while (and children (not found))
1304 (setq child (car children)
1305 children (cdr children)
1306 found (widget-apply child :validate)))
1309 (defun widget-child-value-get (widget)
1310 "Get the value of the first member of :children in WIDGET."
1311 (widget-value (car (widget-get widget :children))))
1313 (defun widget-child-value-inline (widget)
1314 "Get the inline value of the first member of :children in WIDGET."
1315 (widget-apply (car (widget-get widget :children)) :value-inline))
1317 (defun widget-child-validate (widget)
1318 "The result of validating the first member of :children in WIDGET."
1319 (widget-apply (car (widget-get widget :children)) :validate))
1321 (defun widget-type-value-create (widget)
1322 "Convert and instantiate the value of the :type attribute of WIDGET.
1323 Store the newly created widget in the :children attribute.
1325 The value of the :type attribute should be an unconverted widget type."
1326 (let ((value (widget-get widget :value))
1327 (type (widget-get widget :type)))
1328 (widget-put widget :children
1329 (list (widget-create-child-value widget
1330 (widget-convert type)
1333 (defun widget-type-default-get (widget)
1334 "Get default value from the :type attribute of WIDGET.
1336 The value of the :type attribute should be an unconverted widget type."
1337 (widget-default-get (widget-convert (widget-get widget :type))))
1339 (defun widget-type-match (widget value)
1340 "Non-nil if the :type value of WIDGET matches VALUE.
1342 The value of the :type attribute should be an unconverted widget type."
1343 (widget-apply (widget-convert (widget-get widget :type)) :match value))
1345 (defun widget-types-copy (widget)
1346 "Copy :args as widget types in WIDGET."
1347 (widget-put widget :args (mapcar 'widget-copy (widget-get widget :args)))
1350 ;; Made defsubst to speed up face editor creation.
1351 (defsubst widget-types-convert-widget (widget)
1352 "Convert :args as widget types in WIDGET."
1353 (widget-put widget :args (mapcar 'widget-convert (widget-get widget :args)))
1356 (defun widget-value-convert-widget (widget)
1357 "Initialize :value from :args in WIDGET."
1358 (let ((args (widget-get widget :args)))
1360 (widget-put widget :value (car args))
1361 ;; Don't convert :value here, as this is done in `widget-convert'.
1362 ;; (widget-put widget :value (widget-apply widget
1363 ;; :value-to-internal (car args)))
1364 (widget-put widget :args nil)))
1367 (defun widget-value-value-get (widget)
1368 "Return the :value property of WIDGET."
1369 (widget-get widget :value))
1371 ;;; The `default' Widget.
1373 (define-widget 'default nil
1374 "Basic widget other widgets are derived from."
1375 :value-to-internal (lambda (widget value) value)
1376 :value-to-external (lambda (widget value) value)
1377 :button-prefix 'widget-button-prefix
1378 :button-suffix 'widget-button-suffix
1379 :complete 'widget-default-complete
1380 :create 'widget-default-create
1383 :format-handler 'widget-default-format-handler
1384 :button-face-get 'widget-default-button-face-get
1385 :sample-face-get 'widget-default-sample-face-get
1386 :delete 'widget-default-delete
1388 :value-set 'widget-default-value-set
1389 :value-inline 'widget-default-value-inline
1390 :value-delete 'ignore
1391 :default-get 'widget-default-default-get
1392 :menu-tag-get 'widget-default-menu-tag-get
1394 :active 'widget-default-active
1395 :activate 'widget-specify-active
1396 :deactivate 'widget-default-deactivate
1397 :mouse-down-action #'ignore
1398 :action 'widget-default-action
1399 :notify 'widget-default-notify
1400 :prompt-value 'widget-default-prompt-value)
1402 (defun widget-default-complete (widget)
1403 "Call the value of the :complete-function property of WIDGET.
1404 If that does not exists, call the value of `widget-complete-field'."
1405 (call-interactively (or (widget-get widget :complete-function)
1406 widget-complete-field)))
1408 (defun widget-default-create (widget)
1409 "Create WIDGET at point in the current buffer."
1410 (widget-specify-insert
1411 (let ((from (point))
1412 button-begin button-end
1413 sample-begin sample-end
1416 (insert (widget-get widget :format))
1418 ;; Parse escapes in format.
1419 (while (re-search-forward "%\\(.\\)" nil t)
1420 (let ((escape (char-after (match-beginning 1))))
1421 (delete-backward-char 2)
1422 (cond ((eq escape ?%)
1425 (setq button-begin (point))
1426 (insert (widget-get-indirect widget :button-prefix)))
1428 (insert (widget-get-indirect widget :button-suffix))
1429 (setq button-end (point)))
1431 (setq sample-begin (point)))
1433 (setq sample-end (point)))
1435 (when (widget-get widget :indent)
1437 (insert-char ?\s (widget-get widget :indent))))
1439 (let ((image (widget-get widget :tag-glyph))
1440 (tag (widget-get widget :tag)))
1442 (widget-image-insert widget (or tag "image") image))
1446 (princ (widget-get widget :value)
1447 (current-buffer))))))
1449 (let ((doc (widget-get widget :doc)))
1451 (setq doc-begin (point))
1453 (while (eq (preceding-char) ?\n)
1454 (delete-backward-char 1))
1456 (setq doc-end (point)))))
1458 (if (and button-begin (not button-end))
1459 (widget-apply widget :value-create)
1460 (setq value-pos (point))))
1462 (widget-apply widget :format-handler escape)))))
1463 ;; Specify button, sample, and doc, and insert value.
1464 (and button-begin button-end
1465 (widget-specify-button widget button-begin button-end))
1466 (and sample-begin sample-end
1467 (widget-specify-sample widget sample-begin sample-end))
1468 (and doc-begin doc-end
1469 (widget-specify-doc widget doc-begin doc-end))
1471 (goto-char value-pos)
1472 (widget-apply widget :value-create)))
1473 (let ((from (point-min-marker))
1474 (to (point-max-marker)))
1475 (set-marker-insertion-type from t)
1476 (set-marker-insertion-type to nil)
1477 (widget-put widget :from from)
1478 (widget-put widget :to to)))
1479 (widget-clear-undo))
1481 (defun widget-default-format-handler (widget escape)
1482 ;; We recognize the %h escape by default.
1483 (let* ((buttons (widget-get widget :buttons)))
1484 (cond ((eq escape ?h)
1485 (let* ((doc-property (widget-get widget :documentation-property))
1486 (doc-try (cond ((widget-get widget :doc))
1487 ((functionp doc-property)
1488 (funcall doc-property
1489 (widget-get widget :value)))
1490 ((symbolp doc-property)
1491 (documentation-property
1492 (widget-get widget :value)
1494 (doc-text (and (stringp doc-try)
1495 (> (length doc-try) 1)
1497 (doc-indent (widget-get widget :documentation-indent)))
1499 (and (eq (preceding-char) ?\n)
1500 (widget-get widget :indent)
1501 (insert-char ?\s (widget-get widget :indent)))
1502 ;; The `*' in the beginning is redundant.
1503 (when (eq (aref doc-text 0) ?*)
1504 (setq doc-text (substring doc-text 1)))
1505 ;; Get rid of trailing newlines.
1506 (when (string-match "\n+\\'" doc-text)
1507 (setq doc-text (substring doc-text 0 (match-beginning 0))))
1508 (push (widget-create-child-and-convert
1509 widget 'documentation-string
1510 :indent (cond ((numberp doc-indent )
1518 (error "Unknown escape `%c'" escape)))
1519 (widget-put widget :buttons buttons)))
1521 (defun widget-default-button-face-get (widget)
1522 ;; Use :button-face or widget-button-face
1523 (or (widget-get widget :button-face)
1524 (let ((parent (widget-get widget :parent)))
1526 (widget-apply parent :button-face-get)
1527 widget-button-face))))
1529 (defun widget-default-sample-face-get (widget)
1530 ;; Use :sample-face.
1531 (widget-get widget :sample-face))
1533 (defun widget-default-delete (widget)
1534 "Remove widget from the buffer."
1535 (let ((from (widget-get widget :from))
1536 (to (widget-get widget :to))
1537 (inactive-overlay (widget-get widget :inactive))
1538 (button-overlay (widget-get widget :button-overlay))
1539 (sample-overlay (widget-get widget :sample-overlay))
1540 (doc-overlay (widget-get widget :doc-overlay))
1541 (inhibit-modification-hooks t)
1542 (inhibit-read-only t))
1543 (widget-apply widget :value-delete)
1544 (widget-children-value-delete widget)
1545 (when inactive-overlay
1546 (delete-overlay inactive-overlay))
1547 (when button-overlay
1548 (delete-overlay button-overlay))
1549 (when sample-overlay
1550 (delete-overlay sample-overlay))
1552 (delete-overlay doc-overlay))
1554 ;; Kludge: this doesn't need to be true for empty formats.
1555 (delete-region from to))
1556 (set-marker from nil)
1557 (set-marker to nil))
1558 (widget-clear-undo))
1560 (defun widget-default-value-set (widget value)
1561 "Recreate widget with new value."
1562 (let* ((old-pos (point))
1563 (from (copy-marker (widget-get widget :from)))
1564 (to (copy-marker (widget-get widget :to)))
1565 (offset (if (and (<= from old-pos) (<= old-pos to))
1566 (if (>= old-pos (1- to))
1568 (- old-pos from)))))
1569 ;;??? Bug: this ought to insert the new value before deleting the old one,
1570 ;; so that markers on either side of the value automatically
1571 ;; stay on the same side. -- rms.
1573 (goto-char (widget-get widget :from))
1574 (widget-apply widget :delete)
1575 (widget-put widget :value value)
1576 (widget-apply widget :create))
1579 (goto-char (+ (widget-get widget :to) offset 1))
1580 (goto-char (min (+ from offset) (1- (widget-get widget :to))))))))
1582 (defun widget-default-value-inline (widget)
1583 "Wrap value in a list unless it is inline."
1584 (if (widget-get widget :inline)
1585 (widget-value widget)
1586 (list (widget-value widget))))
1588 (defun widget-default-default-get (widget)
1590 (widget-get widget :value))
1592 (defun widget-default-menu-tag-get (widget)
1593 "Use tag or value for menus."
1594 (or (widget-get widget :menu-tag)
1595 (widget-get widget :tag)
1596 (widget-princ-to-string (widget-get widget :value))))
1598 (defun widget-default-active (widget)
1599 "Return t iff this widget active (user modifiable)."
1600 (or (widget-get widget :always-active)
1601 (and (not (widget-get widget :inactive))
1602 (let ((parent (widget-get widget :parent)))
1604 (widget-apply parent :active))))))
1606 (defun widget-default-deactivate (widget)
1607 "Make WIDGET inactive for user modifications."
1608 (widget-specify-inactive widget
1609 (widget-get widget :from)
1610 (widget-get widget :to)))
1612 (defun widget-default-action (widget &optional event)
1613 "Notify the parent when a widget changes."
1614 (let ((parent (widget-get widget :parent)))
1616 (widget-apply parent :notify widget event))))
1618 (defun widget-default-notify (widget child &optional event)
1619 "Pass notification to parent."
1620 (widget-default-action widget event))
1622 (defun widget-default-prompt-value (widget prompt value unbound)
1623 "Read an arbitrary value. Stolen from `set-variable'."
1624 ;; (let ((initial (if unbound
1626 ;; It would be nice if we could do a `(cons val 1)' here.
1627 ;; (prin1-to-string (custom-quote value))))))
1628 (eval-minibuffer prompt))
1630 ;;; The `item' Widget.
1632 (define-widget 'item 'default
1633 "Constant items for inclusion in other widgets."
1634 :convert-widget 'widget-value-convert-widget
1635 :value-create 'widget-item-value-create
1636 :value-delete 'ignore
1637 :value-get 'widget-value-value-get
1638 :match 'widget-item-match
1639 :match-inline 'widget-item-match-inline
1640 :action 'widget-item-action
1643 (defun widget-item-value-create (widget)
1644 "Insert the printed representation of the value."
1645 (princ (widget-get widget :value) (current-buffer)))
1647 (defun widget-item-match (widget value)
1648 ;; Match if the value is the same.
1649 (equal (widget-get widget :value) value))
1651 (defun widget-item-match-inline (widget values)
1652 ;; Match if the value is the same.
1653 (let ((value (widget-get widget :value)))
1655 (<= (length value) (length values))
1656 (let ((head (widget-sublist values 0 (length value))))
1657 (and (equal head value)
1658 (cons head (widget-sublist values (length value))))))))
1660 (defun widget-sublist (list start &optional end)
1661 "Return the sublist of LIST from START to END.
1662 If END is omitted, it defaults to the length of LIST."
1663 (if (> start 0) (setq list (nthcdr start list)))
1665 (unless (<= end start)
1666 (setq list (copy-sequence list))
1667 (setcdr (nthcdr (- end start 1) list) nil)
1669 (copy-sequence list)))
1671 (defun widget-item-action (widget &optional event)
1672 ;; Just notify itself.
1673 (widget-apply widget :notify widget event))
1675 ;;; The `push-button' Widget.
1677 ;; (defcustom widget-push-button-gui t
1678 ;; "If non nil, use GUI push buttons when available."
1682 ;; Cache already created GUI objects.
1683 ;; (defvar widget-push-button-cache nil)
1685 (defcustom widget-push-button-prefix "["
1686 "String used as prefix for buttons."
1688 :group 'widget-button)
1690 (defcustom widget-push-button-suffix "]"
1691 "String used as suffix for buttons."
1693 :group 'widget-button)
1695 (define-widget 'push-button 'item
1696 "A pushable button."
1699 :value-create 'widget-push-button-value-create
1702 (defun widget-push-button-value-create (widget)
1703 "Insert text representing the `on' and `off' states."
1704 (let* ((tag (or (widget-get widget :tag)
1705 (widget-get widget :value)))
1706 (tag-glyph (widget-get widget :tag-glyph))
1707 (text (concat widget-push-button-prefix
1708 tag widget-push-button-suffix)))
1710 (widget-image-insert widget text tag-glyph)
1713 ;; (defun widget-gui-action (widget)
1714 ;; "Apply :action for WIDGET."
1715 ;; (widget-apply-action widget (this-command-keys)))
1717 ;;; The `link' Widget.
1719 (defcustom widget-link-prefix "["
1720 "String used as prefix for links."
1722 :group 'widget-button)
1724 (defcustom widget-link-suffix "]"
1725 "String used as suffix for links."
1727 :group 'widget-button)
1729 (define-widget 'link 'item
1731 :button-prefix 'widget-link-prefix
1732 :button-suffix 'widget-link-suffix
1734 :help-echo "Follow the link."
1737 ;;; The `info-link' Widget.
1739 (define-widget 'info-link 'link
1740 "A link to an info file."
1741 :action 'widget-info-link-action)
1743 (defun widget-info-link-action (widget &optional event)
1744 "Open the info node specified by WIDGET."
1745 (info (widget-value widget)))
1747 ;;; The `url-link' Widget.
1749 (define-widget 'url-link 'link
1750 "A link to an www page."
1751 :action 'widget-url-link-action)
1753 (defun widget-url-link-action (widget &optional event)
1754 "Open the URL specified by WIDGET."
1755 (browse-url (widget-value widget)))
1757 ;;; The `function-link' Widget.
1759 (define-widget 'function-link 'link
1760 "A link to an Emacs function."
1761 :action 'widget-function-link-action)
1763 (defun widget-function-link-action (widget &optional event)
1764 "Show the function specified by WIDGET."
1765 (describe-function (widget-value widget)))
1767 ;;; The `variable-link' Widget.
1769 (define-widget 'variable-link 'link
1770 "A link to an Emacs variable."
1771 :action 'widget-variable-link-action)
1773 (defun widget-variable-link-action (widget &optional event)
1774 "Show the variable specified by WIDGET."
1775 (describe-variable (widget-value widget)))
1777 ;;; The `file-link' Widget.
1779 (define-widget 'file-link 'link
1781 :action 'widget-file-link-action)
1783 (defun widget-file-link-action (widget &optional event)
1784 "Find the file specified by WIDGET."
1785 (find-file (widget-value widget)))
1787 ;;; The `emacs-library-link' Widget.
1789 (define-widget 'emacs-library-link 'link
1790 "A link to an Emacs Lisp library file."
1791 :action 'widget-emacs-library-link-action)
1793 (defun widget-emacs-library-link-action (widget &optional event)
1794 "Find the Emacs library file specified by WIDGET."
1795 (find-file (locate-library (widget-value widget))))
1797 ;;; The `emacs-commentary-link' Widget.
1799 (define-widget 'emacs-commentary-link 'link
1800 "A link to Commentary in an Emacs Lisp library file."
1801 :action 'widget-emacs-commentary-link-action)
1803 (defun widget-emacs-commentary-link-action (widget &optional event)
1804 "Find the Commentary section of the Emacs file specified by WIDGET."
1805 (finder-commentary (widget-value widget)))
1807 ;;; The `editable-field' Widget.
1809 (define-widget 'editable-field 'default
1810 "An editable text field."
1811 :convert-widget 'widget-value-convert-widget
1812 :keymap widget-field-keymap
1814 :help-echo "M-TAB: complete field; RET: enter value"
1816 :prompt-internal 'widget-field-prompt-internal
1817 :prompt-history 'widget-field-history
1818 :prompt-value 'widget-field-prompt-value
1819 :action 'widget-field-action
1820 :validate 'widget-field-validate
1822 :error "Field's value doesn't match allowed forms"
1823 :value-create 'widget-field-value-create
1824 :value-delete 'widget-field-value-delete
1825 :value-get 'widget-field-value-get
1826 :match 'widget-field-match)
1828 (defvar widget-field-history nil
1829 "History of field minibuffer edits.")
1831 (defun widget-field-prompt-internal (widget prompt initial history)
1832 "Read string for WIDGET promptinhg with PROMPT.
1833 INITIAL is the initial input and HISTORY is a symbol containing
1835 (read-string prompt initial history))
1837 (defun widget-field-prompt-value (widget prompt value unbound)
1838 "Prompt for a string."
1839 (widget-apply widget
1841 (widget-apply widget
1842 :prompt-internal prompt
1844 (cons (widget-apply widget
1845 :value-to-internal value)
1847 (widget-get widget :prompt-history))))
1849 (defvar widget-edit-functions nil)
1851 (defun widget-field-action (widget &optional event)
1852 "Move to next field."
1854 (run-hook-with-args 'widget-edit-functions widget))
1856 (defun widget-field-validate (widget)
1857 "Valid if the content matches `:valid-regexp'."
1858 (unless (string-match (widget-get widget :valid-regexp)
1859 (widget-apply widget :value-get))
1862 (defun widget-field-value-create (widget)
1863 "Create an editable text field."
1864 (let ((size (widget-get widget :size))
1865 (value (widget-get widget :value))
1867 ;; This is changed to a real overlay in `widget-setup'. We
1868 ;; need the end points to behave differently until
1869 ;; `widget-setup' is called.
1870 (overlay (cons (make-marker) (make-marker))))
1871 (widget-put widget :field-overlay overlay)
1874 (< (length value) size)
1875 (insert-char ?\s (- size (length value))))
1876 (unless (memq widget widget-field-list)
1877 (setq widget-field-new (cons widget widget-field-new)))
1878 (move-marker (cdr overlay) (point))
1879 (set-marker-insertion-type (cdr overlay) nil)
1882 (move-marker (car overlay) from)
1883 (set-marker-insertion-type (car overlay) t)))
1885 (defun widget-field-value-delete (widget)
1886 "Remove the widget from the list of active editing fields."
1887 (setq widget-field-list (delq widget widget-field-list))
1888 (setq widget-field-new (delq widget widget-field-new))
1889 ;; These are nil if the :format string doesn't contain `%v'.
1890 (let ((overlay (widget-get widget :field-overlay)))
1891 (when (overlayp overlay)
1892 (delete-overlay overlay))))
1894 (defun widget-field-value-get (widget)
1895 "Return current text in editing field."
1896 (let ((from (widget-field-start widget))
1897 (to (widget-field-end widget))
1898 (buffer (widget-field-buffer widget))
1899 (size (widget-get widget :size))
1900 (secret (widget-get widget :secret))
1901 (old (current-buffer)))
1908 (eq (char-after (1- to)) ?\s))
1910 (let ((result (buffer-substring-no-properties from to)))
1913 (while (< (+ from index) to)
1915 (get-char-property (+ from index) 'secret))
1916 (setq index (1+ index)))))
1919 (widget-get widget :value))))
1921 (defun widget-field-match (widget value)
1922 ;; Match any string.
1925 ;;; The `text' Widget.
1927 (define-widget 'text 'editable-field
1928 "A multiline text area."
1929 :keymap widget-text-keymap)
1931 ;;; The `menu-choice' Widget.
1933 (define-widget 'menu-choice 'default
1934 "A menu of options."
1935 :convert-widget 'widget-types-convert-widget
1936 :copy 'widget-types-copy
1937 :format "%[%t%]: %v"
1940 :void '(item :format "invalid (%t)\n")
1941 :value-create 'widget-choice-value-create
1942 :value-get 'widget-child-value-get
1943 :value-inline 'widget-child-value-inline
1944 :default-get 'widget-choice-default-get
1945 :mouse-down-action 'widget-choice-mouse-down-action
1946 :action 'widget-choice-action
1947 :error "Make a choice"
1948 :validate 'widget-choice-validate
1949 :match 'widget-choice-match
1950 :match-inline 'widget-choice-match-inline)
1952 (defun widget-choice-value-create (widget)
1953 "Insert the first choice that matches the value."
1954 (let ((value (widget-get widget :value))
1955 (args (widget-get widget :args))
1956 (explicit (widget-get widget :explicit-choice))
1958 (if (and explicit (equal value (widget-get widget :explicit-choice-value)))
1960 ;; If the user specified the choice for this value,
1961 ;; respect that choice as long as the value is the same.
1962 (widget-put widget :children (list (widget-create-child-value
1963 widget explicit value)))
1964 (widget-put widget :choice explicit))
1966 (setq current (car args)
1968 (when (widget-apply current :match value)
1969 (widget-put widget :children (list (widget-create-child-value
1970 widget current value)))
1971 (widget-put widget :choice current)
1975 (let ((void (widget-get widget :void)))
1976 (widget-put widget :children (list (widget-create-child-and-convert
1977 widget void :value value)))
1978 (widget-put widget :choice void))))))
1980 (defun widget-choice-default-get (widget)
1981 ;; Get default for the first choice.
1982 (widget-default-get (car (widget-get widget :args))))
1984 (defcustom widget-choice-toggle nil
1985 "If non-nil, a binary choice will just toggle between the values.
1986 Otherwise, the user will explicitly have to choose between the values
1987 when he invoked the menu."
1991 (defun widget-choice-mouse-down-action (widget &optional event)
1992 ;; Return non-nil if we need a menu.
1993 (let ((args (widget-get widget :args))
1994 (old (widget-get widget :choice)))
1995 (cond ((not (display-popup-menus-p))
1996 ;; No place to pop up a menu.
1998 ((< (length args) 2)
1999 ;; Empty or singleton list, just return the value.
2001 ((> (length args) widget-menu-max-size)
2002 ;; Too long, prompt.
2004 ((> (length args) 2)
2005 ;; Reasonable sized list, use menu.
2007 ((and widget-choice-toggle (memq old args))
2011 ;; Ask which of the two.
2014 (defun widget-choice-action (widget &optional event)
2016 (let ((args (widget-get widget :args))
2017 (old (widget-get widget :choice))
2018 (tag (widget-apply widget :menu-tag-get))
2019 (completion-ignore-case (widget-get widget :case-fold))
2022 ;; Remember old value.
2023 (if (and old (not (widget-apply widget :validate)))
2024 (let* ((external (widget-value widget))
2025 (internal (widget-apply old :value-to-internal external)))
2026 (widget-put old :value internal)))
2029 (cond ((= (length args) 0)
2031 ((= (length args) 1)
2033 ((and widget-choice-toggle
2036 (if (eq old (nth 0 args))
2041 (setq current (car args)
2044 (cons (cons (widget-apply current :menu-tag-get)
2047 (setq this-explicit t)
2048 (widget-choose tag (reverse choices) event))))
2050 ;; If this was an explicit user choice,
2051 ;; record the choice, and the record the value it was made for.
2052 ;; widget-choice-value-create will respect this choice,
2053 ;; as long as the value is the same.
2055 (widget-put widget :explicit-choice current)
2056 (widget-put widget :explicit-choice-value (widget-get widget :value)))
2057 (widget-value-set widget (widget-default-get current))
2059 (widget-apply widget :notify widget event)))
2060 (run-hook-with-args 'widget-edit-functions widget))
2062 (defun widget-choice-validate (widget)
2063 ;; Valid if we have made a valid choice.
2064 (if (eq (widget-get widget :void) (widget-get widget :choice))
2066 (widget-apply (car (widget-get widget :children)) :validate)))
2068 (defun widget-choice-match (widget value)
2069 ;; Matches if one of the choices matches.
2070 (let ((args (widget-get widget :args))
2072 (while (and args (not found))
2073 (setq current (car args)
2075 found (widget-apply current :match value)))
2078 (defun widget-choice-match-inline (widget values)
2079 ;; Matches if one of the choices matches.
2080 (let ((args (widget-get widget :args))
2082 (while (and args (null found))
2083 (setq current (car args)
2085 found (widget-match-inline current values)))
2088 ;;; The `toggle' Widget.
2090 (define-widget 'toggle 'item
2091 "Toggle between two states."
2093 :value-create 'widget-toggle-value-create
2094 :action 'widget-toggle-action
2095 :match (lambda (widget value) t)
2099 (defun widget-toggle-value-create (widget)
2100 "Insert text representing the `on' and `off' states."
2101 (if (widget-value widget)
2102 (let ((image (widget-get widget :on-glyph)))
2103 (and (display-graphic-p)
2105 (not (eq (car image) 'image))
2106 (widget-put widget :on-glyph (setq image (eval image))))
2107 (widget-image-insert widget
2108 (widget-get widget :on)
2110 (let ((image (widget-get widget :off-glyph)))
2111 (and (display-graphic-p)
2113 (not (eq (car image) 'image))
2114 (widget-put widget :off-glyph (setq image (eval image))))
2115 (widget-image-insert widget (widget-get widget :off) image))))
2117 (defun widget-toggle-action (widget &optional event)
2119 (widget-value-set widget (not (widget-value widget)))
2120 (widget-apply widget :notify widget event)
2121 (run-hook-with-args 'widget-edit-functions widget))
2123 ;;; The `checkbox' Widget.
2125 (define-widget 'checkbox 'toggle
2126 "A checkbox toggle."
2131 ;; We could probably do the same job as the images using single
2132 ;; space characters in a boxed face with a stretch specification to
2133 ;; make them square.
2134 :on-glyph '(create-image "\300\300\141\143\067\076\034\030"
2135 'xbm t :width 8 :height 8
2136 :background "grey75" ; like default mode line
2141 :off-glyph '(create-image (make-string 8 0)
2142 'xbm t :width 8 :height 8
2143 :background "grey75"
2147 :help-echo "Toggle this item."
2148 :action 'widget-checkbox-action)
2150 (defun widget-checkbox-action (widget &optional event)
2151 "Toggle checkbox, notify parent, and set active state of sibling."
2152 (widget-toggle-action widget event)
2153 (let ((sibling (widget-get-sibling widget)))
2155 (if (widget-value widget)
2156 (widget-apply sibling :activate)
2157 (widget-apply sibling :deactivate)))))
2159 ;;; The `checklist' Widget.
2161 (define-widget 'checklist 'default
2162 "A multiple choice widget."
2163 :convert-widget 'widget-types-convert-widget
2164 :copy 'widget-types-copy
2167 :entry-format "%b %v"
2169 :value-create 'widget-checklist-value-create
2170 :value-get 'widget-checklist-value-get
2171 :validate 'widget-checklist-validate
2172 :match 'widget-checklist-match
2173 :match-inline 'widget-checklist-match-inline)
2175 (defun widget-checklist-value-create (widget)
2176 ;; Insert all values
2177 (let ((alist (widget-checklist-match-find widget (widget-get widget :value)))
2178 (args (widget-get widget :args)))
2180 (widget-checklist-add-item widget (car args) (assq (car args) alist))
2181 (setq args (cdr args)))
2182 (widget-put widget :children (nreverse (widget-get widget :children)))))
2184 (defun widget-checklist-add-item (widget type chosen)
2185 "Create checklist item in WIDGET of type TYPE.
2186 If the item is checked, CHOSEN is a cons whose cdr is the value."
2187 (and (eq (preceding-char) ?\n)
2188 (widget-get widget :indent)
2189 (insert-char ?\s (widget-get widget :indent)))
2190 (widget-specify-insert
2191 (let* ((children (widget-get widget :children))
2192 (buttons (widget-get widget :buttons))
2193 (button-args (or (widget-get type :sibling-args)
2194 (widget-get widget :button-args)))
2197 (insert (widget-get widget :entry-format))
2199 ;; Parse % escapes in format.
2200 (while (re-search-forward "%\\([bv%]\\)" nil t)
2201 (let ((escape (char-after (match-beginning 1))))
2202 (delete-backward-char 2)
2203 (cond ((eq escape ?%)
2206 (setq button (apply 'widget-create-child-and-convert
2208 :value (not (null chosen))
2213 (let ((child (widget-create-child widget type)))
2214 (widget-apply child :deactivate)
2216 ((widget-get type :inline)
2217 (widget-create-child-value
2218 widget type (cdr chosen)))
2220 (widget-create-child-value
2221 widget type (car (cdr chosen)))))))
2223 (error "Unknown escape `%c'" escape)))))
2224 ;; Update properties.
2225 (and button child (widget-put child :button button))
2226 (and button (widget-put widget :buttons (cons button buttons)))
2227 (and child (widget-put widget :children (cons child children))))))
2229 (defun widget-checklist-match (widget values)
2230 ;; All values must match a type in the checklist.
2232 (null (cdr (widget-checklist-match-inline widget values)))))
2234 (defun widget-checklist-match-inline (widget values)
2235 ;; Find the values which match a type in the checklist.
2236 (let ((greedy (widget-get widget :greedy))
2237 (args (copy-sequence (widget-get widget :args)))
2240 (let ((answer (widget-checklist-match-up args values)))
2242 (let ((vals (widget-match-inline answer values)))
2243 (setq found (append found (car vals))
2245 args (delq answer args))))
2247 (setq rest (append rest (list (car values)))
2248 values (cdr values)))
2250 (setq rest (append rest values)
2254 (defun widget-checklist-match-find (widget vals)
2255 "Find the vals which match a type in the checklist.
2256 Return an alist of (TYPE MATCH)."
2257 (let ((greedy (widget-get widget :greedy))
2258 (args (copy-sequence (widget-get widget :args)))
2261 (let ((answer (widget-checklist-match-up args vals)))
2263 (let ((match (widget-match-inline answer vals)))
2264 (setq found (cons (cons answer (car match)) found)
2266 args (delq answer args))))
2268 (setq vals (cdr vals)))
2273 (defun widget-checklist-match-up (args vals)
2274 "Return the first type from ARGS that matches VALS."
2275 (let (current found)
2276 (while (and args (null found))
2277 (setq current (car args)
2279 found (widget-match-inline current vals)))
2283 (defun widget-checklist-value-get (widget)
2284 ;; The values of all selected items.
2285 (let ((children (widget-get widget :children))
2288 (setq child (car children)
2289 children (cdr children))
2290 (if (widget-value (widget-get child :button))
2291 (setq result (append result (widget-apply child :value-inline)))))
2294 (defun widget-checklist-validate (widget)
2295 ;; Ticked chilren must be valid.
2296 (let ((children (widget-get widget :children))
2298 (while (and children (not found))
2299 (setq child (car children)
2300 children (cdr children)
2301 button (widget-get child :button)
2302 found (and (widget-value button)
2303 (widget-apply child :validate))))
2306 ;;; The `option' Widget
2308 (define-widget 'option 'checklist
2309 "An widget with an optional item."
2312 ;;; The `choice-item' Widget.
2314 (define-widget 'choice-item 'item
2315 "Button items that delegate action events to their parents."
2316 :action 'widget-parent-action
2317 :format "%[%t%] \n")
2319 ;;; The `radio-button' Widget.
2321 (define-widget 'radio-button 'toggle
2322 "A radio button for use in the `radio' widget."
2323 :notify 'widget-radio-button-notify
2330 :off-glyph "radio0")
2332 (defun widget-radio-button-notify (widget child &optional event)
2334 (widget-apply (widget-get widget :parent) :action widget event))
2336 ;;; The `radio-button-choice' Widget.
2338 (define-widget 'radio-button-choice 'default
2339 "Select one of multiple options."
2340 :convert-widget 'widget-types-convert-widget
2341 :copy 'widget-types-copy
2344 :entry-format "%b %v"
2345 :value-create 'widget-radio-value-create
2346 :value-get 'widget-radio-value-get
2347 :value-inline 'widget-radio-value-inline
2348 :value-set 'widget-radio-value-set
2349 :error "You must push one of the buttons"
2350 :validate 'widget-radio-validate
2351 :match 'widget-choice-match
2352 :match-inline 'widget-choice-match-inline
2353 :action 'widget-radio-action)
2355 (defun widget-radio-value-create (widget)
2356 ;; Insert all values
2357 (let ((args (widget-get widget :args))
2360 (setq arg (car args)
2362 (widget-radio-add-item widget arg))))
2364 (defun widget-radio-add-item (widget type)
2365 "Add to radio widget WIDGET a new radio button item of type TYPE."
2366 ;; (setq type (widget-convert type))
2367 (and (eq (preceding-char) ?\n)
2368 (widget-get widget :indent)
2369 (insert-char ?\s (widget-get widget :indent)))
2370 (widget-specify-insert
2371 (let* ((value (widget-get widget :value))
2372 (children (widget-get widget :children))
2373 (buttons (widget-get widget :buttons))
2374 (button-args (or (widget-get type :sibling-args)
2375 (widget-get widget :button-args)))
2377 (chosen (and (null (widget-get widget :choice))
2378 (widget-apply type :match value)))
2380 (insert (widget-get widget :entry-format))
2382 ;; Parse % escapes in format.
2383 (while (re-search-forward "%\\([bv%]\\)" nil t)
2384 (let ((escape (char-after (match-beginning 1))))
2385 (delete-backward-char 2)
2386 (cond ((eq escape ?%)
2389 (setq button (apply 'widget-create-child-and-convert
2390 widget 'radio-button
2391 :value (not (null chosen))
2394 (setq child (if chosen
2395 (widget-create-child-value
2397 (widget-create-child widget type)))
2399 (widget-apply child :deactivate)))
2401 (error "Unknown escape `%c'" escape)))))
2402 ;; Update properties.
2404 (widget-put widget :choice type))
2406 (widget-put child :button button)
2407 (widget-put widget :buttons (nconc buttons (list button))))
2409 (widget-put widget :children (nconc children (list child))))
2412 (defun widget-radio-value-get (widget)
2413 ;; Get value of the child widget.
2414 (let ((chosen (widget-radio-chosen widget)))
2415 (and chosen (widget-value chosen))))
2417 (defun widget-radio-chosen (widget)
2418 "Return the widget representing the chosen radio button."
2419 (let ((children (widget-get widget :children))
2422 (setq current (car children)
2423 children (cdr children))
2424 (when (widget-apply (widget-get current :button) :value-get)
2429 (defun widget-radio-value-inline (widget)
2430 ;; Get value of the child widget.
2431 (let ((children (widget-get widget :children))
2434 (setq current (car children)
2435 children (cdr children))
2436 (when (widget-apply (widget-get current :button) :value-get)
2437 (setq found (widget-apply current :value-inline)
2441 (defun widget-radio-value-set (widget value)
2442 ;; We can't just delete and recreate a radio widget, since children
2443 ;; can be added after the original creation and won't be recreated
2445 (let ((children (widget-get widget :children))
2448 (setq current (car children)
2449 children (cdr children))
2450 (let* ((button (widget-get current :button))
2451 (match (and (not found)
2452 (widget-apply current :match value))))
2453 (widget-value-set button match)
2456 (widget-value-set current value)
2457 (widget-apply current :activate))
2458 (widget-apply current :deactivate))
2459 (setq found (or found match))))))
2461 (defun widget-radio-validate (widget)
2462 ;; Valid if we have made a valid choice.
2463 (let ((children (widget-get widget :children))
2464 current found button)
2465 (while (and children (not found))
2466 (setq current (car children)
2467 children (cdr children)
2468 button (widget-get current :button)
2469 found (widget-apply button :value-get)))
2471 (widget-apply current :validate)
2474 (defun widget-radio-action (widget child event)
2475 ;; Check if a radio button was pressed.
2476 (let ((children (widget-get widget :children))
2477 (buttons (widget-get widget :buttons))
2479 (when (memq child buttons)
2481 (setq current (car children)
2482 children (cdr children))
2483 (let* ((button (widget-get current :button)))
2484 (cond ((eq child button)
2485 (widget-value-set button t)
2486 (widget-apply current :activate))
2487 ((widget-value button)
2488 (widget-value-set button nil)
2489 (widget-apply current :deactivate)))))))
2490 ;; Pass notification to parent.
2491 (widget-apply widget :notify child event))
2493 ;;; The `insert-button' Widget.
2495 (define-widget 'insert-button 'push-button
2496 "An insert button for the `editable-list' widget."
2498 :help-echo "Insert a new item into the list at this position."
2499 :action 'widget-insert-button-action)
2501 (defun widget-insert-button-action (widget &optional event)
2502 ;; Ask the parent to insert a new item.
2503 (widget-apply (widget-get widget :parent)
2504 :insert-before (widget-get widget :widget)))
2506 ;;; The `delete-button' Widget.
2508 (define-widget 'delete-button 'push-button
2509 "A delete button for the `editable-list' widget."
2511 :help-echo "Delete this item from the list."
2512 :action 'widget-delete-button-action)
2514 (defun widget-delete-button-action (widget &optional event)
2515 ;; Ask the parent to insert a new item.
2516 (widget-apply (widget-get widget :parent)
2517 :delete-at (widget-get widget :widget)))
2519 ;;; The `editable-list' Widget.
2521 ;; (defcustom widget-editable-list-gui nil
2522 ;; "If non nil, use GUI push-buttons in editable list when available."
2526 (define-widget 'editable-list 'default
2527 "A variable list of widgets of the same type."
2528 :convert-widget 'widget-types-convert-widget
2529 :copy 'widget-types-copy
2532 :format-handler 'widget-editable-list-format-handler
2533 :entry-format "%i %d %v"
2534 :value-create 'widget-editable-list-value-create
2535 :value-get 'widget-editable-list-value-get
2536 :validate 'widget-children-validate
2537 :match 'widget-editable-list-match
2538 :match-inline 'widget-editable-list-match-inline
2539 :insert-before 'widget-editable-list-insert-before
2540 :delete-at 'widget-editable-list-delete-at)
2542 (defun widget-editable-list-format-handler (widget escape)
2543 ;; We recognize the insert button.
2544 ;; (let ((widget-push-button-gui widget-editable-list-gui))
2545 (cond ((eq escape ?i)
2546 (and (widget-get widget :indent)
2547 (insert-char ?\s (widget-get widget :indent)))
2548 (apply 'widget-create-child-and-convert
2549 widget 'insert-button
2550 (widget-get widget :append-button-args)))
2552 (widget-default-format-handler widget escape)))
2556 (defun widget-editable-list-value-create (widget)
2557 ;; Insert all values
2558 (let* ((value (widget-get widget :value))
2559 (type (nth 0 (widget-get widget :args)))
2561 (widget-put widget :value-pos (copy-marker (point)))
2562 (set-marker-insertion-type (widget-get widget :value-pos) t)
2564 (let ((answer (widget-match-inline type value)))
2566 (setq children (cons (widget-editable-list-entry-create
2568 (if (widget-get type :inline)
2575 (widget-put widget :children (nreverse children))))
2577 (defun widget-editable-list-value-get (widget)
2578 ;; Get value of the child widget.
2579 (apply 'append (mapcar (lambda (child) (widget-apply child :value-inline))
2580 (widget-get widget :children))))
2582 (defun widget-editable-list-match (widget value)
2583 ;; Value must be a list and all the members must match the type.
2585 (null (cdr (widget-editable-list-match-inline widget value)))))
2587 (defun widget-editable-list-match-inline (widget value)
2588 (let ((type (nth 0 (widget-get widget :args)))
2591 (while (and value ok)
2592 (let ((answer (widget-match-inline type value)))
2594 (setq found (append found (car answer))
2597 (cons found value)))
2599 (defun widget-editable-list-insert-before (widget before)
2600 ;; Insert a new child in the list of children.
2602 (let ((children (widget-get widget :children))
2603 (inhibit-read-only t)
2604 before-change-functions
2605 after-change-functions)
2607 (goto-char (widget-get before :entry-from)))
2609 (goto-char (widget-get widget :value-pos))))
2610 (let ((child (widget-editable-list-entry-create
2612 (when (< (widget-get child :entry-from) (widget-get widget :from))
2613 (set-marker (widget-get widget :from)
2614 (widget-get child :entry-from)))
2615 (if (eq (car children) before)
2616 (widget-put widget :children (cons child children))
2617 (while (not (eq (car (cdr children)) before))
2618 (setq children (cdr children)))
2619 (setcdr children (cons child (cdr children)))))))
2621 (widget-apply widget :notify widget))
2623 (defun widget-editable-list-delete-at (widget child)
2624 ;; Delete child from list of children.
2626 (let ((buttons (copy-sequence (widget-get widget :buttons)))
2628 (inhibit-read-only t)
2629 before-change-functions
2630 after-change-functions)
2632 (setq button (car buttons)
2633 buttons (cdr buttons))
2634 (when (eq (widget-get button :widget) child)
2636 :buttons (delq button (widget-get widget :buttons)))
2637 (widget-delete button))))
2638 (let ((entry-from (widget-get child :entry-from))
2639 (entry-to (widget-get child :entry-to))
2640 (inhibit-read-only t)
2641 before-change-functions
2642 after-change-functions)
2643 (widget-delete child)
2644 (delete-region entry-from entry-to)
2645 (set-marker entry-from nil)
2646 (set-marker entry-to nil))
2647 (widget-put widget :children (delq child (widget-get widget :children))))
2649 (widget-apply widget :notify widget))
2651 (defun widget-editable-list-entry-create (widget value conv)
2652 ;; Create a new entry to the list.
2653 (let ((type (nth 0 (widget-get widget :args)))
2654 ;; (widget-push-button-gui widget-editable-list-gui)
2655 child delete insert)
2656 (widget-specify-insert
2658 (and (widget-get widget :indent)
2659 (insert-char ?\s (widget-get widget :indent)))
2660 (insert (widget-get widget :entry-format)))
2661 ;; Parse % escapes in format.
2662 (while (re-search-forward "%\\(.\\)" nil t)
2663 (let ((escape (char-after (match-beginning 1))))
2664 (delete-backward-char 2)
2665 (cond ((eq escape ?%)
2668 (setq insert (apply 'widget-create-child-and-convert
2669 widget 'insert-button
2670 (widget-get widget :insert-button-args))))
2672 (setq delete (apply 'widget-create-child-and-convert
2673 widget 'delete-button
2674 (widget-get widget :delete-button-args))))
2677 (setq child (widget-create-child-value
2679 (setq child (widget-create-child-value
2680 widget type (widget-default-get type)))))
2682 (error "Unknown escape `%c'" escape)))))
2683 (let ((buttons (widget-get widget :buttons)))
2684 (if insert (push insert buttons))
2685 (if delete (push delete buttons))
2686 (widget-put widget :buttons buttons))
2687 (let ((entry-from (point-min-marker))
2688 (entry-to (point-max-marker)))
2689 (set-marker-insertion-type entry-from t)
2690 (set-marker-insertion-type entry-to nil)
2691 (widget-put child :entry-from entry-from)
2692 (widget-put child :entry-to entry-to)))
2693 (if insert (widget-put insert :widget child))
2694 (if delete (widget-put delete :widget child))
2697 ;;; The `group' Widget.
2699 (define-widget 'group 'default
2700 "A widget which groups other widgets inside."
2701 :convert-widget 'widget-types-convert-widget
2702 :copy 'widget-types-copy
2704 :value-create 'widget-group-value-create
2705 :value-get 'widget-editable-list-value-get
2706 :default-get 'widget-group-default-get
2707 :validate 'widget-children-validate
2708 :match 'widget-group-match
2709 :match-inline 'widget-group-match-inline)
2711 (defun widget-group-value-create (widget)
2712 ;; Create each component.
2713 (let ((args (widget-get widget :args))
2714 (value (widget-get widget :value))
2715 arg answer children)
2717 (setq arg (car args)
2719 answer (widget-match-inline arg value)
2721 (and (eq (preceding-char) ?\n)
2722 (widget-get widget :indent)
2723 (insert-char ?\s (widget-get widget :indent)))
2724 (push (cond ((null answer)
2725 (widget-create-child widget arg))
2726 ((widget-get arg :inline)
2727 (widget-create-child-value widget arg (car answer)))
2729 (widget-create-child-value widget arg (car (car answer)))))
2731 (widget-put widget :children (nreverse children))))
2733 (defun widget-group-default-get (widget)
2734 ;; Get the default of the components.
2735 (mapcar 'widget-default-get (widget-get widget :args)))
2737 (defun widget-group-match (widget values)
2738 ;; Match if the components match.
2740 (let ((match (widget-group-match-inline widget values)))
2741 (and match (null (cdr match))))))
2743 (defun widget-group-match-inline (widget vals)
2744 ;; Match if the components match.
2745 (let ((args (widget-get widget :args))
2746 argument answer found)
2748 (setq argument (car args)
2750 answer (widget-match-inline argument vals))
2752 (setq vals (cdr answer)
2753 found (append found (car answer)))
2757 (cons found vals))))
2759 ;;; The `visibility' Widget.
2761 (define-widget 'visibility 'item
2762 "An indicator and manipulator for hidden items."
2768 :value-create 'widget-visibility-value-create
2769 :action 'widget-toggle-action
2770 :match (lambda (widget value) t))
2772 (defun widget-visibility-value-create (widget)
2773 ;; Insert text representing the `on' and `off' states.
2774 (let ((on (widget-get widget :on))
2775 (off (widget-get widget :off)))
2777 (setq on (concat widget-push-button-prefix
2779 widget-push-button-suffix))
2782 (setq off (concat widget-push-button-prefix
2784 widget-push-button-suffix))
2786 (if (widget-value widget)
2787 (widget-image-insert widget on "down" "down-pushed")
2788 (widget-image-insert widget off "right" "right-pushed"))))
2790 ;;; The `documentation-link' Widget.
2792 ;; This is a helper widget for `documentation-string'.
2794 (define-widget 'documentation-link 'link
2795 "Link type used in documentation strings."
2797 :help-echo "Describe this symbol"
2798 :action 'widget-documentation-link-action)
2800 (defun widget-documentation-link-action (widget &optional event)
2801 "Display documentation for WIDGET's value. Ignore optional argument EVENT."
2802 (let* ((string (widget-get widget :value))
2803 (symbol (intern string)))
2804 (if (and (fboundp symbol) (boundp symbol))
2805 ;; If there are two doc strings, give the user a way to pick one.
2806 (apropos (concat "\\`" (regexp-quote string) "\\'"))
2807 (if (fboundp symbol)
2808 (describe-function symbol)
2809 (describe-variable symbol)))))
2811 (defcustom widget-documentation-links t
2812 "Add hyperlinks to documentation strings when non-nil."
2814 :group 'widget-documentation)
2816 (defcustom widget-documentation-link-regexp "`\\([^\n`' ]+\\)'"
2817 "Regexp for matching potential links in documentation strings.
2818 The first group should be the link itself."
2820 :group 'widget-documentation)
2822 (defcustom widget-documentation-link-p 'intern-soft
2823 "Predicate used to test if a string is useful as a link.
2824 The value should be a function. The function will be called one
2825 argument, a string, and should return non-nil if there should be a
2826 link for that string."
2828 :options '(widget-documentation-link-p)
2829 :group 'widget-documentation)
2831 (defcustom widget-documentation-link-type 'documentation-link
2832 "Widget type used for links in documentation strings."
2834 :group 'widget-documentation)
2836 (defun widget-documentation-link-add (widget from to)
2837 (widget-specify-doc widget from to)
2838 (when widget-documentation-links
2839 (let ((regexp widget-documentation-link-regexp)
2840 (buttons (widget-get widget :buttons))
2841 (widget-mouse-face (default-value 'widget-mouse-face))
2842 (widget-button-face widget-documentation-face)
2843 (widget-button-pressed-face widget-documentation-face))
2846 (while (re-search-forward regexp to t)
2847 (let ((name (match-string 1))
2848 (begin (match-beginning 1))
2849 (end (match-end 1)))
2850 (when (funcall widget-documentation-link-p name)
2851 (push (widget-convert-button widget-documentation-link-type
2852 begin end :value name)
2854 (widget-put widget :buttons buttons)))
2855 (let ((indent (widget-get widget :indent)))
2856 (when (and indent (not (zerop indent)))
2859 (narrow-to-region from to)
2860 (goto-char (point-min))
2861 (while (search-forward "\n" nil t)
2862 (insert-char ?\s indent)))))))
2864 ;;; The `documentation-string' Widget.
2866 (define-widget 'documentation-string 'item
2867 "A documentation string."
2869 :action 'widget-documentation-string-action
2870 :value-create 'widget-documentation-string-value-create)
2872 (defun widget-documentation-string-value-create (widget)
2873 ;; Insert documentation string.
2874 (let ((doc (widget-value widget))
2875 (indent (widget-get widget :indent))
2876 (shown (widget-get (widget-get widget :parent) :documentation-shown))
2878 (if (string-match "\n" doc)
2879 (let ((before (substring doc 0 (match-beginning 0)))
2880 (after (substring doc (match-beginning 0)))
2883 (widget-documentation-link-add widget start (point))
2885 (widget-create-child-and-convert
2887 :help-echo "Show or hide rest of the documentation."
2891 :action 'widget-parent-action
2894 (setq start (point))
2895 (when (and indent (not (zerop indent)))
2896 (insert-char ?\s indent))
2898 (widget-documentation-link-add widget start (point)))
2899 (widget-put widget :buttons (list button)))
2901 (widget-documentation-link-add widget start (point))))
2904 (defun widget-documentation-string-action (widget &rest ignore)
2905 ;; Toggle documentation.
2906 (let ((parent (widget-get widget :parent)))
2907 (widget-put parent :documentation-shown
2908 (not (widget-get parent :documentation-shown))))
2910 (widget-value-set widget (widget-value widget)))
2912 ;;; The Sexp Widgets.
2914 (define-widget 'const 'item
2915 "An immutable sexp."
2916 :prompt-value 'widget-const-prompt-value
2919 (defun widget-const-prompt-value (widget prompt value unbound)
2920 ;; Return the value of the const.
2921 (widget-value widget))
2923 (define-widget 'function-item 'const
2924 "An immutable function name."
2926 :documentation-property (lambda (symbol)
2928 (documentation symbol t)
2931 (define-widget 'variable-item 'const
2932 "An immutable variable name."
2934 :documentation-property 'variable-documentation)
2936 (define-widget 'other 'sexp
2937 "Matches any value, but doesn't let the user edit the value.
2938 This is useful as last item in a `choice' widget.
2939 You should use this widget type with a default value,
2940 as in (other DEFAULT) or (other :tag \"NAME\" DEFAULT).
2941 If the user selects this alternative, that specifies DEFAULT
2947 (defvar widget-string-prompt-value-history nil
2948 "History of input to `widget-string-prompt-value'.")
2950 (define-widget 'string 'editable-field
2953 :format "%{%t%}: %v"
2954 :complete-function 'ispell-complete-word
2955 :prompt-history 'widget-string-prompt-value-history)
2957 (define-widget 'regexp 'string
2958 "A regular expression."
2959 :match 'widget-regexp-match
2960 :validate 'widget-regexp-validate
2961 ;; Doesn't work well with terminating newline.
2962 ;; :value-face 'widget-single-line-field
2965 (defun widget-regexp-match (widget value)
2966 ;; Match valid regexps.
2967 (and (stringp value)
2970 (string-match value ""))
2973 (defun widget-regexp-validate (widget)
2974 "Check that the value of WIDGET is a valid regexp."
2975 (condition-case data
2977 (string-match (widget-value widget) ""))
2978 (error (widget-put widget :error (error-message-string data))
2981 (define-widget 'file 'string
2983 It will read a file name from the minibuffer when invoked."
2984 :complete-function 'widget-file-complete
2985 :prompt-value 'widget-file-prompt-value
2986 :format "%{%t%}: %v"
2987 ;; Doesn't work well with terminating newline.
2988 ;; :value-face 'widget-single-line-field
2991 (defun widget-file-complete ()
2992 "Perform completion on file name preceding point."
2994 (let* ((end (point))
2995 (beg (save-excursion
2996 (skip-chars-backward "^ ")
2998 (pattern (buffer-substring beg end))
2999 (name-part (file-name-nondirectory pattern))
3000 (directory (file-name-directory pattern))
3001 (completion (file-name-completion name-part directory)))
3002 (cond ((eq completion t))
3004 (message "Can't find completion for \"%s\"" pattern)
3006 ((not (string= name-part completion))
3007 (delete-region beg end)
3008 (insert (expand-file-name completion directory)))
3010 (message "Making completion list...")
3011 (with-output-to-temp-buffer "*Completions*"
3012 (display-completion-list
3013 (sort (file-name-all-completions name-part directory)
3015 (message "Making completion list...%s" "done")))))
3017 (defun widget-file-prompt-value (widget prompt value unbound)
3018 ;; Read file from minibuffer.
3019 (abbreviate-file-name
3021 (read-file-name prompt)
3022 (let ((prompt2 (format "%s (default %s) " prompt value))
3023 (dir (file-name-directory value))
3024 (file (file-name-nondirectory value))
3025 (must-match (widget-get widget :must-match)))
3026 (read-file-name prompt2 dir nil must-match file)))))
3028 ;;;(defun widget-file-action (widget &optional event)
3029 ;;; ;; Read a file name from the minibuffer.
3030 ;;; (let* ((value (widget-value widget))
3031 ;;; (dir (file-name-directory value))
3032 ;;; (file (file-name-nondirectory value))
3033 ;;; (menu-tag (widget-apply widget :menu-tag-get))
3034 ;;; (must-match (widget-get widget :must-match))
3035 ;;; (answer (read-file-name (concat menu-tag ": (default `" value "') ")
3036 ;;; dir nil must-match file)))
3037 ;;; (widget-value-set widget (abbreviate-file-name answer))
3039 ;;; (widget-apply widget :notify widget event)))
3041 ;; Fixme: use file-name-as-directory.
3042 (define-widget 'directory 'file
3043 "A directory widget.
3044 It will read a directory name from the minibuffer when invoked."
3047 (defvar widget-symbol-prompt-value-history nil
3048 "History of input to `widget-symbol-prompt-value'.")
3050 (define-widget 'symbol 'editable-field
3054 :format "%{%t%}: %v"
3055 :match (lambda (widget value) (symbolp value))
3056 :complete-function 'lisp-complete-symbol
3057 :prompt-internal 'widget-symbol-prompt-internal
3058 :prompt-match 'symbolp
3059 :prompt-history 'widget-symbol-prompt-value-history
3060 :value-to-internal (lambda (widget value)
3064 :value-to-external (lambda (widget value)
3069 (defun widget-symbol-prompt-internal (widget prompt initial history)
3070 ;; Read file from minibuffer.
3071 (let ((answer (completing-read prompt obarray
3072 (widget-get widget :prompt-match)
3073 nil initial history)))
3074 (if (and (stringp answer)
3075 (not (zerop (length answer))))
3077 (error "No value"))))
3079 (defvar widget-function-prompt-value-history nil
3080 "History of input to `widget-function-prompt-value'.")
3082 (define-widget 'function 'restricted-sexp
3084 :complete-function (lambda ()
3086 (lisp-complete-symbol 'fboundp))
3087 :prompt-value 'widget-field-prompt-value
3088 :prompt-internal 'widget-symbol-prompt-internal
3089 :prompt-match 'fboundp
3090 :prompt-history 'widget-function-prompt-value-history
3091 :action 'widget-field-action
3092 :match-alternatives '(functionp)
3093 :validate (lambda (widget)
3094 (unless (functionp (widget-value widget))
3095 (widget-put widget :error (format "Invalid function: %S"
3096 (widget-value widget)))
3101 (defvar widget-variable-prompt-value-history nil
3102 "History of input to `widget-variable-prompt-value'.")
3104 (define-widget 'variable 'symbol
3106 :prompt-match 'boundp
3107 :prompt-history 'widget-variable-prompt-value-history
3108 :complete-function (lambda ()
3110 (lisp-complete-symbol 'boundp))
3113 (defvar widget-coding-system-prompt-value-history nil
3114 "History of input to `widget-coding-system-prompt-value'.")
3116 (define-widget 'coding-system 'symbol
3117 "A MULE coding-system."
3118 :format "%{%t%}: %v"
3119 :tag "Coding system"
3121 :prompt-history 'widget-coding-system-prompt-value-history
3122 :prompt-value 'widget-coding-system-prompt-value
3123 :action 'widget-coding-system-action
3124 :complete-function (lambda ()
3126 (lisp-complete-symbol 'coding-system-p))
3127 :validate (lambda (widget)
3128 (unless (coding-system-p (widget-value widget))
3129 (widget-put widget :error (format "Invalid coding system: %S"
3130 (widget-value widget)))
3133 :prompt-match 'coding-system-p)
3135 (defun widget-coding-system-prompt-value (widget prompt value unbound)
3136 "Read coding-system from minibuffer."
3137 (if (widget-get widget :base-only)
3139 (completing-read (format "%s (default %s) " prompt value)
3140 (mapcar #'list (coding-system-list t)) nil nil nil
3141 coding-system-history))
3142 (read-coding-system (format "%s (default %s) " prompt value) value)))
3144 (defun widget-coding-system-action (widget &optional event)
3146 (widget-coding-system-prompt-value
3148 (widget-apply widget :menu-tag-get)
3149 (widget-value widget)
3151 (widget-value-set widget answer)
3152 (widget-apply widget :notify widget event)
3155 (define-widget 'sexp 'editable-field
3156 "An arbitrary Lisp expression."
3157 :tag "Lisp expression"
3158 :format "%{%t%}: %v"
3160 :validate 'widget-sexp-validate
3161 :match (lambda (widget value) t)
3162 :value-to-internal 'widget-sexp-value-to-internal
3163 :value-to-external (lambda (widget value) (read value))
3164 :prompt-history 'widget-sexp-prompt-value-history
3165 :prompt-value 'widget-sexp-prompt-value)
3167 (defun widget-sexp-value-to-internal (widget value)
3168 ;; Use pp for printer representation.
3169 (let ((pp (if (symbolp value)
3170 (prin1-to-string value)
3171 (pp-to-string value))))
3172 (while (string-match "\n\\'" pp)
3173 (setq pp (substring pp 0 -1)))
3174 (if (or (string-match "\n\\'" pp)
3179 (defun widget-sexp-validate (widget)
3180 ;; Valid if we can read the string and there is no junk left after it.
3182 (insert (widget-apply widget :value-get))
3183 (goto-char (point-min))
3185 (condition-case data
3187 ;; Avoid a confusing end-of-file error.
3188 (skip-syntax-forward "\\s-")
3190 (setq err "Empty sexp -- use `nil'?")
3191 (unless (widget-apply widget :match (read (current-buffer)))
3192 (setq err (widget-get widget :type-error))))
3193 ;; Allow whitespace after expression.
3194 (skip-syntax-forward "\\s-")
3195 (if (and (not (eobp))
3197 (setq err (format "Junk at end of expression: %s"
3198 (buffer-substring (point)
3200 (end-of-file ; Avoid confusing error message.
3201 (setq err "Unbalanced sexp"))
3202 (error (setq err (error-message-string data))))
3205 (widget-put widget :error err)
3208 (defvar widget-sexp-prompt-value-history nil
3209 "History of input to `widget-sexp-prompt-value'.")
3211 (defun widget-sexp-prompt-value (widget prompt value unbound)
3212 ;; Read an arbitrary sexp.
3213 (let ((found (read-string prompt
3214 (if unbound nil (cons (prin1-to-string value) 0))
3215 (widget-get widget :prompt-history))))
3216 (let ((answer (read-from-string found)))
3217 (unless (= (cdr answer) (length found))
3218 (error "Junk at end of expression: %s"
3219 (substring found (cdr answer))))
3222 (define-widget 'restricted-sexp 'sexp
3223 "A Lisp expression restricted to values that match.
3224 To use this type, you must define :match or :match-alternatives."
3225 :type-error "The specified value is not valid"
3226 :match 'widget-restricted-sexp-match
3227 :value-to-internal (lambda (widget value)
3228 (if (widget-apply widget :match value)
3229 (prin1-to-string value)
3232 (defun widget-restricted-sexp-match (widget value)
3233 (let ((alternatives (widget-get widget :match-alternatives))
3235 (while (and alternatives (not matched))
3236 (if (cond ((functionp (car alternatives))
3237 (funcall (car alternatives) value))
3238 ((and (consp (car alternatives))
3239 (eq (car (car alternatives)) 'quote))
3240 (eq value (nth 1 (car alternatives)))))
3242 (setq alternatives (cdr alternatives)))
3245 (define-widget 'integer 'restricted-sexp
3249 :type-error "This field should contain an integer"
3250 :match-alternatives '(integerp))
3252 (define-widget 'number 'restricted-sexp
3253 "A number (floating point or integer)."
3256 :type-error "This field should contain a number (floating point or integer)"
3257 :match-alternatives '(numberp))
3259 (define-widget 'float 'restricted-sexp
3260 "A floating point number."
3261 :tag "Floating point number"
3263 :type-error "This field should contain a floating point number"
3264 :match-alternatives '(floatp))
3266 (define-widget 'character 'editable-field
3271 :format "%{%t%}: %v\n"
3272 :valid-regexp "\\`.\\'"
3273 :error "This field should contain a single character"
3274 :value-to-internal (lambda (widget value)
3277 (char-to-string value)))
3278 :value-to-external (lambda (widget value)
3282 :match (lambda (widget value)
3283 (char-valid-p value)))
3285 (define-widget 'list 'group
3288 :format "%{%t%}:\n%v")
3290 (define-widget 'vector 'group
3293 :format "%{%t%}:\n%v"
3294 :match 'widget-vector-match
3295 :value-to-internal (lambda (widget value) (append value nil))
3296 :value-to-external (lambda (widget value) (apply 'vector value)))
3298 (defun widget-vector-match (widget value)
3299 (and (vectorp value)
3300 (widget-group-match widget
3301 (widget-apply widget :value-to-internal value))))
3303 (define-widget 'cons 'group
3306 :format "%{%t%}:\n%v"
3307 :match 'widget-cons-match
3308 :value-to-internal (lambda (widget value)
3309 (list (car value) (cdr value)))
3310 :value-to-external (lambda (widget value)
3311 (apply 'cons value)))
3313 (defun widget-cons-match (widget value)
3315 (widget-group-match widget
3316 (widget-apply widget :value-to-internal value))))
3318 ;;; The `lazy' Widget.
3320 ;; Recursive datatypes.
3322 (define-widget 'lazy 'default
3323 "Base widget for recursive datastructures.
3325 The `lazy' widget will, when instantiated, contain a single inferior
3326 widget, of the widget type specified by the :type parameter. The
3327 value of the `lazy' widget is the same as the value of the inferior
3328 widget. When deriving a new widget from the 'lazy' widget, the :type
3329 parameter is allowed to refer to the widget currently being defined,
3330 thus allowing recursive datastructures to be described.
3332 The :type parameter takes the same arguments as the defcustom
3333 parameter with the same name.
3335 Most composite widgets, i.e. widgets containing other widgets, does
3336 not allow recursion. That is, when you define a new widget type, none
3337 of the inferior widgets may be of the same type you are currently
3340 In Lisp, however, it is custom to define datastructures in terms of
3341 themselves. A list, for example, is defined as either nil, or a cons
3342 cell whose cdr itself is a list. The obvious way to translate this
3343 into a widget type would be
3345 (define-widget 'my-list 'choice
3346 \"A list of sexps.\"
3348 :args '((const nil) (cons :value (nil) sexp my-list)))
3350 Here we attempt to define my-list as a choice of either the constant
3351 nil, or a cons-cell containing a sexp and my-lisp. This will not work
3352 because the `choice' widget does not allow recursion.
3354 Using the `lazy' widget you can overcome this problem, as in this
3357 (define-widget 'sexp-list 'lazy
3358 \"A list of sexps.\"
3360 :type '(choice (const nil) (cons :value (nil) sexp sexp-list)))"
3361 :format "%{%t%}: %v"
3362 ;; We don't convert :type because we want to allow recursive
3363 ;; datastructures. This is slow, so we should not create speed
3364 ;; critical widgets by deriving from this.
3365 :convert-widget 'widget-value-convert-widget
3366 :value-create 'widget-type-value-create
3367 :value-get 'widget-child-value-get
3368 :value-inline 'widget-child-value-inline
3369 :default-get 'widget-type-default-get
3370 :match 'widget-type-match
3371 :validate 'widget-child-validate)
3374 ;;; The `plist' Widget.
3378 (define-widget 'plist 'list
3380 :key-type '(symbol :tag "Key")
3381 :value-type '(sexp :tag "Value")
3382 :convert-widget 'widget-plist-convert-widget
3385 (defvar widget-plist-value-type) ;Dynamic variable
3387 (defun widget-plist-convert-widget (widget)
3388 ;; Handle `:options'.
3389 (let* ((options (widget-get widget :options))
3390 (widget-plist-value-type (widget-get widget :value-type))
3391 (other `(editable-list :inline t
3393 ,(widget-get widget :key-type)
3394 ,widget-plist-value-type)))
3396 (list `(checklist :inline t
3398 ,@(mapcar 'widget-plist-convert-option
3402 (widget-put widget :args args)
3405 (defun widget-plist-convert-option (option)
3406 ;; Convert a single plist option.
3407 (let (key-type value-type)
3409 (let ((key (nth 0 option)))
3410 (setq value-type (nth 1 option))
3413 (setq key-type `(const ,key))))
3414 (setq key-type `(const ,option)
3415 value-type widget-plist-value-type))
3416 `(group :format "Key: %v" :inline t ,key-type ,value-type)))
3419 ;;; The `alist' Widget.
3421 ;; Association lists.
3423 (define-widget 'alist 'list
3424 "An association list."
3425 :key-type '(sexp :tag "Key")
3426 :value-type '(sexp :tag "Value")
3427 :convert-widget 'widget-alist-convert-widget
3430 (defvar widget-alist-value-type) ;Dynamic variable
3432 (defun widget-alist-convert-widget (widget)
3433 ;; Handle `:options'.
3434 (let* ((options (widget-get widget :options))
3435 (widget-alist-value-type (widget-get widget :value-type))
3436 (other `(editable-list :inline t
3438 ,(widget-get widget :key-type)
3439 ,widget-alist-value-type)))
3441 (list `(checklist :inline t
3443 ,@(mapcar 'widget-alist-convert-option
3447 (widget-put widget :args args)
3450 (defun widget-alist-convert-option (option)
3451 ;; Convert a single alist option.
3452 (let (key-type value-type)
3454 (let ((key (nth 0 option)))
3455 (setq value-type (nth 1 option))
3458 (setq key-type `(const ,key))))
3459 (setq key-type `(const ,option)
3460 value-type widget-alist-value-type))
3461 `(cons :format "Key: %v" ,key-type ,value-type)))
3463 (define-widget 'choice 'menu-choice
3464 "A union of several sexp types."
3466 :format "%{%t%}: %[Value Menu%] %v"
3467 :button-prefix 'widget-push-button-prefix
3468 :button-suffix 'widget-push-button-suffix
3469 :prompt-value 'widget-choice-prompt-value)
3471 (defun widget-choice-prompt-value (widget prompt value unbound)
3473 (let ((args (widget-get widget :args))
3474 (completion-ignore-case (widget-get widget :case-fold))
3475 current choices old)
3476 ;; Find the first arg that matches VALUE.
3479 (if (widget-apply (car look) :match value)
3480 (setq old (car look)
3482 (setq look (cdr look)))))
3485 (cond ((= (length args) 0)
3487 ((= (length args) 1)
3489 ((and (= (length args) 2)
3491 (if (eq old (nth 0 args))
3496 (setq current (car args)
3499 (cons (cons (widget-apply current :menu-tag-get)
3502 (let ((val (completing-read prompt choices nil t)))
3504 (let ((try (try-completion val choices)))
3507 (cdr (assoc val choices)))
3510 (widget-prompt-value current prompt nil t)
3513 (define-widget 'radio 'radio-button-choice
3514 "A union of several sexp types."
3516 :format "%{%t%}:\n%v"
3517 :prompt-value 'widget-choice-prompt-value)
3519 (define-widget 'repeat 'editable-list
3520 "A variable length homogeneous list."
3522 :format "%{%t%}:\n%v%i\n")
3524 (define-widget 'set 'checklist
3525 "A list of members from a fixed set."
3527 :format "%{%t%}:\n%v")
3529 (define-widget 'boolean 'toggle
3530 "To be nil or non-nil, that is the question."
3532 :prompt-value 'widget-boolean-prompt-value
3533 :button-prefix 'widget-push-button-prefix
3534 :button-suffix 'widget-push-button-suffix
3535 :format "%{%t%}: %[Toggle%] %v\n"
3539 (defun widget-boolean-prompt-value (widget prompt value unbound)
3540 ;; Toggle a boolean.
3543 ;;; The `color' Widget.
3546 (define-widget 'color 'editable-field
3547 "Choose a color name (with sample)."
3548 :format "%t: %v (%{sample%})\n"
3552 :complete 'widget-color-complete
3553 :sample-face-get 'widget-color-sample-face-get
3554 :notify 'widget-color-notify
3555 :action 'widget-color-action)
3557 (defun widget-color-complete (widget)
3558 "Complete the color in WIDGET."
3559 (require 'facemenu) ; for facemenu-color-alist
3560 (let* ((prefix (buffer-substring-no-properties (widget-field-start widget)
3562 (list (or facemenu-color-alist (defined-colors)))
3563 (completion (try-completion prefix list)))
3564 (cond ((eq completion t)
3565 (message "Exact match."))
3567 (error "Can't find completion for \"%s\"" prefix))
3568 ((not (string-equal prefix completion))
3569 (insert-and-inherit (substring completion (length prefix))))
3571 (message "Making completion list...")
3572 (with-output-to-temp-buffer "*Completions*"
3573 (display-completion-list (all-completions prefix list nil)))
3574 (message "Making completion list...done")))))
3576 (defun widget-color-sample-face-get (widget)
3577 (let* ((value (condition-case nil
3578 (widget-value widget)
3579 (error (widget-get widget :value)))))
3580 (if (color-defined-p value)
3581 (list (cons 'foreground-color value))
3584 (defun widget-color-action (widget &optional event)
3585 "Prompt for a color."
3586 (let* ((tag (widget-apply widget :menu-tag-get))
3587 (prompt (concat tag ": "))
3588 (value (widget-value widget))
3589 (start (widget-field-start widget))
3590 (answer (facemenu-read-color prompt)))
3591 (unless (zerop (length answer))
3592 (widget-value-set widget answer)
3594 (widget-apply widget :notify widget event))))
3596 (defun widget-color-notify (widget child &optional event)
3597 "Update the sample, and notify the parent."
3598 (overlay-put (widget-get widget :sample-overlay)
3599 'face (widget-apply widget :sample-face-get))
3600 (widget-default-notify widget child event))
3604 (defun widget-echo-help (pos)
3605 "Display help-echo text for widget at POS."
3606 (let* ((widget (widget-at pos))
3607 (help-echo (and widget (widget-get widget :help-echo))))
3608 (if (functionp help-echo)
3609 (setq help-echo (funcall help-echo widget)))
3610 (if help-echo (message "%s" (eval help-echo)))))
3616 ;;; arch-tag: a076e75e-18a1-4b46-8be5-3f317bcbc707
3617 ;;; wid-edit.el ends here