1 ;;; wid-edit.el --- Functions for creating and using widgets -*-byte-compile-dynamic: t;-*-
3 ;; Copyright (C) 1996, 1997, 1999, 2000 Free Software Foundation, Inc.
5 ;; Author: Per Abrahamsen <abraham@dina.kvl.dk>
7 ;; Keywords: extensions
9 ;; This file is part of GNU Emacs.
11 ;; GNU Emacs is free software; you can redistribute it and/or modify
12 ;; it under the terms of the GNU General Public License as published by
13 ;; the Free Software Foundation; either version 2, or (at your option)
16 ;; GNU Emacs is distributed in the hope that it will be useful,
17 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
18 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 ;; GNU General Public License for more details.
21 ;; You should have received a copy of the GNU General Public License
22 ;; along with GNU Emacs; see the file COPYING. If not, write to the
23 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
24 ;; Boston, MA 02111-1307, USA.
34 (defun widget-event-point (event)
35 "Character position of the end of event if that exists, or nil."
36 (posn-point (event-end event)))
38 (autoload 'pp-to-string "pp")
39 (autoload 'Info-goto-node "info")
41 (defun widget-button-release-event-p (event)
42 "Non-nil if EVENT is a mouse-button-release event object."
44 (memq (event-basic-type event) '(mouse-1 mouse-2 mouse-3))
45 (or (memq 'click (event-modifiers event))
46 (memq 'drag (event-modifiers event)))))
51 "Customization support for the Widget Library."
52 :link '(custom-manual "(widget)Top")
53 :link '(emacs-library-link :tag "Lisp File" "widget.el")
58 (defgroup widget-documentation nil
59 "Options controling the display of documentation strings."
62 (defgroup widget-faces nil
63 "Faces used by the widget library."
67 (defvar widget-documentation-face 'widget-documentation-face
68 "Face used for documentation strings in widgets.
69 This exists as a variable so it can be set locally in certain buffers.")
71 (defface widget-documentation-face '((((class color)
73 (:foreground "lime green"))
76 (:foreground "dark green"))
78 "Face used for documentation text."
79 :group 'widget-documentation
82 (defvar widget-button-face 'widget-button-face
83 "Face used for buttons in widgets.
84 This exists as a variable so it can be set locally in certain buffers.")
86 (defface widget-button-face '((t (:bold t)))
87 "Face used for widget buttons."
90 (defcustom widget-mouse-face 'highlight
91 "Face used for widget buttons when the mouse is above them."
95 (defface widget-field-face '((((class grayscale color)
97 (:background "gray85"))
98 (((class grayscale color)
100 (:background "dim gray"))
103 "Face used for editable fields."
104 :group 'widget-faces)
106 (defface widget-single-line-field-face '((((class grayscale color)
108 (:background "gray85"))
109 (((class grayscale color)
111 (:background "dim gray"))
114 "Face used for editable fields spanning only a single line."
115 :group 'widget-faces)
117 ;;; This causes display-table to be loaded, and not usefully.
118 ;;;(defvar widget-single-line-display-table
119 ;;; (let ((table (make-display-table)))
120 ;;; (aset table 9 "^I")
121 ;;; (aset table 10 "^J")
123 ;;; "Display table used for single-line editable fields.")
125 ;;;(when (fboundp 'set-face-display-table)
126 ;;; (set-face-display-table 'widget-single-line-field-face
127 ;;; widget-single-line-display-table))
129 ;;; Utility functions.
131 ;; These are not really widget specific.
133 (defun widget-princ-to-string (object)
134 "Return string representation of OBJECT, any Lisp object.
135 No quoting characters are used; no delimiters are printed around
136 the contents of strings."
137 (with-output-to-string
140 (defun widget-clear-undo ()
141 "Clear all undo information."
142 (buffer-disable-undo (current-buffer))
143 (buffer-enable-undo))
145 (defcustom widget-menu-max-size 40
146 "Largest number of items allowed in a popup-menu.
147 Larger menus are read through the minibuffer."
151 (defcustom widget-menu-max-shortcuts 40
152 "Largest number of items for which it works to choose one with a character.
153 For a larger number of items, the minibuffer is used."
157 (defcustom widget-menu-minibuffer-flag nil
158 "*Control how to ask for a choice from the keyboard.
159 Non-nil means use the minibuffer;
160 nil means read a single character."
164 (defun widget-choose (title items &optional event)
165 "Choose an item from a list.
167 First argument TITLE is the name of the list.
168 Second argument ITEMS is an list whose members are either
169 (NAME . VALUE), to indicate selectable items, or just strings to
170 indicate unselectable items.
171 Optional third argument EVENT is an input event.
173 The user is asked to choose between each NAME from the items alist,
174 and the VALUE of the chosen element will be returned. If EVENT is a
175 mouse event, and the number of elements in items is less than
176 `widget-menu-max-size', a popup menu will be used, otherwise the
178 (cond ((and (< (length items) widget-menu-max-size)
179 event (fboundp 'x-popup-menu) window-system)
180 ;; We are in Emacs-19, pressed by the mouse
182 (list title (cons "" items))))
183 ((or widget-menu-minibuffer-flag
184 (> (length items) widget-menu-max-shortcuts))
185 ;; Read the choice of name from the minibuffer.
186 (setq items (widget-remove-if 'stringp items))
187 (let ((val (completing-read (concat title ": ") items nil t)))
189 (let ((try (try-completion val items)))
192 (cdr (assoc val items))))))
194 ;; Construct a menu of the choices
195 ;; and then use it for prompting for a single character.
196 (let* ((overriding-terminal-local-map
197 (make-sparse-keymap))
198 map choice (next-digit ?0)
201 ;; Define SPC as a prefix char to get to this menu.
202 (define-key overriding-terminal-local-map " "
203 (setq map (make-sparse-keymap title)))
205 (set-buffer (get-buffer-create " widget-choose"))
207 (insert "Available choices:\n\n")
209 (setq choice (car items) items (cdr items))
211 (let* ((name (car choice))
212 (function (cdr choice)))
213 (insert (format "%c = %s\n" next-digit name))
214 (define-key map (vector next-digit) function)
215 (setq some-choice-enabled t)))
216 ;; Allocate digits to disabled alternatives
217 ;; so that the digit of a given alternative never varies.
218 (setq next-digit (1+ next-digit)))
219 (insert "\nC-g = Quit"))
220 (or some-choice-enabled
221 (error "None of the choices is currently meaningful"))
222 (define-key map [?\C-g] 'keyboard-quit)
223 (define-key map [t] 'keyboard-quit)
224 (define-key map [?\M-\C-v] 'scroll-other-window)
225 (define-key map [?\M--] 'negative-argument)
226 (setcdr map (nreverse (cdr map)))
227 ;; Read a char with the menu, and return the result
228 ;; that corresponds to it.
229 (save-window-excursion
230 (let ((buf (get-buffer " widget-choose")))
232 (let ((cursor-in-echo-area t)
236 (while (not (or (and (>= char ?0) (< char next-digit))
237 (eq value 'keyboard-quit)))
238 ;; Unread a SPC to lead to our new menu.
239 (setq unread-command-events (cons ?\ unread-command-events))
240 (setq keys (read-key-sequence title))
242 (lookup-key overriding-terminal-local-map keys t)
243 char (string-to-char (substring keys 1)))
244 (cond ((eq value 'scroll-other-window)
245 (let ((minibuffer-scroll-window
246 (get-buffer-window buf)))
248 (scroll-other-window-down
249 (window-height minibuffer-scroll-window))
250 (scroll-other-window))
252 ((eq value 'negative-argument)
256 (when (eq value 'keyboard-quit)
260 (defun widget-remove-if (predictate list)
261 (let (result (tail list))
263 (or (funcall predictate (car tail))
264 (setq result (cons (car tail) result)))
265 (setq tail (cdr tail)))
268 ;;; Widget text specifications.
270 ;; These functions are for specifying text properties.
272 (defvar widget-field-add-space t
273 "Non-nil means add extra space at the end of editable text fields.
274 If you don't add the space, it will become impossible to edit a zero
277 (defvar widget-field-use-before-change t
278 "Non-nil means use `before-change-functions' to track editable fields.
279 This enables the use of undo, but doesn't work on Emacs 19.34 and earlier.
280 Using before hooks also means that the :notify function can't know the
283 (defun widget-specify-field (widget from to)
284 "Specify editable button for WIDGET between FROM and TO."
285 ;; Terminating space is not part of the field, but necessary in
286 ;; order for local-map to work. Remove next sexp if local-map works
287 ;; at the end of the overlay.
290 (cond ((null (widget-get widget :size))
292 (widget-field-add-space
293 (insert-and-inherit " ")))
295 (let ((map (widget-get widget :keymap))
296 (face (or (widget-get widget :value-face) 'widget-field-face))
297 (help-echo (widget-get widget :help-echo))
298 (overlay (make-overlay from to nil
299 nil (or (not widget-field-add-space)
300 (widget-get widget :size)))))
301 (widget-put widget :field-overlay overlay)
302 ;;(overlay-put overlay 'detachable nil)
303 (overlay-put overlay 'field widget)
304 (overlay-put overlay 'keymap map)
305 (overlay-put overlay 'face face)
306 ;;(overlay-put overlay 'balloon-help help-echo)
307 (if (stringp help-echo)
308 (overlay-put overlay 'help-echo help-echo)))
309 (widget-specify-secret widget))
311 (defun widget-specify-secret (field)
312 "Replace text in FIELD with value of `:secret', if non-nil."
313 (let ((secret (widget-get field :secret))
314 (size (widget-get field :size)))
316 (let ((begin (widget-field-start field))
317 (end (widget-field-end field)))
319 (while (and (> end begin)
320 (eq (char-after (1- end)) ?\ ))
321 (setq end (1- end))))
323 (let ((old (char-after begin)))
324 (unless (eq old secret)
325 (subst-char-in-region begin (1+ begin) old secret)
326 (put-text-property begin (1+ begin) 'secret old))
327 (setq begin (1+ begin))))))))
329 (defun widget-specify-button (widget from to)
330 "Specify button for WIDGET between FROM and TO."
331 (let ((face (widget-apply widget :button-face-get))
332 (help-echo (widget-get widget :help-echo))
333 (overlay (make-overlay from to nil t nil)))
334 (widget-put widget :button-overlay overlay)
335 (overlay-put overlay 'button widget)
336 ;; We want to avoid the face with image buttons.
337 (unless (widget-get widget :suppress-face)
338 (overlay-put overlay 'face face)
339 (overlay-put overlay 'mouse-face widget-mouse-face))
340 ;;(overlay-put overlay 'balloon-help help-echo)
341 (if (stringp help-echo)
342 (overlay-put overlay 'help-echo help-echo))
343 (overlay-put overlay 'face face)))
345 (defun widget-specify-sample (widget from to)
346 "Specify sample for WIDGET between FROM and TO."
347 (let ((face (widget-apply widget :sample-face-get))
348 (overlay (make-overlay from to nil t nil)))
349 (overlay-put overlay 'face face)
350 (widget-put widget :sample-overlay overlay)))
352 (defun widget-specify-doc (widget from to)
353 "Specify documentation for WIDGET between FROM and TO."
354 (let ((overlay (make-overlay from to nil t nil)))
355 (overlay-put overlay 'widget-doc widget)
356 (overlay-put overlay 'face widget-documentation-face)
357 (widget-put widget :doc-overlay overlay)))
359 (defmacro widget-specify-insert (&rest form)
360 "Execute FORM without inheriting any text properties."
362 (let ((inhibit-read-only t)
364 before-change-functions
365 after-change-functions)
367 (narrow-to-region (- (point) 2) (point))
368 (goto-char (1+ (point-min)))
369 (setq result (progn ,@form))
370 (delete-region (point-min) (1+ (point-min)))
371 (delete-region (1- (point-max)) (point-max))
372 (goto-char (point-max))
375 (defface widget-inactive-face '((((class grayscale color)
377 (:foreground "light gray"))
378 (((class grayscale color)
380 (:foreground "dim gray"))
383 "Face used for inactive widgets."
384 :group 'widget-faces)
386 (defun widget-specify-inactive (widget from to)
387 "Make WIDGET inactive for user modifications."
388 (unless (widget-get widget :inactive)
389 (let ((overlay (make-overlay from to nil t nil)))
390 (overlay-put overlay 'face 'widget-inactive-face)
391 ;; This is disabled, as it makes the mouse cursor change shape.
392 ;; (overlay-put overlay 'mouse-face 'widget-inactive-face)
393 (overlay-put overlay 'evaporate t)
394 (overlay-put overlay 'priority 100)
395 (overlay-put overlay 'modification-hooks '(widget-overlay-inactive))
396 (widget-put widget :inactive overlay))))
398 (defun widget-overlay-inactive (&rest junk)
399 "Ignoring the arguments, signal an error."
400 (unless inhibit-read-only
401 (error "The widget here is not active")))
404 (defun widget-specify-active (widget)
405 "Make WIDGET active for user modifications."
406 (let ((inactive (widget-get widget :inactive)))
408 (delete-overlay inactive)
409 (widget-put widget :inactive nil))))
411 ;;; Widget Properties.
413 (defsubst widget-type (widget)
414 "Return the type of WIDGET, a symbol."
417 (defun widget-get-indirect (widget property)
418 "In WIDGET, get the value of PROPERTY.
419 If the value is a symbol, return its binding.
420 Otherwise, just return the value."
421 (let ((value (widget-get widget property)))
426 (defun widget-member (widget property)
427 "Non-nil iff there is a definition in WIDGET for PROPERTY."
428 (cond ((widget-plist-member (cdr widget) property)
431 (widget-member (get (car widget) 'widget-type) property))
434 (defun widget-value (widget)
435 "Extract the current value of WIDGET."
437 :value-to-external (widget-apply widget :value-get)))
439 (defun widget-value-set (widget value)
440 "Set the current value of WIDGET to VALUE."
442 :value-set (widget-apply widget
443 :value-to-internal value)))
445 (defun widget-default-get (widget)
446 "Extract the default value of WIDGET."
447 (or (widget-get widget :value)
448 (widget-apply widget :default-get)))
450 (defun widget-match-inline (widget vals)
451 "In WIDGET, match the start of VALS."
452 (cond ((widget-get widget :inline)
453 (widget-apply widget :match-inline vals))
455 (widget-apply widget :match (car vals)))
456 (cons (list (car vals)) (cdr vals)))
459 (defun widget-apply-action (widget &optional event)
460 "Apply :action in WIDGET in response to EVENT."
461 (if (widget-apply widget :active)
462 (widget-apply widget :action event)
463 (error "Attempt to perform action on inactive widget")))
465 ;;; Helper functions.
467 ;; These are widget specific.
470 (defun widget-prompt-value (widget prompt &optional value unbound)
471 "Prompt for a value matching WIDGET, using PROMPT.
472 The current value is assumed to be VALUE, unless UNBOUND is non-nil."
473 (unless (listp widget)
474 (setq widget (list widget)))
475 (setq prompt (format "[%s] %s" (widget-type widget) prompt))
476 (setq widget (widget-convert widget))
477 (let ((answer (widget-apply widget :prompt-value prompt value unbound)))
478 (unless (widget-apply widget :match answer)
479 (error "Value does not match %S type" (car widget)))
482 (defun widget-get-sibling (widget)
483 "Get the item WIDGET is assumed to toggle.
484 This is only meaningful for radio buttons or checkboxes in a list."
485 (let* ((parent (widget-get widget :parent))
486 (children (widget-get parent :children))
490 (setq child (car children)
491 children (cdr children))
492 (when (eq (widget-get child :button) widget)
493 (throw 'child child)))
496 (defun widget-map-buttons (function &optional buffer maparg)
497 "Map FUNCTION over the buttons in BUFFER.
498 FUNCTION is called with the arguments WIDGET and MAPARG.
500 If FUNCTION returns non-nil, the walk is cancelled.
502 The arguments MAPARG, and BUFFER default to nil and (current-buffer),
504 (let ((cur (point-min))
508 (save-excursion (set-buffer buffer) (overlay-lists))
510 (setq overlays (append (car overlays) (cdr overlays)))
511 (while (setq cur (pop overlays))
512 (setq widget (overlay-get cur 'button))
513 (if (and widget (funcall function widget maparg))
514 (setq overlays nil)))))
518 (defcustom widget-image-directory (file-name-as-directory
519 (expand-file-name "custom" data-directory))
520 "Where widget button images are located.
521 If this variable is nil, widget will try to locate the directory
526 (defcustom widget-image-enable t
527 "If non nil, use image buttons in widgets when available."
532 (defcustom widget-image-conversion
533 '((xpm ".xpm") (gif ".gif") (png ".png") (jpeg ".jpg" ".jpeg")
535 "Conversion alist from image formats to file name suffixes."
537 :type '(repeat (cons :format "%v"
538 (symbol :tag "Image Format" unknown)
539 (repeat :tag "Suffixes"
540 (string :format "%v")))))
542 (defun widget-image-find (image)
543 "Create a graphical button from IMAGE.
544 IMAGE should either already be an image, or be a file name sans
545 extension (xpm, xbm, gif, jpg, or png) located in
546 `widget-image-directory' or otherwise where `find-image' will find it."
547 (cond ((not (and image widget-image-enable (display-graphic-p)))
548 ;; We don't want or can't use images.
551 (eq 'image (car image)))
552 ;; Already an image spec. Use it.
555 ;; A string. Look it up in relevant directories.
556 (let* ((load-path (cons widget-image-directory load-path))
557 (formats widget-image-conversion)
559 (dolist (elt widget-image-conversion)
560 (dolist (ext (cdr elt))
561 (push (list :type (car elt) :file (concat image ext)) specs)))
562 (setq specs (nreverse specs))
568 (defvar widget-button-pressed-face 'widget-button-pressed-face
569 "Face used for pressed buttons in widgets.
570 This exists as a variable so it can be set locally in certain
573 (defun widget-image-insert (widget tag image &optional down inactive)
574 "In WIDGET, insert the text TAG or, if supported, IMAGE.
575 IMAGE should either be an image or an image file name sans extension
576 \(xpm, xbm, gif, jpg, or png) located in `widget-image-directory'.
578 Optional arguments DOWN and INACTIVE are used instead of IMAGE when the
579 button is pressed or inactive, respectively. These are currently ignored."
580 (if (and (display-graphic-p)
581 (setq image (widget-image-find image)))
582 (progn (widget-put widget :suppress-face t)
585 tag 'mouse-face widget-button-pressed-face)))
590 (defgroup widget-button nil
591 "The look of various kinds of buttons."
594 (defcustom widget-button-prefix ""
595 "String used as prefix for buttons."
597 :group 'widget-button)
599 (defcustom widget-button-suffix ""
600 "String used as suffix for buttons."
602 :group 'widget-button)
604 ;;; Creating Widgets.
607 (defun widget-create (type &rest args)
608 "Create widget of TYPE.
609 The optional ARGS are additional keyword arguments."
610 (let ((widget (apply 'widget-convert type args)))
611 (widget-apply widget :create)
614 (defun widget-create-child-and-convert (parent type &rest args)
615 "As part of the widget PARENT, create a child widget TYPE.
616 The child is converted, using the keyword arguments ARGS."
617 (let ((widget (apply 'widget-convert type args)))
618 (widget-put widget :parent parent)
619 (unless (widget-get widget :indent)
620 (widget-put widget :indent (+ (or (widget-get parent :indent) 0)
621 (or (widget-get widget :extra-offset) 0)
622 (widget-get parent :offset))))
623 (widget-apply widget :create)
626 (defun widget-create-child (parent type)
627 "Create widget of TYPE."
628 (let ((widget (copy-sequence type)))
629 (widget-put widget :parent parent)
630 (unless (widget-get widget :indent)
631 (widget-put widget :indent (+ (or (widget-get parent :indent) 0)
632 (or (widget-get widget :extra-offset) 0)
633 (widget-get parent :offset))))
634 (widget-apply widget :create)
637 (defun widget-create-child-value (parent type value)
638 "Create widget of TYPE with value VALUE."
639 (let ((widget (copy-sequence type)))
640 (widget-put widget :value (widget-apply widget :value-to-internal value))
641 (widget-put widget :parent parent)
642 (unless (widget-get widget :indent)
643 (widget-put widget :indent (+ (or (widget-get parent :indent) 0)
644 (or (widget-get widget :extra-offset) 0)
645 (widget-get parent :offset))))
646 (widget-apply widget :create)
650 (defun widget-delete (widget)
652 (widget-apply widget :delete))
654 (defun widget-convert (type &rest args)
655 "Convert TYPE to a widget without inserting it in the buffer.
656 The optional ARGS are additional keyword arguments."
657 ;; Don't touch the type.
658 (let* ((widget (if (symbolp type)
660 (copy-sequence type)))
663 ;; First set the :args keyword.
664 (while (cdr current) ;Look in the type.
665 (let ((next (car (cdr current))))
667 (setq current (cdr (cdr current)))
668 (setcdr current (list :args (cdr current)))
669 (setq current nil))))
670 (while args ;Look in the args.
671 (let ((next (nth 0 args)))
673 (setq args (nthcdr 2 args))
674 (widget-put widget :args args)
676 ;; Then Convert the widget.
679 (let ((convert-widget (plist-get (cdr type) :convert-widget)))
681 (setq widget (funcall convert-widget widget))))
682 (setq type (get (car type) 'widget-type)))
683 ;; Finally set the keyword args.
685 (let ((next (nth 0 keys)))
688 (widget-put widget next (nth 1 keys))
689 (setq keys (nthcdr 2 keys)))
691 ;; Convert the :value to internal format.
692 (if (widget-member widget :value)
693 (let ((value (widget-get widget :value)))
695 :value (widget-apply widget :value-to-internal value))))
696 ;; Return the newly create widget.
699 (defun widget-insert (&rest args)
700 "Call `insert' with ARGS and make the text read only."
701 (let ((inhibit-read-only t)
702 before-change-functions
703 after-change-functions
705 (apply 'insert args)))
707 (defun widget-convert-text (type from to
708 &optional button-from button-to
710 "Return a widget of type TYPE with endpoint FROM TO.
711 Optional ARGS are extra keyword arguments for TYPE.
712 and TO will be used as the widgets end points. If optional arguments
713 BUTTON-FROM and BUTTON-TO are given, these will be used as the widgets
715 Optional ARGS are extra keyword arguments for TYPE."
716 (let ((widget (apply 'widget-convert type :delete 'widget-leave-text args))
717 (from (copy-marker from))
718 (to (copy-marker to)))
719 (set-marker-insertion-type from t)
720 (set-marker-insertion-type to nil)
721 (widget-put widget :from from)
722 (widget-put widget :to to)
724 (widget-specify-button widget button-from button-to))
727 (defun widget-convert-button (type from to &rest args)
728 "Return a widget of type TYPE with endpoint FROM TO.
729 Optional ARGS are extra keyword arguments for TYPE.
730 No text will be inserted to the buffer, instead the text between FROM
731 and TO will be used as the widgets end points, as well as the widgets
733 (apply 'widget-convert-text type from to from to args))
735 (defun widget-leave-text (widget)
736 "Remove markers and overlays from WIDGET and its children."
737 (let ((from (widget-get widget :from))
738 (to (widget-get widget :to))
739 (button (widget-get widget :button-overlay))
740 (sample (widget-get widget :sample-overlay))
741 (doc (widget-get widget :doc-overlay))
742 (field (widget-get widget :field-overlay))
743 (children (widget-get widget :children)))
744 (set-marker from nil)
747 (delete-overlay button))
749 (delete-overlay sample))
751 (delete-overlay doc))
753 (delete-overlay field))
754 (mapc 'widget-leave-text children)))
756 ;;; Keymap and Commands.
758 (defvar widget-keymap
759 (let ((map (make-sparse-keymap)))
760 (define-key map "\t" 'widget-forward)
761 (define-key map [(shift tab)] 'widget-backward)
762 (define-key map [backtab] 'widget-backward)
763 (define-key map [down-mouse-2] 'widget-button-click)
764 (define-key map "\C-m" 'widget-button-press)
766 "Keymap containing useful binding for buffers containing widgets.
767 Recommended as a parent keymap for modes using widgets.")
769 (defvar widget-global-map global-map
770 "Keymap used for events the widget does not handle themselves.")
771 (make-variable-buffer-local 'widget-global-map)
773 (defvar widget-field-keymap
774 (let ((map (copy-keymap widget-keymap)))
775 (define-key map [menu-bar] nil)
776 (define-key map "\C-k" 'widget-kill-line)
777 (define-key map "\M-\t" 'widget-complete)
778 (define-key map "\C-m" 'widget-field-activate)
779 (define-key map "\C-a" 'widget-beginning-of-line)
780 (define-key map "\C-e" 'widget-end-of-line)
781 (set-keymap-parent map global-map)
783 "Keymap used inside an editable field.")
785 (defvar widget-text-keymap
786 (let ((map (copy-keymap widget-keymap)))
787 (define-key map [menu-bar] 'nil)
788 (define-key map "\C-a" 'widget-beginning-of-line)
789 (define-key map "\C-e" 'widget-end-of-line)
790 (set-keymap-parent map global-map)
792 "Keymap used inside a text field.")
794 (defun widget-field-activate (pos &optional event)
795 "Invoke the ediable field at point."
797 (let ((field (get-char-property pos 'field)))
799 (widget-apply-action field event)
801 (lookup-key widget-global-map (this-command-keys))))))
803 (defface widget-button-pressed-face
807 (:bold t :underline t)))
808 "Face used for pressed buttons."
809 :group 'widget-faces)
811 (defun widget-button-click (event)
812 "Invoke the button that the mouse is pointing at."
814 (if (widget-event-point event)
816 (mouse-set-point event)
817 (let* ((pos (widget-event-point event))
818 (button (get-char-property pos 'button)))
820 (let* ((overlay (widget-get button :button-overlay))
821 (face (overlay-get overlay 'face))
822 (mouse-face (overlay-get overlay 'mouse-face)))
824 (let ((track-mouse t))
826 (when face ; avoid changing around image
828 'face widget-button-pressed-face)
830 'mouse-face widget-button-pressed-face))
831 (unless (widget-apply button :mouse-down-action event)
832 (while (not (widget-button-release-event-p event))
833 (setq event (read-event)
834 pos (widget-event-point event))
836 (eq (get-char-property pos 'button)
841 widget-button-pressed-face)
844 widget-button-pressed-face))
845 (overlay-put overlay 'face face)
846 (overlay-put overlay 'mouse-face mouse-face))))
848 (eq (get-char-property pos 'button) button))
849 (widget-apply-action button event))))
850 (overlay-put overlay 'face face)
851 (overlay-put overlay 'mouse-face mouse-face)))
854 ;; Find the global command to run, and check whether it
855 ;; is bound to an up event.
856 (if (memq (event-basic-type event) '(mouse-1 down-mouse-1))
857 (cond ((setq command ;down event
858 (lookup-key widget-global-map [down-mouse-1]))
860 ((setq command ;up event
861 (lookup-key widget-global-map [mouse-1]))))
862 (cond ((setq command ;down event
863 (lookup-key widget-global-map [down-mouse-2]))
865 ((setq command ;up event
866 (lookup-key widget-global-map [mouse-2])))))
868 ;; Don't execute up events twice.
869 (while (not (widget-button-release-event-p event))
870 (setq event (read-event))))
872 (call-interactively command)))))
873 (unless (pos-visible-in-window-p (widget-event-point event))
874 (mouse-set-point event)
877 (message "You clicked somewhere weird.")))
879 (defun widget-button-press (pos &optional event)
880 "Invoke button at POS."
882 (let ((button (get-char-property pos 'button)))
884 (widget-apply-action button event)
885 (let ((command (lookup-key widget-global-map (this-command-keys))))
886 (when (commandp command)
887 (call-interactively command))))))
889 (defun widget-tabable-at (&optional pos)
890 "Return the tabable widget at POS, or nil.
891 POS defaults to the value of (point)."
894 (let ((widget (or (get-char-property pos 'button)
895 (get-char-property pos 'field))))
897 (let ((order (widget-get widget :tab-order)))
903 (defvar widget-use-overlay-change t
904 "If non-nil, use overlay change functions to tab around in the buffer.
905 This is much faster, but doesn't work reliably on Emacs 19.34.")
907 (defun widget-move (arg)
908 "Move point to the ARG next field or button.
909 ARG may be negative to move backward."
910 (or (bobp) (> arg 0) (backward-char))
913 (old (widget-tabable-at))
918 (goto-char (point-min)))
919 (widget-use-overlay-change
920 (goto-char (next-overlay-change (point))))
923 (and (eq pos (point))
925 (error "No buttons or fields found"))
926 (let ((new (widget-tabable-at)))
934 (goto-char (point-max)))
935 (widget-use-overlay-change
936 (goto-char (previous-overlay-change (point))))
939 (and (eq pos (point))
941 (error "No buttons or fields found"))
942 (let ((new (widget-tabable-at)))
945 (setq arg (1+ arg))))))
946 (let ((new (widget-tabable-at)))
947 (while (eq (widget-tabable-at) new)
950 (widget-echo-help (point))
951 (run-hooks 'widget-move-hook))
953 (defun widget-forward (arg)
954 "Move point to the next field or button.
955 With optional ARG, move across that many fields."
957 (run-hooks 'widget-forward-hook)
960 (defun widget-backward (arg)
961 "Move point to the previous field or button.
962 With optional ARG, move across that many fields."
964 (run-hooks 'widget-backward-hook)
965 (widget-move (- arg)))
967 (defun widget-beginning-of-line ()
968 "Go to beginning of field or beginning of line, whichever is first."
970 (let* ((field (widget-field-find (point)))
971 (start (and field (widget-field-start field)))
972 (bol (line-beginning-position)))
977 (defun widget-end-of-line ()
978 "Go to end of field or end of line, whichever is first."
980 (let* ((field (widget-field-find (point)))
981 (end (and field (widget-field-end field)))
982 (eol (line-end-position)))
987 (defun widget-kill-line ()
988 "Kill to end of field or end of line, whichever is first."
990 (let* ((field (widget-field-find (point)))
991 (newline (save-excursion (forward-line 1) (point)))
992 (end (and field (widget-field-end field))))
993 (if (and field (> newline end))
994 (kill-region (point) end)
995 (call-interactively 'kill-line))))
997 (defcustom widget-complete-field (lookup-key global-map "\M-\t")
998 "Default function to call for completion inside fields."
999 :options '(ispell-complete-word complete-tag lisp-complete-symbol)
1003 (defun widget-complete ()
1004 "Complete content of editable field from point.
1005 When not inside a field, move to the previous button or field."
1007 (let ((field (widget-field-find (point))))
1009 (widget-apply field :complete)
1010 (error "Not in an editable field"))))
1012 ;;; Setting up the buffer.
1014 (defvar widget-field-new nil)
1015 ;; List of all newly created editable fields in the buffer.
1016 (make-variable-buffer-local 'widget-field-new)
1018 (defvar widget-field-list nil)
1019 ;; List of all editable fields in the buffer.
1020 (make-variable-buffer-local 'widget-field-list)
1022 (defun widget-setup ()
1023 "Setup current buffer so editing string widgets works."
1024 (let ((inhibit-read-only t)
1025 (after-change-functions nil)
1026 before-change-functions
1028 (while widget-field-new
1029 (setq field (car widget-field-new)
1030 widget-field-new (cdr widget-field-new)
1031 widget-field-list (cons field widget-field-list))
1032 (let ((from (car (widget-get field :field-overlay)))
1033 (to (cdr (widget-get field :field-overlay))))
1034 (widget-specify-field field
1035 (marker-position from) (marker-position to))
1036 (set-marker from nil)
1037 (set-marker to nil))))
1039 (widget-add-change))
1041 (defvar widget-field-last nil)
1042 ;; Last field containing point.
1043 (make-variable-buffer-local 'widget-field-last)
1045 (defvar widget-field-was nil)
1046 ;; The widget data before the change.
1047 (make-variable-buffer-local 'widget-field-was)
1049 (defun widget-field-buffer (widget)
1050 "Return the start of WIDGET's editing field."
1051 (let ((overlay (widget-get widget :field-overlay)))
1052 (and overlay (overlay-buffer overlay))))
1054 (defun widget-field-start (widget)
1055 "Return the start of WIDGET's editing field."
1056 (let ((overlay (widget-get widget :field-overlay)))
1057 (and overlay (overlay-start overlay))))
1059 (defun widget-field-end (widget)
1060 "Return the end of WIDGET's editing field."
1061 (let ((overlay (widget-get widget :field-overlay)))
1062 ;; Don't subtract one if local-map works at the end of the overlay.
1063 (and overlay (if (or widget-field-add-space
1064 (null (widget-get widget :size)))
1065 (1- (overlay-end overlay))
1066 (overlay-end overlay)))))
1068 (defun widget-field-find (pos)
1069 "Return the field at POS.
1070 Unlike (get-char-property POS 'field) this, works with empty fields too."
1071 (let ((fields widget-field-list)
1074 (setq field (car fields)
1075 fields (cdr fields))
1076 (let ((start (widget-field-start field))
1077 (end (widget-field-end field)))
1078 (when (and (<= start pos) (<= pos end))
1080 (debug "Overlapping fields"))
1081 (setq found field))))
1084 (defun widget-before-change (from to)
1085 ;; This is how, for example, a variable changes its state to `modified'.
1086 ;; when it is being edited.
1087 (unless inhibit-read-only
1088 (let ((from-field (widget-field-find from))
1089 (to-field (widget-field-find to)))
1090 (cond ((not (eq from-field to-field))
1091 (add-hook 'post-command-hook 'widget-add-change nil t)
1092 (signal 'text-read-only
1093 '("Change should be restricted to a single field")))
1095 (add-hook 'post-command-hook 'widget-add-change nil t)
1096 (signal 'text-read-only
1097 '("Attempt to change text outside editable field")))
1098 (widget-field-use-before-change
1100 (widget-apply from-field :notify from-field)
1101 (error (debug "Before Change"))))))))
1103 (defun widget-add-change ()
1104 (make-local-hook 'post-command-hook)
1105 (remove-hook 'post-command-hook 'widget-add-change t)
1106 (make-local-hook 'before-change-functions)
1107 (add-hook 'before-change-functions 'widget-before-change nil t)
1108 (make-local-hook 'after-change-functions)
1109 (add-hook 'after-change-functions 'widget-after-change nil t))
1111 (defun widget-after-change (from to old)
1112 "Adjust field size and text properties."
1114 (let ((field (widget-field-find from))
1115 (other (widget-field-find to)))
1117 (unless (eq field other)
1118 (debug "Change in different fields"))
1119 (let ((size (widget-get field :size)))
1121 (let ((begin (widget-field-start field))
1122 (end (widget-field-end field)))
1123 (cond ((< (- end begin) size)
1127 (insert-char ?\ (- (+ begin size) end))))
1128 ((> (- end begin) size)
1129 ;; Field too large and
1130 (if (or (< (point) (+ begin size))
1132 ;; Point is outside extra space.
1133 (setq begin (+ begin size))
1134 ;; Point is within the extra space.
1135 (setq begin (point)))
1138 (while (and (eq (preceding-char) ?\ )
1140 (delete-backward-char 1)))))))
1141 (widget-specify-secret field))
1142 (widget-apply field :notify field)))
1143 (error (debug "After Change"))))
1145 ;;; Widget Functions
1147 ;; These functions are used in the definition of multiple widgets.
1149 (defun widget-parent-action (widget &optional event)
1150 "Tell :parent of WIDGET to handle the :action.
1151 Optional EVENT is the event that triggered the action."
1152 (widget-apply (widget-get widget :parent) :action event))
1154 (defun widget-children-value-delete (widget)
1155 "Delete all :children and :buttons in WIDGET."
1156 (mapc 'widget-delete (widget-get widget :children))
1157 (widget-put widget :children nil)
1158 (mapc 'widget-delete (widget-get widget :buttons))
1159 (widget-put widget :buttons nil))
1161 (defun widget-children-validate (widget)
1162 "All the :children must be valid."
1163 (let ((children (widget-get widget :children))
1165 (while (and children (not found))
1166 (setq child (car children)
1167 children (cdr children)
1168 found (widget-apply child :validate)))
1171 (defun widget-types-convert-widget (widget)
1172 "Convert :args as widget types in WIDGET."
1173 (widget-put widget :args (mapcar 'widget-convert (widget-get widget :args)))
1176 (defun widget-value-convert-widget (widget)
1177 "Initialize :value from :args in WIDGET."
1178 (let ((args (widget-get widget :args)))
1180 (widget-put widget :value (car args))
1181 ;; Don't convert :value here, as this is done in `widget-convert'.
1182 ;; (widget-put widget :value (widget-apply widget
1183 ;; :value-to-internal (car args)))
1184 (widget-put widget :args nil)))
1187 (defun widget-value-value-get (widget)
1188 "Return the :value property of WIDGET."
1189 (widget-get widget :value))
1191 ;;; The `default' Widget.
1193 (define-widget 'default nil
1194 "Basic widget other widgets are derived from."
1195 :value-to-internal (lambda (widget value) value)
1196 :value-to-external (lambda (widget value) value)
1197 :button-prefix 'widget-button-prefix
1198 :button-suffix 'widget-button-suffix
1199 :complete 'widget-default-complete
1200 :create 'widget-default-create
1203 :format-handler 'widget-default-format-handler
1204 :button-face-get 'widget-default-button-face-get
1205 :sample-face-get 'widget-default-sample-face-get
1206 :delete 'widget-default-delete
1207 :value-set 'widget-default-value-set
1208 :value-inline 'widget-default-value-inline
1209 :default-get 'widget-default-default-get
1210 :menu-tag-get 'widget-default-menu-tag-get
1212 :active 'widget-default-active
1213 :activate 'widget-specify-active
1214 :deactivate 'widget-default-deactivate
1215 :mouse-down-action #'ignore
1216 :action 'widget-default-action
1217 :notify 'widget-default-notify
1218 :prompt-value 'widget-default-prompt-value)
1220 (defun widget-default-complete (widget)
1221 "Call the value of the :complete-function property of WIDGET.
1222 If that does not exists, call the value of `widget-complete-field'."
1223 (let ((fun (widget-get widget :complete-function)))
1224 (call-interactively (or fun widget-complete-field))))
1226 (defun widget-default-create (widget)
1227 "Create WIDGET at point in the current buffer."
1228 (widget-specify-insert
1229 (let ((from (point))
1230 button-begin button-end
1231 sample-begin sample-end
1234 (insert (widget-get widget :format))
1236 ;; Parse escapes in format.
1237 (while (re-search-forward "%\\(.\\)" nil t)
1238 (let ((escape (aref (match-string 1) 0)))
1239 (replace-match "" t t)
1240 (cond ((eq escape ?%)
1243 (setq button-begin (point))
1244 (insert (widget-get-indirect widget :button-prefix)))
1246 (insert (widget-get-indirect widget :button-suffix))
1247 (setq button-end (point)))
1249 (setq sample-begin (point)))
1251 (setq sample-end (point)))
1253 (when (widget-get widget :indent)
1255 (insert-char ? (widget-get widget :indent))))
1257 (let ((image (widget-get widget :tag-glyph))
1258 (tag (widget-get widget :tag)))
1260 (widget-image-insert widget (or tag "image") image))
1264 (princ (widget-get widget :value)
1265 (current-buffer))))))
1267 (let ((doc (widget-get widget :doc)))
1269 (setq doc-begin (point))
1271 (while (eq (preceding-char) ?\n)
1272 (delete-backward-char 1))
1274 (setq doc-end (point)))))
1276 (if (and button-begin (not button-end))
1277 (widget-apply widget :value-create)
1278 (setq value-pos (point))))
1280 (widget-apply widget :format-handler escape)))))
1281 ;; Specify button, sample, and doc, and insert value.
1282 (and button-begin button-end
1283 (widget-specify-button widget button-begin button-end))
1284 (and sample-begin sample-end
1285 (widget-specify-sample widget sample-begin sample-end))
1286 (and doc-begin doc-end
1287 (widget-specify-doc widget doc-begin doc-end))
1289 (goto-char value-pos)
1290 (widget-apply widget :value-create)))
1291 (let ((from (copy-marker (point-min)))
1292 (to (copy-marker (point-max))))
1293 (set-marker-insertion-type from t)
1294 (set-marker-insertion-type to nil)
1295 (widget-put widget :from from)
1296 (widget-put widget :to to)))
1297 (widget-clear-undo))
1299 (defun widget-default-format-handler (widget escape)
1300 ;; We recognize the %h escape by default.
1301 (let* ((buttons (widget-get widget :buttons)))
1302 (cond ((eq escape ?h)
1303 (let* ((doc-property (widget-get widget :documentation-property))
1304 (doc-try (cond ((widget-get widget :doc))
1305 ((symbolp doc-property)
1306 (documentation-property
1307 (widget-get widget :value)
1310 (funcall doc-property
1311 (widget-get widget :value)))))
1312 (doc-text (and (stringp doc-try)
1313 (> (length doc-try) 1)
1315 (doc-indent (widget-get widget :documentation-indent)))
1317 (and (eq (preceding-char) ?\n)
1318 (widget-get widget :indent)
1319 (insert-char ? (widget-get widget :indent)))
1320 ;; The `*' in the beginning is redundant.
1321 (when (eq (aref doc-text 0) ?*)
1322 (setq doc-text (substring doc-text 1)))
1323 ;; Get rid of trailing newlines.
1324 (when (string-match "\n+\\'" doc-text)
1325 (setq doc-text (substring doc-text 0 (match-beginning 0))))
1326 (push (widget-create-child-and-convert
1327 widget 'documentation-string
1328 :indent (cond ((numberp doc-indent )
1336 (error "Unknown escape `%c'" escape)))
1337 (widget-put widget :buttons buttons)))
1339 (defun widget-default-button-face-get (widget)
1340 ;; Use :button-face or widget-button-face
1341 (or (widget-get widget :button-face)
1342 (let ((parent (widget-get widget :parent)))
1344 (widget-apply parent :button-face-get)
1345 widget-button-face))))
1347 (defun widget-default-sample-face-get (widget)
1348 ;; Use :sample-face.
1349 (widget-get widget :sample-face))
1351 (defun widget-default-delete (widget)
1352 "Remove widget from the buffer."
1353 (let ((from (widget-get widget :from))
1354 (to (widget-get widget :to))
1355 (inactive-overlay (widget-get widget :inactive))
1356 (button-overlay (widget-get widget :button-overlay))
1357 (sample-overlay (widget-get widget :sample-overlay))
1358 (doc-overlay (widget-get widget :doc-overlay))
1359 before-change-functions
1360 after-change-functions
1361 (inhibit-read-only t))
1362 (widget-apply widget :value-delete)
1363 (when inactive-overlay
1364 (delete-overlay inactive-overlay))
1365 (when button-overlay
1366 (delete-overlay button-overlay))
1367 (when sample-overlay
1368 (delete-overlay sample-overlay))
1370 (delete-overlay doc-overlay))
1372 ;; Kludge: this doesn't need to be true for empty formats.
1373 (delete-region from to))
1374 (set-marker from nil)
1375 (set-marker to nil))
1376 (widget-clear-undo))
1378 (defun widget-default-value-set (widget value)
1379 "Recreate widget with new value."
1380 (let* ((old-pos (point))
1381 (from (copy-marker (widget-get widget :from)))
1382 (to (copy-marker (widget-get widget :to)))
1383 (offset (if (and (<= from old-pos) (<= old-pos to))
1384 (if (>= old-pos (1- to))
1386 (- old-pos from)))))
1387 ;;??? Bug: this ought to insert the new value before deleting the old one,
1388 ;; so that markers on either side of the value automatically
1389 ;; stay on the same side. -- rms.
1391 (goto-char (widget-get widget :from))
1392 (widget-apply widget :delete)
1393 (widget-put widget :value value)
1394 (widget-apply widget :create))
1397 (goto-char (+ (widget-get widget :to) offset 1))
1398 (goto-char (min (+ from offset) (1- (widget-get widget :to))))))))
1400 (defun widget-default-value-inline (widget)
1401 "Wrap value in a list unless it is inline."
1402 (if (widget-get widget :inline)
1403 (widget-value widget)
1404 (list (widget-value widget))))
1406 (defun widget-default-default-get (widget)
1408 (widget-get widget :value))
1410 (defun widget-default-menu-tag-get (widget)
1411 "Use tag or value for menus."
1412 (or (widget-get widget :menu-tag)
1413 (widget-get widget :tag)
1414 (widget-princ-to-string (widget-get widget :value))))
1416 (defun widget-default-active (widget)
1417 "Return t iff this widget active (user modifiable)."
1418 (or (widget-get widget :always-active)
1419 (and (not (widget-get widget :inactive))
1420 (let ((parent (widget-get widget :parent)))
1422 (widget-apply parent :active))))))
1424 (defun widget-default-deactivate (widget)
1425 "Make WIDGET inactive for user modifications."
1426 (widget-specify-inactive widget
1427 (widget-get widget :from)
1428 (widget-get widget :to)))
1430 (defun widget-default-action (widget &optional event)
1431 "Notify the parent when a widget changes."
1432 (let ((parent (widget-get widget :parent)))
1434 (widget-apply parent :notify widget event))))
1436 (defun widget-default-notify (widget child &optional event)
1437 "Pass notification to parent."
1438 (widget-default-action widget event))
1440 (defun widget-default-prompt-value (widget prompt value unbound)
1441 "Read an arbitrary value. Stolen from `set-variable'."
1442 ;; (let ((initial (if unbound
1444 ;; It would be nice if we could do a `(cons val 1)' here.
1445 ;; (prin1-to-string (custom-quote value))))))
1446 (eval-minibuffer prompt ))
1448 ;;; The `item' Widget.
1450 (define-widget 'item 'default
1451 "Constant items for inclusion in other widgets."
1452 :convert-widget 'widget-value-convert-widget
1453 :value-create 'widget-item-value-create
1454 :value-delete 'ignore
1455 :value-get 'widget-value-value-get
1456 :match 'widget-item-match
1457 :match-inline 'widget-item-match-inline
1458 :action 'widget-item-action
1461 (defun widget-item-value-create (widget)
1462 "Insert the printed representation of the value."
1463 (princ (widget-get widget :value) (current-buffer)))
1465 (defun widget-item-match (widget value)
1466 ;; Match if the value is the same.
1467 (equal (widget-get widget :value) value))
1469 (defun widget-item-match-inline (widget values)
1470 ;; Match if the value is the same.
1471 (let ((value (widget-get widget :value)))
1473 (<= (length value) (length values))
1474 (let ((head (widget-sublist values 0 (length value))))
1475 (and (equal head value)
1476 (cons head (widget-sublist values (length value))))))))
1478 (defun widget-sublist (list start &optional end)
1479 "Return the sublist of LIST from START to END.
1480 If END is omitted, it defaults to the length of LIST."
1481 (if (> start 0) (setq list (nthcdr start list)))
1483 (unless (<= end start)
1484 (setq list (copy-sequence list))
1485 (setcdr (nthcdr (- end start 1) list) nil)
1487 (copy-sequence list)))
1489 (defun widget-item-action (widget &optional event)
1490 ;; Just notify itself.
1491 (widget-apply widget :notify widget event))
1493 ;;; The `push-button' Widget.
1495 (defcustom widget-push-button-gui t
1496 "If non nil, use GUI push buttons when available."
1500 ;; Cache already created GUI objects.
1501 (defvar widget-push-button-cache nil)
1503 (defcustom widget-push-button-prefix "["
1504 "String used as prefix for buttons."
1506 :group 'widget-button)
1508 (defcustom widget-push-button-suffix "]"
1509 "String used as suffix for buttons."
1511 :group 'widget-button)
1513 (define-widget 'push-button 'item
1514 "A pushable button."
1517 :value-create 'widget-push-button-value-create
1520 (defun widget-push-button-value-create (widget)
1521 "Insert text representing the `on' and `off' states."
1522 (let* ((tag (or (widget-get widget :tag)
1523 (widget-get widget :value)))
1524 (tag-glyph (widget-get widget :tag-glyph))
1525 (text (concat widget-push-button-prefix
1526 tag widget-push-button-suffix))
1527 (gui (cdr (assoc tag widget-push-button-cache))))
1529 (widget-image-insert widget text tag-glyph))
1533 (defun widget-gui-action (widget)
1534 "Apply :action for WIDGET."
1535 (widget-apply-action widget (this-command-keys)))
1537 ;;; The `link' Widget.
1539 (defcustom widget-link-prefix "["
1540 "String used as prefix for links."
1542 :group 'widget-button)
1544 (defcustom widget-link-suffix "]"
1545 "String used as suffix for links."
1547 :group 'widget-button)
1549 (define-widget 'link 'item
1551 :button-prefix 'widget-link-prefix
1552 :button-suffix 'widget-link-suffix
1553 :help-echo "Follow the link."
1556 ;;; The `info-link' Widget.
1558 (define-widget 'info-link 'link
1559 "A link to an info file."
1560 :action 'widget-info-link-action)
1562 (defun widget-info-link-action (widget &optional event)
1563 "Open the info node specified by WIDGET."
1564 (Info-goto-node (widget-value widget)))
1566 ;;; The `url-link' Widget.
1568 (define-widget 'url-link 'link
1569 "A link to an www page."
1570 :action 'widget-url-link-action)
1572 (defun widget-url-link-action (widget &optional event)
1573 "Open the url specified by WIDGET."
1574 (browse-url (widget-value widget)))
1576 ;;; The `function-link' Widget.
1578 (define-widget 'function-link 'link
1579 "A link to an Emacs function."
1580 :action 'widget-function-link-action)
1582 (defun widget-function-link-action (widget &optional event)
1583 "Show the function specified by WIDGET."
1584 (describe-function (widget-value widget)))
1586 ;;; The `variable-link' Widget.
1588 (define-widget 'variable-link 'link
1589 "A link to an Emacs variable."
1590 :action 'widget-variable-link-action)
1592 (defun widget-variable-link-action (widget &optional event)
1593 "Show the variable specified by WIDGET."
1594 (describe-variable (widget-value widget)))
1596 ;;; The `file-link' Widget.
1598 (define-widget 'file-link 'link
1600 :action 'widget-file-link-action)
1602 (defun widget-file-link-action (widget &optional event)
1603 "Find the file specified by WIDGET."
1604 (find-file (widget-value widget)))
1606 ;;; The `emacs-library-link' Widget.
1608 (define-widget 'emacs-library-link 'link
1609 "A link to an Emacs Lisp library file."
1610 :action 'widget-emacs-library-link-action)
1612 (defun widget-emacs-library-link-action (widget &optional event)
1613 "Find the Emacs Library file specified by WIDGET."
1614 (find-file (locate-library (widget-value widget))))
1616 ;;; The `emacs-commentary-link' Widget.
1618 (define-widget 'emacs-commentary-link 'link
1619 "A link to Commentary in an Emacs Lisp library file."
1620 :action 'widget-emacs-commentary-link-action)
1622 (defun widget-emacs-commentary-link-action (widget &optional event)
1623 "Find the Commentary section of the Emacs file specified by WIDGET."
1624 (finder-commentary (widget-value widget)))
1626 ;;; The `editable-field' Widget.
1628 (define-widget 'editable-field 'default
1629 "An editable text field."
1630 :convert-widget 'widget-value-convert-widget
1631 :keymap widget-field-keymap
1634 :prompt-internal 'widget-field-prompt-internal
1635 :prompt-history 'widget-field-history
1636 :prompt-value 'widget-field-prompt-value
1637 :action 'widget-field-action
1638 :validate 'widget-field-validate
1641 :value-create 'widget-field-value-create
1642 :value-delete 'widget-field-value-delete
1643 :value-get 'widget-field-value-get
1644 :match 'widget-field-match)
1646 (defvar widget-field-history nil
1647 "History of field minibuffer edits.")
1649 (defun widget-field-prompt-internal (widget prompt initial history)
1650 "Read string for WIDGET promptinhg with PROMPT.
1651 INITIAL is the initial input and HISTORY is a symbol containing
1653 (read-string prompt initial history))
1655 (defun widget-field-prompt-value (widget prompt value unbound)
1656 "Prompt for a string."
1657 (let ((initial (if unbound
1659 (cons (widget-apply widget :value-to-internal
1661 (history (widget-get widget :prompt-history)))
1662 (let ((answer (widget-apply widget
1663 :prompt-internal prompt initial history)))
1664 (widget-apply widget :value-to-external answer))))
1666 (defvar widget-edit-functions nil)
1668 (defun widget-field-action (widget &optional event)
1669 "Move to next field."
1671 (run-hook-with-args 'widget-edit-functions widget))
1673 (defun widget-field-validate (widget)
1674 "Valid if the content matches `:valid-regexp'."
1676 (let ((value (widget-apply widget :value-get))
1677 (regexp (widget-get widget :valid-regexp)))
1678 (if (string-match regexp value)
1682 (defun widget-field-value-create (widget)
1683 "Create an editable text field."
1684 (let ((size (widget-get widget :size))
1685 (value (widget-get widget :value))
1687 ;; This is changed to a real overlay in `widget-setup'. We
1688 ;; need the end points to behave differently until
1689 ;; `widget-setup' is called.
1690 (overlay (cons (make-marker) (make-marker))))
1691 (widget-put widget :field-overlay overlay)
1694 (< (length value) size)
1695 (insert-char ?\ (- size (length value))))
1696 (unless (memq widget widget-field-list)
1697 (setq widget-field-new (cons widget widget-field-new)))
1698 (move-marker (cdr overlay) (point))
1699 (set-marker-insertion-type (cdr overlay) nil)
1702 (move-marker (car overlay) from)
1703 (set-marker-insertion-type (car overlay) t)))
1705 (defun widget-field-value-delete (widget)
1706 "Remove the widget from the list of active editing fields."
1707 (setq widget-field-list (delq widget widget-field-list))
1708 ;; These are nil if the :format string doesn't contain `%v'.
1709 (let ((overlay (widget-get widget :field-overlay)))
1711 (delete-overlay overlay))))
1713 (defun widget-field-value-get (widget)
1714 "Return current text in editing field."
1715 (let ((from (widget-field-start widget))
1716 (to (widget-field-end widget))
1717 (buffer (widget-field-buffer widget))
1718 (size (widget-get widget :size))
1719 (secret (widget-get widget :secret))
1720 (old (current-buffer)))
1727 (eq (char-after (1- to)) ?\ ))
1729 (let ((result (buffer-substring-no-properties from to)))
1732 (while (< (+ from index) to)
1734 (get-char-property (+ from index) 'secret))
1735 (setq index (1+ index)))))
1738 (widget-get widget :value))))
1740 (defun widget-field-match (widget value)
1741 ;; Match any string.
1744 ;;; The `text' Widget.
1746 (define-widget 'text 'editable-field
1747 :keymap widget-text-keymap
1748 "A multiline text area.")
1750 ;;; The `menu-choice' Widget.
1752 (define-widget 'menu-choice 'default
1753 "A menu of options."
1754 :convert-widget 'widget-types-convert-widget
1755 :format "%[%t%]: %v"
1758 :void '(item :format "invalid (%t)\n")
1759 :value-create 'widget-choice-value-create
1760 :value-delete 'widget-children-value-delete
1761 :value-get 'widget-choice-value-get
1762 :value-inline 'widget-choice-value-inline
1763 :default-get 'widget-choice-default-get
1764 :mouse-down-action 'widget-choice-mouse-down-action
1765 :action 'widget-choice-action
1766 :error "Make a choice"
1767 :validate 'widget-choice-validate
1768 :match 'widget-choice-match
1769 :match-inline 'widget-choice-match-inline)
1771 (defun widget-choice-value-create (widget)
1772 "Insert the first choice that matches the value."
1773 (let ((value (widget-get widget :value))
1774 (args (widget-get widget :args))
1775 (explicit (widget-get widget :explicit-choice))
1776 (explicit-value (widget-get widget :explicit-choice-value))
1778 (if (and explicit (equal value explicit-value))
1780 ;; If the user specified the choice for this value,
1781 ;; respect that choice as long as the value is the same.
1782 (widget-put widget :children (list (widget-create-child-value
1783 widget explicit value)))
1784 (widget-put widget :choice explicit))
1786 (setq current (car args)
1788 (when (widget-apply current :match value)
1789 (widget-put widget :children (list (widget-create-child-value
1790 widget current value)))
1791 (widget-put widget :choice current)
1795 (let ((void (widget-get widget :void)))
1796 (widget-put widget :children (list (widget-create-child-and-convert
1797 widget void :value value)))
1798 (widget-put widget :choice void))))))
1800 (defun widget-choice-value-get (widget)
1801 ;; Get value of the child widget.
1802 (widget-value (car (widget-get widget :children))))
1804 (defun widget-choice-value-inline (widget)
1805 ;; Get value of the child widget.
1806 (widget-apply (car (widget-get widget :children)) :value-inline))
1808 (defun widget-choice-default-get (widget)
1809 ;; Get default for the first choice.
1810 (widget-default-get (car (widget-get widget :args))))
1812 (defcustom widget-choice-toggle nil
1813 "If non-nil, a binary choice will just toggle between the values.
1814 Otherwise, the user will explicitly have to choose between the values
1815 when he invoked the menu."
1819 (defun widget-choice-mouse-down-action (widget &optional event)
1820 ;; Return non-nil if we need a menu.
1821 (let ((args (widget-get widget :args))
1822 (old (widget-get widget :choice)))
1823 (cond ((not window-system)
1824 ;; No place to pop up a menu.
1826 ((not (or (fboundp 'x-popup-menu) (fboundp 'popup-menu)))
1827 ;; No way to pop up a menu.
1829 ((< (length args) 2)
1830 ;; Empty or singleton list, just return the value.
1832 ((> (length args) widget-menu-max-size)
1833 ;; Too long, prompt.
1835 ((> (length args) 2)
1836 ;; Reasonable sized list, use menu.
1838 ((and widget-choice-toggle (memq old args))
1842 ;; Ask which of the two.
1845 (defun widget-choice-action (widget &optional event)
1847 (let ((args (widget-get widget :args))
1848 (old (widget-get widget :choice))
1849 (tag (widget-apply widget :menu-tag-get))
1850 (completion-ignore-case (widget-get widget :case-fold))
1853 ;; Remember old value.
1854 (if (and old (not (widget-apply widget :validate)))
1855 (let* ((external (widget-value widget))
1856 (internal (widget-apply old :value-to-internal external)))
1857 (widget-put old :value internal)))
1860 (cond ((= (length args) 0)
1862 ((= (length args) 1)
1864 ((and widget-choice-toggle
1867 (if (eq old (nth 0 args))
1872 (setq current (car args)
1875 (cons (cons (widget-apply current :menu-tag-get)
1878 (setq this-explicit t)
1879 (widget-choose tag (reverse choices) event))))
1881 ;; If this was an explicit user choice,
1882 ;; record the choice, and the record the value it was made for.
1883 ;; widget-choice-value-create will respect this choice,
1884 ;; as long as the value is the same.
1886 (widget-put widget :explicit-choice current)
1887 (widget-put widget :explicit-choice-value (widget-get widget :value)))
1888 (let ((value (widget-default-get current)))
1889 (widget-value-set widget
1890 (widget-apply current :value-to-external value)))
1892 (widget-apply widget :notify widget event)))
1893 (run-hook-with-args 'widget-edit-functions widget))
1895 (defun widget-choice-validate (widget)
1896 ;; Valid if we have made a valid choice.
1897 (let ((void (widget-get widget :void))
1898 (choice (widget-get widget :choice))
1899 (child (car (widget-get widget :children))))
1900 (if (eq void choice)
1902 (widget-apply child :validate))))
1904 (defun widget-choice-match (widget value)
1905 ;; Matches if one of the choices matches.
1906 (let ((args (widget-get widget :args))
1908 (while (and args (not found))
1909 (setq current (car args)
1911 found (widget-apply current :match value)))
1914 (defun widget-choice-match-inline (widget values)
1915 ;; Matches if one of the choices matches.
1916 (let ((args (widget-get widget :args))
1918 (while (and args (null found))
1919 (setq current (car args)
1921 found (widget-match-inline current values)))
1924 ;;; The `toggle' Widget.
1926 (define-widget 'toggle 'item
1927 "Toggle between two states."
1929 :value-create 'widget-toggle-value-create
1930 :action 'widget-toggle-action
1931 :match (lambda (widget value) t)
1935 (defun widget-toggle-value-create (widget)
1936 "Insert text representing the `on' and `off' states."
1937 (if (widget-value widget)
1938 (widget-image-insert widget
1939 (widget-get widget :on)
1940 (widget-get widget :on-glyph))
1941 (widget-image-insert widget
1942 (widget-get widget :off)
1943 (widget-get widget :off-glyph))))
1945 (defun widget-toggle-action (widget &optional event)
1947 (widget-value-set widget (not (widget-value widget)))
1948 (widget-apply widget :notify widget event)
1949 (run-hook-with-args 'widget-edit-functions widget))
1951 ;;; The `checkbox' Widget.
1953 (define-widget 'checkbox 'toggle
1954 "A checkbox toggle."
1959 :on-glyph (create-image (make-bool-vector 49 1)
1960 'xbm t :width 7 :height 7
1961 :foreground "grey75" ; like default mode line
1962 :relief -3 :ascent 'center)
1964 :off-glyph (create-image (make-bool-vector 49 1)
1965 'xbm t :width 7 :height 7
1966 :foreground "grey75"
1967 :relief 3 :ascent 'center)
1968 :help-echo "Toggle this item."
1969 :action 'widget-checkbox-action)
1971 (defun widget-checkbox-action (widget &optional event)
1972 "Toggle checkbox, notify parent, and set active state of sibling."
1973 (widget-toggle-action widget event)
1974 (let ((sibling (widget-get-sibling widget)))
1976 (if (widget-value widget)
1977 (widget-apply sibling :activate)
1978 (widget-apply sibling :deactivate)))))
1980 ;;; The `checklist' Widget.
1982 (define-widget 'checklist 'default
1983 "A multiple choice widget."
1984 :convert-widget 'widget-types-convert-widget
1987 :entry-format "%b %v"
1988 :menu-tag "checklist"
1990 :value-create 'widget-checklist-value-create
1991 :value-delete 'widget-children-value-delete
1992 :value-get 'widget-checklist-value-get
1993 :validate 'widget-checklist-validate
1994 :match 'widget-checklist-match
1995 :match-inline 'widget-checklist-match-inline)
1997 (defun widget-checklist-value-create (widget)
1998 ;; Insert all values
1999 (let ((alist (widget-checklist-match-find widget (widget-get widget :value)))
2000 (args (widget-get widget :args)))
2002 (widget-checklist-add-item widget (car args) (assq (car args) alist))
2003 (setq args (cdr args)))
2004 (widget-put widget :children (nreverse (widget-get widget :children)))))
2006 (defun widget-checklist-add-item (widget type chosen)
2007 "Create checklist item in WIDGET of type TYPE.
2008 If the item is checked, CHOSEN is a cons whose cdr is the value."
2009 (and (eq (preceding-char) ?\n)
2010 (widget-get widget :indent)
2011 (insert-char ? (widget-get widget :indent)))
2012 (widget-specify-insert
2013 (let* ((children (widget-get widget :children))
2014 (buttons (widget-get widget :buttons))
2015 (button-args (or (widget-get type :sibling-args)
2016 (widget-get widget :button-args)))
2019 (insert (widget-get widget :entry-format))
2021 ;; Parse % escapes in format.
2022 (while (re-search-forward "%\\([bv%]\\)" nil t)
2023 (let ((escape (aref (match-string 1) 0)))
2024 (replace-match "" t t)
2025 (cond ((eq escape ?%)
2028 (setq button (apply 'widget-create-child-and-convert
2030 :value (not (null chosen))
2035 (let ((child (widget-create-child widget type)))
2036 (widget-apply child :deactivate)
2038 ((widget-get type :inline)
2039 (widget-create-child-value
2040 widget type (cdr chosen)))
2042 (widget-create-child-value
2043 widget type (car (cdr chosen)))))))
2045 (error "Unknown escape `%c'" escape)))))
2046 ;; Update properties.
2047 (and button child (widget-put child :button button))
2048 (and button (widget-put widget :buttons (cons button buttons)))
2049 (and child (widget-put widget :children (cons child children))))))
2051 (defun widget-checklist-match (widget values)
2052 ;; All values must match a type in the checklist.
2054 (null (cdr (widget-checklist-match-inline widget values)))))
2056 (defun widget-checklist-match-inline (widget values)
2057 ;; Find the values which match a type in the checklist.
2058 (let ((greedy (widget-get widget :greedy))
2059 (args (copy-sequence (widget-get widget :args)))
2062 (let ((answer (widget-checklist-match-up args values)))
2064 (let ((vals (widget-match-inline answer values)))
2065 (setq found (append found (car vals))
2067 args (delq answer args))))
2069 (setq rest (append rest (list (car values)))
2070 values (cdr values)))
2072 (setq rest (append rest values)
2076 (defun widget-checklist-match-find (widget vals)
2077 "Find the vals which match a type in the checklist.
2078 Return an alist of (TYPE MATCH)."
2079 (let ((greedy (widget-get widget :greedy))
2080 (args (copy-sequence (widget-get widget :args)))
2083 (let ((answer (widget-checklist-match-up args vals)))
2085 (let ((match (widget-match-inline answer vals)))
2086 (setq found (cons (cons answer (car match)) found)
2088 args (delq answer args))))
2090 (setq vals (cdr vals)))
2095 (defun widget-checklist-match-up (args vals)
2096 "Return the first type from ARGS that matches VALS."
2097 (let (current found)
2098 (while (and args (null found))
2099 (setq current (car args)
2101 found (widget-match-inline current vals)))
2105 (defun widget-checklist-value-get (widget)
2106 ;; The values of all selected items.
2107 (let ((children (widget-get widget :children))
2110 (setq child (car children)
2111 children (cdr children))
2112 (if (widget-value (widget-get child :button))
2113 (setq result (append result (widget-apply child :value-inline)))))
2116 (defun widget-checklist-validate (widget)
2117 ;; Ticked chilren must be valid.
2118 (let ((children (widget-get widget :children))
2120 (while (and children (not found))
2121 (setq child (car children)
2122 children (cdr children)
2123 button (widget-get child :button)
2124 found (and (widget-value button)
2125 (widget-apply child :validate))))
2128 ;;; The `option' Widget
2130 (define-widget 'option 'checklist
2131 "An widget with an optional item."
2134 ;;; The `choice-item' Widget.
2136 (define-widget 'choice-item 'item
2137 "Button items that delegate action events to their parents."
2138 :action 'widget-parent-action
2139 :format "%[%t%] \n")
2141 ;;; The `radio-button' Widget.
2143 (define-widget 'radio-button 'toggle
2144 "A radio button for use in the `radio' widget."
2145 :notify 'widget-radio-button-notify
2152 :off-glyph "radio0")
2154 (defun widget-radio-button-notify (widget child &optional event)
2156 (widget-apply (widget-get widget :parent) :action widget event))
2158 ;;; The `radio-button-choice' Widget.
2160 (define-widget 'radio-button-choice 'default
2161 "Select one of multiple options."
2162 :convert-widget 'widget-types-convert-widget
2165 :entry-format "%b %v"
2167 :value-create 'widget-radio-value-create
2168 :value-delete 'widget-children-value-delete
2169 :value-get 'widget-radio-value-get
2170 :value-inline 'widget-radio-value-inline
2171 :value-set 'widget-radio-value-set
2172 :error "You must push one of the buttons"
2173 :validate 'widget-radio-validate
2174 :match 'widget-choice-match
2175 :match-inline 'widget-choice-match-inline
2176 :action 'widget-radio-action)
2178 (defun widget-radio-value-create (widget)
2179 ;; Insert all values
2180 (let ((args (widget-get widget :args))
2183 (setq arg (car args)
2185 (widget-radio-add-item widget arg))))
2187 (defun widget-radio-add-item (widget type)
2188 "Add to radio widget WIDGET a new radio button item of type TYPE."
2189 ;; (setq type (widget-convert type))
2190 (and (eq (preceding-char) ?\n)
2191 (widget-get widget :indent)
2192 (insert-char ? (widget-get widget :indent)))
2193 (widget-specify-insert
2194 (let* ((value (widget-get widget :value))
2195 (children (widget-get widget :children))
2196 (buttons (widget-get widget :buttons))
2197 (button-args (or (widget-get type :sibling-args)
2198 (widget-get widget :button-args)))
2200 (chosen (and (null (widget-get widget :choice))
2201 (widget-apply type :match value)))
2203 (insert (widget-get widget :entry-format))
2205 ;; Parse % escapes in format.
2206 (while (re-search-forward "%\\([bv%]\\)" nil t)
2207 (let ((escape (aref (match-string 1) 0)))
2208 (replace-match "" t t)
2209 (cond ((eq escape ?%)
2212 (setq button (apply 'widget-create-child-and-convert
2213 widget 'radio-button
2214 :value (not (null chosen))
2217 (setq child (if chosen
2218 (widget-create-child-value
2220 (widget-create-child widget type)))
2222 (widget-apply child :deactivate)))
2224 (error "Unknown escape `%c'" escape)))))
2225 ;; Update properties.
2227 (widget-put widget :choice type))
2229 (widget-put child :button button)
2230 (widget-put widget :buttons (nconc buttons (list button))))
2232 (widget-put widget :children (nconc children (list child))))
2235 (defun widget-radio-value-get (widget)
2236 ;; Get value of the child widget.
2237 (let ((chosen (widget-radio-chosen widget)))
2238 (and chosen (widget-value chosen))))
2240 (defun widget-radio-chosen (widget)
2241 "Return the widget representing the chosen radio button."
2242 (let ((children (widget-get widget :children))
2245 (setq current (car children)
2246 children (cdr children))
2247 (let* ((button (widget-get current :button))
2248 (value (widget-apply button :value-get)))
2254 (defun widget-radio-value-inline (widget)
2255 ;; Get value of the child widget.
2256 (let ((children (widget-get widget :children))
2259 (setq current (car children)
2260 children (cdr children))
2261 (let* ((button (widget-get current :button))
2262 (value (widget-apply button :value-get)))
2264 (setq found (widget-apply current :value-inline)
2268 (defun widget-radio-value-set (widget value)
2269 ;; We can't just delete and recreate a radio widget, since children
2270 ;; can be added after the original creation and won't be recreated
2272 (let ((children (widget-get widget :children))
2275 (setq current (car children)
2276 children (cdr children))
2277 (let* ((button (widget-get current :button))
2278 (match (and (not found)
2279 (widget-apply current :match value))))
2280 (widget-value-set button match)
2283 (widget-value-set current value)
2284 (widget-apply current :activate))
2285 (widget-apply current :deactivate))
2286 (setq found (or found match))))))
2288 (defun widget-radio-validate (widget)
2289 ;; Valid if we have made a valid choice.
2290 (let ((children (widget-get widget :children))
2291 current found button)
2292 (while (and children (not found))
2293 (setq current (car children)
2294 children (cdr children)
2295 button (widget-get current :button)
2296 found (widget-apply button :value-get)))
2298 (widget-apply current :validate)
2301 (defun widget-radio-action (widget child event)
2302 ;; Check if a radio button was pressed.
2303 (let ((children (widget-get widget :children))
2304 (buttons (widget-get widget :buttons))
2306 (when (memq child buttons)
2308 (setq current (car children)
2309 children (cdr children))
2310 (let* ((button (widget-get current :button)))
2311 (cond ((eq child button)
2312 (widget-value-set button t)
2313 (widget-apply current :activate))
2314 ((widget-value button)
2315 (widget-value-set button nil)
2316 (widget-apply current :deactivate)))))))
2317 ;; Pass notification to parent.
2318 (widget-apply widget :notify child event))
2320 ;;; The `insert-button' Widget.
2322 (define-widget 'insert-button 'push-button
2323 "An insert button for the `editable-list' widget."
2325 :help-echo "Insert a new item into the list at this position."
2326 :action 'widget-insert-button-action)
2328 (defun widget-insert-button-action (widget &optional event)
2329 ;; Ask the parent to insert a new item.
2330 (widget-apply (widget-get widget :parent)
2331 :insert-before (widget-get widget :widget)))
2333 ;;; The `delete-button' Widget.
2335 (define-widget 'delete-button 'push-button
2336 "A delete button for the `editable-list' widget."
2338 :help-echo "Delete this item from the list."
2339 :action 'widget-delete-button-action)
2341 (defun widget-delete-button-action (widget &optional event)
2342 ;; Ask the parent to insert a new item.
2343 (widget-apply (widget-get widget :parent)
2344 :delete-at (widget-get widget :widget)))
2346 ;;; The `editable-list' Widget.
2348 (defcustom widget-editable-list-gui nil
2349 "If non nil, use GUI push-buttons in editable list when available."
2353 (define-widget 'editable-list 'default
2354 "A variable list of widgets of the same type."
2355 :convert-widget 'widget-types-convert-widget
2358 :format-handler 'widget-editable-list-format-handler
2359 :entry-format "%i %d %v"
2360 :menu-tag "editable-list"
2361 :value-create 'widget-editable-list-value-create
2362 :value-delete 'widget-children-value-delete
2363 :value-get 'widget-editable-list-value-get
2364 :validate 'widget-children-validate
2365 :match 'widget-editable-list-match
2366 :match-inline 'widget-editable-list-match-inline
2367 :insert-before 'widget-editable-list-insert-before
2368 :delete-at 'widget-editable-list-delete-at)
2370 (defun widget-editable-list-format-handler (widget escape)
2371 ;; We recognize the insert button.
2372 (let ((widget-push-button-gui widget-editable-list-gui))
2373 (cond ((eq escape ?i)
2374 (and (widget-get widget :indent)
2375 (insert-char ? (widget-get widget :indent)))
2376 (apply 'widget-create-child-and-convert
2377 widget 'insert-button
2378 (widget-get widget :append-button-args)))
2380 (widget-default-format-handler widget escape)))))
2382 (defun widget-editable-list-value-create (widget)
2383 ;; Insert all values
2384 (let* ((value (widget-get widget :value))
2385 (type (nth 0 (widget-get widget :args)))
2386 (inlinep (widget-get type :inline))
2388 (widget-put widget :value-pos (copy-marker (point)))
2389 (set-marker-insertion-type (widget-get widget :value-pos) t)
2391 (let ((answer (widget-match-inline type value)))
2393 (setq children (cons (widget-editable-list-entry-create
2402 (widget-put widget :children (nreverse children))))
2404 (defun widget-editable-list-value-get (widget)
2405 ;; Get value of the child widget.
2406 (apply 'append (mapcar (lambda (child) (widget-apply child :value-inline))
2407 (widget-get widget :children))))
2409 (defun widget-editable-list-match (widget value)
2410 ;; Value must be a list and all the members must match the type.
2412 (null (cdr (widget-editable-list-match-inline widget value)))))
2414 (defun widget-editable-list-match-inline (widget value)
2415 (let ((type (nth 0 (widget-get widget :args)))
2418 (while (and value ok)
2419 (let ((answer (widget-match-inline type value)))
2421 (setq found (append found (car answer))
2424 (cons found value)))
2426 (defun widget-editable-list-insert-before (widget before)
2427 ;; Insert a new child in the list of children.
2429 (let ((children (widget-get widget :children))
2430 (inhibit-read-only t)
2431 before-change-functions
2432 after-change-functions)
2434 (goto-char (widget-get before :entry-from)))
2436 (goto-char (widget-get widget :value-pos))))
2437 (let ((child (widget-editable-list-entry-create
2439 (when (< (widget-get child :entry-from) (widget-get widget :from))
2440 (set-marker (widget-get widget :from)
2441 (widget-get child :entry-from)))
2442 (if (eq (car children) before)
2443 (widget-put widget :children (cons child children))
2444 (while (not (eq (car (cdr children)) before))
2445 (setq children (cdr children)))
2446 (setcdr children (cons child (cdr children)))))))
2448 (widget-apply widget :notify widget))
2450 (defun widget-editable-list-delete-at (widget child)
2451 ;; Delete child from list of children.
2453 (let ((buttons (copy-sequence (widget-get widget :buttons)))
2455 (inhibit-read-only t)
2456 before-change-functions
2457 after-change-functions)
2459 (setq button (car buttons)
2460 buttons (cdr buttons))
2461 (when (eq (widget-get button :widget) child)
2463 :buttons (delq button (widget-get widget :buttons)))
2464 (widget-delete button))))
2465 (let ((entry-from (widget-get child :entry-from))
2466 (entry-to (widget-get child :entry-to))
2467 (inhibit-read-only t)
2468 before-change-functions
2469 after-change-functions)
2470 (widget-delete child)
2471 (delete-region entry-from entry-to)
2472 (set-marker entry-from nil)
2473 (set-marker entry-to nil))
2474 (widget-put widget :children (delq child (widget-get widget :children))))
2476 (widget-apply widget :notify widget))
2478 (defun widget-editable-list-entry-create (widget value conv)
2479 ;; Create a new entry to the list.
2480 (let ((type (nth 0 (widget-get widget :args)))
2481 (widget-push-button-gui widget-editable-list-gui)
2482 child delete insert)
2483 (widget-specify-insert
2485 (and (widget-get widget :indent)
2486 (insert-char ? (widget-get widget :indent)))
2487 (insert (widget-get widget :entry-format)))
2488 ;; Parse % escapes in format.
2489 (while (re-search-forward "%\\(.\\)" nil t)
2490 (let ((escape (aref (match-string 1) 0)))
2491 (replace-match "" t t)
2492 (cond ((eq escape ?%)
2495 (setq insert (apply 'widget-create-child-and-convert
2496 widget 'insert-button
2497 (widget-get widget :insert-button-args))))
2499 (setq delete (apply 'widget-create-child-and-convert
2500 widget 'delete-button
2501 (widget-get widget :delete-button-args))))
2504 (setq child (widget-create-child-value
2506 (setq child (widget-create-child-value
2508 (widget-apply type :value-to-external
2509 (widget-default-get type))))))
2511 (error "Unknown escape `%c'" escape)))))
2513 :buttons (cons delete
2515 (widget-get widget :buttons))))
2516 (let ((entry-from (copy-marker (point-min)))
2517 (entry-to (copy-marker (point-max))))
2518 (set-marker-insertion-type entry-from t)
2519 (set-marker-insertion-type entry-to nil)
2520 (widget-put child :entry-from entry-from)
2521 (widget-put child :entry-to entry-to)))
2522 (widget-put insert :widget child)
2523 (widget-put delete :widget child)
2526 ;;; The `group' Widget.
2528 (define-widget 'group 'default
2529 "A widget which groups other widgets inside."
2530 :convert-widget 'widget-types-convert-widget
2532 :value-create 'widget-group-value-create
2533 :value-delete 'widget-children-value-delete
2534 :value-get 'widget-editable-list-value-get
2535 :default-get 'widget-group-default-get
2536 :validate 'widget-children-validate
2537 :match 'widget-group-match
2538 :match-inline 'widget-group-match-inline)
2540 (defun widget-group-value-create (widget)
2541 ;; Create each component.
2542 (let ((args (widget-get widget :args))
2543 (value (widget-get widget :value))
2544 arg answer children)
2546 (setq arg (car args)
2548 answer (widget-match-inline arg value)
2550 (and (eq (preceding-char) ?\n)
2551 (widget-get widget :indent)
2552 (insert-char ? (widget-get widget :indent)))
2553 (push (cond ((null answer)
2554 (widget-create-child widget arg))
2555 ((widget-get arg :inline)
2556 (widget-create-child-value widget arg (car answer)))
2558 (widget-create-child-value widget arg (car (car answer)))))
2560 (widget-put widget :children (nreverse children))))
2562 (defun widget-group-default-get (widget)
2563 ;; Get the default of the components.
2564 (mapcar 'widget-default-get (widget-get widget :args)))
2566 (defun widget-group-match (widget values)
2567 ;; Match if the components match.
2569 (let ((match (widget-group-match-inline widget values)))
2570 (and match (null (cdr match))))))
2572 (defun widget-group-match-inline (widget vals)
2573 ;; Match if the components match.
2574 (let ((args (widget-get widget :args))
2575 argument answer found)
2577 (setq argument (car args)
2579 answer (widget-match-inline argument vals))
2581 (setq vals (cdr answer)
2582 found (append found (car answer)))
2586 (cons found vals))))
2588 ;;; The `visibility' Widget.
2590 (define-widget 'visibility 'item
2591 "An indicator and manipulator for hidden items."
2597 :value-create 'widget-visibility-value-create
2598 :action 'widget-toggle-action
2599 :match (lambda (widget value) t))
2601 (defun widget-visibility-value-create (widget)
2602 ;; Insert text representing the `on' and `off' states.
2603 (let ((on (widget-get widget :on))
2604 (off (widget-get widget :off)))
2606 (setq on (concat widget-push-button-prefix
2608 widget-push-button-suffix))
2611 (setq off (concat widget-push-button-prefix
2613 widget-push-button-suffix))
2615 (if (widget-value widget)
2616 (widget-image-insert widget on "down" "down-pushed")
2617 (widget-image-insert widget off "right" "right-pushed"))))
2619 ;;; The `documentation-link' Widget.
2621 ;; This is a helper widget for `documentation-string'.
2623 (define-widget 'documentation-link 'link
2624 "Link type used in documentation strings."
2626 :help-echo "Describe this symbol"
2627 :action 'widget-documentation-link-action)
2629 (defun widget-documentation-link-action (widget &optional event)
2630 "Display documentation for WIDGET's value. Ignore optional argument EVENT."
2631 (let* ((string (widget-get widget :value))
2632 (symbol (intern string)))
2633 (if (and (fboundp symbol) (boundp symbol))
2634 ;; If there are two doc strings, give the user a way to pick one.
2635 (apropos (concat "\\`" (regexp-quote string) "\\'"))
2636 (if (fboundp symbol)
2637 (describe-function symbol)
2638 (describe-variable symbol)))))
2640 (defcustom widget-documentation-links t
2641 "Add hyperlinks to documentation strings when non-nil."
2643 :group 'widget-documentation)
2645 (defcustom widget-documentation-link-regexp "`\\([^\n`' ]+\\)'"
2646 "Regexp for matching potential links in documentation strings.
2647 The first group should be the link itself."
2649 :group 'widget-documentation)
2651 (defcustom widget-documentation-link-p 'intern-soft
2652 "Predicate used to test if a string is useful as a link.
2653 The value should be a function. The function will be called one
2654 argument, a string, and should return non-nil if there should be a
2655 link for that string."
2657 :options '(widget-documentation-link-p)
2658 :group 'widget-documentation)
2660 (defcustom widget-documentation-link-type 'documentation-link
2661 "Widget type used for links in documentation strings."
2663 :group 'widget-documentation)
2665 (defun widget-documentation-link-add (widget from to)
2666 (widget-specify-doc widget from to)
2667 (when widget-documentation-links
2668 (let ((regexp widget-documentation-link-regexp)
2669 (predicate widget-documentation-link-p)
2670 (type widget-documentation-link-type)
2671 (buttons (widget-get widget :buttons))
2672 (widget-mouse-face (default-value 'widget-mouse-face))
2673 (widget-button-face widget-documentation-face)
2674 (widget-button-pressed-face widget-documentation-face))
2677 (while (re-search-forward regexp to t)
2678 (let ((name (match-string 1))
2679 (begin (match-beginning 1))
2680 (end (match-end 1)))
2681 (when (funcall predicate name)
2682 (push (widget-convert-button type begin end :value name)
2684 (widget-put widget :buttons buttons)))
2685 (let ((indent (widget-get widget :indent)))
2686 (when (and indent (not (zerop indent)))
2689 (narrow-to-region from to)
2690 (goto-char (point-min))
2691 (while (search-forward "\n" nil t)
2692 (insert-char ?\ indent)))))))
2694 ;;; The `documentation-string' Widget.
2696 (define-widget 'documentation-string 'item
2697 "A documentation string."
2699 :action 'widget-documentation-string-action
2700 :value-delete 'widget-children-value-delete
2701 :value-create 'widget-documentation-string-value-create)
2703 (defun widget-documentation-string-value-create (widget)
2704 ;; Insert documentation string.
2705 (let ((doc (widget-value widget))
2706 (indent (widget-get widget :indent))
2707 (shown (widget-get (widget-get widget :parent) :documentation-shown))
2709 (if (string-match "\n" doc)
2710 (let ((before (substring doc 0 (match-beginning 0)))
2711 (after (substring doc (match-beginning 0)))
2714 (widget-documentation-link-add widget start (point))
2715 (push (widget-create-child-and-convert
2717 :help-echo "Show or hide rest of the documentation."
2720 :action 'widget-parent-action
2724 (setq start (point))
2725 (when (and indent (not (zerop indent)))
2726 (insert-char ?\ indent))
2728 (widget-documentation-link-add widget start (point)))
2729 (widget-put widget :buttons buttons))
2731 (widget-documentation-link-add widget start (point))))
2734 (defun widget-documentation-string-action (widget &rest ignore)
2735 ;; Toggle documentation.
2736 (let ((parent (widget-get widget :parent)))
2737 (widget-put parent :documentation-shown
2738 (not (widget-get parent :documentation-shown))))
2740 (widget-value-set widget (widget-value widget)))
2742 ;;; The Sexp Widgets.
2744 (define-widget 'const 'item
2745 "An immutable sexp."
2746 :prompt-value 'widget-const-prompt-value
2749 (defun widget-const-prompt-value (widget prompt value unbound)
2750 ;; Return the value of the const.
2751 (widget-value widget))
2753 (define-widget 'function-item 'const
2754 "An immutable function name."
2756 :documentation-property (lambda (symbol)
2758 (documentation symbol t)
2761 (define-widget 'variable-item 'const
2762 "An immutable variable name."
2764 :documentation-property 'variable-documentation)
2766 (define-widget 'other 'sexp
2767 "Matches any value, but doesn't let the user edit the value.
2768 This is useful as last item in a `choice' widget.
2769 You should use this widget type with a default value,
2770 as in (other DEFAULT) or (other :tag \"NAME\" DEFAULT).
2771 If the user selects this alternative, that specifies DEFAULT
2777 (defvar widget-string-prompt-value-history nil
2778 "History of input to `widget-string-prompt-value'.")
2780 (define-widget 'string 'editable-field
2783 :format "%{%t%}: %v"
2784 :complete-function 'ispell-complete-word
2785 :prompt-history 'widget-string-prompt-value-history)
2787 (define-widget 'regexp 'string
2788 "A regular expression."
2789 :match 'widget-regexp-match
2790 :validate 'widget-regexp-validate
2791 ;; Doesn't work well with terminating newline.
2792 ;; :value-face 'widget-single-line-field-face
2795 (defun widget-regexp-match (widget value)
2796 ;; Match valid regexps.
2797 (and (stringp value)
2800 (string-match value ""))
2803 (defun widget-regexp-validate (widget)
2804 "Check that the value of WIDGET is a valid regexp."
2805 (let ((val (widget-value widget)))
2806 (condition-case data
2808 (string-match val ""))
2809 (error (widget-put widget :error (error-message-string data))
2812 (define-widget 'file 'string
2814 It will read a file name from the minibuffer when invoked."
2815 :complete-function 'widget-file-complete
2816 :prompt-value 'widget-file-prompt-value
2817 :format "%{%t%}: %v"
2818 ;; Doesn't work well with terminating newline.
2819 ;; :value-face 'widget-single-line-field-face
2822 (defun widget-file-complete ()
2823 "Perform completion on file name preceding point."
2825 (let* ((end (point))
2826 (beg (save-excursion
2827 (skip-chars-backward "^ ")
2829 (pattern (buffer-substring beg end))
2830 (name-part (file-name-nondirectory pattern))
2831 (directory (file-name-directory pattern))
2832 (completion (file-name-completion name-part directory)))
2833 (cond ((eq completion t))
2835 (message "Can't find completion for \"%s\"" pattern)
2837 ((not (string= name-part completion))
2838 (delete-region beg end)
2839 (insert (expand-file-name completion directory)))
2841 (message "Making completion list...")
2842 (let ((list (file-name-all-completions name-part directory)))
2843 (setq list (sort list 'string<))
2844 (with-output-to-temp-buffer "*Completions*"
2845 (display-completion-list list)))
2846 (message "Making completion list...%s" "done")))))
2848 (defun widget-file-prompt-value (widget prompt value unbound)
2849 ;; Read file from minibuffer.
2850 (abbreviate-file-name
2852 (read-file-name prompt)
2853 (let ((prompt2 (format "%s (default %s) " prompt value))
2854 (dir (file-name-directory value))
2855 (file (file-name-nondirectory value))
2856 (must-match (widget-get widget :must-match)))
2857 (read-file-name prompt2 dir nil must-match file)))))
2859 ;;;(defun widget-file-action (widget &optional event)
2860 ;;; ;; Read a file name from the minibuffer.
2861 ;;; (let* ((value (widget-value widget))
2862 ;;; (dir (file-name-directory value))
2863 ;;; (file (file-name-nondirectory value))
2864 ;;; (menu-tag (widget-apply widget :menu-tag-get))
2865 ;;; (must-match (widget-get widget :must-match))
2866 ;;; (answer (read-file-name (concat menu-tag ": (default `" value "') ")
2867 ;;; dir nil must-match file)))
2868 ;;; (widget-value-set widget (abbreviate-file-name answer))
2870 ;;; (widget-apply widget :notify widget event)))
2872 (define-widget 'directory 'file
2873 "A directory widget.
2874 It will read a directory name from the minibuffer when invoked."
2877 (defvar widget-symbol-prompt-value-history nil
2878 "History of input to `widget-symbol-prompt-value'.")
2880 (define-widget 'symbol 'editable-field
2884 :format "%{%t%}: %v"
2885 :match (lambda (widget value) (symbolp value))
2886 :complete-function 'lisp-complete-symbol
2887 :prompt-internal 'widget-symbol-prompt-internal
2888 :prompt-match 'symbolp
2889 :prompt-history 'widget-symbol-prompt-value-history
2890 :value-to-internal (lambda (widget value)
2894 :value-to-external (lambda (widget value)
2899 (defun widget-symbol-prompt-internal (widget prompt initial history)
2900 ;; Read file from minibuffer.
2901 (let ((answer (completing-read prompt obarray
2902 (widget-get widget :prompt-match)
2903 nil initial history)))
2904 (if (and (stringp answer)
2905 (not (zerop (length answer))))
2907 (error "No value"))))
2909 (defvar widget-function-prompt-value-history nil
2910 "History of input to `widget-function-prompt-value'.")
2912 (define-widget 'function 'sexp
2914 :complete-function 'lisp-complete-symbol
2915 :prompt-value 'widget-field-prompt-value
2916 :prompt-internal 'widget-symbol-prompt-internal
2917 :prompt-match 'fboundp
2918 :prompt-history 'widget-function-prompt-value-history
2919 :action 'widget-field-action
2922 (defvar widget-variable-prompt-value-history nil
2923 "History of input to `widget-variable-prompt-value'.")
2925 (define-widget 'variable 'symbol
2926 ;; Should complete on variables.
2928 :prompt-match 'boundp
2929 :prompt-history 'widget-variable-prompt-value-history
2932 (defvar widget-coding-system-prompt-value-history nil
2933 "History of input to `widget-coding-system-prompt-value'.")
2935 (define-widget 'coding-system 'symbol
2936 "A MULE coding-system."
2937 :format "%{%t%}: %v"
2938 :tag "Coding system"
2939 :prompt-history 'widget-coding-system-prompt-value-history
2940 :prompt-value 'widget-coding-system-prompt-value
2941 :action 'widget-coding-system-action)
2943 (defun widget-coding-system-prompt-value (widget prompt value unbound)
2944 ;; Read coding-system from minibuffer.
2946 (completing-read (format "%s (default %s) " prompt value)
2947 (mapcar (lambda (sym)
2948 (list (symbol-name sym)))
2949 (coding-system-list)))))
2951 (defun widget-coding-system-action (widget &optional event)
2952 ;; Read a file name from the minibuffer.
2954 (widget-coding-system-prompt-value
2956 (widget-apply widget :menu-tag-get)
2957 (widget-value widget)
2959 (widget-value-set widget answer)
2960 (widget-apply widget :notify widget event)
2963 (define-widget 'sexp 'editable-field
2964 "An arbitrary Lisp expression."
2965 :tag "Lisp expression"
2966 :format "%{%t%}: %v"
2968 :validate 'widget-sexp-validate
2969 :match (lambda (widget value) t)
2970 :value-to-internal 'widget-sexp-value-to-internal
2971 :value-to-external (lambda (widget value) (read value))
2972 :prompt-history 'widget-sexp-prompt-value-history
2973 :prompt-value 'widget-sexp-prompt-value)
2975 (defun widget-sexp-value-to-internal (widget value)
2976 ;; Use pp for printer representation.
2977 (let ((pp (if (symbolp value)
2978 (prin1-to-string value)
2979 (pp-to-string value))))
2980 (while (string-match "\n\\'" pp)
2981 (setq pp (substring pp 0 -1)))
2982 (if (or (string-match "\n\\'" pp)
2987 (defun widget-sexp-validate (widget)
2988 ;; Valid if we can read the string and there is no junk left after it.
2990 (insert (widget-apply widget :value-get))
2991 (goto-char (point-min))
2992 (condition-case data
2994 ;; Avoid a confusing end-of-file error.
2995 (skip-syntax-forward "\\s-")
2997 (error "Empty sexp -- use `nil'?"))
2998 (let ((value (read (current-buffer))))
3000 (if (widget-apply widget :match value)
3002 (widget-put widget :error (widget-get widget :type-error))
3005 :error (format "Junk at end of expression: %s"
3006 (buffer-substring (point)
3009 (end-of-file ; Avoid confusing error message.
3010 (widget-put widget :error "Unbalanced sexp")
3012 (error (widget-put widget :error (error-message-string data))
3015 (defvar widget-sexp-prompt-value-history nil
3016 "History of input to `widget-sexp-prompt-value'.")
3018 (defun widget-sexp-prompt-value (widget prompt value unbound)
3019 ;; Read an arbitrary sexp.
3020 (let ((found (read-string prompt
3021 (if unbound nil (cons (prin1-to-string value) 0))
3022 (widget-get widget :prompt-history))))
3023 (let ((answer (read-from-string found)))
3024 (unless (= (cdr answer) (length found))
3025 (error "Junk at end of expression: %s"
3026 (substring found (cdr answer))))
3029 (define-widget 'restricted-sexp 'sexp
3030 "A Lisp expression restricted to values that match.
3031 To use this type, you must define :match or :match-alternatives."
3032 :type-error "The specified value is not valid"
3033 :match 'widget-restricted-sexp-match
3034 :value-to-internal (lambda (widget value)
3035 (if (widget-apply widget :match value)
3036 (prin1-to-string value)
3039 (defun widget-restricted-sexp-match (widget value)
3040 (let ((alternatives (widget-get widget :match-alternatives))
3042 (while (and alternatives (not matched))
3043 (if (cond ((functionp (car alternatives))
3044 (funcall (car alternatives) value))
3045 ((and (consp (car alternatives))
3046 (eq (car (car alternatives)) 'quote))
3047 (eq value (nth 1 (car alternatives)))))
3049 (setq alternatives (cdr alternatives)))
3052 (define-widget 'integer 'restricted-sexp
3056 :type-error "This field should contain an integer"
3057 :match-alternatives '(integerp))
3059 (define-widget 'number 'restricted-sexp
3060 "A floating point number."
3063 :type-error "This field should contain a number"
3064 :match-alternatives '(numberp))
3066 (define-widget 'character 'editable-field
3071 :format "%{%t%}: %v\n"
3072 :valid-regexp "\\`.\\'"
3073 :error "This field should contain a single character"
3074 :value-to-internal (lambda (widget value)
3077 (char-to-string value)))
3078 :value-to-external (lambda (widget value)
3082 :match (lambda (widget value)
3083 (char-valid-p value)))
3085 (define-widget 'list 'group
3088 :format "%{%t%}:\n%v")
3090 (define-widget 'vector 'group
3093 :format "%{%t%}:\n%v"
3094 :match 'widget-vector-match
3095 :value-to-internal (lambda (widget value) (append value nil))
3096 :value-to-external (lambda (widget value) (apply 'vector value)))
3098 (defun widget-vector-match (widget value)
3099 (and (vectorp value)
3100 (widget-group-match widget
3101 (widget-apply widget :value-to-internal value))))
3103 (define-widget 'cons 'group
3106 :format "%{%t%}:\n%v"
3107 :match 'widget-cons-match
3108 :value-to-internal (lambda (widget value)
3109 (list (car value) (cdr value)))
3110 :value-to-external (lambda (widget value)
3111 (cons (nth 0 value) (nth 1 value))))
3113 (defun widget-cons-match (widget value)
3115 (widget-group-match widget
3116 (widget-apply widget :value-to-internal value))))
3118 ;;; The `plist' Widget.
3122 (define-widget 'plist 'list
3124 :key-type '(symbol :tag "Key")
3125 :value-type '(sexp :tag "Value")
3126 :convert-widget 'widget-plist-convert-widget
3129 (defvar widget-plist-value-type) ;Dynamic variable
3131 (defun widget-plist-convert-widget (widget)
3132 ;; Handle `:options'.
3133 (let* ((options (widget-get widget :options))
3134 (key-type (widget-get widget :key-type))
3135 (widget-plist-value-type (widget-get widget :value-type))
3136 (other `(editable-list :inline t
3139 ,widget-plist-value-type)))
3141 (list `(checklist :inline t
3143 ,@(mapcar 'widget-plist-convert-option
3147 (widget-put widget :args args)
3150 (defun widget-plist-convert-option (option)
3151 ;; Convert a single plist option.
3152 (let (key-type value-type)
3154 (let ((key (nth 0 option)))
3155 (setq value-type (nth 1 option))
3158 (setq key-type `(const ,key))))
3159 (setq key-type `(const ,option)
3160 value-type widget-plist-value-type))
3161 `(group :format "Key: %v" :inline t ,key-type ,value-type)))
3164 ;;; The `alist' Widget.
3166 ;; Association lists.
3168 (define-widget 'alist 'list
3169 "An association list."
3170 :key-type '(sexp :tag "Key")
3171 :value-type '(sexp :tag "Value")
3172 :convert-widget 'widget-alist-convert-widget
3175 (defvar widget-alist-value-type) ;Dynamic variable
3177 (defun widget-alist-convert-widget (widget)
3178 ;; Handle `:options'.
3179 (let* ((options (widget-get widget :options))
3180 (key-type (widget-get widget :key-type))
3181 (widget-alist-value-type (widget-get widget :value-type))
3182 (other `(editable-list :inline t
3185 ,widget-alist-value-type)))
3187 (list `(checklist :inline t
3189 ,@(mapcar 'widget-alist-convert-option
3193 (widget-put widget :args args)
3196 (defun widget-alist-convert-option (option)
3197 ;; Convert a single alist option.
3198 (let (key-type value-type)
3200 (let ((key (nth 0 option)))
3201 (setq value-type (nth 1 option))
3204 (setq key-type `(const ,key))))
3205 (setq key-type `(const ,option)
3206 value-type widget-alist-value-type))
3207 `(cons :format "Key: %v" ,key-type ,value-type)))
3209 (define-widget 'choice 'menu-choice
3210 "A union of several sexp types."
3212 :format "%{%t%}: %[Value Menu%] %v"
3213 :button-prefix 'widget-push-button-prefix
3214 :button-suffix 'widget-push-button-suffix
3215 :prompt-value 'widget-choice-prompt-value)
3217 (defun widget-choice-prompt-value (widget prompt value unbound)
3219 (let ((args (widget-get widget :args))
3220 (completion-ignore-case (widget-get widget :case-fold))
3221 current choices old)
3222 ;; Find the first arg that match VALUE.
3225 (if (widget-apply (car look) :match value)
3226 (setq old (car look)
3228 (setq look (cdr look)))))
3231 (cond ((= (length args) 0)
3233 ((= (length args) 1)
3235 ((and (= (length args) 2)
3237 (if (eq old (nth 0 args))
3242 (setq current (car args)
3245 (cons (cons (widget-apply current :menu-tag-get)
3248 (let ((val (completing-read prompt choices nil t)))
3250 (let ((try (try-completion val choices)))
3253 (cdr (assoc val choices)))
3256 (widget-prompt-value current prompt nil t)
3259 (define-widget 'radio 'radio-button-choice
3260 "A union of several sexp types."
3262 :format "%{%t%}:\n%v"
3263 :prompt-value 'widget-choice-prompt-value)
3265 (define-widget 'repeat 'editable-list
3266 "A variable length homogeneous list."
3268 :format "%{%t%}:\n%v%i\n")
3270 (define-widget 'set 'checklist
3271 "A list of members from a fixed set."
3273 :format "%{%t%}:\n%v")
3275 (define-widget 'boolean 'toggle
3276 "To be nil or non-nil, that is the question."
3278 :prompt-value 'widget-boolean-prompt-value
3279 :button-prefix 'widget-push-button-prefix
3280 :button-suffix 'widget-push-button-suffix
3281 :format "%{%t%}: %[Toggle%] %v\n"
3285 (defun widget-boolean-prompt-value (widget prompt value unbound)
3286 ;; Toggle a boolean.
3289 ;;; The `color' Widget.
3291 (define-widget 'color 'editable-field
3292 "Choose a color name (with sample)."
3293 :format "%t: %v (%{sample%})\n"
3297 :complete 'widget-color-complete
3298 :sample-face-get 'widget-color-sample-face-get
3299 :notify 'widget-color-notify
3300 :action 'widget-color-action)
3302 (defun widget-color-complete (widget)
3303 "Complete the color in WIDGET."
3304 (require 'facemenu) ; for facemenu-color-alist
3305 (let* ((prefix (buffer-substring-no-properties (widget-field-start widget)
3307 (list (or facemenu-color-alist
3308 (mapcar 'list (defined-colors))))
3309 (completion (try-completion prefix list)))
3310 (cond ((eq completion t)
3311 (message "Exact match."))
3313 (error "Can't find completion for \"%s\"" prefix))
3314 ((not (string-equal prefix completion))
3315 (insert-and-inherit (substring completion (length prefix))))
3317 (message "Making completion list...")
3318 (let ((list (all-completions prefix list nil)))
3319 (with-output-to-temp-buffer "*Completions*"
3320 (display-completion-list list)))
3321 (message "Making completion list...done")))))
3323 (defun widget-color-sample-face-get (widget)
3324 (let* ((value (condition-case nil
3325 (widget-value widget)
3326 (error (widget-get widget :value))))
3327 (symbol (intern (concat "fg:" value))))
3329 (facemenu-get-face symbol)
3332 (defun widget-color-action (widget &optional event)
3333 ;; Prompt for a color.
3334 (let* ((tag (widget-apply widget :menu-tag-get))
3335 (prompt (concat tag ": "))
3336 (value (widget-value widget))
3337 (start (widget-field-start widget))
3338 (pos (cond ((< (point) start)
3340 ((> (point) (+ start (length value)))
3343 (- (point) start))))
3344 (answer (facemenu-read-color prompt)))
3345 (unless (zerop (length answer))
3346 (widget-value-set widget answer)
3348 (widget-apply widget :notify widget event))))
3350 (defun widget-color-notify (widget child &optional event)
3351 "Update the sample, and notofy the parent."
3352 (overlay-put (widget-get widget :sample-overlay)
3353 'face (widget-apply widget :sample-face-get))
3354 (widget-default-notify widget child event))
3358 (defun widget-at (pos)
3359 "The button or field at POS."
3360 (or (get-char-property pos 'button)
3361 (get-char-property pos 'field)))
3363 (defun widget-echo-help (pos)
3364 "Display the help echo for widget at POS."
3365 (let* ((widget (widget-at pos))
3366 (help-echo (and widget (widget-get widget :help-echo))))
3367 (if (or (stringp help-echo)
3368 (and (symbolp help-echo) (fboundp help-echo)
3369 (stringp (setq help-echo (funcall help-echo widget)))))
3370 (message "%s" help-echo))))
3376 ;; wid-edit.el ends here