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, 2006 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 (overlay-put overlay 'mouse-face
407 (widget-apply widget :mouse-face-get)))
408 (overlay-put overlay 'pointer 'hand)
409 (overlay-put overlay 'follow-link follow-link)
410 (overlay-put overlay 'help-echo help-echo)))
412 (defun widget-mouse-help (window overlay point)
413 "Help-echo callback for widgets whose :help-echo is a function."
414 (with-current-buffer (overlay-buffer overlay)
415 (let* ((widget (widget-at (overlay-start overlay)))
416 (help-echo (if widget (widget-get widget :help-echo))))
417 (if (functionp help-echo)
418 (funcall help-echo widget)
421 (defun widget-specify-sample (widget from to)
422 "Specify sample for WIDGET between FROM and TO."
423 (let ((overlay (make-overlay from to nil t nil)))
424 (overlay-put overlay 'face (widget-apply widget :sample-face-get))
425 (overlay-put overlay 'evaporate t)
426 (widget-put widget :sample-overlay overlay)))
428 (defun widget-specify-doc (widget from to)
429 "Specify documentation for WIDGET between FROM and TO."
430 (let ((overlay (make-overlay from to nil t nil)))
431 (overlay-put overlay 'widget-doc widget)
432 (overlay-put overlay 'face widget-documentation-face)
433 (overlay-put overlay 'evaporate t)
434 (widget-put widget :doc-overlay overlay)))
436 (defmacro widget-specify-insert (&rest form)
437 "Execute FORM without inheriting any text properties."
439 (let ((inhibit-read-only t)
440 (inhibit-modification-hooks t))
441 (narrow-to-region (point) (point))
442 (prog1 (progn ,@form)
443 (goto-char (point-max))))))
445 (defface widget-inactive
446 '((t :inherit shadow))
447 "Face used for inactive widgets."
448 :group 'widget-faces)
449 ;; backward-compatibility alias
450 (put 'widget-inactive-face 'face-alias 'widget-inactive)
452 (defun widget-specify-inactive (widget from to)
453 "Make WIDGET inactive for user modifications."
454 (unless (widget-get widget :inactive)
455 (let ((overlay (make-overlay from to nil t nil)))
456 (overlay-put overlay 'face 'widget-inactive)
457 ;; This is disabled, as it makes the mouse cursor change shape.
458 ;; (overlay-put overlay 'mouse-face 'widget-inactive)
459 (overlay-put overlay 'evaporate t)
460 (overlay-put overlay 'priority 100)
461 (overlay-put overlay 'modification-hooks '(widget-overlay-inactive))
462 (widget-put widget :inactive overlay))))
464 (defun widget-overlay-inactive (&rest junk)
465 "Ignoring the arguments, signal an error."
466 (unless inhibit-read-only
467 (error "The widget here is not active")))
470 (defun widget-specify-active (widget)
471 "Make WIDGET active for user modifications."
472 (let ((inactive (widget-get widget :inactive)))
474 (delete-overlay inactive)
475 (widget-put widget :inactive nil))))
477 ;;; Widget Properties.
479 (defsubst widget-type (widget)
480 "Return the type of WIDGET, a symbol."
484 (defun widgetp (widget)
485 "Return non-nil iff WIDGET is a widget."
487 (get widget 'widget-type)
489 (symbolp (car widget))
490 (get (car widget) 'widget-type))))
492 (defun widget-get-indirect (widget property)
493 "In WIDGET, get the value of PROPERTY.
494 If the value is a symbol, return its binding.
495 Otherwise, just return the value."
496 (let ((value (widget-get widget property)))
501 (defun widget-member (widget property)
502 "Non-nil iff there is a definition in WIDGET for PROPERTY."
503 (cond ((plist-member (cdr widget) property)
506 (widget-member (get (car widget) 'widget-type) property))
509 (defun widget-value (widget)
510 "Extract the current value of WIDGET."
512 :value-to-external (widget-apply widget :value-get)))
514 (defun widget-value-set (widget value)
515 "Set the current value of WIDGET to VALUE."
517 :value-set (widget-apply widget
518 :value-to-internal value)))
520 (defun widget-default-get (widget)
521 "Extract the default external value of WIDGET."
522 (widget-apply widget :value-to-external
523 (or (widget-get widget :value)
524 (widget-apply widget :default-get))))
526 (defun widget-match-inline (widget vals)
527 "In WIDGET, match the start of VALS."
528 (cond ((widget-get widget :inline)
529 (widget-apply widget :match-inline vals))
531 (widget-apply widget :match (car vals)))
532 (cons (list (car vals)) (cdr vals)))
535 (defun widget-apply-action (widget &optional event)
536 "Apply :action in WIDGET in response to EVENT."
537 (if (widget-apply widget :active)
538 (widget-apply widget :action event)
539 (error "Attempt to perform action on inactive widget")))
541 ;;; Helper functions.
543 ;; These are widget specific.
546 (defun widget-prompt-value (widget prompt &optional value unbound)
547 "Prompt for a value matching WIDGET, using PROMPT.
548 The current value is assumed to be VALUE, unless UNBOUND is non-nil."
549 (unless (listp widget)
550 (setq widget (list widget)))
551 (setq prompt (format "[%s] %s" (widget-type widget) prompt))
552 (setq widget (widget-convert widget))
553 (let ((answer (widget-apply widget :prompt-value prompt value unbound)))
554 (unless (widget-apply widget :match answer)
555 (error "Value does not match %S type" (car widget)))
558 (defun widget-get-sibling (widget)
559 "Get the item WIDGET is assumed to toggle.
560 This is only meaningful for radio buttons or checkboxes in a list."
561 (let* ((children (widget-get (widget-get widget :parent) :children))
565 (setq child (car children)
566 children (cdr children))
567 (when (eq (widget-get child :button) widget)
568 (throw 'child child)))
571 (defun widget-map-buttons (function &optional buffer maparg)
572 "Map FUNCTION over the buttons in BUFFER.
573 FUNCTION is called with the arguments WIDGET and MAPARG.
575 If FUNCTION returns non-nil, the walk is cancelled.
577 The arguments MAPARG, and BUFFER default to nil and (current-buffer),
579 (let ((cur (point-min))
582 (with-current-buffer buffer (overlay-lists))
584 (setq overlays (append (car overlays) (cdr overlays)))
585 (while (setq cur (pop overlays))
586 (setq widget (overlay-get cur 'button))
587 (if (and widget (funcall function widget maparg))
588 (setq overlays nil)))))
592 (defcustom widget-image-directory (file-name-as-directory
593 (expand-file-name "custom" data-directory))
594 "Where widget button images are located.
595 If this variable is nil, widget will try to locate the directory
600 (defcustom widget-image-enable t
601 "If non nil, use image buttons in widgets when available."
606 (defcustom widget-image-conversion
607 '((xpm ".xpm") (gif ".gif") (png ".png") (jpeg ".jpg" ".jpeg")
609 "Conversion alist from image formats to file name suffixes."
611 :type '(repeat (cons :format "%v"
612 (symbol :tag "Image Format" unknown)
613 (repeat :tag "Suffixes"
614 (string :format "%v")))))
616 (defun widget-image-find (image)
617 "Create a graphical button from IMAGE.
618 IMAGE should either already be an image, or be a file name sans
619 extension (xpm, xbm, gif, jpg, or png) located in
620 `widget-image-directory' or otherwise where `find-image' will find it."
621 (cond ((not (and image widget-image-enable (display-graphic-p)))
622 ;; We don't want or can't use images.
625 (eq 'image (car image)))
626 ;; Already an image spec. Use it.
629 ;; A string. Look it up in relevant directories.
630 (let* ((load-path (cons widget-image-directory load-path))
632 (dolist (elt widget-image-conversion)
633 (dolist (ext (cdr elt))
634 (push (list :type (car elt) :file (concat image ext)) specs)))
635 (setq specs (nreverse specs))
641 (defvar widget-button-pressed-face 'widget-button-pressed
642 "Face used for pressed buttons in widgets.
643 This exists as a variable so it can be set locally in certain
646 (defun widget-image-insert (widget tag image &optional down inactive)
647 "In WIDGET, insert the text TAG or, if supported, IMAGE.
648 IMAGE should either be an image or an image file name sans extension
649 \(xpm, xbm, gif, jpg, or png) located in `widget-image-directory'.
651 Optional arguments DOWN and INACTIVE are used instead of IMAGE when the
652 button is pressed or inactive, respectively. These are currently ignored."
653 (if (and (display-graphic-p)
654 (setq image (widget-image-find image)))
655 (progn (widget-put widget :suppress-face t)
658 tag 'mouse-face widget-button-pressed-face)))
661 (defun widget-move-and-invoke (event)
662 "Move to where you click, and if it is an active field, invoke it."
664 (mouse-set-point event)
665 (let ((pos (widget-event-point event)))
666 (if (and pos (get-char-property pos 'button))
667 (widget-button-click event))))
671 (defgroup widget-button nil
672 "The look of various kinds of buttons."
675 (defcustom widget-button-prefix ""
676 "String used as prefix for buttons."
678 :group 'widget-button)
680 (defcustom widget-button-suffix ""
681 "String used as suffix for buttons."
683 :group 'widget-button)
685 ;;; Creating Widgets.
688 (defun widget-create (type &rest args)
689 "Create widget of TYPE.
690 The optional ARGS are additional keyword arguments."
691 (let ((widget (apply 'widget-convert type args)))
692 (widget-apply widget :create)
695 (defun widget-create-child-and-convert (parent type &rest args)
696 "As part of the widget PARENT, create a child widget TYPE.
697 The child is converted, using the keyword arguments ARGS."
698 (let ((widget (apply 'widget-convert type args)))
699 (widget-put widget :parent parent)
700 (unless (widget-get widget :indent)
701 (widget-put widget :indent (+ (or (widget-get parent :indent) 0)
702 (or (widget-get widget :extra-offset) 0)
703 (widget-get parent :offset))))
704 (widget-apply widget :create)
707 (defun widget-create-child (parent type)
708 "Create widget of TYPE."
709 (let ((widget (widget-copy type)))
710 (widget-put widget :parent parent)
711 (unless (widget-get widget :indent)
712 (widget-put widget :indent (+ (or (widget-get parent :indent) 0)
713 (or (widget-get widget :extra-offset) 0)
714 (widget-get parent :offset))))
715 (widget-apply widget :create)
718 (defun widget-create-child-value (parent type value)
719 "Create widget of TYPE with value VALUE."
720 (let ((widget (widget-copy type)))
721 (widget-put widget :value (widget-apply widget :value-to-internal value))
722 (widget-put widget :parent parent)
723 (unless (widget-get widget :indent)
724 (widget-put widget :indent (+ (or (widget-get parent :indent) 0)
725 (or (widget-get widget :extra-offset) 0)
726 (widget-get parent :offset))))
727 (widget-apply widget :create)
731 (defun widget-delete (widget)
733 (widget-apply widget :delete))
735 (defun widget-copy (widget)
736 "Make a deep copy of WIDGET."
737 (widget-apply (copy-sequence widget) :copy))
739 (defun widget-convert (type &rest args)
740 "Convert TYPE to a widget without inserting it in the buffer.
741 The optional ARGS are additional keyword arguments."
742 ;; Don't touch the type.
743 (let* ((widget (if (symbolp type)
745 (copy-sequence type)))
749 ;; First set the :args keyword.
750 (while (cdr current) ;Look in the type.
751 (if (and (keywordp (cadr current))
752 ;; If the last element is a keyword,
753 ;; it is still the :args element,
754 ;; even though it is a keyword.
756 (if (eq (cadr current) :args)
757 ;; If :args is explicitly specified, obey it.
759 ;; Some other irrelevant keyword.
760 (setq current (cdr (cdr current))))
761 (setcdr current (list :args (cdr current)))
763 (while (and args (not done)) ;Look in ARGS.
764 (cond ((eq (car args) :args)
765 ;; Handle explicit specification of :args.
766 (setq args (cadr args)
768 ((keywordp (car args))
769 (setq args (cddr args)))
772 (widget-put widget :args args))
773 ;; Then Convert the widget.
776 (let ((convert-widget (plist-get (cdr type) :convert-widget)))
778 (setq widget (funcall convert-widget widget))))
779 (setq type (get (car type) 'widget-type)))
780 ;; Finally set the keyword args.
782 (let ((next (nth 0 keys)))
785 (widget-put widget next (nth 1 keys))
786 (setq keys (nthcdr 2 keys)))
788 ;; Convert the :value to internal format.
789 (if (widget-member widget :value)
791 :value (widget-apply widget
793 (widget-get widget :value))))
794 ;; Return the newly create widget.
798 (defun widget-insert (&rest args)
799 "Call `insert' with ARGS even if surrounding text is read only."
800 (let ((inhibit-read-only t)
801 (inhibit-modification-hooks t))
802 (apply 'insert args)))
804 (defun widget-convert-text (type from to
805 &optional button-from button-to
807 "Return a widget of type TYPE with endpoint FROM TO.
808 No text will be inserted to the buffer, instead the text between FROM
809 and TO will be used as the widgets end points. If optional arguments
810 BUTTON-FROM and BUTTON-TO are given, these will be used as the widgets
812 Optional ARGS are extra keyword arguments for TYPE."
813 (let ((widget (apply 'widget-convert type :delete 'widget-leave-text args))
814 (from (copy-marker from))
815 (to (copy-marker to)))
816 (set-marker-insertion-type from t)
817 (set-marker-insertion-type to nil)
818 (widget-put widget :from from)
819 (widget-put widget :to to)
821 (widget-specify-button widget button-from button-to))
824 (defun widget-convert-button (type from to &rest args)
825 "Return a widget of type TYPE with endpoint FROM TO.
826 Optional ARGS are extra keyword arguments for TYPE.
827 No text will be inserted to the buffer, instead the text between FROM
828 and TO will be used as the widgets end points, as well as the widgets
830 (apply 'widget-convert-text type from to from to args))
832 (defun widget-leave-text (widget)
833 "Remove markers and overlays from WIDGET and its children."
834 (let ((button (widget-get widget :button-overlay))
835 (sample (widget-get widget :sample-overlay))
836 (doc (widget-get widget :doc-overlay))
837 (field (widget-get widget :field-overlay)))
838 (set-marker (widget-get widget :from) nil)
839 (set-marker (widget-get widget :to) nil)
841 (delete-overlay button))
843 (delete-overlay sample))
845 (delete-overlay doc))
847 (delete-overlay field))
848 (mapc 'widget-leave-text (widget-get widget :children))))
850 ;;; Keymap and Commands.
853 (defalias 'advertised-widget-backward 'widget-backward)
856 (defvar widget-keymap
857 (let ((map (make-sparse-keymap)))
858 (define-key map "\t" 'widget-forward)
859 (define-key map "\e\t" 'widget-backward)
860 (define-key map [(shift tab)] 'advertised-widget-backward)
861 (define-key map [backtab] 'widget-backward)
862 (define-key map [down-mouse-2] 'widget-button-click)
863 (define-key map [down-mouse-1] 'widget-button-click)
864 (define-key map "\C-m" 'widget-button-press)
866 "Keymap containing useful binding for buffers containing widgets.
867 Recommended as a parent keymap for modes using widgets.")
869 (defvar widget-global-map global-map
870 "Keymap used for events a widget does not handle itself.")
871 (make-variable-buffer-local 'widget-global-map)
873 (defvar widget-field-keymap
874 (let ((map (copy-keymap widget-keymap)))
875 (define-key map "\C-k" 'widget-kill-line)
876 (define-key map "\M-\t" 'widget-complete)
877 (define-key map "\C-m" 'widget-field-activate)
878 ;; Since the widget code uses a `field' property to identify fields,
879 ;; ordinary beginning-of-line does the right thing.
880 ;; (define-key map "\C-a" 'widget-beginning-of-line)
881 (define-key map "\C-e" 'widget-end-of-line)
883 "Keymap used inside an editable field.")
885 (defvar widget-text-keymap
886 (let ((map (copy-keymap widget-keymap)))
887 ;; Since the widget code uses a `field' property to identify fields,
888 ;; ordinary beginning-of-line does the right thing.
889 ;; (define-key map "\C-a" 'widget-beginning-of-line)
890 (define-key map "\C-e" 'widget-end-of-line)
892 "Keymap used inside a text field.")
894 (defun widget-field-activate (pos &optional event)
895 "Invoke the editable field at point."
897 (let ((field (widget-field-at pos)))
899 (widget-apply-action field event)
901 (lookup-key widget-global-map (this-command-keys))))))
903 (defface widget-button-pressed
904 '((((min-colors 88) (class color))
905 (:foreground "red1"))
909 (:weight bold :underline t)))
910 "Face used for pressed buttons."
911 :group 'widget-faces)
912 ;; backward-compatibility alias
913 (put 'widget-button-pressed-face 'face-alias 'widget-button-pressed)
915 (defvar widget-button-click-moves-point nil
916 "If non-nil, `widget-button-click' moves point to a button after invoking it.
917 If nil, point returns to its original position after invoking a button.")
919 (defun widget-button-click (event)
920 "Invoke the button that the mouse is pointing at."
922 (if (widget-event-point event)
923 (let* ((oevent event)
924 (mouse-1 (memq (event-basic-type event) '(mouse-1 down-mouse-1)))
925 (pos (widget-event-point event))
926 (start (event-start event))
927 (button (get-char-property
928 pos 'button (and (windowp (posn-window start))
929 (window-buffer (posn-window start)))))
931 (when (or (null button)
932 (catch 'button-press-cancelled
933 ;; Mouse click on a widget button. Do the following
934 ;; in a save-excursion so that the click on the button
935 ;; doesn't change point.
936 (save-selected-window
937 (select-window (posn-window (event-start event)))
939 (goto-char (posn-point (event-start event)))
940 (let* ((overlay (widget-get button :button-overlay))
941 (pressed-face (or (widget-get button :pressed-face)
942 widget-button-pressed-face))
943 (face (overlay-get overlay 'face))
944 (mouse-face (overlay-get overlay 'mouse-face)))
946 ;; Read events, including mouse-movement
947 ;; events, waiting for a release event. If we
948 ;; began with a mouse-1 event and receive a
949 ;; movement event, that means the user wants
950 ;; to perform drag-selection, so cancel the
951 ;; button press and do the default mouse-1
952 ;; action. For mouse-2, just highlight/
953 ;; unhighlight the button the mouse was
954 ;; initially on when we move over it.
956 (when face ; avoid changing around image
957 (overlay-put overlay 'face pressed-face)
958 (overlay-put overlay 'mouse-face pressed-face))
959 (unless (widget-apply button :mouse-down-action event)
960 (let ((track-mouse t))
961 (while (not (widget-button-release-event-p event))
962 (setq event (read-event))
963 (when (and mouse-1 (mouse-movement-p event))
964 (push event unread-command-events)
966 (throw 'button-press-cancelled t))
967 (unless (or (integerp event)
968 (memq (car event) '(switch-frame select-window))
969 (eq (car event) 'scroll-bar-movement))
970 (setq pos (widget-event-point event))
972 (eq (get-char-property pos 'button)
975 (overlay-put overlay 'face pressed-face)
976 (overlay-put overlay 'mouse-face pressed-face))
977 (overlay-put overlay 'face face)
978 (overlay-put overlay 'mouse-face mouse-face))))))
980 ;; When mouse is released over the button, run
981 ;; its action function.
982 (when (and pos (eq (get-char-property pos 'button) button))
984 (widget-apply-action button event)
985 (if widget-button-click-moves-point
986 (setq newpoint (point)))))
987 (overlay-put overlay 'face face)
988 (overlay-put overlay 'mouse-face mouse-face))))
990 (if newpoint (goto-char newpoint))
991 ;; This loses if the widget action switches windows. -- cyd
992 ;; (unless (pos-visible-in-window-p (widget-event-point event))
993 ;; (mouse-set-point event)
994 ;; (beginning-of-line)
998 (let ((up t) command)
999 ;; Mouse click not on a widget button. Find the global
1000 ;; command to run, and check whether it is bound to an
1003 (cond ((setq command ;down event
1004 (lookup-key widget-global-map [down-mouse-1]))
1006 ((setq command ;up event
1007 (lookup-key widget-global-map [mouse-1]))))
1008 (cond ((setq command ;down event
1009 (lookup-key widget-global-map [down-mouse-2]))
1011 ((setq command ;up event
1012 (lookup-key widget-global-map [mouse-2])))))
1014 ;; Don't execute up events twice.
1015 (while (not (widget-button-release-event-p event))
1016 (setq event (read-event))))
1018 (call-interactively command)))))
1019 (message "You clicked somewhere weird.")))
1021 (defun widget-button-press (pos &optional event)
1022 "Invoke button at POS."
1024 (let ((button (get-char-property pos 'button)))
1026 (widget-apply-action button event)
1027 (let ((command (lookup-key widget-global-map (this-command-keys))))
1028 (when (commandp command)
1029 (call-interactively command))))))
1031 (defun widget-tabable-at (&optional pos)
1032 "Return the tabable widget at POS, or nil.
1033 POS defaults to the value of (point)."
1034 (let ((widget (widget-at pos)))
1036 (let ((order (widget-get widget :tab-order)))
1042 (defvar widget-use-overlay-change t
1043 "If non-nil, use overlay change functions to tab around in the buffer.
1044 This is much faster, but doesn't work reliably on Emacs 19.34.")
1046 (defun widget-move (arg)
1047 "Move point to the ARG next field or button.
1048 ARG may be negative to move backward."
1049 (or (bobp) (> arg 0) (backward-char))
1052 (old (widget-tabable-at)))
1056 (goto-char (point-min))
1057 (setq wrapped (1+ wrapped)))
1058 (widget-use-overlay-change
1059 (goto-char (next-overlay-change (point))))
1064 (error "No buttons or fields found"))
1065 (let ((new (widget-tabable-at)))
1067 (unless (eq new old)
1073 (goto-char (point-max))
1074 (setq wrapped (1+ wrapped)))
1075 (widget-use-overlay-change
1076 (goto-char (previous-overlay-change (point))))
1081 (error "No buttons or fields found"))
1082 (let ((new (widget-tabable-at)))
1084 (unless (eq new old)
1085 (setq arg (1+ arg))))))
1086 (let ((new (widget-tabable-at)))
1087 (while (eq (widget-tabable-at) new)
1090 (widget-echo-help (point))
1091 (run-hooks 'widget-move-hook))
1093 (defun widget-forward (arg)
1094 "Move point to the next field or button.
1095 With optional ARG, move across that many fields."
1097 (run-hooks 'widget-forward-hook)
1100 (defun widget-backward (arg)
1101 "Move point to the previous field or button.
1102 With optional ARG, move across that many fields."
1104 (run-hooks 'widget-backward-hook)
1105 (widget-move (- arg)))
1107 ;; Since the widget code uses a `field' property to identify fields,
1108 ;; ordinary beginning-of-line does the right thing.
1109 (defalias 'widget-beginning-of-line 'beginning-of-line)
1111 (defun widget-end-of-line ()
1112 "Go to end of field or end of line, whichever is first.
1113 Trailing spaces at the end of padded fields are not considered part of
1116 ;; Ordinary end-of-line does the right thing, because we're inside
1117 ;; text with a `field' property.
1120 ;; ... except that we want to ignore trailing spaces in fields that
1121 ;; aren't terminated by a newline, because they are used as padding,
1122 ;; and ignored when extracting the entered value of the field.
1123 (skip-chars-backward " " (field-beginning (1- (point))))))
1125 (defun widget-kill-line ()
1126 "Kill to end of field or end of line, whichever is first."
1128 (let* ((field (widget-field-find (point)))
1129 (end (and field (widget-field-end field))))
1130 (if (and field (> (line-beginning-position 2) end))
1131 (kill-region (point) end)
1132 (call-interactively 'kill-line))))
1134 (defcustom widget-complete-field (lookup-key global-map "\M-\t")
1135 "Default function to call for completion inside fields."
1136 :options '(ispell-complete-word complete-tag lisp-complete-symbol)
1140 (defun widget-narrow-to-field ()
1143 (let ((field (widget-field-find (point))))
1145 (narrow-to-region (line-beginning-position) (line-end-position)))))
1147 (defun widget-complete ()
1148 "Complete content of editable field from point.
1149 When not inside a field, move to the previous button or field."
1151 (let ((field (widget-field-find (point))))
1154 (widget-narrow-to-field)
1155 (widget-apply field :complete))
1156 (error "Not in an editable field"))))
1158 ;;; Setting up the buffer.
1160 (defvar widget-field-new nil
1161 "List of all newly created editable fields in the buffer.")
1162 (make-variable-buffer-local 'widget-field-new)
1164 (defvar widget-field-list nil
1165 "List of all editable fields in the buffer.")
1166 (make-variable-buffer-local 'widget-field-list)
1168 (defun widget-at (&optional pos)
1169 "The button or field at POS (default, point)."
1170 (or (get-char-property (or pos (point)) 'button)
1171 (widget-field-at pos)))
1174 (defun widget-setup ()
1175 "Setup current buffer so editing string widgets works."
1176 (let ((inhibit-read-only t)
1177 (inhibit-modification-hooks t)
1179 (while widget-field-new
1180 (setq field (car widget-field-new)
1181 widget-field-new (cdr widget-field-new)
1182 widget-field-list (cons field widget-field-list))
1183 (let ((from (car (widget-get field :field-overlay)))
1184 (to (cdr (widget-get field :field-overlay))))
1185 (widget-specify-field field
1186 (marker-position from) (marker-position to))
1187 (set-marker from nil)
1188 (set-marker to nil))))
1190 (widget-add-change))
1192 (defvar widget-field-last nil)
1193 ;; Last field containing point.
1194 (make-variable-buffer-local 'widget-field-last)
1196 (defvar widget-field-was nil)
1197 ;; The widget data before the change.
1198 (make-variable-buffer-local 'widget-field-was)
1200 (defun widget-field-at (pos)
1201 "Return the widget field at POS, or nil if none."
1202 (let ((field (get-char-property (or pos (point)) 'field)))
1203 (if (eq field 'boundary)
1204 (get-char-property (or pos (point)) 'real-field)
1207 (defun widget-field-buffer (widget)
1208 "Return the buffer of WIDGET's editing field."
1209 (let ((overlay (widget-get widget :field-overlay)))
1210 (cond ((overlayp overlay)
1211 (overlay-buffer overlay))
1213 (marker-buffer (car overlay))))))
1215 (defun widget-field-start (widget)
1216 "Return the start of WIDGET's editing field."
1217 (let ((overlay (widget-get widget :field-overlay)))
1218 (if (overlayp overlay)
1219 (overlay-start overlay)
1222 (defun widget-field-end (widget)
1223 "Return the end of WIDGET's editing field."
1224 (let ((overlay (widget-get widget :field-overlay)))
1225 ;; Don't subtract one if local-map works at the end of the overlay,
1226 ;; or if a special `boundary' field has been added after the widget
1228 (if (overlayp overlay)
1229 ;; Don't proceed if overlay has been removed from buffer.
1230 (when (overlay-buffer overlay)
1231 (if (and (not (eq (with-current-buffer
1232 (widget-field-buffer widget)
1234 ;; `widget-narrow-to-field' can be
1235 ;; active when this function is called
1236 ;; from an change-functions hook. So
1237 ;; temporarily remove field narrowing
1238 ;; before to call `get-char-property'.
1240 (get-char-property (overlay-end overlay)
1243 (or widget-field-add-space
1244 (null (widget-get widget :size))))
1245 (1- (overlay-end overlay))
1246 (overlay-end overlay)))
1249 (defun widget-field-find (pos)
1250 "Return the field at POS.
1251 Unlike (get-char-property POS 'field), this works with empty fields too."
1252 (let ((fields widget-field-list)
1255 (setq field (car fields)
1256 fields (cdr fields))
1257 (when (and (<= (widget-field-start field) pos)
1258 (<= pos (widget-field-end field)))
1260 (error "Overlapping fields"))
1261 (setq found field)))
1264 (defun widget-before-change (from to)
1265 ;; This is how, for example, a variable changes its state to `modified'.
1266 ;; when it is being edited.
1267 (unless inhibit-read-only
1268 (let ((from-field (widget-field-find from))
1269 (to-field (widget-field-find to)))
1270 (cond ((not (eq from-field to-field))
1271 (add-hook 'post-command-hook 'widget-add-change nil t)
1272 (signal 'text-read-only
1273 '("Change should be restricted to a single field")))
1275 (add-hook 'post-command-hook 'widget-add-change nil t)
1276 (signal 'text-read-only
1277 '("Attempt to change text outside editable field")))
1278 (widget-field-use-before-change
1279 (widget-apply from-field :notify from-field))))))
1281 (defun widget-add-change ()
1282 (remove-hook 'post-command-hook 'widget-add-change t)
1283 (add-hook 'before-change-functions 'widget-before-change nil t)
1284 (add-hook 'after-change-functions 'widget-after-change nil t))
1286 (defun widget-after-change (from to old)
1287 "Adjust field size and text properties."
1288 (let ((field (widget-field-find from))
1289 (other (widget-field-find to)))
1291 (unless (eq field other)
1292 (error "Change in different fields"))
1293 (let ((size (widget-get field :size)))
1295 (let ((begin (widget-field-start field))
1296 (end (widget-field-end field)))
1297 (cond ((< (- end begin) size)
1301 (insert-char ?\s (- (+ begin size) end))))
1302 ((> (- end begin) size)
1303 ;; Field too large and
1304 (if (or (< (point) (+ begin size))
1306 ;; Point is outside extra space.
1307 (setq begin (+ begin size))
1308 ;; Point is within the extra space.
1309 (setq begin (point)))
1312 (while (and (eq (preceding-char) ?\s)
1314 (delete-backward-char 1)))))))
1315 (widget-specify-secret field))
1316 (widget-apply field :notify field))))
1318 ;;; Widget Functions
1320 ;; These functions are used in the definition of multiple widgets.
1322 (defun widget-parent-action (widget &optional event)
1323 "Tell :parent of WIDGET to handle the :action.
1324 Optional EVENT is the event that triggered the action."
1325 (widget-apply (widget-get widget :parent) :action event))
1327 (defun widget-children-value-delete (widget)
1328 "Delete all :children and :buttons in WIDGET."
1329 (mapc 'widget-delete (widget-get widget :children))
1330 (widget-put widget :children nil)
1331 (mapc 'widget-delete (widget-get widget :buttons))
1332 (widget-put widget :buttons nil))
1334 (defun widget-children-validate (widget)
1335 "All the :children must be valid."
1336 (let ((children (widget-get widget :children))
1338 (while (and children (not found))
1339 (setq child (car children)
1340 children (cdr children)
1341 found (widget-apply child :validate)))
1344 (defun widget-child-value-get (widget)
1345 "Get the value of the first member of :children in WIDGET."
1346 (widget-value (car (widget-get widget :children))))
1348 (defun widget-child-value-inline (widget)
1349 "Get the inline value of the first member of :children in WIDGET."
1350 (widget-apply (car (widget-get widget :children)) :value-inline))
1352 (defun widget-child-validate (widget)
1353 "The result of validating the first member of :children in WIDGET."
1354 (widget-apply (car (widget-get widget :children)) :validate))
1356 (defun widget-type-value-create (widget)
1357 "Convert and instantiate the value of the :type attribute of WIDGET.
1358 Store the newly created widget in the :children attribute.
1360 The value of the :type attribute should be an unconverted widget type."
1361 (let ((value (widget-get widget :value))
1362 (type (widget-get widget :type)))
1363 (widget-put widget :children
1364 (list (widget-create-child-value widget
1365 (widget-convert type)
1368 (defun widget-type-default-get (widget)
1369 "Get default value from the :type attribute of WIDGET.
1371 The value of the :type attribute should be an unconverted widget type."
1372 (widget-default-get (widget-convert (widget-get widget :type))))
1374 (defun widget-type-match (widget value)
1375 "Non-nil if the :type value of WIDGET matches VALUE.
1377 The value of the :type attribute should be an unconverted widget type."
1378 (widget-apply (widget-convert (widget-get widget :type)) :match value))
1380 (defun widget-types-copy (widget)
1381 "Copy :args as widget types in WIDGET."
1382 (widget-put widget :args (mapcar 'widget-copy (widget-get widget :args)))
1385 ;; Made defsubst to speed up face editor creation.
1386 (defsubst widget-types-convert-widget (widget)
1387 "Convert :args as widget types in WIDGET."
1388 (widget-put widget :args (mapcar 'widget-convert (widget-get widget :args)))
1391 (defun widget-value-convert-widget (widget)
1392 "Initialize :value from :args in WIDGET."
1393 (let ((args (widget-get widget :args)))
1395 (widget-put widget :value (car args))
1396 ;; Don't convert :value here, as this is done in `widget-convert'.
1397 ;; (widget-put widget :value (widget-apply widget
1398 ;; :value-to-internal (car args)))
1399 (widget-put widget :args nil)))
1402 (defun widget-value-value-get (widget)
1403 "Return the :value property of WIDGET."
1404 (widget-get widget :value))
1406 ;;; The `default' Widget.
1408 (define-widget 'default nil
1409 "Basic widget other widgets are derived from."
1410 :value-to-internal (lambda (widget value) value)
1411 :value-to-external (lambda (widget value) value)
1412 :button-prefix 'widget-button-prefix
1413 :button-suffix 'widget-button-suffix
1414 :complete 'widget-default-complete
1415 :create 'widget-default-create
1418 :format-handler 'widget-default-format-handler
1419 :button-face-get 'widget-default-button-face-get
1420 :mouse-face-get 'widget-default-mouse-face-get
1421 :sample-face-get 'widget-default-sample-face-get
1422 :delete 'widget-default-delete
1424 :value-set 'widget-default-value-set
1425 :value-inline 'widget-default-value-inline
1426 :value-delete 'ignore
1427 :default-get 'widget-default-default-get
1428 :menu-tag-get 'widget-default-menu-tag-get
1430 :active 'widget-default-active
1431 :activate 'widget-specify-active
1432 :deactivate 'widget-default-deactivate
1433 :mouse-down-action #'ignore
1434 :action 'widget-default-action
1435 :notify 'widget-default-notify
1436 :prompt-value 'widget-default-prompt-value)
1438 (defun widget-default-complete (widget)
1439 "Call the value of the :complete-function property of WIDGET.
1440 If that does not exists, call the value of `widget-complete-field'."
1441 (call-interactively (or (widget-get widget :complete-function)
1442 widget-complete-field)))
1444 (defun widget-default-create (widget)
1445 "Create WIDGET at point in the current buffer."
1446 (widget-specify-insert
1447 (let ((from (point))
1448 button-begin button-end
1449 sample-begin sample-end
1452 (insert (widget-get widget :format))
1454 ;; Parse escapes in format.
1455 (while (re-search-forward "%\\(.\\)" nil t)
1456 (let ((escape (char-after (match-beginning 1))))
1457 (delete-backward-char 2)
1458 (cond ((eq escape ?%)
1461 (setq button-begin (point))
1462 (insert (widget-get-indirect widget :button-prefix)))
1464 (insert (widget-get-indirect widget :button-suffix))
1465 (setq button-end (point)))
1467 (setq sample-begin (point)))
1469 (setq sample-end (point)))
1471 (when (widget-get widget :indent)
1473 (insert-char ?\s (widget-get widget :indent))))
1475 (let ((image (widget-get widget :tag-glyph))
1476 (tag (widget-get widget :tag)))
1478 (widget-image-insert widget (or tag "image") image))
1482 (princ (widget-get widget :value)
1483 (current-buffer))))))
1485 (let ((doc (widget-get widget :doc)))
1487 (setq doc-begin (point))
1489 (while (eq (preceding-char) ?\n)
1490 (delete-backward-char 1))
1492 (setq doc-end (point)))))
1494 (if (and button-begin (not button-end))
1495 (widget-apply widget :value-create)
1496 (setq value-pos (point))))
1498 (widget-apply widget :format-handler escape)))))
1499 ;; Specify button, sample, and doc, and insert value.
1500 (and button-begin button-end
1501 (widget-specify-button widget button-begin button-end))
1502 (and sample-begin sample-end
1503 (widget-specify-sample widget sample-begin sample-end))
1504 (and doc-begin doc-end
1505 (widget-specify-doc widget doc-begin doc-end))
1507 (goto-char value-pos)
1508 (widget-apply widget :value-create)))
1509 (let ((from (point-min-marker))
1510 (to (point-max-marker)))
1511 (set-marker-insertion-type from t)
1512 (set-marker-insertion-type to nil)
1513 (widget-put widget :from from)
1514 (widget-put widget :to to)))
1515 (widget-clear-undo))
1517 (defun widget-default-format-handler (widget escape)
1518 ;; We recognize the %h escape by default.
1519 (let* ((buttons (widget-get widget :buttons)))
1520 (cond ((eq escape ?h)
1521 (let* ((doc-property (widget-get widget :documentation-property))
1522 (doc-try (cond ((widget-get widget :doc))
1523 ((functionp doc-property)
1524 (funcall doc-property
1525 (widget-get widget :value)))
1526 ((symbolp doc-property)
1527 (documentation-property
1528 (widget-get widget :value)
1530 (doc-text (and (stringp doc-try)
1531 (> (length doc-try) 1)
1533 (doc-indent (widget-get widget :documentation-indent)))
1535 (and (eq (preceding-char) ?\n)
1536 (widget-get widget :indent)
1537 (insert-char ?\s (widget-get widget :indent)))
1538 ;; The `*' in the beginning is redundant.
1539 (when (eq (aref doc-text 0) ?*)
1540 (setq doc-text (substring doc-text 1)))
1541 ;; Get rid of trailing newlines.
1542 (when (string-match "\n+\\'" doc-text)
1543 (setq doc-text (substring doc-text 0 (match-beginning 0))))
1544 (push (widget-create-child-and-convert
1545 widget 'documentation-string
1546 :indent (cond ((numberp doc-indent )
1554 (error "Unknown escape `%c'" escape)))
1555 (widget-put widget :buttons buttons)))
1557 (defun widget-default-button-face-get (widget)
1558 ;; Use :button-face or widget-button-face
1559 (or (widget-get widget :button-face)
1560 (let ((parent (widget-get widget :parent)))
1562 (widget-apply parent :button-face-get)
1563 widget-button-face))))
1565 (defun widget-default-mouse-face-get (widget)
1566 ;; Use :mouse-face or widget-mouse-face
1567 (or (widget-get widget :mouse-face)
1568 (let ((parent (widget-get widget :parent)))
1570 (widget-apply parent :mouse-face-get)
1571 widget-mouse-face))))
1573 (defun widget-default-sample-face-get (widget)
1574 ;; Use :sample-face.
1575 (widget-get widget :sample-face))
1577 (defun widget-default-delete (widget)
1578 "Remove widget from the buffer."
1579 (let ((from (widget-get widget :from))
1580 (to (widget-get widget :to))
1581 (inactive-overlay (widget-get widget :inactive))
1582 (button-overlay (widget-get widget :button-overlay))
1583 (sample-overlay (widget-get widget :sample-overlay))
1584 (doc-overlay (widget-get widget :doc-overlay))
1585 (inhibit-modification-hooks t)
1586 (inhibit-read-only t))
1587 (widget-apply widget :value-delete)
1588 (widget-children-value-delete widget)
1589 (when inactive-overlay
1590 (delete-overlay inactive-overlay))
1591 (when button-overlay
1592 (delete-overlay button-overlay))
1593 (when sample-overlay
1594 (delete-overlay sample-overlay))
1596 (delete-overlay doc-overlay))
1598 ;; Kludge: this doesn't need to be true for empty formats.
1599 (delete-region from to))
1600 (set-marker from nil)
1601 (set-marker to nil))
1602 (widget-clear-undo))
1604 (defun widget-default-value-set (widget value)
1605 "Recreate widget with new value."
1606 (let* ((old-pos (point))
1607 (from (copy-marker (widget-get widget :from)))
1608 (to (copy-marker (widget-get widget :to)))
1609 (offset (if (and (<= from old-pos) (<= old-pos to))
1610 (if (>= old-pos (1- to))
1612 (- old-pos from)))))
1613 ;;??? Bug: this ought to insert the new value before deleting the old one,
1614 ;; so that markers on either side of the value automatically
1615 ;; stay on the same side. -- rms.
1617 (goto-char (widget-get widget :from))
1618 (widget-apply widget :delete)
1619 (widget-put widget :value value)
1620 (widget-apply widget :create))
1623 (goto-char (+ (widget-get widget :to) offset 1))
1624 (goto-char (min (+ from offset) (1- (widget-get widget :to))))))))
1626 (defun widget-default-value-inline (widget)
1627 "Wrap value in a list unless it is inline."
1628 (if (widget-get widget :inline)
1629 (widget-value widget)
1630 (list (widget-value widget))))
1632 (defun widget-default-default-get (widget)
1634 (widget-get widget :value))
1636 (defun widget-default-menu-tag-get (widget)
1637 "Use tag or value for menus."
1638 (or (widget-get widget :menu-tag)
1639 (widget-get widget :tag)
1640 (widget-princ-to-string (widget-get widget :value))))
1642 (defun widget-default-active (widget)
1643 "Return t iff this widget active (user modifiable)."
1644 (or (widget-get widget :always-active)
1645 (and (not (widget-get widget :inactive))
1646 (let ((parent (widget-get widget :parent)))
1648 (widget-apply parent :active))))))
1650 (defun widget-default-deactivate (widget)
1651 "Make WIDGET inactive for user modifications."
1652 (widget-specify-inactive widget
1653 (widget-get widget :from)
1654 (widget-get widget :to)))
1656 (defun widget-default-action (widget &optional event)
1657 "Notify the parent when a widget changes."
1658 (let ((parent (widget-get widget :parent)))
1660 (widget-apply parent :notify widget event))))
1662 (defun widget-default-notify (widget child &optional event)
1663 "Pass notification to parent."
1664 (widget-default-action widget event))
1666 (defun widget-default-prompt-value (widget prompt value unbound)
1667 "Read an arbitrary value. Stolen from `set-variable'."
1668 ;; (let ((initial (if unbound
1670 ;; It would be nice if we could do a `(cons val 1)' here.
1671 ;; (prin1-to-string (custom-quote value))))))
1672 (eval-minibuffer prompt))
1674 ;;; The `item' Widget.
1676 (define-widget 'item 'default
1677 "Constant items for inclusion in other widgets."
1678 :convert-widget 'widget-value-convert-widget
1679 :value-create 'widget-item-value-create
1680 :value-delete 'ignore
1681 :value-get 'widget-value-value-get
1682 :match 'widget-item-match
1683 :match-inline 'widget-item-match-inline
1684 :action 'widget-item-action
1687 (defun widget-item-value-create (widget)
1688 "Insert the printed representation of the value."
1689 (princ (widget-get widget :value) (current-buffer)))
1691 (defun widget-item-match (widget value)
1692 ;; Match if the value is the same.
1693 (equal (widget-get widget :value) value))
1695 (defun widget-item-match-inline (widget values)
1696 ;; Match if the value is the same.
1697 (let ((value (widget-get widget :value)))
1699 (<= (length value) (length values))
1700 (let ((head (widget-sublist values 0 (length value))))
1701 (and (equal head value)
1702 (cons head (widget-sublist values (length value))))))))
1704 (defun widget-sublist (list start &optional end)
1705 "Return the sublist of LIST from START to END.
1706 If END is omitted, it defaults to the length of LIST."
1707 (if (> start 0) (setq list (nthcdr start list)))
1709 (unless (<= end start)
1710 (setq list (copy-sequence list))
1711 (setcdr (nthcdr (- end start 1) list) nil)
1713 (copy-sequence list)))
1715 (defun widget-item-action (widget &optional event)
1716 ;; Just notify itself.
1717 (widget-apply widget :notify widget event))
1719 ;;; The `push-button' Widget.
1721 ;; (defcustom widget-push-button-gui t
1722 ;; "If non nil, use GUI push buttons when available."
1726 ;; Cache already created GUI objects.
1727 ;; (defvar widget-push-button-cache nil)
1729 (defcustom widget-push-button-prefix "["
1730 "String used as prefix for buttons."
1732 :group 'widget-button)
1734 (defcustom widget-push-button-suffix "]"
1735 "String used as suffix for buttons."
1737 :group 'widget-button)
1739 (define-widget 'push-button 'item
1740 "A pushable button."
1743 :value-create 'widget-push-button-value-create
1746 (defun widget-push-button-value-create (widget)
1747 "Insert text representing the `on' and `off' states."
1748 (let* ((tag (or (widget-get widget :tag)
1749 (widget-get widget :value)))
1750 (tag-glyph (widget-get widget :tag-glyph))
1751 (text (concat widget-push-button-prefix
1752 tag widget-push-button-suffix)))
1754 (widget-image-insert widget text tag-glyph)
1757 ;; (defun widget-gui-action (widget)
1758 ;; "Apply :action for WIDGET."
1759 ;; (widget-apply-action widget (this-command-keys)))
1761 ;;; The `link' Widget.
1763 (defcustom widget-link-prefix "["
1764 "String used as prefix for links."
1766 :group 'widget-button)
1768 (defcustom widget-link-suffix "]"
1769 "String used as suffix for links."
1771 :group 'widget-button)
1773 (define-widget 'link 'item
1775 :button-prefix 'widget-link-prefix
1776 :button-suffix 'widget-link-suffix
1778 :help-echo "Follow the link."
1781 ;;; The `info-link' Widget.
1783 (define-widget 'info-link 'link
1784 "A link to an info file."
1785 :action 'widget-info-link-action)
1787 (defun widget-info-link-action (widget &optional event)
1788 "Open the info node specified by WIDGET."
1789 (info (widget-value widget)))
1791 ;;; The `url-link' Widget.
1793 (define-widget 'url-link 'link
1794 "A link to an www page."
1795 :action 'widget-url-link-action)
1797 (defun widget-url-link-action (widget &optional event)
1798 "Open the URL specified by WIDGET."
1799 (browse-url (widget-value widget)))
1801 ;;; The `function-link' Widget.
1803 (define-widget 'function-link 'link
1804 "A link to an Emacs function."
1805 :action 'widget-function-link-action)
1807 (defun widget-function-link-action (widget &optional event)
1808 "Show the function specified by WIDGET."
1809 (describe-function (widget-value widget)))
1811 ;;; The `variable-link' Widget.
1813 (define-widget 'variable-link 'link
1814 "A link to an Emacs variable."
1815 :action 'widget-variable-link-action)
1817 (defun widget-variable-link-action (widget &optional event)
1818 "Show the variable specified by WIDGET."
1819 (describe-variable (widget-value widget)))
1821 ;;; The `file-link' Widget.
1823 (define-widget 'file-link 'link
1825 :action 'widget-file-link-action)
1827 (defun widget-file-link-action (widget &optional event)
1828 "Find the file specified by WIDGET."
1829 (find-file (widget-value widget)))
1831 ;;; The `emacs-library-link' Widget.
1833 (define-widget 'emacs-library-link 'link
1834 "A link to an Emacs Lisp library file."
1835 :action 'widget-emacs-library-link-action)
1837 (defun widget-emacs-library-link-action (widget &optional event)
1838 "Find the Emacs library file specified by WIDGET."
1839 (find-file (locate-library (widget-value widget))))
1841 ;;; The `emacs-commentary-link' Widget.
1843 (define-widget 'emacs-commentary-link 'link
1844 "A link to Commentary in an Emacs Lisp library file."
1845 :action 'widget-emacs-commentary-link-action)
1847 (defun widget-emacs-commentary-link-action (widget &optional event)
1848 "Find the Commentary section of the Emacs file specified by WIDGET."
1849 (finder-commentary (widget-value widget)))
1851 ;;; The `editable-field' Widget.
1853 (define-widget 'editable-field 'default
1854 "An editable text field."
1855 :convert-widget 'widget-value-convert-widget
1856 :keymap widget-field-keymap
1858 :help-echo "M-TAB: complete field; RET: enter value"
1860 :prompt-internal 'widget-field-prompt-internal
1861 :prompt-history 'widget-field-history
1862 :prompt-value 'widget-field-prompt-value
1863 :action 'widget-field-action
1864 :validate 'widget-field-validate
1866 :error "Field's value doesn't match allowed forms"
1867 :value-create 'widget-field-value-create
1868 :value-delete 'widget-field-value-delete
1869 :value-get 'widget-field-value-get
1870 :match 'widget-field-match)
1872 (defvar widget-field-history nil
1873 "History of field minibuffer edits.")
1875 (defun widget-field-prompt-internal (widget prompt initial history)
1876 "Read string for WIDGET prompting with PROMPT.
1877 INITIAL is the initial input and HISTORY is a symbol containing
1879 (read-string prompt initial history))
1881 (defun widget-field-prompt-value (widget prompt value unbound)
1882 "Prompt for a string."
1883 (widget-apply widget
1885 (widget-apply widget
1886 :prompt-internal prompt
1888 (cons (widget-apply widget
1889 :value-to-internal value)
1891 (widget-get widget :prompt-history))))
1893 (defvar widget-edit-functions nil)
1895 (defun widget-field-action (widget &optional event)
1896 "Move to next field."
1898 (run-hook-with-args 'widget-edit-functions widget))
1900 (defun widget-field-validate (widget)
1901 "Valid if the content matches `:valid-regexp'."
1902 (unless (string-match (widget-get widget :valid-regexp)
1903 (widget-apply widget :value-get))
1906 (defun widget-field-value-create (widget)
1907 "Create an editable text field."
1908 (let ((size (widget-get widget :size))
1909 (value (widget-get widget :value))
1911 ;; This is changed to a real overlay in `widget-setup'. We
1912 ;; need the end points to behave differently until
1913 ;; `widget-setup' is called.
1914 (overlay (cons (make-marker) (make-marker))))
1915 (widget-put widget :field-overlay overlay)
1918 (< (length value) size)
1919 (insert-char ?\s (- size (length value))))
1920 (unless (memq widget widget-field-list)
1921 (setq widget-field-new (cons widget widget-field-new)))
1922 (move-marker (cdr overlay) (point))
1923 (set-marker-insertion-type (cdr overlay) nil)
1926 (move-marker (car overlay) from)
1927 (set-marker-insertion-type (car overlay) t)))
1929 (defun widget-field-value-delete (widget)
1930 "Remove the widget from the list of active editing fields."
1931 (setq widget-field-list (delq widget widget-field-list))
1932 (setq widget-field-new (delq widget widget-field-new))
1933 ;; These are nil if the :format string doesn't contain `%v'.
1934 (let ((overlay (widget-get widget :field-overlay)))
1935 (when (overlayp overlay)
1936 (delete-overlay overlay))))
1938 (defun widget-field-value-get (widget)
1939 "Return current text in editing field."
1940 (let ((from (widget-field-start widget))
1941 (to (widget-field-end widget))
1942 (buffer (widget-field-buffer widget))
1943 (size (widget-get widget :size))
1944 (secret (widget-get widget :secret))
1945 (old (current-buffer)))
1952 (eq (char-after (1- to)) ?\s))
1954 (let ((result (buffer-substring-no-properties from to)))
1957 (while (< (+ from index) to)
1959 (get-char-property (+ from index) 'secret))
1960 (setq index (1+ index)))))
1963 (widget-get widget :value))))
1965 (defun widget-field-match (widget value)
1966 ;; Match any string.
1969 ;;; The `text' Widget.
1971 (define-widget 'text 'editable-field
1972 "A multiline text area."
1973 :keymap widget-text-keymap)
1975 ;;; The `menu-choice' Widget.
1977 (define-widget 'menu-choice 'default
1978 "A menu of options."
1979 :convert-widget 'widget-types-convert-widget
1980 :copy 'widget-types-copy
1981 :format "%[%t%]: %v"
1984 :void '(item :format "invalid (%t)\n")
1985 :value-create 'widget-choice-value-create
1986 :value-get 'widget-child-value-get
1987 :value-inline 'widget-child-value-inline
1988 :default-get 'widget-choice-default-get
1989 :mouse-down-action 'widget-choice-mouse-down-action
1990 :action 'widget-choice-action
1991 :error "Make a choice"
1992 :validate 'widget-choice-validate
1993 :match 'widget-choice-match
1994 :match-inline 'widget-choice-match-inline)
1996 (defun widget-choice-value-create (widget)
1997 "Insert the first choice that matches the value."
1998 (let ((value (widget-get widget :value))
1999 (args (widget-get widget :args))
2000 (explicit (widget-get widget :explicit-choice))
2004 ;; If the user specified the choice for this value,
2005 ;; respect that choice.
2006 (widget-put widget :children (list (widget-create-child-value
2007 widget explicit value)))
2008 (widget-put widget :choice explicit)
2009 (widget-put widget :explicit-choice nil))
2011 (setq current (car args)
2013 (when (widget-apply current :match value)
2014 (widget-put widget :children (list (widget-create-child-value
2015 widget current value)))
2016 (widget-put widget :choice current)
2020 (let ((void (widget-get widget :void)))
2021 (widget-put widget :children (list (widget-create-child-and-convert
2022 widget void :value value)))
2023 (widget-put widget :choice void))))))
2025 (defun widget-choice-default-get (widget)
2026 ;; Get default for the first choice.
2027 (widget-default-get (car (widget-get widget :args))))
2029 (defcustom widget-choice-toggle nil
2030 "If non-nil, a binary choice will just toggle between the values.
2031 Otherwise, the user will explicitly have to choose between the values
2032 when he invoked the menu."
2036 (defun widget-choice-mouse-down-action (widget &optional event)
2037 ;; Return non-nil if we need a menu.
2038 (let ((args (widget-get widget :args))
2039 (old (widget-get widget :choice)))
2040 (cond ((not (display-popup-menus-p))
2041 ;; No place to pop up a menu.
2043 ((< (length args) 2)
2044 ;; Empty or singleton list, just return the value.
2046 ((> (length args) widget-menu-max-size)
2047 ;; Too long, prompt.
2049 ((> (length args) 2)
2050 ;; Reasonable sized list, use menu.
2052 ((and widget-choice-toggle (memq old args))
2056 ;; Ask which of the two.
2059 (defun widget-choice-action (widget &optional event)
2061 (let ((args (widget-get widget :args))
2062 (old (widget-get widget :choice))
2063 (tag (widget-apply widget :menu-tag-get))
2064 (completion-ignore-case (widget-get widget :case-fold))
2067 ;; Remember old value.
2068 (if (and old (not (widget-apply widget :validate)))
2069 (let* ((external (widget-value widget))
2070 (internal (widget-apply old :value-to-internal external)))
2071 (widget-put old :value internal)))
2074 (cond ((= (length args) 0)
2076 ((= (length args) 1)
2078 ((and widget-choice-toggle
2081 (if (eq old (nth 0 args))
2086 (setq current (car args)
2089 (cons (cons (widget-apply current :menu-tag-get)
2092 (setq this-explicit t)
2093 (widget-choose tag (reverse choices) event))))
2095 ;; If this was an explicit user choice, record the choice,
2096 ;; so that widget-choice-value-create will respect it.
2098 (widget-put widget :explicit-choice current))
2099 (widget-value-set widget (widget-default-get current))
2101 (widget-apply widget :notify widget event)))
2102 (run-hook-with-args 'widget-edit-functions widget))
2104 (defun widget-choice-validate (widget)
2105 ;; Valid if we have made a valid choice.
2106 (if (eq (widget-get widget :void) (widget-get widget :choice))
2108 (widget-apply (car (widget-get widget :children)) :validate)))
2110 (defun widget-choice-match (widget value)
2111 ;; Matches if one of the choices matches.
2112 (let ((args (widget-get widget :args))
2114 (while (and args (not found))
2115 (setq current (car args)
2117 found (widget-apply current :match value)))
2120 (defun widget-choice-match-inline (widget values)
2121 ;; Matches if one of the choices matches.
2122 (let ((args (widget-get widget :args))
2124 (while (and args (null found))
2125 (setq current (car args)
2127 found (widget-match-inline current values)))
2130 ;;; The `toggle' Widget.
2132 (define-widget 'toggle 'item
2133 "Toggle between two states."
2135 :value-create 'widget-toggle-value-create
2136 :action 'widget-toggle-action
2137 :match (lambda (widget value) t)
2141 (defun widget-toggle-value-create (widget)
2142 "Insert text representing the `on' and `off' states."
2143 (if (widget-value widget)
2144 (let ((image (widget-get widget :on-glyph)))
2145 (and (display-graphic-p)
2147 (not (eq (car image) 'image))
2148 (widget-put widget :on-glyph (setq image (eval image))))
2149 (widget-image-insert widget
2150 (widget-get widget :on)
2152 (let ((image (widget-get widget :off-glyph)))
2153 (and (display-graphic-p)
2155 (not (eq (car image) 'image))
2156 (widget-put widget :off-glyph (setq image (eval image))))
2157 (widget-image-insert widget (widget-get widget :off) image))))
2159 (defun widget-toggle-action (widget &optional event)
2161 (widget-value-set widget (not (widget-value widget)))
2162 (widget-apply widget :notify widget event)
2163 (run-hook-with-args 'widget-edit-functions widget))
2165 ;;; The `checkbox' Widget.
2167 (define-widget 'checkbox 'toggle
2168 "A checkbox toggle."
2173 ;; We could probably do the same job as the images using single
2174 ;; space characters in a boxed face with a stretch specification to
2175 ;; make them square.
2176 :on-glyph '(create-image "\300\300\141\143\067\076\034\030"
2177 'xbm t :width 8 :height 8
2178 :background "grey75" ; like default mode line
2183 :off-glyph '(create-image (make-string 8 0)
2184 'xbm t :width 8 :height 8
2185 :background "grey75"
2189 :help-echo "Toggle this item."
2190 :action 'widget-checkbox-action)
2192 (defun widget-checkbox-action (widget &optional event)
2193 "Toggle checkbox, notify parent, and set active state of sibling."
2194 (widget-toggle-action widget event)
2195 (let ((sibling (widget-get-sibling widget)))
2197 (if (widget-value widget)
2198 (widget-apply sibling :activate)
2199 (widget-apply sibling :deactivate))
2200 (widget-clear-undo))))
2202 ;;; The `checklist' Widget.
2204 (define-widget 'checklist 'default
2205 "A multiple choice widget."
2206 :convert-widget 'widget-types-convert-widget
2207 :copy 'widget-types-copy
2210 :entry-format "%b %v"
2212 :value-create 'widget-checklist-value-create
2213 :value-get 'widget-checklist-value-get
2214 :validate 'widget-checklist-validate
2215 :match 'widget-checklist-match
2216 :match-inline 'widget-checklist-match-inline)
2218 (defun widget-checklist-value-create (widget)
2219 ;; Insert all values
2220 (let ((alist (widget-checklist-match-find widget (widget-get widget :value)))
2221 (args (widget-get widget :args)))
2223 (widget-checklist-add-item widget (car args) (assq (car args) alist))
2224 (setq args (cdr args)))
2225 (widget-put widget :children (nreverse (widget-get widget :children)))))
2227 (defun widget-checklist-add-item (widget type chosen)
2228 "Create checklist item in WIDGET of type TYPE.
2229 If the item is checked, CHOSEN is a cons whose cdr is the value."
2230 (and (eq (preceding-char) ?\n)
2231 (widget-get widget :indent)
2232 (insert-char ?\s (widget-get widget :indent)))
2233 (widget-specify-insert
2234 (let* ((children (widget-get widget :children))
2235 (buttons (widget-get widget :buttons))
2236 (button-args (or (widget-get type :sibling-args)
2237 (widget-get widget :button-args)))
2240 (insert (widget-get widget :entry-format))
2242 ;; Parse % escapes in format.
2243 (while (re-search-forward "%\\([bv%]\\)" nil t)
2244 (let ((escape (char-after (match-beginning 1))))
2245 (delete-backward-char 2)
2246 (cond ((eq escape ?%)
2249 (setq button (apply 'widget-create-child-and-convert
2251 :value (not (null chosen))
2256 (let ((child (widget-create-child widget type)))
2257 (widget-apply child :deactivate)
2259 ((widget-get type :inline)
2260 (widget-create-child-value
2261 widget type (cdr chosen)))
2263 (widget-create-child-value
2264 widget type (car (cdr chosen)))))))
2266 (error "Unknown escape `%c'" escape)))))
2267 ;; Update properties.
2268 (and button child (widget-put child :button button))
2269 (and button (widget-put widget :buttons (cons button buttons)))
2270 (and child (widget-put widget :children (cons child children))))))
2272 (defun widget-checklist-match (widget values)
2273 ;; All values must match a type in the checklist.
2275 (null (cdr (widget-checklist-match-inline widget values)))))
2277 (defun widget-checklist-match-inline (widget values)
2278 ;; Find the values which match a type in the checklist.
2279 (let ((greedy (widget-get widget :greedy))
2280 (args (copy-sequence (widget-get widget :args)))
2283 (let ((answer (widget-checklist-match-up args values)))
2285 (let ((vals (widget-match-inline answer values)))
2286 (setq found (append found (car vals))
2288 args (delq answer args))))
2290 (setq rest (append rest (list (car values)))
2291 values (cdr values)))
2293 (setq rest (append rest values)
2297 (defun widget-checklist-match-find (widget vals)
2298 "Find the vals which match a type in the checklist.
2299 Return an alist of (TYPE MATCH)."
2300 (let ((greedy (widget-get widget :greedy))
2301 (args (copy-sequence (widget-get widget :args)))
2304 (let ((answer (widget-checklist-match-up args vals)))
2306 (let ((match (widget-match-inline answer vals)))
2307 (setq found (cons (cons answer (car match)) found)
2309 args (delq answer args))))
2311 (setq vals (cdr vals)))
2316 (defun widget-checklist-match-up (args vals)
2317 "Return the first type from ARGS that matches VALS."
2318 (let (current found)
2319 (while (and args (null found))
2320 (setq current (car args)
2322 found (widget-match-inline current vals)))
2326 (defun widget-checklist-value-get (widget)
2327 ;; The values of all selected items.
2328 (let ((children (widget-get widget :children))
2331 (setq child (car children)
2332 children (cdr children))
2333 (if (widget-value (widget-get child :button))
2334 (setq result (append result (widget-apply child :value-inline)))))
2337 (defun widget-checklist-validate (widget)
2338 ;; Ticked chilren must be valid.
2339 (let ((children (widget-get widget :children))
2341 (while (and children (not found))
2342 (setq child (car children)
2343 children (cdr children)
2344 button (widget-get child :button)
2345 found (and (widget-value button)
2346 (widget-apply child :validate))))
2349 ;;; The `option' Widget
2351 (define-widget 'option 'checklist
2352 "An widget with an optional item."
2355 ;;; The `choice-item' Widget.
2357 (define-widget 'choice-item 'item
2358 "Button items that delegate action events to their parents."
2359 :action 'widget-parent-action
2360 :format "%[%t%] \n")
2362 ;;; The `radio-button' Widget.
2364 (define-widget 'radio-button 'toggle
2365 "A radio button for use in the `radio' widget."
2366 :notify 'widget-radio-button-notify
2373 :off-glyph "radio0")
2375 (defun widget-radio-button-notify (widget child &optional event)
2377 (widget-apply (widget-get widget :parent) :action widget event))
2379 ;;; The `radio-button-choice' Widget.
2381 (define-widget 'radio-button-choice 'default
2382 "Select one of multiple options."
2383 :convert-widget 'widget-types-convert-widget
2384 :copy 'widget-types-copy
2387 :entry-format "%b %v"
2388 :value-create 'widget-radio-value-create
2389 :value-get 'widget-radio-value-get
2390 :value-inline 'widget-radio-value-inline
2391 :value-set 'widget-radio-value-set
2392 :error "You must push one of the buttons"
2393 :validate 'widget-radio-validate
2394 :match 'widget-choice-match
2395 :match-inline 'widget-choice-match-inline
2396 :action 'widget-radio-action)
2398 (defun widget-radio-value-create (widget)
2399 ;; Insert all values
2400 (let ((args (widget-get widget :args))
2403 (setq arg (car args)
2405 (widget-radio-add-item widget arg))))
2407 (defun widget-radio-add-item (widget type)
2408 "Add to radio widget WIDGET a new radio button item of type TYPE."
2409 ;; (setq type (widget-convert type))
2410 (and (eq (preceding-char) ?\n)
2411 (widget-get widget :indent)
2412 (insert-char ?\s (widget-get widget :indent)))
2413 (widget-specify-insert
2414 (let* ((value (widget-get widget :value))
2415 (children (widget-get widget :children))
2416 (buttons (widget-get widget :buttons))
2417 (button-args (or (widget-get type :sibling-args)
2418 (widget-get widget :button-args)))
2420 (chosen (and (null (widget-get widget :choice))
2421 (widget-apply type :match value)))
2423 (insert (widget-get widget :entry-format))
2425 ;; Parse % escapes in format.
2426 (while (re-search-forward "%\\([bv%]\\)" nil t)
2427 (let ((escape (char-after (match-beginning 1))))
2428 (delete-backward-char 2)
2429 (cond ((eq escape ?%)
2432 (setq button (apply 'widget-create-child-and-convert
2433 widget 'radio-button
2434 :value (not (null chosen))
2437 (setq child (if chosen
2438 (widget-create-child-value
2440 (widget-create-child widget type)))
2442 (widget-apply child :deactivate)))
2444 (error "Unknown escape `%c'" escape)))))
2445 ;; Update properties.
2447 (widget-put widget :choice type))
2449 (widget-put child :button button)
2450 (widget-put widget :buttons (nconc buttons (list button))))
2452 (widget-put widget :children (nconc children (list child))))
2455 (defun widget-radio-value-get (widget)
2456 ;; Get value of the child widget.
2457 (let ((chosen (widget-radio-chosen widget)))
2458 (and chosen (widget-value chosen))))
2460 (defun widget-radio-chosen (widget)
2461 "Return the widget representing the chosen radio button."
2462 (let ((children (widget-get widget :children))
2465 (setq current (car children)
2466 children (cdr children))
2467 (when (widget-apply (widget-get current :button) :value-get)
2472 (defun widget-radio-value-inline (widget)
2473 ;; Get value of the child widget.
2474 (let ((children (widget-get widget :children))
2477 (setq current (car children)
2478 children (cdr children))
2479 (when (widget-apply (widget-get current :button) :value-get)
2480 (setq found (widget-apply current :value-inline)
2484 (defun widget-radio-value-set (widget value)
2485 ;; We can't just delete and recreate a radio widget, since children
2486 ;; can be added after the original creation and won't be recreated
2488 (let ((children (widget-get widget :children))
2491 (setq current (car children)
2492 children (cdr children))
2493 (let* ((button (widget-get current :button))
2494 (match (and (not found)
2495 (widget-apply current :match value))))
2496 (widget-value-set button match)
2499 (widget-value-set current value)
2500 (widget-apply current :activate))
2501 (widget-apply current :deactivate))
2502 (setq found (or found match))))))
2504 (defun widget-radio-validate (widget)
2505 ;; Valid if we have made a valid choice.
2506 (let ((children (widget-get widget :children))
2507 current found button)
2508 (while (and children (not found))
2509 (setq current (car children)
2510 children (cdr children)
2511 button (widget-get current :button)
2512 found (widget-apply button :value-get)))
2514 (widget-apply current :validate)
2517 (defun widget-radio-action (widget child event)
2518 ;; Check if a radio button was pressed.
2519 (let ((children (widget-get widget :children))
2520 (buttons (widget-get widget :buttons))
2522 (when (memq child buttons)
2524 (setq current (car children)
2525 children (cdr children))
2526 (let* ((button (widget-get current :button)))
2527 (cond ((eq child button)
2528 (widget-value-set button t)
2529 (widget-apply current :activate))
2530 ((widget-value button)
2531 (widget-value-set button nil)
2532 (widget-apply current :deactivate)))))))
2533 ;; Pass notification to parent.
2534 (widget-apply widget :notify child event))
2536 ;;; The `insert-button' Widget.
2538 (define-widget 'insert-button 'push-button
2539 "An insert button for the `editable-list' widget."
2541 :help-echo "Insert a new item into the list at this position."
2542 :action 'widget-insert-button-action)
2544 (defun widget-insert-button-action (widget &optional event)
2545 ;; Ask the parent to insert a new item.
2546 (widget-apply (widget-get widget :parent)
2547 :insert-before (widget-get widget :widget)))
2549 ;;; The `delete-button' Widget.
2551 (define-widget 'delete-button 'push-button
2552 "A delete button for the `editable-list' widget."
2554 :help-echo "Delete this item from the list."
2555 :action 'widget-delete-button-action)
2557 (defun widget-delete-button-action (widget &optional event)
2558 ;; Ask the parent to insert a new item.
2559 (widget-apply (widget-get widget :parent)
2560 :delete-at (widget-get widget :widget)))
2562 ;;; The `editable-list' Widget.
2564 ;; (defcustom widget-editable-list-gui nil
2565 ;; "If non nil, use GUI push-buttons in editable list when available."
2569 (define-widget 'editable-list 'default
2570 "A variable list of widgets of the same type."
2571 :convert-widget 'widget-types-convert-widget
2572 :copy 'widget-types-copy
2575 :format-handler 'widget-editable-list-format-handler
2576 :entry-format "%i %d %v"
2577 :value-create 'widget-editable-list-value-create
2578 :value-get 'widget-editable-list-value-get
2579 :validate 'widget-children-validate
2580 :match 'widget-editable-list-match
2581 :match-inline 'widget-editable-list-match-inline
2582 :insert-before 'widget-editable-list-insert-before
2583 :delete-at 'widget-editable-list-delete-at)
2585 (defun widget-editable-list-format-handler (widget escape)
2586 ;; We recognize the insert button.
2587 ;; (let ((widget-push-button-gui widget-editable-list-gui))
2588 (cond ((eq escape ?i)
2589 (and (widget-get widget :indent)
2590 (insert-char ?\s (widget-get widget :indent)))
2591 (apply 'widget-create-child-and-convert
2592 widget 'insert-button
2593 (widget-get widget :append-button-args)))
2595 (widget-default-format-handler widget escape)))
2599 (defun widget-editable-list-value-create (widget)
2600 ;; Insert all values
2601 (let* ((value (widget-get widget :value))
2602 (type (nth 0 (widget-get widget :args)))
2604 (widget-put widget :value-pos (copy-marker (point)))
2605 (set-marker-insertion-type (widget-get widget :value-pos) t)
2607 (let ((answer (widget-match-inline type value)))
2609 (setq children (cons (widget-editable-list-entry-create
2611 (if (widget-get type :inline)
2618 (widget-put widget :children (nreverse children))))
2620 (defun widget-editable-list-value-get (widget)
2621 ;; Get value of the child widget.
2622 (apply 'append (mapcar (lambda (child) (widget-apply child :value-inline))
2623 (widget-get widget :children))))
2625 (defun widget-editable-list-match (widget value)
2626 ;; Value must be a list and all the members must match the type.
2628 (null (cdr (widget-editable-list-match-inline widget value)))))
2630 (defun widget-editable-list-match-inline (widget value)
2631 (let ((type (nth 0 (widget-get widget :args)))
2634 (while (and value ok)
2635 (let ((answer (widget-match-inline type value)))
2637 (setq found (append found (car answer))
2640 (cons found value)))
2642 (defun widget-editable-list-insert-before (widget before)
2643 ;; Insert a new child in the list of children.
2645 (let ((children (widget-get widget :children))
2646 (inhibit-read-only t)
2647 before-change-functions
2648 after-change-functions)
2650 (goto-char (widget-get before :entry-from)))
2652 (goto-char (widget-get widget :value-pos))))
2653 (let ((child (widget-editable-list-entry-create
2655 (when (< (widget-get child :entry-from) (widget-get widget :from))
2656 (set-marker (widget-get widget :from)
2657 (widget-get child :entry-from)))
2658 (if (eq (car children) before)
2659 (widget-put widget :children (cons child children))
2660 (while (not (eq (car (cdr children)) before))
2661 (setq children (cdr children)))
2662 (setcdr children (cons child (cdr children)))))))
2664 (widget-apply widget :notify widget))
2666 (defun widget-editable-list-delete-at (widget child)
2667 ;; Delete child from list of children.
2669 (let ((buttons (copy-sequence (widget-get widget :buttons)))
2671 (inhibit-read-only t)
2672 before-change-functions
2673 after-change-functions)
2675 (setq button (car buttons)
2676 buttons (cdr buttons))
2677 (when (eq (widget-get button :widget) child)
2679 :buttons (delq button (widget-get widget :buttons)))
2680 (widget-delete button))))
2681 (let ((entry-from (widget-get child :entry-from))
2682 (entry-to (widget-get child :entry-to))
2683 (inhibit-read-only t)
2684 before-change-functions
2685 after-change-functions)
2686 (widget-delete child)
2687 (delete-region entry-from entry-to)
2688 (set-marker entry-from nil)
2689 (set-marker entry-to nil))
2690 (widget-put widget :children (delq child (widget-get widget :children))))
2692 (widget-apply widget :notify widget))
2694 (defun widget-editable-list-entry-create (widget value conv)
2695 ;; Create a new entry to the list.
2696 (let ((type (nth 0 (widget-get widget :args)))
2697 ;; (widget-push-button-gui widget-editable-list-gui)
2698 child delete insert)
2699 (widget-specify-insert
2701 (and (widget-get widget :indent)
2702 (insert-char ?\s (widget-get widget :indent)))
2703 (insert (widget-get widget :entry-format)))
2704 ;; Parse % escapes in format.
2705 (while (re-search-forward "%\\(.\\)" nil t)
2706 (let ((escape (char-after (match-beginning 1))))
2707 (delete-backward-char 2)
2708 (cond ((eq escape ?%)
2711 (setq insert (apply 'widget-create-child-and-convert
2712 widget 'insert-button
2713 (widget-get widget :insert-button-args))))
2715 (setq delete (apply 'widget-create-child-and-convert
2716 widget 'delete-button
2717 (widget-get widget :delete-button-args))))
2720 (setq child (widget-create-child-value
2722 (setq child (widget-create-child-value
2723 widget type (widget-default-get type)))))
2725 (error "Unknown escape `%c'" escape)))))
2726 (let ((buttons (widget-get widget :buttons)))
2727 (if insert (push insert buttons))
2728 (if delete (push delete buttons))
2729 (widget-put widget :buttons buttons))
2730 (let ((entry-from (point-min-marker))
2731 (entry-to (point-max-marker)))
2732 (set-marker-insertion-type entry-from t)
2733 (set-marker-insertion-type entry-to nil)
2734 (widget-put child :entry-from entry-from)
2735 (widget-put child :entry-to entry-to)))
2736 (if insert (widget-put insert :widget child))
2737 (if delete (widget-put delete :widget child))
2740 ;;; The `group' Widget.
2742 (define-widget 'group 'default
2743 "A widget which groups other widgets inside."
2744 :convert-widget 'widget-types-convert-widget
2745 :copy 'widget-types-copy
2747 :value-create 'widget-group-value-create
2748 :value-get 'widget-editable-list-value-get
2749 :default-get 'widget-group-default-get
2750 :validate 'widget-children-validate
2751 :match 'widget-group-match
2752 :match-inline 'widget-group-match-inline)
2754 (defun widget-group-value-create (widget)
2755 ;; Create each component.
2756 (let ((args (widget-get widget :args))
2757 (value (widget-get widget :value))
2758 arg answer children)
2760 (setq arg (car args)
2762 answer (widget-match-inline arg value)
2764 (and (eq (preceding-char) ?\n)
2765 (widget-get widget :indent)
2766 (insert-char ?\s (widget-get widget :indent)))
2767 (push (cond ((null answer)
2768 (widget-create-child widget arg))
2769 ((widget-get arg :inline)
2770 (widget-create-child-value widget arg (car answer)))
2772 (widget-create-child-value widget arg (car (car answer)))))
2774 (widget-put widget :children (nreverse children))))
2776 (defun widget-group-default-get (widget)
2777 ;; Get the default of the components.
2778 (mapcar 'widget-default-get (widget-get widget :args)))
2780 (defun widget-group-match (widget values)
2781 ;; Match if the components match.
2783 (let ((match (widget-group-match-inline widget values)))
2784 (and match (null (cdr match))))))
2786 (defun widget-group-match-inline (widget vals)
2787 ;; Match if the components match.
2788 (let ((args (widget-get widget :args))
2789 argument answer found)
2791 (setq argument (car args)
2793 answer (widget-match-inline argument vals))
2795 (setq vals (cdr answer)
2796 found (append found (car answer)))
2800 (cons found vals))))
2802 ;;; The `visibility' Widget.
2804 (define-widget 'visibility 'item
2805 "An indicator and manipulator for hidden items."
2811 :value-create 'widget-visibility-value-create
2812 :action 'widget-toggle-action
2813 :match (lambda (widget value) t))
2815 (defun widget-visibility-value-create (widget)
2816 ;; Insert text representing the `on' and `off' states.
2817 (let ((on (widget-get widget :on))
2818 (off (widget-get widget :off)))
2820 (setq on (concat widget-push-button-prefix
2822 widget-push-button-suffix))
2825 (setq off (concat widget-push-button-prefix
2827 widget-push-button-suffix))
2829 (if (widget-value widget)
2830 (widget-image-insert widget on "down" "down-pushed")
2831 (widget-image-insert widget off "right" "right-pushed"))))
2833 ;;; The `documentation-link' Widget.
2835 ;; This is a helper widget for `documentation-string'.
2837 (define-widget 'documentation-link 'link
2838 "Link type used in documentation strings."
2840 :help-echo "Describe this symbol"
2841 :action 'widget-documentation-link-action)
2843 (defun widget-documentation-link-action (widget &optional event)
2844 "Display documentation for WIDGET's value. Ignore optional argument EVENT."
2845 (let* ((string (widget-get widget :value))
2846 (symbol (intern string)))
2847 (if (and (fboundp symbol) (boundp symbol))
2848 ;; If there are two doc strings, give the user a way to pick one.
2849 (apropos (concat "\\`" (regexp-quote string) "\\'"))
2850 (if (fboundp symbol)
2851 (describe-function symbol)
2852 (describe-variable symbol)))))
2854 (defcustom widget-documentation-links t
2855 "Add hyperlinks to documentation strings when non-nil."
2857 :group 'widget-documentation)
2859 (defcustom widget-documentation-link-regexp "`\\([^\n`' ]+\\)'"
2860 "Regexp for matching potential links in documentation strings.
2861 The first group should be the link itself."
2863 :group 'widget-documentation)
2865 (defcustom widget-documentation-link-p 'intern-soft
2866 "Predicate used to test if a string is useful as a link.
2867 The value should be a function. The function will be called with one
2868 argument, a string, and should return non-nil if there should be a
2869 link for that string."
2871 :options '(widget-documentation-link-p)
2872 :group 'widget-documentation)
2874 (defcustom widget-documentation-link-type 'documentation-link
2875 "Widget type used for links in documentation strings."
2877 :group 'widget-documentation)
2879 (defun widget-documentation-link-add (widget from to)
2880 (widget-specify-doc widget from to)
2881 (when widget-documentation-links
2882 (let ((regexp widget-documentation-link-regexp)
2883 (buttons (widget-get widget :buttons))
2884 (widget-mouse-face (default-value 'widget-mouse-face))
2885 (widget-button-face widget-documentation-face)
2886 (widget-button-pressed-face widget-documentation-face))
2889 (while (re-search-forward regexp to t)
2890 (let ((name (match-string 1))
2891 (begin (match-beginning 1))
2892 (end (match-end 1)))
2893 (when (funcall widget-documentation-link-p name)
2894 (push (widget-convert-button widget-documentation-link-type
2895 begin end :value name)
2897 (widget-put widget :buttons buttons)))
2898 (let ((indent (widget-get widget :indent)))
2899 (when (and indent (not (zerop indent)))
2902 (narrow-to-region from to)
2903 (goto-char (point-min))
2904 (while (search-forward "\n" nil t)
2905 (insert-char ?\s indent)))))))
2907 ;;; The `documentation-string' Widget.
2909 (define-widget 'documentation-string 'item
2910 "A documentation string."
2912 :action 'widget-documentation-string-action
2913 :value-create 'widget-documentation-string-value-create)
2915 (defun widget-documentation-string-value-create (widget)
2916 ;; Insert documentation string.
2917 (let ((doc (widget-value widget))
2918 (indent (widget-get widget :indent))
2919 (shown (widget-get (widget-get widget :parent) :documentation-shown))
2921 (if (string-match "\n" doc)
2922 (let ((before (substring doc 0 (match-beginning 0)))
2923 (after (substring doc (match-beginning 0)))
2926 (widget-documentation-link-add widget start (point))
2928 (widget-create-child-and-convert
2930 :help-echo "Show or hide rest of the documentation."
2934 :action 'widget-parent-action
2937 (setq start (point))
2938 (when (and indent (not (zerop indent)))
2939 (insert-char ?\s indent))
2941 (widget-documentation-link-add widget start (point)))
2942 (widget-put widget :buttons (list button)))
2944 (widget-documentation-link-add widget start (point))))
2947 (defun widget-documentation-string-action (widget &rest ignore)
2948 ;; Toggle documentation.
2949 (let ((parent (widget-get widget :parent)))
2950 (widget-put parent :documentation-shown
2951 (not (widget-get parent :documentation-shown))))
2953 (widget-value-set widget (widget-value widget)))
2955 ;;; The Sexp Widgets.
2957 (define-widget 'const 'item
2958 "An immutable sexp."
2959 :prompt-value 'widget-const-prompt-value
2962 (defun widget-const-prompt-value (widget prompt value unbound)
2963 ;; Return the value of the const.
2964 (widget-value widget))
2966 (define-widget 'function-item 'const
2967 "An immutable function name."
2969 :documentation-property (lambda (symbol)
2971 (documentation symbol t)
2974 (define-widget 'variable-item 'const
2975 "An immutable variable name."
2977 :documentation-property 'variable-documentation)
2979 (define-widget 'other 'sexp
2980 "Matches any value, but doesn't let the user edit the value.
2981 This is useful as last item in a `choice' widget.
2982 You should use this widget type with a default value,
2983 as in (other DEFAULT) or (other :tag \"NAME\" DEFAULT).
2984 If the user selects this alternative, that specifies DEFAULT
2990 (defvar widget-string-prompt-value-history nil
2991 "History of input to `widget-string-prompt-value'.")
2993 (define-widget 'string 'editable-field
2996 :format "%{%t%}: %v"
2997 :complete-function 'ispell-complete-word
2998 :prompt-history 'widget-string-prompt-value-history)
3000 (define-widget 'regexp 'string
3001 "A regular expression."
3002 :match 'widget-regexp-match
3003 :validate 'widget-regexp-validate
3004 ;; Doesn't work well with terminating newline.
3005 ;; :value-face 'widget-single-line-field
3008 (defun widget-regexp-match (widget value)
3009 ;; Match valid regexps.
3010 (and (stringp value)
3013 (string-match value ""))
3016 (defun widget-regexp-validate (widget)
3017 "Check that the value of WIDGET is a valid regexp."
3018 (condition-case data
3020 (string-match (widget-value widget) ""))
3021 (error (widget-put widget :error (error-message-string data))
3024 (define-widget 'file 'string
3026 It reads a file name from an editable text field."
3027 :complete-function 'widget-file-complete
3028 :prompt-value 'widget-file-prompt-value
3029 :format "%{%t%}: %v"
3030 ;; Doesn't work well with terminating newline.
3031 ;; :value-face 'widget-single-line-field
3034 (defun widget-file-complete ()
3035 "Perform completion on file name preceding point."
3037 (let* ((end (point))
3038 (beg (widget-field-start widget))
3039 (pattern (buffer-substring beg end))
3040 (name-part (file-name-nondirectory pattern))
3041 ;; I think defaulting to root is right
3042 ;; because these really should be absolute file names.
3043 (directory (or (file-name-directory pattern) "/"))
3044 (completion (file-name-completion name-part directory)))
3045 (cond ((eq completion t))
3047 (message "Can't find completion for \"%s\"" pattern)
3049 ((not (string= name-part completion))
3050 (delete-region beg end)
3051 (insert (expand-file-name completion directory)))
3053 (message "Making completion list...")
3054 (with-output-to-temp-buffer "*Completions*"
3055 (display-completion-list
3056 (sort (file-name-all-completions name-part directory)
3059 (message "Making completion list...%s" "done")))))
3061 (defun widget-file-prompt-value (widget prompt value unbound)
3062 ;; Read file from minibuffer.
3063 (abbreviate-file-name
3065 (read-file-name prompt)
3066 (let ((prompt2 (format "%s (default %s): " prompt value))
3067 (dir (file-name-directory value))
3068 (file (file-name-nondirectory value))
3069 (must-match (widget-get widget :must-match)))
3070 (read-file-name prompt2 dir nil must-match file)))))
3072 ;;;(defun widget-file-action (widget &optional event)
3073 ;;; ;; Read a file name from the minibuffer.
3074 ;;; (let* ((value (widget-value widget))
3075 ;;; (dir (file-name-directory value))
3076 ;;; (file (file-name-nondirectory value))
3077 ;;; (menu-tag (widget-apply widget :menu-tag-get))
3078 ;;; (must-match (widget-get widget :must-match))
3079 ;;; (answer (read-file-name (concat menu-tag " (default " value "): ")
3080 ;;; dir nil must-match file)))
3081 ;;; (widget-value-set widget (abbreviate-file-name answer))
3083 ;;; (widget-apply widget :notify widget event)))
3085 ;; Fixme: use file-name-as-directory.
3086 (define-widget 'directory 'file
3087 "A directory widget.
3088 It reads a directory name from an editable text field."
3091 (defvar widget-symbol-prompt-value-history nil
3092 "History of input to `widget-symbol-prompt-value'.")
3094 (define-widget 'symbol 'editable-field
3098 :format "%{%t%}: %v"
3099 :match (lambda (widget value) (symbolp value))
3100 :complete-function 'lisp-complete-symbol
3101 :prompt-internal 'widget-symbol-prompt-internal
3102 :prompt-match 'symbolp
3103 :prompt-history 'widget-symbol-prompt-value-history
3104 :value-to-internal (lambda (widget value)
3108 :value-to-external (lambda (widget value)
3113 (defun widget-symbol-prompt-internal (widget prompt initial history)
3114 ;; Read file from minibuffer.
3115 (let ((answer (completing-read prompt obarray
3116 (widget-get widget :prompt-match)
3117 nil initial history)))
3118 (if (and (stringp answer)
3119 (not (zerop (length answer))))
3121 (error "No value"))))
3123 (defvar widget-function-prompt-value-history nil
3124 "History of input to `widget-function-prompt-value'.")
3126 (define-widget 'function 'restricted-sexp
3128 :complete-function (lambda ()
3130 (lisp-complete-symbol 'fboundp))
3131 :prompt-value 'widget-field-prompt-value
3132 :prompt-internal 'widget-symbol-prompt-internal
3133 :prompt-match 'fboundp
3134 :prompt-history 'widget-function-prompt-value-history
3135 :action 'widget-field-action
3136 :match-alternatives '(functionp)
3137 :validate (lambda (widget)
3138 (unless (functionp (widget-value widget))
3139 (widget-put widget :error (format "Invalid function: %S"
3140 (widget-value widget)))
3145 (defvar widget-variable-prompt-value-history nil
3146 "History of input to `widget-variable-prompt-value'.")
3148 (define-widget 'variable 'symbol
3150 :prompt-match 'boundp
3151 :prompt-history 'widget-variable-prompt-value-history
3152 :complete-function (lambda ()
3154 (lisp-complete-symbol 'boundp))
3157 (defvar widget-coding-system-prompt-value-history nil
3158 "History of input to `widget-coding-system-prompt-value'.")
3160 (define-widget 'coding-system 'symbol
3161 "A MULE coding-system."
3162 :format "%{%t%}: %v"
3163 :tag "Coding system"
3165 :prompt-history 'widget-coding-system-prompt-value-history
3166 :prompt-value 'widget-coding-system-prompt-value
3167 :action 'widget-coding-system-action
3168 :complete-function (lambda ()
3170 (lisp-complete-symbol 'coding-system-p))
3171 :validate (lambda (widget)
3172 (unless (coding-system-p (widget-value widget))
3173 (widget-put widget :error (format "Invalid coding system: %S"
3174 (widget-value widget)))
3177 :prompt-match 'coding-system-p)
3179 (defun widget-coding-system-prompt-value (widget prompt value unbound)
3180 "Read coding-system from minibuffer."
3181 (if (widget-get widget :base-only)
3183 (completing-read (format "%s (default %s): " prompt value)
3184 (mapcar #'list (coding-system-list t)) nil nil nil
3185 coding-system-history))
3186 (read-coding-system (format "%s (default %s): " prompt value) value)))
3188 (defun widget-coding-system-action (widget &optional event)
3190 (widget-coding-system-prompt-value
3192 (widget-apply widget :menu-tag-get)
3193 (widget-value widget)
3195 (widget-value-set widget answer)
3196 (widget-apply widget :notify widget event)
3199 ;;; I'm not sure about what this is good for? KFS.
3200 (defvar widget-key-sequence-prompt-value-history nil
3201 "History of input to `widget-key-sequence-prompt-value'.")
3203 (defvar widget-key-sequence-default-value [ignore]
3204 "Default value for an empty key sequence.")
3206 (defvar widget-key-sequence-map
3207 (let ((map (make-sparse-keymap)))
3208 (set-keymap-parent map widget-field-keymap)
3209 (define-key map [(control ?q)] 'widget-key-sequence-read-event)
3212 (define-widget 'key-sequence 'restricted-sexp
3214 :prompt-value 'widget-field-prompt-value
3215 :prompt-internal 'widget-symbol-prompt-internal
3216 ; :prompt-match 'fboundp ;; What was this good for? KFS
3217 :prompt-history 'widget-key-sequence-prompt-value-history
3218 :action 'widget-field-action
3219 :match-alternatives '(stringp vectorp)
3220 :format "%{%t%}: %v"
3221 :validate 'widget-key-sequence-validate
3222 :value-to-internal 'widget-key-sequence-value-to-internal
3223 :value-to-external 'widget-key-sequence-value-to-external
3224 :value widget-key-sequence-default-value
3225 :keymap widget-key-sequence-map
3226 :help-echo "C-q: insert KEY, EVENT, or CODE; RET: enter value"
3227 :tag "Key sequence")
3229 (defun widget-key-sequence-read-event (ev)
3231 (let ((inhibit-quit t) quit-flag)
3232 (read-event "Insert KEY, EVENT, or CODE: "))))
3233 (let ((ev2 (and (memq 'down (event-modifiers ev))
3235 (tr (and (keymapp function-key-map)
3236 (lookup-key function-key-map (vector ev)))))
3237 (when (and (integerp ev)
3238 (or (and (<= ?0 ev) (< ev (+ ?0 (min 10 read-quoted-char-radix))))
3239 (and (<= ?a (downcase ev))
3240 (< (downcase ev) (+ ?a -10 (min 36 read-quoted-char-radix))))))
3241 (setq unread-command-events (cons ev unread-command-events)
3242 ev (read-quoted-char (format "Enter code (radix %d)" read-quoted-char-radix))
3244 (if (and (integerp ev) (not (char-valid-p ev)))
3245 (insert (char-to-string ev)))) ;; throw invalid char error
3246 (setq ev (key-description (list ev)))
3248 (setq tr (key-description (list (aref tr 0))))
3249 (if (y-or-n-p (format "Key %s is translated to %s -- use %s? " ev tr tr))
3250 (setq ev tr ev2 nil)))
3251 (insert (if (= (char-before) ?\s) "" " ") ev " ")
3253 (insert (key-description (list ev2)) " "))))
3255 (defun widget-key-sequence-validate (widget)
3256 (unless (or (stringp (widget-value widget))
3257 (vectorp (widget-value widget)))
3258 (widget-put widget :error (format "Invalid key sequence: %S"
3259 (widget-value widget)))
3262 (defun widget-key-sequence-value-to-internal (widget value)
3263 (if (widget-apply widget :match value)
3264 (if (equal value widget-key-sequence-default-value)
3266 (key-description value))
3269 (defun widget-key-sequence-value-to-external (widget value)
3271 (if (string-match "\\`[[:space:]]*\\'" value)
3272 widget-key-sequence-default-value
3273 (read-kbd-macro value))
3277 (define-widget 'sexp 'editable-field
3278 "An arbitrary Lisp expression."
3279 :tag "Lisp expression"
3280 :format "%{%t%}: %v"
3282 :validate 'widget-sexp-validate
3283 :match (lambda (widget value) t)
3284 :value-to-internal 'widget-sexp-value-to-internal
3285 :value-to-external (lambda (widget value) (read value))
3286 :prompt-history 'widget-sexp-prompt-value-history
3287 :prompt-value 'widget-sexp-prompt-value)
3289 (defun widget-sexp-value-to-internal (widget value)
3290 ;; Use pp for printer representation.
3291 (let ((pp (if (symbolp value)
3292 (prin1-to-string value)
3293 (pp-to-string value))))
3294 (while (string-match "\n\\'" pp)
3295 (setq pp (substring pp 0 -1)))
3296 (if (or (string-match "\n\\'" pp)
3301 (defun widget-sexp-validate (widget)
3302 ;; Valid if we can read the string and there is no junk left after it.
3304 (insert (widget-apply widget :value-get))
3305 (goto-char (point-min))
3307 (condition-case data
3309 ;; Avoid a confusing end-of-file error.
3310 (skip-syntax-forward "\\s-")
3312 (setq err "Empty sexp -- use `nil'?")
3313 (unless (widget-apply widget :match (read (current-buffer)))
3314 (setq err (widget-get widget :type-error))))
3315 ;; Allow whitespace after expression.
3316 (skip-syntax-forward "\\s-")
3317 (if (and (not (eobp))
3319 (setq err (format "Junk at end of expression: %s"
3320 (buffer-substring (point)
3322 (end-of-file ; Avoid confusing error message.
3323 (setq err "Unbalanced sexp"))
3324 (error (setq err (error-message-string data))))
3327 (widget-put widget :error err)
3330 (defvar widget-sexp-prompt-value-history nil
3331 "History of input to `widget-sexp-prompt-value'.")
3333 (defun widget-sexp-prompt-value (widget prompt value unbound)
3334 ;; Read an arbitrary sexp.
3335 (let ((found (read-string prompt
3336 (if unbound nil (cons (prin1-to-string value) 0))
3337 (widget-get widget :prompt-history))))
3338 (let ((answer (read-from-string found)))
3339 (unless (= (cdr answer) (length found))
3340 (error "Junk at end of expression: %s"
3341 (substring found (cdr answer))))
3344 (define-widget 'restricted-sexp 'sexp
3345 "A Lisp expression restricted to values that match.
3346 To use this type, you must define :match or :match-alternatives."
3347 :type-error "The specified value is not valid"
3348 :match 'widget-restricted-sexp-match
3349 :value-to-internal (lambda (widget value)
3350 (if (widget-apply widget :match value)
3351 (prin1-to-string value)
3354 (defun widget-restricted-sexp-match (widget value)
3355 (let ((alternatives (widget-get widget :match-alternatives))
3357 (while (and alternatives (not matched))
3358 (if (cond ((functionp (car alternatives))
3359 (funcall (car alternatives) value))
3360 ((and (consp (car alternatives))
3361 (eq (car (car alternatives)) 'quote))
3362 (eq value (nth 1 (car alternatives)))))
3364 (setq alternatives (cdr alternatives)))
3367 (define-widget 'integer 'restricted-sexp
3371 :type-error "This field should contain an integer"
3372 :match-alternatives '(integerp))
3374 (define-widget 'number 'restricted-sexp
3375 "A number (floating point or integer)."
3378 :type-error "This field should contain a number (floating point or integer)"
3379 :match-alternatives '(numberp))
3381 (define-widget 'float 'restricted-sexp
3382 "A floating point number."
3383 :tag "Floating point number"
3385 :type-error "This field should contain a floating point number"
3386 :match-alternatives '(floatp))
3388 (define-widget 'character 'editable-field
3393 :format "%{%t%}: %v\n"
3394 :valid-regexp "\\`.\\'"
3395 :error "This field should contain a single character"
3396 :value-to-internal (lambda (widget value)
3399 (char-to-string value)))
3400 :value-to-external (lambda (widget value)
3404 :match (lambda (widget value)
3405 (char-valid-p value)))
3407 (define-widget 'list 'group
3410 :format "%{%t%}:\n%v")
3412 (define-widget 'vector 'group
3415 :format "%{%t%}:\n%v"
3416 :match 'widget-vector-match
3417 :value-to-internal (lambda (widget value) (append value nil))
3418 :value-to-external (lambda (widget value) (apply 'vector value)))
3420 (defun widget-vector-match (widget value)
3421 (and (vectorp value)
3422 (widget-group-match widget
3423 (widget-apply widget :value-to-internal value))))
3425 (define-widget 'cons 'group
3428 :format "%{%t%}:\n%v"
3429 :match 'widget-cons-match
3430 :value-to-internal (lambda (widget value)
3431 (list (car value) (cdr value)))
3432 :value-to-external (lambda (widget value)
3433 (apply 'cons value)))
3435 (defun widget-cons-match (widget value)
3437 (widget-group-match widget
3438 (widget-apply widget :value-to-internal value))))
3440 ;;; The `lazy' Widget.
3442 ;; Recursive datatypes.
3444 (define-widget 'lazy 'default
3445 "Base widget for recursive datastructures.
3447 The `lazy' widget will, when instantiated, contain a single inferior
3448 widget, of the widget type specified by the :type parameter. The
3449 value of the `lazy' widget is the same as the value of the inferior
3450 widget. When deriving a new widget from the 'lazy' widget, the :type
3451 parameter is allowed to refer to the widget currently being defined,
3452 thus allowing recursive datastructures to be described.
3454 The :type parameter takes the same arguments as the defcustom
3455 parameter with the same name.
3457 Most composite widgets, i.e. widgets containing other widgets, does
3458 not allow recursion. That is, when you define a new widget type, none
3459 of the inferior widgets may be of the same type you are currently
3462 In Lisp, however, it is custom to define datastructures in terms of
3463 themselves. A list, for example, is defined as either nil, or a cons
3464 cell whose cdr itself is a list. The obvious way to translate this
3465 into a widget type would be
3467 (define-widget 'my-list 'choice
3468 \"A list of sexps.\"
3470 :args '((const nil) (cons :value (nil) sexp my-list)))
3472 Here we attempt to define my-list as a choice of either the constant
3473 nil, or a cons-cell containing a sexp and my-lisp. This will not work
3474 because the `choice' widget does not allow recursion.
3476 Using the `lazy' widget you can overcome this problem, as in this
3479 (define-widget 'sexp-list 'lazy
3480 \"A list of sexps.\"
3482 :type '(choice (const nil) (cons :value (nil) sexp sexp-list)))"
3483 :format "%{%t%}: %v"
3484 ;; We don't convert :type because we want to allow recursive
3485 ;; datastructures. This is slow, so we should not create speed
3486 ;; critical widgets by deriving from this.
3487 :convert-widget 'widget-value-convert-widget
3488 :value-create 'widget-type-value-create
3489 :value-get 'widget-child-value-get
3490 :value-inline 'widget-child-value-inline
3491 :default-get 'widget-type-default-get
3492 :match 'widget-type-match
3493 :validate 'widget-child-validate)
3496 ;;; The `plist' Widget.
3500 (define-widget 'plist 'list
3502 :key-type '(symbol :tag "Key")
3503 :value-type '(sexp :tag "Value")
3504 :convert-widget 'widget-plist-convert-widget
3507 (defvar widget-plist-value-type) ;Dynamic variable
3509 (defun widget-plist-convert-widget (widget)
3510 ;; Handle `:options'.
3511 (let* ((options (widget-get widget :options))
3512 (widget-plist-value-type (widget-get widget :value-type))
3513 (other `(editable-list :inline t
3515 ,(widget-get widget :key-type)
3516 ,widget-plist-value-type)))
3518 (list `(checklist :inline t
3520 ,@(mapcar 'widget-plist-convert-option
3524 (widget-put widget :args args)
3527 (defun widget-plist-convert-option (option)
3528 ;; Convert a single plist option.
3529 (let (key-type value-type)
3531 (let ((key (nth 0 option)))
3532 (setq value-type (nth 1 option))
3535 (setq key-type `(const ,key))))
3536 (setq key-type `(const ,option)
3537 value-type widget-plist-value-type))
3538 `(group :format "Key: %v" :inline t ,key-type ,value-type)))
3541 ;;; The `alist' Widget.
3543 ;; Association lists.
3545 (define-widget 'alist 'list
3546 "An association list."
3547 :key-type '(sexp :tag "Key")
3548 :value-type '(sexp :tag "Value")
3549 :convert-widget 'widget-alist-convert-widget
3552 (defvar widget-alist-value-type) ;Dynamic variable
3554 (defun widget-alist-convert-widget (widget)
3555 ;; Handle `:options'.
3556 (let* ((options (widget-get widget :options))
3557 (widget-alist-value-type (widget-get widget :value-type))
3558 (other `(editable-list :inline t
3560 ,(widget-get widget :key-type)
3561 ,widget-alist-value-type)))
3563 (list `(checklist :inline t
3565 ,@(mapcar 'widget-alist-convert-option
3569 (widget-put widget :args args)
3572 (defun widget-alist-convert-option (option)
3573 ;; Convert a single alist option.
3574 (let (key-type value-type)
3576 (let ((key (nth 0 option)))
3577 (setq value-type (nth 1 option))
3580 (setq key-type `(const ,key))))
3581 (setq key-type `(const ,option)
3582 value-type widget-alist-value-type))
3583 `(cons :format "Key: %v" ,key-type ,value-type)))
3585 (define-widget 'choice 'menu-choice
3586 "A union of several sexp types."
3588 :format "%{%t%}: %[Value Menu%] %v"
3589 :button-prefix 'widget-push-button-prefix
3590 :button-suffix 'widget-push-button-suffix
3591 :prompt-value 'widget-choice-prompt-value)
3593 (defun widget-choice-prompt-value (widget prompt value unbound)
3595 (let ((args (widget-get widget :args))
3596 (completion-ignore-case (widget-get widget :case-fold))
3597 current choices old)
3598 ;; Find the first arg that matches VALUE.
3601 (if (widget-apply (car look) :match value)
3602 (setq old (car look)
3604 (setq look (cdr look)))))
3607 (cond ((= (length args) 0)
3609 ((= (length args) 1)
3611 ((and (= (length args) 2)
3613 (if (eq old (nth 0 args))
3618 (setq current (car args)
3621 (cons (cons (widget-apply current :menu-tag-get)
3624 (let ((val (completing-read prompt choices nil t)))
3626 (let ((try (try-completion val choices)))
3629 (cdr (assoc val choices)))
3632 (widget-prompt-value current prompt nil t)
3635 (define-widget 'radio 'radio-button-choice
3636 "A union of several sexp types."
3638 :format "%{%t%}:\n%v"
3639 :prompt-value 'widget-choice-prompt-value)
3641 (define-widget 'repeat 'editable-list
3642 "A variable length homogeneous list."
3644 :format "%{%t%}:\n%v%i\n")
3646 (define-widget 'set 'checklist
3647 "A list of members from a fixed set."
3649 :format "%{%t%}:\n%v")
3651 (define-widget 'boolean 'toggle
3652 "To be nil or non-nil, that is the question."
3654 :prompt-value 'widget-boolean-prompt-value
3655 :button-prefix 'widget-push-button-prefix
3656 :button-suffix 'widget-push-button-suffix
3657 :format "%{%t%}: %[Toggle%] %v\n"
3661 (defun widget-boolean-prompt-value (widget prompt value unbound)
3662 ;; Toggle a boolean.
3665 ;;; The `color' Widget.
3668 (define-widget 'color 'editable-field
3669 "Choose a color name (with sample)."
3670 :format "%{%t%}: %v (%{sample%})\n"
3674 :complete 'widget-color-complete
3675 :sample-face-get 'widget-color-sample-face-get
3676 :notify 'widget-color-notify
3677 :action 'widget-color-action)
3679 (defun widget-color-complete (widget)
3680 "Complete the color in WIDGET."
3681 (require 'facemenu) ; for facemenu-color-alist
3682 (let* ((prefix (buffer-substring-no-properties (widget-field-start widget)
3684 (list (or facemenu-color-alist (defined-colors)))
3685 (completion (try-completion prefix list)))
3686 (cond ((eq completion t)
3687 (message "Exact match."))
3689 (error "Can't find completion for \"%s\"" prefix))
3690 ((not (string-equal prefix completion))
3691 (insert-and-inherit (substring completion (length prefix))))
3693 (message "Making completion list...")
3694 (with-output-to-temp-buffer "*Completions*"
3695 (display-completion-list (all-completions prefix list nil)
3697 (message "Making completion list...done")))))
3699 (defun widget-color-sample-face-get (widget)
3700 (let* ((value (condition-case nil
3701 (widget-value widget)
3702 (error (widget-get widget :value)))))
3703 (if (color-defined-p value)
3704 (list (cons 'foreground-color value))
3707 (defun widget-color-action (widget &optional event)
3708 "Prompt for a color."
3709 (let* ((tag (widget-apply widget :menu-tag-get))
3710 (prompt (concat tag ": "))
3711 (value (widget-value widget))
3712 (start (widget-field-start widget))
3713 (answer (facemenu-read-color prompt)))
3714 (unless (zerop (length answer))
3715 (widget-value-set widget answer)
3717 (widget-apply widget :notify widget event))))
3719 (defun widget-color-notify (widget child &optional event)
3720 "Update the sample, and notify the parent."
3721 (overlay-put (widget-get widget :sample-overlay)
3722 'face (widget-apply widget :sample-face-get))
3723 (widget-default-notify widget child event))
3727 (defun widget-echo-help (pos)
3728 "Display help-echo text for widget at POS."
3729 (let* ((widget (widget-at pos))
3730 (help-echo (and widget (widget-get widget :help-echo))))
3731 (if (functionp help-echo)
3732 (setq help-echo (funcall help-echo widget)))
3733 (if help-echo (message "%s" (eval help-echo)))))
3739 ;;; arch-tag: a076e75e-18a1-4b46-8be5-3f317bcbc707
3740 ;;; wid-edit.el ends here