1 ;;; wid-edit.el --- Functions for creating and using widgets.
3 ;; Copyright (C) 1996, 1997, 1999 Free Software Foundation, Inc.
5 ;; Author: Per Abrahamsen <abraham@dina.kvl.dk>
7 ;; Keywords: extensions
9 ;; X-URL: http://www.dina.kvl.dk/~abraham/custom/ (probably obsolete)
11 ;; This file is part of GNU Emacs.
13 ;; GNU Emacs is free software; you can redistribute it and/or modify
14 ;; it under the terms of the GNU General Public License as published by
15 ;; the Free Software Foundation; either version 2, or (at your option)
18 ;; GNU Emacs is distributed in the hope that it will be useful,
19 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
20 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 ;; GNU General Public License for more details.
23 ;; You should have received a copy of the GNU General Public License
24 ;; along with GNU Emacs; see the file COPYING. If not, write to the
25 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
26 ;; Boston, MA 02111-1307, USA.
35 (eval-when-compile (require 'cl
))
39 (defun widget-event-point (event)
40 "Character position of the end of event if that exists, or nil."
41 (posn-point (event-end event
)))
43 (defalias 'widget-read-event
'read-event
)
46 (autoload 'pp-to-string
"pp")
47 (autoload 'Info-goto-node
"info")
48 (autoload 'finder-commentary
"finder" nil t
)
50 (unless (fboundp 'button-release-event-p
)
51 ;; XEmacs function missing from Emacs.
52 (defun button-release-event-p (event)
53 "Non-nil if EVENT is a mouse-button-release event object."
55 (memq (event-basic-type event
) '(mouse-1 mouse-2 mouse-3
))
56 (or (memq 'click
(event-modifiers event
))
57 (memq 'drag
(event-modifiers event
)))))))
62 "Customization support for the Widget Library."
63 :link
'(custom-manual "(widget)Top")
64 :link
'(url-link :tag
"Development Page"
65 "http://www.dina.kvl.dk/~abraham/custom/")
66 :link
'(emacs-library-link :tag
"Lisp File" "widget.el")
71 (defgroup widget-documentation nil
72 "Options controling the display of documentation strings."
75 (defgroup widget-faces nil
76 "Faces used by the widget library."
80 (defvar widget-documentation-face
'widget-documentation-face
81 "Face used for documentation strings in widgets.
82 This exists as a variable so it can be set locally in certain buffers.")
84 (defface widget-documentation-face
'((((class color
)
86 (:foreground
"lime green"))
89 (:foreground
"dark green"))
91 "Face used for documentation text."
92 :group
'widget-documentation
95 (defvar widget-button-face
'widget-button-face
96 "Face used for buttons in widgets.
97 This exists as a variable so it can be set locally in certain buffers.")
99 (defface widget-button-face
'((t (:bold t
)))
100 "Face used for widget buttons."
101 :group
'widget-faces
)
103 (defcustom widget-mouse-face
'highlight
104 "Face used for widget buttons when the mouse is above them."
106 :group
'widget-faces
)
108 (defface widget-field-face
'((((class grayscale color
)
110 (:background
"gray85"))
111 (((class grayscale color
)
113 (:background
"dim gray"))
116 "Face used for editable fields."
117 :group
'widget-faces
)
119 (defface widget-single-line-field-face
'((((class grayscale color
)
121 (:background
"gray85"))
122 (((class grayscale color
)
124 (:background
"dim gray"))
127 "Face used for editable fields spanning only a single line."
128 :group
'widget-faces
)
130 ;;; This causes display-table to be loaded, and not usefully.
131 ;;;(defvar widget-single-line-display-table
132 ;;; (let ((table (make-display-table)))
133 ;;; (aset table 9 "^I")
134 ;;; (aset table 10 "^J")
136 ;;; "Display table used for single-line editable fields.")
138 ;;;(when (fboundp 'set-face-display-table)
139 ;;; (set-face-display-table 'widget-single-line-field-face
140 ;;; widget-single-line-display-table))
142 ;;; Utility functions.
144 ;; These are not really widget specific.
146 (defun widget-princ-to-string (object)
147 ;; Return string representation of OBJECT, any Lisp object.
148 ;; No quoting characters are used; no delimiters are printed around
149 ;; the contents of strings.
151 (set-buffer (get-buffer-create " *widget-tmp*"))
153 (let ((standard-output (current-buffer)))
157 (defun widget-clear-undo ()
158 "Clear all undo information."
159 (buffer-disable-undo (current-buffer))
160 (buffer-enable-undo))
162 (defcustom widget-menu-max-size
40
163 "Largest number of items allowed in a popup-menu.
164 Larger menus are read through the minibuffer."
168 (defcustom widget-menu-max-shortcuts
40
169 "Largest number of items for which it works to choose one with a character.
170 For a larger number of items, the minibuffer is used."
174 (defcustom widget-menu-minibuffer-flag nil
175 "*Control how to ask for a choice from the keyboard.
176 Non-nil means use the minibuffer;
177 nil means read a single character."
181 (defun widget-choose (title items
&optional event
)
182 "Choose an item from a list.
184 First argument TITLE is the name of the list.
185 Second argument ITEMS is an list whose members are either
186 (NAME . VALUE), to indicate selectable items, or just strings to
187 indicate unselectable items.
188 Optional third argument EVENT is an input event.
190 The user is asked to choose between each NAME from the items alist,
191 and the VALUE of the chosen element will be returned. If EVENT is a
192 mouse event, and the number of elements in items is less than
193 `widget-menu-max-size', a popup menu will be used, otherwise the
195 (cond ((and (< (length items
) widget-menu-max-size
)
196 event
(fboundp 'x-popup-menu
) window-system
)
197 ;; We are in Emacs-19, pressed by the mouse
199 (list title
(cons "" items
))))
200 ((or widget-menu-minibuffer-flag
201 (> (length items
) widget-menu-max-shortcuts
))
202 ;; Read the choice of name from the minibuffer.
203 (setq items
(widget-remove-if 'stringp items
))
204 (let ((val (completing-read (concat title
": ") items nil t
)))
206 (let ((try (try-completion val items
)))
209 (cdr (assoc val items
)))
212 ;; Construct a menu of the choices
213 ;; and then use it for prompting for a single character.
214 (let* ((overriding-terminal-local-map
215 (make-sparse-keymap))
216 map choice
(next-digit ?
0)
219 ;; Define SPC as a prefix char to get to this menu.
220 (define-key overriding-terminal-local-map
" "
221 (setq map
(make-sparse-keymap title
)))
223 (set-buffer (get-buffer-create " widget-choose"))
225 (insert "Available choices:\n\n")
227 (setq choice
(car items
) items
(cdr items
))
229 (let* ((name (car choice
))
230 (function (cdr choice
)))
231 (insert (format "%c = %s\n" next-digit name
))
232 (define-key map
(vector next-digit
) function
)
233 (setq some-choice-enabled t
)))
234 ;; Allocate digits to disabled alternatives
235 ;; so that the digit of a given alternative never varies.
236 (setq next-digit
(1+ next-digit
)))
237 (insert "\nC-g = Quit"))
238 (or some-choice-enabled
239 (error "None of the choices is currently meaningful"))
240 (define-key map
[?\C-g
] 'keyboard-quit
)
241 (define-key map
[t] 'keyboard-quit)
242 (define-key map [?\M-\C-v] 'scroll-other-window)
243 (define-key map [?\M--] 'negative-argument)
244 (setcdr map (nreverse (cdr map)))
245 ;; Read a char with the menu, and return the result
246 ;; that corresponds to it.
247 (save-window-excursion
248 (let ((buf (get-buffer " widget-choose")))
250 (let ((cursor-in-echo-area t)
254 (while (not (or (and (>= char ?0) (< char next-digit))
255 (eq value 'keyboard-quit)))
256 ;; Unread a SPC to lead to our new menu.
257 (setq unread-command-events (cons ?\ unread-command-events))
258 (setq keys (read-key-sequence title))
259 (setq value (lookup-key overriding-terminal-local-map keys t)
260 char (string-to-char (substring keys 1)))
261 (cond ((eq value 'scroll-other-window)
262 (let ((minibuffer-scroll-window (get-buffer-window buf)))
264 (scroll-other-window-down (window-height minibuffer-scroll-window))
265 (scroll-other-window))
267 ((eq value 'negative-argument)
271 (when (eq value 'keyboard-quit)
275 (defun widget-remove-if (predictate list)
276 (let (result (tail list))
278 (or (funcall predictate (car tail))
279 (setq result (cons (car tail) result)))
280 (setq tail (cdr tail)))
283 ;;; Widget text specifications.
285 ;; These functions are for specifying text properties.
287 (defcustom widget-field-add-space
288 (or (< emacs-major-version 20)
289 (and (eq emacs-major-version 20)
290 (< emacs-minor-version 3))
291 (not (string-match "XEmacs" emacs-version)))
292 "Non-nil means add extra space at the end of editable text fields.
294 This is needed on all versions of Emacs, and on XEmacs before 20.3.
295 If you don't add the space, it will become impossible to edit a zero
300 (defcustom widget-field-use-before-change
301 (and (or (> emacs-minor-version 34)
302 (> emacs-major-version 19))
303 (not (string-match "XEmacs" emacs-version)))
304 "Non-nil means use `before-change-functions' to track editable fields.
305 This enables the use of undo, but doesn't work on Emacs 19.34 and earlier.
306 Using before hooks also means that the :notify function can't know the
311 (defun widget-specify-field (widget from to)
312 "Specify editable button for WIDGET between FROM and TO."
313 ;; Terminating space is not part of the field, but necessary in
314 ;; order for local-map to work. Remove next sexp if local-map works
315 ;; at the end of the overlay.
318 (cond ((null (widget-get widget :size))
320 (widget-field-add-space
321 (insert-and-inherit " ")))
323 (let ((map (widget-get widget :keymap))
324 (face (or (widget-get widget :value-face) 'widget-field-face))
325 (help-echo (widget-get widget :help-echo))
326 (overlay (make-overlay from to nil
327 nil (or (not widget-field-add-space)
328 (widget-get widget :size)))))
329 (unless (or (stringp help-echo) (null help-echo))
330 (setq help-echo 'widget-mouse-help))
331 (widget-put widget :field-overlay overlay)
332 ;;(overlay-put overlay 'detachable nil)
333 (overlay-put overlay 'field widget)
334 (overlay-put overlay 'local-map map)
335 ;;(overlay-put overlay 'keymap map)
336 (overlay-put overlay 'face face)
337 ;;(overlay-put overlay 'balloon-help help-echo)
338 (overlay-put overlay 'help-echo help-echo))
339 (widget-specify-secret widget))
341 (defun widget-specify-secret (field)
342 "Replace text in FIELD with value of `:secret', if non-nil."
343 (let ((secret (widget-get field :secret))
344 (size (widget-get field :size)))
346 (let ((begin (widget-field-start field))
347 (end (widget-field-end field)))
349 (while (and (> end begin)
350 (eq (char-after (1- end)) ?\ ))
351 (setq end (1- end))))
353 (let ((old (char-after begin)))
354 (unless (eq old secret)
355 (subst-char-in-region begin (1+ begin) old secret)
356 (put-text-property begin (1+ begin) 'secret old))
357 (setq begin (1+ begin))))))))
359 (defun widget-specify-button (widget from to)
360 "Specify button for WIDGET between FROM and TO."
361 (let ((face (widget-apply widget :button-face-get))
362 (help-echo (widget-get widget :help-echo))
363 (overlay (make-overlay from to nil t nil)))
364 (widget-put widget :button-overlay overlay)
365 (unless (or (null help-echo) (stringp help-echo))
366 (setq help-echo 'widget-mouse-help))
367 (overlay-put overlay 'button widget)
368 (overlay-put overlay 'mouse-face widget-mouse-face)
369 ;;(overlay-put overlay 'balloon-help help-echo)
370 (overlay-put overlay 'help-echo help-echo)
371 (overlay-put overlay 'face face)))
373 (defun widget-mouse-help (extent)
374 "Find mouse help string for button in extent."
375 (let* ((widget (widget-at (extent-start-position extent)))
376 (help-echo (and widget (widget-get widget :help-echo))))
377 (cond ((stringp help-echo)
379 ((and (symbolp help-echo) (fboundp help-echo)
380 (stringp (setq help-echo (funcall help-echo widget))))
383 (format "(widget %S :help-echo %S)" widget help-echo)))))
385 (defun widget-specify-sample (widget from to)
386 ;; Specify sample for WIDGET between FROM and TO.
387 (let ((face (widget-apply widget :sample-face-get))
388 (overlay (make-overlay from to nil t nil)))
389 (overlay-put overlay 'face face)
390 (widget-put widget :sample-overlay overlay)))
392 (defun widget-specify-doc (widget from to)
393 ;; Specify documentation for WIDGET between FROM and TO.
394 (let ((overlay (make-overlay from to nil t nil)))
395 (overlay-put overlay 'widget-doc widget)
396 (overlay-put overlay 'face widget-documentation-face)
397 (widget-put widget :doc-overlay overlay)))
399 (defmacro widget-specify-insert (&rest form)
400 ;; Execute FORM without inheriting any text properties.
403 (let ((inhibit-read-only t)
405 before-change-functions
406 after-change-functions)
408 (narrow-to-region (- (point) 2) (point))
409 (goto-char (1+ (point-min)))
410 (setq result (progn (,@ form)))
411 (delete-region (point-min) (1+ (point-min)))
412 (delete-region (1- (point-max)) (point-max))
413 (goto-char (point-max))
416 (defface widget-inactive-face '((((class grayscale color)
418 (:foreground "light gray"))
419 (((class grayscale color)
421 (:foreground "dim gray"))
424 "Face used for inactive widgets."
425 :group 'widget-faces)
427 (defun widget-specify-inactive (widget from to)
428 "Make WIDGET inactive for user modifications."
429 (unless (widget-get widget :inactive)
430 (let ((overlay (make-overlay from to nil t nil)))
431 (overlay-put overlay 'face 'widget-inactive-face)
432 ;; This is disabled, as it makes the mouse cursor change shape.
433 ;; (overlay-put overlay 'mouse-face 'widget-inactive-face)
434 (overlay-put overlay 'evaporate t)
435 (overlay-put overlay 'priority 100)
436 (overlay-put overlay 'modification-hooks '(widget-overlay-inactive))
437 (widget-put widget :inactive overlay))))
439 (defun widget-overlay-inactive (&rest junk)
440 "Ignoring the arguments, signal an error."
441 (unless inhibit-read-only
442 (error "The widget here is not active")))
445 (defun widget-specify-active (widget)
446 "Make WIDGET active for user modifications."
447 (let ((inactive (widget-get widget :inactive)))
449 (delete-overlay inactive)
450 (widget-put widget :inactive nil))))
452 ;;; Widget Properties.
454 (defsubst widget-type (widget)
455 "Return the type of WIDGET, a symbol."
458 (defun widget-get-indirect (widget property)
459 "In WIDGET, get the value of PROPERTY.
460 If the value is a symbol, return its binding.
461 Otherwise, just return the value."
462 (let ((value (widget-get widget property)))
467 (defun widget-member (widget property)
468 "Non-nil iff there is a definition in WIDGET for PROPERTY."
469 (cond ((widget-plist-member (cdr widget) property)
472 (widget-member (get (car widget) 'widget-type) property))
475 (defun widget-value (widget)
476 "Extract the current value of WIDGET."
478 :value-to-external (widget-apply widget :value-get)))
480 (defun widget-value-set (widget value)
481 "Set the current value of WIDGET to VALUE."
483 :value-set (widget-apply widget
484 :value-to-internal value)))
486 (defun widget-default-get (widget)
487 "Extract the default value of WIDGET."
488 (or (widget-get widget :value)
489 (widget-apply widget :default-get)))
491 (defun widget-match-inline (widget vals)
492 "In WIDGET, match the start of VALS."
493 (cond ((widget-get widget :inline)
494 (widget-apply widget :match-inline vals))
496 (widget-apply widget :match (car vals)))
497 (cons (list (car vals)) (cdr vals)))
500 (defun widget-apply-action (widget &optional event)
501 "Apply :action in WIDGET in response to EVENT."
502 (if (widget-apply widget :active)
503 (widget-apply widget :action event)
504 (error "Attempt to perform action on inactive widget")))
506 ;;; Helper functions.
508 ;; These are widget specific.
511 (defun widget-prompt-value (widget prompt &optional value unbound)
512 "Prompt for a value matching WIDGET, using PROMPT.
513 The current value is assumed to be VALUE, unless UNBOUND is non-nil."
514 (unless (listp widget)
515 (setq widget (list widget)))
516 (setq prompt (format "[%s] %s" (widget-type widget) prompt))
517 (setq widget (widget-convert widget))
518 (let ((answer (widget-apply widget :prompt-value prompt value unbound)))
519 (unless (widget-apply widget :match answer)
520 (error "Value does not match %S type." (car widget)))
523 (defun widget-get-sibling (widget)
524 "Get the item WIDGET is assumed to toggle.
525 This is only meaningful for radio buttons or checkboxes in a list."
526 (let* ((parent (widget-get widget :parent))
527 (children (widget-get parent :children))
531 (setq child (car children)
532 children (cdr children))
533 (when (eq (widget-get child :button) widget)
534 (throw 'child child)))
537 (defun widget-map-buttons (function &optional buffer maparg)
538 "Map FUNCTION over the buttons in BUFFER.
539 FUNCTION is called with the arguments WIDGET and MAPARG.
541 If FUNCTION returns non-nil, the walk is cancelled.
543 The arguments MAPARG, and BUFFER default to nil and (current-buffer),
545 (let ((cur (point-min))
549 (save-excursion (set-buffer buffer) (overlay-lists))
551 (setq overlays (append (car overlays) (cdr overlays)))
552 (while (setq cur (pop overlays))
553 (setq widget (overlay-get cur 'button))
554 (if (and widget (funcall function widget maparg))
555 (setq overlays nil)))))
559 (defcustom widget-glyph-directory (concat data-directory "custom/")
560 "Where widget glyphs are located.
561 If this variable is nil, widget will try to locate the directory
566 (defcustom widget-glyph-enable t
567 "If non nil, use glyphs in images when available."
571 (defcustom widget-image-conversion
572 '((xpm ".xpm") (gif ".gif") (png ".png") (jpeg ".jpg" ".jpeg")
574 "Conversion alist from image formats to file name suffixes."
576 :type '(repeat (cons :format "%v"
577 (symbol :tag "Image Format" unknown)
578 (repeat :tag "Suffixes"
579 (string :format "%v")))))
581 (defun widget-glyph-find (image tag)
582 "Create a glyph corresponding to IMAGE with string TAG as fallback.
583 IMAGE should either already be a glyph, or be a file name sans
584 extension (xpm, xbm, gif, jpg, or png) located in
585 `widget-glyph-directory'."
586 (cond ((not (and image
587 (string-match "XEmacs" emacs-version)
589 (fboundp 'make-glyph)
590 (fboundp 'locate-file)
592 ;; We don't want or can't use glyphs.
594 ((and (fboundp 'glyphp)
596 ;; Already a glyph. Use it.
599 ;; A string. Look it up in relevant directories.
600 (let* ((dirlist (list (or widget-glyph-directory
601 (concat data-directory
604 (formats widget-image-conversion)
606 (while (and formats (not file))
607 (when (valid-image-instantiator-format-p (car (car formats)))
608 (setq file (locate-file image dirlist
613 (setq formats (cdr formats))))
615 ;; We create a glyph with the file as the default image
616 ;; instantiator, and the TAG fallback
617 (make-glyph (list (vector (car (car formats)) ':file file)
618 (vector 'string ':data tag))))))
619 ((valid-instantiator-p image 'image)
620 ;; A valid image instantiator (e.g. [gif :file "somefile"] etc.)
621 (make-glyph (list image
622 (vector 'string ':data tag))))
624 ;; This could be virtually anything. Let `make-glyph' sort it out.
630 (defun widget-glyph-insert (widget tag image &optional down inactive)
631 "In WIDGET, insert the text TAG or, if supported, IMAGE.
632 IMAGE should either be a glyph, an image instantiator, or an image file
633 name sans extension (xpm, xbm, gif, jpg, or png) located in
634 `widget-glyph-directory'.
636 Optional arguments DOWN and INACTIVE is used instead of IMAGE when the
637 glyph is pressed or inactive, respectively.
639 WARNING: If you call this with a glyph, and you want the user to be
640 able to invoke the glyph, make sure it is unique. If you use the
641 same glyph for multiple widgets, invoking any of the glyphs will
642 cause the last created widget to be invoked.
644 Instead of an instantiator, you can also use a list of instantiators,
645 or whatever `make-glyph' will accept. However, in that case you must
646 provide the fallback TAG as a part of the instantiator yourself."
647 (let ((glyph (widget-glyph-find image tag)))
649 (widget-glyph-insert-glyph widget
651 (widget-glyph-find down tag)
652 (widget-glyph-find inactive tag))
655 (defun widget-glyph-insert-glyph (widget glyph &optional down inactive)
656 "In WIDGET, insert GLYPH.
657 If optional arguments DOWN and INACTIVE are given, they should be
658 glyphs used when the widget is pushed and inactive, respectively."
660 (set-glyph-property glyph 'widget widget)
662 (set-glyph-property down 'widget widget))
664 (set-glyph-property inactive 'widget widget)))
666 (let ((ext (make-extent (point) (1- (point))))
667 (help-echo (and widget (widget-get widget :help-echo))))
668 (set-extent-property ext 'invisible t)
669 (set-extent-property ext 'start-open t)
670 (set-extent-property ext 'end-open t)
671 (set-extent-end-glyph ext glyph)
673 (set-extent-property ext 'balloon-help help-echo)
674 (set-extent-property ext 'help-echo help-echo)))
676 (widget-put widget :glyph-up glyph)
677 (when down (widget-put widget :glyph-down down))
678 (when inactive (widget-put widget :glyph-inactive inactive))))
682 (defgroup widget-button nil
683 "The look of various kinds of buttons."
686 (defcustom widget-button-prefix ""
687 "String used as prefix for buttons."
689 :group 'widget-button)
691 (defcustom widget-button-suffix ""
692 "String used as suffix for buttons."
694 :group 'widget-button)
696 ;;; Creating Widgets.
699 (defun widget-create (type &rest args)
700 "Create widget of TYPE.
701 The optional ARGS are additional keyword arguments."
702 (let ((widget (apply 'widget-convert type args)))
703 (widget-apply widget :create)
706 (defun widget-create-child-and-convert (parent type &rest args)
707 "As part of the widget PARENT, create a child widget TYPE.
708 The child is converted, using the keyword arguments ARGS."
709 (let ((widget (apply 'widget-convert type args)))
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 (parent type)
719 "Create widget of TYPE."
720 (let ((widget (copy-sequence type)))
721 (widget-put widget :parent parent)
722 (unless (widget-get widget :indent)
723 (widget-put widget :indent (+ (or (widget-get parent :indent) 0)
724 (or (widget-get widget :extra-offset) 0)
725 (widget-get parent :offset))))
726 (widget-apply widget :create)
729 (defun widget-create-child-value (parent type value)
730 "Create widget of TYPE with value VALUE."
731 (let ((widget (copy-sequence type)))
732 (widget-put widget :value (widget-apply widget :value-to-internal value))
733 (widget-put widget :parent parent)
734 (unless (widget-get widget :indent)
735 (widget-put widget :indent (+ (or (widget-get parent :indent) 0)
736 (or (widget-get widget :extra-offset) 0)
737 (widget-get parent :offset))))
738 (widget-apply widget :create)
742 (defun widget-delete (widget)
744 (widget-apply widget :delete))
746 (defun widget-convert (type &rest args)
747 "Convert TYPE to a widget without inserting it in the buffer.
748 The optional ARGS are additional keyword arguments."
749 ;; Don't touch the type.
750 (let* ((widget (if (symbolp type)
752 (copy-sequence type)))
755 ;; First set the :args keyword.
756 (while (cdr current) ;Look in the type.
757 (let ((next (car (cdr current))))
758 (if (and (symbolp next) (eq (aref (symbol-name next) 0) ?:))
759 (setq current (cdr (cdr current)))
760 (setcdr current (list :args (cdr current)))
761 (setq current nil))))
762 (while args ;Look in the args.
763 (let ((next (nth 0 args)))
764 (if (and (symbolp next) (eq (aref (symbol-name next) 0) ?:))
765 (setq args (nthcdr 2 args))
766 (widget-put widget :args args)
768 ;; Then Convert the widget.
771 (let ((convert-widget (plist-get (cdr type) :convert-widget)))
773 (setq widget (funcall convert-widget widget))))
774 (setq type (get (car type) 'widget-type)))
775 ;; Finally set the keyword args.
777 (let ((next (nth 0 keys)))
778 (if (and (symbolp next) (eq (aref (symbol-name next) 0) ?:))
780 (widget-put widget next (nth 1 keys))
781 (setq keys (nthcdr 2 keys)))
783 ;; Convert the :value to internal format.
784 (if (widget-member widget :value)
785 (let ((value (widget-get widget :value)))
787 :value (widget-apply widget :value-to-internal value))))
788 ;; Return the newly create widget.
791 (defun widget-insert (&rest args)
792 "Call `insert' with ARGS and make the text read only."
793 (let ((inhibit-read-only t)
794 before-change-functions
795 after-change-functions
797 (apply 'insert args)))
799 (defun widget-convert-text (type from to
800 &optional button-from button-to
802 "Return a widget of type TYPE with endpoint FROM TO.
803 Optional ARGS are extra keyword arguments for TYPE.
804 and TO will be used as the widgets end points. If optional arguments
805 BUTTON-FROM and BUTTON-TO are given, these will be used as the widgets
807 Optional ARGS are extra keyword arguments for TYPE."
808 (let ((widget (apply 'widget-convert type :delete 'widget-leave-text args))
809 (from (copy-marker from))
810 (to (copy-marker to)))
811 (set-marker-insertion-type from t)
812 (set-marker-insertion-type to nil)
813 (widget-put widget :from from)
814 (widget-put widget :to to)
816 (widget-specify-button widget button-from button-to))
819 (defun widget-convert-button (type from to &rest args)
820 "Return a widget of type TYPE with endpoint FROM TO.
821 Optional ARGS are extra keyword arguments for TYPE.
822 No text will be inserted to the buffer, instead the text between FROM
823 and TO will be used as the widgets end points, as well as the widgets
825 (apply 'widget-convert-text type from to from to args))
827 (defun widget-leave-text (widget)
828 "Remove markers and overlays from WIDGET and its children."
829 (let ((from (widget-get widget :from))
830 (to (widget-get widget :to))
831 (button (widget-get widget :button-overlay))
832 (sample (widget-get widget :sample-overlay))
833 (doc (widget-get widget :doc-overlay))
834 (field (widget-get widget :field-overlay))
835 (children (widget-get widget :children)))
836 (set-marker from nil)
839 (delete-overlay button))
841 (delete-overlay sample))
843 (delete-overlay doc))
845 (delete-overlay field))
846 (mapcar 'widget-leave-text children)))
848 ;;; Keymap and Commands.
850 (defvar widget-keymap nil
851 "Keymap containing useful binding for buffers containing widgets.
852 Recommended as a parent keymap for modes using widgets.")
854 (unless widget-keymap
855 (setq widget-keymap (make-sparse-keymap))
856 (define-key widget-keymap "\t" 'widget-forward)
857 (define-key widget-keymap [(shift tab)] 'widget-backward)
858 (define-key widget-keymap [backtab] 'widget-backward)
859 (if (string-match "XEmacs" emacs-version)
862 (define-key widget-keymap [button1] 'widget-button1-click)
863 (define-key widget-keymap [button2] 'widget-button-click))
864 (define-key widget-keymap [down-mouse-2] 'widget-button-click))
865 (define-key widget-keymap "\C-m" 'widget-button-press))
867 (defvar widget-global-map global-map
868 "Keymap used for events the widget does not handle themselves.")
869 (make-variable-buffer-local 'widget-global-map)
871 (defvar widget-field-keymap nil
872 "Keymap used inside an editable field.")
874 (unless widget-field-keymap
875 (setq widget-field-keymap (copy-keymap widget-keymap))
876 (define-key widget-field-keymap [menu-bar] 'nil)
877 (define-key widget-field-keymap "\C-k" 'widget-kill-line)
878 (define-key widget-field-keymap "\M-\t" 'widget-complete)
879 (define-key widget-field-keymap "\C-m" 'widget-field-activate)
880 (define-key widget-field-keymap "\C-a" 'widget-beginning-of-line)
881 (define-key widget-field-keymap "\C-e" 'widget-end-of-line)
882 (set-keymap-parent widget-field-keymap global-map))
884 (defvar widget-text-keymap nil
885 "Keymap used inside a text field.")
887 (unless widget-text-keymap
888 (setq widget-text-keymap (copy-keymap widget-keymap))
889 (define-key widget-text-keymap [menu-bar] 'nil)
890 (define-key widget-text-keymap "\C-a" 'widget-beginning-of-line)
891 (define-key widget-text-keymap "\C-e" 'widget-end-of-line)
892 (set-keymap-parent widget-text-keymap global-map))
894 (defun widget-field-activate (pos &optional event)
895 "Invoke the ediable field at point."
897 (let ((field (get-char-property pos 'field)))
899 (widget-apply-action field event)
901 (lookup-key widget-global-map (this-command-keys))))))
903 (defvar widget-button-pressed-face 'widget-button-pressed-face
904 "Face used for pressed buttons in widgets.
905 This exists as a variable so it can be set locally in certain buffers.")
907 (defface widget-button-pressed-face
911 (:bold t :underline t)))
912 "Face used for pressed buttons."
913 :group 'widget-faces)
915 (defun widget-button-click (event)
916 "Invoke the button that the mouse is pointing at, and move there."
918 (mouse-set-point event)
919 (cond ((and (fboundp 'event-glyph)
921 (widget-glyph-click event))
922 ((widget-event-point event)
923 (let* ((pos (widget-event-point event))
924 (button (get-char-property pos 'button)))
926 (let* ((overlay (widget-get button :button-overlay))
927 (face (overlay-get overlay 'face))
928 (mouse-face (overlay-get overlay 'mouse-face)))
930 (let ((track-mouse t))
932 'face widget-button-pressed-face)
934 'mouse-face widget-button-pressed-face)
935 (unless (widget-apply button :mouse-down-action event)
936 (while (not (button-release-event-p event))
937 (setq event (widget-read-event)
938 pos (widget-event-point event))
940 (eq (get-char-property pos 'button)
945 widget-button-pressed-face)
948 widget-button-pressed-face))
949 (overlay-put overlay 'face face)
950 (overlay-put overlay 'mouse-face mouse-face))))
952 (eq (get-char-property pos 'button) button))
953 (widget-apply-action button event)))
954 (overlay-put overlay 'face face)
955 (overlay-put overlay 'mouse-face mouse-face)))
958 ;; Find the global command to run, and check whether it
959 ;; is bound to an up event.
960 (cond ((setq command ;down event
961 (lookup-key widget-global-map [ button2 ]))
963 ((setq command ;down event
964 (lookup-key widget-global-map [ down-mouse-2 ]))
966 ((setq command ;up event
967 (lookup-key widget-global-map [ button2up ])))
968 ((setq command ;up event
969 (lookup-key widget-global-map [ mouse-2]))))
971 ;; Don't execute up events twice.
972 (while (not (button-release-event-p event))
973 (setq event (widget-read-event))))
975 (call-interactively command))))))
977 (message "You clicked somewhere weird."))))
979 (defun widget-button1-click (event)
980 "Invoke glyph below mouse pointer."
982 (if (and (fboundp 'event-glyph)
984 (widget-glyph-click event)
985 (call-interactively (lookup-key widget-global-map (this-command-keys)))))
987 (defun widget-glyph-click (event)
988 "Handle click on a glyph."
989 (let* ((glyph (event-glyph event))
990 (widget (glyph-property glyph 'widget))
991 (extent (event-glyph-extent event))
992 (down-glyph (or (and widget (widget-get widget :glyph-down)) glyph))
993 (up-glyph (or (and widget (widget-get widget :glyph-up)) glyph))
995 ;; Wait for the release.
996 (while (not (button-release-event-p last))
997 (if (eq extent (event-glyph-extent last))
998 (set-extent-property extent 'end-glyph down-glyph)
999 (set-extent-property extent 'end-glyph up-glyph))
1000 (setq last (read-event event)))
1003 (set-extent-property extent 'end-glyph up-glyph))
1004 ;; Apply widget action.
1005 (when (eq extent (event-glyph-extent last))
1006 (let ((widget (glyph-property (event-glyph event) 'widget)))
1007 (cond ((null widget)
1008 (message "You clicked on a glyph."))
1009 ((not (widget-apply widget :active))
1010 (message "This glyph is inactive."))
1012 (widget-apply-action widget event)))))))
1014 (defun widget-button-press (pos &optional event)
1015 "Invoke button at POS."
1017 (let ((button (get-char-property pos 'button)))
1019 (widget-apply-action button event)
1020 (let ((command (lookup-key widget-global-map (this-command-keys))))
1021 (when (commandp command)
1022 (call-interactively command))))))
1024 (defun widget-tabable-at (&optional pos)
1025 "Return the tabable widget at POS, or nil.
1026 POS defaults to the value of (point)."
1029 (let ((widget (or (get-char-property (point) 'button)
1030 (get-char-property (point) 'field))))
1032 (let ((order (widget-get widget :tab-order)))
1040 (defvar widget-use-overlay-change t
1041 "If non-nil, use overlay change functions to tab around in the buffer.
1042 This is much faster, but doesn't work reliably on Emacs 19.34.")
1044 (defun widget-move (arg)
1045 "Move point to the ARG next field or button.
1046 ARG may be negative to move backward."
1047 (or (bobp) (> arg 0) (backward-char))
1050 (old (widget-tabable-at))
1055 (goto-char (point-min)))
1056 (widget-use-overlay-change
1057 (goto-char (next-overlay-change (point))))
1060 (and (eq pos (point))
1062 (error "No buttons or fields found"))
1063 (let ((new (widget-tabable-at)))
1065 (unless (eq new old)
1071 (goto-char (point-max)))
1072 (widget-use-overlay-change
1073 (goto-char (previous-overlay-change (point))))
1076 (and (eq pos (point))
1078 (error "No buttons or fields found"))
1079 (let ((new (widget-tabable-at)))
1081 (unless (eq new old)
1082 (setq arg (1+ arg))))))
1083 (let ((new (widget-tabable-at)))
1084 (while (eq (widget-tabable-at) new)
1087 (widget-echo-help (point))
1088 (run-hooks 'widget-move-hook))
1090 (defun widget-forward (arg)
1091 "Move point to the next field or button.
1092 With optional ARG, move across that many fields."
1094 (run-hooks 'widget-forward-hook)
1097 (defun widget-backward (arg)
1098 "Move point to the previous field or button.
1099 With optional ARG, move across that many fields."
1101 (run-hooks 'widget-backward-hook)
1102 (widget-move (- arg)))
1104 (defun widget-beginning-of-line ()
1105 "Go to beginning of field or beginning of line, whichever is first."
1107 (let* ((field (widget-field-find (point)))
1108 (start (and field (widget-field-start field)))
1109 (bol (save-excursion
1112 (goto-char (if start
1116 (defun widget-end-of-line ()
1117 "Go to end of field or end of line, whichever is first."
1119 (let* ((field (widget-field-find (point)))
1120 (end (and field (widget-field-end field)))
1121 (eol (save-excursion
1128 (defun widget-kill-line ()
1129 "Kill to end of field or end of line, whichever is first."
1131 (let* ((field (widget-field-find (point)))
1132 (newline (save-excursion (forward-line 1) (point)))
1133 (end (and field (widget-field-end field))))
1134 (if (and field (> newline end))
1135 (kill-region (point) end)
1136 (call-interactively 'kill-line))))
1138 (defcustom widget-complete-field (lookup-key global-map "\M-\t")
1139 "Default function to call for completion inside fields."
1140 :options '(ispell-complete-word complete-tag lisp-complete-symbol)
1144 (defun widget-complete ()
1145 "Complete content of editable field from point.
1146 When not inside a field, move to the previous button or field."
1148 (let ((field (widget-field-find (point))))
1150 (widget-apply field :complete)
1151 (error "Not in an editable field"))))
1153 ;;; Setting up the buffer.
1155 (defvar widget-field-new nil)
1156 ;; List of all newly created editable fields in the buffer.
1157 (make-variable-buffer-local 'widget-field-new)
1159 (defvar widget-field-list nil)
1160 ;; List of all editable fields in the buffer.
1161 (make-variable-buffer-local 'widget-field-list)
1163 (defun widget-setup ()
1164 "Setup current buffer so editing string widgets works."
1165 (let ((inhibit-read-only t)
1166 (after-change-functions nil)
1167 before-change-functions
1169 (while widget-field-new
1170 (setq field (car widget-field-new)
1171 widget-field-new (cdr widget-field-new)
1172 widget-field-list (cons field widget-field-list))
1173 (let ((from (car (widget-get field :field-overlay)))
1174 (to (cdr (widget-get field :field-overlay))))
1175 (widget-specify-field field
1176 (marker-position from) (marker-position to))
1177 (set-marker from nil)
1178 (set-marker to nil))))
1180 (widget-add-change))
1182 (defvar widget-field-last nil)
1183 ;; Last field containing point.
1184 (make-variable-buffer-local 'widget-field-last)
1186 (defvar widget-field-was nil)
1187 ;; The widget data before the change.
1188 (make-variable-buffer-local 'widget-field-was)
1190 (defun widget-field-buffer (widget)
1191 "Return the start of WIDGET's editing field."
1192 (let ((overlay (widget-get widget :field-overlay)))
1193 (and overlay (overlay-buffer overlay))))
1195 (defun widget-field-start (widget)
1196 "Return the start of WIDGET's editing field."
1197 (let ((overlay (widget-get widget :field-overlay)))
1198 (and overlay (overlay-start overlay))))
1200 (defun widget-field-end (widget)
1201 "Return the end of WIDGET's editing field."
1202 (let ((overlay (widget-get widget :field-overlay)))
1203 ;; Don't subtract one if local-map works at the end of the overlay.
1204 (and overlay (if (or widget-field-add-space
1205 (null (widget-get widget :size)))
1206 (1- (overlay-end overlay))
1207 (overlay-end overlay)))))
1209 (defun widget-field-find (pos)
1210 "Return the field at POS.
1211 Unlike (get-char-property POS 'field) this, works with empty fields too."
1212 (let ((fields widget-field-list)
1215 (setq field (car fields)
1216 fields (cdr fields))
1217 (let ((start (widget-field-start field))
1218 (end (widget-field-end field)))
1219 (when (and (<= start pos) (<= pos end))
1221 (debug "Overlapping fields"))
1222 (setq found field))))
1225 (defun widget-before-change (from to)
1226 ;; This is how, for example, a variable changes its state to `modified'.
1227 ;; when it is being edited.
1228 (unless inhibit-read-only
1229 (let ((from-field (widget-field-find from))
1230 (to-field (widget-field-find to)))
1231 (cond ((not (eq from-field to-field))
1232 (add-hook 'post-command-hook 'widget-add-change nil t)
1233 (signal 'text-read-only
1234 '("Change should be restricted to a single field")))
1236 (add-hook 'post-command-hook 'widget-add-change nil t)
1237 (signal 'text-read-only
1238 '("Attempt to change text outside editable field")))
1239 (widget-field-use-before-change
1241 (widget-apply from-field :notify from-field)
1242 (error (debug "Before Change"))))))))
1244 (defun widget-add-change ()
1245 (make-local-hook 'post-command-hook)
1246 (remove-hook 'post-command-hook 'widget-add-change t)
1247 (make-local-hook 'before-change-functions)
1248 (add-hook 'before-change-functions 'widget-before-change nil t)
1249 (make-local-hook 'after-change-functions)
1250 (add-hook 'after-change-functions 'widget-after-change nil t))
1252 (defun widget-after-change (from to old)
1253 ;; Adjust field size and text properties.
1255 (let ((field (widget-field-find from))
1256 (other (widget-field-find to)))
1258 (unless (eq field other)
1259 (debug "Change in different fields"))
1260 (let ((size (widget-get field :size)))
1262 (let ((begin (widget-field-start field))
1263 (end (widget-field-end field)))
1264 (cond ((< (- end begin) size)
1268 (insert-char ?\ (- (+ begin size) end))))
1269 ((> (- end begin) size)
1270 ;; Field too large and
1271 (if (or (< (point) (+ begin size))
1273 ;; Point is outside extra space.
1274 (setq begin (+ begin size))
1275 ;; Point is within the extra space.
1276 (setq begin (point)))
1279 (while (and (eq (preceding-char) ?\ )
1281 (delete-backward-char 1)))))))
1282 (widget-specify-secret field))
1283 (widget-apply field :notify field)))
1284 (error (debug "After Change"))))
1286 ;;; Widget Functions
1288 ;; These functions are used in the definition of multiple widgets.
1290 (defun widget-parent-action (widget &optional event)
1291 "Tell :parent of WIDGET to handle the :action.
1292 Optional EVENT is the event that triggered the action."
1293 (widget-apply (widget-get widget :parent) :action event))
1295 (defun widget-children-value-delete (widget)
1296 "Delete all :children and :buttons in WIDGET."
1297 (mapcar 'widget-delete (widget-get widget :children))
1298 (widget-put widget :children nil)
1299 (mapcar 'widget-delete (widget-get widget :buttons))
1300 (widget-put widget :buttons nil))
1302 (defun widget-children-validate (widget)
1303 "All the :children must be valid."
1304 (let ((children (widget-get widget :children))
1306 (while (and children (not found))
1307 (setq child (car children)
1308 children (cdr children)
1309 found (widget-apply child :validate)))
1312 (defun widget-types-convert-widget (widget)
1313 "Convert :args as widget types in WIDGET."
1314 (widget-put widget :args (mapcar 'widget-convert (widget-get widget :args)))
1317 (defun widget-value-convert-widget (widget)
1318 "Initialize :value from :args in WIDGET."
1319 (let ((args (widget-get widget :args)))
1321 (widget-put widget :value (car args))
1322 ;; Don't convert :value here, as this is done in `widget-convert'.
1323 ;; (widget-put widget :value (widget-apply widget
1324 ;; :value-to-internal (car args)))
1325 (widget-put widget :args nil)))
1328 (defun widget-value-value-get (widget)
1329 "Return the :value property of WIDGET."
1330 (widget-get widget :value))
1332 ;;; The `default' Widget.
1334 (define-widget 'default nil
1335 "Basic widget other widgets are derived from."
1336 :value-to-internal (lambda (widget value) value)
1337 :value-to-external (lambda (widget value) value)
1338 :button-prefix 'widget-button-prefix
1339 :button-suffix 'widget-button-suffix
1340 :complete 'widget-default-complete
1341 :create 'widget-default-create
1344 :format-handler 'widget-default-format-handler
1345 :button-face-get 'widget-default-button-face-get
1346 :sample-face-get 'widget-default-sample-face-get
1347 :delete 'widget-default-delete
1348 :value-set 'widget-default-value-set
1349 :value-inline 'widget-default-value-inline
1350 :default-get 'widget-default-default-get
1351 :menu-tag-get 'widget-default-menu-tag-get
1352 :validate (lambda (widget) nil)
1353 :active 'widget-default-active
1354 :activate 'widget-specify-active
1355 :deactivate 'widget-default-deactivate
1356 :mouse-down-action (lambda (widget event) nil)
1357 :action 'widget-default-action
1358 :notify 'widget-default-notify
1359 :prompt-value 'widget-default-prompt-value)
1361 (defun widget-default-complete (widget)
1362 "Call the value of the :complete-function property of WIDGET.
1363 If that does not exists, call the value of `widget-complete-field'."
1364 (let ((fun (widget-get widget :complete-function)))
1365 (call-interactively (or fun widget-complete-field))))
1367 (defun widget-default-create (widget)
1368 "Create WIDGET at point in the current buffer."
1369 (widget-specify-insert
1370 (let ((from (point))
1371 button-begin button-end
1372 sample-begin sample-end
1375 (insert (widget-get widget :format))
1377 ;; Parse escapes in format.
1378 (while (re-search-forward "%\\(.\\)" nil t)
1379 (let ((escape (aref (match-string 1) 0)))
1380 (replace-match "" t t)
1381 (cond ((eq escape ?%)
1384 (setq button-begin (point))
1385 (insert (widget-get-indirect widget :button-prefix)))
1387 (insert (widget-get-indirect widget :button-suffix))
1388 (setq button-end (point)))
1390 (setq sample-begin (point)))
1392 (setq sample-end (point)))
1394 (when (widget-get widget :indent)
1396 (insert-char ? (widget-get widget :indent))))
1398 (let ((glyph (widget-get widget :tag-glyph))
1399 (tag (widget-get widget :tag)))
1401 (widget-glyph-insert widget (or tag "image") glyph))
1405 (let ((standard-output (current-buffer)))
1406 (princ (widget-get widget :value)))))))
1408 (let ((doc (widget-get widget :doc)))
1410 (setq doc-begin (point))
1412 (while (eq (preceding-char) ?\n)
1413 (delete-backward-char 1))
1415 (setq doc-end (point)))))
1417 (if (and button-begin (not button-end))
1418 (widget-apply widget :value-create)
1419 (setq value-pos (point))))
1421 (widget-apply widget :format-handler escape)))))
1422 ;; Specify button, sample, and doc, and insert value.
1423 (and button-begin button-end
1424 (widget-specify-button widget button-begin button-end))
1425 (and sample-begin sample-end
1426 (widget-specify-sample widget sample-begin sample-end))
1427 (and doc-begin doc-end
1428 (widget-specify-doc widget doc-begin doc-end))
1430 (goto-char value-pos)
1431 (widget-apply widget :value-create)))
1432 (let ((from (copy-marker (point-min)))
1433 (to (copy-marker (point-max))))
1434 (set-marker-insertion-type from t)
1435 (set-marker-insertion-type to nil)
1436 (widget-put widget :from from)
1437 (widget-put widget :to to)))
1438 (widget-clear-undo))
1440 (defun widget-default-format-handler (widget escape)
1441 ;; We recognize the %h escape by default.
1442 (let* ((buttons (widget-get widget :buttons)))
1443 (cond ((eq escape ?h)
1444 (let* ((doc-property (widget-get widget :documentation-property))
1445 (doc-try (cond ((widget-get widget :doc))
1446 ((symbolp doc-property)
1447 (documentation-property
1448 (widget-get widget :value)
1451 (funcall doc-property
1452 (widget-get widget :value)))))
1453 (doc-text (and (stringp doc-try)
1454 (> (length doc-try) 1)
1456 (doc-indent (widget-get widget :documentation-indent)))
1458 (and (eq (preceding-char) ?\n)
1459 (widget-get widget :indent)
1460 (insert-char ? (widget-get widget :indent)))
1461 ;; The `*' in the beginning is redundant.
1462 (when (eq (aref doc-text 0) ?*)
1463 (setq doc-text (substring doc-text 1)))
1464 ;; Get rid of trailing newlines.
1465 (when (string-match "\n+\\'" doc-text)
1466 (setq doc-text (substring doc-text 0 (match-beginning 0))))
1467 (push (widget-create-child-and-convert
1468 widget 'documentation-string
1469 :indent (cond ((numberp doc-indent )
1477 (error "Unknown escape `%c'" escape)))
1478 (widget-put widget :buttons buttons)))
1480 (defun widget-default-button-face-get (widget)
1481 ;; Use :button-face or widget-button-face
1482 (or (widget-get widget :button-face)
1483 (let ((parent (widget-get widget :parent)))
1485 (widget-apply parent :button-face-get)
1486 widget-button-face))))
1488 (defun widget-default-sample-face-get (widget)
1489 ;; Use :sample-face.
1490 (widget-get widget :sample-face))
1492 (defun widget-default-delete (widget)
1493 ;; Remove widget from the buffer.
1494 (let ((from (widget-get widget :from))
1495 (to (widget-get widget :to))
1496 (inactive-overlay (widget-get widget :inactive))
1497 (button-overlay (widget-get widget :button-overlay))
1498 (sample-overlay (widget-get widget :sample-overlay))
1499 (doc-overlay (widget-get widget :doc-overlay))
1500 before-change-functions
1501 after-change-functions
1502 (inhibit-read-only t))
1503 (widget-apply widget :value-delete)
1504 (when inactive-overlay
1505 (delete-overlay inactive-overlay))
1506 (when button-overlay
1507 (delete-overlay button-overlay))
1508 (when sample-overlay
1509 (delete-overlay sample-overlay))
1511 (delete-overlay doc-overlay))
1513 ;; Kludge: this doesn't need to be true for empty formats.
1514 (delete-region from to))
1515 (set-marker from nil)
1516 (set-marker to nil))
1517 (widget-clear-undo))
1519 (defun widget-default-value-set (widget value)
1520 ;; Recreate widget with new value.
1521 (let* ((old-pos (point))
1522 (from (copy-marker (widget-get widget :from)))
1523 (to (copy-marker (widget-get widget :to)))
1524 (offset (if (and (<= from old-pos) (<= old-pos to))
1525 (if (>= old-pos (1- to))
1527 (- old-pos from)))))
1528 ;;??? Bug: this ought to insert the new value before deleting the old one,
1529 ;; so that markers on either side of the value automatically
1530 ;; stay on the same side. -- rms.
1532 (goto-char (widget-get widget :from))
1533 (widget-apply widget :delete)
1534 (widget-put widget :value value)
1535 (widget-apply widget :create))
1538 (goto-char (+ (widget-get widget :to) offset 1))
1539 (goto-char (min (+ from offset) (1- (widget-get widget :to))))))))
1541 (defun widget-default-value-inline (widget)
1542 ;; Wrap value in a list unless it is inline.
1543 (if (widget-get widget :inline)
1544 (widget-value widget)
1545 (list (widget-value widget))))
1547 (defun widget-default-default-get (widget)
1549 (widget-get widget :value))
1551 (defun widget-default-menu-tag-get (widget)
1552 ;; Use tag or value for menus.
1553 (or (widget-get widget :menu-tag)
1554 (widget-get widget :tag)
1555 (widget-princ-to-string (widget-get widget :value))))
1557 (defun widget-default-active (widget)
1558 "Return t iff this widget active (user modifiable)."
1559 (and (not (widget-get widget :inactive))
1560 (let ((parent (widget-get widget :parent)))
1562 (widget-apply parent :active)))))
1564 (defun widget-default-deactivate (widget)
1565 "Make WIDGET inactive for user modifications."
1566 (widget-specify-inactive widget
1567 (widget-get widget :from)
1568 (widget-get widget :to)))
1570 (defun widget-default-action (widget &optional event)
1571 ;; Notify the parent when a widget change
1572 (let ((parent (widget-get widget :parent)))
1574 (widget-apply parent :notify widget event))))
1576 (defun widget-default-notify (widget child &optional event)
1577 ;; Pass notification to parent.
1578 (widget-default-action widget event))
1580 (defun widget-default-prompt-value (widget prompt value unbound)
1581 ;; Read an arbitrary value. Stolen from `set-variable'.
1582 ;; (let ((initial (if unbound
1584 ;; ;; It would be nice if we could do a `(cons val 1)' here.
1585 ;; (prin1-to-string (custom-quote value))))))
1586 (eval-minibuffer prompt ))
1588 ;;; The `item' Widget.
1590 (define-widget 'item 'default
1591 "Constant items for inclusion in other widgets."
1592 :convert-widget 'widget-value-convert-widget
1593 :value-create 'widget-item-value-create
1594 :value-delete 'ignore
1595 :value-get 'widget-value-value-get
1596 :match 'widget-item-match
1597 :match-inline 'widget-item-match-inline
1598 :action 'widget-item-action
1601 (defun widget-item-value-create (widget)
1602 ;; Insert the printed representation of the value.
1603 (let ((standard-output (current-buffer)))
1604 (princ (widget-get widget :value))))
1606 (defun widget-item-match (widget value)
1607 ;; Match if the value is the same.
1608 (equal (widget-get widget :value) value))
1610 (defun widget-item-match-inline (widget values)
1611 ;; Match if the value is the same.
1612 (let ((value (widget-get widget :value)))
1614 (<= (length value) (length values))
1615 (let ((head (widget-sublist values 0 (length value))))
1616 (and (equal head value)
1617 (cons head (widget-sublist values (length value))))))))
1619 (defun widget-sublist (list start &optional end)
1620 "Return the sublist of LIST from START to END.
1621 If END is omitted, it defaults to the length of LIST."
1622 (if (> start 0) (setq list (nthcdr start list)))
1626 (setq list (copy-sequence list))
1627 (setcdr (nthcdr (- end start 1) list) nil)
1629 (copy-sequence list)))
1631 (defun widget-item-action (widget &optional event)
1632 ;; Just notify itself.
1633 (widget-apply widget :notify widget event))
1635 ;;; The `push-button' Widget.
1637 (defcustom widget-push-button-gui t
1638 "If non nil, use GUI push buttons when available."
1642 ;; Cache already created GUI objects.
1643 (defvar widget-push-button-cache nil)
1645 (defcustom widget-push-button-prefix "["
1646 "String used as prefix for buttons."
1648 :group 'widget-button)
1650 (defcustom widget-push-button-suffix "]"
1651 "String used as suffix for buttons."
1653 :group 'widget-button)
1655 (define-widget 'push-button 'item
1656 "A pushable button."
1659 :value-create 'widget-push-button-value-create
1662 (defun widget-push-button-value-create (widget)
1663 ;; Insert text representing the `on' and `off' states.
1664 (let* ((tag (or (widget-get widget :tag)
1665 (widget-get widget :value)))
1666 (tag-glyph (widget-get widget :tag-glyph))
1667 (text (concat widget-push-button-prefix
1668 tag widget-push-button-suffix))
1669 (gui (cdr (assoc tag widget-push-button-cache))))
1671 (widget-glyph-insert widget text tag-glyph))
1672 ((and (fboundp 'make-gui-button)
1673 (fboundp 'make-glyph)
1674 widget-push-button-gui
1675 (fboundp 'device-on-window-system-p)
1676 (device-on-window-system-p)
1677 (string-match "XEmacs" emacs-version))
1679 (setq gui (make-gui-button tag 'widget-gui-action widget))
1680 (push (cons tag gui) widget-push-button-cache))
1681 (widget-glyph-insert-glyph widget
1683 (list (nth 0 (aref gui 1))
1684 (vector 'string ':data text)))
1686 (list (nth 1 (aref gui 1))
1687 (vector 'string ':data text)))
1689 (list (nth 2 (aref gui 1))
1690 (vector 'string ':data text)))))
1694 (defun widget-gui-action (widget)
1695 "Apply :action for WIDGET."
1696 (widget-apply-action widget (this-command-keys)))
1698 ;;; The `link' Widget.
1700 (defcustom widget-link-prefix "["
1701 "String used as prefix for links."
1703 :group 'widget-button)
1705 (defcustom widget-link-suffix "]"
1706 "String used as suffix for links."
1708 :group 'widget-button)
1710 (define-widget 'link 'item
1712 :button-prefix 'widget-link-prefix
1713 :button-suffix 'widget-link-suffix
1714 :help-echo "Follow the link."
1717 ;;; The `info-link' Widget.
1719 (define-widget 'info-link 'link
1720 "A link to an info file."
1721 :action 'widget-info-link-action)
1723 (defun widget-info-link-action (widget &optional event)
1724 "Open the info node specified by WIDGET."
1725 (Info-goto-node (widget-value widget)))
1727 ;;; The `url-link' Widget.
1729 (define-widget 'url-link 'link
1730 "A link to an www page."
1731 :action 'widget-url-link-action)
1733 (defun widget-url-link-action (widget &optional event)
1734 "Open the url specified by WIDGET."
1735 (browse-url (widget-value widget)))
1737 ;;; The `function-link' Widget.
1739 (define-widget 'function-link 'link
1740 "A link to an Emacs function."
1741 :action 'widget-function-link-action)
1743 (defun widget-function-link-action (widget &optional event)
1744 "Show the function specified by WIDGET."
1745 (describe-function (widget-value widget)))
1747 ;;; The `variable-link' Widget.
1749 (define-widget 'variable-link 'link
1750 "A link to an Emacs variable."
1751 :action 'widget-variable-link-action)
1753 (defun widget-variable-link-action (widget &optional event)
1754 "Show the variable specified by WIDGET."
1755 (describe-variable (widget-value widget)))
1757 ;;; The `file-link' Widget.
1759 (define-widget 'file-link 'link
1761 :action 'widget-file-link-action)
1763 (defun widget-file-link-action (widget &optional event)
1764 "Find the file specified by WIDGET."
1765 (find-file (widget-value widget)))
1767 ;;; The `emacs-library-link' Widget.
1769 (define-widget 'emacs-library-link 'link
1770 "A link to an Emacs Lisp library file."
1771 :action 'widget-emacs-library-link-action)
1773 (defun widget-emacs-library-link-action (widget &optional event)
1774 "Find the Emacs Library file specified by WIDGET."
1775 (find-file (locate-library (widget-value widget))))
1777 ;;; The `emacs-commentary-link' Widget.
1779 (define-widget 'emacs-commentary-link 'link
1780 "A link to Commentary in an Emacs Lisp library file."
1781 :action 'widget-emacs-commentary-link-action)
1783 (defun widget-emacs-commentary-link-action (widget &optional event)
1784 "Find the Commentary section of the Emacs file specified by WIDGET."
1785 (finder-commentary (widget-value widget)))
1787 ;;; The `editable-field' Widget.
1789 (define-widget 'editable-field 'default
1790 "An editable text field."
1791 :convert-widget 'widget-value-convert-widget
1792 :keymap widget-field-keymap
1795 :prompt-internal 'widget-field-prompt-internal
1796 :prompt-history 'widget-field-history
1797 :prompt-value 'widget-field-prompt-value
1798 :action 'widget-field-action
1799 :validate 'widget-field-validate
1802 :value-create 'widget-field-value-create
1803 :value-delete 'widget-field-value-delete
1804 :value-get 'widget-field-value-get
1805 :match 'widget-field-match)
1807 (defvar widget-field-history nil
1808 "History of field minibuffer edits.")
1810 (defun widget-field-prompt-internal (widget prompt initial history)
1811 ;; Read string for WIDGET promptinhg with PROMPT.
1812 ;; INITIAL is the initial input and HISTORY is a symbol containing
1813 ;; the earlier input.
1814 (read-string prompt initial history))
1816 (defun widget-field-prompt-value (widget prompt value unbound)
1817 ;; Prompt for a string.
1818 (let ((initial (if unbound
1820 (cons (widget-apply widget :value-to-internal
1822 (history (widget-get widget :prompt-history)))
1823 (let ((answer (widget-apply widget
1824 :prompt-internal prompt initial history)))
1825 (widget-apply widget :value-to-external answer))))
1827 (defvar widget-edit-functions nil)
1829 (defun widget-field-action (widget &optional event)
1830 ;; Move to next field.
1832 (run-hook-with-args 'widget-edit-functions widget))
1834 (defun widget-field-validate (widget)
1835 ;; Valid if the content matches `:valid-regexp'.
1837 (let ((value (widget-apply widget :value-get))
1838 (regexp (widget-get widget :valid-regexp)))
1839 (if (string-match regexp value)
1843 (defun widget-field-value-create (widget)
1844 ;; Create an editable text field.
1845 (let ((size (widget-get widget :size))
1846 (value (widget-get widget :value))
1848 ;; This is changed to a real overlay in `widget-setup'. We
1849 ;; need the end points to behave differently until
1850 ;; `widget-setup' is called.
1851 (overlay (cons (make-marker) (make-marker))))
1852 (widget-put widget :field-overlay overlay)
1855 (< (length value) size)
1856 (insert-char ?\ (- size (length value))))
1857 (unless (memq widget widget-field-list)
1858 (setq widget-field-new (cons widget widget-field-new)))
1859 (move-marker (cdr overlay) (point))
1860 (set-marker-insertion-type (cdr overlay) nil)
1863 (move-marker (car overlay) from)
1864 (set-marker-insertion-type (car overlay) t)))
1866 (defun widget-field-value-delete (widget)
1867 ;; Remove the widget from the list of active editing fields.
1868 (setq widget-field-list (delq widget widget-field-list))
1869 ;; These are nil if the :format string doesn't contain `%v'.
1870 (let ((overlay (widget-get widget :field-overlay)))
1872 (delete-overlay overlay))))
1874 (defun widget-field-value-get (widget)
1875 ;; Return current text in editing field.
1876 (let ((from (widget-field-start widget))
1877 (to (widget-field-end widget))
1878 (buffer (widget-field-buffer widget))
1879 (size (widget-get widget :size))
1880 (secret (widget-get widget :secret))
1881 (old (current-buffer)))
1888 (eq (char-after (1- to)) ?\ ))
1890 (let ((result (buffer-substring-no-properties from to)))
1893 (while (< (+ from index) to)
1895 (get-char-property (+ from index) 'secret))
1896 (setq index (1+ index)))))
1899 (widget-get widget :value))))
1901 (defun widget-field-match (widget value)
1902 ;; Match any string.
1905 ;;; The `text' Widget.
1907 (define-widget 'text 'editable-field
1908 :keymap widget-text-keymap
1909 "A multiline text area.")
1911 ;;; The `menu-choice' Widget.
1913 (define-widget 'menu-choice 'default
1914 "A menu of options."
1915 :convert-widget 'widget-types-convert-widget
1916 :format "%[%t%]: %v"
1919 :void '(item :format "invalid (%t)\n")
1920 :value-create 'widget-choice-value-create
1921 :value-delete 'widget-children-value-delete
1922 :value-get 'widget-choice-value-get
1923 :value-inline 'widget-choice-value-inline
1924 :default-get 'widget-choice-default-get
1925 :mouse-down-action 'widget-choice-mouse-down-action
1926 :action 'widget-choice-action
1927 :error "Make a choice"
1928 :validate 'widget-choice-validate
1929 :match 'widget-choice-match
1930 :match-inline 'widget-choice-match-inline)
1932 (defun widget-choice-value-create (widget)
1933 ;; Insert the first choice that matches the value.
1934 (let ((value (widget-get widget :value))
1935 (args (widget-get widget :args))
1936 (explicit (widget-get widget :explicit-choice))
1937 (explicit-value (widget-get widget :explicit-choice-value))
1939 (if (and explicit (equal value explicit-value))
1941 ;; If the user specified the choice for this value,
1942 ;; respect that choice as long as the value is the same.
1943 (widget-put widget :children (list (widget-create-child-value
1944 widget explicit value)))
1945 (widget-put widget :choice explicit))
1947 (setq current (car args)
1949 (when (widget-apply current :match value)
1950 (widget-put widget :children (list (widget-create-child-value
1951 widget current value)))
1952 (widget-put widget :choice current)
1956 (let ((void (widget-get widget :void)))
1957 (widget-put widget :children (list (widget-create-child-and-convert
1958 widget void :value value)))
1959 (widget-put widget :choice void))))))
1961 (defun widget-choice-value-get (widget)
1962 ;; Get value of the child widget.
1963 (widget-value (car (widget-get widget :children))))
1965 (defun widget-choice-value-inline (widget)
1966 ;; Get value of the child widget.
1967 (widget-apply (car (widget-get widget :children)) :value-inline))
1969 (defun widget-choice-default-get (widget)
1970 ;; Get default for the first choice.
1971 (widget-default-get (car (widget-get widget :args))))
1973 (defcustom widget-choice-toggle nil
1974 "If non-nil, a binary choice will just toggle between the values.
1975 Otherwise, the user will explicitly have to choose between the values
1976 when he invoked the menu."
1980 (defun widget-choice-mouse-down-action (widget &optional event)
1981 ;; Return non-nil if we need a menu.
1982 (let ((args (widget-get widget :args))
1983 (old (widget-get widget :choice)))
1984 (cond ((not window-system)
1985 ;; No place to pop up a menu.
1987 ((not (or (fboundp 'x-popup-menu) (fboundp 'popup-menu)))
1988 ;; No way to pop up a menu.
1990 ((< (length args) 2)
1991 ;; Empty or singleton list, just return the value.
1993 ((> (length args) widget-menu-max-size)
1994 ;; Too long, prompt.
1996 ((> (length args) 2)
1997 ;; Reasonable sized list, use menu.
1999 ((and widget-choice-toggle (memq old args))
2003 ;; Ask which of the two.
2006 (defun widget-choice-action (widget &optional event)
2008 (let ((args (widget-get widget :args))
2009 (old (widget-get widget :choice))
2010 (tag (widget-apply widget :menu-tag-get))
2011 (completion-ignore-case (widget-get widget :case-fold))
2014 ;; Remember old value.
2015 (if (and old (not (widget-apply widget :validate)))
2016 (let* ((external (widget-value widget))
2017 (internal (widget-apply old :value-to-internal external)))
2018 (widget-put old :value internal)))
2021 (cond ((= (length args) 0)
2023 ((= (length args) 1)
2025 ((and widget-choice-toggle
2028 (if (eq old (nth 0 args))
2033 (setq current (car args)
2036 (cons (cons (widget-apply current :menu-tag-get)
2039 (setq this-explicit t)
2040 (widget-choose tag (reverse choices) event))))
2042 ;; If this was an explicit user choice,
2043 ;; record the choice, and the record the value it was made for.
2044 ;; widget-choice-value-create will respect this choice,
2045 ;; as long as the value is the same.
2047 (widget-put widget :explicit-choice current)
2048 (widget-put widget :explicit-choice-value (widget-get widget :value)))
2049 (let ((value (widget-default-get current)))
2050 (widget-value-set widget
2051 (widget-apply current :value-to-external value)))
2053 (widget-apply widget :notify widget event)))
2054 (run-hook-with-args 'widget-edit-functions widget))
2056 (defun widget-choice-validate (widget)
2057 ;; Valid if we have made a valid choice.
2058 (let ((void (widget-get widget :void))
2059 (choice (widget-get widget :choice))
2060 (child (car (widget-get widget :children))))
2061 (if (eq void choice)
2063 (widget-apply child :validate))))
2065 (defun widget-choice-match (widget value)
2066 ;; Matches if one of the choices matches.
2067 (let ((args (widget-get widget :args))
2069 (while (and args (not found))
2070 (setq current (car args)
2072 found (widget-apply current :match value)))
2075 (defun widget-choice-match-inline (widget values)
2076 ;; Matches if one of the choices matches.
2077 (let ((args (widget-get widget :args))
2079 (while (and args (null found))
2080 (setq current (car args)
2082 found (widget-match-inline current values)))
2085 ;;; The `toggle' Widget.
2087 (define-widget 'toggle 'item
2088 "Toggle between two states."
2090 :value-create 'widget-toggle-value-create
2091 :action 'widget-toggle-action
2092 :match (lambda (widget value) t)
2096 (defun widget-toggle-value-create (widget)
2097 ;; Insert text representing the `on' and `off' states.
2098 (if (widget-value widget)
2099 (widget-glyph-insert widget
2100 (widget-get widget :on)
2101 (widget-get widget :on-glyph))
2102 (widget-glyph-insert widget
2103 (widget-get widget :off)
2104 (widget-get widget :off-glyph))))
2106 (defun widget-toggle-action (widget &optional event)
2108 (widget-value-set widget (not (widget-value widget)))
2109 (widget-apply widget :notify widget event)
2110 (run-hook-with-args 'widget-edit-functions widget))
2112 ;;; The `checkbox' Widget.
2114 (define-widget 'checkbox 'toggle
2115 "A checkbox toggle."
2123 :action 'widget-checkbox-action)
2125 (defun widget-checkbox-action (widget &optional event)
2126 "Toggle checkbox, notify parent, and set active state of sibling."
2127 (widget-toggle-action widget event)
2128 (let ((sibling (widget-get-sibling widget)))
2130 (if (widget-value widget)
2131 (widget-apply sibling :activate)
2132 (widget-apply sibling :deactivate)))))
2134 ;;; The `checklist' Widget.
2136 (define-widget 'checklist 'default
2137 "A multiple choice widget."
2138 :convert-widget 'widget-types-convert-widget
2141 :entry-format "%b %v"
2142 :menu-tag "checklist"
2144 :value-create 'widget-checklist-value-create
2145 :value-delete 'widget-children-value-delete
2146 :value-get 'widget-checklist-value-get
2147 :validate 'widget-checklist-validate
2148 :match 'widget-checklist-match
2149 :match-inline 'widget-checklist-match-inline)
2151 (defun widget-checklist-value-create (widget)
2152 ;; Insert all values
2153 (let ((alist (widget-checklist-match-find widget (widget-get widget :value)))
2154 (args (widget-get widget :args)))
2156 (widget-checklist-add-item widget (car args) (assq (car args) alist))
2157 (setq args (cdr args)))
2158 (widget-put widget :children (nreverse (widget-get widget :children)))))
2160 (defun widget-checklist-add-item (widget type chosen)
2161 ;; Create checklist item in WIDGET of type TYPE.
2162 ;; If the item is checked, CHOSEN is a cons whose cdr is the value.
2163 (and (eq (preceding-char) ?\n)
2164 (widget-get widget :indent)
2165 (insert-char ? (widget-get widget :indent)))
2166 (widget-specify-insert
2167 (let* ((children (widget-get widget :children))
2168 (buttons (widget-get widget :buttons))
2169 (button-args (or (widget-get type :sibling-args)
2170 (widget-get widget :button-args)))
2173 (insert (widget-get widget :entry-format))
2175 ;; Parse % escapes in format.
2176 (while (re-search-forward "%\\([bv%]\\)" nil t)
2177 (let ((escape (aref (match-string 1) 0)))
2178 (replace-match "" t t)
2179 (cond ((eq escape ?%)
2182 (setq button (apply 'widget-create-child-and-convert
2184 :value (not (null chosen))
2189 (let ((child (widget-create-child widget type)))
2190 (widget-apply child :deactivate)
2192 ((widget-get type :inline)
2193 (widget-create-child-value
2194 widget type (cdr chosen)))
2196 (widget-create-child-value
2197 widget type (car (cdr chosen)))))))
2199 (error "Unknown escape `%c'" escape)))))
2200 ;; Update properties.
2201 (and button child (widget-put child :button button))
2202 (and button (widget-put widget :buttons (cons button buttons)))
2203 (and child (widget-put widget :children (cons child children))))))
2205 (defun widget-checklist-match (widget values)
2206 ;; All values must match a type in the checklist.
2208 (null (cdr (widget-checklist-match-inline widget values)))))
2210 (defun widget-checklist-match-inline (widget values)
2211 ;; Find the values which match a type in the checklist.
2212 (let ((greedy (widget-get widget :greedy))
2213 (args (copy-sequence (widget-get widget :args)))
2216 (let ((answer (widget-checklist-match-up args values)))
2218 (let ((vals (widget-match-inline answer values)))
2219 (setq found (append found (car vals))
2221 args (delq answer args))))
2223 (setq rest (append rest (list (car values)))
2224 values (cdr values)))
2226 (setq rest (append rest values)
2230 (defun widget-checklist-match-find (widget vals)
2231 ;; Find the vals which match a type in the checklist.
2232 ;; Return an alist of (TYPE MATCH).
2233 (let ((greedy (widget-get widget :greedy))
2234 (args (copy-sequence (widget-get widget :args)))
2237 (let ((answer (widget-checklist-match-up args vals)))
2239 (let ((match (widget-match-inline answer vals)))
2240 (setq found (cons (cons answer (car match)) found)
2242 args (delq answer args))))
2244 (setq vals (cdr vals)))
2249 (defun widget-checklist-match-up (args vals)
2250 ;; Rerturn the first type from ARGS that matches VALS.
2251 (let (current found)
2252 (while (and args (null found))
2253 (setq current (car args)
2255 found (widget-match-inline current vals)))
2260 (defun widget-checklist-value-get (widget)
2261 ;; The values of all selected items.
2262 (let ((children (widget-get widget :children))
2265 (setq child (car children)
2266 children (cdr children))
2267 (if (widget-value (widget-get child :button))
2268 (setq result (append result (widget-apply child :value-inline)))))
2271 (defun widget-checklist-validate (widget)
2272 ;; Ticked chilren must be valid.
2273 (let ((children (widget-get widget :children))
2275 (while (and children (not found))
2276 (setq child (car children)
2277 children (cdr children)
2278 button (widget-get child :button)
2279 found (and (widget-value button)
2280 (widget-apply child :validate))))
2283 ;;; The `option' Widget
2285 (define-widget 'option 'checklist
2286 "An widget with an optional item."
2289 ;;; The `choice-item' Widget.
2291 (define-widget 'choice-item 'item
2292 "Button items that delegate action events to their parents."
2293 :action 'widget-parent-action
2294 :format "%[%t%] \n")
2296 ;;; The `radio-button' Widget.
2298 (define-widget 'radio-button 'toggle
2299 "A radio button for use in the `radio' widget."
2300 :notify 'widget-radio-button-notify
2307 :off-glyph "radio0")
2309 (defun widget-radio-button-notify (widget child &optional event)
2311 (widget-apply (widget-get widget :parent) :action widget event))
2313 ;;; The `radio-button-choice' Widget.
2315 (define-widget 'radio-button-choice 'default
2316 "Select one of multiple options."
2317 :convert-widget 'widget-types-convert-widget
2320 :entry-format "%b %v"
2322 :value-create 'widget-radio-value-create
2323 :value-delete 'widget-children-value-delete
2324 :value-get 'widget-radio-value-get
2325 :value-inline 'widget-radio-value-inline
2326 :value-set 'widget-radio-value-set
2327 :error "You must push one of the buttons"
2328 :validate 'widget-radio-validate
2329 :match 'widget-choice-match
2330 :match-inline 'widget-choice-match-inline
2331 :action 'widget-radio-action)
2333 (defun widget-radio-value-create (widget)
2334 ;; Insert all values
2335 (let ((args (widget-get widget :args))
2338 (setq arg (car args)
2340 (widget-radio-add-item widget arg))))
2342 (defun widget-radio-add-item (widget type)
2343 "Add to radio widget WIDGET a new radio button item of type TYPE."
2344 ;; (setq type (widget-convert type))
2345 (and (eq (preceding-char) ?\n)
2346 (widget-get widget :indent)
2347 (insert-char ? (widget-get widget :indent)))
2348 (widget-specify-insert
2349 (let* ((value (widget-get widget :value))
2350 (children (widget-get widget :children))
2351 (buttons (widget-get widget :buttons))
2352 (button-args (or (widget-get type :sibling-args)
2353 (widget-get widget :button-args)))
2355 (chosen (and (null (widget-get widget :choice))
2356 (widget-apply type :match value)))
2358 (insert (widget-get widget :entry-format))
2360 ;; Parse % escapes in format.
2361 (while (re-search-forward "%\\([bv%]\\)" nil t)
2362 (let ((escape (aref (match-string 1) 0)))
2363 (replace-match "" t t)
2364 (cond ((eq escape ?%)
2367 (setq button (apply 'widget-create-child-and-convert
2368 widget 'radio-button
2369 :value (not (null chosen))
2372 (setq child (if chosen
2373 (widget-create-child-value
2375 (widget-create-child widget type)))
2377 (widget-apply child :deactivate)))
2379 (error "Unknown escape `%c'" escape)))))
2380 ;; Update properties.
2382 (widget-put widget :choice type))
2384 (widget-put child :button button)
2385 (widget-put widget :buttons (nconc buttons (list button))))
2387 (widget-put widget :children (nconc children (list child))))
2390 (defun widget-radio-value-get (widget)
2391 ;; Get value of the child widget.
2392 (let ((chosen (widget-radio-chosen widget)))
2393 (and chosen (widget-value chosen))))
2395 (defun widget-radio-chosen (widget)
2396 "Return the widget representing the chosen radio button."
2397 (let ((children (widget-get widget :children))
2400 (setq current (car children)
2401 children (cdr children))
2402 (let* ((button (widget-get current :button))
2403 (value (widget-apply button :value-get)))
2409 (defun widget-radio-value-inline (widget)
2410 ;; Get value of the child widget.
2411 (let ((children (widget-get widget :children))
2414 (setq current (car children)
2415 children (cdr children))
2416 (let* ((button (widget-get current :button))
2417 (value (widget-apply button :value-get)))
2419 (setq found (widget-apply current :value-inline)
2423 (defun widget-radio-value-set (widget value)
2424 ;; We can't just delete and recreate a radio widget, since children
2425 ;; can be added after the original creation and won't be recreated
2427 (let ((children (widget-get widget :children))
2430 (setq current (car children)
2431 children (cdr children))
2432 (let* ((button (widget-get current :button))
2433 (match (and (not found)
2434 (widget-apply current :match value))))
2435 (widget-value-set button match)
2438 (widget-value-set current value)
2439 (widget-apply current :activate))
2440 (widget-apply current :deactivate))
2441 (setq found (or found match))))))
2443 (defun widget-radio-validate (widget)
2444 ;; Valid if we have made a valid choice.
2445 (let ((children (widget-get widget :children))
2446 current found button)
2447 (while (and children (not found))
2448 (setq current (car children)
2449 children (cdr children)
2450 button (widget-get current :button)
2451 found (widget-apply button :value-get)))
2453 (widget-apply current :validate)
2456 (defun widget-radio-action (widget child event)
2457 ;; Check if a radio button was pressed.
2458 (let ((children (widget-get widget :children))
2459 (buttons (widget-get widget :buttons))
2461 (when (memq child buttons)
2463 (setq current (car children)
2464 children (cdr children))
2465 (let* ((button (widget-get current :button)))
2466 (cond ((eq child button)
2467 (widget-value-set button t)
2468 (widget-apply current :activate))
2469 ((widget-value button)
2470 (widget-value-set button nil)
2471 (widget-apply current :deactivate)))))))
2472 ;; Pass notification to parent.
2473 (widget-apply widget :notify child event))
2475 ;;; The `insert-button' Widget.
2477 (define-widget 'insert-button 'push-button
2478 "An insert button for the `editable-list' widget."
2480 :help-echo "Insert a new item into the list at this position."
2481 :action 'widget-insert-button-action)
2483 (defun widget-insert-button-action (widget &optional event)
2484 ;; Ask the parent to insert a new item.
2485 (widget-apply (widget-get widget :parent)
2486 :insert-before (widget-get widget :widget)))
2488 ;;; The `delete-button' Widget.
2490 (define-widget 'delete-button 'push-button
2491 "A delete button for the `editable-list' widget."
2493 :help-echo "Delete this item from the list."
2494 :action 'widget-delete-button-action)
2496 (defun widget-delete-button-action (widget &optional event)
2497 ;; Ask the parent to insert a new item.
2498 (widget-apply (widget-get widget :parent)
2499 :delete-at (widget-get widget :widget)))
2501 ;;; The `editable-list' Widget.
2503 (defcustom widget-editable-list-gui nil
2504 "If non nil, use GUI push-buttons in editable list when available."
2508 (define-widget 'editable-list 'default
2509 "A variable list of widgets of the same type."
2510 :convert-widget 'widget-types-convert-widget
2513 :format-handler 'widget-editable-list-format-handler
2514 :entry-format "%i %d %v"
2515 :menu-tag "editable-list"
2516 :value-create 'widget-editable-list-value-create
2517 :value-delete 'widget-children-value-delete
2518 :value-get 'widget-editable-list-value-get
2519 :validate 'widget-children-validate
2520 :match 'widget-editable-list-match
2521 :match-inline 'widget-editable-list-match-inline
2522 :insert-before 'widget-editable-list-insert-before
2523 :delete-at 'widget-editable-list-delete-at)
2525 (defun widget-editable-list-format-handler (widget escape)
2526 ;; We recognize the insert button.
2527 (let ((widget-push-button-gui widget-editable-list-gui))
2528 (cond ((eq escape ?i)
2529 (and (widget-get widget :indent)
2530 (insert-char ? (widget-get widget :indent)))
2531 (apply 'widget-create-child-and-convert
2532 widget 'insert-button
2533 (widget-get widget :append-button-args)))
2535 (widget-default-format-handler widget escape)))))
2537 (defun widget-editable-list-value-create (widget)
2538 ;; Insert all values
2539 (let* ((value (widget-get widget :value))
2540 (type (nth 0 (widget-get widget :args)))
2541 (inlinep (widget-get type :inline))
2543 (widget-put widget :value-pos (copy-marker (point)))
2544 (set-marker-insertion-type (widget-get widget :value-pos) t)
2546 (let ((answer (widget-match-inline type value)))
2548 (setq children (cons (widget-editable-list-entry-create
2557 (widget-put widget :children (nreverse children))))
2559 (defun widget-editable-list-value-get (widget)
2560 ;; Get value of the child widget.
2561 (apply 'append (mapcar (lambda (child) (widget-apply child :value-inline))
2562 (widget-get widget :children))))
2564 (defun widget-editable-list-match (widget value)
2565 ;; Value must be a list and all the members must match the type.
2567 (null (cdr (widget-editable-list-match-inline widget value)))))
2569 (defun widget-editable-list-match-inline (widget value)
2570 (let ((type (nth 0 (widget-get widget :args)))
2573 (while (and value ok)
2574 (let ((answer (widget-match-inline type value)))
2576 (setq found (append found (car answer))
2579 (cons found value)))
2581 (defun widget-editable-list-insert-before (widget before)
2582 ;; Insert a new child in the list of children.
2584 (let ((children (widget-get widget :children))
2585 (inhibit-read-only t)
2586 before-change-functions
2587 after-change-functions)
2589 (goto-char (widget-get before :entry-from)))
2591 (goto-char (widget-get widget :value-pos))))
2592 (let ((child (widget-editable-list-entry-create
2594 (when (< (widget-get child :entry-from) (widget-get widget :from))
2595 (set-marker (widget-get widget :from)
2596 (widget-get child :entry-from)))
2597 (if (eq (car children) before)
2598 (widget-put widget :children (cons child children))
2599 (while (not (eq (car (cdr children)) before))
2600 (setq children (cdr children)))
2601 (setcdr children (cons child (cdr children)))))))
2603 (widget-apply widget :notify widget))
2605 (defun widget-editable-list-delete-at (widget child)
2606 ;; Delete child from list of children.
2608 (let ((buttons (copy-sequence (widget-get widget :buttons)))
2610 (inhibit-read-only t)
2611 before-change-functions
2612 after-change-functions)
2614 (setq button (car buttons)
2615 buttons (cdr buttons))
2616 (when (eq (widget-get button :widget) child)
2618 :buttons (delq button (widget-get widget :buttons)))
2619 (widget-delete button))))
2620 (let ((entry-from (widget-get child :entry-from))
2621 (entry-to (widget-get child :entry-to))
2622 (inhibit-read-only t)
2623 before-change-functions
2624 after-change-functions)
2625 (widget-delete child)
2626 (delete-region entry-from entry-to)
2627 (set-marker entry-from nil)
2628 (set-marker entry-to nil))
2629 (widget-put widget :children (delq child (widget-get widget :children))))
2631 (widget-apply widget :notify widget))
2633 (defun widget-editable-list-entry-create (widget value conv)
2634 ;; Create a new entry to the list.
2635 (let ((type (nth 0 (widget-get widget :args)))
2636 (widget-push-button-gui widget-editable-list-gui)
2637 child delete insert)
2638 (widget-specify-insert
2640 (and (widget-get widget :indent)
2641 (insert-char ? (widget-get widget :indent)))
2642 (insert (widget-get widget :entry-format)))
2643 ;; Parse % escapes in format.
2644 (while (re-search-forward "%\\(.\\)" nil t)
2645 (let ((escape (aref (match-string 1) 0)))
2646 (replace-match "" t t)
2647 (cond ((eq escape ?%)
2650 (setq insert (apply 'widget-create-child-and-convert
2651 widget 'insert-button
2652 (widget-get widget :insert-button-args))))
2654 (setq delete (apply 'widget-create-child-and-convert
2655 widget 'delete-button
2656 (widget-get widget :delete-button-args))))
2659 (setq child (widget-create-child-value
2661 (setq child (widget-create-child-value
2663 (widget-apply type :value-to-external
2664 (widget-default-get type))))))
2666 (error "Unknown escape `%c'" escape)))))
2668 :buttons (cons delete
2670 (widget-get widget :buttons))))
2671 (let ((entry-from (copy-marker (point-min)))
2672 (entry-to (copy-marker (point-max))))
2673 (set-marker-insertion-type entry-from t)
2674 (set-marker-insertion-type entry-to nil)
2675 (widget-put child :entry-from entry-from)
2676 (widget-put child :entry-to entry-to)))
2677 (widget-put insert :widget child)
2678 (widget-put delete :widget child)
2681 ;;; The `group' Widget.
2683 (define-widget 'group 'default
2684 "A widget which groups other widgets inside."
2685 :convert-widget 'widget-types-convert-widget
2687 :value-create 'widget-group-value-create
2688 :value-delete 'widget-children-value-delete
2689 :value-get 'widget-editable-list-value-get
2690 :default-get 'widget-group-default-get
2691 :validate 'widget-children-validate
2692 :match 'widget-group-match
2693 :match-inline 'widget-group-match-inline)
2695 (defun widget-group-value-create (widget)
2696 ;; Create each component.
2697 (let ((args (widget-get widget :args))
2698 (value (widget-get widget :value))
2699 arg answer children)
2701 (setq arg (car args)
2703 answer (widget-match-inline arg value)
2705 (and (eq (preceding-char) ?\n)
2706 (widget-get widget :indent)
2707 (insert-char ? (widget-get widget :indent)))
2708 (push (cond ((null answer)
2709 (widget-create-child widget arg))
2710 ((widget-get arg :inline)
2711 (widget-create-child-value widget arg (car answer)))
2713 (widget-create-child-value widget arg (car (car answer)))))
2715 (widget-put widget :children (nreverse children))))
2717 (defun widget-group-default-get (widget)
2718 ;; Get the default of the components.
2719 (mapcar 'widget-default-get (widget-get widget :args)))
2721 (defun widget-group-match (widget values)
2722 ;; Match if the components match.
2724 (let ((match (widget-group-match-inline widget values)))
2725 (and match (null (cdr match))))))
2727 (defun widget-group-match-inline (widget vals)
2728 ;; Match if the components match.
2729 (let ((args (widget-get widget :args))
2730 argument answer found)
2732 (setq argument (car args)
2734 answer (widget-match-inline argument vals))
2736 (setq vals (cdr answer)
2737 found (append found (car answer)))
2744 ;;; The `visibility' Widget.
2746 (define-widget 'visibility 'item
2747 "An indicator and manipulator for hidden items."
2753 :value-create 'widget-visibility-value-create
2754 :action 'widget-toggle-action
2755 :match (lambda (widget value) t))
2757 (defun widget-visibility-value-create (widget)
2758 ;; Insert text representing the `on' and `off' states.
2759 (let ((on (widget-get widget :on))
2760 (off (widget-get widget :off)))
2762 (setq on (concat widget-push-button-prefix
2764 widget-push-button-suffix))
2767 (setq off (concat widget-push-button-prefix
2769 widget-push-button-suffix))
2771 (if (widget-value widget)
2772 (widget-glyph-insert widget on "down" "down-pushed")
2773 (widget-glyph-insert widget off "right" "right-pushed"))))
2775 ;;; The `documentation-link' Widget.
2777 ;; This is a helper widget for `documentation-string'.
2779 (define-widget 'documentation-link 'link
2780 "Link type used in documentation strings."
2782 :help-echo 'widget-documentation-link-echo-help
2783 :action 'widget-documentation-link-action)
2785 (defun widget-documentation-link-echo-help (widget)
2786 "Tell what this link will describe."
2787 (concat "Describe the `" (widget-get widget :value) "' symbol."))
2789 (defun widget-documentation-link-action (widget &optional event)
2790 "Display documentation for WIDGET's value. Ignore optional argument EVENT."
2791 (let* ((string (widget-get widget :value))
2792 (symbol (intern string)))
2793 (if (and (fboundp symbol) (boundp symbol))
2794 ;; If there are two doc strings, give the user a way to pick one.
2795 (apropos (concat "\\`" (regexp-quote string) "\\'"))
2796 (if (fboundp symbol)
2797 (describe-function symbol)
2798 (describe-variable symbol)))))
2800 (defcustom widget-documentation-links t
2801 "Add hyperlinks to documentation strings when non-nil."
2803 :group 'widget-documentation)
2805 (defcustom widget-documentation-link-regexp "`\\([^\n`' ]+\\)'"
2806 "Regexp for matching potential links in documentation strings.
2807 The first group should be the link itself."
2809 :group 'widget-documentation)
2811 (defcustom widget-documentation-link-p 'intern-soft
2812 "Predicate used to test if a string is useful as a link.
2813 The value should be a function. The function will be called one
2814 argument, a string, and should return non-nil if there should be a
2815 link for that string."
2817 :options '(widget-documentation-link-p)
2818 :group 'widget-documentation)
2820 (defcustom widget-documentation-link-type 'documentation-link
2821 "Widget type used for links in documentation strings."
2823 :group 'widget-documentation)
2825 (defun widget-documentation-link-add (widget from to)
2826 (widget-specify-doc widget from to)
2827 (when widget-documentation-links
2828 (let ((regexp widget-documentation-link-regexp)
2829 (predicate widget-documentation-link-p)
2830 (type widget-documentation-link-type)
2831 (buttons (widget-get widget :buttons))
2832 (widget-mouse-face (default-value 'widget-mouse-face))
2833 (widget-button-face widget-documentation-face)
2834 (widget-button-pressed-face widget-documentation-face))
2837 (while (re-search-forward regexp to t)
2838 (let ((name (match-string 1))
2839 (begin (match-beginning 1))
2840 (end (match-end 1)))
2841 (when (funcall predicate name)
2842 (push (widget-convert-button type begin end :value name)
2844 (widget-put widget :buttons buttons)))
2845 (let ((indent (widget-get widget :indent)))
2846 (when (and indent (not (zerop indent)))
2849 (narrow-to-region from to)
2850 (goto-char (point-min))
2851 (while (search-forward "\n" nil t)
2852 (insert-char ?\ indent)))))))
2854 ;;; The `documentation-string' Widget.
2856 (define-widget 'documentation-string 'item
2857 "A documentation string."
2859 :action 'widget-documentation-string-action
2860 :value-delete 'widget-children-value-delete
2861 :value-create 'widget-documentation-string-value-create)
2863 (defun widget-documentation-string-value-create (widget)
2864 ;; Insert documentation string.
2865 (let ((doc (widget-value widget))
2866 (indent (widget-get widget :indent))
2867 (shown (widget-get (widget-get widget :parent) :documentation-shown))
2869 (if (string-match "\n" doc)
2870 (let ((before (substring doc 0 (match-beginning 0)))
2871 (after (substring doc (match-beginning 0)))
2874 (widget-documentation-link-add widget start (point))
2875 (push (widget-create-child-and-convert
2877 :help-echo "Show or hide rest of the documentation."
2879 :action 'widget-parent-action
2883 (setq start (point))
2884 (when (and indent (not (zerop indent)))
2885 (insert-char ?\ indent))
2887 (widget-documentation-link-add widget start (point)))
2888 (widget-put widget :buttons buttons))
2890 (widget-documentation-link-add widget start (point))))
2893 (defun widget-documentation-string-action (widget &rest ignore)
2894 ;; Toggle documentation.
2895 (let ((parent (widget-get widget :parent)))
2896 (widget-put parent :documentation-shown
2897 (not (widget-get parent :documentation-shown))))
2899 (widget-value-set widget (widget-value widget)))
2901 ;;; The Sexp Widgets.
2903 (define-widget 'const 'item
2904 "An immutable sexp."
2905 :prompt-value 'widget-const-prompt-value
2908 (defun widget-const-prompt-value (widget prompt value unbound)
2909 ;; Return the value of the const.
2910 (widget-value widget))
2912 (define-widget 'function-item 'const
2913 "An immutable function name."
2915 :documentation-property (lambda (symbol)
2917 (documentation symbol t)
2920 (define-widget 'variable-item 'const
2921 "An immutable variable name."
2923 :documentation-property 'variable-documentation)
2925 (define-widget 'other 'sexp
2926 "Matches any value, but doesn't let the user edit the value.
2927 This is useful as last item in a `choice' widget.
2928 You should use this widget type with a default value,
2929 as in (other DEFAULT) or (other :tag \"NAME\" DEFAULT).
2930 If the user selects this alternative, that specifies DEFAULT
2936 (defvar widget-string-prompt-value-history nil
2937 "History of input to `widget-string-prompt-value'.")
2939 (define-widget 'string 'editable-field
2942 :format "%{%t%}: %v"
2943 :complete-function 'ispell-complete-word
2944 :prompt-history 'widget-string-prompt-value-history)
2946 (define-widget 'regexp 'string
2947 "A regular expression."
2948 :match 'widget-regexp-match
2949 :validate 'widget-regexp-validate
2950 ;; Doesn't work well with terminating newline.
2951 ;; :value-face 'widget-single-line-field-face
2954 (defun widget-regexp-match (widget value)
2955 ;; Match valid regexps.
2956 (and (stringp value)
2959 (string-match value ""))
2962 (defun widget-regexp-validate (widget)
2963 "Check that the value of WIDGET is a valid regexp."
2964 (let ((val (widget-value widget)))
2965 (condition-case data
2967 (string-match val ""))
2968 (error (widget-put widget :error (error-message-string data))
2971 (define-widget 'file 'string
2973 It will read a file name from the minibuffer when invoked."
2974 :complete-function 'widget-file-complete
2975 :prompt-value 'widget-file-prompt-value
2976 :format "%{%t%}: %v"
2977 ;; Doesn't work well with terminating newline.
2978 ;; :value-face 'widget-single-line-field-face
2981 (defun widget-file-complete ()
2982 "Perform completion on file name preceding point."
2984 (let* ((end (point))
2985 (beg (save-excursion
2986 (skip-chars-backward "^ ")
2988 (pattern (buffer-substring beg end))
2989 (name-part (file-name-nondirectory pattern))
2990 (directory (file-name-directory pattern))
2991 (completion (file-name-completion name-part directory)))
2992 (cond ((eq completion t))
2994 (message "Can't find completion for \"%s\"" pattern)
2996 ((not (string= name-part completion))
2997 (delete-region beg end)
2998 (insert (expand-file-name completion directory)))
3000 (message "Making completion list...")
3001 (let ((list (file-name-all-completions name-part directory)))
3002 (setq list (sort list 'string<))
3003 (with-output-to-temp-buffer "*Completions*"
3004 (display-completion-list list)))
3005 (message "Making completion list...%s" "done")))))
3007 (defun widget-file-prompt-value (widget prompt value unbound)
3008 ;; Read file from minibuffer.
3009 (abbreviate-file-name
3011 (read-file-name prompt)
3012 (let ((prompt2 (format "%s (default %s) " prompt value))
3013 (dir (file-name-directory value))
3014 (file (file-name-nondirectory value))
3015 (must-match (widget-get widget :must-match)))
3016 (read-file-name prompt2 dir nil must-match file)))))
3018 ;;;(defun widget-file-action (widget &optional event)
3019 ;;; ;; Read a file name from the minibuffer.
3020 ;;; (let* ((value (widget-value widget))
3021 ;;; (dir (file-name-directory value))
3022 ;;; (file (file-name-nondirectory value))
3023 ;;; (menu-tag (widget-apply widget :menu-tag-get))
3024 ;;; (must-match (widget-get widget :must-match))
3025 ;;; (answer (read-file-name (concat menu-tag ": (default `" value "') ")
3026 ;;; dir nil must-match file)))
3027 ;;; (widget-value-set widget (abbreviate-file-name answer))
3029 ;;; (widget-apply widget :notify widget event)))
3031 (define-widget 'directory 'file
3032 "A directory widget.
3033 It will read a directory name from the minibuffer when invoked."
3036 (defvar widget-symbol-prompt-value-history nil
3037 "History of input to `widget-symbol-prompt-value'.")
3039 (define-widget 'symbol 'editable-field
3043 :format "%{%t%}: %v"
3044 :match (lambda (widget value) (symbolp value))
3045 :complete-function 'lisp-complete-symbol
3046 :prompt-internal 'widget-symbol-prompt-internal
3047 :prompt-match 'symbolp
3048 :prompt-history 'widget-symbol-prompt-value-history
3049 :value-to-internal (lambda (widget value)
3053 :value-to-external (lambda (widget value)
3058 (defun widget-symbol-prompt-internal (widget prompt initial history)
3059 ;; Read file from minibuffer.
3060 (let ((answer (completing-read prompt obarray
3061 (widget-get widget :prompt-match)
3062 nil initial history)))
3063 (if (and (stringp answer)
3064 (not (zerop (length answer))))
3066 (error "No value"))))
3068 (defvar widget-function-prompt-value-history nil
3069 "History of input to `widget-function-prompt-value'.")
3071 (define-widget 'function 'sexp
3073 :complete-function 'lisp-complete-symbol
3074 :prompt-value 'widget-field-prompt-value
3075 :prompt-internal 'widget-symbol-prompt-internal
3076 :prompt-match 'fboundp
3077 :prompt-history 'widget-function-prompt-value-history
3078 :action 'widget-field-action
3081 (defvar widget-variable-prompt-value-history nil
3082 "History of input to `widget-variable-prompt-value'.")
3084 (define-widget 'variable 'symbol
3085 ;; Should complete on variables.
3087 :prompt-match 'boundp
3088 :prompt-history 'widget-variable-prompt-value-history
3091 (defvar widget-coding-system-prompt-value-history nil
3092 "History of input to `widget-coding-system-prompt-value'.")
3094 (define-widget 'coding-system 'symbol
3095 "A MULE coding-system."
3096 :format "%{%t%}: %v"
3097 :tag "Coding system"
3098 :prompt-history 'widget-coding-system-prompt-value-history
3099 :prompt-value 'widget-coding-system-prompt-value
3100 :action 'widget-coding-system-action)
3102 (defun widget-coding-system-prompt-value (widget prompt value unbound)
3103 ;; Read coding-system from minibuffer.
3105 (completing-read (format "%s (default %s) " prompt value)
3108 (list (symbol-name sym))
3110 (coding-system-list)))))
3112 (defun widget-coding-system-action (widget &optional event)
3113 ;; Read a file name from the minibuffer.
3115 (widget-coding-system-prompt-value
3117 (widget-apply widget :menu-tag-get)
3118 (widget-value widget)
3120 (widget-value-set widget answer)
3121 (widget-apply widget :notify widget event)
3124 (define-widget 'sexp 'editable-field
3125 "An arbitrary Lisp expression."
3126 :tag "Lisp expression"
3127 :format "%{%t%}: %v"
3129 :validate 'widget-sexp-validate
3130 :match (lambda (widget value) t)
3131 :value-to-internal 'widget-sexp-value-to-internal
3132 :value-to-external (lambda (widget value) (read value))
3133 :prompt-history 'widget-sexp-prompt-value-history
3134 :prompt-value 'widget-sexp-prompt-value)
3136 (defun widget-sexp-value-to-internal (widget value)
3137 ;; Use pp for printer representation.
3138 (let ((pp (if (symbolp value)
3139 (prin1-to-string value)
3140 (pp-to-string value))))
3141 (while (string-match "\n\\'" pp)
3142 (setq pp (substring pp 0 -1)))
3143 (if (or (string-match "\n\\'" pp)
3148 (defun widget-sexp-validate (widget)
3149 ;; Valid if we can read the string and there is no junk left after it.
3151 (let ((buffer (set-buffer (get-buffer-create " *Widget Scratch*"))))
3153 (insert (widget-apply widget :value-get))
3154 (goto-char (point-min))
3155 (condition-case data
3156 (let ((value (read buffer)))
3158 (if (widget-apply widget :match value)
3160 (widget-put widget :error (widget-get widget :type-error))
3163 :error (format "Junk at end of expression: %s"
3164 (buffer-substring (point)
3167 (error (widget-put widget :error (error-message-string data))
3170 (defvar widget-sexp-prompt-value-history nil
3171 "History of input to `widget-sexp-prompt-value'.")
3173 (defun widget-sexp-prompt-value (widget prompt value unbound)
3174 ;; Read an arbitrary sexp.
3175 (let ((found (read-string prompt
3176 (if unbound nil (cons (prin1-to-string value) 0))
3177 (widget-get widget :prompt-history))))
3179 (let ((buffer (set-buffer (get-buffer-create " *Widget Scratch*"))))
3182 (goto-char (point-min))
3183 (let ((answer (read buffer)))
3185 (error "Junk at end of expression: %s"
3186 (buffer-substring (point) (point-max))))
3189 (define-widget 'restricted-sexp 'sexp
3190 "A Lisp expression restricted to values that match.
3191 To use this type, you must define :match or :match-alternatives."
3192 :type-error "The specified value is not valid"
3193 :match 'widget-restricted-sexp-match
3194 :value-to-internal (lambda (widget value)
3195 (if (widget-apply widget :match value)
3196 (prin1-to-string value)
3199 (defun widget-restricted-sexp-match (widget value)
3200 (let ((alternatives (widget-get widget :match-alternatives))
3202 (while (and alternatives (not matched))
3203 (if (cond ((functionp (car alternatives))
3204 (funcall (car alternatives) value))
3205 ((and (consp (car alternatives))
3206 (eq (car (car alternatives)) 'quote))
3207 (eq value (nth 1 (car alternatives)))))
3209 (setq alternatives (cdr alternatives)))
3212 (define-widget 'integer 'restricted-sexp
3216 :type-error "This field should contain an integer"
3217 :match-alternatives '(integerp))
3219 (define-widget 'number 'restricted-sexp
3220 "A floating point number."
3223 :type-error "This field should contain a number"
3224 :match-alternatives '(numberp))
3226 (define-widget 'character 'editable-field
3231 :format "%{%t%}: %v\n"
3232 :valid-regexp "\\`.\\'"
3233 :error "This field should contain a single character"
3234 :value-to-internal (lambda (widget value)
3237 (char-to-string value)))
3238 :value-to-external (lambda (widget value)
3242 :match (lambda (widget value)
3243 (if (fboundp 'characterp)
3247 (define-widget 'list 'group
3250 :format "%{%t%}:\n%v")
3252 (define-widget 'vector 'group
3255 :format "%{%t%}:\n%v"
3256 :match 'widget-vector-match
3257 :value-to-internal (lambda (widget value) (append value nil))
3258 :value-to-external (lambda (widget value) (apply 'vector value)))
3260 (defun widget-vector-match (widget value)
3261 (and (vectorp value)
3262 (widget-group-match widget
3263 (widget-apply widget :value-to-internal value))))
3265 (define-widget 'cons 'group
3268 :format "%{%t%}:\n%v"
3269 :match 'widget-cons-match
3270 :value-to-internal (lambda (widget value)
3271 (list (car value) (cdr value)))
3272 :value-to-external (lambda (widget value)
3273 (cons (nth 0 value) (nth 1 value))))
3275 (defun widget-cons-match (widget value)
3277 (widget-group-match widget
3278 (widget-apply widget :value-to-internal value))))
3280 ;;; The `plist' Widget.
3284 (define-widget 'plist 'list
3286 :key-type '(symbol :tag "Key")
3287 :value-type '(sexp :tag "Value")
3288 :convert-widget 'widget-plist-convert-widget
3291 (defvar widget-plist-value-type) ;Dynamic variable
3293 (defun widget-plist-convert-widget (widget)
3294 ;; Handle `:options'.
3295 (let* ((options (widget-get widget :options))
3296 (key-type (widget-get widget :key-type))
3297 (widget-plist-value-type (widget-get widget :value-type))
3298 (other `(editable-list :inline t
3301 ,widget-plist-value-type)))
3303 (list `(checklist :inline t
3305 ,@(mapcar 'widget-plist-convert-option
3309 (widget-put widget :args args)
3312 (defun widget-plist-convert-option (option)
3313 ;; Convert a single plist option.
3314 (let (key-type value-type)
3316 (let ((key (nth 0 option)))
3317 (setq value-type (nth 1 option))
3320 (setq key-type `(const ,key))))
3321 (setq key-type `(const ,option)
3322 value-type widget-plist-value-type))
3323 `(group :format "Key: %v" :inline t ,key-type ,value-type)))
3326 ;;; The `alist' Widget.
3328 ;; Association lists.
3330 (define-widget 'alist 'list
3331 "An association list."
3332 :key-type '(sexp :tag "Key")
3333 :value-type '(sexp :tag "Value")
3334 :convert-widget 'widget-alist-convert-widget
3337 (defvar widget-alist-value-type) ;Dynamic variable
3339 (defun widget-alist-convert-widget (widget)
3340 ;; Handle `:options'.
3341 (let* ((options (widget-get widget :options))
3342 (key-type (widget-get widget :key-type))
3343 (widget-alist-value-type (widget-get widget :value-type))
3344 (other `(editable-list :inline t
3347 ,widget-alist-value-type)))
3349 (list `(checklist :inline t
3351 ,@(mapcar 'widget-alist-convert-option
3355 (widget-put widget :args args)
3358 (defun widget-alist-convert-option (option)
3359 ;; Convert a single alist option.
3360 (let (key-type value-type)
3362 (let ((key (nth 0 option)))
3363 (setq value-type (nth 1 option))
3366 (setq key-type `(const ,key))))
3367 (setq key-type `(const ,option)
3368 value-type widget-alist-value-type))
3369 `(cons :format "Key: %v" ,key-type ,value-type)))
3371 (define-widget 'choice 'menu-choice
3372 "A union of several sexp types."
3374 :format "%{%t%}: %[Value Menu%] %v"
3375 :button-prefix 'widget-push-button-prefix
3376 :button-suffix 'widget-push-button-suffix
3377 :prompt-value 'widget-choice-prompt-value)
3379 (defun widget-choice-prompt-value (widget prompt value unbound)
3381 (let ((args (widget-get widget :args))
3382 (completion-ignore-case (widget-get widget :case-fold))
3383 current choices old)
3384 ;; Find the first arg that match VALUE.
3387 (if (widget-apply (car look) :match value)
3388 (setq old (car look)
3390 (setq look (cdr look)))))
3393 (cond ((= (length args) 0)
3395 ((= (length args) 1)
3397 ((and (= (length args) 2)
3399 (if (eq old (nth 0 args))
3404 (setq current (car args)
3407 (cons (cons (widget-apply current :menu-tag-get)
3410 (let ((val (completing-read prompt choices nil t)))
3412 (let ((try (try-completion val choices)))
3415 (cdr (assoc val choices)))
3418 (widget-prompt-value current prompt nil t)
3421 (define-widget 'radio 'radio-button-choice
3422 "A union of several sexp types."
3424 :format "%{%t%}:\n%v"
3425 :prompt-value 'widget-choice-prompt-value)
3427 (define-widget 'repeat 'editable-list
3428 "A variable length homogeneous list."
3430 :format "%{%t%}:\n%v%i\n")
3432 (define-widget 'set 'checklist
3433 "A list of members from a fixed set."
3435 :format "%{%t%}:\n%v")
3437 (define-widget 'boolean 'toggle
3438 "To be nil or non-nil, that is the question."
3440 :prompt-value 'widget-boolean-prompt-value
3441 :button-prefix 'widget-push-button-prefix
3442 :button-suffix 'widget-push-button-suffix
3443 :format "%{%t%}: %[Toggle%] %v\n"
3447 (defun widget-boolean-prompt-value (widget prompt value unbound)
3448 ;; Toggle a boolean.
3451 ;;; The `color' Widget.
3453 (define-widget 'color 'editable-field
3454 "Choose a color name (with sample)."
3455 :format "%t: %v (%{sample%})\n"
3459 :complete 'widget-color-complete
3460 :sample-face-get 'widget-color-sample-face-get
3461 :notify 'widget-color-notify
3462 :action 'widget-color-action)
3464 (defun widget-color-complete (widget)
3465 "Complete the color in WIDGET."
3466 (let* ((prefix (buffer-substring-no-properties (widget-field-start widget)
3468 (list (widget-color-choice-list))
3469 (completion (try-completion prefix list)))
3470 (cond ((eq completion t)
3471 (message "Exact match."))
3473 (error "Can't find completion for \"%s\"" prefix))
3474 ((not (string-equal prefix completion))
3475 (insert-and-inherit (substring completion (length prefix))))
3477 (message "Making completion list...")
3478 (let ((list (all-completions prefix list nil)))
3479 (with-output-to-temp-buffer "*Completions*"
3480 (display-completion-list list)))
3481 (message "Making completion list...done")))))
3483 (defun widget-color-sample-face-get (widget)
3484 (let* ((value (condition-case nil
3485 (widget-value widget)
3486 (error (widget-get widget :value))))
3487 (symbol (intern (concat "fg:" value))))
3489 (facemenu-get-face symbol)
3492 (defvar widget-color-choice-list nil)
3493 ;; Variable holding the possible colors.
3495 (defun widget-color-choice-list ()
3496 (unless widget-color-choice-list
3497 (setq widget-color-choice-list
3498 (mapcar '(lambda (color) (list color))
3499 (x-defined-colors))))
3500 widget-color-choice-list)
3502 (defvar widget-color-history nil
3503 "History of entered colors")
3505 (defun widget-color-action (widget &optional event)
3506 ;; Prompt for a color.
3507 (let* ((tag (widget-apply widget :menu-tag-get))
3508 (prompt (concat tag ": "))
3509 (value (widget-value widget))
3510 (start (widget-field-start widget))
3511 (pos (cond ((< (point) start)
3513 ((> (point) (+ start (length value)))
3516 (- (point) start))))
3517 (answer (if (commandp 'read-color)
3519 (completing-read (concat tag ": ")
3520 (widget-color-choice-list)
3523 'widget-color-history))))
3524 (unless (zerop (length answer))
3525 (widget-value-set widget answer)
3527 (widget-apply widget :notify widget event))))
3529 (defun widget-color-notify (widget child &optional event)
3530 "Update the sample, and notofy the parent."
3531 (overlay-put (widget-get widget :sample-overlay)
3532 'face (widget-apply widget :sample-face-get))
3533 (widget-default-notify widget child event))
3537 (defun widget-at (pos)
3538 "The button or field at POS."
3539 (or (get-char-property pos 'button)
3540 (get-char-property pos 'field)))
3542 (defun widget-echo-help (pos)
3543 "Display the help echo for widget at POS."
3544 (let* ((widget (widget-at pos))
3545 (help-echo (and widget (widget-get widget :help-echo))))
3546 (cond ((stringp help-echo)
3547 (message "%s" help-echo))
3548 ((and (symbolp help-echo) (fboundp help-echo)
3549 (stringp (setq help-echo (funcall help-echo widget))))
3550 (message "%s" help-echo)))))
3556 ;; wid-edit.el ends here