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
93 "Face used for documentation strings in widgets.
94 This exists as a variable so it can be set locally in certain buffers.")
96 (defface widget-documentation
'((((class color
)
98 (:foreground
"lime green"))
101 (:foreground
"dark green"))
103 "Face used for documentation text."
104 :group
'widget-documentation
105 :group
'widget-faces
)
106 ;; backward compatibility alias
107 (put 'widget-documentation-face
'face-alias
'widget-documentation
)
109 (defvar widget-button-face
'widget-button
110 "Face used for buttons in widgets.
111 This exists as a variable so it can be set locally in certain buffers.")
113 (defface widget-button
'((t (:weight bold
)))
114 "Face used for widget buttons."
115 :group
'widget-faces
)
116 ;; backward compatibility alias
117 (put 'widget-button-face
'face-alias
'widget-button
)
119 (defcustom widget-mouse-face
'highlight
120 "Face used for widget buttons when the mouse is above them."
122 :group
'widget-faces
)
124 ;; TTY gets special definitions here and in the next defface, because
125 ;; the gray colors defined for other displays cause black text on a black
126 ;; background, at least on light-background TTYs.
127 (defface widget-field
'((((type tty
))
128 :background
"yellow3"
130 (((class grayscale color
)
132 :background
"gray85")
133 (((class grayscale color
)
135 :background
"dim gray")
138 "Face used for editable fields."
139 :group
'widget-faces
)
140 ;; backward-compatibility alias
141 (put 'widget-field-face
'face-alias
'widget-field
)
143 (defface widget-single-line-field
'((((type tty
))
146 (((class grayscale color
)
148 :background
"gray85")
149 (((class grayscale color
)
151 :background
"dim gray")
154 "Face used for editable fields spanning only a single line."
155 :group
'widget-faces
)
156 ;; backward-compatibility alias
157 (put 'widget-single-line-field-face
'face-alias
'widget-single-line-field
)
159 ;;; This causes display-table to be loaded, and not usefully.
160 ;;;(defvar widget-single-line-display-table
161 ;;; (let ((table (make-display-table)))
162 ;;; (aset table 9 "^I")
163 ;;; (aset table 10 "^J")
165 ;;; "Display table used for single-line editable fields.")
167 ;;;(when (fboundp 'set-face-display-table)
168 ;;; (set-face-display-table 'widget-single-line-field-face
169 ;;; widget-single-line-display-table))
171 ;;; Utility functions.
173 ;; These are not really widget specific.
175 (defun widget-princ-to-string (object)
176 "Return string representation of OBJECT, any Lisp object.
177 No quoting characters are used; no delimiters are printed around
178 the contents of strings."
179 (with-output-to-string
182 (defun widget-clear-undo ()
183 "Clear all undo information."
184 (buffer-disable-undo (current-buffer))
185 (buffer-enable-undo))
187 (defcustom widget-menu-max-size
40
188 "Largest number of items allowed in a popup-menu.
189 Larger menus are read through the minibuffer."
193 (defcustom widget-menu-max-shortcuts
40
194 "Largest number of items for which it works to choose one with a character.
195 For a larger number of items, the minibuffer is used."
199 (defcustom widget-menu-minibuffer-flag nil
200 "*Control how to ask for a choice from the keyboard.
201 Non-nil means use the minibuffer;
202 nil means read a single character."
206 (defun widget-choose (title items
&optional event
)
207 "Choose an item from a list.
209 First argument TITLE is the name of the list.
210 Second argument ITEMS is a list whose members are either
211 (NAME . VALUE), to indicate selectable items, or just strings to
212 indicate unselectable items.
213 Optional third argument EVENT is an input event.
215 The user is asked to choose between each NAME from the items alist,
216 and the VALUE of the chosen element will be returned. If EVENT is a
217 mouse event, and the number of elements in items is less than
218 `widget-menu-max-size', a popup menu will be used, otherwise the
220 (cond ((and (< (length items
) widget-menu-max-size
)
221 event
(display-popup-menus-p))
224 (list title
(cons "" items
))))
225 ((or widget-menu-minibuffer-flag
226 (> (length items
) widget-menu-max-shortcuts
))
227 ;; Read the choice of name from the minibuffer.
228 (setq items
(widget-remove-if 'stringp items
))
229 (let ((val (completing-read (concat title
": ") items nil t
)))
231 (let ((try (try-completion val items
)))
234 (cdr (assoc val items
))))))
236 ;; Construct a menu of the choices
237 ;; and then use it for prompting for a single character.
238 (let* ((overriding-terminal-local-map (make-sparse-keymap))
240 map choice some-choice-enabled value
)
241 ;; Define SPC as a prefix char to get to this menu.
242 (define-key overriding-terminal-local-map
" "
243 (setq map
(make-sparse-keymap title
)))
244 (with-current-buffer (get-buffer-create " widget-choose")
246 (insert "Available choices:\n\n")
248 (setq choice
(car items
) items
(cdr items
))
250 (let* ((name (car choice
))
251 (function (cdr choice
)))
252 (insert (format "%c = %s\n" next-digit name
))
253 (define-key map
(vector next-digit
) function
)
254 (setq some-choice-enabled t
)))
255 ;; Allocate digits to disabled alternatives
256 ;; so that the digit of a given alternative never varies.
257 (setq next-digit
(1+ next-digit
)))
258 (insert "\nC-g = Quit"))
259 (or some-choice-enabled
260 (error "None of the choices is currently meaningful"))
261 (define-key map
[?\C-g
] 'keyboard-quit
)
262 (define-key map
[t] 'keyboard-quit)
263 (define-key map [?\M-\C-v] 'scroll-other-window)
264 (define-key map [?\M--] 'negative-argument)
265 (setcdr map (nreverse (cdr map)))
266 ;; Read a char with the menu, and return the result
267 ;; that corresponds to it.
268 (save-window-excursion
269 (let ((buf (get-buffer " widget-choose")))
270 (fit-window-to-buffer (display-buffer buf))
271 (let ((cursor-in-echo-area t)
275 (while (not (or (and (>= char ?0) (< char next-digit))
276 (eq value 'keyboard-quit)))
277 ;; Unread a SPC to lead to our new menu.
278 (setq unread-command-events (cons ?\ unread-command-events))
279 (setq keys (read-key-sequence title))
281 (lookup-key overriding-terminal-local-map keys t)
282 char (string-to-char (substring keys 1)))
283 (cond ((eq value 'scroll-other-window)
284 (let ((minibuffer-scroll-window
285 (get-buffer-window buf)))
287 (scroll-other-window-down
288 (window-height minibuffer-scroll-window))
289 (scroll-other-window))
291 ((eq value 'negative-argument)
295 (when (eq value 'keyboard-quit)
299 (defun widget-remove-if (predictate list)
300 (let (result (tail list))
302 (or (funcall predictate (car tail))
303 (setq result (cons (car tail) result)))
304 (setq tail (cdr tail)))
307 ;;; Widget text specifications.
309 ;; These functions are for specifying text properties.
311 ;; We can set it to nil now that get_local_map uses get_pos_property.
312 (defconst widget-field-add-space nil
313 "Non-nil means add extra space at the end of editable text fields.
314 If you don't add the space, it will become impossible to edit a zero
317 (defvar widget-field-use-before-change t
318 "Non-nil means use `before-change-functions' to track editable fields.
319 This enables the use of undo, but doesn't work on Emacs 19.34 and earlier.
320 Using before hooks also means that the :notify function can't know the
323 (defun widget-specify-field (widget from to)
324 "Specify editable button for WIDGET between FROM and TO."
325 ;; Terminating space is not part of the field, but necessary in
326 ;; order for local-map to work. Remove next sexp if local-map works
327 ;; at the end of the overlay.
330 (cond ((null (widget-get widget :size))
332 (widget-field-add-space
333 (insert-and-inherit " ")))
335 (let ((keymap (widget-get widget :keymap))
336 (face (or (widget-get widget :value-face) 'widget-field))
337 (help-echo (widget-get widget :help-echo))
338 (follow-link (widget-get widget :follow-link))
340 (or (not widget-field-add-space) (widget-get widget :size))))
341 (if (functionp help-echo)
342 (setq help-echo 'widget-mouse-help))
343 (when (= (char-before to) ?\n)
344 ;; When the last character in the field is a newline, we want to
345 ;; give it a `field' char-property of `boundary', which helps the
346 ;; C-n/C-p act more naturally when entering/leaving the field. We
347 ;; do this by making a small secondary overlay to contain just that
349 (let ((overlay (make-overlay (1- to) to nil t nil)))
350 (overlay-put overlay 'field 'boundary)
351 ;; We need the real field for tabbing.
352 (overlay-put overlay 'real-field widget)
353 ;; Use `local-map' here, not `keymap', so that normal editing
354 ;; works in the field when, say, Custom uses `suppress-keymap'.
355 (overlay-put overlay 'local-map keymap)
356 (overlay-put overlay 'face face)
357 (overlay-put overlay 'follow-link follow-link)
358 (overlay-put overlay 'help-echo help-echo))
360 (setq rear-sticky t))
361 (let ((overlay (make-overlay from to nil nil rear-sticky)))
362 (widget-put widget :field-overlay overlay)
363 ;;(overlay-put overlay 'detachable nil)
364 (overlay-put overlay 'field widget)
365 (overlay-put overlay 'local-map keymap)
366 (overlay-put overlay 'face face)
367 (overlay-put overlay 'follow-link follow-link)
368 (overlay-put overlay 'help-echo help-echo)))
369 (widget-specify-secret widget))
371 (defun widget-specify-secret (field)
372 "Replace text in FIELD with value of `:secret', if non-nil."
373 (let ((secret (widget-get field :secret))
374 (size (widget-get field :size)))
376 (let ((begin (widget-field-start field))
377 (end (widget-field-end field)))
379 (while (and (> end begin)
380 (eq (char-after (1- end)) ?\ ))
381 (setq end (1- end))))
383 (let ((old (char-after begin)))
384 (unless (eq old secret)
385 (subst-char-in-region begin (1+ begin) old secret)
386 (put-text-property begin (1+ begin) 'secret old))
387 (setq begin (1+ begin))))))))
389 (defun widget-specify-button (widget from to)
390 "Specify button for WIDGET between FROM and TO."
391 (let ((overlay (make-overlay from to nil t nil))
392 (follow-link (widget-get widget :follow-link))
393 (help-echo (widget-get widget :help-echo)))
394 (widget-put widget :button-overlay overlay)
395 (if (functionp help-echo)
396 (setq help-echo 'widget-mouse-help))
397 (overlay-put overlay 'button widget)
398 (overlay-put overlay 'keymap (widget-get widget :keymap))
399 (overlay-put overlay 'evaporate t)
400 ;; We want to avoid the face with image buttons.
401 (unless (widget-get widget :suppress-face)
402 (overlay-put overlay 'face (widget-apply widget :button-face-get))
403 ; Text terminals cannot change mouse pointer shape, so use mouse
405 (or (display-graphic-p)
406 (overlay-put overlay 'mouse-face widget-mouse-face)))
407 (overlay-put overlay 'pointer 'hand)
408 (overlay-put overlay 'follow-link follow-link)
409 (overlay-put overlay 'help-echo help-echo)))
411 (defun widget-mouse-help (window overlay point)
412 "Help-echo callback for widgets whose :help-echo is a function."
413 (with-current-buffer (overlay-buffer overlay)
414 (let* ((widget (widget-at (overlay-start overlay)))
415 (help-echo (if widget (widget-get widget :help-echo))))
416 (if (functionp help-echo)
417 (funcall help-echo widget)
420 (defun widget-specify-sample (widget from to)
421 "Specify sample for WIDGET between FROM and TO."
422 (let ((overlay (make-overlay from to nil t nil)))
423 (overlay-put overlay 'face (widget-apply widget :sample-face-get))
424 (overlay-put overlay 'evaporate t)
425 (widget-put widget :sample-overlay overlay)))
427 (defun widget-specify-doc (widget from to)
428 "Specify documentation for WIDGET between FROM and TO."
429 (let ((overlay (make-overlay from to nil t nil)))
430 (overlay-put overlay 'widget-doc widget)
431 (overlay-put overlay 'face widget-documentation-face)
432 (overlay-put overlay 'evaporate t)
433 (widget-put widget :doc-overlay overlay)))
435 (defmacro widget-specify-insert (&rest form)
436 "Execute FORM without inheriting any text properties."
438 (let ((inhibit-read-only t)
439 (inhibit-modification-hooks t))
440 (narrow-to-region (point) (point))
441 (prog1 (progn ,@form)
442 (goto-char (point-max))))))
444 (defface widget-inactive '((((class grayscale color)
446 (:foreground "light gray"))
447 (((class grayscale color)
449 (:foreground "dim gray"))
452 "Face used for inactive widgets."
453 :group 'widget-faces)
454 ;; backward-compatibility alias
455 (put 'widget-inactive-face 'face-alias 'widget-inactive)
457 (defun widget-specify-inactive (widget from to)
458 "Make WIDGET inactive for user modifications."
459 (unless (widget-get widget :inactive)
460 (let ((overlay (make-overlay from to nil t nil)))
461 (overlay-put overlay 'face 'widget-inactive)
462 ;; This is disabled, as it makes the mouse cursor change shape.
463 ;; (overlay-put overlay 'mouse-face 'widget-inactive)
464 (overlay-put overlay 'evaporate t)
465 (overlay-put overlay 'priority 100)
466 (overlay-put overlay 'modification-hooks '(widget-overlay-inactive))
467 (widget-put widget :inactive overlay))))
469 (defun widget-overlay-inactive (&rest junk)
470 "Ignoring the arguments, signal an error."
471 (unless inhibit-read-only
472 (error "The widget here is not active")))
475 (defun widget-specify-active (widget)
476 "Make WIDGET active for user modifications."
477 (let ((inactive (widget-get widget :inactive)))
479 (delete-overlay inactive)
480 (widget-put widget :inactive nil))))
482 ;;; Widget Properties.
484 (defsubst widget-type (widget)
485 "Return the type of WIDGET, a symbol."
489 (defun widgetp (widget)
490 "Return non-nil iff WIDGET is a widget."
492 (get widget 'widget-type)
494 (symbolp (car widget))
495 (get (car widget) 'widget-type))))
497 (defun widget-get-indirect (widget property)
498 "In WIDGET, get the value of PROPERTY.
499 If the value is a symbol, return its binding.
500 Otherwise, just return the value."
501 (let ((value (widget-get widget property)))
506 (defun widget-member (widget property)
507 "Non-nil iff there is a definition in WIDGET for PROPERTY."
508 (cond ((plist-member (cdr widget) property)
511 (widget-member (get (car widget) 'widget-type) property))
514 (defun widget-value (widget)
515 "Extract the current value of WIDGET."
517 :value-to-external (widget-apply widget :value-get)))
519 (defun widget-value-set (widget value)
520 "Set the current value of WIDGET to VALUE."
522 :value-set (widget-apply widget
523 :value-to-internal value)))
525 (defun widget-default-get (widget)
526 "Extract the default external value of WIDGET."
527 (widget-apply widget :value-to-external
528 (or (widget-get widget :value)
529 (widget-apply widget :default-get))))
531 (defun widget-match-inline (widget vals)
532 "In WIDGET, match the start of VALS."
533 (cond ((widget-get widget :inline)
534 (widget-apply widget :match-inline vals))
536 (widget-apply widget :match (car vals)))
537 (cons (list (car vals)) (cdr vals)))
540 (defun widget-apply-action (widget &optional event)
541 "Apply :action in WIDGET in response to EVENT."
542 (if (widget-apply widget :active)
543 (widget-apply widget :action event)
544 (error "Attempt to perform action on inactive widget")))
546 ;;; Helper functions.
548 ;; These are widget specific.
551 (defun widget-prompt-value (widget prompt &optional value unbound)
552 "Prompt for a value matching WIDGET, using PROMPT.
553 The current value is assumed to be VALUE, unless UNBOUND is non-nil."
554 (unless (listp widget)
555 (setq widget (list widget)))
556 (setq prompt (format "[%s] %s" (widget-type widget) prompt))
557 (setq widget (widget-convert widget))
558 (let ((answer (widget-apply widget :prompt-value prompt value unbound)))
559 (unless (widget-apply widget :match answer)
560 (error "Value does not match %S type" (car widget)))
563 (defun widget-get-sibling (widget)
564 "Get the item WIDGET is assumed to toggle.
565 This is only meaningful for radio buttons or checkboxes in a list."
566 (let* ((children (widget-get (widget-get widget :parent) :children))
570 (setq child (car children)
571 children (cdr children))
572 (when (eq (widget-get child :button) widget)
573 (throw 'child child)))
576 (defun widget-map-buttons (function &optional buffer maparg)
577 "Map FUNCTION over the buttons in BUFFER.
578 FUNCTION is called with the arguments WIDGET and MAPARG.
580 If FUNCTION returns non-nil, the walk is cancelled.
582 The arguments MAPARG, and BUFFER default to nil and (current-buffer),
584 (let ((cur (point-min))
587 (with-current-buffer buffer (overlay-lists))
589 (setq overlays (append (car overlays) (cdr overlays)))
590 (while (setq cur (pop overlays))
591 (setq widget (overlay-get cur 'button))
592 (if (and widget (funcall function widget maparg))
593 (setq overlays nil)))))
597 (defcustom widget-image-directory (file-name-as-directory
598 (expand-file-name "custom" data-directory))
599 "Where widget button images are located.
600 If this variable is nil, widget will try to locate the directory
605 (defcustom widget-image-enable t
606 "If non nil, use image buttons in widgets when available."
611 (defcustom widget-image-conversion
612 '((xpm ".xpm") (gif ".gif") (png ".png") (jpeg ".jpg" ".jpeg")
614 "Conversion alist from image formats to file name suffixes."
616 :type '(repeat (cons :format "%v"
617 (symbol :tag "Image Format" unknown)
618 (repeat :tag "Suffixes"
619 (string :format "%v")))))
621 (defun widget-image-find (image)
622 "Create a graphical button from IMAGE.
623 IMAGE should either already be an image, or be a file name sans
624 extension (xpm, xbm, gif, jpg, or png) located in
625 `widget-image-directory' or otherwise where `find-image' will find it."
626 (cond ((not (and image widget-image-enable (display-graphic-p)))
627 ;; We don't want or can't use images.
630 (eq 'image (car image)))
631 ;; Already an image spec. Use it.
634 ;; A string. Look it up in relevant directories.
635 (let* ((load-path (cons widget-image-directory load-path))
637 (dolist (elt widget-image-conversion)
638 (dolist (ext (cdr elt))
639 (push (list :type (car elt) :file (concat image ext)) specs)))
640 (setq specs (nreverse specs))
646 (defvar widget-button-pressed-face 'widget-button-pressed
647 "Face used for pressed buttons in widgets.
648 This exists as a variable so it can be set locally in certain
651 (defun widget-image-insert (widget tag image &optional down inactive)
652 "In WIDGET, insert the text TAG or, if supported, IMAGE.
653 IMAGE should either be an image or an image file name sans extension
654 \(xpm, xbm, gif, jpg, or png) located in `widget-image-directory'.
656 Optional arguments DOWN and INACTIVE are used instead of IMAGE when the
657 button is pressed or inactive, respectively. These are currently ignored."
658 (if (and (display-graphic-p)
659 (setq image (widget-image-find image)))
660 (progn (widget-put widget :suppress-face t)
663 tag 'mouse-face widget-button-pressed-face)))
668 (defgroup widget-button nil
669 "The look of various kinds of buttons."
672 (defcustom widget-button-prefix ""
673 "String used as prefix for buttons."
675 :group 'widget-button)
677 (defcustom widget-button-suffix ""
678 "String used as suffix for buttons."
680 :group 'widget-button)
682 ;;; Creating Widgets.
685 (defun widget-create (type &rest args)
686 "Create widget of TYPE.
687 The optional ARGS are additional keyword arguments."
688 (let ((widget (apply 'widget-convert type args)))
689 (widget-apply widget :create)
692 (defun widget-create-child-and-convert (parent type &rest args)
693 "As part of the widget PARENT, create a child widget TYPE.
694 The child is converted, using the keyword arguments ARGS."
695 (let ((widget (apply 'widget-convert type args)))
696 (widget-put widget :parent parent)
697 (unless (widget-get widget :indent)
698 (widget-put widget :indent (+ (or (widget-get parent :indent) 0)
699 (or (widget-get widget :extra-offset) 0)
700 (widget-get parent :offset))))
701 (widget-apply widget :create)
704 (defun widget-create-child (parent type)
705 "Create widget of TYPE."
706 (let ((widget (widget-copy type)))
707 (widget-put widget :parent parent)
708 (unless (widget-get widget :indent)
709 (widget-put widget :indent (+ (or (widget-get parent :indent) 0)
710 (or (widget-get widget :extra-offset) 0)
711 (widget-get parent :offset))))
712 (widget-apply widget :create)
715 (defun widget-create-child-value (parent type value)
716 "Create widget of TYPE with value VALUE."
717 (let ((widget (widget-copy type)))
718 (widget-put widget :value (widget-apply widget :value-to-internal value))
719 (widget-put widget :parent parent)
720 (unless (widget-get widget :indent)
721 (widget-put widget :indent (+ (or (widget-get parent :indent) 0)
722 (or (widget-get widget :extra-offset) 0)
723 (widget-get parent :offset))))
724 (widget-apply widget :create)
728 (defun widget-delete (widget)
730 (widget-apply widget :delete))
732 (defun widget-copy (widget)
733 "Make a deep copy of WIDGET."
734 (widget-apply (copy-sequence widget) :copy))
736 (defun widget-convert (type &rest args)
737 "Convert TYPE to a widget without inserting it in the buffer.
738 The optional ARGS are additional keyword arguments."
739 ;; Don't touch the type.
740 (let* ((widget (if (symbolp type)
742 (copy-sequence type)))
746 ;; First set the :args keyword.
747 (while (cdr current) ;Look in the type.
748 (if (and (keywordp (cadr current))
749 ;; If the last element is a keyword,
750 ;; it is still the :args element,
751 ;; even though it is a keyword.
753 (if (eq (cadr current) :args)
754 ;; If :args is explicitly specified, obey it.
756 ;; Some other irrelevant keyword.
757 (setq current (cdr (cdr current))))
758 (setcdr current (list :args (cdr current)))
760 (while (and args (not done)) ;Look in ARGS.
761 (cond ((eq (car args) :args)
762 ;; Handle explicit specification of :args.
763 (setq args (cadr args)
765 ((keywordp (car args))
766 (setq args (cddr args)))
769 (widget-put widget :args args))
770 ;; Then Convert the widget.
773 (let ((convert-widget (plist-get (cdr type) :convert-widget)))
775 (setq widget (funcall convert-widget widget))))
776 (setq type (get (car type) 'widget-type)))
777 ;; Finally set the keyword args.
779 (let ((next (nth 0 keys)))
782 (widget-put widget next (nth 1 keys))
783 (setq keys (nthcdr 2 keys)))
785 ;; Convert the :value to internal format.
786 (if (widget-member widget :value)
788 :value (widget-apply widget
790 (widget-get widget :value))))
791 ;; Return the newly create widget.
795 (defun widget-insert (&rest args)
796 "Call `insert' with ARGS even if surrounding text is read only."
797 (let ((inhibit-read-only t)
798 (inhibit-modification-hooks t))
799 (apply 'insert args)))
801 (defun widget-convert-text (type from to
802 &optional button-from button-to
804 "Return a widget of type TYPE with endpoint FROM TO.
805 Optional ARGS are extra keyword arguments for TYPE.
806 and TO will be used as the widgets end points. If optional arguments
807 BUTTON-FROM and BUTTON-TO are given, these will be used as the widgets
809 Optional ARGS are extra keyword arguments for TYPE."
810 (let ((widget (apply 'widget-convert type :delete 'widget-leave-text args))
811 (from (copy-marker from))
812 (to (copy-marker to)))
813 (set-marker-insertion-type from t)
814 (set-marker-insertion-type to nil)
815 (widget-put widget :from from)
816 (widget-put widget :to to)
818 (widget-specify-button widget button-from button-to))
821 (defun widget-convert-button (type from to &rest args)
822 "Return a widget of type TYPE with endpoint FROM TO.
823 Optional ARGS are extra keyword arguments for TYPE.
824 No text will be inserted to the buffer, instead the text between FROM
825 and TO will be used as the widgets end points, as well as the widgets
827 (apply 'widget-convert-text type from to from to args))
829 (defun widget-leave-text (widget)
830 "Remove markers and overlays from WIDGET and its children."
831 (let ((button (widget-get widget :button-overlay))
832 (sample (widget-get widget :sample-overlay))
833 (doc (widget-get widget :doc-overlay))
834 (field (widget-get widget :field-overlay)))
835 (set-marker (widget-get widget :from) nil)
836 (set-marker (widget-get widget :to) nil)
838 (delete-overlay button))
840 (delete-overlay sample))
842 (delete-overlay doc))
844 (delete-overlay field))
845 (mapc 'widget-leave-text (widget-get widget :children))))
847 ;;; Keymap and Commands.
850 (defvar widget-keymap
851 (let ((map (make-sparse-keymap)))
852 (define-key map "\t" 'widget-forward)
853 (define-key map [(shift tab)] 'widget-backward)
854 (define-key map [backtab] 'widget-backward)
855 (define-key map [down-mouse-2] 'widget-button-click)
856 (define-key map "\C-m" 'widget-button-press)
858 "Keymap containing useful binding for buffers containing widgets.
859 Recommended as a parent keymap for modes using widgets.")
861 (defvar widget-global-map global-map
862 "Keymap used for events a widget does not handle itself.")
863 (make-variable-buffer-local 'widget-global-map)
865 (defvar widget-field-keymap
866 (let ((map (copy-keymap widget-keymap)))
867 (define-key map "\C-k" 'widget-kill-line)
868 (define-key map "\M-\t" 'widget-complete)
869 (define-key map "\C-m" 'widget-field-activate)
870 ;; Since the widget code uses a `field' property to identify fields,
871 ;; ordinary beginning-of-line does the right thing.
872 ;; (define-key map "\C-a" 'widget-beginning-of-line)
873 (define-key map "\C-e" 'widget-end-of-line)
875 "Keymap used inside an editable field.")
877 (defvar widget-text-keymap
878 (let ((map (copy-keymap widget-keymap)))
879 ;; Since the widget code uses a `field' property to identify fields,
880 ;; ordinary beginning-of-line does the right thing.
881 ;; (define-key map "\C-a" 'widget-beginning-of-line)
882 (define-key map "\C-e" 'widget-end-of-line)
884 "Keymap used inside a text field.")
886 (defun widget-field-activate (pos &optional event)
887 "Invoke the editable field at point."
889 (let ((field (widget-field-at pos)))
891 (widget-apply-action field event)
893 (lookup-key widget-global-map (this-command-keys))))))
895 (defface widget-button-pressed
896 '((((min-colors 88) (class color))
897 (:foreground "red1"))
901 (:weight bold :underline t)))
902 "Face used for pressed buttons."
903 :group 'widget-faces)
904 ;; backward-compatibility alias
905 (put 'widget-button-pressed-face 'face-alias 'widget-button-pressed)
907 (defun widget-button-click (event)
908 "Invoke the button that the mouse is pointing at."
910 (if (widget-event-point event)
911 (let* ((pos (widget-event-point event))
912 (start (event-start event))
913 (button (get-char-property
914 pos 'button (and (windowp (posn-window start))
915 (window-buffer (posn-window start))))))
917 ;; Mouse click on a widget button. Do the following
918 ;; in a save-excursion so that the click on the button
919 ;; doesn't change point.
920 (save-selected-window
921 (select-window (posn-window (event-start event)))
923 (goto-char (posn-point (event-start event)))
924 (let* ((overlay (widget-get button :button-overlay))
925 (face (overlay-get overlay 'face))
926 (mouse-face (overlay-get overlay 'mouse-face)))
928 ;; Read events, including mouse-movement events
929 ;; until we receive a release event. Highlight/
930 ;; unhighlight the button the mouse was initially
931 ;; on when we move over it.
933 (when face ; avoid changing around image
935 'face widget-button-pressed-face)
937 'mouse-face widget-button-pressed-face))
938 (unless (widget-apply button :mouse-down-action event)
939 (let ((track-mouse t))
940 (while (not (widget-button-release-event-p event))
941 (setq event (read-event)
942 pos (widget-event-point event))
944 (eq (get-char-property pos 'button)
949 widget-button-pressed-face)
952 widget-button-pressed-face))
953 (overlay-put overlay 'face face)
954 (overlay-put overlay 'mouse-face mouse-face)))))
956 ;; When mouse is released over the button, run
957 ;; its action function.
959 (eq (get-char-property pos 'button) button))
960 (widget-apply-action button event)))
961 (overlay-put overlay 'face face)
962 (overlay-put overlay 'mouse-face mouse-face))))
964 (unless (pos-visible-in-window-p (widget-event-point event))
965 (mouse-set-point event)
970 (let ((up t) command)
971 ;; Mouse click not on a widget button. Find the global
972 ;; command to run, and check whether it is bound to an
974 (mouse-set-point event)
975 (if (memq (event-basic-type event) '(mouse-1 down-mouse-1))
976 (cond ((setq command ;down event
977 (lookup-key widget-global-map [down-mouse-1]))
979 ((setq command ;up event
980 (lookup-key widget-global-map [mouse-1]))))
981 (cond ((setq command ;down event
982 (lookup-key widget-global-map [down-mouse-2]))
984 ((setq command ;up event
985 (lookup-key widget-global-map [mouse-2])))))
987 ;; Don't execute up events twice.
988 (while (not (widget-button-release-event-p event))
989 (setq event (read-event))))
991 (call-interactively command)))))
992 (message "You clicked somewhere weird.")))
994 (defun widget-button-press (pos &optional event)
995 "Invoke button at POS."
997 (let ((button (get-char-property pos 'button)))
999 (widget-apply-action button event)
1000 (let ((command (lookup-key widget-global-map (this-command-keys))))
1001 (when (commandp command)
1002 (call-interactively command))))))
1004 (defun widget-tabable-at (&optional pos)
1005 "Return the tabable widget at POS, or nil.
1006 POS defaults to the value of (point)."
1007 (let ((widget (widget-at pos)))
1009 (let ((order (widget-get widget :tab-order)))
1015 (defvar widget-use-overlay-change t
1016 "If non-nil, use overlay change functions to tab around in the buffer.
1017 This is much faster, but doesn't work reliably on Emacs 19.34.")
1019 (defun widget-move (arg)
1020 "Move point to the ARG next field or button.
1021 ARG may be negative to move backward."
1022 (or (bobp) (> arg 0) (backward-char))
1025 (old (widget-tabable-at)))
1029 (goto-char (point-min))
1030 (setq wrapped (1+ wrapped)))
1031 (widget-use-overlay-change
1032 (goto-char (next-overlay-change (point))))
1037 (error "No buttons or fields found"))
1038 (let ((new (widget-tabable-at)))
1040 (unless (eq new old)
1046 (goto-char (point-max))
1047 (setq wrapped (1+ wrapped)))
1048 (widget-use-overlay-change
1049 (goto-char (previous-overlay-change (point))))
1054 (error "No buttons or fields found"))
1055 (let ((new (widget-tabable-at)))
1057 (unless (eq new old)
1058 (setq arg (1+ arg))))))
1059 (let ((new (widget-tabable-at)))
1060 (while (eq (widget-tabable-at) new)
1063 (widget-echo-help (point))
1064 (run-hooks 'widget-move-hook))
1066 (defun widget-forward (arg)
1067 "Move point to the next field or button.
1068 With optional ARG, move across that many fields."
1070 (run-hooks 'widget-forward-hook)
1073 (defun widget-backward (arg)
1074 "Move point to the previous field or button.
1075 With optional ARG, move across that many fields."
1077 (run-hooks 'widget-backward-hook)
1078 (widget-move (- arg)))
1080 ;; Since the widget code uses a `field' property to identify fields,
1081 ;; ordinary beginning-of-line does the right thing.
1082 (defalias 'widget-beginning-of-line 'beginning-of-line)
1084 (defun widget-end-of-line ()
1085 "Go to end of field or end of line, whichever is first.
1086 Trailing spaces at the end of padded fields are not considered part of
1089 ;; Ordinary end-of-line does the right thing, because we're inside
1090 ;; text with a `field' property.
1093 ;; ... except that we want to ignore trailing spaces in fields that
1094 ;; aren't terminated by a newline, because they are used as padding,
1095 ;; and ignored when extracting the entered value of the field.
1096 (skip-chars-backward " " (field-beginning (1- (point))))))
1098 (defun widget-kill-line ()
1099 "Kill to end of field or end of line, whichever is first."
1101 (let* ((field (widget-field-find (point)))
1102 (end (and field (widget-field-end field))))
1103 (if (and field (> (line-beginning-position 2) end))
1104 (kill-region (point) end)
1105 (call-interactively 'kill-line))))
1107 (defcustom widget-complete-field (lookup-key global-map "\M-\t")
1108 "Default function to call for completion inside fields."
1109 :options '(ispell-complete-word complete-tag lisp-complete-symbol)
1113 (defun widget-narrow-to-field ()
1116 (let ((field (widget-field-find (point))))
1118 (narrow-to-region (line-beginning-position) (line-end-position)))))
1120 (defun widget-complete ()
1121 "Complete content of editable field from point.
1122 When not inside a field, move to the previous button or field."
1124 (let ((field (widget-field-find (point))))
1127 (widget-narrow-to-field)
1128 (widget-apply field :complete))
1129 (error "Not in an editable field"))))
1131 ;;; Setting up the buffer.
1133 (defvar widget-field-new nil
1134 "List of all newly created editable fields in the buffer.")
1135 (make-variable-buffer-local 'widget-field-new)
1137 (defvar widget-field-list nil
1138 "List of all editable fields in the buffer.")
1139 (make-variable-buffer-local 'widget-field-list)
1141 (defun widget-at (&optional pos)
1142 "The button or field at POS (default, point)."
1143 (or (get-char-property (or pos (point)) 'button)
1144 (widget-field-at pos)))
1147 (defun widget-setup ()
1148 "Setup current buffer so editing string widgets works."
1149 (let ((inhibit-read-only t)
1150 (inhibit-modification-hooks t)
1152 (while widget-field-new
1153 (setq field (car widget-field-new)
1154 widget-field-new (cdr widget-field-new)
1155 widget-field-list (cons field widget-field-list))
1156 (let ((from (car (widget-get field :field-overlay)))
1157 (to (cdr (widget-get field :field-overlay))))
1158 (widget-specify-field field
1159 (marker-position from) (marker-position to))
1160 (set-marker from nil)
1161 (set-marker to nil))))
1163 (widget-add-change))
1165 (defvar widget-field-last nil)
1166 ;; Last field containing point.
1167 (make-variable-buffer-local 'widget-field-last)
1169 (defvar widget-field-was nil)
1170 ;; The widget data before the change.
1171 (make-variable-buffer-local 'widget-field-was)
1173 (defun widget-field-at (pos)
1174 "Return the widget field at POS, or nil if none."
1175 (let ((field (get-char-property (or pos (point)) 'field)))
1176 (if (eq field 'boundary)
1177 (get-char-property (or pos (point)) 'real-field)
1180 (defun widget-field-buffer (widget)
1181 "Return the buffer of WIDGET's editing field."
1182 (let ((overlay (widget-get widget :field-overlay)))
1183 (cond ((overlayp overlay)
1184 (overlay-buffer overlay))
1186 (marker-buffer (car overlay))))))
1188 (defun widget-field-start (widget)
1189 "Return the start of WIDGET's editing field."
1190 (let ((overlay (widget-get widget :field-overlay)))
1191 (if (overlayp overlay)
1192 (overlay-start overlay)
1195 (defun widget-field-end (widget)
1196 "Return the end of WIDGET's editing field."
1197 (let ((overlay (widget-get widget :field-overlay)))
1198 ;; Don't subtract one if local-map works at the end of the overlay,
1199 ;; or if a special `boundary' field has been added after the widget
1201 (if (overlayp overlay)
1202 (if (and (not (eq (with-current-buffer
1203 (widget-field-buffer widget)
1205 ;; `widget-narrow-to-field' can be
1206 ;; active when this function is called
1207 ;; from an change-functions hook. So
1208 ;; temporarily remove field narrowing
1209 ;; before to call `get-char-property'.
1211 (get-char-property (overlay-end overlay)
1214 (or widget-field-add-space
1215 (null (widget-get widget :size))))
1216 (1- (overlay-end overlay))
1217 (overlay-end overlay))
1220 (defun widget-field-find (pos)
1221 "Return the field at POS.
1222 Unlike (get-char-property POS 'field) this, works with empty fields too."
1223 (let ((fields widget-field-list)
1226 (setq field (car fields)
1227 fields (cdr fields))
1228 (when (and (<= (widget-field-start field) pos)
1229 (<= pos (widget-field-end field)))
1231 (error "Overlapping fields"))
1232 (setq found field)))
1235 (defun widget-before-change (from to)
1236 ;; This is how, for example, a variable changes its state to `modified'.
1237 ;; when it is being edited.
1238 (unless inhibit-read-only
1239 (let ((from-field (widget-field-find from))
1240 (to-field (widget-field-find to)))
1241 (cond ((not (eq from-field to-field))
1242 (add-hook 'post-command-hook 'widget-add-change nil t)
1243 (signal 'text-read-only
1244 '("Change should be restricted to a single field")))
1246 (add-hook 'post-command-hook 'widget-add-change nil t)
1247 (signal 'text-read-only
1248 '("Attempt to change text outside editable field")))
1249 (widget-field-use-before-change
1250 (widget-apply from-field :notify from-field))))))
1252 (defun widget-add-change ()
1253 (remove-hook 'post-command-hook 'widget-add-change t)
1254 (add-hook 'before-change-functions 'widget-before-change nil t)
1255 (add-hook 'after-change-functions 'widget-after-change nil t))
1257 (defun widget-after-change (from to old)
1258 "Adjust field size and text properties."
1259 (let ((field (widget-field-find from))
1260 (other (widget-field-find to)))
1262 (unless (eq field other)
1263 (error "Change in different fields"))
1264 (let ((size (widget-get field :size)))
1266 (let ((begin (widget-field-start field))
1267 (end (widget-field-end field)))
1268 (cond ((< (- end begin) size)
1272 (insert-char ?\ (- (+ begin size) end))))
1273 ((> (- end begin) size)
1274 ;; Field too large and
1275 (if (or (< (point) (+ begin size))
1277 ;; Point is outside extra space.
1278 (setq begin (+ begin size))
1279 ;; Point is within the extra space.
1280 (setq begin (point)))
1283 (while (and (eq (preceding-char) ?\ )
1285 (delete-backward-char 1)))))))
1286 (widget-specify-secret field))
1287 (widget-apply field :notify field))))
1289 ;;; Widget Functions
1291 ;; These functions are used in the definition of multiple widgets.
1293 (defun widget-parent-action (widget &optional event)
1294 "Tell :parent of WIDGET to handle the :action.
1295 Optional EVENT is the event that triggered the action."
1296 (widget-apply (widget-get widget :parent) :action event))
1298 (defun widget-children-value-delete (widget)
1299 "Delete all :children and :buttons in WIDGET."
1300 (mapc 'widget-delete (widget-get widget :children))
1301 (widget-put widget :children nil)
1302 (mapc 'widget-delete (widget-get widget :buttons))
1303 (widget-put widget :buttons nil))
1305 (defun widget-children-validate (widget)
1306 "All the :children must be valid."
1307 (let ((children (widget-get widget :children))
1309 (while (and children (not found))
1310 (setq child (car children)
1311 children (cdr children)
1312 found (widget-apply child :validate)))
1315 (defun widget-child-value-get (widget)
1316 "Get the value of the first member of :children in WIDGET."
1317 (widget-value (car (widget-get widget :children))))
1319 (defun widget-child-value-inline (widget)
1320 "Get the inline value of the first member of :children in WIDGET."
1321 (widget-apply (car (widget-get widget :children)) :value-inline))
1323 (defun widget-child-validate (widget)
1324 "The result of validating the first member of :children in WIDGET."
1325 (widget-apply (car (widget-get widget :children)) :validate))
1327 (defun widget-type-value-create (widget)
1328 "Convert and instantiate the value of the :type attribute of WIDGET.
1329 Store the newly created widget in the :children attribute.
1331 The value of the :type attribute should be an unconverted widget type."
1332 (let ((value (widget-get widget :value))
1333 (type (widget-get widget :type)))
1334 (widget-put widget :children
1335 (list (widget-create-child-value widget
1336 (widget-convert type)
1339 (defun widget-type-default-get (widget)
1340 "Get default value from the :type attribute of WIDGET.
1342 The value of the :type attribute should be an unconverted widget type."
1343 (widget-default-get (widget-convert (widget-get widget :type))))
1345 (defun widget-type-match (widget value)
1346 "Non-nil if the :type value of WIDGET matches VALUE.
1348 The value of the :type attribute should be an unconverted widget type."
1349 (widget-apply (widget-convert (widget-get widget :type)) :match value))
1351 (defun widget-types-copy (widget)
1352 "Copy :args as widget types in WIDGET."
1353 (widget-put widget :args (mapcar 'widget-copy (widget-get widget :args)))
1356 ;; Made defsubst to speed up face editor creation.
1357 (defsubst widget-types-convert-widget (widget)
1358 "Convert :args as widget types in WIDGET."
1359 (widget-put widget :args (mapcar 'widget-convert (widget-get widget :args)))
1362 (defun widget-value-convert-widget (widget)
1363 "Initialize :value from :args in WIDGET."
1364 (let ((args (widget-get widget :args)))
1366 (widget-put widget :value (car args))
1367 ;; Don't convert :value here, as this is done in `widget-convert'.
1368 ;; (widget-put widget :value (widget-apply widget
1369 ;; :value-to-internal (car args)))
1370 (widget-put widget :args nil)))
1373 (defun widget-value-value-get (widget)
1374 "Return the :value property of WIDGET."
1375 (widget-get widget :value))
1377 ;;; The `default' Widget.
1379 (define-widget 'default nil
1380 "Basic widget other widgets are derived from."
1381 :value-to-internal (lambda (widget value) value)
1382 :value-to-external (lambda (widget value) value)
1383 :button-prefix 'widget-button-prefix
1384 :button-suffix 'widget-button-suffix
1385 :complete 'widget-default-complete
1386 :create 'widget-default-create
1389 :format-handler 'widget-default-format-handler
1390 :button-face-get 'widget-default-button-face-get
1391 :sample-face-get 'widget-default-sample-face-get
1392 :delete 'widget-default-delete
1394 :value-set 'widget-default-value-set
1395 :value-inline 'widget-default-value-inline
1396 :value-delete 'ignore
1397 :default-get 'widget-default-default-get
1398 :menu-tag-get 'widget-default-menu-tag-get
1400 :active 'widget-default-active
1401 :activate 'widget-specify-active
1402 :deactivate 'widget-default-deactivate
1403 :mouse-down-action #'ignore
1404 :action 'widget-default-action
1405 :notify 'widget-default-notify
1406 :prompt-value 'widget-default-prompt-value)
1408 (defun widget-default-complete (widget)
1409 "Call the value of the :complete-function property of WIDGET.
1410 If that does not exists, call the value of `widget-complete-field'."
1411 (call-interactively (or (widget-get widget :complete-function)
1412 widget-complete-field)))
1414 (defun widget-default-create (widget)
1415 "Create WIDGET at point in the current buffer."
1416 (widget-specify-insert
1417 (let ((from (point))
1418 button-begin button-end
1419 sample-begin sample-end
1422 (insert (widget-get widget :format))
1424 ;; Parse escapes in format.
1425 (while (re-search-forward "%\\(.\\)" nil t)
1426 (let ((escape (char-after (match-beginning 1))))
1427 (delete-backward-char 2)
1428 (cond ((eq escape ?%)
1431 (setq button-begin (point))
1432 (insert (widget-get-indirect widget :button-prefix)))
1434 (insert (widget-get-indirect widget :button-suffix))
1435 (setq button-end (point)))
1437 (setq sample-begin (point)))
1439 (setq sample-end (point)))
1441 (when (widget-get widget :indent)
1443 (insert-char ? (widget-get widget :indent))))
1445 (let ((image (widget-get widget :tag-glyph))
1446 (tag (widget-get widget :tag)))
1448 (widget-image-insert widget (or tag "image") image))
1452 (princ (widget-get widget :value)
1453 (current-buffer))))))
1455 (let ((doc (widget-get widget :doc)))
1457 (setq doc-begin (point))
1459 (while (eq (preceding-char) ?\n)
1460 (delete-backward-char 1))
1462 (setq doc-end (point)))))
1464 (if (and button-begin (not button-end))
1465 (widget-apply widget :value-create)
1466 (setq value-pos (point))))
1468 (widget-apply widget :format-handler escape)))))
1469 ;; Specify button, sample, and doc, and insert value.
1470 (and button-begin button-end
1471 (widget-specify-button widget button-begin button-end))
1472 (and sample-begin sample-end
1473 (widget-specify-sample widget sample-begin sample-end))
1474 (and doc-begin doc-end
1475 (widget-specify-doc widget doc-begin doc-end))
1477 (goto-char value-pos)
1478 (widget-apply widget :value-create)))
1479 (let ((from (point-min-marker))
1480 (to (point-max-marker)))
1481 (set-marker-insertion-type from t)
1482 (set-marker-insertion-type to nil)
1483 (widget-put widget :from from)
1484 (widget-put widget :to to)))
1485 (widget-clear-undo))
1487 (defun widget-default-format-handler (widget escape)
1488 ;; We recognize the %h escape by default.
1489 (let* ((buttons (widget-get widget :buttons)))
1490 (cond ((eq escape ?h)
1491 (let* ((doc-property (widget-get widget :documentation-property))
1492 (doc-try (cond ((widget-get widget :doc))
1493 ((functionp doc-property)
1494 (funcall doc-property
1495 (widget-get widget :value)))
1496 ((symbolp doc-property)
1497 (documentation-property
1498 (widget-get widget :value)
1500 (doc-text (and (stringp doc-try)
1501 (> (length doc-try) 1)
1503 (doc-indent (widget-get widget :documentation-indent)))
1505 (and (eq (preceding-char) ?\n)
1506 (widget-get widget :indent)
1507 (insert-char ? (widget-get widget :indent)))
1508 ;; The `*' in the beginning is redundant.
1509 (when (eq (aref doc-text 0) ?*)
1510 (setq doc-text (substring doc-text 1)))
1511 ;; Get rid of trailing newlines.
1512 (when (string-match "\n+\\'" doc-text)
1513 (setq doc-text (substring doc-text 0 (match-beginning 0))))
1514 (push (widget-create-child-and-convert
1515 widget 'documentation-string
1516 :indent (cond ((numberp doc-indent )
1524 (error "Unknown escape `%c'" escape)))
1525 (widget-put widget :buttons buttons)))
1527 (defun widget-default-button-face-get (widget)
1528 ;; Use :button-face or widget-button-face
1529 (or (widget-get widget :button-face)
1530 (let ((parent (widget-get widget :parent)))
1532 (widget-apply parent :button-face-get)
1533 widget-button-face))))
1535 (defun widget-default-sample-face-get (widget)
1536 ;; Use :sample-face.
1537 (widget-get widget :sample-face))
1539 (defun widget-default-delete (widget)
1540 "Remove widget from the buffer."
1541 (let ((from (widget-get widget :from))
1542 (to (widget-get widget :to))
1543 (inactive-overlay (widget-get widget :inactive))
1544 (button-overlay (widget-get widget :button-overlay))
1545 (sample-overlay (widget-get widget :sample-overlay))
1546 (doc-overlay (widget-get widget :doc-overlay))
1547 (inhibit-modification-hooks t)
1548 (inhibit-read-only t))
1549 (widget-apply widget :value-delete)
1550 (widget-children-value-delete widget)
1551 (when inactive-overlay
1552 (delete-overlay inactive-overlay))
1553 (when button-overlay
1554 (delete-overlay button-overlay))
1555 (when sample-overlay
1556 (delete-overlay sample-overlay))
1558 (delete-overlay doc-overlay))
1560 ;; Kludge: this doesn't need to be true for empty formats.
1561 (delete-region from to))
1562 (set-marker from nil)
1563 (set-marker to nil))
1564 (widget-clear-undo))
1566 (defun widget-default-value-set (widget value)
1567 "Recreate widget with new value."
1568 (let* ((old-pos (point))
1569 (from (copy-marker (widget-get widget :from)))
1570 (to (copy-marker (widget-get widget :to)))
1571 (offset (if (and (<= from old-pos) (<= old-pos to))
1572 (if (>= old-pos (1- to))
1574 (- old-pos from)))))
1575 ;;??? Bug: this ought to insert the new value before deleting the old one,
1576 ;; so that markers on either side of the value automatically
1577 ;; stay on the same side. -- rms.
1579 (goto-char (widget-get widget :from))
1580 (widget-apply widget :delete)
1581 (widget-put widget :value value)
1582 (widget-apply widget :create))
1585 (goto-char (+ (widget-get widget :to) offset 1))
1586 (goto-char (min (+ from offset) (1- (widget-get widget :to))))))))
1588 (defun widget-default-value-inline (widget)
1589 "Wrap value in a list unless it is inline."
1590 (if (widget-get widget :inline)
1591 (widget-value widget)
1592 (list (widget-value widget))))
1594 (defun widget-default-default-get (widget)
1596 (widget-get widget :value))
1598 (defun widget-default-menu-tag-get (widget)
1599 "Use tag or value for menus."
1600 (or (widget-get widget :menu-tag)
1601 (widget-get widget :tag)
1602 (widget-princ-to-string (widget-get widget :value))))
1604 (defun widget-default-active (widget)
1605 "Return t iff this widget active (user modifiable)."
1606 (or (widget-get widget :always-active)
1607 (and (not (widget-get widget :inactive))
1608 (let ((parent (widget-get widget :parent)))
1610 (widget-apply parent :active))))))
1612 (defun widget-default-deactivate (widget)
1613 "Make WIDGET inactive for user modifications."
1614 (widget-specify-inactive widget
1615 (widget-get widget :from)
1616 (widget-get widget :to)))
1618 (defun widget-default-action (widget &optional event)
1619 "Notify the parent when a widget changes."
1620 (let ((parent (widget-get widget :parent)))
1622 (widget-apply parent :notify widget event))))
1624 (defun widget-default-notify (widget child &optional event)
1625 "Pass notification to parent."
1626 (widget-default-action widget event))
1628 (defun widget-default-prompt-value (widget prompt value unbound)
1629 "Read an arbitrary value. Stolen from `set-variable'."
1630 ;; (let ((initial (if unbound
1632 ;; It would be nice if we could do a `(cons val 1)' here.
1633 ;; (prin1-to-string (custom-quote value))))))
1634 (eval-minibuffer prompt))
1636 ;;; The `item' Widget.
1638 (define-widget 'item 'default
1639 "Constant items for inclusion in other widgets."
1640 :convert-widget 'widget-value-convert-widget
1641 :value-create 'widget-item-value-create
1642 :value-delete 'ignore
1643 :value-get 'widget-value-value-get
1644 :match 'widget-item-match
1645 :match-inline 'widget-item-match-inline
1646 :action 'widget-item-action
1649 (defun widget-item-value-create (widget)
1650 "Insert the printed representation of the value."
1651 (princ (widget-get widget :value) (current-buffer)))
1653 (defun widget-item-match (widget value)
1654 ;; Match if the value is the same.
1655 (equal (widget-get widget :value) value))
1657 (defun widget-item-match-inline (widget values)
1658 ;; Match if the value is the same.
1659 (let ((value (widget-get widget :value)))
1661 (<= (length value) (length values))
1662 (let ((head (widget-sublist values 0 (length value))))
1663 (and (equal head value)
1664 (cons head (widget-sublist values (length value))))))))
1666 (defun widget-sublist (list start &optional end)
1667 "Return the sublist of LIST from START to END.
1668 If END is omitted, it defaults to the length of LIST."
1669 (if (> start 0) (setq list (nthcdr start list)))
1671 (unless (<= end start)
1672 (setq list (copy-sequence list))
1673 (setcdr (nthcdr (- end start 1) list) nil)
1675 (copy-sequence list)))
1677 (defun widget-item-action (widget &optional event)
1678 ;; Just notify itself.
1679 (widget-apply widget :notify widget event))
1681 ;;; The `push-button' Widget.
1683 ;; (defcustom widget-push-button-gui t
1684 ;; "If non nil, use GUI push buttons when available."
1688 ;; Cache already created GUI objects.
1689 ;; (defvar widget-push-button-cache nil)
1691 (defcustom widget-push-button-prefix "["
1692 "String used as prefix for buttons."
1694 :group 'widget-button)
1696 (defcustom widget-push-button-suffix "]"
1697 "String used as suffix for buttons."
1699 :group 'widget-button)
1701 (define-widget 'push-button 'item
1702 "A pushable button."
1705 :value-create 'widget-push-button-value-create
1708 (defun widget-push-button-value-create (widget)
1709 "Insert text representing the `on' and `off' states."
1710 (let* ((tag (or (widget-get widget :tag)
1711 (widget-get widget :value)))
1712 (tag-glyph (widget-get widget :tag-glyph))
1713 (text (concat widget-push-button-prefix
1714 tag widget-push-button-suffix)))
1716 (widget-image-insert widget text tag-glyph)
1719 ;; (defun widget-gui-action (widget)
1720 ;; "Apply :action for WIDGET."
1721 ;; (widget-apply-action widget (this-command-keys)))
1723 ;;; The `link' Widget.
1725 (defcustom widget-link-prefix "["
1726 "String used as prefix for links."
1728 :group 'widget-button)
1730 (defcustom widget-link-suffix "]"
1731 "String used as suffix for links."
1733 :group 'widget-button)
1735 (define-widget 'link 'item
1737 :button-prefix 'widget-link-prefix
1738 :button-suffix 'widget-link-suffix
1740 :help-echo "Follow the link."
1743 ;;; The `info-link' Widget.
1745 (define-widget 'info-link 'link
1746 "A link to an info file."
1747 :action 'widget-info-link-action)
1749 (defun widget-info-link-action (widget &optional event)
1750 "Open the info node specified by WIDGET."
1751 (info (widget-value widget)))
1753 ;;; The `url-link' Widget.
1755 (define-widget 'url-link 'link
1756 "A link to an www page."
1757 :action 'widget-url-link-action)
1759 (defun widget-url-link-action (widget &optional event)
1760 "Open the url specified by WIDGET."
1761 (browse-url (widget-value widget)))
1763 ;;; The `function-link' Widget.
1765 (define-widget 'function-link 'link
1766 "A link to an Emacs function."
1767 :action 'widget-function-link-action)
1769 (defun widget-function-link-action (widget &optional event)
1770 "Show the function specified by WIDGET."
1771 (describe-function (widget-value widget)))
1773 ;;; The `variable-link' Widget.
1775 (define-widget 'variable-link 'link
1776 "A link to an Emacs variable."
1777 :action 'widget-variable-link-action)
1779 (defun widget-variable-link-action (widget &optional event)
1780 "Show the variable specified by WIDGET."
1781 (describe-variable (widget-value widget)))
1783 ;;; The `file-link' Widget.
1785 (define-widget 'file-link 'link
1787 :action 'widget-file-link-action)
1789 (defun widget-file-link-action (widget &optional event)
1790 "Find the file specified by WIDGET."
1791 (find-file (widget-value widget)))
1793 ;;; The `emacs-library-link' Widget.
1795 (define-widget 'emacs-library-link 'link
1796 "A link to an Emacs Lisp library file."
1797 :action 'widget-emacs-library-link-action)
1799 (defun widget-emacs-library-link-action (widget &optional event)
1800 "Find the Emacs Library file specified by WIDGET."
1801 (find-file (locate-library (widget-value widget))))
1803 ;;; The `emacs-commentary-link' Widget.
1805 (define-widget 'emacs-commentary-link 'link
1806 "A link to Commentary in an Emacs Lisp library file."
1807 :action 'widget-emacs-commentary-link-action)
1809 (defun widget-emacs-commentary-link-action (widget &optional event)
1810 "Find the Commentary section of the Emacs file specified by WIDGET."
1811 (finder-commentary (widget-value widget)))
1813 ;;; The `editable-field' Widget.
1815 (define-widget 'editable-field 'default
1816 "An editable text field."
1817 :convert-widget 'widget-value-convert-widget
1818 :keymap widget-field-keymap
1820 :help-echo "M-TAB: complete field; RET: enter value"
1822 :prompt-internal 'widget-field-prompt-internal
1823 :prompt-history 'widget-field-history
1824 :prompt-value 'widget-field-prompt-value
1825 :action 'widget-field-action
1826 :validate 'widget-field-validate
1828 :error "Field's value doesn't match allowed forms"
1829 :value-create 'widget-field-value-create
1830 :value-delete 'widget-field-value-delete
1831 :value-get 'widget-field-value-get
1832 :match 'widget-field-match)
1834 (defvar widget-field-history nil
1835 "History of field minibuffer edits.")
1837 (defun widget-field-prompt-internal (widget prompt initial history)
1838 "Read string for WIDGET promptinhg with PROMPT.
1839 INITIAL is the initial input and HISTORY is a symbol containing
1841 (read-string prompt initial history))
1843 (defun widget-field-prompt-value (widget prompt value unbound)
1844 "Prompt for a string."
1845 (widget-apply widget
1847 (widget-apply widget
1848 :prompt-internal prompt
1850 (cons (widget-apply widget
1851 :value-to-internal value)
1853 (widget-get widget :prompt-history))))
1855 (defvar widget-edit-functions nil)
1857 (defun widget-field-action (widget &optional event)
1858 "Move to next field."
1860 (run-hook-with-args 'widget-edit-functions widget))
1862 (defun widget-field-validate (widget)
1863 "Valid if the content matches `:valid-regexp'."
1864 (unless (string-match (widget-get widget :valid-regexp)
1865 (widget-apply widget :value-get))
1868 (defun widget-field-value-create (widget)
1869 "Create an editable text field."
1870 (let ((size (widget-get widget :size))
1871 (value (widget-get widget :value))
1873 ;; This is changed to a real overlay in `widget-setup'. We
1874 ;; need the end points to behave differently until
1875 ;; `widget-setup' is called.
1876 (overlay (cons (make-marker) (make-marker))))
1877 (widget-put widget :field-overlay overlay)
1880 (< (length value) size)
1881 (insert-char ?\ (- size (length value))))
1882 (unless (memq widget widget-field-list)
1883 (setq widget-field-new (cons widget widget-field-new)))
1884 (move-marker (cdr overlay) (point))
1885 (set-marker-insertion-type (cdr overlay) nil)
1888 (move-marker (car overlay) from)
1889 (set-marker-insertion-type (car overlay) t)))
1891 (defun widget-field-value-delete (widget)
1892 "Remove the widget from the list of active editing fields."
1893 (setq widget-field-list (delq widget widget-field-list))
1894 (setq widget-field-new (delq widget widget-field-new))
1895 ;; These are nil if the :format string doesn't contain `%v'.
1896 (let ((overlay (widget-get widget :field-overlay)))
1897 (when (overlayp overlay)
1898 (delete-overlay overlay))))
1900 (defun widget-field-value-get (widget)
1901 "Return current text in editing field."
1902 (let ((from (widget-field-start widget))
1903 (to (widget-field-end widget))
1904 (buffer (widget-field-buffer widget))
1905 (size (widget-get widget :size))
1906 (secret (widget-get widget :secret))
1907 (old (current-buffer)))
1914 (eq (char-after (1- to)) ?\ ))
1916 (let ((result (buffer-substring-no-properties from to)))
1919 (while (< (+ from index) to)
1921 (get-char-property (+ from index) 'secret))
1922 (setq index (1+ index)))))
1925 (widget-get widget :value))))
1927 (defun widget-field-match (widget value)
1928 ;; Match any string.
1931 ;;; The `text' Widget.
1933 (define-widget 'text 'editable-field
1934 "A multiline text area."
1935 :keymap widget-text-keymap)
1937 ;;; The `menu-choice' Widget.
1939 (define-widget 'menu-choice 'default
1940 "A menu of options."
1941 :convert-widget 'widget-types-convert-widget
1942 :copy 'widget-types-copy
1943 :format "%[%t%]: %v"
1946 :void '(item :format "invalid (%t)\n")
1947 :value-create 'widget-choice-value-create
1948 :value-get 'widget-child-value-get
1949 :value-inline 'widget-child-value-inline
1950 :default-get 'widget-choice-default-get
1951 :mouse-down-action 'widget-choice-mouse-down-action
1952 :action 'widget-choice-action
1953 :error "Make a choice"
1954 :validate 'widget-choice-validate
1955 :match 'widget-choice-match
1956 :match-inline 'widget-choice-match-inline)
1958 (defun widget-choice-value-create (widget)
1959 "Insert the first choice that matches the value."
1960 (let ((value (widget-get widget :value))
1961 (args (widget-get widget :args))
1962 (explicit (widget-get widget :explicit-choice))
1964 (if (and explicit (equal value (widget-get widget :explicit-choice-value)))
1966 ;; If the user specified the choice for this value,
1967 ;; respect that choice as long as the value is the same.
1968 (widget-put widget :children (list (widget-create-child-value
1969 widget explicit value)))
1970 (widget-put widget :choice explicit))
1972 (setq current (car args)
1974 (when (widget-apply current :match value)
1975 (widget-put widget :children (list (widget-create-child-value
1976 widget current value)))
1977 (widget-put widget :choice current)
1981 (let ((void (widget-get widget :void)))
1982 (widget-put widget :children (list (widget-create-child-and-convert
1983 widget void :value value)))
1984 (widget-put widget :choice void))))))
1986 (defun widget-choice-default-get (widget)
1987 ;; Get default for the first choice.
1988 (widget-default-get (car (widget-get widget :args))))
1990 (defcustom widget-choice-toggle nil
1991 "If non-nil, a binary choice will just toggle between the values.
1992 Otherwise, the user will explicitly have to choose between the values
1993 when he invoked the menu."
1997 (defun widget-choice-mouse-down-action (widget &optional event)
1998 ;; Return non-nil if we need a menu.
1999 (let ((args (widget-get widget :args))
2000 (old (widget-get widget :choice)))
2001 (cond ((not (display-popup-menus-p))
2002 ;; No place to pop up a menu.
2004 ((< (length args) 2)
2005 ;; Empty or singleton list, just return the value.
2007 ((> (length args) widget-menu-max-size)
2008 ;; Too long, prompt.
2010 ((> (length args) 2)
2011 ;; Reasonable sized list, use menu.
2013 ((and widget-choice-toggle (memq old args))
2017 ;; Ask which of the two.
2020 (defun widget-choice-action (widget &optional event)
2022 (let ((args (widget-get widget :args))
2023 (old (widget-get widget :choice))
2024 (tag (widget-apply widget :menu-tag-get))
2025 (completion-ignore-case (widget-get widget :case-fold))
2028 ;; Remember old value.
2029 (if (and old (not (widget-apply widget :validate)))
2030 (let* ((external (widget-value widget))
2031 (internal (widget-apply old :value-to-internal external)))
2032 (widget-put old :value internal)))
2035 (cond ((= (length args) 0)
2037 ((= (length args) 1)
2039 ((and widget-choice-toggle
2042 (if (eq old (nth 0 args))
2047 (setq current (car args)
2050 (cons (cons (widget-apply current :menu-tag-get)
2053 (setq this-explicit t)
2054 (widget-choose tag (reverse choices) event))))
2056 ;; If this was an explicit user choice,
2057 ;; record the choice, and the record the value it was made for.
2058 ;; widget-choice-value-create will respect this choice,
2059 ;; as long as the value is the same.
2061 (widget-put widget :explicit-choice current)
2062 (widget-put widget :explicit-choice-value (widget-get widget :value)))
2063 (widget-value-set widget (widget-default-get current))
2065 (widget-apply widget :notify widget event)))
2066 (run-hook-with-args 'widget-edit-functions widget))
2068 (defun widget-choice-validate (widget)
2069 ;; Valid if we have made a valid choice.
2070 (if (eq (widget-get widget :void) (widget-get widget :choice))
2072 (widget-apply (car (widget-get widget :children)) :validate)))
2074 (defun widget-choice-match (widget value)
2075 ;; Matches if one of the choices matches.
2076 (let ((args (widget-get widget :args))
2078 (while (and args (not found))
2079 (setq current (car args)
2081 found (widget-apply current :match value)))
2084 (defun widget-choice-match-inline (widget values)
2085 ;; Matches if one of the choices matches.
2086 (let ((args (widget-get widget :args))
2088 (while (and args (null found))
2089 (setq current (car args)
2091 found (widget-match-inline current values)))
2094 ;;; The `toggle' Widget.
2096 (define-widget 'toggle 'item
2097 "Toggle between two states."
2099 :value-create 'widget-toggle-value-create
2100 :action 'widget-toggle-action
2101 :match (lambda (widget value) t)
2105 (defun widget-toggle-value-create (widget)
2106 "Insert text representing the `on' and `off' states."
2107 (if (widget-value widget)
2108 (let ((image (widget-get widget :on-glyph)))
2109 (and (display-graphic-p)
2111 (not (eq (car image) 'image))
2112 (widget-put widget :on-glyph (setq image (eval image))))
2113 (widget-image-insert widget
2114 (widget-get widget :on)
2116 (let ((image (widget-get widget :off-glyph)))
2117 (and (display-graphic-p)
2119 (not (eq (car image) 'image))
2120 (widget-put widget :off-glyph (setq image (eval image))))
2121 (widget-image-insert widget (widget-get widget :off) image))))
2123 (defun widget-toggle-action (widget &optional event)
2125 (widget-value-set widget (not (widget-value widget)))
2126 (widget-apply widget :notify widget event)
2127 (run-hook-with-args 'widget-edit-functions widget))
2129 ;;; The `checkbox' Widget.
2131 (define-widget 'checkbox 'toggle
2132 "A checkbox toggle."
2137 ;; We could probably do the same job as the images using single
2138 ;; space characters in a boxed face with a stretch specification to
2139 ;; make them square.
2140 :on-glyph '(create-image "\300\300\141\143\067\076\034\030"
2141 'xbm t :width 8 :height 8
2142 :background "grey75" ; like default mode line
2147 :off-glyph '(create-image (make-string 8 0)
2148 'xbm t :width 8 :height 8
2149 :background "grey75"
2153 :help-echo "Toggle this item."
2154 :action 'widget-checkbox-action)
2156 (defun widget-checkbox-action (widget &optional event)
2157 "Toggle checkbox, notify parent, and set active state of sibling."
2158 (widget-toggle-action widget event)
2159 (let ((sibling (widget-get-sibling widget)))
2161 (if (widget-value widget)
2162 (widget-apply sibling :activate)
2163 (widget-apply sibling :deactivate)))))
2165 ;;; The `checklist' Widget.
2167 (define-widget 'checklist 'default
2168 "A multiple choice widget."
2169 :convert-widget 'widget-types-convert-widget
2170 :copy 'widget-types-copy
2173 :entry-format "%b %v"
2175 :value-create 'widget-checklist-value-create
2176 :value-get 'widget-checklist-value-get
2177 :validate 'widget-checklist-validate
2178 :match 'widget-checklist-match
2179 :match-inline 'widget-checklist-match-inline)
2181 (defun widget-checklist-value-create (widget)
2182 ;; Insert all values
2183 (let ((alist (widget-checklist-match-find widget (widget-get widget :value)))
2184 (args (widget-get widget :args)))
2186 (widget-checklist-add-item widget (car args) (assq (car args) alist))
2187 (setq args (cdr args)))
2188 (widget-put widget :children (nreverse (widget-get widget :children)))))
2190 (defun widget-checklist-add-item (widget type chosen)
2191 "Create checklist item in WIDGET of type TYPE.
2192 If the item is checked, CHOSEN is a cons whose cdr is the value."
2193 (and (eq (preceding-char) ?\n)
2194 (widget-get widget :indent)
2195 (insert-char ? (widget-get widget :indent)))
2196 (widget-specify-insert
2197 (let* ((children (widget-get widget :children))
2198 (buttons (widget-get widget :buttons))
2199 (button-args (or (widget-get type :sibling-args)
2200 (widget-get widget :button-args)))
2203 (insert (widget-get widget :entry-format))
2205 ;; Parse % escapes in format.
2206 (while (re-search-forward "%\\([bv%]\\)" nil t)
2207 (let ((escape (char-after (match-beginning 1))))
2208 (delete-backward-char 2)
2209 (cond ((eq escape ?%)
2212 (setq button (apply 'widget-create-child-and-convert
2214 :value (not (null chosen))
2219 (let ((child (widget-create-child widget type)))
2220 (widget-apply child :deactivate)
2222 ((widget-get type :inline)
2223 (widget-create-child-value
2224 widget type (cdr chosen)))
2226 (widget-create-child-value
2227 widget type (car (cdr chosen)))))))
2229 (error "Unknown escape `%c'" escape)))))
2230 ;; Update properties.
2231 (and button child (widget-put child :button button))
2232 (and button (widget-put widget :buttons (cons button buttons)))
2233 (and child (widget-put widget :children (cons child children))))))
2235 (defun widget-checklist-match (widget values)
2236 ;; All values must match a type in the checklist.
2238 (null (cdr (widget-checklist-match-inline widget values)))))
2240 (defun widget-checklist-match-inline (widget values)
2241 ;; Find the values which match a type in the checklist.
2242 (let ((greedy (widget-get widget :greedy))
2243 (args (copy-sequence (widget-get widget :args)))
2246 (let ((answer (widget-checklist-match-up args values)))
2248 (let ((vals (widget-match-inline answer values)))
2249 (setq found (append found (car vals))
2251 args (delq answer args))))
2253 (setq rest (append rest (list (car values)))
2254 values (cdr values)))
2256 (setq rest (append rest values)
2260 (defun widget-checklist-match-find (widget vals)
2261 "Find the vals which match a type in the checklist.
2262 Return an alist of (TYPE MATCH)."
2263 (let ((greedy (widget-get widget :greedy))
2264 (args (copy-sequence (widget-get widget :args)))
2267 (let ((answer (widget-checklist-match-up args vals)))
2269 (let ((match (widget-match-inline answer vals)))
2270 (setq found (cons (cons answer (car match)) found)
2272 args (delq answer args))))
2274 (setq vals (cdr vals)))
2279 (defun widget-checklist-match-up (args vals)
2280 "Return the first type from ARGS that matches VALS."
2281 (let (current found)
2282 (while (and args (null found))
2283 (setq current (car args)
2285 found (widget-match-inline current vals)))
2289 (defun widget-checklist-value-get (widget)
2290 ;; The values of all selected items.
2291 (let ((children (widget-get widget :children))
2294 (setq child (car children)
2295 children (cdr children))
2296 (if (widget-value (widget-get child :button))
2297 (setq result (append result (widget-apply child :value-inline)))))
2300 (defun widget-checklist-validate (widget)
2301 ;; Ticked chilren must be valid.
2302 (let ((children (widget-get widget :children))
2304 (while (and children (not found))
2305 (setq child (car children)
2306 children (cdr children)
2307 button (widget-get child :button)
2308 found (and (widget-value button)
2309 (widget-apply child :validate))))
2312 ;;; The `option' Widget
2314 (define-widget 'option 'checklist
2315 "An widget with an optional item."
2318 ;;; The `choice-item' Widget.
2320 (define-widget 'choice-item 'item
2321 "Button items that delegate action events to their parents."
2322 :action 'widget-parent-action
2323 :format "%[%t%] \n")
2325 ;;; The `radio-button' Widget.
2327 (define-widget 'radio-button 'toggle
2328 "A radio button for use in the `radio' widget."
2329 :notify 'widget-radio-button-notify
2336 :off-glyph "radio0")
2338 (defun widget-radio-button-notify (widget child &optional event)
2340 (widget-apply (widget-get widget :parent) :action widget event))
2342 ;;; The `radio-button-choice' Widget.
2344 (define-widget 'radio-button-choice 'default
2345 "Select one of multiple options."
2346 :convert-widget 'widget-types-convert-widget
2347 :copy 'widget-types-copy
2350 :entry-format "%b %v"
2351 :value-create 'widget-radio-value-create
2352 :value-get 'widget-radio-value-get
2353 :value-inline 'widget-radio-value-inline
2354 :value-set 'widget-radio-value-set
2355 :error "You must push one of the buttons"
2356 :validate 'widget-radio-validate
2357 :match 'widget-choice-match
2358 :match-inline 'widget-choice-match-inline
2359 :action 'widget-radio-action)
2361 (defun widget-radio-value-create (widget)
2362 ;; Insert all values
2363 (let ((args (widget-get widget :args))
2366 (setq arg (car args)
2368 (widget-radio-add-item widget arg))))
2370 (defun widget-radio-add-item (widget type)
2371 "Add to radio widget WIDGET a new radio button item of type TYPE."
2372 ;; (setq type (widget-convert type))
2373 (and (eq (preceding-char) ?\n)
2374 (widget-get widget :indent)
2375 (insert-char ? (widget-get widget :indent)))
2376 (widget-specify-insert
2377 (let* ((value (widget-get widget :value))
2378 (children (widget-get widget :children))
2379 (buttons (widget-get widget :buttons))
2380 (button-args (or (widget-get type :sibling-args)
2381 (widget-get widget :button-args)))
2383 (chosen (and (null (widget-get widget :choice))
2384 (widget-apply type :match value)))
2386 (insert (widget-get widget :entry-format))
2388 ;; Parse % escapes in format.
2389 (while (re-search-forward "%\\([bv%]\\)" nil t)
2390 (let ((escape (char-after (match-beginning 1))))
2391 (delete-backward-char 2)
2392 (cond ((eq escape ?%)
2395 (setq button (apply 'widget-create-child-and-convert
2396 widget 'radio-button
2397 :value (not (null chosen))
2400 (setq child (if chosen
2401 (widget-create-child-value
2403 (widget-create-child widget type)))
2405 (widget-apply child :deactivate)))
2407 (error "Unknown escape `%c'" escape)))))
2408 ;; Update properties.
2410 (widget-put widget :choice type))
2412 (widget-put child :button button)
2413 (widget-put widget :buttons (nconc buttons (list button))))
2415 (widget-put widget :children (nconc children (list child))))
2418 (defun widget-radio-value-get (widget)
2419 ;; Get value of the child widget.
2420 (let ((chosen (widget-radio-chosen widget)))
2421 (and chosen (widget-value chosen))))
2423 (defun widget-radio-chosen (widget)
2424 "Return the widget representing the chosen radio button."
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)
2435 (defun widget-radio-value-inline (widget)
2436 ;; Get value of the child widget.
2437 (let ((children (widget-get widget :children))
2440 (setq current (car children)
2441 children (cdr children))
2442 (when (widget-apply (widget-get current :button) :value-get)
2443 (setq found (widget-apply current :value-inline)
2447 (defun widget-radio-value-set (widget value)
2448 ;; We can't just delete and recreate a radio widget, since children
2449 ;; can be added after the original creation and won't be recreated
2451 (let ((children (widget-get widget :children))
2454 (setq current (car children)
2455 children (cdr children))
2456 (let* ((button (widget-get current :button))
2457 (match (and (not found)
2458 (widget-apply current :match value))))
2459 (widget-value-set button match)
2462 (widget-value-set current value)
2463 (widget-apply current :activate))
2464 (widget-apply current :deactivate))
2465 (setq found (or found match))))))
2467 (defun widget-radio-validate (widget)
2468 ;; Valid if we have made a valid choice.
2469 (let ((children (widget-get widget :children))
2470 current found button)
2471 (while (and children (not found))
2472 (setq current (car children)
2473 children (cdr children)
2474 button (widget-get current :button)
2475 found (widget-apply button :value-get)))
2477 (widget-apply current :validate)
2480 (defun widget-radio-action (widget child event)
2481 ;; Check if a radio button was pressed.
2482 (let ((children (widget-get widget :children))
2483 (buttons (widget-get widget :buttons))
2485 (when (memq child buttons)
2487 (setq current (car children)
2488 children (cdr children))
2489 (let* ((button (widget-get current :button)))
2490 (cond ((eq child button)
2491 (widget-value-set button t)
2492 (widget-apply current :activate))
2493 ((widget-value button)
2494 (widget-value-set button nil)
2495 (widget-apply current :deactivate)))))))
2496 ;; Pass notification to parent.
2497 (widget-apply widget :notify child event))
2499 ;;; The `insert-button' Widget.
2501 (define-widget 'insert-button 'push-button
2502 "An insert button for the `editable-list' widget."
2504 :help-echo "Insert a new item into the list at this position."
2505 :action 'widget-insert-button-action)
2507 (defun widget-insert-button-action (widget &optional event)
2508 ;; Ask the parent to insert a new item.
2509 (widget-apply (widget-get widget :parent)
2510 :insert-before (widget-get widget :widget)))
2512 ;;; The `delete-button' Widget.
2514 (define-widget 'delete-button 'push-button
2515 "A delete button for the `editable-list' widget."
2517 :help-echo "Delete this item from the list."
2518 :action 'widget-delete-button-action)
2520 (defun widget-delete-button-action (widget &optional event)
2521 ;; Ask the parent to insert a new item.
2522 (widget-apply (widget-get widget :parent)
2523 :delete-at (widget-get widget :widget)))
2525 ;;; The `editable-list' Widget.
2527 ;; (defcustom widget-editable-list-gui nil
2528 ;; "If non nil, use GUI push-buttons in editable list when available."
2532 (define-widget 'editable-list 'default
2533 "A variable list of widgets of the same type."
2534 :convert-widget 'widget-types-convert-widget
2535 :copy 'widget-types-copy
2538 :format-handler 'widget-editable-list-format-handler
2539 :entry-format "%i %d %v"
2540 :value-create 'widget-editable-list-value-create
2541 :value-get 'widget-editable-list-value-get
2542 :validate 'widget-children-validate
2543 :match 'widget-editable-list-match
2544 :match-inline 'widget-editable-list-match-inline
2545 :insert-before 'widget-editable-list-insert-before
2546 :delete-at 'widget-editable-list-delete-at)
2548 (defun widget-editable-list-format-handler (widget escape)
2549 ;; We recognize the insert button.
2550 ;; (let ((widget-push-button-gui widget-editable-list-gui))
2551 (cond ((eq escape ?i)
2552 (and (widget-get widget :indent)
2553 (insert-char ?\ (widget-get widget :indent)))
2554 (apply 'widget-create-child-and-convert
2555 widget 'insert-button
2556 (widget-get widget :append-button-args)))
2558 (widget-default-format-handler widget escape)))
2562 (defun widget-editable-list-value-create (widget)
2563 ;; Insert all values
2564 (let* ((value (widget-get widget :value))
2565 (type (nth 0 (widget-get widget :args)))
2567 (widget-put widget :value-pos (copy-marker (point)))
2568 (set-marker-insertion-type (widget-get widget :value-pos) t)
2570 (let ((answer (widget-match-inline type value)))
2572 (setq children (cons (widget-editable-list-entry-create
2574 (if (widget-get type :inline)
2581 (widget-put widget :children (nreverse children))))
2583 (defun widget-editable-list-value-get (widget)
2584 ;; Get value of the child widget.
2585 (apply 'append (mapcar (lambda (child) (widget-apply child :value-inline))
2586 (widget-get widget :children))))
2588 (defun widget-editable-list-match (widget value)
2589 ;; Value must be a list and all the members must match the type.
2591 (null (cdr (widget-editable-list-match-inline widget value)))))
2593 (defun widget-editable-list-match-inline (widget value)
2594 (let ((type (nth 0 (widget-get widget :args)))
2597 (while (and value ok)
2598 (let ((answer (widget-match-inline type value)))
2600 (setq found (append found (car answer))
2603 (cons found value)))
2605 (defun widget-editable-list-insert-before (widget before)
2606 ;; Insert a new child in the list of children.
2608 (let ((children (widget-get widget :children))
2609 (inhibit-read-only t)
2610 before-change-functions
2611 after-change-functions)
2613 (goto-char (widget-get before :entry-from)))
2615 (goto-char (widget-get widget :value-pos))))
2616 (let ((child (widget-editable-list-entry-create
2618 (when (< (widget-get child :entry-from) (widget-get widget :from))
2619 (set-marker (widget-get widget :from)
2620 (widget-get child :entry-from)))
2621 (if (eq (car children) before)
2622 (widget-put widget :children (cons child children))
2623 (while (not (eq (car (cdr children)) before))
2624 (setq children (cdr children)))
2625 (setcdr children (cons child (cdr children)))))))
2627 (widget-apply widget :notify widget))
2629 (defun widget-editable-list-delete-at (widget child)
2630 ;; Delete child from list of children.
2632 (let ((buttons (copy-sequence (widget-get widget :buttons)))
2634 (inhibit-read-only t)
2635 before-change-functions
2636 after-change-functions)
2638 (setq button (car buttons)
2639 buttons (cdr buttons))
2640 (when (eq (widget-get button :widget) child)
2642 :buttons (delq button (widget-get widget :buttons)))
2643 (widget-delete button))))
2644 (let ((entry-from (widget-get child :entry-from))
2645 (entry-to (widget-get child :entry-to))
2646 (inhibit-read-only t)
2647 before-change-functions
2648 after-change-functions)
2649 (widget-delete child)
2650 (delete-region entry-from entry-to)
2651 (set-marker entry-from nil)
2652 (set-marker entry-to nil))
2653 (widget-put widget :children (delq child (widget-get widget :children))))
2655 (widget-apply widget :notify widget))
2657 (defun widget-editable-list-entry-create (widget value conv)
2658 ;; Create a new entry to the list.
2659 (let ((type (nth 0 (widget-get widget :args)))
2660 ;; (widget-push-button-gui widget-editable-list-gui)
2661 child delete insert)
2662 (widget-specify-insert
2664 (and (widget-get widget :indent)
2665 (insert-char ?\ (widget-get widget :indent)))
2666 (insert (widget-get widget :entry-format)))
2667 ;; Parse % escapes in format.
2668 (while (re-search-forward "%\\(.\\)" nil t)
2669 (let ((escape (char-after (match-beginning 1))))
2670 (delete-backward-char 2)
2671 (cond ((eq escape ?%)
2674 (setq insert (apply 'widget-create-child-and-convert
2675 widget 'insert-button
2676 (widget-get widget :insert-button-args))))
2678 (setq delete (apply 'widget-create-child-and-convert
2679 widget 'delete-button
2680 (widget-get widget :delete-button-args))))
2683 (setq child (widget-create-child-value
2685 (setq child (widget-create-child-value
2686 widget type (widget-default-get type)))))
2688 (error "Unknown escape `%c'" escape)))))
2689 (let ((buttons (widget-get widget :buttons)))
2690 (if insert (push insert buttons))
2691 (if delete (push delete buttons))
2692 (widget-put widget :buttons buttons))
2693 (let ((entry-from (point-min-marker))
2694 (entry-to (point-max-marker)))
2695 (set-marker-insertion-type entry-from t)
2696 (set-marker-insertion-type entry-to nil)
2697 (widget-put child :entry-from entry-from)
2698 (widget-put child :entry-to entry-to)))
2699 (if insert (widget-put insert :widget child))
2700 (if delete (widget-put delete :widget child))
2703 ;;; The `group' Widget.
2705 (define-widget 'group 'default
2706 "A widget which groups other widgets inside."
2707 :convert-widget 'widget-types-convert-widget
2708 :copy 'widget-types-copy
2710 :value-create 'widget-group-value-create
2711 :value-get 'widget-editable-list-value-get
2712 :default-get 'widget-group-default-get
2713 :validate 'widget-children-validate
2714 :match 'widget-group-match
2715 :match-inline 'widget-group-match-inline)
2717 (defun widget-group-value-create (widget)
2718 ;; Create each component.
2719 (let ((args (widget-get widget :args))
2720 (value (widget-get widget :value))
2721 arg answer children)
2723 (setq arg (car args)
2725 answer (widget-match-inline arg value)
2727 (and (eq (preceding-char) ?\n)
2728 (widget-get widget :indent)
2729 (insert-char ?\ (widget-get widget :indent)))
2730 (push (cond ((null answer)
2731 (widget-create-child widget arg))
2732 ((widget-get arg :inline)
2733 (widget-create-child-value widget arg (car answer)))
2735 (widget-create-child-value widget arg (car (car answer)))))
2737 (widget-put widget :children (nreverse children))))
2739 (defun widget-group-default-get (widget)
2740 ;; Get the default of the components.
2741 (mapcar 'widget-default-get (widget-get widget :args)))
2743 (defun widget-group-match (widget values)
2744 ;; Match if the components match.
2746 (let ((match (widget-group-match-inline widget values)))
2747 (and match (null (cdr match))))))
2749 (defun widget-group-match-inline (widget vals)
2750 ;; Match if the components match.
2751 (let ((args (widget-get widget :args))
2752 argument answer found)
2754 (setq argument (car args)
2756 answer (widget-match-inline argument vals))
2758 (setq vals (cdr answer)
2759 found (append found (car answer)))
2763 (cons found vals))))
2765 ;;; The `visibility' Widget.
2767 (define-widget 'visibility 'item
2768 "An indicator and manipulator for hidden items."
2774 :value-create 'widget-visibility-value-create
2775 :action 'widget-toggle-action
2776 :match (lambda (widget value) t))
2778 (defun widget-visibility-value-create (widget)
2779 ;; Insert text representing the `on' and `off' states.
2780 (let ((on (widget-get widget :on))
2781 (off (widget-get widget :off)))
2783 (setq on (concat widget-push-button-prefix
2785 widget-push-button-suffix))
2788 (setq off (concat widget-push-button-prefix
2790 widget-push-button-suffix))
2792 (if (widget-value widget)
2793 (widget-image-insert widget on "down" "down-pushed")
2794 (widget-image-insert widget off "right" "right-pushed"))))
2796 ;;; The `documentation-link' Widget.
2798 ;; This is a helper widget for `documentation-string'.
2800 (define-widget 'documentation-link 'link
2801 "Link type used in documentation strings."
2803 :help-echo "Describe this symbol"
2804 :action 'widget-documentation-link-action)
2806 (defun widget-documentation-link-action (widget &optional event)
2807 "Display documentation for WIDGET's value. Ignore optional argument EVENT."
2808 (let* ((string (widget-get widget :value))
2809 (symbol (intern string)))
2810 (if (and (fboundp symbol) (boundp symbol))
2811 ;; If there are two doc strings, give the user a way to pick one.
2812 (apropos (concat "\\`" (regexp-quote string) "\\'"))
2813 (if (fboundp symbol)
2814 (describe-function symbol)
2815 (describe-variable symbol)))))
2817 (defcustom widget-documentation-links t
2818 "Add hyperlinks to documentation strings when non-nil."
2820 :group 'widget-documentation)
2822 (defcustom widget-documentation-link-regexp "`\\([^\n`' ]+\\)'"
2823 "Regexp for matching potential links in documentation strings.
2824 The first group should be the link itself."
2826 :group 'widget-documentation)
2828 (defcustom widget-documentation-link-p 'intern-soft
2829 "Predicate used to test if a string is useful as a link.
2830 The value should be a function. The function will be called one
2831 argument, a string, and should return non-nil if there should be a
2832 link for that string."
2834 :options '(widget-documentation-link-p)
2835 :group 'widget-documentation)
2837 (defcustom widget-documentation-link-type 'documentation-link
2838 "Widget type used for links in documentation strings."
2840 :group 'widget-documentation)
2842 (defun widget-documentation-link-add (widget from to)
2843 (widget-specify-doc widget from to)
2844 (when widget-documentation-links
2845 (let ((regexp widget-documentation-link-regexp)
2846 (buttons (widget-get widget :buttons))
2847 (widget-mouse-face (default-value 'widget-mouse-face))
2848 (widget-button-face widget-documentation-face)
2849 (widget-button-pressed-face widget-documentation-face))
2852 (while (re-search-forward regexp to t)
2853 (let ((name (match-string 1))
2854 (begin (match-beginning 1))
2855 (end (match-end 1)))
2856 (when (funcall widget-documentation-link-p name)
2857 (push (widget-convert-button widget-documentation-link-type
2858 begin end :value name)
2860 (widget-put widget :buttons buttons)))
2861 (let ((indent (widget-get widget :indent)))
2862 (when (and indent (not (zerop indent)))
2865 (narrow-to-region from to)
2866 (goto-char (point-min))
2867 (while (search-forward "\n" nil t)
2868 (insert-char ?\ indent)))))))
2870 ;;; The `documentation-string' Widget.
2872 (define-widget 'documentation-string 'item
2873 "A documentation string."
2875 :action 'widget-documentation-string-action
2876 :value-create 'widget-documentation-string-value-create)
2878 (defun widget-documentation-string-value-create (widget)
2879 ;; Insert documentation string.
2880 (let ((doc (widget-value widget))
2881 (indent (widget-get widget :indent))
2882 (shown (widget-get (widget-get widget :parent) :documentation-shown))
2884 (if (string-match "\n" doc)
2885 (let ((before (substring doc 0 (match-beginning 0)))
2886 (after (substring doc (match-beginning 0)))
2889 (widget-documentation-link-add widget start (point))
2891 (widget-create-child-and-convert
2893 :help-echo "Show or hide rest of the documentation."
2897 :action 'widget-parent-action
2900 (setq start (point))
2901 (when (and indent (not (zerop indent)))
2902 (insert-char ?\ indent))
2904 (widget-documentation-link-add widget start (point)))
2905 (widget-put widget :buttons (list button)))
2907 (widget-documentation-link-add widget start (point))))
2910 (defun widget-documentation-string-action (widget &rest ignore)
2911 ;; Toggle documentation.
2912 (let ((parent (widget-get widget :parent)))
2913 (widget-put parent :documentation-shown
2914 (not (widget-get parent :documentation-shown))))
2916 (widget-value-set widget (widget-value widget)))
2918 ;;; The Sexp Widgets.
2920 (define-widget 'const 'item
2921 "An immutable sexp."
2922 :prompt-value 'widget-const-prompt-value
2925 (defun widget-const-prompt-value (widget prompt value unbound)
2926 ;; Return the value of the const.
2927 (widget-value widget))
2929 (define-widget 'function-item 'const
2930 "An immutable function name."
2932 :documentation-property (lambda (symbol)
2934 (documentation symbol t)
2937 (define-widget 'variable-item 'const
2938 "An immutable variable name."
2940 :documentation-property 'variable-documentation)
2942 (define-widget 'other 'sexp
2943 "Matches any value, but doesn't let the user edit the value.
2944 This is useful as last item in a `choice' widget.
2945 You should use this widget type with a default value,
2946 as in (other DEFAULT) or (other :tag \"NAME\" DEFAULT).
2947 If the user selects this alternative, that specifies DEFAULT
2953 (defvar widget-string-prompt-value-history nil
2954 "History of input to `widget-string-prompt-value'.")
2956 (define-widget 'string 'editable-field
2959 :format "%{%t%}: %v"
2960 :complete-function 'ispell-complete-word
2961 :prompt-history 'widget-string-prompt-value-history)
2963 (define-widget 'regexp 'string
2964 "A regular expression."
2965 :match 'widget-regexp-match
2966 :validate 'widget-regexp-validate
2967 ;; Doesn't work well with terminating newline.
2968 ;; :value-face 'widget-single-line-field
2971 (defun widget-regexp-match (widget value)
2972 ;; Match valid regexps.
2973 (and (stringp value)
2976 (string-match value ""))
2979 (defun widget-regexp-validate (widget)
2980 "Check that the value of WIDGET is a valid regexp."
2981 (condition-case data
2983 (string-match (widget-value widget) ""))
2984 (error (widget-put widget :error (error-message-string data))
2987 (define-widget 'file 'string
2989 It will read a file name from the minibuffer when invoked."
2990 :complete-function 'widget-file-complete
2991 :prompt-value 'widget-file-prompt-value
2992 :format "%{%t%}: %v"
2993 ;; Doesn't work well with terminating newline.
2994 ;; :value-face 'widget-single-line-field
2997 (defun widget-file-complete ()
2998 "Perform completion on file name preceding point."
3000 (let* ((end (point))
3001 (beg (save-excursion
3002 (skip-chars-backward "^ ")
3004 (pattern (buffer-substring beg end))
3005 (name-part (file-name-nondirectory pattern))
3006 (directory (file-name-directory pattern))
3007 (completion (file-name-completion name-part directory)))
3008 (cond ((eq completion t))
3010 (message "Can't find completion for \"%s\"" pattern)
3012 ((not (string= name-part completion))
3013 (delete-region beg end)
3014 (insert (expand-file-name completion directory)))
3016 (message "Making completion list...")
3017 (with-output-to-temp-buffer "*Completions*"
3018 (display-completion-list
3019 (sort (file-name-all-completions name-part directory)
3021 (message "Making completion list...%s" "done")))))
3023 (defun widget-file-prompt-value (widget prompt value unbound)
3024 ;; Read file from minibuffer.
3025 (abbreviate-file-name
3027 (read-file-name prompt)
3028 (let ((prompt2 (format "%s (default %s) " prompt value))
3029 (dir (file-name-directory value))
3030 (file (file-name-nondirectory value))
3031 (must-match (widget-get widget :must-match)))
3032 (read-file-name prompt2 dir nil must-match file)))))
3034 ;;;(defun widget-file-action (widget &optional event)
3035 ;;; ;; Read a file name from the minibuffer.
3036 ;;; (let* ((value (widget-value widget))
3037 ;;; (dir (file-name-directory value))
3038 ;;; (file (file-name-nondirectory value))
3039 ;;; (menu-tag (widget-apply widget :menu-tag-get))
3040 ;;; (must-match (widget-get widget :must-match))
3041 ;;; (answer (read-file-name (concat menu-tag ": (default `" value "') ")
3042 ;;; dir nil must-match file)))
3043 ;;; (widget-value-set widget (abbreviate-file-name answer))
3045 ;;; (widget-apply widget :notify widget event)))
3047 ;; Fixme: use file-name-as-directory.
3048 (define-widget 'directory 'file
3049 "A directory widget.
3050 It will read a directory name from the minibuffer when invoked."
3053 (defvar widget-symbol-prompt-value-history nil
3054 "History of input to `widget-symbol-prompt-value'.")
3056 (define-widget 'symbol 'editable-field
3060 :format "%{%t%}: %v"
3061 :match (lambda (widget value) (symbolp value))
3062 :complete-function 'lisp-complete-symbol
3063 :prompt-internal 'widget-symbol-prompt-internal
3064 :prompt-match 'symbolp
3065 :prompt-history 'widget-symbol-prompt-value-history
3066 :value-to-internal (lambda (widget value)
3070 :value-to-external (lambda (widget value)
3075 (defun widget-symbol-prompt-internal (widget prompt initial history)
3076 ;; Read file from minibuffer.
3077 (let ((answer (completing-read prompt obarray
3078 (widget-get widget :prompt-match)
3079 nil initial history)))
3080 (if (and (stringp answer)
3081 (not (zerop (length answer))))
3083 (error "No value"))))
3085 (defvar widget-function-prompt-value-history nil
3086 "History of input to `widget-function-prompt-value'.")
3088 (define-widget 'function 'restricted-sexp
3090 :complete-function (lambda ()
3092 (lisp-complete-symbol 'fboundp))
3093 :prompt-value 'widget-field-prompt-value
3094 :prompt-internal 'widget-symbol-prompt-internal
3095 :prompt-match 'fboundp
3096 :prompt-history 'widget-function-prompt-value-history
3097 :action 'widget-field-action
3098 :match-alternatives '(functionp)
3099 :validate (lambda (widget)
3100 (unless (functionp (widget-value widget))
3101 (widget-put widget :error (format "Invalid function: %S"
3102 (widget-value widget)))
3107 (defvar widget-variable-prompt-value-history nil
3108 "History of input to `widget-variable-prompt-value'.")
3110 (define-widget 'variable 'symbol
3112 :prompt-match 'boundp
3113 :prompt-history 'widget-variable-prompt-value-history
3114 :complete-function (lambda ()
3116 (lisp-complete-symbol 'boundp))
3119 (defvar widget-coding-system-prompt-value-history nil
3120 "History of input to `widget-coding-system-prompt-value'.")
3122 (define-widget 'coding-system 'symbol
3123 "A MULE coding-system."
3124 :format "%{%t%}: %v"
3125 :tag "Coding system"
3127 :prompt-history 'widget-coding-system-prompt-value-history
3128 :prompt-value 'widget-coding-system-prompt-value
3129 :action 'widget-coding-system-action
3130 :complete-function (lambda ()
3132 (lisp-complete-symbol 'coding-system-p))
3133 :validate (lambda (widget)
3134 (unless (coding-system-p (widget-value widget))
3135 (widget-put widget :error (format "Invalid coding system: %S"
3136 (widget-value widget)))
3139 :prompt-match 'coding-system-p)
3141 (defun widget-coding-system-prompt-value (widget prompt value unbound)
3142 "Read coding-system from minibuffer."
3143 (if (widget-get widget :base-only)
3145 (completing-read (format "%s (default %s) " prompt value)
3146 (mapcar #'list (coding-system-list t)) nil nil nil
3147 coding-system-history))
3148 (read-coding-system (format "%s (default %s) " prompt value) value)))
3150 (defun widget-coding-system-action (widget &optional event)
3152 (widget-coding-system-prompt-value
3154 (widget-apply widget :menu-tag-get)
3155 (widget-value widget)
3157 (widget-value-set widget answer)
3158 (widget-apply widget :notify widget event)
3161 (define-widget 'sexp 'editable-field
3162 "An arbitrary Lisp expression."
3163 :tag "Lisp expression"
3164 :format "%{%t%}: %v"
3166 :validate 'widget-sexp-validate
3167 :match (lambda (widget value) t)
3168 :value-to-internal 'widget-sexp-value-to-internal
3169 :value-to-external (lambda (widget value) (read value))
3170 :prompt-history 'widget-sexp-prompt-value-history
3171 :prompt-value 'widget-sexp-prompt-value)
3173 (defun widget-sexp-value-to-internal (widget value)
3174 ;; Use pp for printer representation.
3175 (let ((pp (if (symbolp value)
3176 (prin1-to-string value)
3177 (pp-to-string value))))
3178 (while (string-match "\n\\'" pp)
3179 (setq pp (substring pp 0 -1)))
3180 (if (or (string-match "\n\\'" pp)
3185 (defun widget-sexp-validate (widget)
3186 ;; Valid if we can read the string and there is no junk left after it.
3188 (insert (widget-apply widget :value-get))
3189 (goto-char (point-min))
3191 (condition-case data
3193 ;; Avoid a confusing end-of-file error.
3194 (skip-syntax-forward "\\s-")
3196 (setq err "Empty sexp -- use `nil'?")
3197 (unless (widget-apply widget :match (read (current-buffer)))
3198 (setq err (widget-get widget :type-error))))
3199 ;; Allow whitespace after expression.
3200 (skip-syntax-forward "\\s-")
3201 (if (and (not (eobp))
3203 (setq err (format "Junk at end of expression: %s"
3204 (buffer-substring (point)
3206 (end-of-file ; Avoid confusing error message.
3207 (setq err "Unbalanced sexp"))
3208 (error (setq err (error-message-string data))))
3211 (widget-put widget :error err)
3214 (defvar widget-sexp-prompt-value-history nil
3215 "History of input to `widget-sexp-prompt-value'.")
3217 (defun widget-sexp-prompt-value (widget prompt value unbound)
3218 ;; Read an arbitrary sexp.
3219 (let ((found (read-string prompt
3220 (if unbound nil (cons (prin1-to-string value) 0))
3221 (widget-get widget :prompt-history))))
3222 (let ((answer (read-from-string found)))
3223 (unless (= (cdr answer) (length found))
3224 (error "Junk at end of expression: %s"
3225 (substring found (cdr answer))))
3228 (define-widget 'restricted-sexp 'sexp
3229 "A Lisp expression restricted to values that match.
3230 To use this type, you must define :match or :match-alternatives."
3231 :type-error "The specified value is not valid"
3232 :match 'widget-restricted-sexp-match
3233 :value-to-internal (lambda (widget value)
3234 (if (widget-apply widget :match value)
3235 (prin1-to-string value)
3238 (defun widget-restricted-sexp-match (widget value)
3239 (let ((alternatives (widget-get widget :match-alternatives))
3241 (while (and alternatives (not matched))
3242 (if (cond ((functionp (car alternatives))
3243 (funcall (car alternatives) value))
3244 ((and (consp (car alternatives))
3245 (eq (car (car alternatives)) 'quote))
3246 (eq value (nth 1 (car alternatives)))))
3248 (setq alternatives (cdr alternatives)))
3251 (define-widget 'integer 'restricted-sexp
3255 :type-error "This field should contain an integer"
3256 :match-alternatives '(integerp))
3258 (define-widget 'number 'restricted-sexp
3259 "A number (floating point or integer)."
3262 :type-error "This field should contain a number (floating point or integer)"
3263 :match-alternatives '(numberp))
3265 (define-widget 'float 'restricted-sexp
3266 "A floating point number."
3267 :tag "Floating point number"
3269 :type-error "This field should contain a floating point number"
3270 :match-alternatives '(floatp))
3272 (define-widget 'character 'editable-field
3277 :format "%{%t%}: %v\n"
3278 :valid-regexp "\\`.\\'"
3279 :error "This field should contain a single character"
3280 :value-to-internal (lambda (widget value)
3283 (char-to-string value)))
3284 :value-to-external (lambda (widget value)
3288 :match (lambda (widget value)
3289 (char-valid-p value)))
3291 (define-widget 'list 'group
3294 :format "%{%t%}:\n%v")
3296 (define-widget 'vector 'group
3299 :format "%{%t%}:\n%v"
3300 :match 'widget-vector-match
3301 :value-to-internal (lambda (widget value) (append value nil))
3302 :value-to-external (lambda (widget value) (apply 'vector value)))
3304 (defun widget-vector-match (widget value)
3305 (and (vectorp value)
3306 (widget-group-match widget
3307 (widget-apply widget :value-to-internal value))))
3309 (define-widget 'cons 'group
3312 :format "%{%t%}:\n%v"
3313 :match 'widget-cons-match
3314 :value-to-internal (lambda (widget value)
3315 (list (car value) (cdr value)))
3316 :value-to-external (lambda (widget value)
3317 (apply 'cons value)))
3319 (defun widget-cons-match (widget value)
3321 (widget-group-match widget
3322 (widget-apply widget :value-to-internal value))))
3324 ;;; The `lazy' Widget.
3326 ;; Recursive datatypes.
3328 (define-widget 'lazy 'default
3329 "Base widget for recursive datastructures.
3331 The `lazy' widget will, when instantiated, contain a single inferior
3332 widget, of the widget type specified by the :type parameter. The
3333 value of the `lazy' widget is the same as the value of the inferior
3334 widget. When deriving a new widget from the 'lazy' widget, the :type
3335 parameter is allowed to refer to the widget currently being defined,
3336 thus allowing recursive datastructures to be described.
3338 The :type parameter takes the same arguments as the defcustom
3339 parameter with the same name.
3341 Most composite widgets, i.e. widgets containing other widgets, does
3342 not allow recursion. That is, when you define a new widget type, none
3343 of the inferior widgets may be of the same type you are currently
3346 In Lisp, however, it is custom to define datastructures in terms of
3347 themselves. A list, for example, is defined as either nil, or a cons
3348 cell whose cdr itself is a list. The obvious way to translate this
3349 into a widget type would be
3351 (define-widget 'my-list 'choice
3352 \"A list of sexps.\"
3354 :args '((const nil) (cons :value (nil) sexp my-list)))
3356 Here we attempt to define my-list as a choice of either the constant
3357 nil, or a cons-cell containing a sexp and my-lisp. This will not work
3358 because the `choice' widget does not allow recursion.
3360 Using the `lazy' widget you can overcome this problem, as in this
3363 (define-widget 'sexp-list 'lazy
3364 \"A list of sexps.\"
3366 :type '(choice (const nil) (cons :value (nil) sexp sexp-list)))"
3367 :format "%{%t%}: %v"
3368 ;; We don't convert :type because we want to allow recursive
3369 ;; datastructures. This is slow, so we should not create speed
3370 ;; critical widgets by deriving from this.
3371 :convert-widget 'widget-value-convert-widget
3372 :value-create 'widget-type-value-create
3373 :value-get 'widget-child-value-get
3374 :value-inline 'widget-child-value-inline
3375 :default-get 'widget-type-default-get
3376 :match 'widget-type-match
3377 :validate 'widget-child-validate)
3380 ;;; The `plist' Widget.
3384 (define-widget 'plist 'list
3386 :key-type '(symbol :tag "Key")
3387 :value-type '(sexp :tag "Value")
3388 :convert-widget 'widget-plist-convert-widget
3391 (defvar widget-plist-value-type) ;Dynamic variable
3393 (defun widget-plist-convert-widget (widget)
3394 ;; Handle `:options'.
3395 (let* ((options (widget-get widget :options))
3396 (widget-plist-value-type (widget-get widget :value-type))
3397 (other `(editable-list :inline t
3399 ,(widget-get widget :key-type)
3400 ,widget-plist-value-type)))
3402 (list `(checklist :inline t
3404 ,@(mapcar 'widget-plist-convert-option
3408 (widget-put widget :args args)
3411 (defun widget-plist-convert-option (option)
3412 ;; Convert a single plist option.
3413 (let (key-type value-type)
3415 (let ((key (nth 0 option)))
3416 (setq value-type (nth 1 option))
3419 (setq key-type `(const ,key))))
3420 (setq key-type `(const ,option)
3421 value-type widget-plist-value-type))
3422 `(group :format "Key: %v" :inline t ,key-type ,value-type)))
3425 ;;; The `alist' Widget.
3427 ;; Association lists.
3429 (define-widget 'alist 'list
3430 "An association list."
3431 :key-type '(sexp :tag "Key")
3432 :value-type '(sexp :tag "Value")
3433 :convert-widget 'widget-alist-convert-widget
3436 (defvar widget-alist-value-type) ;Dynamic variable
3438 (defun widget-alist-convert-widget (widget)
3439 ;; Handle `:options'.
3440 (let* ((options (widget-get widget :options))
3441 (widget-alist-value-type (widget-get widget :value-type))
3442 (other `(editable-list :inline t
3444 ,(widget-get widget :key-type)
3445 ,widget-alist-value-type)))
3447 (list `(checklist :inline t
3449 ,@(mapcar 'widget-alist-convert-option
3453 (widget-put widget :args args)
3456 (defun widget-alist-convert-option (option)
3457 ;; Convert a single alist option.
3458 (let (key-type value-type)
3460 (let ((key (nth 0 option)))
3461 (setq value-type (nth 1 option))
3464 (setq key-type `(const ,key))))
3465 (setq key-type `(const ,option)
3466 value-type widget-alist-value-type))
3467 `(cons :format "Key: %v" ,key-type ,value-type)))
3469 (define-widget 'choice 'menu-choice
3470 "A union of several sexp types."
3472 :format "%{%t%}: %[Value Menu%] %v"
3473 :button-prefix 'widget-push-button-prefix
3474 :button-suffix 'widget-push-button-suffix
3475 :prompt-value 'widget-choice-prompt-value)
3477 (defun widget-choice-prompt-value (widget prompt value unbound)
3479 (let ((args (widget-get widget :args))
3480 (completion-ignore-case (widget-get widget :case-fold))
3481 current choices old)
3482 ;; Find the first arg that matches VALUE.
3485 (if (widget-apply (car look) :match value)
3486 (setq old (car look)
3488 (setq look (cdr look)))))
3491 (cond ((= (length args) 0)
3493 ((= (length args) 1)
3495 ((and (= (length args) 2)
3497 (if (eq old (nth 0 args))
3502 (setq current (car args)
3505 (cons (cons (widget-apply current :menu-tag-get)
3508 (let ((val (completing-read prompt choices nil t)))
3510 (let ((try (try-completion val choices)))
3513 (cdr (assoc val choices)))
3516 (widget-prompt-value current prompt nil t)
3519 (define-widget 'radio 'radio-button-choice
3520 "A union of several sexp types."
3522 :format "%{%t%}:\n%v"
3523 :prompt-value 'widget-choice-prompt-value)
3525 (define-widget 'repeat 'editable-list
3526 "A variable length homogeneous list."
3528 :format "%{%t%}:\n%v%i\n")
3530 (define-widget 'set 'checklist
3531 "A list of members from a fixed set."
3533 :format "%{%t%}:\n%v")
3535 (define-widget 'boolean 'toggle
3536 "To be nil or non-nil, that is the question."
3538 :prompt-value 'widget-boolean-prompt-value
3539 :button-prefix 'widget-push-button-prefix
3540 :button-suffix 'widget-push-button-suffix
3541 :format "%{%t%}: %[Toggle%] %v\n"
3545 (defun widget-boolean-prompt-value (widget prompt value unbound)
3546 ;; Toggle a boolean.
3549 ;;; The `color' Widget.
3552 (define-widget 'color 'editable-field
3553 "Choose a color name (with sample)."
3554 :format "%t: %v (%{sample%})\n"
3558 :complete 'widget-color-complete
3559 :sample-face-get 'widget-color-sample-face-get
3560 :notify 'widget-color-notify
3561 :action 'widget-color-action)
3563 (defun widget-color-complete (widget)
3564 "Complete the color in WIDGET."
3565 (require 'facemenu) ; for facemenu-color-alist
3566 (let* ((prefix (buffer-substring-no-properties (widget-field-start widget)
3568 (list (or facemenu-color-alist (defined-colors)))
3569 (completion (try-completion prefix list)))
3570 (cond ((eq completion t)
3571 (message "Exact match."))
3573 (error "Can't find completion for \"%s\"" prefix))
3574 ((not (string-equal prefix completion))
3575 (insert-and-inherit (substring completion (length prefix))))
3577 (message "Making completion list...")
3578 (with-output-to-temp-buffer "*Completions*"
3579 (display-completion-list (all-completions prefix list nil)))
3580 (message "Making completion list...done")))))
3582 (defun widget-color-sample-face-get (widget)
3583 (let* ((value (condition-case nil
3584 (widget-value widget)
3585 (error (widget-get widget :value)))))
3586 (if (color-defined-p value)
3587 (list (cons 'foreground-color value))
3590 (defun widget-color-action (widget &optional event)
3591 "Prompt for a color."
3592 (let* ((tag (widget-apply widget :menu-tag-get))
3593 (prompt (concat tag ": "))
3594 (value (widget-value widget))
3595 (start (widget-field-start widget))
3596 (answer (facemenu-read-color prompt)))
3597 (unless (zerop (length answer))
3598 (widget-value-set widget answer)
3600 (widget-apply widget :notify widget event))))
3602 (defun widget-color-notify (widget child &optional event)
3603 "Update the sample, and notofy the parent."
3604 (overlay-put (widget-get widget :sample-overlay)
3605 'face (widget-apply widget :sample-face-get))
3606 (widget-default-notify widget child event))
3610 (defun widget-echo-help (pos)
3611 "Display help-echo text for widget at POS."
3612 (let* ((widget (widget-at pos))
3613 (help-echo (and widget (widget-get widget :help-echo))))
3614 (if (functionp help-echo)
3615 (setq help-echo (funcall help-echo widget)))
3616 (if help-echo (message "%s" (eval help-echo)))))
3622 ;;; arch-tag: a076e75e-18a1-4b46-8be5-3f317bcbc707
3623 ;;; wid-edit.el ends here