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., 59 Temple Place - Suite 330,
24 ;; Boston, MA 02111-1307, USA.
26 ;;; Wishlist items (from widget.texi):
28 ;; * The `menu-choice' tag should be prettier, something like the
29 ;; abbreviated menus in Open Look.
31 ;; * Finish `:tab-order'.
33 ;; * Make indentation work with glyphs and proportional fonts.
35 ;; * Add commands to show overview of object and class hierarchies to
38 ;; * Find a way to disable mouse highlight for inactive widgets.
40 ;; * Find a way to make glyphs look inactive.
42 ;; * Add `key-binding' widget.
44 ;; * Add `widget' widget for editing widget specifications.
46 ;; * Find clean way to implement variable length list. See
47 ;; `TeX-printer-list' for an explanation.
49 ;; * `C-h' in `widget-prompt-value' should give type specific help.
51 ;; * A mailto widget. [This should work OK as a url-link if with
52 ;; browse-url-browser-function' set up appropriately.]
62 (defun widget-event-point (event)
63 "Character position of the end of event if that exists, or nil."
64 (posn-point (event-end event
)))
66 (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-face
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-face
'((((class color
)
98 (:foreground
"lime green"))
101 (:foreground
"dark green"))
103 "Face used for documentation text."
104 :group
'widget-documentation
105 :group
'widget-faces
)
107 (defvar widget-button-face
'widget-button-face
108 "Face used for buttons in widgets.
109 This exists as a variable so it can be set locally in certain buffers.")
111 (defface widget-button-face
'((t (:weight bold
)))
112 "Face used for widget buttons."
113 :group
'widget-faces
)
115 (defcustom widget-mouse-face
'highlight
116 "Face used for widget buttons when the mouse is above them."
118 :group
'widget-faces
)
120 ;; TTY gets special definitions here and in the next defface, because
121 ;; the gray colors defined for other displays cause black text on a black
122 ;; background, at least on light-background TTYs.
123 (defface widget-field-face
'((((type tty
))
124 :background
"yellow3"
126 (((class grayscale color
)
128 :background
"gray85")
129 (((class grayscale color
)
131 :background
"dim gray")
134 "Face used for editable fields."
135 :group
'widget-faces
)
137 (defface widget-single-line-field-face
'((((type tty
))
140 (((class grayscale color
)
142 :background
"gray85")
143 (((class grayscale color
)
145 :background
"dim gray")
148 "Face used for editable fields spanning only a single line."
149 :group
'widget-faces
)
151 ;;; This causes display-table to be loaded, and not usefully.
152 ;;;(defvar widget-single-line-display-table
153 ;;; (let ((table (make-display-table)))
154 ;;; (aset table 9 "^I")
155 ;;; (aset table 10 "^J")
157 ;;; "Display table used for single-line editable fields.")
159 ;;;(when (fboundp 'set-face-display-table)
160 ;;; (set-face-display-table 'widget-single-line-field-face
161 ;;; widget-single-line-display-table))
163 ;;; Utility functions.
165 ;; These are not really widget specific.
167 (defun widget-princ-to-string (object)
168 "Return string representation of OBJECT, any Lisp object.
169 No quoting characters are used; no delimiters are printed around
170 the contents of strings."
171 (with-output-to-string
174 (defun widget-clear-undo ()
175 "Clear all undo information."
176 (buffer-disable-undo (current-buffer))
177 (buffer-enable-undo))
179 (defcustom widget-menu-max-size
40
180 "Largest number of items allowed in a popup-menu.
181 Larger menus are read through the minibuffer."
185 (defcustom widget-menu-max-shortcuts
40
186 "Largest number of items for which it works to choose one with a character.
187 For a larger number of items, the minibuffer is used."
191 (defcustom widget-menu-minibuffer-flag nil
192 "*Control how to ask for a choice from the keyboard.
193 Non-nil means use the minibuffer;
194 nil means read a single character."
198 (defun widget-choose (title items
&optional event
)
199 "Choose an item from a list.
201 First argument TITLE is the name of the list.
202 Second argument ITEMS is a list whose members are either
203 (NAME . VALUE), to indicate selectable items, or just strings to
204 indicate unselectable items.
205 Optional third argument EVENT is an input event.
207 The user is asked to choose between each NAME from the items alist,
208 and the VALUE of the chosen element will be returned. If EVENT is a
209 mouse event, and the number of elements in items is less than
210 `widget-menu-max-size', a popup menu will be used, otherwise the
212 (cond ((and (< (length items
) widget-menu-max-size
)
213 event
(display-popup-menus-p))
216 (list title
(cons "" items
))))
217 ((or widget-menu-minibuffer-flag
218 (> (length items
) widget-menu-max-shortcuts
))
219 ;; Read the choice of name from the minibuffer.
220 (setq items
(widget-remove-if 'stringp items
))
221 (let ((val (completing-read (concat title
": ") items nil t
)))
223 (let ((try (try-completion val items
)))
226 (cdr (assoc val items
))))))
228 ;; Construct a menu of the choices
229 ;; and then use it for prompting for a single character.
230 (let* ((overriding-terminal-local-map (make-sparse-keymap))
232 map choice some-choice-enabled value
)
233 ;; Define SPC as a prefix char to get to this menu.
234 (define-key overriding-terminal-local-map
" "
235 (setq map
(make-sparse-keymap title
)))
236 (with-current-buffer (get-buffer-create " widget-choose")
238 (insert "Available choices:\n\n")
240 (setq choice
(car items
) items
(cdr items
))
242 (let* ((name (car choice
))
243 (function (cdr choice
)))
244 (insert (format "%c = %s\n" next-digit name
))
245 (define-key map
(vector next-digit
) function
)
246 (setq some-choice-enabled t
)))
247 ;; Allocate digits to disabled alternatives
248 ;; so that the digit of a given alternative never varies.
249 (setq next-digit
(1+ next-digit
)))
250 (insert "\nC-g = Quit"))
251 (or some-choice-enabled
252 (error "None of the choices is currently meaningful"))
253 (define-key map
[?\C-g
] 'keyboard-quit
)
254 (define-key map
[t] 'keyboard-quit)
255 (define-key map [?\M-\C-v] 'scroll-other-window)
256 (define-key map [?\M--] 'negative-argument)
257 (setcdr map (nreverse (cdr map)))
258 ;; Read a char with the menu, and return the result
259 ;; that corresponds to it.
260 (save-window-excursion
261 (let ((buf (get-buffer " widget-choose")))
262 (fit-window-to-buffer (display-buffer buf))
263 (let ((cursor-in-echo-area t)
267 (while (not (or (and (>= char ?0) (< char next-digit))
268 (eq value 'keyboard-quit)))
269 ;; Unread a SPC to lead to our new menu.
270 (setq unread-command-events (cons ?\ unread-command-events))
271 (setq keys (read-key-sequence title))
273 (lookup-key overriding-terminal-local-map keys t)
274 char (string-to-char (substring keys 1)))
275 (cond ((eq value 'scroll-other-window)
276 (let ((minibuffer-scroll-window
277 (get-buffer-window buf)))
279 (scroll-other-window-down
280 (window-height minibuffer-scroll-window))
281 (scroll-other-window))
283 ((eq value 'negative-argument)
287 (when (eq value 'keyboard-quit)
291 (defun widget-remove-if (predictate list)
292 (let (result (tail list))
294 (or (funcall predictate (car tail))
295 (setq result (cons (car tail) result)))
296 (setq tail (cdr tail)))
299 ;;; Widget text specifications.
301 ;; These functions are for specifying text properties.
303 ;; We can set it to nil now that get_local_map uses get_pos_property.
304 (defconst widget-field-add-space nil
305 "Non-nil means add extra space at the end of editable text fields.
306 If you don't add the space, it will become impossible to edit a zero
309 (defvar widget-field-use-before-change t
310 "Non-nil means use `before-change-functions' to track editable fields.
311 This enables the use of undo, but doesn't work on Emacs 19.34 and earlier.
312 Using before hooks also means that the :notify function can't know the
315 (defun widget-specify-field (widget from to)
316 "Specify editable button for WIDGET between FROM and TO."
317 ;; Terminating space is not part of the field, but necessary in
318 ;; order for local-map to work. Remove next sexp if local-map works
319 ;; at the end of the overlay.
322 (cond ((null (widget-get widget :size))
324 (widget-field-add-space
325 (insert-and-inherit " ")))
327 (let ((keymap (widget-get widget :keymap))
328 (face (or (widget-get widget :value-face) 'widget-field-face))
329 (help-echo (widget-get widget :help-echo))
330 (follow-link (widget-get widget :follow-link))
332 (or (not widget-field-add-space) (widget-get widget :size))))
333 (if (functionp help-echo)
334 (setq help-echo 'widget-mouse-help))
335 (when (= (char-before to) ?\n)
336 ;; When the last character in the field is a newline, we want to
337 ;; give it a `field' char-property of `boundary', which helps the
338 ;; C-n/C-p act more naturally when entering/leaving the field. We
339 ;; do this by making a small secondary overlay to contain just that
341 (let ((overlay (make-overlay (1- to) to nil t nil)))
342 (overlay-put overlay 'field 'boundary)
343 ;; We need the real field for tabbing.
344 (overlay-put overlay 'real-field widget)
345 ;; Use `local-map' here, not `keymap', so that normal editing
346 ;; works in the field when, say, Custom uses `suppress-keymap'.
347 (overlay-put overlay 'local-map keymap)
348 (overlay-put overlay 'face face)
349 (overlay-put overlay 'follow-link follow-link)
350 (overlay-put overlay 'help-echo help-echo))
352 (setq rear-sticky t))
353 (let ((overlay (make-overlay from to nil nil rear-sticky)))
354 (widget-put widget :field-overlay overlay)
355 ;;(overlay-put overlay 'detachable nil)
356 (overlay-put overlay 'field widget)
357 (overlay-put overlay 'local-map keymap)
358 (overlay-put overlay 'face face)
359 (overlay-put overlay 'follow-link follow-link)
360 (overlay-put overlay 'help-echo help-echo)))
361 (widget-specify-secret widget))
363 (defun widget-specify-secret (field)
364 "Replace text in FIELD with value of `:secret', if non-nil."
365 (let ((secret (widget-get field :secret))
366 (size (widget-get field :size)))
368 (let ((begin (widget-field-start field))
369 (end (widget-field-end field)))
371 (while (and (> end begin)
372 (eq (char-after (1- end)) ?\ ))
373 (setq end (1- end))))
375 (let ((old (char-after begin)))
376 (unless (eq old secret)
377 (subst-char-in-region begin (1+ begin) old secret)
378 (put-text-property begin (1+ begin) 'secret old))
379 (setq begin (1+ begin))))))))
381 (defun widget-specify-button (widget from to)
382 "Specify button for WIDGET between FROM and TO."
383 (let ((overlay (make-overlay from to nil t nil))
384 (follow-link (widget-get widget :follow-link))
385 (help-echo (widget-get widget :help-echo)))
386 (widget-put widget :button-overlay overlay)
387 (if (functionp help-echo)
388 (setq help-echo 'widget-mouse-help))
389 (overlay-put overlay 'button widget)
390 (overlay-put overlay 'keymap (widget-get widget :keymap))
391 (overlay-put overlay 'evaporate t)
392 ;; We want to avoid the face with image buttons.
393 (unless (widget-get widget :suppress-face)
394 (overlay-put overlay 'face (widget-apply widget :button-face-get))
395 ; Text terminals cannot change mouse pointer shape, so use mouse
397 (or (display-graphic-p)
398 (overlay-put overlay 'mouse-face widget-mouse-face)))
399 (overlay-put overlay 'pointer 'hand)
400 (overlay-put overlay 'follow-link follow-link)
401 (overlay-put overlay 'help-echo help-echo)))
403 (defun widget-mouse-help (window overlay point)
404 "Help-echo callback for widgets whose :help-echo is a function."
405 (with-current-buffer (overlay-buffer overlay)
406 (let* ((widget (widget-at (overlay-start overlay)))
407 (help-echo (if widget (widget-get widget :help-echo))))
408 (if (functionp help-echo)
409 (funcall help-echo widget)
412 (defun widget-specify-sample (widget from to)
413 "Specify sample for WIDGET between FROM and TO."
414 (let ((overlay (make-overlay from to nil t nil)))
415 (overlay-put overlay 'face (widget-apply widget :sample-face-get))
416 (overlay-put overlay 'evaporate t)
417 (widget-put widget :sample-overlay overlay)))
419 (defun widget-specify-doc (widget from to)
420 "Specify documentation for WIDGET between FROM and TO."
421 (let ((overlay (make-overlay from to nil t nil)))
422 (overlay-put overlay 'widget-doc widget)
423 (overlay-put overlay 'face widget-documentation-face)
424 (overlay-put overlay 'evaporate t)
425 (widget-put widget :doc-overlay overlay)))
427 (defmacro widget-specify-insert (&rest form)
428 "Execute FORM without inheriting any text properties."
430 (let ((inhibit-read-only t)
431 (inhibit-modification-hooks t))
432 (narrow-to-region (point) (point))
433 (prog1 (progn ,@form)
434 (goto-char (point-max))))))
436 (defface widget-inactive-face '((((class grayscale color)
438 (:foreground "light gray"))
439 (((class grayscale color)
441 (:foreground "dim gray"))
444 "Face used for inactive widgets."
445 :group 'widget-faces)
447 (defun widget-specify-inactive (widget from to)
448 "Make WIDGET inactive for user modifications."
449 (unless (widget-get widget :inactive)
450 (let ((overlay (make-overlay from to nil t nil)))
451 (overlay-put overlay 'face 'widget-inactive-face)
452 ;; This is disabled, as it makes the mouse cursor change shape.
453 ;; (overlay-put overlay 'mouse-face 'widget-inactive-face)
454 (overlay-put overlay 'evaporate t)
455 (overlay-put overlay 'priority 100)
456 (overlay-put overlay 'modification-hooks '(widget-overlay-inactive))
457 (widget-put widget :inactive overlay))))
459 (defun widget-overlay-inactive (&rest junk)
460 "Ignoring the arguments, signal an error."
461 (unless inhibit-read-only
462 (error "The widget here is not active")))
465 (defun widget-specify-active (widget)
466 "Make WIDGET active for user modifications."
467 (let ((inactive (widget-get widget :inactive)))
469 (delete-overlay inactive)
470 (widget-put widget :inactive nil))))
472 ;;; Widget Properties.
474 (defsubst widget-type (widget)
475 "Return the type of WIDGET, a symbol."
479 (defun widgetp (widget)
480 "Return non-nil iff WIDGET is a widget."
482 (get widget 'widget-type)
484 (symbolp (car widget))
485 (get (car widget) 'widget-type))))
487 (defun widget-get-indirect (widget property)
488 "In WIDGET, get the value of PROPERTY.
489 If the value is a symbol, return its binding.
490 Otherwise, just return the value."
491 (let ((value (widget-get widget property)))
496 (defun widget-member (widget property)
497 "Non-nil iff there is a definition in WIDGET for PROPERTY."
498 (cond ((plist-member (cdr widget) property)
501 (widget-member (get (car widget) 'widget-type) property))
504 (defun widget-value (widget)
505 "Extract the current value of WIDGET."
507 :value-to-external (widget-apply widget :value-get)))
509 (defun widget-value-set (widget value)
510 "Set the current value of WIDGET to VALUE."
512 :value-set (widget-apply widget
513 :value-to-internal value)))
515 (defun widget-default-get (widget)
516 "Extract the default external value of WIDGET."
517 (widget-apply widget :value-to-external
518 (or (widget-get widget :value)
519 (widget-apply widget :default-get))))
521 (defun widget-match-inline (widget vals)
522 "In WIDGET, match the start of VALS."
523 (cond ((widget-get widget :inline)
524 (widget-apply widget :match-inline vals))
526 (widget-apply widget :match (car vals)))
527 (cons (list (car vals)) (cdr vals)))
530 (defun widget-apply-action (widget &optional event)
531 "Apply :action in WIDGET in response to EVENT."
532 (if (widget-apply widget :active)
533 (widget-apply widget :action event)
534 (error "Attempt to perform action on inactive widget")))
536 ;;; Helper functions.
538 ;; These are widget specific.
541 (defun widget-prompt-value (widget prompt &optional value unbound)
542 "Prompt for a value matching WIDGET, using PROMPT.
543 The current value is assumed to be VALUE, unless UNBOUND is non-nil."
544 (unless (listp widget)
545 (setq widget (list widget)))
546 (setq prompt (format "[%s] %s" (widget-type widget) prompt))
547 (setq widget (widget-convert widget))
548 (let ((answer (widget-apply widget :prompt-value prompt value unbound)))
549 (unless (widget-apply widget :match answer)
550 (error "Value does not match %S type" (car widget)))
553 (defun widget-get-sibling (widget)
554 "Get the item WIDGET is assumed to toggle.
555 This is only meaningful for radio buttons or checkboxes in a list."
556 (let* ((children (widget-get (widget-get widget :parent) :children))
560 (setq child (car children)
561 children (cdr children))
562 (when (eq (widget-get child :button) widget)
563 (throw 'child child)))
566 (defun widget-map-buttons (function &optional buffer maparg)
567 "Map FUNCTION over the buttons in BUFFER.
568 FUNCTION is called with the arguments WIDGET and MAPARG.
570 If FUNCTION returns non-nil, the walk is cancelled.
572 The arguments MAPARG, and BUFFER default to nil and (current-buffer),
574 (let ((cur (point-min))
577 (with-current-buffer buffer (overlay-lists))
579 (setq overlays (append (car overlays) (cdr overlays)))
580 (while (setq cur (pop overlays))
581 (setq widget (overlay-get cur 'button))
582 (if (and widget (funcall function widget maparg))
583 (setq overlays nil)))))
587 (defcustom widget-image-directory (file-name-as-directory
588 (expand-file-name "custom" data-directory))
589 "Where widget button images are located.
590 If this variable is nil, widget will try to locate the directory
595 (defcustom widget-image-enable t
596 "If non nil, use image buttons in widgets when available."
601 (defcustom widget-image-conversion
602 '((xpm ".xpm") (gif ".gif") (png ".png") (jpeg ".jpg" ".jpeg")
604 "Conversion alist from image formats to file name suffixes."
606 :type '(repeat (cons :format "%v"
607 (symbol :tag "Image Format" unknown)
608 (repeat :tag "Suffixes"
609 (string :format "%v")))))
611 (defun widget-image-find (image)
612 "Create a graphical button from IMAGE.
613 IMAGE should either already be an image, or be a file name sans
614 extension (xpm, xbm, gif, jpg, or png) located in
615 `widget-image-directory' or otherwise where `find-image' will find it."
616 (cond ((not (and image widget-image-enable (display-graphic-p)))
617 ;; We don't want or can't use images.
620 (eq 'image (car image)))
621 ;; Already an image spec. Use it.
624 ;; A string. Look it up in relevant directories.
625 (let* ((load-path (cons widget-image-directory load-path))
627 (dolist (elt widget-image-conversion)
628 (dolist (ext (cdr elt))
629 (push (list :type (car elt) :file (concat image ext)) specs)))
630 (setq specs (nreverse specs))
636 (defvar widget-button-pressed-face 'widget-button-pressed-face
637 "Face used for pressed buttons in widgets.
638 This exists as a variable so it can be set locally in certain
641 (defun widget-image-insert (widget tag image &optional down inactive)
642 "In WIDGET, insert the text TAG or, if supported, IMAGE.
643 IMAGE should either be an image or an image file name sans extension
644 \(xpm, xbm, gif, jpg, or png) located in `widget-image-directory'.
646 Optional arguments DOWN and INACTIVE are used instead of IMAGE when the
647 button is pressed or inactive, respectively. These are currently ignored."
648 (if (and (display-graphic-p)
649 (setq image (widget-image-find image)))
650 (progn (widget-put widget :suppress-face t)
653 tag 'mouse-face widget-button-pressed-face)))
658 (defgroup widget-button nil
659 "The look of various kinds of buttons."
662 (defcustom widget-button-prefix ""
663 "String used as prefix for buttons."
665 :group 'widget-button)
667 (defcustom widget-button-suffix ""
668 "String used as suffix for buttons."
670 :group 'widget-button)
672 ;;; Creating Widgets.
675 (defun widget-create (type &rest args)
676 "Create widget of TYPE.
677 The optional ARGS are additional keyword arguments."
678 (let ((widget (apply 'widget-convert type args)))
679 (widget-apply widget :create)
682 (defun widget-create-child-and-convert (parent type &rest args)
683 "As part of the widget PARENT, create a child widget TYPE.
684 The child is converted, using the keyword arguments ARGS."
685 (let ((widget (apply 'widget-convert type args)))
686 (widget-put widget :parent parent)
687 (unless (widget-get widget :indent)
688 (widget-put widget :indent (+ (or (widget-get parent :indent) 0)
689 (or (widget-get widget :extra-offset) 0)
690 (widget-get parent :offset))))
691 (widget-apply widget :create)
694 (defun widget-create-child (parent type)
695 "Create widget of TYPE."
696 (let ((widget (widget-copy type)))
697 (widget-put widget :parent parent)
698 (unless (widget-get widget :indent)
699 (widget-put widget :indent (+ (or (widget-get parent :indent) 0)
700 (or (widget-get widget :extra-offset) 0)
701 (widget-get parent :offset))))
702 (widget-apply widget :create)
705 (defun widget-create-child-value (parent type value)
706 "Create widget of TYPE with value VALUE."
707 (let ((widget (widget-copy type)))
708 (widget-put widget :value (widget-apply widget :value-to-internal value))
709 (widget-put widget :parent parent)
710 (unless (widget-get widget :indent)
711 (widget-put widget :indent (+ (or (widget-get parent :indent) 0)
712 (or (widget-get widget :extra-offset) 0)
713 (widget-get parent :offset))))
714 (widget-apply widget :create)
718 (defun widget-delete (widget)
720 (widget-apply widget :delete))
722 (defun widget-copy (widget)
723 "Make a deep copy of WIDGET."
724 (widget-apply (copy-sequence widget) :copy))
726 (defun widget-convert (type &rest args)
727 "Convert TYPE to a widget without inserting it in the buffer.
728 The optional ARGS are additional keyword arguments."
729 ;; Don't touch the type.
730 (let* ((widget (if (symbolp type)
732 (copy-sequence type)))
736 ;; First set the :args keyword.
737 (while (cdr current) ;Look in the type.
738 (if (and (keywordp (cadr current))
739 ;; If the last element is a keyword,
740 ;; it is still the :args element,
741 ;; even though it is a keyword.
743 (if (eq (cadr current) :args)
744 ;; If :args is explicitly specified, obey it.
746 ;; Some other irrelevant keyword.
747 (setq current (cdr (cdr current))))
748 (setcdr current (list :args (cdr current)))
750 (while (and args (not done)) ;Look in ARGS.
751 (cond ((eq (car args) :args)
752 ;; Handle explicit specification of :args.
753 (setq args (cadr args)
755 ((keywordp (car args))
756 (setq args (cddr args)))
759 (widget-put widget :args args))
760 ;; Then Convert the widget.
763 (let ((convert-widget (plist-get (cdr type) :convert-widget)))
765 (setq widget (funcall convert-widget widget))))
766 (setq type (get (car type) 'widget-type)))
767 ;; Finally set the keyword args.
769 (let ((next (nth 0 keys)))
772 (widget-put widget next (nth 1 keys))
773 (setq keys (nthcdr 2 keys)))
775 ;; Convert the :value to internal format.
776 (if (widget-member widget :value)
778 :value (widget-apply widget
780 (widget-get widget :value))))
781 ;; Return the newly create widget.
785 (defun widget-insert (&rest args)
786 "Call `insert' with ARGS even if surrounding text is read only."
787 (let ((inhibit-read-only t)
788 (inhibit-modification-hooks t))
789 (apply 'insert args)))
791 (defun widget-convert-text (type from to
792 &optional button-from button-to
794 "Return a widget of type TYPE with endpoint FROM TO.
795 Optional ARGS are extra keyword arguments for TYPE.
796 and TO will be used as the widgets end points. If optional arguments
797 BUTTON-FROM and BUTTON-TO are given, these will be used as the widgets
799 Optional ARGS are extra keyword arguments for TYPE."
800 (let ((widget (apply 'widget-convert type :delete 'widget-leave-text args))
801 (from (copy-marker from))
802 (to (copy-marker to)))
803 (set-marker-insertion-type from t)
804 (set-marker-insertion-type to nil)
805 (widget-put widget :from from)
806 (widget-put widget :to to)
808 (widget-specify-button widget button-from button-to))
811 (defun widget-convert-button (type from to &rest args)
812 "Return a widget of type TYPE with endpoint FROM TO.
813 Optional ARGS are extra keyword arguments for TYPE.
814 No text will be inserted to the buffer, instead the text between FROM
815 and TO will be used as the widgets end points, as well as the widgets
817 (apply 'widget-convert-text type from to from to args))
819 (defun widget-leave-text (widget)
820 "Remove markers and overlays from WIDGET and its children."
821 (let ((button (widget-get widget :button-overlay))
822 (sample (widget-get widget :sample-overlay))
823 (doc (widget-get widget :doc-overlay))
824 (field (widget-get widget :field-overlay)))
825 (set-marker (widget-get widget :from) nil)
826 (set-marker (widget-get widget :to) nil)
828 (delete-overlay button))
830 (delete-overlay sample))
832 (delete-overlay doc))
834 (delete-overlay field))
835 (mapc 'widget-leave-text (widget-get widget :children))))
837 ;;; Keymap and Commands.
840 (defvar widget-keymap
841 (let ((map (make-sparse-keymap)))
842 (define-key map "\t" 'widget-forward)
843 (define-key map [(shift tab)] 'widget-backward)
844 (define-key map [backtab] 'widget-backward)
845 (define-key map [down-mouse-2] 'widget-button-click)
846 (define-key map "\C-m" 'widget-button-press)
848 "Keymap containing useful binding for buffers containing widgets.
849 Recommended as a parent keymap for modes using widgets.")
851 (defvar widget-global-map global-map
852 "Keymap used for events a widget does not handle itself.")
853 (make-variable-buffer-local 'widget-global-map)
855 (defvar widget-field-keymap
856 (let ((map (copy-keymap widget-keymap)))
857 (define-key map "\C-k" 'widget-kill-line)
858 (define-key map "\M-\t" 'widget-complete)
859 (define-key map "\C-m" 'widget-field-activate)
860 ;; Since the widget code uses a `field' property to identify fields,
861 ;; ordinary beginning-of-line does the right thing.
862 ;; (define-key map "\C-a" 'widget-beginning-of-line)
863 (define-key map "\C-e" 'widget-end-of-line)
865 "Keymap used inside an editable field.")
867 (defvar widget-text-keymap
868 (let ((map (copy-keymap widget-keymap)))
869 ;; Since the widget code uses a `field' property to identify fields,
870 ;; ordinary beginning-of-line does the right thing.
871 ;; (define-key map "\C-a" 'widget-beginning-of-line)
872 (define-key map "\C-e" 'widget-end-of-line)
874 "Keymap used inside a text field.")
876 (defun widget-field-activate (pos &optional event)
877 "Invoke the editable field at point."
879 (let ((field (widget-field-at pos)))
881 (widget-apply-action field event)
883 (lookup-key widget-global-map (this-command-keys))))))
885 (defface widget-button-pressed-face
886 '((((min-colors 88) (class color))
887 (:foreground "red1"))
891 (:weight bold :underline t)))
892 "Face used for pressed buttons."
893 :group 'widget-faces)
895 (defun widget-button-click (event)
896 "Invoke the button that the mouse is pointing at."
898 (if (widget-event-point event)
899 (let* ((pos (widget-event-point event))
900 (start (event-start event))
901 (button (get-char-property
902 pos 'button (and (windowp (posn-window start))
903 (window-buffer (posn-window start))))))
905 ;; Mouse click on a widget button. Do the following
906 ;; in a save-excursion so that the click on the button
907 ;; doesn't change point.
908 (save-selected-window
909 (select-window (posn-window (event-start event)))
911 (goto-char (posn-point (event-start event)))
912 (let* ((overlay (widget-get button :button-overlay))
913 (face (overlay-get overlay 'face))
914 (mouse-face (overlay-get overlay 'mouse-face)))
916 ;; Read events, including mouse-movement events
917 ;; until we receive a release event. Highlight/
918 ;; unhighlight the button the mouse was initially
919 ;; on when we move over it.
921 (when face ; avoid changing around image
923 'face widget-button-pressed-face)
925 'mouse-face widget-button-pressed-face))
926 (unless (widget-apply button :mouse-down-action event)
927 (let ((track-mouse t))
928 (while (not (widget-button-release-event-p event))
929 (setq event (read-event)
930 pos (widget-event-point event))
932 (eq (get-char-property pos 'button)
937 widget-button-pressed-face)
940 widget-button-pressed-face))
941 (overlay-put overlay 'face face)
942 (overlay-put overlay 'mouse-face mouse-face)))))
944 ;; When mouse is released over the button, run
945 ;; its action function.
947 (eq (get-char-property pos 'button) button))
948 (widget-apply-action button event)))
949 (overlay-put overlay 'face face)
950 (overlay-put overlay 'mouse-face mouse-face))))
952 (unless (pos-visible-in-window-p (widget-event-point event))
953 (mouse-set-point event)
958 (let ((up t) command)
959 ;; Mouse click not on a widget button. Find the global
960 ;; command to run, and check whether it is bound to an
962 (mouse-set-point event)
963 (if (memq (event-basic-type event) '(mouse-1 down-mouse-1))
964 (cond ((setq command ;down event
965 (lookup-key widget-global-map [down-mouse-1]))
967 ((setq command ;up event
968 (lookup-key widget-global-map [mouse-1]))))
969 (cond ((setq command ;down event
970 (lookup-key widget-global-map [down-mouse-2]))
972 ((setq command ;up event
973 (lookup-key widget-global-map [mouse-2])))))
975 ;; Don't execute up events twice.
976 (while (not (widget-button-release-event-p event))
977 (setq event (read-event))))
979 (call-interactively command)))))
980 (message "You clicked somewhere weird.")))
982 (defun widget-button-press (pos &optional event)
983 "Invoke button at POS."
985 (let ((button (get-char-property pos 'button)))
987 (widget-apply-action button event)
988 (let ((command (lookup-key widget-global-map (this-command-keys))))
989 (when (commandp command)
990 (call-interactively command))))))
992 (defun widget-tabable-at (&optional pos)
993 "Return the tabable widget at POS, or nil.
994 POS defaults to the value of (point)."
995 (let ((widget (widget-at pos)))
997 (let ((order (widget-get widget :tab-order)))
1003 (defvar widget-use-overlay-change t
1004 "If non-nil, use overlay change functions to tab around in the buffer.
1005 This is much faster, but doesn't work reliably on Emacs 19.34.")
1007 (defun widget-move (arg)
1008 "Move point to the ARG next field or button.
1009 ARG may be negative to move backward."
1010 (or (bobp) (> arg 0) (backward-char))
1013 (old (widget-tabable-at)))
1017 (goto-char (point-min))
1018 (setq wrapped (1+ wrapped)))
1019 (widget-use-overlay-change
1020 (goto-char (next-overlay-change (point))))
1025 (error "No buttons or fields found"))
1026 (let ((new (widget-tabable-at)))
1028 (unless (eq new old)
1034 (goto-char (point-max))
1035 (setq wrapped (1+ wrapped)))
1036 (widget-use-overlay-change
1037 (goto-char (previous-overlay-change (point))))
1042 (error "No buttons or fields found"))
1043 (let ((new (widget-tabable-at)))
1045 (unless (eq new old)
1046 (setq arg (1+ arg))))))
1047 (let ((new (widget-tabable-at)))
1048 (while (eq (widget-tabable-at) new)
1051 (widget-echo-help (point))
1052 (run-hooks 'widget-move-hook))
1054 (defun widget-forward (arg)
1055 "Move point to the next field or button.
1056 With optional ARG, move across that many fields."
1058 (run-hooks 'widget-forward-hook)
1061 (defun widget-backward (arg)
1062 "Move point to the previous field or button.
1063 With optional ARG, move across that many fields."
1065 (run-hooks 'widget-backward-hook)
1066 (widget-move (- arg)))
1068 ;; Since the widget code uses a `field' property to identify fields,
1069 ;; ordinary beginning-of-line does the right thing.
1070 (defalias 'widget-beginning-of-line 'beginning-of-line)
1072 (defun widget-end-of-line ()
1073 "Go to end of field or end of line, whichever is first.
1074 Trailing spaces at the end of padded fields are not considered part of
1077 ;; Ordinary end-of-line does the right thing, because we're inside
1078 ;; text with a `field' property.
1081 ;; ... except that we want to ignore trailing spaces in fields that
1082 ;; aren't terminated by a newline, because they are used as padding,
1083 ;; and ignored when extracting the entered value of the field.
1084 (skip-chars-backward " " (field-beginning (1- (point))))))
1086 (defun widget-kill-line ()
1087 "Kill to end of field or end of line, whichever is first."
1089 (let* ((field (widget-field-find (point)))
1090 (end (and field (widget-field-end field))))
1091 (if (and field (> (line-beginning-position 2) end))
1092 (kill-region (point) end)
1093 (call-interactively 'kill-line))))
1095 (defcustom widget-complete-field (lookup-key global-map "\M-\t")
1096 "Default function to call for completion inside fields."
1097 :options '(ispell-complete-word complete-tag lisp-complete-symbol)
1101 (defun widget-narrow-to-field ()
1104 (let ((field (widget-field-find (point))))
1106 (narrow-to-region (line-beginning-position) (line-end-position)))))
1108 (defun widget-complete ()
1109 "Complete content of editable field from point.
1110 When not inside a field, move to the previous button or field."
1112 (let ((field (widget-field-find (point))))
1115 (widget-narrow-to-field)
1116 (widget-apply field :complete))
1117 (error "Not in an editable field"))))
1119 ;;; Setting up the buffer.
1121 (defvar widget-field-new nil
1122 "List of all newly created editable fields in the buffer.")
1123 (make-variable-buffer-local 'widget-field-new)
1125 (defvar widget-field-list nil
1126 "List of all editable fields in the buffer.")
1127 (make-variable-buffer-local 'widget-field-list)
1129 (defun widget-at (&optional pos)
1130 "The button or field at POS (default, point)."
1131 (or (get-char-property (or pos (point)) 'button)
1132 (widget-field-at pos)))
1135 (defun widget-setup ()
1136 "Setup current buffer so editing string widgets works."
1137 (let ((inhibit-read-only t)
1138 (inhibit-modification-hooks t)
1140 (while widget-field-new
1141 (setq field (car widget-field-new)
1142 widget-field-new (cdr widget-field-new)
1143 widget-field-list (cons field widget-field-list))
1144 (let ((from (car (widget-get field :field-overlay)))
1145 (to (cdr (widget-get field :field-overlay))))
1146 (widget-specify-field field
1147 (marker-position from) (marker-position to))
1148 (set-marker from nil)
1149 (set-marker to nil))))
1151 (widget-add-change))
1153 (defvar widget-field-last nil)
1154 ;; Last field containing point.
1155 (make-variable-buffer-local 'widget-field-last)
1157 (defvar widget-field-was nil)
1158 ;; The widget data before the change.
1159 (make-variable-buffer-local 'widget-field-was)
1161 (defun widget-field-at (pos)
1162 "Return the widget field at POS, or nil if none."
1163 (let ((field (get-char-property (or pos (point)) 'field)))
1164 (if (eq field 'boundary)
1165 (get-char-property (or pos (point)) 'real-field)
1168 (defun widget-field-buffer (widget)
1169 "Return the buffer of WIDGET's editing field."
1170 (let ((overlay (widget-get widget :field-overlay)))
1171 (cond ((overlayp overlay)
1172 (overlay-buffer overlay))
1174 (marker-buffer (car overlay))))))
1176 (defun widget-field-start (widget)
1177 "Return the start of WIDGET's editing field."
1178 (let ((overlay (widget-get widget :field-overlay)))
1179 (if (overlayp overlay)
1180 (overlay-start overlay)
1183 (defun widget-field-end (widget)
1184 "Return the end of WIDGET's editing field."
1185 (let ((overlay (widget-get widget :field-overlay)))
1186 ;; Don't subtract one if local-map works at the end of the overlay,
1187 ;; or if a special `boundary' field has been added after the widget
1189 (if (overlayp overlay)
1190 (if (and (not (eq (with-current-buffer
1191 (widget-field-buffer widget)
1193 ;; `widget-narrow-to-field' can be
1194 ;; active when this function is called
1195 ;; from an change-functions hook. So
1196 ;; temporarily remove field narrowing
1197 ;; before to call `get-char-property'.
1199 (get-char-property (overlay-end overlay)
1202 (or widget-field-add-space
1203 (null (widget-get widget :size))))
1204 (1- (overlay-end overlay))
1205 (overlay-end overlay))
1208 (defun widget-field-find (pos)
1209 "Return the field at POS.
1210 Unlike (get-char-property POS 'field) this, works with empty fields too."
1211 (let ((fields widget-field-list)
1214 (setq field (car fields)
1215 fields (cdr fields))
1216 (when (and (<= (widget-field-start field) pos)
1217 (<= pos (widget-field-end field)))
1219 (error "Overlapping fields"))
1220 (setq found field)))
1223 (defun widget-before-change (from to)
1224 ;; This is how, for example, a variable changes its state to `modified'.
1225 ;; when it is being edited.
1226 (unless inhibit-read-only
1227 (let ((from-field (widget-field-find from))
1228 (to-field (widget-field-find to)))
1229 (cond ((not (eq from-field to-field))
1230 (add-hook 'post-command-hook 'widget-add-change nil t)
1231 (signal 'text-read-only
1232 '("Change should be restricted to a single field")))
1234 (add-hook 'post-command-hook 'widget-add-change nil t)
1235 (signal 'text-read-only
1236 '("Attempt to change text outside editable field")))
1237 (widget-field-use-before-change
1238 (widget-apply from-field :notify from-field))))))
1240 (defun widget-add-change ()
1241 (remove-hook 'post-command-hook 'widget-add-change t)
1242 (add-hook 'before-change-functions 'widget-before-change nil t)
1243 (add-hook 'after-change-functions 'widget-after-change nil t))
1245 (defun widget-after-change (from to old)
1246 "Adjust field size and text properties."
1247 (let ((field (widget-field-find from))
1248 (other (widget-field-find to)))
1250 (unless (eq field other)
1251 (error "Change in different fields"))
1252 (let ((size (widget-get field :size)))
1254 (let ((begin (widget-field-start field))
1255 (end (widget-field-end field)))
1256 (cond ((< (- end begin) size)
1260 (insert-char ?\ (- (+ begin size) end))))
1261 ((> (- end begin) size)
1262 ;; Field too large and
1263 (if (or (< (point) (+ begin size))
1265 ;; Point is outside extra space.
1266 (setq begin (+ begin size))
1267 ;; Point is within the extra space.
1268 (setq begin (point)))
1271 (while (and (eq (preceding-char) ?\ )
1273 (delete-backward-char 1)))))))
1274 (widget-specify-secret field))
1275 (widget-apply field :notify field))))
1277 ;;; Widget Functions
1279 ;; These functions are used in the definition of multiple widgets.
1281 (defun widget-parent-action (widget &optional event)
1282 "Tell :parent of WIDGET to handle the :action.
1283 Optional EVENT is the event that triggered the action."
1284 (widget-apply (widget-get widget :parent) :action event))
1286 (defun widget-children-value-delete (widget)
1287 "Delete all :children and :buttons in WIDGET."
1288 (mapc 'widget-delete (widget-get widget :children))
1289 (widget-put widget :children nil)
1290 (mapc 'widget-delete (widget-get widget :buttons))
1291 (widget-put widget :buttons nil))
1293 (defun widget-children-validate (widget)
1294 "All the :children must be valid."
1295 (let ((children (widget-get widget :children))
1297 (while (and children (not found))
1298 (setq child (car children)
1299 children (cdr children)
1300 found (widget-apply child :validate)))
1303 (defun widget-child-value-get (widget)
1304 "Get the value of the first member of :children in WIDGET."
1305 (widget-value (car (widget-get widget :children))))
1307 (defun widget-child-value-inline (widget)
1308 "Get the inline value of the first member of :children in WIDGET."
1309 (widget-apply (car (widget-get widget :children)) :value-inline))
1311 (defun widget-child-validate (widget)
1312 "The result of validating the first member of :children in WIDGET."
1313 (widget-apply (car (widget-get widget :children)) :validate))
1315 (defun widget-type-value-create (widget)
1316 "Convert and instantiate the value of the :type attribute of WIDGET.
1317 Store the newly created widget in the :children attribute.
1319 The value of the :type attribute should be an unconverted widget type."
1320 (let ((value (widget-get widget :value))
1321 (type (widget-get widget :type)))
1322 (widget-put widget :children
1323 (list (widget-create-child-value widget
1324 (widget-convert type)
1327 (defun widget-type-default-get (widget)
1328 "Get default value from the :type attribute of WIDGET.
1330 The value of the :type attribute should be an unconverted widget type."
1331 (widget-default-get (widget-convert (widget-get widget :type))))
1333 (defun widget-type-match (widget value)
1334 "Non-nil if the :type value of WIDGET matches VALUE.
1336 The value of the :type attribute should be an unconverted widget type."
1337 (widget-apply (widget-convert (widget-get widget :type)) :match value))
1339 (defun widget-types-copy (widget)
1340 "Copy :args as widget types in WIDGET."
1341 (widget-put widget :args (mapcar 'widget-copy (widget-get widget :args)))
1344 ;; Made defsubst to speed up face editor creation.
1345 (defsubst widget-types-convert-widget (widget)
1346 "Convert :args as widget types in WIDGET."
1347 (widget-put widget :args (mapcar 'widget-convert (widget-get widget :args)))
1350 (defun widget-value-convert-widget (widget)
1351 "Initialize :value from :args in WIDGET."
1352 (let ((args (widget-get widget :args)))
1354 (widget-put widget :value (car args))
1355 ;; Don't convert :value here, as this is done in `widget-convert'.
1356 ;; (widget-put widget :value (widget-apply widget
1357 ;; :value-to-internal (car args)))
1358 (widget-put widget :args nil)))
1361 (defun widget-value-value-get (widget)
1362 "Return the :value property of WIDGET."
1363 (widget-get widget :value))
1365 ;;; The `default' Widget.
1367 (define-widget 'default nil
1368 "Basic widget other widgets are derived from."
1369 :value-to-internal (lambda (widget value) value)
1370 :value-to-external (lambda (widget value) value)
1371 :button-prefix 'widget-button-prefix
1372 :button-suffix 'widget-button-suffix
1373 :complete 'widget-default-complete
1374 :create 'widget-default-create
1377 :format-handler 'widget-default-format-handler
1378 :button-face-get 'widget-default-button-face-get
1379 :sample-face-get 'widget-default-sample-face-get
1380 :delete 'widget-default-delete
1382 :value-set 'widget-default-value-set
1383 :value-inline 'widget-default-value-inline
1384 :value-delete 'ignore
1385 :default-get 'widget-default-default-get
1386 :menu-tag-get 'widget-default-menu-tag-get
1388 :active 'widget-default-active
1389 :activate 'widget-specify-active
1390 :deactivate 'widget-default-deactivate
1391 :mouse-down-action #'ignore
1392 :action 'widget-default-action
1393 :notify 'widget-default-notify
1394 :prompt-value 'widget-default-prompt-value)
1396 (defun widget-default-complete (widget)
1397 "Call the value of the :complete-function property of WIDGET.
1398 If that does not exists, call the value of `widget-complete-field'."
1399 (call-interactively (or (widget-get widget :complete-function)
1400 widget-complete-field)))
1402 (defun widget-default-create (widget)
1403 "Create WIDGET at point in the current buffer."
1404 (widget-specify-insert
1405 (let ((from (point))
1406 button-begin button-end
1407 sample-begin sample-end
1410 (insert (widget-get widget :format))
1412 ;; Parse escapes in format.
1413 (while (re-search-forward "%\\(.\\)" nil t)
1414 (let ((escape (char-after (match-beginning 1))))
1415 (delete-backward-char 2)
1416 (cond ((eq escape ?%)
1419 (setq button-begin (point))
1420 (insert (widget-get-indirect widget :button-prefix)))
1422 (insert (widget-get-indirect widget :button-suffix))
1423 (setq button-end (point)))
1425 (setq sample-begin (point)))
1427 (setq sample-end (point)))
1429 (when (widget-get widget :indent)
1431 (insert-char ? (widget-get widget :indent))))
1433 (let ((image (widget-get widget :tag-glyph))
1434 (tag (widget-get widget :tag)))
1436 (widget-image-insert widget (or tag "image") image))
1440 (princ (widget-get widget :value)
1441 (current-buffer))))))
1443 (let ((doc (widget-get widget :doc)))
1445 (setq doc-begin (point))
1447 (while (eq (preceding-char) ?\n)
1448 (delete-backward-char 1))
1450 (setq doc-end (point)))))
1452 (if (and button-begin (not button-end))
1453 (widget-apply widget :value-create)
1454 (setq value-pos (point))))
1456 (widget-apply widget :format-handler escape)))))
1457 ;; Specify button, sample, and doc, and insert value.
1458 (and button-begin button-end
1459 (widget-specify-button widget button-begin button-end))
1460 (and sample-begin sample-end
1461 (widget-specify-sample widget sample-begin sample-end))
1462 (and doc-begin doc-end
1463 (widget-specify-doc widget doc-begin doc-end))
1465 (goto-char value-pos)
1466 (widget-apply widget :value-create)))
1467 (let ((from (point-min-marker))
1468 (to (point-max-marker)))
1469 (set-marker-insertion-type from t)
1470 (set-marker-insertion-type to nil)
1471 (widget-put widget :from from)
1472 (widget-put widget :to to)))
1473 (widget-clear-undo))
1475 (defun widget-default-format-handler (widget escape)
1476 ;; We recognize the %h escape by default.
1477 (let* ((buttons (widget-get widget :buttons)))
1478 (cond ((eq escape ?h)
1479 (let* ((doc-property (widget-get widget :documentation-property))
1480 (doc-try (cond ((widget-get widget :doc))
1481 ((functionp doc-property)
1482 (funcall doc-property
1483 (widget-get widget :value)))
1484 ((symbolp doc-property)
1485 (documentation-property
1486 (widget-get widget :value)
1488 (doc-text (and (stringp doc-try)
1489 (> (length doc-try) 1)
1491 (doc-indent (widget-get widget :documentation-indent)))
1493 (and (eq (preceding-char) ?\n)
1494 (widget-get widget :indent)
1495 (insert-char ? (widget-get widget :indent)))
1496 ;; The `*' in the beginning is redundant.
1497 (when (eq (aref doc-text 0) ?*)
1498 (setq doc-text (substring doc-text 1)))
1499 ;; Get rid of trailing newlines.
1500 (when (string-match "\n+\\'" doc-text)
1501 (setq doc-text (substring doc-text 0 (match-beginning 0))))
1502 (push (widget-create-child-and-convert
1503 widget 'documentation-string
1504 :indent (cond ((numberp doc-indent )
1512 (error "Unknown escape `%c'" escape)))
1513 (widget-put widget :buttons buttons)))
1515 (defun widget-default-button-face-get (widget)
1516 ;; Use :button-face or widget-button-face
1517 (or (widget-get widget :button-face)
1518 (let ((parent (widget-get widget :parent)))
1520 (widget-apply parent :button-face-get)
1521 widget-button-face))))
1523 (defun widget-default-sample-face-get (widget)
1524 ;; Use :sample-face.
1525 (widget-get widget :sample-face))
1527 (defun widget-default-delete (widget)
1528 "Remove widget from the buffer."
1529 (let ((from (widget-get widget :from))
1530 (to (widget-get widget :to))
1531 (inactive-overlay (widget-get widget :inactive))
1532 (button-overlay (widget-get widget :button-overlay))
1533 (sample-overlay (widget-get widget :sample-overlay))
1534 (doc-overlay (widget-get widget :doc-overlay))
1535 (inhibit-modification-hooks t)
1536 (inhibit-read-only t))
1537 (widget-apply widget :value-delete)
1538 (widget-children-value-delete widget)
1539 (when inactive-overlay
1540 (delete-overlay inactive-overlay))
1541 (when button-overlay
1542 (delete-overlay button-overlay))
1543 (when sample-overlay
1544 (delete-overlay sample-overlay))
1546 (delete-overlay doc-overlay))
1548 ;; Kludge: this doesn't need to be true for empty formats.
1549 (delete-region from to))
1550 (set-marker from nil)
1551 (set-marker to nil))
1552 (widget-clear-undo))
1554 (defun widget-default-value-set (widget value)
1555 "Recreate widget with new value."
1556 (let* ((old-pos (point))
1557 (from (copy-marker (widget-get widget :from)))
1558 (to (copy-marker (widget-get widget :to)))
1559 (offset (if (and (<= from old-pos) (<= old-pos to))
1560 (if (>= old-pos (1- to))
1562 (- old-pos from)))))
1563 ;;??? Bug: this ought to insert the new value before deleting the old one,
1564 ;; so that markers on either side of the value automatically
1565 ;; stay on the same side. -- rms.
1567 (goto-char (widget-get widget :from))
1568 (widget-apply widget :delete)
1569 (widget-put widget :value value)
1570 (widget-apply widget :create))
1573 (goto-char (+ (widget-get widget :to) offset 1))
1574 (goto-char (min (+ from offset) (1- (widget-get widget :to))))))))
1576 (defun widget-default-value-inline (widget)
1577 "Wrap value in a list unless it is inline."
1578 (if (widget-get widget :inline)
1579 (widget-value widget)
1580 (list (widget-value widget))))
1582 (defun widget-default-default-get (widget)
1584 (widget-get widget :value))
1586 (defun widget-default-menu-tag-get (widget)
1587 "Use tag or value for menus."
1588 (or (widget-get widget :menu-tag)
1589 (widget-get widget :tag)
1590 (widget-princ-to-string (widget-get widget :value))))
1592 (defun widget-default-active (widget)
1593 "Return t iff this widget active (user modifiable)."
1594 (or (widget-get widget :always-active)
1595 (and (not (widget-get widget :inactive))
1596 (let ((parent (widget-get widget :parent)))
1598 (widget-apply parent :active))))))
1600 (defun widget-default-deactivate (widget)
1601 "Make WIDGET inactive for user modifications."
1602 (widget-specify-inactive widget
1603 (widget-get widget :from)
1604 (widget-get widget :to)))
1606 (defun widget-default-action (widget &optional event)
1607 "Notify the parent when a widget changes."
1608 (let ((parent (widget-get widget :parent)))
1610 (widget-apply parent :notify widget event))))
1612 (defun widget-default-notify (widget child &optional event)
1613 "Pass notification to parent."
1614 (widget-default-action widget event))
1616 (defun widget-default-prompt-value (widget prompt value unbound)
1617 "Read an arbitrary value. Stolen from `set-variable'."
1618 ;; (let ((initial (if unbound
1620 ;; It would be nice if we could do a `(cons val 1)' here.
1621 ;; (prin1-to-string (custom-quote value))))))
1622 (eval-minibuffer prompt))
1624 ;;; The `item' Widget.
1626 (define-widget 'item 'default
1627 "Constant items for inclusion in other widgets."
1628 :convert-widget 'widget-value-convert-widget
1629 :value-create 'widget-item-value-create
1630 :value-delete 'ignore
1631 :value-get 'widget-value-value-get
1632 :match 'widget-item-match
1633 :match-inline 'widget-item-match-inline
1634 :action 'widget-item-action
1637 (defun widget-item-value-create (widget)
1638 "Insert the printed representation of the value."
1639 (princ (widget-get widget :value) (current-buffer)))
1641 (defun widget-item-match (widget value)
1642 ;; Match if the value is the same.
1643 (equal (widget-get widget :value) value))
1645 (defun widget-item-match-inline (widget values)
1646 ;; Match if the value is the same.
1647 (let ((value (widget-get widget :value)))
1649 (<= (length value) (length values))
1650 (let ((head (widget-sublist values 0 (length value))))
1651 (and (equal head value)
1652 (cons head (widget-sublist values (length value))))))))
1654 (defun widget-sublist (list start &optional end)
1655 "Return the sublist of LIST from START to END.
1656 If END is omitted, it defaults to the length of LIST."
1657 (if (> start 0) (setq list (nthcdr start list)))
1659 (unless (<= end start)
1660 (setq list (copy-sequence list))
1661 (setcdr (nthcdr (- end start 1) list) nil)
1663 (copy-sequence list)))
1665 (defun widget-item-action (widget &optional event)
1666 ;; Just notify itself.
1667 (widget-apply widget :notify widget event))
1669 ;;; The `push-button' Widget.
1671 ;; (defcustom widget-push-button-gui t
1672 ;; "If non nil, use GUI push buttons when available."
1676 ;; Cache already created GUI objects.
1677 ;; (defvar widget-push-button-cache nil)
1679 (defcustom widget-push-button-prefix "["
1680 "String used as prefix for buttons."
1682 :group 'widget-button)
1684 (defcustom widget-push-button-suffix "]"
1685 "String used as suffix for buttons."
1687 :group 'widget-button)
1689 (define-widget 'push-button 'item
1690 "A pushable button."
1693 :value-create 'widget-push-button-value-create
1696 (defun widget-push-button-value-create (widget)
1697 "Insert text representing the `on' and `off' states."
1698 (let* ((tag (or (widget-get widget :tag)
1699 (widget-get widget :value)))
1700 (tag-glyph (widget-get widget :tag-glyph))
1701 (text (concat widget-push-button-prefix
1702 tag widget-push-button-suffix)))
1704 (widget-image-insert widget text tag-glyph)
1707 ;; (defun widget-gui-action (widget)
1708 ;; "Apply :action for WIDGET."
1709 ;; (widget-apply-action widget (this-command-keys)))
1711 ;;; The `link' Widget.
1713 (defcustom widget-link-prefix "["
1714 "String used as prefix for links."
1716 :group 'widget-button)
1718 (defcustom widget-link-suffix "]"
1719 "String used as suffix for links."
1721 :group 'widget-button)
1723 (define-widget 'link 'item
1725 :button-prefix 'widget-link-prefix
1726 :button-suffix 'widget-link-suffix
1728 :help-echo "Follow the link."
1731 ;;; The `info-link' Widget.
1733 (define-widget 'info-link 'link
1734 "A link to an info file."
1735 :action 'widget-info-link-action)
1737 (defun widget-info-link-action (widget &optional event)
1738 "Open the info node specified by WIDGET."
1739 (info (widget-value widget)))
1741 ;;; The `url-link' Widget.
1743 (define-widget 'url-link 'link
1744 "A link to an www page."
1745 :action 'widget-url-link-action)
1747 (defun widget-url-link-action (widget &optional event)
1748 "Open the url specified by WIDGET."
1749 (browse-url (widget-value widget)))
1751 ;;; The `function-link' Widget.
1753 (define-widget 'function-link 'link
1754 "A link to an Emacs function."
1755 :action 'widget-function-link-action)
1757 (defun widget-function-link-action (widget &optional event)
1758 "Show the function specified by WIDGET."
1759 (describe-function (widget-value widget)))
1761 ;;; The `variable-link' Widget.
1763 (define-widget 'variable-link 'link
1764 "A link to an Emacs variable."
1765 :action 'widget-variable-link-action)
1767 (defun widget-variable-link-action (widget &optional event)
1768 "Show the variable specified by WIDGET."
1769 (describe-variable (widget-value widget)))
1771 ;;; The `file-link' Widget.
1773 (define-widget 'file-link 'link
1775 :action 'widget-file-link-action)
1777 (defun widget-file-link-action (widget &optional event)
1778 "Find the file specified by WIDGET."
1779 (find-file (widget-value widget)))
1781 ;;; The `emacs-library-link' Widget.
1783 (define-widget 'emacs-library-link 'link
1784 "A link to an Emacs Lisp library file."
1785 :action 'widget-emacs-library-link-action)
1787 (defun widget-emacs-library-link-action (widget &optional event)
1788 "Find the Emacs Library file specified by WIDGET."
1789 (find-file (locate-library (widget-value widget))))
1791 ;;; The `emacs-commentary-link' Widget.
1793 (define-widget 'emacs-commentary-link 'link
1794 "A link to Commentary in an Emacs Lisp library file."
1795 :action 'widget-emacs-commentary-link-action)
1797 (defun widget-emacs-commentary-link-action (widget &optional event)
1798 "Find the Commentary section of the Emacs file specified by WIDGET."
1799 (finder-commentary (widget-value widget)))
1801 ;;; The `editable-field' Widget.
1803 (define-widget 'editable-field 'default
1804 "An editable text field."
1805 :convert-widget 'widget-value-convert-widget
1806 :keymap widget-field-keymap
1808 :help-echo "M-TAB: complete field; RET: enter value"
1810 :prompt-internal 'widget-field-prompt-internal
1811 :prompt-history 'widget-field-history
1812 :prompt-value 'widget-field-prompt-value
1813 :action 'widget-field-action
1814 :validate 'widget-field-validate
1816 :error "Field's value doesn't match allowed forms"
1817 :value-create 'widget-field-value-create
1818 :value-delete 'widget-field-value-delete
1819 :value-get 'widget-field-value-get
1820 :match 'widget-field-match)
1822 (defvar widget-field-history nil
1823 "History of field minibuffer edits.")
1825 (defun widget-field-prompt-internal (widget prompt initial history)
1826 "Read string for WIDGET promptinhg with PROMPT.
1827 INITIAL is the initial input and HISTORY is a symbol containing
1829 (read-string prompt initial history))
1831 (defun widget-field-prompt-value (widget prompt value unbound)
1832 "Prompt for a string."
1833 (widget-apply widget
1835 (widget-apply widget
1836 :prompt-internal prompt
1838 (cons (widget-apply widget
1839 :value-to-internal value)
1841 (widget-get widget :prompt-history))))
1843 (defvar widget-edit-functions nil)
1845 (defun widget-field-action (widget &optional event)
1846 "Move to next field."
1848 (run-hook-with-args 'widget-edit-functions widget))
1850 (defun widget-field-validate (widget)
1851 "Valid if the content matches `:valid-regexp'."
1852 (unless (string-match (widget-get widget :valid-regexp)
1853 (widget-apply widget :value-get))
1856 (defun widget-field-value-create (widget)
1857 "Create an editable text field."
1858 (let ((size (widget-get widget :size))
1859 (value (widget-get widget :value))
1861 ;; This is changed to a real overlay in `widget-setup'. We
1862 ;; need the end points to behave differently until
1863 ;; `widget-setup' is called.
1864 (overlay (cons (make-marker) (make-marker))))
1865 (widget-put widget :field-overlay overlay)
1868 (< (length value) size)
1869 (insert-char ?\ (- size (length value))))
1870 (unless (memq widget widget-field-list)
1871 (setq widget-field-new (cons widget widget-field-new)))
1872 (move-marker (cdr overlay) (point))
1873 (set-marker-insertion-type (cdr overlay) nil)
1876 (move-marker (car overlay) from)
1877 (set-marker-insertion-type (car overlay) t)))
1879 (defun widget-field-value-delete (widget)
1880 "Remove the widget from the list of active editing fields."
1881 (setq widget-field-list (delq widget widget-field-list))
1882 (setq widget-field-new (delq widget widget-field-new))
1883 ;; These are nil if the :format string doesn't contain `%v'.
1884 (let ((overlay (widget-get widget :field-overlay)))
1885 (when (overlayp overlay)
1886 (delete-overlay overlay))))
1888 (defun widget-field-value-get (widget)
1889 "Return current text in editing field."
1890 (let ((from (widget-field-start widget))
1891 (to (widget-field-end widget))
1892 (buffer (widget-field-buffer widget))
1893 (size (widget-get widget :size))
1894 (secret (widget-get widget :secret))
1895 (old (current-buffer)))
1902 (eq (char-after (1- to)) ?\ ))
1904 (let ((result (buffer-substring-no-properties from to)))
1907 (while (< (+ from index) to)
1909 (get-char-property (+ from index) 'secret))
1910 (setq index (1+ index)))))
1913 (widget-get widget :value))))
1915 (defun widget-field-match (widget value)
1916 ;; Match any string.
1919 ;;; The `text' Widget.
1921 (define-widget 'text 'editable-field
1922 "A multiline text area."
1923 :keymap widget-text-keymap)
1925 ;;; The `menu-choice' Widget.
1927 (define-widget 'menu-choice 'default
1928 "A menu of options."
1929 :convert-widget 'widget-types-convert-widget
1930 :copy 'widget-types-copy
1931 :format "%[%t%]: %v"
1934 :void '(item :format "invalid (%t)\n")
1935 :value-create 'widget-choice-value-create
1936 :value-get 'widget-child-value-get
1937 :value-inline 'widget-child-value-inline
1938 :default-get 'widget-choice-default-get
1939 :mouse-down-action 'widget-choice-mouse-down-action
1940 :action 'widget-choice-action
1941 :error "Make a choice"
1942 :validate 'widget-choice-validate
1943 :match 'widget-choice-match
1944 :match-inline 'widget-choice-match-inline)
1946 (defun widget-choice-value-create (widget)
1947 "Insert the first choice that matches the value."
1948 (let ((value (widget-get widget :value))
1949 (args (widget-get widget :args))
1950 (explicit (widget-get widget :explicit-choice))
1952 (if (and explicit (equal value (widget-get widget :explicit-choice-value)))
1954 ;; If the user specified the choice for this value,
1955 ;; respect that choice as long as the value is the same.
1956 (widget-put widget :children (list (widget-create-child-value
1957 widget explicit value)))
1958 (widget-put widget :choice explicit))
1960 (setq current (car args)
1962 (when (widget-apply current :match value)
1963 (widget-put widget :children (list (widget-create-child-value
1964 widget current value)))
1965 (widget-put widget :choice current)
1969 (let ((void (widget-get widget :void)))
1970 (widget-put widget :children (list (widget-create-child-and-convert
1971 widget void :value value)))
1972 (widget-put widget :choice void))))))
1974 (defun widget-choice-default-get (widget)
1975 ;; Get default for the first choice.
1976 (widget-default-get (car (widget-get widget :args))))
1978 (defcustom widget-choice-toggle nil
1979 "If non-nil, a binary choice will just toggle between the values.
1980 Otherwise, the user will explicitly have to choose between the values
1981 when he invoked the menu."
1985 (defun widget-choice-mouse-down-action (widget &optional event)
1986 ;; Return non-nil if we need a menu.
1987 (let ((args (widget-get widget :args))
1988 (old (widget-get widget :choice)))
1989 (cond ((not (display-popup-menus-p))
1990 ;; No place to pop up a menu.
1992 ((< (length args) 2)
1993 ;; Empty or singleton list, just return the value.
1995 ((> (length args) widget-menu-max-size)
1996 ;; Too long, prompt.
1998 ((> (length args) 2)
1999 ;; Reasonable sized list, use menu.
2001 ((and widget-choice-toggle (memq old args))
2005 ;; Ask which of the two.
2008 (defun widget-choice-action (widget &optional event)
2010 (let ((args (widget-get widget :args))
2011 (old (widget-get widget :choice))
2012 (tag (widget-apply widget :menu-tag-get))
2013 (completion-ignore-case (widget-get widget :case-fold))
2016 ;; Remember old value.
2017 (if (and old (not (widget-apply widget :validate)))
2018 (let* ((external (widget-value widget))
2019 (internal (widget-apply old :value-to-internal external)))
2020 (widget-put old :value internal)))
2023 (cond ((= (length args) 0)
2025 ((= (length args) 1)
2027 ((and widget-choice-toggle
2030 (if (eq old (nth 0 args))
2035 (setq current (car args)
2038 (cons (cons (widget-apply current :menu-tag-get)
2041 (setq this-explicit t)
2042 (widget-choose tag (reverse choices) event))))
2044 ;; If this was an explicit user choice,
2045 ;; record the choice, and the record the value it was made for.
2046 ;; widget-choice-value-create will respect this choice,
2047 ;; as long as the value is the same.
2049 (widget-put widget :explicit-choice current)
2050 (widget-put widget :explicit-choice-value (widget-get widget :value)))
2051 (widget-value-set widget (widget-default-get current))
2053 (widget-apply widget :notify widget event)))
2054 (run-hook-with-args 'widget-edit-functions widget))
2056 (defun widget-choice-validate (widget)
2057 ;; Valid if we have made a valid choice.
2058 (if (eq (widget-get widget :void) (widget-get widget :choice))
2060 (widget-apply (car (widget-get widget :children)) :validate)))
2062 (defun widget-choice-match (widget value)
2063 ;; Matches if one of the choices matches.
2064 (let ((args (widget-get widget :args))
2066 (while (and args (not found))
2067 (setq current (car args)
2069 found (widget-apply current :match value)))
2072 (defun widget-choice-match-inline (widget values)
2073 ;; Matches if one of the choices matches.
2074 (let ((args (widget-get widget :args))
2076 (while (and args (null found))
2077 (setq current (car args)
2079 found (widget-match-inline current values)))
2082 ;;; The `toggle' Widget.
2084 (define-widget 'toggle 'item
2085 "Toggle between two states."
2087 :value-create 'widget-toggle-value-create
2088 :action 'widget-toggle-action
2089 :match (lambda (widget value) t)
2093 (defun widget-toggle-value-create (widget)
2094 "Insert text representing the `on' and `off' states."
2095 (if (widget-value widget)
2096 (let ((image (widget-get widget :on-glyph)))
2097 (and (display-graphic-p)
2099 (not (eq (car image) 'image))
2100 (widget-put widget :on-glyph (setq image (eval image))))
2101 (widget-image-insert widget
2102 (widget-get widget :on)
2104 (let ((image (widget-get widget :off-glyph)))
2105 (and (display-graphic-p)
2107 (not (eq (car image) 'image))
2108 (widget-put widget :off-glyph (setq image (eval image))))
2109 (widget-image-insert widget (widget-get widget :off) image))))
2111 (defun widget-toggle-action (widget &optional event)
2113 (widget-value-set widget (not (widget-value widget)))
2114 (widget-apply widget :notify widget event)
2115 (run-hook-with-args 'widget-edit-functions widget))
2117 ;;; The `checkbox' Widget.
2119 (define-widget 'checkbox 'toggle
2120 "A checkbox toggle."
2125 ;; We could probably do the same job as the images using single
2126 ;; space characters in a boxed face with a stretch specification to
2127 ;; make them square.
2128 :on-glyph '(create-image "\300\300\141\143\067\076\034\030"
2129 'xbm t :width 8 :height 8
2130 :background "grey75" ; like default mode line
2135 :off-glyph '(create-image (make-string 8 0)
2136 'xbm t :width 8 :height 8
2137 :background "grey75"
2141 :help-echo "Toggle this item."
2142 :action 'widget-checkbox-action)
2144 (defun widget-checkbox-action (widget &optional event)
2145 "Toggle checkbox, notify parent, and set active state of sibling."
2146 (widget-toggle-action widget event)
2147 (let ((sibling (widget-get-sibling widget)))
2149 (if (widget-value widget)
2150 (widget-apply sibling :activate)
2151 (widget-apply sibling :deactivate)))))
2153 ;;; The `checklist' Widget.
2155 (define-widget 'checklist 'default
2156 "A multiple choice widget."
2157 :convert-widget 'widget-types-convert-widget
2158 :copy 'widget-types-copy
2161 :entry-format "%b %v"
2163 :value-create 'widget-checklist-value-create
2164 :value-get 'widget-checklist-value-get
2165 :validate 'widget-checklist-validate
2166 :match 'widget-checklist-match
2167 :match-inline 'widget-checklist-match-inline)
2169 (defun widget-checklist-value-create (widget)
2170 ;; Insert all values
2171 (let ((alist (widget-checklist-match-find widget (widget-get widget :value)))
2172 (args (widget-get widget :args)))
2174 (widget-checklist-add-item widget (car args) (assq (car args) alist))
2175 (setq args (cdr args)))
2176 (widget-put widget :children (nreverse (widget-get widget :children)))))
2178 (defun widget-checklist-add-item (widget type chosen)
2179 "Create checklist item in WIDGET of type TYPE.
2180 If the item is checked, CHOSEN is a cons whose cdr is the value."
2181 (and (eq (preceding-char) ?\n)
2182 (widget-get widget :indent)
2183 (insert-char ? (widget-get widget :indent)))
2184 (widget-specify-insert
2185 (let* ((children (widget-get widget :children))
2186 (buttons (widget-get widget :buttons))
2187 (button-args (or (widget-get type :sibling-args)
2188 (widget-get widget :button-args)))
2191 (insert (widget-get widget :entry-format))
2193 ;; Parse % escapes in format.
2194 (while (re-search-forward "%\\([bv%]\\)" nil t)
2195 (let ((escape (char-after (match-beginning 1))))
2196 (delete-backward-char 2)
2197 (cond ((eq escape ?%)
2200 (setq button (apply 'widget-create-child-and-convert
2202 :value (not (null chosen))
2207 (let ((child (widget-create-child widget type)))
2208 (widget-apply child :deactivate)
2210 ((widget-get type :inline)
2211 (widget-create-child-value
2212 widget type (cdr chosen)))
2214 (widget-create-child-value
2215 widget type (car (cdr chosen)))))))
2217 (error "Unknown escape `%c'" escape)))))
2218 ;; Update properties.
2219 (and button child (widget-put child :button button))
2220 (and button (widget-put widget :buttons (cons button buttons)))
2221 (and child (widget-put widget :children (cons child children))))))
2223 (defun widget-checklist-match (widget values)
2224 ;; All values must match a type in the checklist.
2226 (null (cdr (widget-checklist-match-inline widget values)))))
2228 (defun widget-checklist-match-inline (widget values)
2229 ;; Find the values which match a type in the checklist.
2230 (let ((greedy (widget-get widget :greedy))
2231 (args (copy-sequence (widget-get widget :args)))
2234 (let ((answer (widget-checklist-match-up args values)))
2236 (let ((vals (widget-match-inline answer values)))
2237 (setq found (append found (car vals))
2239 args (delq answer args))))
2241 (setq rest (append rest (list (car values)))
2242 values (cdr values)))
2244 (setq rest (append rest values)
2248 (defun widget-checklist-match-find (widget vals)
2249 "Find the vals which match a type in the checklist.
2250 Return an alist of (TYPE MATCH)."
2251 (let ((greedy (widget-get widget :greedy))
2252 (args (copy-sequence (widget-get widget :args)))
2255 (let ((answer (widget-checklist-match-up args vals)))
2257 (let ((match (widget-match-inline answer vals)))
2258 (setq found (cons (cons answer (car match)) found)
2260 args (delq answer args))))
2262 (setq vals (cdr vals)))
2267 (defun widget-checklist-match-up (args vals)
2268 "Return the first type from ARGS that matches VALS."
2269 (let (current found)
2270 (while (and args (null found))
2271 (setq current (car args)
2273 found (widget-match-inline current vals)))
2277 (defun widget-checklist-value-get (widget)
2278 ;; The values of all selected items.
2279 (let ((children (widget-get widget :children))
2282 (setq child (car children)
2283 children (cdr children))
2284 (if (widget-value (widget-get child :button))
2285 (setq result (append result (widget-apply child :value-inline)))))
2288 (defun widget-checklist-validate (widget)
2289 ;; Ticked chilren must be valid.
2290 (let ((children (widget-get widget :children))
2292 (while (and children (not found))
2293 (setq child (car children)
2294 children (cdr children)
2295 button (widget-get child :button)
2296 found (and (widget-value button)
2297 (widget-apply child :validate))))
2300 ;;; The `option' Widget
2302 (define-widget 'option 'checklist
2303 "An widget with an optional item."
2306 ;;; The `choice-item' Widget.
2308 (define-widget 'choice-item 'item
2309 "Button items that delegate action events to their parents."
2310 :action 'widget-parent-action
2311 :format "%[%t%] \n")
2313 ;;; The `radio-button' Widget.
2315 (define-widget 'radio-button 'toggle
2316 "A radio button for use in the `radio' widget."
2317 :notify 'widget-radio-button-notify
2324 :off-glyph "radio0")
2326 (defun widget-radio-button-notify (widget child &optional event)
2328 (widget-apply (widget-get widget :parent) :action widget event))
2330 ;;; The `radio-button-choice' Widget.
2332 (define-widget 'radio-button-choice 'default
2333 "Select one of multiple options."
2334 :convert-widget 'widget-types-convert-widget
2335 :copy 'widget-types-copy
2338 :entry-format "%b %v"
2339 :value-create 'widget-radio-value-create
2340 :value-get 'widget-radio-value-get
2341 :value-inline 'widget-radio-value-inline
2342 :value-set 'widget-radio-value-set
2343 :error "You must push one of the buttons"
2344 :validate 'widget-radio-validate
2345 :match 'widget-choice-match
2346 :match-inline 'widget-choice-match-inline
2347 :action 'widget-radio-action)
2349 (defun widget-radio-value-create (widget)
2350 ;; Insert all values
2351 (let ((args (widget-get widget :args))
2354 (setq arg (car args)
2356 (widget-radio-add-item widget arg))))
2358 (defun widget-radio-add-item (widget type)
2359 "Add to radio widget WIDGET a new radio button item of type TYPE."
2360 ;; (setq type (widget-convert type))
2361 (and (eq (preceding-char) ?\n)
2362 (widget-get widget :indent)
2363 (insert-char ? (widget-get widget :indent)))
2364 (widget-specify-insert
2365 (let* ((value (widget-get widget :value))
2366 (children (widget-get widget :children))
2367 (buttons (widget-get widget :buttons))
2368 (button-args (or (widget-get type :sibling-args)
2369 (widget-get widget :button-args)))
2371 (chosen (and (null (widget-get widget :choice))
2372 (widget-apply type :match value)))
2374 (insert (widget-get widget :entry-format))
2376 ;; Parse % escapes in format.
2377 (while (re-search-forward "%\\([bv%]\\)" nil t)
2378 (let ((escape (char-after (match-beginning 1))))
2379 (delete-backward-char 2)
2380 (cond ((eq escape ?%)
2383 (setq button (apply 'widget-create-child-and-convert
2384 widget 'radio-button
2385 :value (not (null chosen))
2388 (setq child (if chosen
2389 (widget-create-child-value
2391 (widget-create-child widget type)))
2393 (widget-apply child :deactivate)))
2395 (error "Unknown escape `%c'" escape)))))
2396 ;; Update properties.
2398 (widget-put widget :choice type))
2400 (widget-put child :button button)
2401 (widget-put widget :buttons (nconc buttons (list button))))
2403 (widget-put widget :children (nconc children (list child))))
2406 (defun widget-radio-value-get (widget)
2407 ;; Get value of the child widget.
2408 (let ((chosen (widget-radio-chosen widget)))
2409 (and chosen (widget-value chosen))))
2411 (defun widget-radio-chosen (widget)
2412 "Return the widget representing the chosen radio button."
2413 (let ((children (widget-get widget :children))
2416 (setq current (car children)
2417 children (cdr children))
2418 (when (widget-apply (widget-get current :button) :value-get)
2423 (defun widget-radio-value-inline (widget)
2424 ;; Get value of the child widget.
2425 (let ((children (widget-get widget :children))
2428 (setq current (car children)
2429 children (cdr children))
2430 (when (widget-apply (widget-get current :button) :value-get)
2431 (setq found (widget-apply current :value-inline)
2435 (defun widget-radio-value-set (widget value)
2436 ;; We can't just delete and recreate a radio widget, since children
2437 ;; can be added after the original creation and won't be recreated
2439 (let ((children (widget-get widget :children))
2442 (setq current (car children)
2443 children (cdr children))
2444 (let* ((button (widget-get current :button))
2445 (match (and (not found)
2446 (widget-apply current :match value))))
2447 (widget-value-set button match)
2450 (widget-value-set current value)
2451 (widget-apply current :activate))
2452 (widget-apply current :deactivate))
2453 (setq found (or found match))))))
2455 (defun widget-radio-validate (widget)
2456 ;; Valid if we have made a valid choice.
2457 (let ((children (widget-get widget :children))
2458 current found button)
2459 (while (and children (not found))
2460 (setq current (car children)
2461 children (cdr children)
2462 button (widget-get current :button)
2463 found (widget-apply button :value-get)))
2465 (widget-apply current :validate)
2468 (defun widget-radio-action (widget child event)
2469 ;; Check if a radio button was pressed.
2470 (let ((children (widget-get widget :children))
2471 (buttons (widget-get widget :buttons))
2473 (when (memq child buttons)
2475 (setq current (car children)
2476 children (cdr children))
2477 (let* ((button (widget-get current :button)))
2478 (cond ((eq child button)
2479 (widget-value-set button t)
2480 (widget-apply current :activate))
2481 ((widget-value button)
2482 (widget-value-set button nil)
2483 (widget-apply current :deactivate)))))))
2484 ;; Pass notification to parent.
2485 (widget-apply widget :notify child event))
2487 ;;; The `insert-button' Widget.
2489 (define-widget 'insert-button 'push-button
2490 "An insert button for the `editable-list' widget."
2492 :help-echo "Insert a new item into the list at this position."
2493 :action 'widget-insert-button-action)
2495 (defun widget-insert-button-action (widget &optional event)
2496 ;; Ask the parent to insert a new item.
2497 (widget-apply (widget-get widget :parent)
2498 :insert-before (widget-get widget :widget)))
2500 ;;; The `delete-button' Widget.
2502 (define-widget 'delete-button 'push-button
2503 "A delete button for the `editable-list' widget."
2505 :help-echo "Delete this item from the list."
2506 :action 'widget-delete-button-action)
2508 (defun widget-delete-button-action (widget &optional event)
2509 ;; Ask the parent to insert a new item.
2510 (widget-apply (widget-get widget :parent)
2511 :delete-at (widget-get widget :widget)))
2513 ;;; The `editable-list' Widget.
2515 ;; (defcustom widget-editable-list-gui nil
2516 ;; "If non nil, use GUI push-buttons in editable list when available."
2520 (define-widget 'editable-list 'default
2521 "A variable list of widgets of the same type."
2522 :convert-widget 'widget-types-convert-widget
2523 :copy 'widget-types-copy
2526 :format-handler 'widget-editable-list-format-handler
2527 :entry-format "%i %d %v"
2528 :value-create 'widget-editable-list-value-create
2529 :value-get 'widget-editable-list-value-get
2530 :validate 'widget-children-validate
2531 :match 'widget-editable-list-match
2532 :match-inline 'widget-editable-list-match-inline
2533 :insert-before 'widget-editable-list-insert-before
2534 :delete-at 'widget-editable-list-delete-at)
2536 (defun widget-editable-list-format-handler (widget escape)
2537 ;; We recognize the insert button.
2538 ;; (let ((widget-push-button-gui widget-editable-list-gui))
2539 (cond ((eq escape ?i)
2540 (and (widget-get widget :indent)
2541 (insert-char ?\ (widget-get widget :indent)))
2542 (apply 'widget-create-child-and-convert
2543 widget 'insert-button
2544 (widget-get widget :append-button-args)))
2546 (widget-default-format-handler widget escape)))
2550 (defun widget-editable-list-value-create (widget)
2551 ;; Insert all values
2552 (let* ((value (widget-get widget :value))
2553 (type (nth 0 (widget-get widget :args)))
2555 (widget-put widget :value-pos (copy-marker (point)))
2556 (set-marker-insertion-type (widget-get widget :value-pos) t)
2558 (let ((answer (widget-match-inline type value)))
2560 (setq children (cons (widget-editable-list-entry-create
2562 (if (widget-get type :inline)
2569 (widget-put widget :children (nreverse children))))
2571 (defun widget-editable-list-value-get (widget)
2572 ;; Get value of the child widget.
2573 (apply 'append (mapcar (lambda (child) (widget-apply child :value-inline))
2574 (widget-get widget :children))))
2576 (defun widget-editable-list-match (widget value)
2577 ;; Value must be a list and all the members must match the type.
2579 (null (cdr (widget-editable-list-match-inline widget value)))))
2581 (defun widget-editable-list-match-inline (widget value)
2582 (let ((type (nth 0 (widget-get widget :args)))
2585 (while (and value ok)
2586 (let ((answer (widget-match-inline type value)))
2588 (setq found (append found (car answer))
2591 (cons found value)))
2593 (defun widget-editable-list-insert-before (widget before)
2594 ;; Insert a new child in the list of children.
2596 (let ((children (widget-get widget :children))
2597 (inhibit-read-only t)
2598 before-change-functions
2599 after-change-functions)
2601 (goto-char (widget-get before :entry-from)))
2603 (goto-char (widget-get widget :value-pos))))
2604 (let ((child (widget-editable-list-entry-create
2606 (when (< (widget-get child :entry-from) (widget-get widget :from))
2607 (set-marker (widget-get widget :from)
2608 (widget-get child :entry-from)))
2609 (if (eq (car children) before)
2610 (widget-put widget :children (cons child children))
2611 (while (not (eq (car (cdr children)) before))
2612 (setq children (cdr children)))
2613 (setcdr children (cons child (cdr children)))))))
2615 (widget-apply widget :notify widget))
2617 (defun widget-editable-list-delete-at (widget child)
2618 ;; Delete child from list of children.
2620 (let ((buttons (copy-sequence (widget-get widget :buttons)))
2622 (inhibit-read-only t)
2623 before-change-functions
2624 after-change-functions)
2626 (setq button (car buttons)
2627 buttons (cdr buttons))
2628 (when (eq (widget-get button :widget) child)
2630 :buttons (delq button (widget-get widget :buttons)))
2631 (widget-delete button))))
2632 (let ((entry-from (widget-get child :entry-from))
2633 (entry-to (widget-get child :entry-to))
2634 (inhibit-read-only t)
2635 before-change-functions
2636 after-change-functions)
2637 (widget-delete child)
2638 (delete-region entry-from entry-to)
2639 (set-marker entry-from nil)
2640 (set-marker entry-to nil))
2641 (widget-put widget :children (delq child (widget-get widget :children))))
2643 (widget-apply widget :notify widget))
2645 (defun widget-editable-list-entry-create (widget value conv)
2646 ;; Create a new entry to the list.
2647 (let ((type (nth 0 (widget-get widget :args)))
2648 ;; (widget-push-button-gui widget-editable-list-gui)
2649 child delete insert)
2650 (widget-specify-insert
2652 (and (widget-get widget :indent)
2653 (insert-char ?\ (widget-get widget :indent)))
2654 (insert (widget-get widget :entry-format)))
2655 ;; Parse % escapes in format.
2656 (while (re-search-forward "%\\(.\\)" nil t)
2657 (let ((escape (char-after (match-beginning 1))))
2658 (delete-backward-char 2)
2659 (cond ((eq escape ?%)
2662 (setq insert (apply 'widget-create-child-and-convert
2663 widget 'insert-button
2664 (widget-get widget :insert-button-args))))
2666 (setq delete (apply 'widget-create-child-and-convert
2667 widget 'delete-button
2668 (widget-get widget :delete-button-args))))
2671 (setq child (widget-create-child-value
2673 (setq child (widget-create-child-value
2674 widget type (widget-default-get type)))))
2676 (error "Unknown escape `%c'" escape)))))
2677 (let ((buttons (widget-get widget :buttons)))
2678 (if insert (push insert buttons))
2679 (if delete (push delete buttons))
2680 (widget-put widget :buttons buttons))
2681 (let ((entry-from (point-min-marker))
2682 (entry-to (point-max-marker)))
2683 (set-marker-insertion-type entry-from t)
2684 (set-marker-insertion-type entry-to nil)
2685 (widget-put child :entry-from entry-from)
2686 (widget-put child :entry-to entry-to)))
2687 (if insert (widget-put insert :widget child))
2688 (if delete (widget-put delete :widget child))
2691 ;;; The `group' Widget.
2693 (define-widget 'group 'default
2694 "A widget which groups other widgets inside."
2695 :convert-widget 'widget-types-convert-widget
2696 :copy 'widget-types-copy
2698 :value-create 'widget-group-value-create
2699 :value-get 'widget-editable-list-value-get
2700 :default-get 'widget-group-default-get
2701 :validate 'widget-children-validate
2702 :match 'widget-group-match
2703 :match-inline 'widget-group-match-inline)
2705 (defun widget-group-value-create (widget)
2706 ;; Create each component.
2707 (let ((args (widget-get widget :args))
2708 (value (widget-get widget :value))
2709 arg answer children)
2711 (setq arg (car args)
2713 answer (widget-match-inline arg value)
2715 (and (eq (preceding-char) ?\n)
2716 (widget-get widget :indent)
2717 (insert-char ?\ (widget-get widget :indent)))
2718 (push (cond ((null answer)
2719 (widget-create-child widget arg))
2720 ((widget-get arg :inline)
2721 (widget-create-child-value widget arg (car answer)))
2723 (widget-create-child-value widget arg (car (car answer)))))
2725 (widget-put widget :children (nreverse children))))
2727 (defun widget-group-default-get (widget)
2728 ;; Get the default of the components.
2729 (mapcar 'widget-default-get (widget-get widget :args)))
2731 (defun widget-group-match (widget values)
2732 ;; Match if the components match.
2734 (let ((match (widget-group-match-inline widget values)))
2735 (and match (null (cdr match))))))
2737 (defun widget-group-match-inline (widget vals)
2738 ;; Match if the components match.
2739 (let ((args (widget-get widget :args))
2740 argument answer found)
2742 (setq argument (car args)
2744 answer (widget-match-inline argument vals))
2746 (setq vals (cdr answer)
2747 found (append found (car answer)))
2751 (cons found vals))))
2753 ;;; The `visibility' Widget.
2755 (define-widget 'visibility 'item
2756 "An indicator and manipulator for hidden items."
2762 :value-create 'widget-visibility-value-create
2763 :action 'widget-toggle-action
2764 :match (lambda (widget value) t))
2766 (defun widget-visibility-value-create (widget)
2767 ;; Insert text representing the `on' and `off' states.
2768 (let ((on (widget-get widget :on))
2769 (off (widget-get widget :off)))
2771 (setq on (concat widget-push-button-prefix
2773 widget-push-button-suffix))
2776 (setq off (concat widget-push-button-prefix
2778 widget-push-button-suffix))
2780 (if (widget-value widget)
2781 (widget-image-insert widget on "down" "down-pushed")
2782 (widget-image-insert widget off "right" "right-pushed"))))
2784 ;;; The `documentation-link' Widget.
2786 ;; This is a helper widget for `documentation-string'.
2788 (define-widget 'documentation-link 'link
2789 "Link type used in documentation strings."
2791 :help-echo "Describe this symbol"
2792 :action 'widget-documentation-link-action)
2794 (defun widget-documentation-link-action (widget &optional event)
2795 "Display documentation for WIDGET's value. Ignore optional argument EVENT."
2796 (let* ((string (widget-get widget :value))
2797 (symbol (intern string)))
2798 (if (and (fboundp symbol) (boundp symbol))
2799 ;; If there are two doc strings, give the user a way to pick one.
2800 (apropos (concat "\\`" (regexp-quote string) "\\'"))
2801 (if (fboundp symbol)
2802 (describe-function symbol)
2803 (describe-variable symbol)))))
2805 (defcustom widget-documentation-links t
2806 "Add hyperlinks to documentation strings when non-nil."
2808 :group 'widget-documentation)
2810 (defcustom widget-documentation-link-regexp "`\\([^\n`' ]+\\)'"
2811 "Regexp for matching potential links in documentation strings.
2812 The first group should be the link itself."
2814 :group 'widget-documentation)
2816 (defcustom widget-documentation-link-p 'intern-soft
2817 "Predicate used to test if a string is useful as a link.
2818 The value should be a function. The function will be called one
2819 argument, a string, and should return non-nil if there should be a
2820 link for that string."
2822 :options '(widget-documentation-link-p)
2823 :group 'widget-documentation)
2825 (defcustom widget-documentation-link-type 'documentation-link
2826 "Widget type used for links in documentation strings."
2828 :group 'widget-documentation)
2830 (defun widget-documentation-link-add (widget from to)
2831 (widget-specify-doc widget from to)
2832 (when widget-documentation-links
2833 (let ((regexp widget-documentation-link-regexp)
2834 (buttons (widget-get widget :buttons))
2835 (widget-mouse-face (default-value 'widget-mouse-face))
2836 (widget-button-face widget-documentation-face)
2837 (widget-button-pressed-face widget-documentation-face))
2840 (while (re-search-forward regexp to t)
2841 (let ((name (match-string 1))
2842 (begin (match-beginning 1))
2843 (end (match-end 1)))
2844 (when (funcall widget-documentation-link-p name)
2845 (push (widget-convert-button widget-documentation-link-type
2846 begin end :value name)
2848 (widget-put widget :buttons buttons)))
2849 (let ((indent (widget-get widget :indent)))
2850 (when (and indent (not (zerop indent)))
2853 (narrow-to-region from to)
2854 (goto-char (point-min))
2855 (while (search-forward "\n" nil t)
2856 (insert-char ?\ indent)))))))
2858 ;;; The `documentation-string' Widget.
2860 (define-widget 'documentation-string 'item
2861 "A documentation string."
2863 :action 'widget-documentation-string-action
2864 :value-create 'widget-documentation-string-value-create)
2866 (defun widget-documentation-string-value-create (widget)
2867 ;; Insert documentation string.
2868 (let ((doc (widget-value widget))
2869 (indent (widget-get widget :indent))
2870 (shown (widget-get (widget-get widget :parent) :documentation-shown))
2872 (if (string-match "\n" doc)
2873 (let ((before (substring doc 0 (match-beginning 0)))
2874 (after (substring doc (match-beginning 0)))
2877 (widget-documentation-link-add widget start (point))
2879 (widget-create-child-and-convert
2881 :help-echo "Show or hide rest of the documentation."
2885 :action 'widget-parent-action
2888 (setq start (point))
2889 (when (and indent (not (zerop indent)))
2890 (insert-char ?\ indent))
2892 (widget-documentation-link-add widget start (point)))
2893 (widget-put widget :buttons (list button)))
2895 (widget-documentation-link-add widget start (point))))
2898 (defun widget-documentation-string-action (widget &rest ignore)
2899 ;; Toggle documentation.
2900 (let ((parent (widget-get widget :parent)))
2901 (widget-put parent :documentation-shown
2902 (not (widget-get parent :documentation-shown))))
2904 (widget-value-set widget (widget-value widget)))
2906 ;;; The Sexp Widgets.
2908 (define-widget 'const 'item
2909 "An immutable sexp."
2910 :prompt-value 'widget-const-prompt-value
2913 (defun widget-const-prompt-value (widget prompt value unbound)
2914 ;; Return the value of the const.
2915 (widget-value widget))
2917 (define-widget 'function-item 'const
2918 "An immutable function name."
2920 :documentation-property (lambda (symbol)
2922 (documentation symbol t)
2925 (define-widget 'variable-item 'const
2926 "An immutable variable name."
2928 :documentation-property 'variable-documentation)
2930 (define-widget 'other 'sexp
2931 "Matches any value, but doesn't let the user edit the value.
2932 This is useful as last item in a `choice' widget.
2933 You should use this widget type with a default value,
2934 as in (other DEFAULT) or (other :tag \"NAME\" DEFAULT).
2935 If the user selects this alternative, that specifies DEFAULT
2941 (defvar widget-string-prompt-value-history nil
2942 "History of input to `widget-string-prompt-value'.")
2944 (define-widget 'string 'editable-field
2947 :format "%{%t%}: %v"
2948 :complete-function 'ispell-complete-word
2949 :prompt-history 'widget-string-prompt-value-history)
2951 (define-widget 'regexp 'string
2952 "A regular expression."
2953 :match 'widget-regexp-match
2954 :validate 'widget-regexp-validate
2955 ;; Doesn't work well with terminating newline.
2956 ;; :value-face 'widget-single-line-field-face
2959 (defun widget-regexp-match (widget value)
2960 ;; Match valid regexps.
2961 (and (stringp value)
2964 (string-match value ""))
2967 (defun widget-regexp-validate (widget)
2968 "Check that the value of WIDGET is a valid regexp."
2969 (condition-case data
2971 (string-match (widget-value widget) ""))
2972 (error (widget-put widget :error (error-message-string data))
2975 (define-widget 'file 'string
2977 It will read a file name from the minibuffer when invoked."
2978 :complete-function 'widget-file-complete
2979 :prompt-value 'widget-file-prompt-value
2980 :format "%{%t%}: %v"
2981 ;; Doesn't work well with terminating newline.
2982 ;; :value-face 'widget-single-line-field-face
2985 (defun widget-file-complete ()
2986 "Perform completion on file name preceding point."
2988 (let* ((end (point))
2989 (beg (save-excursion
2990 (skip-chars-backward "^ ")
2992 (pattern (buffer-substring beg end))
2993 (name-part (file-name-nondirectory pattern))
2994 (directory (file-name-directory pattern))
2995 (completion (file-name-completion name-part directory)))
2996 (cond ((eq completion t))
2998 (message "Can't find completion for \"%s\"" pattern)
3000 ((not (string= name-part completion))
3001 (delete-region beg end)
3002 (insert (expand-file-name completion directory)))
3004 (message "Making completion list...")
3005 (with-output-to-temp-buffer "*Completions*"
3006 (display-completion-list
3007 (sort (file-name-all-completions name-part directory)
3009 (message "Making completion list...%s" "done")))))
3011 (defun widget-file-prompt-value (widget prompt value unbound)
3012 ;; Read file from minibuffer.
3013 (abbreviate-file-name
3015 (read-file-name prompt)
3016 (let ((prompt2 (format "%s (default %s) " prompt value))
3017 (dir (file-name-directory value))
3018 (file (file-name-nondirectory value))
3019 (must-match (widget-get widget :must-match)))
3020 (read-file-name prompt2 dir nil must-match file)))))
3022 ;;;(defun widget-file-action (widget &optional event)
3023 ;;; ;; Read a file name from the minibuffer.
3024 ;;; (let* ((value (widget-value widget))
3025 ;;; (dir (file-name-directory value))
3026 ;;; (file (file-name-nondirectory value))
3027 ;;; (menu-tag (widget-apply widget :menu-tag-get))
3028 ;;; (must-match (widget-get widget :must-match))
3029 ;;; (answer (read-file-name (concat menu-tag ": (default `" value "') ")
3030 ;;; dir nil must-match file)))
3031 ;;; (widget-value-set widget (abbreviate-file-name answer))
3033 ;;; (widget-apply widget :notify widget event)))
3035 ;; Fixme: use file-name-as-directory.
3036 (define-widget 'directory 'file
3037 "A directory widget.
3038 It will read a directory name from the minibuffer when invoked."
3041 (defvar widget-symbol-prompt-value-history nil
3042 "History of input to `widget-symbol-prompt-value'.")
3044 (define-widget 'symbol 'editable-field
3048 :format "%{%t%}: %v"
3049 :match (lambda (widget value) (symbolp value))
3050 :complete-function 'lisp-complete-symbol
3051 :prompt-internal 'widget-symbol-prompt-internal
3052 :prompt-match 'symbolp
3053 :prompt-history 'widget-symbol-prompt-value-history
3054 :value-to-internal (lambda (widget value)
3058 :value-to-external (lambda (widget value)
3063 (defun widget-symbol-prompt-internal (widget prompt initial history)
3064 ;; Read file from minibuffer.
3065 (let ((answer (completing-read prompt obarray
3066 (widget-get widget :prompt-match)
3067 nil initial history)))
3068 (if (and (stringp answer)
3069 (not (zerop (length answer))))
3071 (error "No value"))))
3073 (defvar widget-function-prompt-value-history nil
3074 "History of input to `widget-function-prompt-value'.")
3076 (define-widget 'function 'restricted-sexp
3078 :complete-function (lambda ()
3080 (lisp-complete-symbol 'fboundp))
3081 :prompt-value 'widget-field-prompt-value
3082 :prompt-internal 'widget-symbol-prompt-internal
3083 :prompt-match 'fboundp
3084 :prompt-history 'widget-function-prompt-value-history
3085 :action 'widget-field-action
3086 :match-alternatives '(functionp)
3087 :validate (lambda (widget)
3088 (unless (functionp (widget-value widget))
3089 (widget-put widget :error (format "Invalid function: %S"
3090 (widget-value widget)))
3095 (defvar widget-variable-prompt-value-history nil
3096 "History of input to `widget-variable-prompt-value'.")
3098 (define-widget 'variable 'symbol
3100 :prompt-match 'boundp
3101 :prompt-history 'widget-variable-prompt-value-history
3102 :complete-function (lambda ()
3104 (lisp-complete-symbol 'boundp))
3107 (defvar widget-coding-system-prompt-value-history nil
3108 "History of input to `widget-coding-system-prompt-value'.")
3110 (define-widget 'coding-system 'symbol
3111 "A MULE coding-system."
3112 :format "%{%t%}: %v"
3113 :tag "Coding system"
3115 :prompt-history 'widget-coding-system-prompt-value-history
3116 :prompt-value 'widget-coding-system-prompt-value
3117 :action 'widget-coding-system-action
3118 :complete-function (lambda ()
3120 (lisp-complete-symbol 'coding-system-p))
3121 :validate (lambda (widget)
3122 (unless (coding-system-p (widget-value widget))
3123 (widget-put widget :error (format "Invalid coding system: %S"
3124 (widget-value widget)))
3127 :prompt-match 'coding-system-p)
3129 (defun widget-coding-system-prompt-value (widget prompt value unbound)
3130 "Read coding-system from minibuffer."
3131 (if (widget-get widget :base-only)
3133 (completing-read (format "%s (default %s) " prompt value)
3134 (mapcar #'list (coding-system-list t)) nil nil nil
3135 coding-system-history))
3136 (read-coding-system (format "%s (default %s) " prompt value) value)))
3138 (defun widget-coding-system-action (widget &optional event)
3140 (widget-coding-system-prompt-value
3142 (widget-apply widget :menu-tag-get)
3143 (widget-value widget)
3145 (widget-value-set widget answer)
3146 (widget-apply widget :notify widget event)
3149 (define-widget 'sexp 'editable-field
3150 "An arbitrary Lisp expression."
3151 :tag "Lisp expression"
3152 :format "%{%t%}: %v"
3154 :validate 'widget-sexp-validate
3155 :match (lambda (widget value) t)
3156 :value-to-internal 'widget-sexp-value-to-internal
3157 :value-to-external (lambda (widget value) (read value))
3158 :prompt-history 'widget-sexp-prompt-value-history
3159 :prompt-value 'widget-sexp-prompt-value)
3161 (defun widget-sexp-value-to-internal (widget value)
3162 ;; Use pp for printer representation.
3163 (let ((pp (if (symbolp value)
3164 (prin1-to-string value)
3165 (pp-to-string value))))
3166 (while (string-match "\n\\'" pp)
3167 (setq pp (substring pp 0 -1)))
3168 (if (or (string-match "\n\\'" pp)
3173 (defun widget-sexp-validate (widget)
3174 ;; Valid if we can read the string and there is no junk left after it.
3176 (insert (widget-apply widget :value-get))
3177 (goto-char (point-min))
3179 (condition-case data
3181 ;; Avoid a confusing end-of-file error.
3182 (skip-syntax-forward "\\s-")
3184 (setq err "Empty sexp -- use `nil'?")
3185 (unless (widget-apply widget :match (read (current-buffer)))
3186 (setq err (widget-get widget :type-error))))
3187 ;; Allow whitespace after expression.
3188 (skip-syntax-forward "\\s-")
3189 (if (and (not (eobp))
3191 (setq err (format "Junk at end of expression: %s"
3192 (buffer-substring (point)
3194 (end-of-file ; Avoid confusing error message.
3195 (setq err "Unbalanced sexp"))
3196 (error (setq err (error-message-string data))))
3199 (widget-put widget :error err)
3202 (defvar widget-sexp-prompt-value-history nil
3203 "History of input to `widget-sexp-prompt-value'.")
3205 (defun widget-sexp-prompt-value (widget prompt value unbound)
3206 ;; Read an arbitrary sexp.
3207 (let ((found (read-string prompt
3208 (if unbound nil (cons (prin1-to-string value) 0))
3209 (widget-get widget :prompt-history))))
3210 (let ((answer (read-from-string found)))
3211 (unless (= (cdr answer) (length found))
3212 (error "Junk at end of expression: %s"
3213 (substring found (cdr answer))))
3216 (define-widget 'restricted-sexp 'sexp
3217 "A Lisp expression restricted to values that match.
3218 To use this type, you must define :match or :match-alternatives."
3219 :type-error "The specified value is not valid"
3220 :match 'widget-restricted-sexp-match
3221 :value-to-internal (lambda (widget value)
3222 (if (widget-apply widget :match value)
3223 (prin1-to-string value)
3226 (defun widget-restricted-sexp-match (widget value)
3227 (let ((alternatives (widget-get widget :match-alternatives))
3229 (while (and alternatives (not matched))
3230 (if (cond ((functionp (car alternatives))
3231 (funcall (car alternatives) value))
3232 ((and (consp (car alternatives))
3233 (eq (car (car alternatives)) 'quote))
3234 (eq value (nth 1 (car alternatives)))))
3236 (setq alternatives (cdr alternatives)))
3239 (define-widget 'integer 'restricted-sexp
3243 :type-error "This field should contain an integer"
3244 :match-alternatives '(integerp))
3246 (define-widget 'number 'restricted-sexp
3247 "A number (floating point or integer)."
3250 :type-error "This field should contain a number (floating point or integer)"
3251 :match-alternatives '(numberp))
3253 (define-widget 'float 'restricted-sexp
3254 "A floating point number."
3255 :tag "Floating point number"
3257 :type-error "This field should contain a floating point number"
3258 :match-alternatives '(floatp))
3260 (define-widget 'character 'editable-field
3265 :format "%{%t%}: %v\n"
3266 :valid-regexp "\\`.\\'"
3267 :error "This field should contain a single character"
3268 :value-to-internal (lambda (widget value)
3271 (char-to-string value)))
3272 :value-to-external (lambda (widget value)
3276 :match (lambda (widget value)
3277 (char-valid-p value)))
3279 (define-widget 'list 'group
3282 :format "%{%t%}:\n%v")
3284 (define-widget 'vector 'group
3287 :format "%{%t%}:\n%v"
3288 :match 'widget-vector-match
3289 :value-to-internal (lambda (widget value) (append value nil))
3290 :value-to-external (lambda (widget value) (apply 'vector value)))
3292 (defun widget-vector-match (widget value)
3293 (and (vectorp value)
3294 (widget-group-match widget
3295 (widget-apply widget :value-to-internal value))))
3297 (define-widget 'cons 'group
3300 :format "%{%t%}:\n%v"
3301 :match 'widget-cons-match
3302 :value-to-internal (lambda (widget value)
3303 (list (car value) (cdr value)))
3304 :value-to-external (lambda (widget value)
3305 (apply 'cons value)))
3307 (defun widget-cons-match (widget value)
3309 (widget-group-match widget
3310 (widget-apply widget :value-to-internal value))))
3312 ;;; The `lazy' Widget.
3314 ;; Recursive datatypes.
3316 (define-widget 'lazy 'default
3317 "Base widget for recursive datastructures.
3319 The `lazy' widget will, when instantiated, contain a single inferior
3320 widget, of the widget type specified by the :type parameter. The
3321 value of the `lazy' widget is the same as the value of the inferior
3322 widget. When deriving a new widget from the 'lazy' widget, the :type
3323 parameter is allowed to refer to the widget currently being defined,
3324 thus allowing recursive datastructures to be described.
3326 The :type parameter takes the same arguments as the defcustom
3327 parameter with the same name.
3329 Most composite widgets, i.e. widgets containing other widgets, does
3330 not allow recursion. That is, when you define a new widget type, none
3331 of the inferior widgets may be of the same type you are currently
3334 In Lisp, however, it is custom to define datastructures in terms of
3335 themselves. A list, for example, is defined as either nil, or a cons
3336 cell whose cdr itself is a list. The obvious way to translate this
3337 into a widget type would be
3339 (define-widget 'my-list 'choice
3340 \"A list of sexps.\"
3342 :args '((const nil) (cons :value (nil) sexp my-list)))
3344 Here we attempt to define my-list as a choice of either the constant
3345 nil, or a cons-cell containing a sexp and my-lisp. This will not work
3346 because the `choice' widget does not allow recursion.
3348 Using the `lazy' widget you can overcome this problem, as in this
3351 (define-widget 'sexp-list 'lazy
3352 \"A list of sexps.\"
3354 :type '(choice (const nil) (cons :value (nil) sexp sexp-list)))"
3355 :format "%{%t%}: %v"
3356 ;; We don't convert :type because we want to allow recursive
3357 ;; datastructures. This is slow, so we should not create speed
3358 ;; critical widgets by deriving from this.
3359 :convert-widget 'widget-value-convert-widget
3360 :value-create 'widget-type-value-create
3361 :value-get 'widget-child-value-get
3362 :value-inline 'widget-child-value-inline
3363 :default-get 'widget-type-default-get
3364 :match 'widget-type-match
3365 :validate 'widget-child-validate)
3368 ;;; The `plist' Widget.
3372 (define-widget 'plist 'list
3374 :key-type '(symbol :tag "Key")
3375 :value-type '(sexp :tag "Value")
3376 :convert-widget 'widget-plist-convert-widget
3379 (defvar widget-plist-value-type) ;Dynamic variable
3381 (defun widget-plist-convert-widget (widget)
3382 ;; Handle `:options'.
3383 (let* ((options (widget-get widget :options))
3384 (widget-plist-value-type (widget-get widget :value-type))
3385 (other `(editable-list :inline t
3387 ,(widget-get widget :key-type)
3388 ,widget-plist-value-type)))
3390 (list `(checklist :inline t
3392 ,@(mapcar 'widget-plist-convert-option
3396 (widget-put widget :args args)
3399 (defun widget-plist-convert-option (option)
3400 ;; Convert a single plist option.
3401 (let (key-type value-type)
3403 (let ((key (nth 0 option)))
3404 (setq value-type (nth 1 option))
3407 (setq key-type `(const ,key))))
3408 (setq key-type `(const ,option)
3409 value-type widget-plist-value-type))
3410 `(group :format "Key: %v" :inline t ,key-type ,value-type)))
3413 ;;; The `alist' Widget.
3415 ;; Association lists.
3417 (define-widget 'alist 'list
3418 "An association list."
3419 :key-type '(sexp :tag "Key")
3420 :value-type '(sexp :tag "Value")
3421 :convert-widget 'widget-alist-convert-widget
3424 (defvar widget-alist-value-type) ;Dynamic variable
3426 (defun widget-alist-convert-widget (widget)
3427 ;; Handle `:options'.
3428 (let* ((options (widget-get widget :options))
3429 (widget-alist-value-type (widget-get widget :value-type))
3430 (other `(editable-list :inline t
3432 ,(widget-get widget :key-type)
3433 ,widget-alist-value-type)))
3435 (list `(checklist :inline t
3437 ,@(mapcar 'widget-alist-convert-option
3441 (widget-put widget :args args)
3444 (defun widget-alist-convert-option (option)
3445 ;; Convert a single alist option.
3446 (let (key-type value-type)
3448 (let ((key (nth 0 option)))
3449 (setq value-type (nth 1 option))
3452 (setq key-type `(const ,key))))
3453 (setq key-type `(const ,option)
3454 value-type widget-alist-value-type))
3455 `(cons :format "Key: %v" ,key-type ,value-type)))
3457 (define-widget 'choice 'menu-choice
3458 "A union of several sexp types."
3460 :format "%{%t%}: %[Value Menu%] %v"
3461 :button-prefix 'widget-push-button-prefix
3462 :button-suffix 'widget-push-button-suffix
3463 :prompt-value 'widget-choice-prompt-value)
3465 (defun widget-choice-prompt-value (widget prompt value unbound)
3467 (let ((args (widget-get widget :args))
3468 (completion-ignore-case (widget-get widget :case-fold))
3469 current choices old)
3470 ;; Find the first arg that matches VALUE.
3473 (if (widget-apply (car look) :match value)
3474 (setq old (car look)
3476 (setq look (cdr look)))))
3479 (cond ((= (length args) 0)
3481 ((= (length args) 1)
3483 ((and (= (length args) 2)
3485 (if (eq old (nth 0 args))
3490 (setq current (car args)
3493 (cons (cons (widget-apply current :menu-tag-get)
3496 (let ((val (completing-read prompt choices nil t)))
3498 (let ((try (try-completion val choices)))
3501 (cdr (assoc val choices)))
3504 (widget-prompt-value current prompt nil t)
3507 (define-widget 'radio 'radio-button-choice
3508 "A union of several sexp types."
3510 :format "%{%t%}:\n%v"
3511 :prompt-value 'widget-choice-prompt-value)
3513 (define-widget 'repeat 'editable-list
3514 "A variable length homogeneous list."
3516 :format "%{%t%}:\n%v%i\n")
3518 (define-widget 'set 'checklist
3519 "A list of members from a fixed set."
3521 :format "%{%t%}:\n%v")
3523 (define-widget 'boolean 'toggle
3524 "To be nil or non-nil, that is the question."
3526 :prompt-value 'widget-boolean-prompt-value
3527 :button-prefix 'widget-push-button-prefix
3528 :button-suffix 'widget-push-button-suffix
3529 :format "%{%t%}: %[Toggle%] %v\n"
3533 (defun widget-boolean-prompt-value (widget prompt value unbound)
3534 ;; Toggle a boolean.
3537 ;;; The `color' Widget.
3540 (define-widget 'color 'editable-field
3541 "Choose a color name (with sample)."
3542 :format "%t: %v (%{sample%})\n"
3546 :complete 'widget-color-complete
3547 :sample-face-get 'widget-color-sample-face-get
3548 :notify 'widget-color-notify
3549 :action 'widget-color-action)
3551 (defun widget-color-complete (widget)
3552 "Complete the color in WIDGET."
3553 (require 'facemenu) ; for facemenu-color-alist
3554 (let* ((prefix (buffer-substring-no-properties (widget-field-start widget)
3556 (list (or facemenu-color-alist (defined-colors)))
3557 (completion (try-completion prefix list)))
3558 (cond ((eq completion t)
3559 (message "Exact match."))
3561 (error "Can't find completion for \"%s\"" prefix))
3562 ((not (string-equal prefix completion))
3563 (insert-and-inherit (substring completion (length prefix))))
3565 (message "Making completion list...")
3566 (with-output-to-temp-buffer "*Completions*"
3567 (display-completion-list (all-completions prefix list nil)))
3568 (message "Making completion list...done")))))
3570 (defun widget-color-sample-face-get (widget)
3571 (let* ((value (condition-case nil
3572 (widget-value widget)
3573 (error (widget-get widget :value)))))
3574 (if (color-defined-p value)
3575 (list (cons 'foreground-color value))
3578 (defun widget-color-action (widget &optional event)
3579 "Prompt for a color."
3580 (let* ((tag (widget-apply widget :menu-tag-get))
3581 (prompt (concat tag ": "))
3582 (value (widget-value widget))
3583 (start (widget-field-start widget))
3584 (answer (facemenu-read-color prompt)))
3585 (unless (zerop (length answer))
3586 (widget-value-set widget answer)
3588 (widget-apply widget :notify widget event))))
3590 (defun widget-color-notify (widget child &optional event)
3591 "Update the sample, and notofy the parent."
3592 (overlay-put (widget-get widget :sample-overlay)
3593 'face (widget-apply widget :sample-face-get))
3594 (widget-default-notify widget child event))
3598 (defun widget-echo-help (pos)
3599 "Display help-echo text for widget at POS."
3600 (let* ((widget (widget-at pos))
3601 (help-echo (and widget (widget-get widget :help-echo))))
3602 (if (functionp help-echo)
3603 (setq help-echo (funcall help-echo widget)))
3604 (if help-echo (message "%s" (eval help-echo)))))
3610 ;;; arch-tag: a076e75e-18a1-4b46-8be5-3f317bcbc707
3611 ;;; wid-edit.el ends here