1 ;;; wid-edit.el --- Functions for creating and using widgets -*-byte-compile-dynamic: t;-*-
3 ;; Copyright (C) 1996, 1997, 1999, 2000, 2001, 2002, 2003,
4 ;; 2004, 2005 Free Software Foundation, Inc.
6 ;; Author: Per Abrahamsen <abraham@dina.kvl.dk>
8 ;; Keywords: extensions
10 ;; This file is part of GNU Emacs.
12 ;; GNU Emacs is free software; you can redistribute it and/or modify
13 ;; it under the terms of the GNU General Public License as published by
14 ;; the Free Software Foundation; either version 2, or (at your option)
17 ;; GNU Emacs is distributed in the hope that it will be useful,
18 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
19 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 ;; GNU General Public License for more details.
22 ;; You should have received a copy of the GNU General Public License
23 ;; along with GNU Emacs; see the file COPYING. If not, write to the
24 ;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
25 ;; Boston, MA 02110-1301, USA.
27 ;;; Wishlist items (from widget.texi):
29 ;; * The `menu-choice' tag should be prettier, something like the
30 ;; abbreviated menus in Open Look.
32 ;; * Finish `:tab-order'.
34 ;; * Make indentation work with glyphs and proportional fonts.
36 ;; * Add commands to show overview of object and class hierarchies to
39 ;; * Find a way to disable mouse highlight for inactive widgets.
41 ;; * Find a way to make glyphs look inactive.
43 ;; * Add `key-binding' widget.
45 ;; * Add `widget' widget for editing widget specifications.
47 ;; * Find clean way to implement variable length list. See
48 ;; `TeX-printer-list' for an explanation.
50 ;; * `C-h' in `widget-prompt-value' should give type specific help.
52 ;; * A mailto widget. [This should work OK as a url-link if with
53 ;; browse-url-browser-function' set up appropriately.]
65 (defun widget-event-point (event)
66 "Character position of the end of event if that exists, or nil."
67 (posn-point (event-end event
)))
69 (defun widget-button-release-event-p (event)
70 "Non-nil if EVENT is a mouse-button-release event object."
72 (memq (event-basic-type event
) '(mouse-1 mouse-2 mouse-3
))
73 (or (memq 'click
(event-modifiers event
))
74 (memq 'drag
(event-modifiers event
)))))
79 "Customization support for the Widget Library."
80 :link
'(custom-manual "(widget)Top")
81 :link
'(emacs-library-link :tag
"Lisp File" "widget.el")
86 (defgroup widget-documentation nil
87 "Options controling the display of documentation strings."
90 (defgroup widget-faces nil
91 "Faces used by the widget library."
95 (defvar widget-documentation-face
'widget-documentation
96 "Face used for documentation strings in widgets.
97 This exists as a variable so it can be set locally in certain buffers.")
99 (defface widget-documentation
'((((class color
)
101 (:foreground
"lime green"))
104 (:foreground
"dark green"))
106 "Face used for documentation text."
107 :group
'widget-documentation
108 :group
'widget-faces
)
109 ;; backward compatibility alias
110 (put 'widget-documentation-face
'face-alias
'widget-documentation
)
112 (defvar widget-button-face
'widget-button
113 "Face used for buttons in widgets.
114 This exists as a variable so it can be set locally in certain buffers.")
116 (defface widget-button
'((t (:weight bold
)))
117 "Face used for widget buttons."
118 :group
'widget-faces
)
119 ;; backward compatibility alias
120 (put 'widget-button-face
'face-alias
'widget-button
)
122 (defcustom widget-mouse-face
'highlight
123 "Face used for widget buttons when the mouse is above them."
125 :group
'widget-faces
)
127 ;; TTY gets special definitions here and in the next defface, because
128 ;; the gray colors defined for other displays cause black text on a black
129 ;; background, at least on light-background TTYs.
130 (defface widget-field
'((((type tty
))
131 :background
"yellow3"
133 (((class grayscale color
)
135 :background
"gray85")
136 (((class grayscale color
)
138 :background
"dim gray")
141 "Face used for editable fields."
142 :group
'widget-faces
)
143 ;; backward-compatibility alias
144 (put 'widget-field-face
'face-alias
'widget-field
)
146 (defface widget-single-line-field
'((((type tty
))
149 (((class grayscale color
)
151 :background
"gray85")
152 (((class grayscale color
)
154 :background
"dim gray")
157 "Face used for editable fields spanning only a single line."
158 :group
'widget-faces
)
159 ;; backward-compatibility alias
160 (put 'widget-single-line-field-face
'face-alias
'widget-single-line-field
)
162 ;;; This causes display-table to be loaded, and not usefully.
163 ;;;(defvar widget-single-line-display-table
164 ;;; (let ((table (make-display-table)))
165 ;;; (aset table 9 "^I")
166 ;;; (aset table 10 "^J")
168 ;;; "Display table used for single-line editable fields.")
170 ;;;(when (fboundp 'set-face-display-table)
171 ;;; (set-face-display-table 'widget-single-line-field-face
172 ;;; widget-single-line-display-table))
174 ;;; Utility functions.
176 ;; These are not really widget specific.
178 (defun widget-princ-to-string (object)
179 "Return string representation of OBJECT, any Lisp object.
180 No quoting characters are used; no delimiters are printed around
181 the contents of strings."
182 (with-output-to-string
185 (defun widget-clear-undo ()
186 "Clear all undo information."
187 (buffer-disable-undo (current-buffer))
188 (buffer-enable-undo))
190 (defcustom widget-menu-max-size
40
191 "Largest number of items allowed in a popup-menu.
192 Larger menus are read through the minibuffer."
196 (defcustom widget-menu-max-shortcuts
40
197 "Largest number of items for which it works to choose one with a character.
198 For a larger number of items, the minibuffer is used."
202 (defcustom widget-menu-minibuffer-flag nil
203 "*Control how to ask for a choice from the keyboard.
204 Non-nil means use the minibuffer;
205 nil means read a single character."
209 (defun widget-choose (title items
&optional event
)
210 "Choose an item from a list.
212 First argument TITLE is the name of the list.
213 Second argument ITEMS is a list whose members are either
214 (NAME . VALUE), to indicate selectable items, or just strings to
215 indicate unselectable items.
216 Optional third argument EVENT is an input event.
218 The user is asked to choose between each NAME from the items alist,
219 and the VALUE of the chosen element will be returned. If EVENT is a
220 mouse event, and the number of elements in items is less than
221 `widget-menu-max-size', a popup menu will be used, otherwise the
223 (cond ((and (< (length items
) widget-menu-max-size
)
224 event
(display-popup-menus-p))
227 (list title
(cons "" items
))))
228 ((or widget-menu-minibuffer-flag
229 (> (length items
) widget-menu-max-shortcuts
))
230 ;; Read the choice of name from the minibuffer.
231 (setq items
(widget-remove-if 'stringp items
))
232 (let ((val (completing-read (concat title
": ") items nil t
)))
234 (let ((try (try-completion val items
)))
237 (cdr (assoc val items
))))))
239 ;; Construct a menu of the choices
240 ;; and then use it for prompting for a single character.
241 (let* ((overriding-terminal-local-map (make-sparse-keymap))
243 map choice some-choice-enabled value
)
244 ;; Define SPC as a prefix char to get to this menu.
245 (define-key overriding-terminal-local-map
" "
246 (setq map
(make-sparse-keymap title
)))
247 (with-current-buffer (get-buffer-create " widget-choose")
249 (insert "Available choices:\n\n")
251 (setq choice
(car items
) items
(cdr items
))
253 (let* ((name (car choice
))
254 (function (cdr choice
)))
255 (insert (format "%c = %s\n" next-digit name
))
256 (define-key map
(vector next-digit
) function
)
257 (setq some-choice-enabled t
)))
258 ;; Allocate digits to disabled alternatives
259 ;; so that the digit of a given alternative never varies.
260 (setq next-digit
(1+ next-digit
)))
261 (insert "\nC-g = Quit"))
262 (or some-choice-enabled
263 (error "None of the choices is currently meaningful"))
264 (define-key map
[?\C-g
] 'keyboard-quit
)
265 (define-key map
[t] 'keyboard-quit)
266 (define-key map [?\M-\C-v] 'scroll-other-window)
267 (define-key map [?\M--] 'negative-argument)
268 (setcdr map (nreverse (cdr map)))
269 ;; Read a char with the menu, and return the result
270 ;; that corresponds to it.
271 (save-window-excursion
272 (let ((buf (get-buffer " widget-choose")))
273 (fit-window-to-buffer (display-buffer buf))
274 (let ((cursor-in-echo-area t)
278 (while (not (or (and (>= char ?0) (< char next-digit))
279 (eq value 'keyboard-quit)))
280 ;; Unread a SPC to lead to our new menu.
281 (setq unread-command-events (cons ?\s unread-command-events))
282 (setq keys (read-key-sequence title))
284 (lookup-key overriding-terminal-local-map keys t)
285 char (string-to-char (substring keys 1)))
286 (cond ((eq value 'scroll-other-window)
287 (let ((minibuffer-scroll-window
288 (get-buffer-window buf)))
290 (scroll-other-window-down
291 (window-height minibuffer-scroll-window))
292 (scroll-other-window))
294 ((eq value 'negative-argument)
298 (when (eq value 'keyboard-quit)
302 (defun widget-remove-if (predictate list)
303 (let (result (tail list))
305 (or (funcall predictate (car tail))
306 (setq result (cons (car tail) result)))
307 (setq tail (cdr tail)))
310 ;;; Widget text specifications.
312 ;; These functions are for specifying text properties.
314 ;; We can set it to nil now that get_local_map uses get_pos_property.
315 (defconst widget-field-add-space nil
316 "Non-nil means add extra space at the end of editable text fields.
317 If you don't add the space, it will become impossible to edit a zero
320 (defvar widget-field-use-before-change t
321 "Non-nil means use `before-change-functions' to track editable fields.
322 This enables the use of undo, but doesn't work on Emacs 19.34 and earlier.
323 Using before hooks also means that the :notify function can't know the
326 (defun widget-specify-field (widget from to)
327 "Specify editable button for WIDGET between FROM and TO."
328 ;; Terminating space is not part of the field, but necessary in
329 ;; order for local-map to work. Remove next sexp if local-map works
330 ;; at the end of the overlay.
333 (cond ((null (widget-get widget :size))
335 (widget-field-add-space
336 (insert-and-inherit " ")))
338 (let ((keymap (widget-get widget :keymap))
339 (face (or (widget-get widget :value-face) 'widget-field))
340 (help-echo (widget-get widget :help-echo))
341 (follow-link (widget-get widget :follow-link))
343 (or (not widget-field-add-space) (widget-get widget :size))))
344 (if (functionp help-echo)
345 (setq help-echo 'widget-mouse-help))
346 (when (= (char-before to) ?\n)
347 ;; When the last character in the field is a newline, we want to
348 ;; give it a `field' char-property of `boundary', which helps the
349 ;; C-n/C-p act more naturally when entering/leaving the field. We
350 ;; do this by making a small secondary overlay to contain just that
352 (let ((overlay (make-overlay (1- to) to nil t nil)))
353 (overlay-put overlay 'field 'boundary)
354 ;; We need the real field for tabbing.
355 (overlay-put overlay 'real-field widget)
356 ;; Use `local-map' here, not `keymap', so that normal editing
357 ;; works in the field when, say, Custom uses `suppress-keymap'.
358 (overlay-put overlay 'local-map keymap)
359 (overlay-put overlay 'face face)
360 (overlay-put overlay 'follow-link follow-link)
361 (overlay-put overlay 'help-echo help-echo))
363 (setq rear-sticky t))
364 (let ((overlay (make-overlay from to nil nil rear-sticky)))
365 (widget-put widget :field-overlay overlay)
366 ;;(overlay-put overlay 'detachable nil)
367 (overlay-put overlay 'field widget)
368 (overlay-put overlay 'local-map keymap)
369 (overlay-put overlay 'face face)
370 (overlay-put overlay 'follow-link follow-link)
371 (overlay-put overlay 'help-echo help-echo)))
372 (widget-specify-secret widget))
374 (defun widget-specify-secret (field)
375 "Replace text in FIELD with value of `:secret', if non-nil."
376 (let ((secret (widget-get field :secret))
377 (size (widget-get field :size)))
379 (let ((begin (widget-field-start field))
380 (end (widget-field-end field)))
382 (while (and (> end begin)
383 (eq (char-after (1- end)) ?\s))
384 (setq end (1- end))))
386 (let ((old (char-after begin)))
387 (unless (eq old secret)
388 (subst-char-in-region begin (1+ begin) old secret)
389 (put-text-property begin (1+ begin) 'secret old))
390 (setq begin (1+ begin))))))))
392 (defun widget-specify-button (widget from to)
393 "Specify button for WIDGET between FROM and TO."
394 (let ((overlay (make-overlay from to nil t nil))
395 (follow-link (widget-get widget :follow-link))
396 (help-echo (widget-get widget :help-echo)))
397 (widget-put widget :button-overlay overlay)
398 (if (functionp help-echo)
399 (setq help-echo 'widget-mouse-help))
400 (overlay-put overlay 'button widget)
401 (overlay-put overlay 'keymap (widget-get widget :keymap))
402 (overlay-put overlay 'evaporate t)
403 ;; We want to avoid the face with image buttons.
404 (unless (widget-get widget :suppress-face)
405 (overlay-put overlay 'face (widget-apply widget :button-face-get))
406 ; Text terminals cannot change mouse pointer shape, so use mouse
408 (or (display-graphic-p)
409 (overlay-put overlay 'mouse-face widget-mouse-face)))
410 (overlay-put overlay 'pointer 'hand)
411 (overlay-put overlay 'follow-link follow-link)
412 (overlay-put overlay 'help-echo help-echo)))
414 (defun widget-mouse-help (window overlay point)
415 "Help-echo callback for widgets whose :help-echo is a function."
416 (with-current-buffer (overlay-buffer overlay)
417 (let* ((widget (widget-at (overlay-start overlay)))
418 (help-echo (if widget (widget-get widget :help-echo))))
419 (if (functionp help-echo)
420 (funcall help-echo widget)
423 (defun widget-specify-sample (widget from to)
424 "Specify sample for WIDGET between FROM and TO."
425 (let ((overlay (make-overlay from to nil t nil)))
426 (overlay-put overlay 'face (widget-apply widget :sample-face-get))
427 (overlay-put overlay 'evaporate t)
428 (widget-put widget :sample-overlay overlay)))
430 (defun widget-specify-doc (widget from to)
431 "Specify documentation for WIDGET between FROM and TO."
432 (let ((overlay (make-overlay from to nil t nil)))
433 (overlay-put overlay 'widget-doc widget)
434 (overlay-put overlay 'face widget-documentation-face)
435 (overlay-put overlay 'evaporate t)
436 (widget-put widget :doc-overlay overlay)))
438 (defmacro widget-specify-insert (&rest form)
439 "Execute FORM without inheriting any text properties."
441 (let ((inhibit-read-only t)
442 (inhibit-modification-hooks t))
443 (narrow-to-region (point) (point))
444 (prog1 (progn ,@form)
445 (goto-char (point-max))))))
447 (defface widget-inactive
448 '((t :inherit shadow))
449 "Face used for inactive widgets."
450 :group 'widget-faces)
451 ;; backward-compatibility alias
452 (put 'widget-inactive-face 'face-alias 'widget-inactive)
454 (defun widget-specify-inactive (widget from to)
455 "Make WIDGET inactive for user modifications."
456 (unless (widget-get widget :inactive)
457 (let ((overlay (make-overlay from to nil t nil)))
458 (overlay-put overlay 'face 'widget-inactive)
459 ;; This is disabled, as it makes the mouse cursor change shape.
460 ;; (overlay-put overlay 'mouse-face 'widget-inactive)
461 (overlay-put overlay 'evaporate t)
462 (overlay-put overlay 'priority 100)
463 (overlay-put overlay 'modification-hooks '(widget-overlay-inactive))
464 (widget-put widget :inactive overlay))))
466 (defun widget-overlay-inactive (&rest junk)
467 "Ignoring the arguments, signal an error."
468 (unless inhibit-read-only
469 (error "The widget here is not active")))
472 (defun widget-specify-active (widget)
473 "Make WIDGET active for user modifications."
474 (let ((inactive (widget-get widget :inactive)))
476 (delete-overlay inactive)
477 (widget-put widget :inactive nil))))
479 ;;; Widget Properties.
481 (defsubst widget-type (widget)
482 "Return the type of WIDGET, a symbol."
486 (defun widgetp (widget)
487 "Return non-nil iff WIDGET is a widget."
489 (get widget 'widget-type)
491 (symbolp (car widget))
492 (get (car widget) 'widget-type))))
494 (defun widget-get-indirect (widget property)
495 "In WIDGET, get the value of PROPERTY.
496 If the value is a symbol, return its binding.
497 Otherwise, just return the value."
498 (let ((value (widget-get widget property)))
503 (defun widget-member (widget property)
504 "Non-nil iff there is a definition in WIDGET for PROPERTY."
505 (cond ((plist-member (cdr widget) property)
508 (widget-member (get (car widget) 'widget-type) property))
511 (defun widget-value (widget)
512 "Extract the current value of WIDGET."
514 :value-to-external (widget-apply widget :value-get)))
516 (defun widget-value-set (widget value)
517 "Set the current value of WIDGET to VALUE."
519 :value-set (widget-apply widget
520 :value-to-internal value)))
522 (defun widget-default-get (widget)
523 "Extract the default external value of WIDGET."
524 (widget-apply widget :value-to-external
525 (or (widget-get widget :value)
526 (widget-apply widget :default-get))))
528 (defun widget-match-inline (widget vals)
529 "In WIDGET, match the start of VALS."
530 (cond ((widget-get widget :inline)
531 (widget-apply widget :match-inline vals))
533 (widget-apply widget :match (car vals)))
534 (cons (list (car vals)) (cdr vals)))
537 (defun widget-apply-action (widget &optional event)
538 "Apply :action in WIDGET in response to EVENT."
539 (if (widget-apply widget :active)
540 (widget-apply widget :action event)
541 (error "Attempt to perform action on inactive widget")))
543 ;;; Helper functions.
545 ;; These are widget specific.
548 (defun widget-prompt-value (widget prompt &optional value unbound)
549 "Prompt for a value matching WIDGET, using PROMPT.
550 The current value is assumed to be VALUE, unless UNBOUND is non-nil."
551 (unless (listp widget)
552 (setq widget (list widget)))
553 (setq prompt (format "[%s] %s" (widget-type widget) prompt))
554 (setq widget (widget-convert widget))
555 (let ((answer (widget-apply widget :prompt-value prompt value unbound)))
556 (unless (widget-apply widget :match answer)
557 (error "Value does not match %S type" (car widget)))
560 (defun widget-get-sibling (widget)
561 "Get the item WIDGET is assumed to toggle.
562 This is only meaningful for radio buttons or checkboxes in a list."
563 (let* ((children (widget-get (widget-get widget :parent) :children))
567 (setq child (car children)
568 children (cdr children))
569 (when (eq (widget-get child :button) widget)
570 (throw 'child child)))
573 (defun widget-map-buttons (function &optional buffer maparg)
574 "Map FUNCTION over the buttons in BUFFER.
575 FUNCTION is called with the arguments WIDGET and MAPARG.
577 If FUNCTION returns non-nil, the walk is cancelled.
579 The arguments MAPARG, and BUFFER default to nil and (current-buffer),
581 (let ((cur (point-min))
584 (with-current-buffer buffer (overlay-lists))
586 (setq overlays (append (car overlays) (cdr overlays)))
587 (while (setq cur (pop overlays))
588 (setq widget (overlay-get cur 'button))
589 (if (and widget (funcall function widget maparg))
590 (setq overlays nil)))))
594 (defcustom widget-image-directory (file-name-as-directory
595 (expand-file-name "custom" data-directory))
596 "Where widget button images are located.
597 If this variable is nil, widget will try to locate the directory
602 (defcustom widget-image-enable t
603 "If non nil, use image buttons in widgets when available."
608 (defcustom widget-image-conversion
609 '((xpm ".xpm") (gif ".gif") (png ".png") (jpeg ".jpg" ".jpeg")
611 "Conversion alist from image formats to file name suffixes."
613 :type '(repeat (cons :format "%v"
614 (symbol :tag "Image Format" unknown)
615 (repeat :tag "Suffixes"
616 (string :format "%v")))))
618 (defun widget-image-find (image)
619 "Create a graphical button from IMAGE.
620 IMAGE should either already be an image, or be a file name sans
621 extension (xpm, xbm, gif, jpg, or png) located in
622 `widget-image-directory' or otherwise where `find-image' will find it."
623 (cond ((not (and image widget-image-enable (display-graphic-p)))
624 ;; We don't want or can't use images.
627 (eq 'image (car image)))
628 ;; Already an image spec. Use it.
631 ;; A string. Look it up in relevant directories.
632 (let* ((load-path (cons widget-image-directory load-path))
634 (dolist (elt widget-image-conversion)
635 (dolist (ext (cdr elt))
636 (push (list :type (car elt) :file (concat image ext)) specs)))
637 (setq specs (nreverse specs))
643 (defvar widget-button-pressed-face 'widget-button-pressed
644 "Face used for pressed buttons in widgets.
645 This exists as a variable so it can be set locally in certain
648 (defun widget-image-insert (widget tag image &optional down inactive)
649 "In WIDGET, insert the text TAG or, if supported, IMAGE.
650 IMAGE should either be an image or an image file name sans extension
651 \(xpm, xbm, gif, jpg, or png) located in `widget-image-directory'.
653 Optional arguments DOWN and INACTIVE are used instead of IMAGE when the
654 button is pressed or inactive, respectively. These are currently ignored."
655 (if (and (display-graphic-p)
656 (setq image (widget-image-find image)))
657 (progn (widget-put widget :suppress-face t)
660 tag 'mouse-face widget-button-pressed-face)))
665 (defgroup widget-button nil
666 "The look of various kinds of buttons."
669 (defcustom widget-button-prefix ""
670 "String used as prefix for buttons."
672 :group 'widget-button)
674 (defcustom widget-button-suffix ""
675 "String used as suffix for buttons."
677 :group 'widget-button)
679 ;;; Creating Widgets.
682 (defun widget-create (type &rest args)
683 "Create widget of TYPE.
684 The optional ARGS are additional keyword arguments."
685 (let ((widget (apply 'widget-convert type args)))
686 (widget-apply widget :create)
689 (defun widget-create-child-and-convert (parent type &rest args)
690 "As part of the widget PARENT, create a child widget TYPE.
691 The child is converted, using the keyword arguments ARGS."
692 (let ((widget (apply 'widget-convert type args)))
693 (widget-put widget :parent parent)
694 (unless (widget-get widget :indent)
695 (widget-put widget :indent (+ (or (widget-get parent :indent) 0)
696 (or (widget-get widget :extra-offset) 0)
697 (widget-get parent :offset))))
698 (widget-apply widget :create)
701 (defun widget-create-child (parent type)
702 "Create widget of TYPE."
703 (let ((widget (widget-copy type)))
704 (widget-put widget :parent parent)
705 (unless (widget-get widget :indent)
706 (widget-put widget :indent (+ (or (widget-get parent :indent) 0)
707 (or (widget-get widget :extra-offset) 0)
708 (widget-get parent :offset))))
709 (widget-apply widget :create)
712 (defun widget-create-child-value (parent type value)
713 "Create widget of TYPE with value VALUE."
714 (let ((widget (widget-copy type)))
715 (widget-put widget :value (widget-apply widget :value-to-internal value))
716 (widget-put widget :parent parent)
717 (unless (widget-get widget :indent)
718 (widget-put widget :indent (+ (or (widget-get parent :indent) 0)
719 (or (widget-get widget :extra-offset) 0)
720 (widget-get parent :offset))))
721 (widget-apply widget :create)
725 (defun widget-delete (widget)
727 (widget-apply widget :delete))
729 (defun widget-copy (widget)
730 "Make a deep copy of WIDGET."
731 (widget-apply (copy-sequence widget) :copy))
733 (defun widget-convert (type &rest args)
734 "Convert TYPE to a widget without inserting it in the buffer.
735 The optional ARGS are additional keyword arguments."
736 ;; Don't touch the type.
737 (let* ((widget (if (symbolp type)
739 (copy-sequence type)))
743 ;; First set the :args keyword.
744 (while (cdr current) ;Look in the type.
745 (if (and (keywordp (cadr current))
746 ;; If the last element is a keyword,
747 ;; it is still the :args element,
748 ;; even though it is a keyword.
750 (if (eq (cadr current) :args)
751 ;; If :args is explicitly specified, obey it.
753 ;; Some other irrelevant keyword.
754 (setq current (cdr (cdr current))))
755 (setcdr current (list :args (cdr current)))
757 (while (and args (not done)) ;Look in ARGS.
758 (cond ((eq (car args) :args)
759 ;; Handle explicit specification of :args.
760 (setq args (cadr args)
762 ((keywordp (car args))
763 (setq args (cddr args)))
766 (widget-put widget :args args))
767 ;; Then Convert the widget.
770 (let ((convert-widget (plist-get (cdr type) :convert-widget)))
772 (setq widget (funcall convert-widget widget))))
773 (setq type (get (car type) 'widget-type)))
774 ;; Finally set the keyword args.
776 (let ((next (nth 0 keys)))
779 (widget-put widget next (nth 1 keys))
780 (setq keys (nthcdr 2 keys)))
782 ;; Convert the :value to internal format.
783 (if (widget-member widget :value)
785 :value (widget-apply widget
787 (widget-get widget :value))))
788 ;; Return the newly create widget.
792 (defun widget-insert (&rest args)
793 "Call `insert' with ARGS even if surrounding text is read only."
794 (let ((inhibit-read-only t)
795 (inhibit-modification-hooks t))
796 (apply 'insert args)))
798 (defun widget-convert-text (type from to
799 &optional button-from button-to
801 "Return a widget of type TYPE with endpoint FROM TO.
802 No text will be inserted to the buffer, instead the text between FROM
803 and TO will be used as the widgets end points. If optional arguments
804 BUTTON-FROM and BUTTON-TO are given, these will be used as the widgets
806 Optional ARGS are extra keyword arguments for TYPE."
807 (let ((widget (apply 'widget-convert type :delete 'widget-leave-text args))
808 (from (copy-marker from))
809 (to (copy-marker to)))
810 (set-marker-insertion-type from t)
811 (set-marker-insertion-type to nil)
812 (widget-put widget :from from)
813 (widget-put widget :to to)
815 (widget-specify-button widget button-from button-to))
818 (defun widget-convert-button (type from to &rest args)
819 "Return a widget of type TYPE with endpoint FROM TO.
820 Optional ARGS are extra keyword arguments for TYPE.
821 No text will be inserted to the buffer, instead the text between FROM
822 and TO will be used as the widgets end points, as well as the widgets
824 (apply 'widget-convert-text type from to from to args))
826 (defun widget-leave-text (widget)
827 "Remove markers and overlays from WIDGET and its children."
828 (let ((button (widget-get widget :button-overlay))
829 (sample (widget-get widget :sample-overlay))
830 (doc (widget-get widget :doc-overlay))
831 (field (widget-get widget :field-overlay)))
832 (set-marker (widget-get widget :from) nil)
833 (set-marker (widget-get widget :to) nil)
835 (delete-overlay button))
837 (delete-overlay sample))
839 (delete-overlay doc))
841 (delete-overlay field))
842 (mapc 'widget-leave-text (widget-get widget :children))))
844 ;;; Keymap and Commands.
847 (defvar widget-keymap
848 (let ((map (make-sparse-keymap)))
849 (define-key map "\t" 'widget-forward)
850 (define-key map [(shift tab)] 'widget-backward)
851 (define-key map [backtab] 'widget-backward)
852 (define-key map [mouse-1] 'widget-move-and-invoke)
853 (define-key map [down-mouse-2] 'widget-button-click)
854 (define-key map "\C-m" 'widget-button-press)
856 "Keymap containing useful binding for buffers containing widgets.
857 Recommended as a parent keymap for modes using widgets.")
859 (defvar widget-global-map global-map
860 "Keymap used for events a widget does not handle itself.")
861 (make-variable-buffer-local 'widget-global-map)
863 (defvar widget-field-keymap
864 (let ((map (copy-keymap widget-keymap)))
865 (define-key map "\C-k" 'widget-kill-line)
866 (define-key map "\M-\t" 'widget-complete)
867 (define-key map "\C-m" 'widget-field-activate)
868 ;; Since the widget code uses a `field' property to identify fields,
869 ;; ordinary beginning-of-line does the right thing.
870 ;; (define-key map "\C-a" 'widget-beginning-of-line)
871 (define-key map "\C-e" 'widget-end-of-line)
873 "Keymap used inside an editable field.")
875 (defvar widget-text-keymap
876 (let ((map (copy-keymap widget-keymap)))
877 ;; Since the widget code uses a `field' property to identify fields,
878 ;; ordinary beginning-of-line does the right thing.
879 ;; (define-key map "\C-a" 'widget-beginning-of-line)
880 (define-key map "\C-e" 'widget-end-of-line)
882 "Keymap used inside a text field.")
884 (defun widget-field-activate (pos &optional event)
885 "Invoke the editable field at point."
887 (let ((field (widget-field-at pos)))
889 (widget-apply-action field event)
891 (lookup-key widget-global-map (this-command-keys))))))
893 (defface widget-button-pressed
894 '((((min-colors 88) (class color))
895 (:foreground "red1"))
899 (:weight bold :underline t)))
900 "Face used for pressed buttons."
901 :group 'widget-faces)
902 ;; backward-compatibility alias
903 (put 'widget-button-pressed-face 'face-alias 'widget-button-pressed)
905 (defun widget-move-and-invoke (event)
906 "Move to where you click, and if it is an active field, invoke it."
908 (mouse-set-point event)
909 (let ((pos (widget-event-point event)))
910 (if (and pos (get-char-property pos 'button))
911 (widget-button-click event))))
913 (defun widget-button-click (event)
914 "Invoke the button that the mouse is pointing at."
916 (if (widget-event-point event)
917 (let* ((pos (widget-event-point event))
918 (start (event-start event))
919 (button (get-char-property
920 pos 'button (and (windowp (posn-window start))
921 (window-buffer (posn-window start))))))
923 ;; Mouse click on a widget button. Do the following
924 ;; in a save-excursion so that the click on the button
925 ;; doesn't change point.
926 (save-selected-window
927 (select-window (posn-window (event-start event)))
929 (goto-char (posn-point (event-start event)))
930 (let* ((overlay (widget-get button :button-overlay))
931 (face (overlay-get overlay 'face))
932 (mouse-face (overlay-get overlay 'mouse-face)))
934 ;; Read events, including mouse-movement events
935 ;; until we receive a release event. Highlight/
936 ;; unhighlight the button the mouse was initially
937 ;; on when we move over it.
939 (when face ; avoid changing around image
941 'face widget-button-pressed-face)
943 'mouse-face widget-button-pressed-face))
944 (unless (widget-apply button :mouse-down-action event)
945 (let ((track-mouse t))
946 (while (not (widget-button-release-event-p event))
947 (setq event (read-event)
948 pos (widget-event-point event))
950 (eq (get-char-property pos 'button)
955 widget-button-pressed-face)
958 widget-button-pressed-face))
959 (overlay-put overlay 'face face)
960 (overlay-put overlay 'mouse-face mouse-face)))))
962 ;; When mouse is released over the button, run
963 ;; its action function.
965 (eq (get-char-property pos 'button) button))
966 (widget-apply-action button event)))
967 (overlay-put overlay 'face face)
968 (overlay-put overlay 'mouse-face mouse-face))))
970 (unless (pos-visible-in-window-p (widget-event-point event))
971 (mouse-set-point event)
976 (let ((up t) command)
977 ;; Mouse click not on a widget button. Find the global
978 ;; command to run, and check whether it is bound to an
980 (mouse-set-point event)
981 (if (memq (event-basic-type event) '(mouse-1 down-mouse-1))
982 (cond ((setq command ;down event
983 (lookup-key widget-global-map [down-mouse-1]))
985 ((setq command ;up event
986 (lookup-key widget-global-map [mouse-1]))))
987 (cond ((setq command ;down event
988 (lookup-key widget-global-map [down-mouse-2]))
990 ((setq command ;up event
991 (lookup-key widget-global-map [mouse-2])))))
993 ;; Don't execute up events twice.
994 (while (not (widget-button-release-event-p event))
995 (setq event (read-event))))
997 (call-interactively command)))))
998 (message "You clicked somewhere weird.")))
1000 (defun widget-button-press (pos &optional event)
1001 "Invoke button at POS."
1003 (let ((button (get-char-property pos 'button)))
1005 (widget-apply-action button event)
1006 (let ((command (lookup-key widget-global-map (this-command-keys))))
1007 (when (commandp command)
1008 (call-interactively command))))))
1010 (defun widget-tabable-at (&optional pos)
1011 "Return the tabable widget at POS, or nil.
1012 POS defaults to the value of (point)."
1013 (let ((widget (widget-at pos)))
1015 (let ((order (widget-get widget :tab-order)))
1021 (defvar widget-use-overlay-change t
1022 "If non-nil, use overlay change functions to tab around in the buffer.
1023 This is much faster, but doesn't work reliably on Emacs 19.34.")
1025 (defun widget-move (arg)
1026 "Move point to the ARG next field or button.
1027 ARG may be negative to move backward."
1028 (or (bobp) (> arg 0) (backward-char))
1031 (old (widget-tabable-at)))
1035 (goto-char (point-min))
1036 (setq wrapped (1+ wrapped)))
1037 (widget-use-overlay-change
1038 (goto-char (next-overlay-change (point))))
1043 (error "No buttons or fields found"))
1044 (let ((new (widget-tabable-at)))
1046 (unless (eq new old)
1052 (goto-char (point-max))
1053 (setq wrapped (1+ wrapped)))
1054 (widget-use-overlay-change
1055 (goto-char (previous-overlay-change (point))))
1060 (error "No buttons or fields found"))
1061 (let ((new (widget-tabable-at)))
1063 (unless (eq new old)
1064 (setq arg (1+ arg))))))
1065 (let ((new (widget-tabable-at)))
1066 (while (eq (widget-tabable-at) new)
1069 (widget-echo-help (point))
1070 (run-hooks 'widget-move-hook))
1072 (defun widget-forward (arg)
1073 "Move point to the next field or button.
1074 With optional ARG, move across that many fields."
1076 (run-hooks 'widget-forward-hook)
1079 (defun widget-backward (arg)
1080 "Move point to the previous field or button.
1081 With optional ARG, move across that many fields."
1083 (run-hooks 'widget-backward-hook)
1084 (widget-move (- arg)))
1086 ;; Since the widget code uses a `field' property to identify fields,
1087 ;; ordinary beginning-of-line does the right thing.
1088 (defalias 'widget-beginning-of-line 'beginning-of-line)
1090 (defun widget-end-of-line ()
1091 "Go to end of field or end of line, whichever is first.
1092 Trailing spaces at the end of padded fields are not considered part of
1095 ;; Ordinary end-of-line does the right thing, because we're inside
1096 ;; text with a `field' property.
1099 ;; ... except that we want to ignore trailing spaces in fields that
1100 ;; aren't terminated by a newline, because they are used as padding,
1101 ;; and ignored when extracting the entered value of the field.
1102 (skip-chars-backward " " (field-beginning (1- (point))))))
1104 (defun widget-kill-line ()
1105 "Kill to end of field or end of line, whichever is first."
1107 (let* ((field (widget-field-find (point)))
1108 (end (and field (widget-field-end field))))
1109 (if (and field (> (line-beginning-position 2) end))
1110 (kill-region (point) end)
1111 (call-interactively 'kill-line))))
1113 (defcustom widget-complete-field (lookup-key global-map "\M-\t")
1114 "Default function to call for completion inside fields."
1115 :options '(ispell-complete-word complete-tag lisp-complete-symbol)
1119 (defun widget-narrow-to-field ()
1122 (let ((field (widget-field-find (point))))
1124 (narrow-to-region (line-beginning-position) (line-end-position)))))
1126 (defun widget-complete ()
1127 "Complete content of editable field from point.
1128 When not inside a field, move to the previous button or field."
1130 (let ((field (widget-field-find (point))))
1133 (widget-narrow-to-field)
1134 (widget-apply field :complete))
1135 (error "Not in an editable field"))))
1137 ;;; Setting up the buffer.
1139 (defvar widget-field-new nil
1140 "List of all newly created editable fields in the buffer.")
1141 (make-variable-buffer-local 'widget-field-new)
1143 (defvar widget-field-list nil
1144 "List of all editable fields in the buffer.")
1145 (make-variable-buffer-local 'widget-field-list)
1147 (defun widget-at (&optional pos)
1148 "The button or field at POS (default, point)."
1149 (or (get-char-property (or pos (point)) 'button)
1150 (widget-field-at pos)))
1153 (defun widget-setup ()
1154 "Setup current buffer so editing string widgets works."
1155 (let ((inhibit-read-only t)
1156 (inhibit-modification-hooks t)
1158 (while widget-field-new
1159 (setq field (car widget-field-new)
1160 widget-field-new (cdr widget-field-new)
1161 widget-field-list (cons field widget-field-list))
1162 (let ((from (car (widget-get field :field-overlay)))
1163 (to (cdr (widget-get field :field-overlay))))
1164 (widget-specify-field field
1165 (marker-position from) (marker-position to))
1166 (set-marker from nil)
1167 (set-marker to nil))))
1169 (widget-add-change))
1171 (defvar widget-field-last nil)
1172 ;; Last field containing point.
1173 (make-variable-buffer-local 'widget-field-last)
1175 (defvar widget-field-was nil)
1176 ;; The widget data before the change.
1177 (make-variable-buffer-local 'widget-field-was)
1179 (defun widget-field-at (pos)
1180 "Return the widget field at POS, or nil if none."
1181 (let ((field (get-char-property (or pos (point)) 'field)))
1182 (if (eq field 'boundary)
1183 (get-char-property (or pos (point)) 'real-field)
1186 (defun widget-field-buffer (widget)
1187 "Return the buffer of WIDGET's editing field."
1188 (let ((overlay (widget-get widget :field-overlay)))
1189 (cond ((overlayp overlay)
1190 (overlay-buffer overlay))
1192 (marker-buffer (car overlay))))))
1194 (defun widget-field-start (widget)
1195 "Return the start of WIDGET's editing field."
1196 (let ((overlay (widget-get widget :field-overlay)))
1197 (if (overlayp overlay)
1198 (overlay-start overlay)
1201 (defun widget-field-end (widget)
1202 "Return the end of WIDGET's editing field."
1203 (let ((overlay (widget-get widget :field-overlay)))
1204 ;; Don't subtract one if local-map works at the end of the overlay,
1205 ;; or if a special `boundary' field has been added after the widget
1207 (if (overlayp overlay)
1208 (if (and (not (eq (with-current-buffer
1209 (widget-field-buffer widget)
1211 ;; `widget-narrow-to-field' can be
1212 ;; active when this function is called
1213 ;; from an change-functions hook. So
1214 ;; temporarily remove field narrowing
1215 ;; before to call `get-char-property'.
1217 (get-char-property (overlay-end overlay)
1220 (or widget-field-add-space
1221 (null (widget-get widget :size))))
1222 (1- (overlay-end overlay))
1223 (overlay-end overlay))
1226 (defun widget-field-find (pos)
1227 "Return the field at POS.
1228 Unlike (get-char-property POS 'field), this works with empty fields too."
1229 (let ((fields widget-field-list)
1232 (setq field (car fields)
1233 fields (cdr fields))
1234 (when (and (<= (widget-field-start field) pos)
1235 (<= pos (widget-field-end field)))
1237 (error "Overlapping fields"))
1238 (setq found field)))
1241 (defun widget-before-change (from to)
1242 ;; This is how, for example, a variable changes its state to `modified'.
1243 ;; when it is being edited.
1244 (unless inhibit-read-only
1245 (let ((from-field (widget-field-find from))
1246 (to-field (widget-field-find to)))
1247 (cond ((not (eq from-field to-field))
1248 (add-hook 'post-command-hook 'widget-add-change nil t)
1249 (signal 'text-read-only
1250 '("Change should be restricted to a single field")))
1252 (add-hook 'post-command-hook 'widget-add-change nil t)
1253 (signal 'text-read-only
1254 '("Attempt to change text outside editable field")))
1255 (widget-field-use-before-change
1256 (widget-apply from-field :notify from-field))))))
1258 (defun widget-add-change ()
1259 (remove-hook 'post-command-hook 'widget-add-change t)
1260 (add-hook 'before-change-functions 'widget-before-change nil t)
1261 (add-hook 'after-change-functions 'widget-after-change nil t))
1263 (defun widget-after-change (from to old)
1264 "Adjust field size and text properties."
1265 (let ((field (widget-field-find from))
1266 (other (widget-field-find to)))
1268 (unless (eq field other)
1269 (error "Change in different fields"))
1270 (let ((size (widget-get field :size)))
1272 (let ((begin (widget-field-start field))
1273 (end (widget-field-end field)))
1274 (cond ((< (- end begin) size)
1278 (insert-char ?\s (- (+ begin size) end))))
1279 ((> (- end begin) size)
1280 ;; Field too large and
1281 (if (or (< (point) (+ begin size))
1283 ;; Point is outside extra space.
1284 (setq begin (+ begin size))
1285 ;; Point is within the extra space.
1286 (setq begin (point)))
1289 (while (and (eq (preceding-char) ?\s)
1291 (delete-backward-char 1)))))))
1292 (widget-specify-secret field))
1293 (widget-apply field :notify field))))
1295 ;;; Widget Functions
1297 ;; These functions are used in the definition of multiple widgets.
1299 (defun widget-parent-action (widget &optional event)
1300 "Tell :parent of WIDGET to handle the :action.
1301 Optional EVENT is the event that triggered the action."
1302 (widget-apply (widget-get widget :parent) :action event))
1304 (defun widget-children-value-delete (widget)
1305 "Delete all :children and :buttons in WIDGET."
1306 (mapc 'widget-delete (widget-get widget :children))
1307 (widget-put widget :children nil)
1308 (mapc 'widget-delete (widget-get widget :buttons))
1309 (widget-put widget :buttons nil))
1311 (defun widget-children-validate (widget)
1312 "All the :children must be valid."
1313 (let ((children (widget-get widget :children))
1315 (while (and children (not found))
1316 (setq child (car children)
1317 children (cdr children)
1318 found (widget-apply child :validate)))
1321 (defun widget-child-value-get (widget)
1322 "Get the value of the first member of :children in WIDGET."
1323 (widget-value (car (widget-get widget :children))))
1325 (defun widget-child-value-inline (widget)
1326 "Get the inline value of the first member of :children in WIDGET."
1327 (widget-apply (car (widget-get widget :children)) :value-inline))
1329 (defun widget-child-validate (widget)
1330 "The result of validating the first member of :children in WIDGET."
1331 (widget-apply (car (widget-get widget :children)) :validate))
1333 (defun widget-type-value-create (widget)
1334 "Convert and instantiate the value of the :type attribute of WIDGET.
1335 Store the newly created widget in the :children attribute.
1337 The value of the :type attribute should be an unconverted widget type."
1338 (let ((value (widget-get widget :value))
1339 (type (widget-get widget :type)))
1340 (widget-put widget :children
1341 (list (widget-create-child-value widget
1342 (widget-convert type)
1345 (defun widget-type-default-get (widget)
1346 "Get default value from the :type attribute of WIDGET.
1348 The value of the :type attribute should be an unconverted widget type."
1349 (widget-default-get (widget-convert (widget-get widget :type))))
1351 (defun widget-type-match (widget value)
1352 "Non-nil if the :type value of WIDGET matches VALUE.
1354 The value of the :type attribute should be an unconverted widget type."
1355 (widget-apply (widget-convert (widget-get widget :type)) :match value))
1357 (defun widget-types-copy (widget)
1358 "Copy :args as widget types in WIDGET."
1359 (widget-put widget :args (mapcar 'widget-copy (widget-get widget :args)))
1362 ;; Made defsubst to speed up face editor creation.
1363 (defsubst widget-types-convert-widget (widget)
1364 "Convert :args as widget types in WIDGET."
1365 (widget-put widget :args (mapcar 'widget-convert (widget-get widget :args)))
1368 (defun widget-value-convert-widget (widget)
1369 "Initialize :value from :args in WIDGET."
1370 (let ((args (widget-get widget :args)))
1372 (widget-put widget :value (car args))
1373 ;; Don't convert :value here, as this is done in `widget-convert'.
1374 ;; (widget-put widget :value (widget-apply widget
1375 ;; :value-to-internal (car args)))
1376 (widget-put widget :args nil)))
1379 (defun widget-value-value-get (widget)
1380 "Return the :value property of WIDGET."
1381 (widget-get widget :value))
1383 ;;; The `default' Widget.
1385 (define-widget 'default nil
1386 "Basic widget other widgets are derived from."
1387 :value-to-internal (lambda (widget value) value)
1388 :value-to-external (lambda (widget value) value)
1389 :button-prefix 'widget-button-prefix
1390 :button-suffix 'widget-button-suffix
1391 :complete 'widget-default-complete
1392 :create 'widget-default-create
1395 :format-handler 'widget-default-format-handler
1396 :button-face-get 'widget-default-button-face-get
1397 :sample-face-get 'widget-default-sample-face-get
1398 :delete 'widget-default-delete
1400 :value-set 'widget-default-value-set
1401 :value-inline 'widget-default-value-inline
1402 :value-delete 'ignore
1403 :default-get 'widget-default-default-get
1404 :menu-tag-get 'widget-default-menu-tag-get
1406 :active 'widget-default-active
1407 :activate 'widget-specify-active
1408 :deactivate 'widget-default-deactivate
1409 :mouse-down-action #'ignore
1410 :action 'widget-default-action
1411 :notify 'widget-default-notify
1412 :prompt-value 'widget-default-prompt-value)
1414 (defun widget-default-complete (widget)
1415 "Call the value of the :complete-function property of WIDGET.
1416 If that does not exists, call the value of `widget-complete-field'."
1417 (call-interactively (or (widget-get widget :complete-function)
1418 widget-complete-field)))
1420 (defun widget-default-create (widget)
1421 "Create WIDGET at point in the current buffer."
1422 (widget-specify-insert
1423 (let ((from (point))
1424 button-begin button-end
1425 sample-begin sample-end
1428 (insert (widget-get widget :format))
1430 ;; Parse escapes in format.
1431 (while (re-search-forward "%\\(.\\)" nil t)
1432 (let ((escape (char-after (match-beginning 1))))
1433 (delete-backward-char 2)
1434 (cond ((eq escape ?%)
1437 (setq button-begin (point))
1438 (insert (widget-get-indirect widget :button-prefix)))
1440 (insert (widget-get-indirect widget :button-suffix))
1441 (setq button-end (point)))
1443 (setq sample-begin (point)))
1445 (setq sample-end (point)))
1447 (when (widget-get widget :indent)
1449 (insert-char ?\s (widget-get widget :indent))))
1451 (let ((image (widget-get widget :tag-glyph))
1452 (tag (widget-get widget :tag)))
1454 (widget-image-insert widget (or tag "image") image))
1458 (princ (widget-get widget :value)
1459 (current-buffer))))))
1461 (let ((doc (widget-get widget :doc)))
1463 (setq doc-begin (point))
1465 (while (eq (preceding-char) ?\n)
1466 (delete-backward-char 1))
1468 (setq doc-end (point)))))
1470 (if (and button-begin (not button-end))
1471 (widget-apply widget :value-create)
1472 (setq value-pos (point))))
1474 (widget-apply widget :format-handler escape)))))
1475 ;; Specify button, sample, and doc, and insert value.
1476 (and button-begin button-end
1477 (widget-specify-button widget button-begin button-end))
1478 (and sample-begin sample-end
1479 (widget-specify-sample widget sample-begin sample-end))
1480 (and doc-begin doc-end
1481 (widget-specify-doc widget doc-begin doc-end))
1483 (goto-char value-pos)
1484 (widget-apply widget :value-create)))
1485 (let ((from (point-min-marker))
1486 (to (point-max-marker)))
1487 (set-marker-insertion-type from t)
1488 (set-marker-insertion-type to nil)
1489 (widget-put widget :from from)
1490 (widget-put widget :to to)))
1491 (widget-clear-undo))
1493 (defun widget-default-format-handler (widget escape)
1494 ;; We recognize the %h escape by default.
1495 (let* ((buttons (widget-get widget :buttons)))
1496 (cond ((eq escape ?h)
1497 (let* ((doc-property (widget-get widget :documentation-property))
1498 (doc-try (cond ((widget-get widget :doc))
1499 ((functionp doc-property)
1500 (funcall doc-property
1501 (widget-get widget :value)))
1502 ((symbolp doc-property)
1503 (documentation-property
1504 (widget-get widget :value)
1506 (doc-text (and (stringp doc-try)
1507 (> (length doc-try) 1)
1509 (doc-indent (widget-get widget :documentation-indent)))
1511 (and (eq (preceding-char) ?\n)
1512 (widget-get widget :indent)
1513 (insert-char ?\s (widget-get widget :indent)))
1514 ;; The `*' in the beginning is redundant.
1515 (when (eq (aref doc-text 0) ?*)
1516 (setq doc-text (substring doc-text 1)))
1517 ;; Get rid of trailing newlines.
1518 (when (string-match "\n+\\'" doc-text)
1519 (setq doc-text (substring doc-text 0 (match-beginning 0))))
1520 (push (widget-create-child-and-convert
1521 widget 'documentation-string
1522 :indent (cond ((numberp doc-indent )
1530 (error "Unknown escape `%c'" escape)))
1531 (widget-put widget :buttons buttons)))
1533 (defun widget-default-button-face-get (widget)
1534 ;; Use :button-face or widget-button-face
1535 (or (widget-get widget :button-face)
1536 (let ((parent (widget-get widget :parent)))
1538 (widget-apply parent :button-face-get)
1539 widget-button-face))))
1541 (defun widget-default-sample-face-get (widget)
1542 ;; Use :sample-face.
1543 (widget-get widget :sample-face))
1545 (defun widget-default-delete (widget)
1546 "Remove widget from the buffer."
1547 (let ((from (widget-get widget :from))
1548 (to (widget-get widget :to))
1549 (inactive-overlay (widget-get widget :inactive))
1550 (button-overlay (widget-get widget :button-overlay))
1551 (sample-overlay (widget-get widget :sample-overlay))
1552 (doc-overlay (widget-get widget :doc-overlay))
1553 (inhibit-modification-hooks t)
1554 (inhibit-read-only t))
1555 (widget-apply widget :value-delete)
1556 (widget-children-value-delete widget)
1557 (when inactive-overlay
1558 (delete-overlay inactive-overlay))
1559 (when button-overlay
1560 (delete-overlay button-overlay))
1561 (when sample-overlay
1562 (delete-overlay sample-overlay))
1564 (delete-overlay doc-overlay))
1566 ;; Kludge: this doesn't need to be true for empty formats.
1567 (delete-region from to))
1568 (set-marker from nil)
1569 (set-marker to nil))
1570 (widget-clear-undo))
1572 (defun widget-default-value-set (widget value)
1573 "Recreate widget with new value."
1574 (let* ((old-pos (point))
1575 (from (copy-marker (widget-get widget :from)))
1576 (to (copy-marker (widget-get widget :to)))
1577 (offset (if (and (<= from old-pos) (<= old-pos to))
1578 (if (>= old-pos (1- to))
1580 (- old-pos from)))))
1581 ;;??? Bug: this ought to insert the new value before deleting the old one,
1582 ;; so that markers on either side of the value automatically
1583 ;; stay on the same side. -- rms.
1585 (goto-char (widget-get widget :from))
1586 (widget-apply widget :delete)
1587 (widget-put widget :value value)
1588 (widget-apply widget :create))
1591 (goto-char (+ (widget-get widget :to) offset 1))
1592 (goto-char (min (+ from offset) (1- (widget-get widget :to))))))))
1594 (defun widget-default-value-inline (widget)
1595 "Wrap value in a list unless it is inline."
1596 (if (widget-get widget :inline)
1597 (widget-value widget)
1598 (list (widget-value widget))))
1600 (defun widget-default-default-get (widget)
1602 (widget-get widget :value))
1604 (defun widget-default-menu-tag-get (widget)
1605 "Use tag or value for menus."
1606 (or (widget-get widget :menu-tag)
1607 (widget-get widget :tag)
1608 (widget-princ-to-string (widget-get widget :value))))
1610 (defun widget-default-active (widget)
1611 "Return t iff this widget active (user modifiable)."
1612 (or (widget-get widget :always-active)
1613 (and (not (widget-get widget :inactive))
1614 (let ((parent (widget-get widget :parent)))
1616 (widget-apply parent :active))))))
1618 (defun widget-default-deactivate (widget)
1619 "Make WIDGET inactive for user modifications."
1620 (widget-specify-inactive widget
1621 (widget-get widget :from)
1622 (widget-get widget :to)))
1624 (defun widget-default-action (widget &optional event)
1625 "Notify the parent when a widget changes."
1626 (let ((parent (widget-get widget :parent)))
1628 (widget-apply parent :notify widget event))))
1630 (defun widget-default-notify (widget child &optional event)
1631 "Pass notification to parent."
1632 (widget-default-action widget event))
1634 (defun widget-default-prompt-value (widget prompt value unbound)
1635 "Read an arbitrary value. Stolen from `set-variable'."
1636 ;; (let ((initial (if unbound
1638 ;; It would be nice if we could do a `(cons val 1)' here.
1639 ;; (prin1-to-string (custom-quote value))))))
1640 (eval-minibuffer prompt))
1642 ;;; The `item' Widget.
1644 (define-widget 'item 'default
1645 "Constant items for inclusion in other widgets."
1646 :convert-widget 'widget-value-convert-widget
1647 :value-create 'widget-item-value-create
1648 :value-delete 'ignore
1649 :value-get 'widget-value-value-get
1650 :match 'widget-item-match
1651 :match-inline 'widget-item-match-inline
1652 :action 'widget-item-action
1655 (defun widget-item-value-create (widget)
1656 "Insert the printed representation of the value."
1657 (princ (widget-get widget :value) (current-buffer)))
1659 (defun widget-item-match (widget value)
1660 ;; Match if the value is the same.
1661 (equal (widget-get widget :value) value))
1663 (defun widget-item-match-inline (widget values)
1664 ;; Match if the value is the same.
1665 (let ((value (widget-get widget :value)))
1667 (<= (length value) (length values))
1668 (let ((head (widget-sublist values 0 (length value))))
1669 (and (equal head value)
1670 (cons head (widget-sublist values (length value))))))))
1672 (defun widget-sublist (list start &optional end)
1673 "Return the sublist of LIST from START to END.
1674 If END is omitted, it defaults to the length of LIST."
1675 (if (> start 0) (setq list (nthcdr start list)))
1677 (unless (<= end start)
1678 (setq list (copy-sequence list))
1679 (setcdr (nthcdr (- end start 1) list) nil)
1681 (copy-sequence list)))
1683 (defun widget-item-action (widget &optional event)
1684 ;; Just notify itself.
1685 (widget-apply widget :notify widget event))
1687 ;;; The `push-button' Widget.
1689 ;; (defcustom widget-push-button-gui t
1690 ;; "If non nil, use GUI push buttons when available."
1694 ;; Cache already created GUI objects.
1695 ;; (defvar widget-push-button-cache nil)
1697 (defcustom widget-push-button-prefix "["
1698 "String used as prefix for buttons."
1700 :group 'widget-button)
1702 (defcustom widget-push-button-suffix "]"
1703 "String used as suffix for buttons."
1705 :group 'widget-button)
1707 (define-widget 'push-button 'item
1708 "A pushable button."
1711 :value-create 'widget-push-button-value-create
1714 (defun widget-push-button-value-create (widget)
1715 "Insert text representing the `on' and `off' states."
1716 (let* ((tag (or (widget-get widget :tag)
1717 (widget-get widget :value)))
1718 (tag-glyph (widget-get widget :tag-glyph))
1719 (text (concat widget-push-button-prefix
1720 tag widget-push-button-suffix)))
1722 (widget-image-insert widget text tag-glyph)
1725 ;; (defun widget-gui-action (widget)
1726 ;; "Apply :action for WIDGET."
1727 ;; (widget-apply-action widget (this-command-keys)))
1729 ;;; The `link' Widget.
1731 (defcustom widget-link-prefix "["
1732 "String used as prefix for links."
1734 :group 'widget-button)
1736 (defcustom widget-link-suffix "]"
1737 "String used as suffix for links."
1739 :group 'widget-button)
1741 (define-widget 'link 'item
1743 :button-prefix 'widget-link-prefix
1744 :button-suffix 'widget-link-suffix
1746 :help-echo "Follow the link."
1749 ;;; The `info-link' Widget.
1751 (define-widget 'info-link 'link
1752 "A link to an info file."
1753 :action 'widget-info-link-action)
1755 (defun widget-info-link-action (widget &optional event)
1756 "Open the info node specified by WIDGET."
1757 (info (widget-value widget)))
1759 ;;; The `url-link' Widget.
1761 (define-widget 'url-link 'link
1762 "A link to an www page."
1763 :action 'widget-url-link-action)
1765 (defun widget-url-link-action (widget &optional event)
1766 "Open the URL specified by WIDGET."
1767 (browse-url (widget-value widget)))
1769 ;;; The `function-link' Widget.
1771 (define-widget 'function-link 'link
1772 "A link to an Emacs function."
1773 :action 'widget-function-link-action)
1775 (defun widget-function-link-action (widget &optional event)
1776 "Show the function specified by WIDGET."
1777 (describe-function (widget-value widget)))
1779 ;;; The `variable-link' Widget.
1781 (define-widget 'variable-link 'link
1782 "A link to an Emacs variable."
1783 :action 'widget-variable-link-action)
1785 (defun widget-variable-link-action (widget &optional event)
1786 "Show the variable specified by WIDGET."
1787 (describe-variable (widget-value widget)))
1789 ;;; The `file-link' Widget.
1791 (define-widget 'file-link 'link
1793 :action 'widget-file-link-action)
1795 (defun widget-file-link-action (widget &optional event)
1796 "Find the file specified by WIDGET."
1797 (find-file (widget-value widget)))
1799 ;;; The `emacs-library-link' Widget.
1801 (define-widget 'emacs-library-link 'link
1802 "A link to an Emacs Lisp library file."
1803 :action 'widget-emacs-library-link-action)
1805 (defun widget-emacs-library-link-action (widget &optional event)
1806 "Find the Emacs library file specified by WIDGET."
1807 (find-file (locate-library (widget-value widget))))
1809 ;;; The `emacs-commentary-link' Widget.
1811 (define-widget 'emacs-commentary-link 'link
1812 "A link to Commentary in an Emacs Lisp library file."
1813 :action 'widget-emacs-commentary-link-action)
1815 (defun widget-emacs-commentary-link-action (widget &optional event)
1816 "Find the Commentary section of the Emacs file specified by WIDGET."
1817 (finder-commentary (widget-value widget)))
1819 ;;; The `editable-field' Widget.
1821 (define-widget 'editable-field 'default
1822 "An editable text field."
1823 :convert-widget 'widget-value-convert-widget
1824 :keymap widget-field-keymap
1826 :help-echo "M-TAB: complete field; RET: enter value"
1828 :prompt-internal 'widget-field-prompt-internal
1829 :prompt-history 'widget-field-history
1830 :prompt-value 'widget-field-prompt-value
1831 :action 'widget-field-action
1832 :validate 'widget-field-validate
1834 :error "Field's value doesn't match allowed forms"
1835 :value-create 'widget-field-value-create
1836 :value-delete 'widget-field-value-delete
1837 :value-get 'widget-field-value-get
1838 :match 'widget-field-match)
1840 (defvar widget-field-history nil
1841 "History of field minibuffer edits.")
1843 (defun widget-field-prompt-internal (widget prompt initial history)
1844 "Read string for WIDGET promptinhg with PROMPT.
1845 INITIAL is the initial input and HISTORY is a symbol containing
1847 (read-string prompt initial history))
1849 (defun widget-field-prompt-value (widget prompt value unbound)
1850 "Prompt for a string."
1851 (widget-apply widget
1853 (widget-apply widget
1854 :prompt-internal prompt
1856 (cons (widget-apply widget
1857 :value-to-internal value)
1859 (widget-get widget :prompt-history))))
1861 (defvar widget-edit-functions nil)
1863 (defun widget-field-action (widget &optional event)
1864 "Move to next field."
1866 (run-hook-with-args 'widget-edit-functions widget))
1868 (defun widget-field-validate (widget)
1869 "Valid if the content matches `:valid-regexp'."
1870 (unless (string-match (widget-get widget :valid-regexp)
1871 (widget-apply widget :value-get))
1874 (defun widget-field-value-create (widget)
1875 "Create an editable text field."
1876 (let ((size (widget-get widget :size))
1877 (value (widget-get widget :value))
1879 ;; This is changed to a real overlay in `widget-setup'. We
1880 ;; need the end points to behave differently until
1881 ;; `widget-setup' is called.
1882 (overlay (cons (make-marker) (make-marker))))
1883 (widget-put widget :field-overlay overlay)
1886 (< (length value) size)
1887 (insert-char ?\s (- size (length value))))
1888 (unless (memq widget widget-field-list)
1889 (setq widget-field-new (cons widget widget-field-new)))
1890 (move-marker (cdr overlay) (point))
1891 (set-marker-insertion-type (cdr overlay) nil)
1894 (move-marker (car overlay) from)
1895 (set-marker-insertion-type (car overlay) t)))
1897 (defun widget-field-value-delete (widget)
1898 "Remove the widget from the list of active editing fields."
1899 (setq widget-field-list (delq widget widget-field-list))
1900 (setq widget-field-new (delq widget widget-field-new))
1901 ;; These are nil if the :format string doesn't contain `%v'.
1902 (let ((overlay (widget-get widget :field-overlay)))
1903 (when (overlayp overlay)
1904 (delete-overlay overlay))))
1906 (defun widget-field-value-get (widget)
1907 "Return current text in editing field."
1908 (let ((from (widget-field-start widget))
1909 (to (widget-field-end widget))
1910 (buffer (widget-field-buffer widget))
1911 (size (widget-get widget :size))
1912 (secret (widget-get widget :secret))
1913 (old (current-buffer)))
1920 (eq (char-after (1- to)) ?\s))
1922 (let ((result (buffer-substring-no-properties from to)))
1925 (while (< (+ from index) to)
1927 (get-char-property (+ from index) 'secret))
1928 (setq index (1+ index)))))
1931 (widget-get widget :value))))
1933 (defun widget-field-match (widget value)
1934 ;; Match any string.
1937 ;;; The `text' Widget.
1939 (define-widget 'text 'editable-field
1940 "A multiline text area."
1941 :keymap widget-text-keymap)
1943 ;;; The `menu-choice' Widget.
1945 (define-widget 'menu-choice 'default
1946 "A menu of options."
1947 :convert-widget 'widget-types-convert-widget
1948 :copy 'widget-types-copy
1949 :format "%[%t%]: %v"
1952 :void '(item :format "invalid (%t)\n")
1953 :value-create 'widget-choice-value-create
1954 :value-get 'widget-child-value-get
1955 :value-inline 'widget-child-value-inline
1956 :default-get 'widget-choice-default-get
1957 :mouse-down-action 'widget-choice-mouse-down-action
1958 :action 'widget-choice-action
1959 :error "Make a choice"
1960 :validate 'widget-choice-validate
1961 :match 'widget-choice-match
1962 :match-inline 'widget-choice-match-inline)
1964 (defun widget-choice-value-create (widget)
1965 "Insert the first choice that matches the value."
1966 (let ((value (widget-get widget :value))
1967 (args (widget-get widget :args))
1968 (explicit (widget-get widget :explicit-choice))
1972 ;; If the user specified the choice for this value,
1973 ;; respect that choice.
1974 (widget-put widget :children (list (widget-create-child-value
1975 widget explicit value)))
1976 (widget-put widget :choice explicit)
1977 (widget-put widget :explicit-choice nil))
1979 (setq current (car args)
1981 (when (widget-apply current :match value)
1982 (widget-put widget :children (list (widget-create-child-value
1983 widget current value)))
1984 (widget-put widget :choice current)
1988 (let ((void (widget-get widget :void)))
1989 (widget-put widget :children (list (widget-create-child-and-convert
1990 widget void :value value)))
1991 (widget-put widget :choice void))))))
1993 (defun widget-choice-default-get (widget)
1994 ;; Get default for the first choice.
1995 (widget-default-get (car (widget-get widget :args))))
1997 (defcustom widget-choice-toggle nil
1998 "If non-nil, a binary choice will just toggle between the values.
1999 Otherwise, the user will explicitly have to choose between the values
2000 when he invoked the menu."
2004 (defun widget-choice-mouse-down-action (widget &optional event)
2005 ;; Return non-nil if we need a menu.
2006 (let ((args (widget-get widget :args))
2007 (old (widget-get widget :choice)))
2008 (cond ((not (display-popup-menus-p))
2009 ;; No place to pop up a menu.
2011 ((< (length args) 2)
2012 ;; Empty or singleton list, just return the value.
2014 ((> (length args) widget-menu-max-size)
2015 ;; Too long, prompt.
2017 ((> (length args) 2)
2018 ;; Reasonable sized list, use menu.
2020 ((and widget-choice-toggle (memq old args))
2024 ;; Ask which of the two.
2027 (defun widget-choice-action (widget &optional event)
2029 (let ((args (widget-get widget :args))
2030 (old (widget-get widget :choice))
2031 (tag (widget-apply widget :menu-tag-get))
2032 (completion-ignore-case (widget-get widget :case-fold))
2035 ;; Remember old value.
2036 (if (and old (not (widget-apply widget :validate)))
2037 (let* ((external (widget-value widget))
2038 (internal (widget-apply old :value-to-internal external)))
2039 (widget-put old :value internal)))
2042 (cond ((= (length args) 0)
2044 ((= (length args) 1)
2046 ((and widget-choice-toggle
2049 (if (eq old (nth 0 args))
2054 (setq current (car args)
2057 (cons (cons (widget-apply current :menu-tag-get)
2060 (setq this-explicit t)
2061 (widget-choose tag (reverse choices) event))))
2063 ;; If this was an explicit user choice, record the choice,
2064 ;; so that widget-choice-value-create will respect it.
2066 (widget-put widget :explicit-choice current))
2067 (widget-value-set widget (widget-default-get current))
2069 (widget-apply widget :notify widget event)))
2070 (run-hook-with-args 'widget-edit-functions widget))
2072 (defun widget-choice-validate (widget)
2073 ;; Valid if we have made a valid choice.
2074 (if (eq (widget-get widget :void) (widget-get widget :choice))
2076 (widget-apply (car (widget-get widget :children)) :validate)))
2078 (defun widget-choice-match (widget value)
2079 ;; Matches if one of the choices matches.
2080 (let ((args (widget-get widget :args))
2082 (while (and args (not found))
2083 (setq current (car args)
2085 found (widget-apply current :match value)))
2088 (defun widget-choice-match-inline (widget values)
2089 ;; Matches if one of the choices matches.
2090 (let ((args (widget-get widget :args))
2092 (while (and args (null found))
2093 (setq current (car args)
2095 found (widget-match-inline current values)))
2098 ;;; The `toggle' Widget.
2100 (define-widget 'toggle 'item
2101 "Toggle between two states."
2103 :value-create 'widget-toggle-value-create
2104 :action 'widget-toggle-action
2105 :match (lambda (widget value) t)
2109 (defun widget-toggle-value-create (widget)
2110 "Insert text representing the `on' and `off' states."
2111 (if (widget-value widget)
2112 (let ((image (widget-get widget :on-glyph)))
2113 (and (display-graphic-p)
2115 (not (eq (car image) 'image))
2116 (widget-put widget :on-glyph (setq image (eval image))))
2117 (widget-image-insert widget
2118 (widget-get widget :on)
2120 (let ((image (widget-get widget :off-glyph)))
2121 (and (display-graphic-p)
2123 (not (eq (car image) 'image))
2124 (widget-put widget :off-glyph (setq image (eval image))))
2125 (widget-image-insert widget (widget-get widget :off) image))))
2127 (defun widget-toggle-action (widget &optional event)
2129 (widget-value-set widget (not (widget-value widget)))
2130 (widget-apply widget :notify widget event)
2131 (run-hook-with-args 'widget-edit-functions widget))
2133 ;;; The `checkbox' Widget.
2135 (define-widget 'checkbox 'toggle
2136 "A checkbox toggle."
2141 ;; We could probably do the same job as the images using single
2142 ;; space characters in a boxed face with a stretch specification to
2143 ;; make them square.
2144 :on-glyph '(create-image "\300\300\141\143\067\076\034\030"
2145 'xbm t :width 8 :height 8
2146 :background "grey75" ; like default mode line
2151 :off-glyph '(create-image (make-string 8 0)
2152 'xbm t :width 8 :height 8
2153 :background "grey75"
2157 :help-echo "Toggle this item."
2158 :action 'widget-checkbox-action)
2160 (defun widget-checkbox-action (widget &optional event)
2161 "Toggle checkbox, notify parent, and set active state of sibling."
2162 (widget-toggle-action widget event)
2163 (let ((sibling (widget-get-sibling widget)))
2165 (if (widget-value widget)
2166 (widget-apply sibling :activate)
2167 (widget-apply sibling :deactivate)))))
2169 ;;; The `checklist' Widget.
2171 (define-widget 'checklist 'default
2172 "A multiple choice widget."
2173 :convert-widget 'widget-types-convert-widget
2174 :copy 'widget-types-copy
2177 :entry-format "%b %v"
2179 :value-create 'widget-checklist-value-create
2180 :value-get 'widget-checklist-value-get
2181 :validate 'widget-checklist-validate
2182 :match 'widget-checklist-match
2183 :match-inline 'widget-checklist-match-inline)
2185 (defun widget-checklist-value-create (widget)
2186 ;; Insert all values
2187 (let ((alist (widget-checklist-match-find widget (widget-get widget :value)))
2188 (args (widget-get widget :args)))
2190 (widget-checklist-add-item widget (car args) (assq (car args) alist))
2191 (setq args (cdr args)))
2192 (widget-put widget :children (nreverse (widget-get widget :children)))))
2194 (defun widget-checklist-add-item (widget type chosen)
2195 "Create checklist item in WIDGET of type TYPE.
2196 If the item is checked, CHOSEN is a cons whose cdr is the value."
2197 (and (eq (preceding-char) ?\n)
2198 (widget-get widget :indent)
2199 (insert-char ?\s (widget-get widget :indent)))
2200 (widget-specify-insert
2201 (let* ((children (widget-get widget :children))
2202 (buttons (widget-get widget :buttons))
2203 (button-args (or (widget-get type :sibling-args)
2204 (widget-get widget :button-args)))
2207 (insert (widget-get widget :entry-format))
2209 ;; Parse % escapes in format.
2210 (while (re-search-forward "%\\([bv%]\\)" nil t)
2211 (let ((escape (char-after (match-beginning 1))))
2212 (delete-backward-char 2)
2213 (cond ((eq escape ?%)
2216 (setq button (apply 'widget-create-child-and-convert
2218 :value (not (null chosen))
2223 (let ((child (widget-create-child widget type)))
2224 (widget-apply child :deactivate)
2226 ((widget-get type :inline)
2227 (widget-create-child-value
2228 widget type (cdr chosen)))
2230 (widget-create-child-value
2231 widget type (car (cdr chosen)))))))
2233 (error "Unknown escape `%c'" escape)))))
2234 ;; Update properties.
2235 (and button child (widget-put child :button button))
2236 (and button (widget-put widget :buttons (cons button buttons)))
2237 (and child (widget-put widget :children (cons child children))))))
2239 (defun widget-checklist-match (widget values)
2240 ;; All values must match a type in the checklist.
2242 (null (cdr (widget-checklist-match-inline widget values)))))
2244 (defun widget-checklist-match-inline (widget values)
2245 ;; Find the values which match a type in the checklist.
2246 (let ((greedy (widget-get widget :greedy))
2247 (args (copy-sequence (widget-get widget :args)))
2250 (let ((answer (widget-checklist-match-up args values)))
2252 (let ((vals (widget-match-inline answer values)))
2253 (setq found (append found (car vals))
2255 args (delq answer args))))
2257 (setq rest (append rest (list (car values)))
2258 values (cdr values)))
2260 (setq rest (append rest values)
2264 (defun widget-checklist-match-find (widget vals)
2265 "Find the vals which match a type in the checklist.
2266 Return an alist of (TYPE MATCH)."
2267 (let ((greedy (widget-get widget :greedy))
2268 (args (copy-sequence (widget-get widget :args)))
2271 (let ((answer (widget-checklist-match-up args vals)))
2273 (let ((match (widget-match-inline answer vals)))
2274 (setq found (cons (cons answer (car match)) found)
2276 args (delq answer args))))
2278 (setq vals (cdr vals)))
2283 (defun widget-checklist-match-up (args vals)
2284 "Return the first type from ARGS that matches VALS."
2285 (let (current found)
2286 (while (and args (null found))
2287 (setq current (car args)
2289 found (widget-match-inline current vals)))
2293 (defun widget-checklist-value-get (widget)
2294 ;; The values of all selected items.
2295 (let ((children (widget-get widget :children))
2298 (setq child (car children)
2299 children (cdr children))
2300 (if (widget-value (widget-get child :button))
2301 (setq result (append result (widget-apply child :value-inline)))))
2304 (defun widget-checklist-validate (widget)
2305 ;; Ticked chilren must be valid.
2306 (let ((children (widget-get widget :children))
2308 (while (and children (not found))
2309 (setq child (car children)
2310 children (cdr children)
2311 button (widget-get child :button)
2312 found (and (widget-value button)
2313 (widget-apply child :validate))))
2316 ;;; The `option' Widget
2318 (define-widget 'option 'checklist
2319 "An widget with an optional item."
2322 ;;; The `choice-item' Widget.
2324 (define-widget 'choice-item 'item
2325 "Button items that delegate action events to their parents."
2326 :action 'widget-parent-action
2327 :format "%[%t%] \n")
2329 ;;; The `radio-button' Widget.
2331 (define-widget 'radio-button 'toggle
2332 "A radio button for use in the `radio' widget."
2333 :notify 'widget-radio-button-notify
2340 :off-glyph "radio0")
2342 (defun widget-radio-button-notify (widget child &optional event)
2344 (widget-apply (widget-get widget :parent) :action widget event))
2346 ;;; The `radio-button-choice' Widget.
2348 (define-widget 'radio-button-choice 'default
2349 "Select one of multiple options."
2350 :convert-widget 'widget-types-convert-widget
2351 :copy 'widget-types-copy
2354 :entry-format "%b %v"
2355 :value-create 'widget-radio-value-create
2356 :value-get 'widget-radio-value-get
2357 :value-inline 'widget-radio-value-inline
2358 :value-set 'widget-radio-value-set
2359 :error "You must push one of the buttons"
2360 :validate 'widget-radio-validate
2361 :match 'widget-choice-match
2362 :match-inline 'widget-choice-match-inline
2363 :action 'widget-radio-action)
2365 (defun widget-radio-value-create (widget)
2366 ;; Insert all values
2367 (let ((args (widget-get widget :args))
2370 (setq arg (car args)
2372 (widget-radio-add-item widget arg))))
2374 (defun widget-radio-add-item (widget type)
2375 "Add to radio widget WIDGET a new radio button item of type TYPE."
2376 ;; (setq type (widget-convert type))
2377 (and (eq (preceding-char) ?\n)
2378 (widget-get widget :indent)
2379 (insert-char ?\s (widget-get widget :indent)))
2380 (widget-specify-insert
2381 (let* ((value (widget-get widget :value))
2382 (children (widget-get widget :children))
2383 (buttons (widget-get widget :buttons))
2384 (button-args (or (widget-get type :sibling-args)
2385 (widget-get widget :button-args)))
2387 (chosen (and (null (widget-get widget :choice))
2388 (widget-apply type :match value)))
2390 (insert (widget-get widget :entry-format))
2392 ;; Parse % escapes in format.
2393 (while (re-search-forward "%\\([bv%]\\)" nil t)
2394 (let ((escape (char-after (match-beginning 1))))
2395 (delete-backward-char 2)
2396 (cond ((eq escape ?%)
2399 (setq button (apply 'widget-create-child-and-convert
2400 widget 'radio-button
2401 :value (not (null chosen))
2404 (setq child (if chosen
2405 (widget-create-child-value
2407 (widget-create-child widget type)))
2409 (widget-apply child :deactivate)))
2411 (error "Unknown escape `%c'" escape)))))
2412 ;; Update properties.
2414 (widget-put widget :choice type))
2416 (widget-put child :button button)
2417 (widget-put widget :buttons (nconc buttons (list button))))
2419 (widget-put widget :children (nconc children (list child))))
2422 (defun widget-radio-value-get (widget)
2423 ;; Get value of the child widget.
2424 (let ((chosen (widget-radio-chosen widget)))
2425 (and chosen (widget-value chosen))))
2427 (defun widget-radio-chosen (widget)
2428 "Return the widget representing the chosen radio button."
2429 (let ((children (widget-get widget :children))
2432 (setq current (car children)
2433 children (cdr children))
2434 (when (widget-apply (widget-get current :button) :value-get)
2439 (defun widget-radio-value-inline (widget)
2440 ;; Get value of the child widget.
2441 (let ((children (widget-get widget :children))
2444 (setq current (car children)
2445 children (cdr children))
2446 (when (widget-apply (widget-get current :button) :value-get)
2447 (setq found (widget-apply current :value-inline)
2451 (defun widget-radio-value-set (widget value)
2452 ;; We can't just delete and recreate a radio widget, since children
2453 ;; can be added after the original creation and won't be recreated
2455 (let ((children (widget-get widget :children))
2458 (setq current (car children)
2459 children (cdr children))
2460 (let* ((button (widget-get current :button))
2461 (match (and (not found)
2462 (widget-apply current :match value))))
2463 (widget-value-set button match)
2466 (widget-value-set current value)
2467 (widget-apply current :activate))
2468 (widget-apply current :deactivate))
2469 (setq found (or found match))))))
2471 (defun widget-radio-validate (widget)
2472 ;; Valid if we have made a valid choice.
2473 (let ((children (widget-get widget :children))
2474 current found button)
2475 (while (and children (not found))
2476 (setq current (car children)
2477 children (cdr children)
2478 button (widget-get current :button)
2479 found (widget-apply button :value-get)))
2481 (widget-apply current :validate)
2484 (defun widget-radio-action (widget child event)
2485 ;; Check if a radio button was pressed.
2486 (let ((children (widget-get widget :children))
2487 (buttons (widget-get widget :buttons))
2489 (when (memq child buttons)
2491 (setq current (car children)
2492 children (cdr children))
2493 (let* ((button (widget-get current :button)))
2494 (cond ((eq child button)
2495 (widget-value-set button t)
2496 (widget-apply current :activate))
2497 ((widget-value button)
2498 (widget-value-set button nil)
2499 (widget-apply current :deactivate)))))))
2500 ;; Pass notification to parent.
2501 (widget-apply widget :notify child event))
2503 ;;; The `insert-button' Widget.
2505 (define-widget 'insert-button 'push-button
2506 "An insert button for the `editable-list' widget."
2508 :help-echo "Insert a new item into the list at this position."
2509 :action 'widget-insert-button-action)
2511 (defun widget-insert-button-action (widget &optional event)
2512 ;; Ask the parent to insert a new item.
2513 (widget-apply (widget-get widget :parent)
2514 :insert-before (widget-get widget :widget)))
2516 ;;; The `delete-button' Widget.
2518 (define-widget 'delete-button 'push-button
2519 "A delete button for the `editable-list' widget."
2521 :help-echo "Delete this item from the list."
2522 :action 'widget-delete-button-action)
2524 (defun widget-delete-button-action (widget &optional event)
2525 ;; Ask the parent to insert a new item.
2526 (widget-apply (widget-get widget :parent)
2527 :delete-at (widget-get widget :widget)))
2529 ;;; The `editable-list' Widget.
2531 ;; (defcustom widget-editable-list-gui nil
2532 ;; "If non nil, use GUI push-buttons in editable list when available."
2536 (define-widget 'editable-list 'default
2537 "A variable list of widgets of the same type."
2538 :convert-widget 'widget-types-convert-widget
2539 :copy 'widget-types-copy
2542 :format-handler 'widget-editable-list-format-handler
2543 :entry-format "%i %d %v"
2544 :value-create 'widget-editable-list-value-create
2545 :value-get 'widget-editable-list-value-get
2546 :validate 'widget-children-validate
2547 :match 'widget-editable-list-match
2548 :match-inline 'widget-editable-list-match-inline
2549 :insert-before 'widget-editable-list-insert-before
2550 :delete-at 'widget-editable-list-delete-at)
2552 (defun widget-editable-list-format-handler (widget escape)
2553 ;; We recognize the insert button.
2554 ;; (let ((widget-push-button-gui widget-editable-list-gui))
2555 (cond ((eq escape ?i)
2556 (and (widget-get widget :indent)
2557 (insert-char ?\s (widget-get widget :indent)))
2558 (apply 'widget-create-child-and-convert
2559 widget 'insert-button
2560 (widget-get widget :append-button-args)))
2562 (widget-default-format-handler widget escape)))
2566 (defun widget-editable-list-value-create (widget)
2567 ;; Insert all values
2568 (let* ((value (widget-get widget :value))
2569 (type (nth 0 (widget-get widget :args)))
2571 (widget-put widget :value-pos (copy-marker (point)))
2572 (set-marker-insertion-type (widget-get widget :value-pos) t)
2574 (let ((answer (widget-match-inline type value)))
2576 (setq children (cons (widget-editable-list-entry-create
2578 (if (widget-get type :inline)
2585 (widget-put widget :children (nreverse children))))
2587 (defun widget-editable-list-value-get (widget)
2588 ;; Get value of the child widget.
2589 (apply 'append (mapcar (lambda (child) (widget-apply child :value-inline))
2590 (widget-get widget :children))))
2592 (defun widget-editable-list-match (widget value)
2593 ;; Value must be a list and all the members must match the type.
2595 (null (cdr (widget-editable-list-match-inline widget value)))))
2597 (defun widget-editable-list-match-inline (widget value)
2598 (let ((type (nth 0 (widget-get widget :args)))
2601 (while (and value ok)
2602 (let ((answer (widget-match-inline type value)))
2604 (setq found (append found (car answer))
2607 (cons found value)))
2609 (defun widget-editable-list-insert-before (widget before)
2610 ;; Insert a new child in the list of children.
2612 (let ((children (widget-get widget :children))
2613 (inhibit-read-only t)
2614 before-change-functions
2615 after-change-functions)
2617 (goto-char (widget-get before :entry-from)))
2619 (goto-char (widget-get widget :value-pos))))
2620 (let ((child (widget-editable-list-entry-create
2622 (when (< (widget-get child :entry-from) (widget-get widget :from))
2623 (set-marker (widget-get widget :from)
2624 (widget-get child :entry-from)))
2625 (if (eq (car children) before)
2626 (widget-put widget :children (cons child children))
2627 (while (not (eq (car (cdr children)) before))
2628 (setq children (cdr children)))
2629 (setcdr children (cons child (cdr children)))))))
2631 (widget-apply widget :notify widget))
2633 (defun widget-editable-list-delete-at (widget child)
2634 ;; Delete child from list of children.
2636 (let ((buttons (copy-sequence (widget-get widget :buttons)))
2638 (inhibit-read-only t)
2639 before-change-functions
2640 after-change-functions)
2642 (setq button (car buttons)
2643 buttons (cdr buttons))
2644 (when (eq (widget-get button :widget) child)
2646 :buttons (delq button (widget-get widget :buttons)))
2647 (widget-delete button))))
2648 (let ((entry-from (widget-get child :entry-from))
2649 (entry-to (widget-get child :entry-to))
2650 (inhibit-read-only t)
2651 before-change-functions
2652 after-change-functions)
2653 (widget-delete child)
2654 (delete-region entry-from entry-to)
2655 (set-marker entry-from nil)
2656 (set-marker entry-to nil))
2657 (widget-put widget :children (delq child (widget-get widget :children))))
2659 (widget-apply widget :notify widget))
2661 (defun widget-editable-list-entry-create (widget value conv)
2662 ;; Create a new entry to the list.
2663 (let ((type (nth 0 (widget-get widget :args)))
2664 ;; (widget-push-button-gui widget-editable-list-gui)
2665 child delete insert)
2666 (widget-specify-insert
2668 (and (widget-get widget :indent)
2669 (insert-char ?\s (widget-get widget :indent)))
2670 (insert (widget-get widget :entry-format)))
2671 ;; Parse % escapes in format.
2672 (while (re-search-forward "%\\(.\\)" nil t)
2673 (let ((escape (char-after (match-beginning 1))))
2674 (delete-backward-char 2)
2675 (cond ((eq escape ?%)
2678 (setq insert (apply 'widget-create-child-and-convert
2679 widget 'insert-button
2680 (widget-get widget :insert-button-args))))
2682 (setq delete (apply 'widget-create-child-and-convert
2683 widget 'delete-button
2684 (widget-get widget :delete-button-args))))
2687 (setq child (widget-create-child-value
2689 (setq child (widget-create-child-value
2690 widget type (widget-default-get type)))))
2692 (error "Unknown escape `%c'" escape)))))
2693 (let ((buttons (widget-get widget :buttons)))
2694 (if insert (push insert buttons))
2695 (if delete (push delete buttons))
2696 (widget-put widget :buttons buttons))
2697 (let ((entry-from (point-min-marker))
2698 (entry-to (point-max-marker)))
2699 (set-marker-insertion-type entry-from t)
2700 (set-marker-insertion-type entry-to nil)
2701 (widget-put child :entry-from entry-from)
2702 (widget-put child :entry-to entry-to)))
2703 (if insert (widget-put insert :widget child))
2704 (if delete (widget-put delete :widget child))
2707 ;;; The `group' Widget.
2709 (define-widget 'group 'default
2710 "A widget which groups other widgets inside."
2711 :convert-widget 'widget-types-convert-widget
2712 :copy 'widget-types-copy
2714 :value-create 'widget-group-value-create
2715 :value-get 'widget-editable-list-value-get
2716 :default-get 'widget-group-default-get
2717 :validate 'widget-children-validate
2718 :match 'widget-group-match
2719 :match-inline 'widget-group-match-inline)
2721 (defun widget-group-value-create (widget)
2722 ;; Create each component.
2723 (let ((args (widget-get widget :args))
2724 (value (widget-get widget :value))
2725 arg answer children)
2727 (setq arg (car args)
2729 answer (widget-match-inline arg value)
2731 (and (eq (preceding-char) ?\n)
2732 (widget-get widget :indent)
2733 (insert-char ?\s (widget-get widget :indent)))
2734 (push (cond ((null answer)
2735 (widget-create-child widget arg))
2736 ((widget-get arg :inline)
2737 (widget-create-child-value widget arg (car answer)))
2739 (widget-create-child-value widget arg (car (car answer)))))
2741 (widget-put widget :children (nreverse children))))
2743 (defun widget-group-default-get (widget)
2744 ;; Get the default of the components.
2745 (mapcar 'widget-default-get (widget-get widget :args)))
2747 (defun widget-group-match (widget values)
2748 ;; Match if the components match.
2750 (let ((match (widget-group-match-inline widget values)))
2751 (and match (null (cdr match))))))
2753 (defun widget-group-match-inline (widget vals)
2754 ;; Match if the components match.
2755 (let ((args (widget-get widget :args))
2756 argument answer found)
2758 (setq argument (car args)
2760 answer (widget-match-inline argument vals))
2762 (setq vals (cdr answer)
2763 found (append found (car answer)))
2767 (cons found vals))))
2769 ;;; The `visibility' Widget.
2771 (define-widget 'visibility 'item
2772 "An indicator and manipulator for hidden items."
2778 :value-create 'widget-visibility-value-create
2779 :action 'widget-toggle-action
2780 :match (lambda (widget value) t))
2782 (defun widget-visibility-value-create (widget)
2783 ;; Insert text representing the `on' and `off' states.
2784 (let ((on (widget-get widget :on))
2785 (off (widget-get widget :off)))
2787 (setq on (concat widget-push-button-prefix
2789 widget-push-button-suffix))
2792 (setq off (concat widget-push-button-prefix
2794 widget-push-button-suffix))
2796 (if (widget-value widget)
2797 (widget-image-insert widget on "down" "down-pushed")
2798 (widget-image-insert widget off "right" "right-pushed"))))
2800 ;;; The `documentation-link' Widget.
2802 ;; This is a helper widget for `documentation-string'.
2804 (define-widget 'documentation-link 'link
2805 "Link type used in documentation strings."
2807 :help-echo "Describe this symbol"
2808 :action 'widget-documentation-link-action)
2810 (defun widget-documentation-link-action (widget &optional event)
2811 "Display documentation for WIDGET's value. Ignore optional argument EVENT."
2812 (let* ((string (widget-get widget :value))
2813 (symbol (intern string)))
2814 (if (and (fboundp symbol) (boundp symbol))
2815 ;; If there are two doc strings, give the user a way to pick one.
2816 (apropos (concat "\\`" (regexp-quote string) "\\'"))
2817 (if (fboundp symbol)
2818 (describe-function symbol)
2819 (describe-variable symbol)))))
2821 (defcustom widget-documentation-links t
2822 "Add hyperlinks to documentation strings when non-nil."
2824 :group 'widget-documentation)
2826 (defcustom widget-documentation-link-regexp "`\\([^\n`' ]+\\)'"
2827 "Regexp for matching potential links in documentation strings.
2828 The first group should be the link itself."
2830 :group 'widget-documentation)
2832 (defcustom widget-documentation-link-p 'intern-soft
2833 "Predicate used to test if a string is useful as a link.
2834 The value should be a function. The function will be called one
2835 argument, a string, and should return non-nil if there should be a
2836 link for that string."
2838 :options '(widget-documentation-link-p)
2839 :group 'widget-documentation)
2841 (defcustom widget-documentation-link-type 'documentation-link
2842 "Widget type used for links in documentation strings."
2844 :group 'widget-documentation)
2846 (defun widget-documentation-link-add (widget from to)
2847 (widget-specify-doc widget from to)
2848 (when widget-documentation-links
2849 (let ((regexp widget-documentation-link-regexp)
2850 (buttons (widget-get widget :buttons))
2851 (widget-mouse-face (default-value 'widget-mouse-face))
2852 (widget-button-face widget-documentation-face)
2853 (widget-button-pressed-face widget-documentation-face))
2856 (while (re-search-forward regexp to t)
2857 (let ((name (match-string 1))
2858 (begin (match-beginning 1))
2859 (end (match-end 1)))
2860 (when (funcall widget-documentation-link-p name)
2861 (push (widget-convert-button widget-documentation-link-type
2862 begin end :value name)
2864 (widget-put widget :buttons buttons)))
2865 (let ((indent (widget-get widget :indent)))
2866 (when (and indent (not (zerop indent)))
2869 (narrow-to-region from to)
2870 (goto-char (point-min))
2871 (while (search-forward "\n" nil t)
2872 (insert-char ?\s indent)))))))
2874 ;;; The `documentation-string' Widget.
2876 (define-widget 'documentation-string 'item
2877 "A documentation string."
2879 :action 'widget-documentation-string-action
2880 :value-create 'widget-documentation-string-value-create)
2882 (defun widget-documentation-string-value-create (widget)
2883 ;; Insert documentation string.
2884 (let ((doc (widget-value widget))
2885 (indent (widget-get widget :indent))
2886 (shown (widget-get (widget-get widget :parent) :documentation-shown))
2888 (if (string-match "\n" doc)
2889 (let ((before (substring doc 0 (match-beginning 0)))
2890 (after (substring doc (match-beginning 0)))
2893 (widget-documentation-link-add widget start (point))
2895 (widget-create-child-and-convert
2897 :help-echo "Show or hide rest of the documentation."
2901 :action 'widget-parent-action
2904 (setq start (point))
2905 (when (and indent (not (zerop indent)))
2906 (insert-char ?\s indent))
2908 (widget-documentation-link-add widget start (point)))
2909 (widget-put widget :buttons (list button)))
2911 (widget-documentation-link-add widget start (point))))
2914 (defun widget-documentation-string-action (widget &rest ignore)
2915 ;; Toggle documentation.
2916 (let ((parent (widget-get widget :parent)))
2917 (widget-put parent :documentation-shown
2918 (not (widget-get parent :documentation-shown))))
2920 (widget-value-set widget (widget-value widget)))
2922 ;;; The Sexp Widgets.
2924 (define-widget 'const 'item
2925 "An immutable sexp."
2926 :prompt-value 'widget-const-prompt-value
2929 (defun widget-const-prompt-value (widget prompt value unbound)
2930 ;; Return the value of the const.
2931 (widget-value widget))
2933 (define-widget 'function-item 'const
2934 "An immutable function name."
2936 :documentation-property (lambda (symbol)
2938 (documentation symbol t)
2941 (define-widget 'variable-item 'const
2942 "An immutable variable name."
2944 :documentation-property 'variable-documentation)
2946 (define-widget 'other 'sexp
2947 "Matches any value, but doesn't let the user edit the value.
2948 This is useful as last item in a `choice' widget.
2949 You should use this widget type with a default value,
2950 as in (other DEFAULT) or (other :tag \"NAME\" DEFAULT).
2951 If the user selects this alternative, that specifies DEFAULT
2957 (defvar widget-string-prompt-value-history nil
2958 "History of input to `widget-string-prompt-value'.")
2960 (define-widget 'string 'editable-field
2963 :format "%{%t%}: %v"
2964 :complete-function 'ispell-complete-word
2965 :prompt-history 'widget-string-prompt-value-history)
2967 (define-widget 'regexp 'string
2968 "A regular expression."
2969 :match 'widget-regexp-match
2970 :validate 'widget-regexp-validate
2971 ;; Doesn't work well with terminating newline.
2972 ;; :value-face 'widget-single-line-field
2975 (defun widget-regexp-match (widget value)
2976 ;; Match valid regexps.
2977 (and (stringp value)
2980 (string-match value ""))
2983 (defun widget-regexp-validate (widget)
2984 "Check that the value of WIDGET is a valid regexp."
2985 (condition-case data
2987 (string-match (widget-value widget) ""))
2988 (error (widget-put widget :error (error-message-string data))
2991 (define-widget 'file 'string
2993 It will read a file name from the minibuffer when invoked."
2994 :complete-function 'widget-file-complete
2995 :prompt-value 'widget-file-prompt-value
2996 :format "%{%t%}: %v"
2997 ;; Doesn't work well with terminating newline.
2998 ;; :value-face 'widget-single-line-field
3001 (defun widget-file-complete ()
3002 "Perform completion on file name preceding point."
3004 (let* ((end (point))
3005 (beg (widget-field-start widget))
3006 (pattern (buffer-substring beg end))
3007 (name-part (file-name-nondirectory pattern))
3008 ;; I think defaulting to root is right
3009 ;; because these really should be absolute file names.
3010 (directory (or (file-name-directory pattern) "/"))
3011 (completion (file-name-completion name-part directory)))
3012 (cond ((eq completion t))
3014 (message "Can't find completion for \"%s\"" pattern)
3016 ((not (string= name-part completion))
3017 (delete-region beg end)
3018 (insert (expand-file-name completion directory)))
3020 (message "Making completion list...")
3021 (with-output-to-temp-buffer "*Completions*"
3022 (display-completion-list
3023 (sort (file-name-all-completions name-part directory)
3026 (message "Making completion list...%s" "done")))))
3028 (defun widget-file-prompt-value (widget prompt value unbound)
3029 ;; Read file from minibuffer.
3030 (abbreviate-file-name
3032 (read-file-name prompt)
3033 (let ((prompt2 (format "%s (default %s): " prompt value))
3034 (dir (file-name-directory value))
3035 (file (file-name-nondirectory value))
3036 (must-match (widget-get widget :must-match)))
3037 (read-file-name prompt2 dir nil must-match file)))))
3039 ;;;(defun widget-file-action (widget &optional event)
3040 ;;; ;; Read a file name from the minibuffer.
3041 ;;; (let* ((value (widget-value widget))
3042 ;;; (dir (file-name-directory value))
3043 ;;; (file (file-name-nondirectory value))
3044 ;;; (menu-tag (widget-apply widget :menu-tag-get))
3045 ;;; (must-match (widget-get widget :must-match))
3046 ;;; (answer (read-file-name (concat menu-tag " (default " value "): ")
3047 ;;; dir nil must-match file)))
3048 ;;; (widget-value-set widget (abbreviate-file-name answer))
3050 ;;; (widget-apply widget :notify widget event)))
3052 ;; Fixme: use file-name-as-directory.
3053 (define-widget 'directory 'file
3054 "A directory widget.
3055 It will read a directory name from the minibuffer when invoked."
3058 (defvar widget-symbol-prompt-value-history nil
3059 "History of input to `widget-symbol-prompt-value'.")
3061 (define-widget 'symbol 'editable-field
3065 :format "%{%t%}: %v"
3066 :match (lambda (widget value) (symbolp value))
3067 :complete-function 'lisp-complete-symbol
3068 :prompt-internal 'widget-symbol-prompt-internal
3069 :prompt-match 'symbolp
3070 :prompt-history 'widget-symbol-prompt-value-history
3071 :value-to-internal (lambda (widget value)
3075 :value-to-external (lambda (widget value)
3080 (defun widget-symbol-prompt-internal (widget prompt initial history)
3081 ;; Read file from minibuffer.
3082 (let ((answer (completing-read prompt obarray
3083 (widget-get widget :prompt-match)
3084 nil initial history)))
3085 (if (and (stringp answer)
3086 (not (zerop (length answer))))
3088 (error "No value"))))
3090 (defvar widget-function-prompt-value-history nil
3091 "History of input to `widget-function-prompt-value'.")
3093 (define-widget 'function 'restricted-sexp
3095 :complete-function (lambda ()
3097 (lisp-complete-symbol 'fboundp))
3098 :prompt-value 'widget-field-prompt-value
3099 :prompt-internal 'widget-symbol-prompt-internal
3100 :prompt-match 'fboundp
3101 :prompt-history 'widget-function-prompt-value-history
3102 :action 'widget-field-action
3103 :match-alternatives '(functionp)
3104 :validate (lambda (widget)
3105 (unless (functionp (widget-value widget))
3106 (widget-put widget :error (format "Invalid function: %S"
3107 (widget-value widget)))
3112 (defvar widget-variable-prompt-value-history nil
3113 "History of input to `widget-variable-prompt-value'.")
3115 (define-widget 'variable 'symbol
3117 :prompt-match 'boundp
3118 :prompt-history 'widget-variable-prompt-value-history
3119 :complete-function (lambda ()
3121 (lisp-complete-symbol 'boundp))
3124 (defvar widget-coding-system-prompt-value-history nil
3125 "History of input to `widget-coding-system-prompt-value'.")
3127 (define-widget 'coding-system 'symbol
3128 "A MULE coding-system."
3129 :format "%{%t%}: %v"
3130 :tag "Coding system"
3132 :prompt-history 'widget-coding-system-prompt-value-history
3133 :prompt-value 'widget-coding-system-prompt-value
3134 :action 'widget-coding-system-action
3135 :complete-function (lambda ()
3137 (lisp-complete-symbol 'coding-system-p))
3138 :validate (lambda (widget)
3139 (unless (coding-system-p (widget-value widget))
3140 (widget-put widget :error (format "Invalid coding system: %S"
3141 (widget-value widget)))
3144 :prompt-match 'coding-system-p)
3146 (defun widget-coding-system-prompt-value (widget prompt value unbound)
3147 "Read coding-system from minibuffer."
3148 (if (widget-get widget :base-only)
3150 (completing-read (format "%s (default %s): " prompt value)
3151 (mapcar #'list (coding-system-list t)) nil nil nil
3152 coding-system-history))
3153 (read-coding-system (format "%s (default %s): " prompt value) value)))
3155 (defun widget-coding-system-action (widget &optional event)
3157 (widget-coding-system-prompt-value
3159 (widget-apply widget :menu-tag-get)
3160 (widget-value widget)
3162 (widget-value-set widget answer)
3163 (widget-apply widget :notify widget event)
3166 (define-widget 'sexp 'editable-field
3167 "An arbitrary Lisp expression."
3168 :tag "Lisp expression"
3169 :format "%{%t%}: %v"
3171 :validate 'widget-sexp-validate
3172 :match (lambda (widget value) t)
3173 :value-to-internal 'widget-sexp-value-to-internal
3174 :value-to-external (lambda (widget value) (read value))
3175 :prompt-history 'widget-sexp-prompt-value-history
3176 :prompt-value 'widget-sexp-prompt-value)
3178 (defun widget-sexp-value-to-internal (widget value)
3179 ;; Use pp for printer representation.
3180 (let ((pp (if (symbolp value)
3181 (prin1-to-string value)
3182 (pp-to-string value))))
3183 (while (string-match "\n\\'" pp)
3184 (setq pp (substring pp 0 -1)))
3185 (if (or (string-match "\n\\'" pp)
3190 (defun widget-sexp-validate (widget)
3191 ;; Valid if we can read the string and there is no junk left after it.
3193 (insert (widget-apply widget :value-get))
3194 (goto-char (point-min))
3196 (condition-case data
3198 ;; Avoid a confusing end-of-file error.
3199 (skip-syntax-forward "\\s-")
3201 (setq err "Empty sexp -- use `nil'?")
3202 (unless (widget-apply widget :match (read (current-buffer)))
3203 (setq err (widget-get widget :type-error))))
3204 ;; Allow whitespace after expression.
3205 (skip-syntax-forward "\\s-")
3206 (if (and (not (eobp))
3208 (setq err (format "Junk at end of expression: %s"
3209 (buffer-substring (point)
3211 (end-of-file ; Avoid confusing error message.
3212 (setq err "Unbalanced sexp"))
3213 (error (setq err (error-message-string data))))
3216 (widget-put widget :error err)
3219 (defvar widget-sexp-prompt-value-history nil
3220 "History of input to `widget-sexp-prompt-value'.")
3222 (defun widget-sexp-prompt-value (widget prompt value unbound)
3223 ;; Read an arbitrary sexp.
3224 (let ((found (read-string prompt
3225 (if unbound nil (cons (prin1-to-string value) 0))
3226 (widget-get widget :prompt-history))))
3227 (let ((answer (read-from-string found)))
3228 (unless (= (cdr answer) (length found))
3229 (error "Junk at end of expression: %s"
3230 (substring found (cdr answer))))
3233 (define-widget 'restricted-sexp 'sexp
3234 "A Lisp expression restricted to values that match.
3235 To use this type, you must define :match or :match-alternatives."
3236 :type-error "The specified value is not valid"
3237 :match 'widget-restricted-sexp-match
3238 :value-to-internal (lambda (widget value)
3239 (if (widget-apply widget :match value)
3240 (prin1-to-string value)
3243 (defun widget-restricted-sexp-match (widget value)
3244 (let ((alternatives (widget-get widget :match-alternatives))
3246 (while (and alternatives (not matched))
3247 (if (cond ((functionp (car alternatives))
3248 (funcall (car alternatives) value))
3249 ((and (consp (car alternatives))
3250 (eq (car (car alternatives)) 'quote))
3251 (eq value (nth 1 (car alternatives)))))
3253 (setq alternatives (cdr alternatives)))
3256 (define-widget 'integer 'restricted-sexp
3260 :type-error "This field should contain an integer"
3261 :match-alternatives '(integerp))
3263 (define-widget 'number 'restricted-sexp
3264 "A number (floating point or integer)."
3267 :type-error "This field should contain a number (floating point or integer)"
3268 :match-alternatives '(numberp))
3270 (define-widget 'float 'restricted-sexp
3271 "A floating point number."
3272 :tag "Floating point number"
3274 :type-error "This field should contain a floating point number"
3275 :match-alternatives '(floatp))
3277 (define-widget 'character 'editable-field
3282 :format "%{%t%}: %v\n"
3283 :valid-regexp "\\`.\\'"
3284 :error "This field should contain a single character"
3285 :value-to-internal (lambda (widget value)
3288 (char-to-string value)))
3289 :value-to-external (lambda (widget value)
3293 :match (lambda (widget value)
3294 (char-valid-p value)))
3296 (define-widget 'list 'group
3299 :format "%{%t%}:\n%v")
3301 (define-widget 'vector 'group
3304 :format "%{%t%}:\n%v"
3305 :match 'widget-vector-match
3306 :value-to-internal (lambda (widget value) (append value nil))
3307 :value-to-external (lambda (widget value) (apply 'vector value)))
3309 (defun widget-vector-match (widget value)
3310 (and (vectorp value)
3311 (widget-group-match widget
3312 (widget-apply widget :value-to-internal value))))
3314 (define-widget 'cons 'group
3317 :format "%{%t%}:\n%v"
3318 :match 'widget-cons-match
3319 :value-to-internal (lambda (widget value)
3320 (list (car value) (cdr value)))
3321 :value-to-external (lambda (widget value)
3322 (apply 'cons value)))
3324 (defun widget-cons-match (widget value)
3326 (widget-group-match widget
3327 (widget-apply widget :value-to-internal value))))
3329 ;;; The `lazy' Widget.
3331 ;; Recursive datatypes.
3333 (define-widget 'lazy 'default
3334 "Base widget for recursive datastructures.
3336 The `lazy' widget will, when instantiated, contain a single inferior
3337 widget, of the widget type specified by the :type parameter. The
3338 value of the `lazy' widget is the same as the value of the inferior
3339 widget. When deriving a new widget from the 'lazy' widget, the :type
3340 parameter is allowed to refer to the widget currently being defined,
3341 thus allowing recursive datastructures to be described.
3343 The :type parameter takes the same arguments as the defcustom
3344 parameter with the same name.
3346 Most composite widgets, i.e. widgets containing other widgets, does
3347 not allow recursion. That is, when you define a new widget type, none
3348 of the inferior widgets may be of the same type you are currently
3351 In Lisp, however, it is custom to define datastructures in terms of
3352 themselves. A list, for example, is defined as either nil, or a cons
3353 cell whose cdr itself is a list. The obvious way to translate this
3354 into a widget type would be
3356 (define-widget 'my-list 'choice
3357 \"A list of sexps.\"
3359 :args '((const nil) (cons :value (nil) sexp my-list)))
3361 Here we attempt to define my-list as a choice of either the constant
3362 nil, or a cons-cell containing a sexp and my-lisp. This will not work
3363 because the `choice' widget does not allow recursion.
3365 Using the `lazy' widget you can overcome this problem, as in this
3368 (define-widget 'sexp-list 'lazy
3369 \"A list of sexps.\"
3371 :type '(choice (const nil) (cons :value (nil) sexp sexp-list)))"
3372 :format "%{%t%}: %v"
3373 ;; We don't convert :type because we want to allow recursive
3374 ;; datastructures. This is slow, so we should not create speed
3375 ;; critical widgets by deriving from this.
3376 :convert-widget 'widget-value-convert-widget
3377 :value-create 'widget-type-value-create
3378 :value-get 'widget-child-value-get
3379 :value-inline 'widget-child-value-inline
3380 :default-get 'widget-type-default-get
3381 :match 'widget-type-match
3382 :validate 'widget-child-validate)
3385 ;;; The `plist' Widget.
3389 (define-widget 'plist 'list
3391 :key-type '(symbol :tag "Key")
3392 :value-type '(sexp :tag "Value")
3393 :convert-widget 'widget-plist-convert-widget
3396 (defvar widget-plist-value-type) ;Dynamic variable
3398 (defun widget-plist-convert-widget (widget)
3399 ;; Handle `:options'.
3400 (let* ((options (widget-get widget :options))
3401 (widget-plist-value-type (widget-get widget :value-type))
3402 (other `(editable-list :inline t
3404 ,(widget-get widget :key-type)
3405 ,widget-plist-value-type)))
3407 (list `(checklist :inline t
3409 ,@(mapcar 'widget-plist-convert-option
3413 (widget-put widget :args args)
3416 (defun widget-plist-convert-option (option)
3417 ;; Convert a single plist option.
3418 (let (key-type value-type)
3420 (let ((key (nth 0 option)))
3421 (setq value-type (nth 1 option))
3424 (setq key-type `(const ,key))))
3425 (setq key-type `(const ,option)
3426 value-type widget-plist-value-type))
3427 `(group :format "Key: %v" :inline t ,key-type ,value-type)))
3430 ;;; The `alist' Widget.
3432 ;; Association lists.
3434 (define-widget 'alist 'list
3435 "An association list."
3436 :key-type '(sexp :tag "Key")
3437 :value-type '(sexp :tag "Value")
3438 :convert-widget 'widget-alist-convert-widget
3441 (defvar widget-alist-value-type) ;Dynamic variable
3443 (defun widget-alist-convert-widget (widget)
3444 ;; Handle `:options'.
3445 (let* ((options (widget-get widget :options))
3446 (widget-alist-value-type (widget-get widget :value-type))
3447 (other `(editable-list :inline t
3449 ,(widget-get widget :key-type)
3450 ,widget-alist-value-type)))
3452 (list `(checklist :inline t
3454 ,@(mapcar 'widget-alist-convert-option
3458 (widget-put widget :args args)
3461 (defun widget-alist-convert-option (option)
3462 ;; Convert a single alist option.
3463 (let (key-type value-type)
3465 (let ((key (nth 0 option)))
3466 (setq value-type (nth 1 option))
3469 (setq key-type `(const ,key))))
3470 (setq key-type `(const ,option)
3471 value-type widget-alist-value-type))
3472 `(cons :format "Key: %v" ,key-type ,value-type)))
3474 (define-widget 'choice 'menu-choice
3475 "A union of several sexp types."
3477 :format "%{%t%}: %[Value Menu%] %v"
3478 :button-prefix 'widget-push-button-prefix
3479 :button-suffix 'widget-push-button-suffix
3480 :prompt-value 'widget-choice-prompt-value)
3482 (defun widget-choice-prompt-value (widget prompt value unbound)
3484 (let ((args (widget-get widget :args))
3485 (completion-ignore-case (widget-get widget :case-fold))
3486 current choices old)
3487 ;; Find the first arg that matches VALUE.
3490 (if (widget-apply (car look) :match value)
3491 (setq old (car look)
3493 (setq look (cdr look)))))
3496 (cond ((= (length args) 0)
3498 ((= (length args) 1)
3500 ((and (= (length args) 2)
3502 (if (eq old (nth 0 args))
3507 (setq current (car args)
3510 (cons (cons (widget-apply current :menu-tag-get)
3513 (let ((val (completing-read prompt choices nil t)))
3515 (let ((try (try-completion val choices)))
3518 (cdr (assoc val choices)))
3521 (widget-prompt-value current prompt nil t)
3524 (define-widget 'radio 'radio-button-choice
3525 "A union of several sexp types."
3527 :format "%{%t%}:\n%v"
3528 :prompt-value 'widget-choice-prompt-value)
3530 (define-widget 'repeat 'editable-list
3531 "A variable length homogeneous list."
3533 :format "%{%t%}:\n%v%i\n")
3535 (define-widget 'set 'checklist
3536 "A list of members from a fixed set."
3538 :format "%{%t%}:\n%v")
3540 (define-widget 'boolean 'toggle
3541 "To be nil or non-nil, that is the question."
3543 :prompt-value 'widget-boolean-prompt-value
3544 :button-prefix 'widget-push-button-prefix
3545 :button-suffix 'widget-push-button-suffix
3546 :format "%{%t%}: %[Toggle%] %v\n"
3550 (defun widget-boolean-prompt-value (widget prompt value unbound)
3551 ;; Toggle a boolean.
3554 ;;; The `color' Widget.
3557 (define-widget 'color 'editable-field
3558 "Choose a color name (with sample)."
3559 :format "%t: %v (%{sample%})\n"
3563 :complete 'widget-color-complete
3564 :sample-face-get 'widget-color-sample-face-get
3565 :notify 'widget-color-notify
3566 :action 'widget-color-action)
3568 (defun widget-color-complete (widget)
3569 "Complete the color in WIDGET."
3570 (require 'facemenu) ; for facemenu-color-alist
3571 (let* ((prefix (buffer-substring-no-properties (widget-field-start widget)
3573 (list (or facemenu-color-alist (defined-colors)))
3574 (completion (try-completion prefix list)))
3575 (cond ((eq completion t)
3576 (message "Exact match."))
3578 (error "Can't find completion for \"%s\"" prefix))
3579 ((not (string-equal prefix completion))
3580 (insert-and-inherit (substring completion (length prefix))))
3582 (message "Making completion list...")
3583 (with-output-to-temp-buffer "*Completions*"
3584 (display-completion-list (all-completions prefix list nil)
3586 (message "Making completion list...done")))))
3588 (defun widget-color-sample-face-get (widget)
3589 (let* ((value (condition-case nil
3590 (widget-value widget)
3591 (error (widget-get widget :value)))))
3592 (if (color-defined-p value)
3593 (list (cons 'foreground-color value))
3596 (defun widget-color-action (widget &optional event)
3597 "Prompt for a color."
3598 (let* ((tag (widget-apply widget :menu-tag-get))
3599 (prompt (concat tag ": "))
3600 (value (widget-value widget))
3601 (start (widget-field-start widget))
3602 (answer (facemenu-read-color prompt)))
3603 (unless (zerop (length answer))
3604 (widget-value-set widget answer)
3606 (widget-apply widget :notify widget event))))
3608 (defun widget-color-notify (widget child &optional event)
3609 "Update the sample, and notify the parent."
3610 (overlay-put (widget-get widget :sample-overlay)
3611 'face (widget-apply widget :sample-face-get))
3612 (widget-default-notify widget child event))
3616 (defun widget-echo-help (pos)
3617 "Display help-echo text for widget at POS."
3618 (let* ((widget (widget-at pos))
3619 (help-echo (and widget (widget-get widget :help-echo))))
3620 (if (functionp help-echo)
3621 (setq help-echo (funcall help-echo widget)))
3622 (if help-echo (message "%s" (eval help-echo)))))
3628 ;;; arch-tag: a076e75e-18a1-4b46-8be5-3f317bcbc707
3629 ;;; wid-edit.el ends here