1 ;;; wid-edit.el --- Functions for creating and using widgets -*-byte-compile-dynamic: t;-*-
3 ;; Copyright (C) 1996,97,1999,2000,01,02,2003, 2004 Free Software Foundation, Inc.
5 ;; Author: Per Abrahamsen <abraham@dina.kvl.dk>
7 ;; Keywords: extensions
9 ;; This file is part of GNU Emacs.
11 ;; GNU Emacs is free software; you can redistribute it and/or modify
12 ;; it under the terms of the GNU General Public License as published by
13 ;; the Free Software Foundation; either version 2, or (at your option)
16 ;; GNU Emacs is distributed in the hope that it will be useful,
17 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
18 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 ;; GNU General Public License for more details.
21 ;; You should have received a copy of the GNU General Public License
22 ;; along with GNU Emacs; see the file COPYING. If not, write to the
23 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
24 ;; Boston, MA 02111-1307, USA.
26 ;;; Wishlist items (from widget.texi):
28 ;; * The `menu-choice' tag should be prettier, something like the
29 ;; abbreviated menus in Open Look.
31 ;; * Finish `:tab-order'.
33 ;; * Make indentation work with glyphs and proportional fonts.
35 ;; * Add commands to show overview of object and class hierarchies to
38 ;; * Find a way to disable mouse highlight for inactive widgets.
40 ;; * Find a way to make glyphs look inactive.
42 ;; * Add `key-binding' widget.
44 ;; * Add `widget' widget for editing widget specifications.
46 ;; * Find clean way to implement variable length list. See
47 ;; `TeX-printer-list' for an explanation.
49 ;; * `C-h' in `widget-prompt-value' should give type specific help.
51 ;; * A mailto widget. [This should work OK as a url-link if with
52 ;; browse-url-browser-function' set up appropriately.]
62 (defun widget-event-point (event)
63 "Character position of the end of event if that exists, or nil."
64 (posn-point (event-end event)))
66 (defun widget-button-release-event-p (event)
67 "Non-nil if EVENT is a mouse-button-release event object."
69 (memq (event-basic-type event) '(mouse-1 mouse-2 mouse-3))
70 (or (memq 'click (event-modifiers event))
71 (memq 'drag (event-modifiers event)))))
76 "Customization support for the Widget Library."
77 :link '(custom-manual "(widget)Top")
78 :link '(emacs-library-link :tag "Lisp File" "widget.el")
83 (defgroup widget-documentation nil
84 "Options controling the display of documentation strings."
87 (defgroup widget-faces nil
88 "Faces used by the widget library."
92 (defvar widget-documentation-face 'widget-documentation-face
93 "Face used for documentation strings in widgets.
94 This exists as a variable so it can be set locally in certain buffers.")
96 (defface widget-documentation-face '((((class color)
98 (:foreground "lime green"))
101 (:foreground "dark green"))
103 "Face used for documentation text."
104 :group 'widget-documentation
105 :group 'widget-faces)
107 (defvar widget-button-face 'widget-button-face
108 "Face used for buttons in widgets.
109 This exists as a variable so it can be set locally in certain buffers.")
111 (defface widget-button-face '((t (:weight bold)))
112 "Face used for widget buttons."
113 :group 'widget-faces)
115 (defcustom widget-mouse-face 'highlight
116 "Face used for widget buttons when the mouse is above them."
118 :group 'widget-faces)
120 ;; TTY gets special definitions here and in the next defface, because
121 ;; the gray colors defined for other displays cause black text on a black
122 ;; background, at least on light-background TTYs.
123 (defface widget-field-face '((((type tty))
124 :background "yellow3"
126 (((class grayscale color)
128 :background "gray85")
129 (((class grayscale color)
131 :background "dim gray")
134 "Face used for editable fields."
135 :group 'widget-faces)
137 (defface widget-single-line-field-face '((((type tty))
140 (((class grayscale color)
142 :background "gray85")
143 (((class grayscale color)
145 :background "dim gray")
148 "Face used for editable fields spanning only a single line."
149 :group 'widget-faces)
151 ;;; This causes display-table to be loaded, and not usefully.
152 ;;;(defvar widget-single-line-display-table
153 ;;; (let ((table (make-display-table)))
154 ;;; (aset table 9 "^I")
155 ;;; (aset table 10 "^J")
157 ;;; "Display table used for single-line editable fields.")
159 ;;;(when (fboundp 'set-face-display-table)
160 ;;; (set-face-display-table 'widget-single-line-field-face
161 ;;; widget-single-line-display-table))
163 ;;; Utility functions.
165 ;; These are not really widget specific.
167 (defun widget-princ-to-string (object)
168 "Return string representation of OBJECT, any Lisp object.
169 No quoting characters are used; no delimiters are printed around
170 the contents of strings."
171 (with-output-to-string
174 (defun widget-clear-undo ()
175 "Clear all undo information."
176 (buffer-disable-undo (current-buffer))
177 (buffer-enable-undo))
179 (defcustom widget-menu-max-size 40
180 "Largest number of items allowed in a popup-menu.
181 Larger menus are read through the minibuffer."
185 (defcustom widget-menu-max-shortcuts 40
186 "Largest number of items for which it works to choose one with a character.
187 For a larger number of items, the minibuffer is used."
191 (defcustom widget-menu-minibuffer-flag nil
192 "*Control how to ask for a choice from the keyboard.
193 Non-nil means use the minibuffer;
194 nil means read a single character."
198 (defun widget-choose (title items &optional event)
199 "Choose an item from a list.
201 First argument TITLE is the name of the list.
202 Second argument ITEMS is a list whose members are either
203 (NAME . VALUE), to indicate selectable items, or just strings to
204 indicate unselectable items.
205 Optional third argument EVENT is an input event.
207 The user is asked to choose between each NAME from the items alist,
208 and the VALUE of the chosen element will be returned. If EVENT is a
209 mouse event, and the number of elements in items is less than
210 `widget-menu-max-size', a popup menu will be used, otherwise the
212 (cond ((and (< (length items) widget-menu-max-size)
213 event (display-popup-menus-p))
216 (list title (cons "" items))))
217 ((or widget-menu-minibuffer-flag
218 (> (length items) widget-menu-max-shortcuts))
219 ;; Read the choice of name from the minibuffer.
220 (setq items (widget-remove-if 'stringp items))
221 (let ((val (completing-read (concat title ": ") items nil t)))
223 (let ((try (try-completion val items)))
226 (cdr (assoc val items))))))
228 ;; Construct a menu of the choices
229 ;; and then use it for prompting for a single character.
230 (let* ((overriding-terminal-local-map (make-sparse-keymap))
232 map choice some-choice-enabled value)
233 ;; Define SPC as a prefix char to get to this menu.
234 (define-key overriding-terminal-local-map " "
235 (setq map (make-sparse-keymap title)))
236 (with-current-buffer (get-buffer-create " widget-choose")
238 (insert "Available choices:\n\n")
240 (setq choice (car items) items (cdr items))
242 (let* ((name (car choice))
243 (function (cdr choice)))
244 (insert (format "%c = %s\n" next-digit name))
245 (define-key map (vector next-digit) function)
246 (setq some-choice-enabled t)))
247 ;; Allocate digits to disabled alternatives
248 ;; so that the digit of a given alternative never varies.
249 (setq next-digit (1+ next-digit)))
250 (insert "\nC-g = Quit"))
251 (or some-choice-enabled
252 (error "None of the choices is currently meaningful"))
253 (define-key map [?\C-g] 'keyboard-quit)
254 (define-key map [t] 'keyboard-quit)
255 (define-key map [?\M-\C-v] 'scroll-other-window)
256 (define-key map [?\M--] 'negative-argument)
257 (setcdr map (nreverse (cdr map)))
258 ;; Read a char with the menu, and return the result
259 ;; that corresponds to it.
260 (save-window-excursion
261 (let ((buf (get-buffer " widget-choose")))
262 (fit-window-to-buffer (display-buffer buf))
263 (let ((cursor-in-echo-area t)
267 (while (not (or (and (>= char ?0) (< char next-digit))
268 (eq value 'keyboard-quit)))
269 ;; Unread a SPC to lead to our new menu.
270 (setq unread-command-events (cons ?\ unread-command-events))
271 (setq keys (read-key-sequence title))
273 (lookup-key overriding-terminal-local-map keys t)
274 char (string-to-char (substring keys 1)))
275 (cond ((eq value 'scroll-other-window)
276 (let ((minibuffer-scroll-window
277 (get-buffer-window buf)))
279 (scroll-other-window-down
280 (window-height minibuffer-scroll-window))
281 (scroll-other-window))
283 ((eq value 'negative-argument)
287 (when (eq value 'keyboard-quit)
291 (defun widget-remove-if (predictate list)
292 (let (result (tail list))
294 (or (funcall predictate (car tail))
295 (setq result (cons (car tail) result)))
296 (setq tail (cdr tail)))
299 ;;; Widget text specifications.
301 ;; These functions are for specifying text properties.
303 ;; We can set it to nil now that get_local_map uses get_pos_property.
304 (defconst widget-field-add-space nil
305 "Non-nil means add extra space at the end of editable text fields.
306 If you don't add the space, it will become impossible to edit a zero
309 (defvar widget-field-use-before-change t
310 "Non-nil means use `before-change-functions' to track editable fields.
311 This enables the use of undo, but doesn't work on Emacs 19.34 and earlier.
312 Using before hooks also means that the :notify function can't know the
315 (defun widget-specify-field (widget from to)
316 "Specify editable button for WIDGET between FROM and TO."
317 ;; Terminating space is not part of the field, but necessary in
318 ;; order for local-map to work. Remove next sexp if local-map works
319 ;; at the end of the overlay.
322 (cond ((null (widget-get widget :size))
324 (widget-field-add-space
325 (insert-and-inherit " ")))
327 (let ((keymap (widget-get widget :keymap))
328 (face (or (widget-get widget :value-face) 'widget-field-face))
329 (help-echo (widget-get widget :help-echo))
330 (follow-link (widget-get widget :follow-link))
332 (or (not widget-field-add-space) (widget-get widget :size))))
333 (if (functionp help-echo)
334 (setq help-echo 'widget-mouse-help))
335 (when (= (char-before to) ?\n)
336 ;; When the last character in the field is a newline, we want to
337 ;; give it a `field' char-property of `boundary', which helps the
338 ;; C-n/C-p act more naturally when entering/leaving the field. We
339 ;; do this by making a small secondary overlay to contain just that
341 (let ((overlay (make-overlay (1- to) to nil t nil)))
342 (overlay-put overlay 'field 'boundary)
343 ;; We need the real field for tabbing.
344 (overlay-put overlay 'real-field widget)
345 ;; Use `local-map' here, not `keymap', so that normal editing
346 ;; works in the field when, say, Custom uses `suppress-keymap'.
347 (overlay-put overlay 'local-map keymap)
348 (overlay-put overlay 'face face)
349 (overlay-put overlay 'follow-link follow-link)
350 (overlay-put overlay 'help-echo help-echo))
352 (setq rear-sticky t))
353 (let ((overlay (make-overlay from to nil nil rear-sticky)))
354 (widget-put widget :field-overlay overlay)
355 ;;(overlay-put overlay 'detachable nil)
356 (overlay-put overlay 'field widget)
357 (overlay-put overlay 'local-map keymap)
358 (overlay-put overlay 'face face)
359 (overlay-put overlay 'follow-link follow-link)
360 (overlay-put overlay 'help-echo help-echo)))
361 (widget-specify-secret widget))
363 (defun widget-specify-secret (field)
364 "Replace text in FIELD with value of `:secret', if non-nil."
365 (let ((secret (widget-get field :secret))
366 (size (widget-get field :size)))
368 (let ((begin (widget-field-start field))
369 (end (widget-field-end field)))
371 (while (and (> end begin)
372 (eq (char-after (1- end)) ?\ ))
373 (setq end (1- end))))
375 (let ((old (char-after begin)))
376 (unless (eq old secret)
377 (subst-char-in-region begin (1+ begin) old secret)
378 (put-text-property begin (1+ begin) 'secret old))
379 (setq begin (1+ begin))))))))
381 (defun widget-specify-button (widget from to)
382 "Specify button for WIDGET between FROM and TO."
383 (let ((overlay (make-overlay from to nil t nil))
384 (follow-link (widget-get widget :follow-link))
385 (help-echo (widget-get widget :help-echo)))
386 (widget-put widget :button-overlay overlay)
387 (if (functionp help-echo)
388 (setq help-echo 'widget-mouse-help))
389 (overlay-put overlay 'button widget)
390 (overlay-put overlay 'keymap (widget-get widget :keymap))
391 (overlay-put overlay 'evaporate t)
392 ;; We want to avoid the face with image buttons.
393 (unless (widget-get widget :suppress-face)
394 (overlay-put overlay 'face (widget-apply widget :button-face-get)))
395 (overlay-put overlay 'pointer 'hand)
396 (overlay-put overlay 'follow-link follow-link)
397 (overlay-put overlay 'help-echo help-echo)))
399 (defun widget-mouse-help (window overlay point)
400 "Help-echo callback for widgets whose :help-echo is a function."
401 (with-current-buffer (overlay-buffer overlay)
402 (let* ((widget (widget-at (overlay-start overlay)))
403 (help-echo (if widget (widget-get widget :help-echo))))
404 (if (functionp help-echo)
405 (funcall help-echo widget)
408 (defun widget-specify-sample (widget from to)
409 "Specify sample for WIDGET between FROM and TO."
410 (let ((overlay (make-overlay from to nil t nil)))
411 (overlay-put overlay 'face (widget-apply widget :sample-face-get))
412 (overlay-put overlay 'evaporate t)
413 (widget-put widget :sample-overlay overlay)))
415 (defun widget-specify-doc (widget from to)
416 "Specify documentation for WIDGET between FROM and TO."
417 (let ((overlay (make-overlay from to nil t nil)))
418 (overlay-put overlay 'widget-doc widget)
419 (overlay-put overlay 'face widget-documentation-face)
420 (overlay-put overlay 'evaporate t)
421 (widget-put widget :doc-overlay overlay)))
423 (defmacro widget-specify-insert (&rest form)
424 "Execute FORM without inheriting any text properties."
426 (let ((inhibit-read-only t)
427 (inhibit-modification-hooks t))
428 (narrow-to-region (point) (point))
429 (prog1 (progn ,@form)
430 (goto-char (point-max))))))
432 (defface widget-inactive-face '((((class grayscale color)
434 (:foreground "light gray"))
435 (((class grayscale color)
437 (:foreground "dim gray"))
440 "Face used for inactive widgets."
441 :group 'widget-faces)
443 (defun widget-specify-inactive (widget from to)
444 "Make WIDGET inactive for user modifications."
445 (unless (widget-get widget :inactive)
446 (let ((overlay (make-overlay from to nil t nil)))
447 (overlay-put overlay 'face 'widget-inactive-face)
448 ;; This is disabled, as it makes the mouse cursor change shape.
449 ;; (overlay-put overlay 'mouse-face 'widget-inactive-face)
450 (overlay-put overlay 'evaporate t)
451 (overlay-put overlay 'priority 100)
452 (overlay-put overlay 'modification-hooks '(widget-overlay-inactive))
453 (widget-put widget :inactive overlay))))
455 (defun widget-overlay-inactive (&rest junk)
456 "Ignoring the arguments, signal an error."
457 (unless inhibit-read-only
458 (error "The widget here is not active")))
461 (defun widget-specify-active (widget)
462 "Make WIDGET active for user modifications."
463 (let ((inactive (widget-get widget :inactive)))
465 (delete-overlay inactive)
466 (widget-put widget :inactive nil))))
468 ;;; Widget Properties.
470 (defsubst widget-type (widget)
471 "Return the type of WIDGET, a symbol."
475 (defun widgetp (widget)
476 "Return non-nil iff WIDGET is a widget."
478 (get widget 'widget-type)
480 (symbolp (car widget))
481 (get (car widget) 'widget-type))))
483 (defun widget-get-indirect (widget property)
484 "In WIDGET, get the value of PROPERTY.
485 If the value is a symbol, return its binding.
486 Otherwise, just return the value."
487 (let ((value (widget-get widget property)))
492 (defun widget-member (widget property)
493 "Non-nil iff there is a definition in WIDGET for PROPERTY."
494 (cond ((plist-member (cdr widget) property)
497 (widget-member (get (car widget) 'widget-type) property))
500 (defun widget-value (widget)
501 "Extract the current value of WIDGET."
503 :value-to-external (widget-apply widget :value-get)))
505 (defun widget-value-set (widget value)
506 "Set the current value of WIDGET to VALUE."
508 :value-set (widget-apply widget
509 :value-to-internal value)))
511 (defun widget-default-get (widget)
512 "Extract the default external value of WIDGET."
513 (widget-apply widget :value-to-external
514 (or (widget-get widget :value)
515 (widget-apply widget :default-get))))
517 (defun widget-match-inline (widget vals)
518 "In WIDGET, match the start of VALS."
519 (cond ((widget-get widget :inline)
520 (widget-apply widget :match-inline vals))
522 (widget-apply widget :match (car vals)))
523 (cons (list (car vals)) (cdr vals)))
526 (defun widget-apply-action (widget &optional event)
527 "Apply :action in WIDGET in response to EVENT."
528 (if (widget-apply widget :active)
529 (widget-apply widget :action event)
530 (error "Attempt to perform action on inactive widget")))
532 ;;; Helper functions.
534 ;; These are widget specific.
537 (defun widget-prompt-value (widget prompt &optional value unbound)
538 "Prompt for a value matching WIDGET, using PROMPT.
539 The current value is assumed to be VALUE, unless UNBOUND is non-nil."
540 (unless (listp widget)
541 (setq widget (list widget)))
542 (setq prompt (format "[%s] %s" (widget-type widget) prompt))
543 (setq widget (widget-convert widget))
544 (let ((answer (widget-apply widget :prompt-value prompt value unbound)))
545 (unless (widget-apply widget :match answer)
546 (error "Value does not match %S type" (car widget)))
549 (defun widget-get-sibling (widget)
550 "Get the item WIDGET is assumed to toggle.
551 This is only meaningful for radio buttons or checkboxes in a list."
552 (let* ((children (widget-get (widget-get widget :parent) :children))
556 (setq child (car children)
557 children (cdr children))
558 (when (eq (widget-get child :button) widget)
559 (throw 'child child)))
562 (defun widget-map-buttons (function &optional buffer maparg)
563 "Map FUNCTION over the buttons in BUFFER.
564 FUNCTION is called with the arguments WIDGET and MAPARG.
566 If FUNCTION returns non-nil, the walk is cancelled.
568 The arguments MAPARG, and BUFFER default to nil and (current-buffer),
570 (let ((cur (point-min))
573 (with-current-buffer buffer (overlay-lists))
575 (setq overlays (append (car overlays) (cdr overlays)))
576 (while (setq cur (pop overlays))
577 (setq widget (overlay-get cur 'button))
578 (if (and widget (funcall function widget maparg))
579 (setq overlays nil)))))
583 (defcustom widget-image-directory (file-name-as-directory
584 (expand-file-name "custom" data-directory))
585 "Where widget button images are located.
586 If this variable is nil, widget will try to locate the directory
591 (defcustom widget-image-enable t
592 "If non nil, use image buttons in widgets when available."
597 (defcustom widget-image-conversion
598 '((xpm ".xpm") (gif ".gif") (png ".png") (jpeg ".jpg" ".jpeg")
600 "Conversion alist from image formats to file name suffixes."
602 :type '(repeat (cons :format "%v"
603 (symbol :tag "Image Format" unknown)
604 (repeat :tag "Suffixes"
605 (string :format "%v")))))
607 (defun widget-image-find (image)
608 "Create a graphical button from IMAGE.
609 IMAGE should either already be an image, or be a file name sans
610 extension (xpm, xbm, gif, jpg, or png) located in
611 `widget-image-directory' or otherwise where `find-image' will find it."
612 (cond ((not (and image widget-image-enable (display-graphic-p)))
613 ;; We don't want or can't use images.
616 (eq 'image (car image)))
617 ;; Already an image spec. Use it.
620 ;; A string. Look it up in relevant directories.
621 (let* ((load-path (cons widget-image-directory load-path))
623 (dolist (elt widget-image-conversion)
624 (dolist (ext (cdr elt))
625 (push (list :type (car elt) :file (concat image ext)) specs)))
626 (setq specs (nreverse specs))
632 (defvar widget-button-pressed-face 'widget-button-pressed-face
633 "Face used for pressed buttons in widgets.
634 This exists as a variable so it can be set locally in certain
637 (defun widget-image-insert (widget tag image &optional down inactive)
638 "In WIDGET, insert the text TAG or, if supported, IMAGE.
639 IMAGE should either be an image or an image file name sans extension
640 \(xpm, xbm, gif, jpg, or png) located in `widget-image-directory'.
642 Optional arguments DOWN and INACTIVE are used instead of IMAGE when the
643 button is pressed or inactive, respectively. These are currently ignored."
644 (if (and (display-graphic-p)
645 (setq image (widget-image-find image)))
646 (progn (widget-put widget :suppress-face t)
649 tag 'mouse-face widget-button-pressed-face)))
654 (defgroup widget-button nil
655 "The look of various kinds of buttons."
658 (defcustom widget-button-prefix ""
659 "String used as prefix for buttons."
661 :group 'widget-button)
663 (defcustom widget-button-suffix ""
664 "String used as suffix for buttons."
666 :group 'widget-button)
668 ;;; Creating Widgets.
671 (defun widget-create (type &rest args)
672 "Create widget of TYPE.
673 The optional ARGS are additional keyword arguments."
674 (let ((widget (apply 'widget-convert type args)))
675 (widget-apply widget :create)
678 (defun widget-create-child-and-convert (parent type &rest args)
679 "As part of the widget PARENT, create a child widget TYPE.
680 The child is converted, using the keyword arguments ARGS."
681 (let ((widget (apply 'widget-convert type args)))
682 (widget-put widget :parent parent)
683 (unless (widget-get widget :indent)
684 (widget-put widget :indent (+ (or (widget-get parent :indent) 0)
685 (or (widget-get widget :extra-offset) 0)
686 (widget-get parent :offset))))
687 (widget-apply widget :create)
690 (defun widget-create-child (parent type)
691 "Create widget of TYPE."
692 (let ((widget (widget-copy type)))
693 (widget-put widget :parent parent)
694 (unless (widget-get widget :indent)
695 (widget-put widget :indent (+ (or (widget-get parent :indent) 0)
696 (or (widget-get widget :extra-offset) 0)
697 (widget-get parent :offset))))
698 (widget-apply widget :create)
701 (defun widget-create-child-value (parent type value)
702 "Create widget of TYPE with value VALUE."
703 (let ((widget (widget-copy type)))
704 (widget-put widget :value (widget-apply widget :value-to-internal value))
705 (widget-put widget :parent parent)
706 (unless (widget-get widget :indent)
707 (widget-put widget :indent (+ (or (widget-get parent :indent) 0)
708 (or (widget-get widget :extra-offset) 0)
709 (widget-get parent :offset))))
710 (widget-apply widget :create)
714 (defun widget-delete (widget)
716 (widget-apply widget :delete))
718 (defun widget-copy (widget)
719 "Make a deep copy of WIDGET."
720 (widget-apply (copy-sequence widget) :copy))
722 (defun widget-convert (type &rest args)
723 "Convert TYPE to a widget without inserting it in the buffer.
724 The optional ARGS are additional keyword arguments."
725 ;; Don't touch the type.
726 (let* ((widget (if (symbolp type)
728 (copy-sequence type)))
732 ;; First set the :args keyword.
733 (while (cdr current) ;Look in the type.
734 (if (and (keywordp (cadr current))
735 ;; If the last element is a keyword,
736 ;; it is still the :args element,
737 ;; even though it is a keyword.
739 (if (eq (cadr current) :args)
740 ;; If :args is explicitly specified, obey it.
742 ;; Some other irrelevant keyword.
743 (setq current (cdr (cdr current))))
744 (setcdr current (list :args (cdr current)))
746 (while (and args (not done)) ;Look in ARGS.
747 (cond ((eq (car args) :args)
748 ;; Handle explicit specification of :args.
749 (setq args (cadr args)
751 ((keywordp (car args))
752 (setq args (cddr args)))
755 (widget-put widget :args args))
756 ;; Then Convert the widget.
759 (let ((convert-widget (plist-get (cdr type) :convert-widget)))
761 (setq widget (funcall convert-widget widget))))
762 (setq type (get (car type) 'widget-type)))
763 ;; Finally set the keyword args.
765 (let ((next (nth 0 keys)))
768 (widget-put widget next (nth 1 keys))
769 (setq keys (nthcdr 2 keys)))
771 ;; Convert the :value to internal format.
772 (if (widget-member widget :value)
774 :value (widget-apply widget
776 (widget-get widget :value))))
777 ;; Return the newly create widget.
781 (defun widget-insert (&rest args)
782 "Call `insert' with ARGS even if surrounding text is read only."
783 (let ((inhibit-read-only t)
784 (inhibit-modification-hooks t))
785 (apply 'insert args)))
787 (defun widget-convert-text (type from to
788 &optional button-from button-to
790 "Return a widget of type TYPE with endpoint FROM TO.
791 Optional ARGS are extra keyword arguments for TYPE.
792 and TO will be used as the widgets end points. If optional arguments
793 BUTTON-FROM and BUTTON-TO are given, these will be used as the widgets
795 Optional ARGS are extra keyword arguments for TYPE."
796 (let ((widget (apply 'widget-convert type :delete 'widget-leave-text args))
797 (from (copy-marker from))
798 (to (copy-marker to)))
799 (set-marker-insertion-type from t)
800 (set-marker-insertion-type to nil)
801 (widget-put widget :from from)
802 (widget-put widget :to to)
804 (widget-specify-button widget button-from button-to))
807 (defun widget-convert-button (type from to &rest args)
808 "Return a widget of type TYPE with endpoint FROM TO.
809 Optional ARGS are extra keyword arguments for TYPE.
810 No text will be inserted to the buffer, instead the text between FROM
811 and TO will be used as the widgets end points, as well as the widgets
813 (apply 'widget-convert-text type from to from to args))
815 (defun widget-leave-text (widget)
816 "Remove markers and overlays from WIDGET and its children."
817 (let ((button (widget-get widget :button-overlay))
818 (sample (widget-get widget :sample-overlay))
819 (doc (widget-get widget :doc-overlay))
820 (field (widget-get widget :field-overlay)))
821 (set-marker (widget-get widget :from) nil)
822 (set-marker (widget-get widget :to) nil)
824 (delete-overlay button))
826 (delete-overlay sample))
828 (delete-overlay doc))
830 (delete-overlay field))
831 (mapc 'widget-leave-text (widget-get widget :children))))
833 ;;; Keymap and Commands.
836 (defvar widget-keymap
837 (let ((map (make-sparse-keymap)))
838 (define-key map "\t" 'widget-forward)
839 (define-key map [(shift tab)] 'widget-backward)
840 (define-key map [backtab] 'widget-backward)
841 (define-key map [down-mouse-2] 'widget-button-click)
842 (define-key map "\C-m" 'widget-button-press)
844 "Keymap containing useful binding for buffers containing widgets.
845 Recommended as a parent keymap for modes using widgets.")
847 (defvar widget-global-map global-map
848 "Keymap used for events a widget does not handle itself.")
849 (make-variable-buffer-local 'widget-global-map)
851 (defvar widget-field-keymap
852 (let ((map (copy-keymap widget-keymap)))
853 (define-key map "\C-k" 'widget-kill-line)
854 (define-key map "\M-\t" 'widget-complete)
855 (define-key map "\C-m" 'widget-field-activate)
856 ;; Since the widget code uses a `field' property to identify fields,
857 ;; ordinary beginning-of-line does the right thing.
858 ;; (define-key map "\C-a" 'widget-beginning-of-line)
859 (define-key map "\C-e" 'widget-end-of-line)
861 "Keymap used inside an editable field.")
863 (defvar widget-text-keymap
864 (let ((map (copy-keymap widget-keymap)))
865 ;; Since the widget code uses a `field' property to identify fields,
866 ;; ordinary beginning-of-line does the right thing.
867 ;; (define-key map "\C-a" 'widget-beginning-of-line)
868 (define-key map "\C-e" 'widget-end-of-line)
870 "Keymap used inside a text field.")
872 (defun widget-field-activate (pos &optional event)
873 "Invoke the editable field at point."
875 (let ((field (widget-field-at pos)))
877 (widget-apply-action field event)
879 (lookup-key widget-global-map (this-command-keys))))))
881 (defface widget-button-pressed-face
885 (:weight bold :underline t)))
886 "Face used for pressed buttons."
887 :group 'widget-faces)
889 (defun widget-button-click (event)
890 "Invoke the button that the mouse is pointing at."
892 (if (widget-event-point event)
893 (let* ((pos (widget-event-point event))
894 (start (event-start event))
895 (button (get-char-property
896 pos 'button (and (windowp (posn-window start))
897 (window-buffer (posn-window start))))))
899 ;; Mouse click on a widget button. Do the following
900 ;; in a save-excursion so that the click on the button
901 ;; doesn't change point.
902 (save-selected-window
903 (select-window (posn-window (event-start event)))
905 (goto-char (posn-point (event-start event)))
906 (let* ((overlay (widget-get button :button-overlay))
907 (face (overlay-get overlay 'face))
908 (mouse-face (overlay-get overlay 'mouse-face)))
910 ;; Read events, including mouse-movement events
911 ;; until we receive a release event. Highlight/
912 ;; unhighlight the button the mouse was initially
913 ;; on when we move over it.
914 (let ((track-mouse t))
916 (when face ; avoid changing around image
918 'face widget-button-pressed-face)
920 'mouse-face widget-button-pressed-face))
921 (unless (widget-apply button :mouse-down-action event)
922 (while (not (widget-button-release-event-p event))
923 (setq event (read-event)
924 pos (widget-event-point event))
926 (eq (get-char-property pos 'button)
931 widget-button-pressed-face)
934 widget-button-pressed-face))
935 (overlay-put overlay 'face face)
936 (overlay-put overlay 'mouse-face mouse-face))))
938 ;; When mouse is released over the button, run
939 ;; its action function.
941 (eq (get-char-property pos 'button) button))
942 (widget-apply-action button event))))
943 (overlay-put overlay 'face face)
944 (overlay-put overlay 'mouse-face mouse-face))))
946 (unless (pos-visible-in-window-p (widget-event-point event))
947 (mouse-set-point event)
952 (let ((up t) command)
953 ;; Mouse click not on a widget button. Find the global
954 ;; command to run, and check whether it is bound to an
956 (mouse-set-point event)
957 (if (memq (event-basic-type event) '(mouse-1 down-mouse-1))
958 (cond ((setq command ;down event
959 (lookup-key widget-global-map [down-mouse-1]))
961 ((setq command ;up event
962 (lookup-key widget-global-map [mouse-1]))))
963 (cond ((setq command ;down event
964 (lookup-key widget-global-map [down-mouse-2]))
966 ((setq command ;up event
967 (lookup-key widget-global-map [mouse-2])))))
969 ;; Don't execute up events twice.
970 (while (not (widget-button-release-event-p event))
971 (setq event (read-event))))
973 (call-interactively command)))))
974 (message "You clicked somewhere weird.")))
976 (defun widget-button-press (pos &optional event)
977 "Invoke button at POS."
979 (let ((button (get-char-property pos 'button)))
981 (widget-apply-action button event)
982 (let ((command (lookup-key widget-global-map (this-command-keys))))
983 (when (commandp command)
984 (call-interactively command))))))
986 (defun widget-tabable-at (&optional pos)
987 "Return the tabable widget at POS, or nil.
988 POS defaults to the value of (point)."
989 (let ((widget (widget-at pos)))
991 (let ((order (widget-get widget :tab-order)))
997 (defvar widget-use-overlay-change t
998 "If non-nil, use overlay change functions to tab around in the buffer.
999 This is much faster, but doesn't work reliably on Emacs 19.34.")
1001 (defun widget-move (arg)
1002 "Move point to the ARG next field or button.
1003 ARG may be negative to move backward."
1004 (or (bobp) (> arg 0) (backward-char))
1007 (old (widget-tabable-at)))
1011 (goto-char (point-min))
1012 (setq wrapped (1+ wrapped)))
1013 (widget-use-overlay-change
1014 (goto-char (next-overlay-change (point))))
1019 (error "No buttons or fields found"))
1020 (let ((new (widget-tabable-at)))
1022 (unless (eq new old)
1028 (goto-char (point-max))
1029 (setq wrapped (1+ wrapped)))
1030 (widget-use-overlay-change
1031 (goto-char (previous-overlay-change (point))))
1036 (error "No buttons or fields found"))
1037 (let ((new (widget-tabable-at)))
1039 (unless (eq new old)
1040 (setq arg (1+ arg))))))
1041 (let ((new (widget-tabable-at)))
1042 (while (eq (widget-tabable-at) new)
1045 (widget-echo-help (point))
1046 (run-hooks 'widget-move-hook))
1048 (defun widget-forward (arg)
1049 "Move point to the next field or button.
1050 With optional ARG, move across that many fields."
1052 (run-hooks 'widget-forward-hook)
1055 (defun widget-backward (arg)
1056 "Move point to the previous field or button.
1057 With optional ARG, move across that many fields."
1059 (run-hooks 'widget-backward-hook)
1060 (widget-move (- arg)))
1062 ;; Since the widget code uses a `field' property to identify fields,
1063 ;; ordinary beginning-of-line does the right thing.
1064 (defalias 'widget-beginning-of-line 'beginning-of-line)
1066 (defun widget-end-of-line ()
1067 "Go to end of field or end of line, whichever is first.
1068 Trailing spaces at the end of padded fields are not considered part of
1071 ;; Ordinary end-of-line does the right thing, because we're inside
1072 ;; text with a `field' property.
1075 ;; ... except that we want to ignore trailing spaces in fields that
1076 ;; aren't terminated by a newline, because they are used as padding,
1077 ;; and ignored when extracting the entered value of the field.
1078 (skip-chars-backward " " (field-beginning (1- (point))))))
1080 (defun widget-kill-line ()
1081 "Kill to end of field or end of line, whichever is first."
1083 (let* ((field (widget-field-find (point)))
1084 (end (and field (widget-field-end field))))
1085 (if (and field (> (line-beginning-position 2) end))
1086 (kill-region (point) end)
1087 (call-interactively 'kill-line))))
1089 (defcustom widget-complete-field (lookup-key global-map "\M-\t")
1090 "Default function to call for completion inside fields."
1091 :options '(ispell-complete-word complete-tag lisp-complete-symbol)
1095 (defun widget-narrow-to-field ()
1098 (let ((field (widget-field-find (point))))
1100 (narrow-to-region (line-beginning-position) (line-end-position)))))
1102 (defun widget-complete ()
1103 "Complete content of editable field from point.
1104 When not inside a field, move to the previous button or field."
1106 (let ((field (widget-field-find (point))))
1109 (widget-narrow-to-field)
1110 (widget-apply field :complete))
1111 (error "Not in an editable field"))))
1113 ;;; Setting up the buffer.
1115 (defvar widget-field-new nil
1116 "List of all newly created editable fields in the buffer.")
1117 (make-variable-buffer-local 'widget-field-new)
1119 (defvar widget-field-list nil
1120 "List of all editable fields in the buffer.")
1121 (make-variable-buffer-local 'widget-field-list)
1123 (defun widget-at (&optional pos)
1124 "The button or field at POS (default, point)."
1125 (or (get-char-property (or pos (point)) 'button)
1126 (widget-field-at pos)))
1129 (defun widget-setup ()
1130 "Setup current buffer so editing string widgets works."
1131 (let ((inhibit-read-only t)
1132 (inhibit-modification-hooks t)
1134 (while widget-field-new
1135 (setq field (car widget-field-new)
1136 widget-field-new (cdr widget-field-new)
1137 widget-field-list (cons field widget-field-list))
1138 (let ((from (car (widget-get field :field-overlay)))
1139 (to (cdr (widget-get field :field-overlay))))
1140 (widget-specify-field field
1141 (marker-position from) (marker-position to))
1142 (set-marker from nil)
1143 (set-marker to nil))))
1145 (widget-add-change))
1147 (defvar widget-field-last nil)
1148 ;; Last field containing point.
1149 (make-variable-buffer-local 'widget-field-last)
1151 (defvar widget-field-was nil)
1152 ;; The widget data before the change.
1153 (make-variable-buffer-local 'widget-field-was)
1155 (defun widget-field-at (pos)
1156 "Return the widget field at POS, or nil if none."
1157 (let ((field (get-char-property (or pos (point)) 'field)))
1158 (if (eq field 'boundary)
1159 (get-char-property (or pos (point)) 'real-field)
1162 (defun widget-field-buffer (widget)
1163 "Return the buffer of WIDGET's editing field."
1164 (let ((overlay (widget-get widget :field-overlay)))
1165 (cond ((overlayp overlay)
1166 (overlay-buffer overlay))
1168 (marker-buffer (car overlay))))))
1170 (defun widget-field-start (widget)
1171 "Return the start of WIDGET's editing field."
1172 (let ((overlay (widget-get widget :field-overlay)))
1173 (if (overlayp overlay)
1174 (overlay-start overlay)
1177 (defun widget-field-end (widget)
1178 "Return the end of WIDGET's editing field."
1179 (let ((overlay (widget-get widget :field-overlay)))
1180 ;; Don't subtract one if local-map works at the end of the overlay,
1181 ;; or if a special `boundary' field has been added after the widget
1183 (if (overlayp overlay)
1184 (if (and (not (eq (get-char-property (overlay-end overlay)
1186 (widget-field-buffer widget))
1188 (or widget-field-add-space
1189 (null (widget-get widget :size))))
1190 (1- (overlay-end overlay))
1191 (overlay-end overlay))
1194 (defun widget-field-find (pos)
1195 "Return the field at POS.
1196 Unlike (get-char-property POS 'field) this, works with empty fields too."
1197 (let ((fields widget-field-list)
1200 (setq field (car fields)
1201 fields (cdr fields))
1202 (when (and (<= (widget-field-start field) pos)
1203 (<= pos (widget-field-end field)))
1205 (error "Overlapping fields"))
1206 (setq found field)))
1209 (defun widget-before-change (from to)
1210 ;; This is how, for example, a variable changes its state to `modified'.
1211 ;; when it is being edited.
1212 (unless inhibit-read-only
1213 (let ((from-field (widget-field-find from))
1214 (to-field (widget-field-find to)))
1215 (cond ((not (eq from-field to-field))
1216 (add-hook 'post-command-hook 'widget-add-change nil t)
1217 (signal 'text-read-only
1218 '("Change should be restricted to a single field")))
1220 (add-hook 'post-command-hook 'widget-add-change nil t)
1221 (signal 'text-read-only
1222 '("Attempt to change text outside editable field")))
1223 (widget-field-use-before-change
1224 (widget-apply from-field :notify from-field))))))
1226 (defun widget-add-change ()
1227 (remove-hook 'post-command-hook 'widget-add-change t)
1228 (add-hook 'before-change-functions 'widget-before-change nil t)
1229 (add-hook 'after-change-functions 'widget-after-change nil t))
1231 (defun widget-after-change (from to old)
1232 "Adjust field size and text properties."
1233 (let ((field (widget-field-find from))
1234 (other (widget-field-find to)))
1236 (unless (eq field other)
1237 (error "Change in different fields"))
1238 (let ((size (widget-get field :size)))
1240 (let ((begin (widget-field-start field))
1241 (end (widget-field-end field)))
1242 (cond ((< (- end begin) size)
1246 (insert-char ?\ (- (+ begin size) end))))
1247 ((> (- end begin) size)
1248 ;; Field too large and
1249 (if (or (< (point) (+ begin size))
1251 ;; Point is outside extra space.
1252 (setq begin (+ begin size))
1253 ;; Point is within the extra space.
1254 (setq begin (point)))
1257 (while (and (eq (preceding-char) ?\ )
1259 (delete-backward-char 1)))))))
1260 (widget-specify-secret field))
1261 (widget-apply field :notify field))))
1263 ;;; Widget Functions
1265 ;; These functions are used in the definition of multiple widgets.
1267 (defun widget-parent-action (widget &optional event)
1268 "Tell :parent of WIDGET to handle the :action.
1269 Optional EVENT is the event that triggered the action."
1270 (widget-apply (widget-get widget :parent) :action event))
1272 (defun widget-children-value-delete (widget)
1273 "Delete all :children and :buttons in WIDGET."
1274 (mapc 'widget-delete (widget-get widget :children))
1275 (widget-put widget :children nil)
1276 (mapc 'widget-delete (widget-get widget :buttons))
1277 (widget-put widget :buttons nil))
1279 (defun widget-children-validate (widget)
1280 "All the :children must be valid."
1281 (let ((children (widget-get widget :children))
1283 (while (and children (not found))
1284 (setq child (car children)
1285 children (cdr children)
1286 found (widget-apply child :validate)))
1289 (defun widget-child-value-get (widget)
1290 "Get the value of the first member of :children in WIDGET."
1291 (widget-value (car (widget-get widget :children))))
1293 (defun widget-child-value-inline (widget)
1294 "Get the inline value of the first member of :children in WIDGET."
1295 (widget-apply (car (widget-get widget :children)) :value-inline))
1297 (defun widget-child-validate (widget)
1298 "The result of validating the first member of :children in WIDGET."
1299 (widget-apply (car (widget-get widget :children)) :validate))
1301 (defun widget-type-value-create (widget)
1302 "Convert and instantiate the value of the :type attribute of WIDGET.
1303 Store the newly created widget in the :children attribute.
1305 The value of the :type attribute should be an unconverted widget type."
1306 (let ((value (widget-get widget :value))
1307 (type (widget-get widget :type)))
1308 (widget-put widget :children
1309 (list (widget-create-child-value widget
1310 (widget-convert type)
1313 (defun widget-type-default-get (widget)
1314 "Get default value from the :type attribute of WIDGET.
1316 The value of the :type attribute should be an unconverted widget type."
1317 (widget-default-get (widget-convert (widget-get widget :type))))
1319 (defun widget-type-match (widget value)
1320 "Non-nil if the :type value of WIDGET matches VALUE.
1322 The value of the :type attribute should be an unconverted widget type."
1323 (widget-apply (widget-convert (widget-get widget :type)) :match value))
1325 (defun widget-types-copy (widget)
1326 "Copy :args as widget types in WIDGET."
1327 (widget-put widget :args (mapcar 'widget-copy (widget-get widget :args)))
1330 ;; Made defsubst to speed up face editor creation.
1331 (defsubst widget-types-convert-widget (widget)
1332 "Convert :args as widget types in WIDGET."
1333 (widget-put widget :args (mapcar 'widget-convert (widget-get widget :args)))
1336 (defun widget-value-convert-widget (widget)
1337 "Initialize :value from :args in WIDGET."
1338 (let ((args (widget-get widget :args)))
1340 (widget-put widget :value (car args))
1341 ;; Don't convert :value here, as this is done in `widget-convert'.
1342 ;; (widget-put widget :value (widget-apply widget
1343 ;; :value-to-internal (car args)))
1344 (widget-put widget :args nil)))
1347 (defun widget-value-value-get (widget)
1348 "Return the :value property of WIDGET."
1349 (widget-get widget :value))
1351 ;;; The `default' Widget.
1353 (define-widget 'default nil
1354 "Basic widget other widgets are derived from."
1355 :value-to-internal (lambda (widget value) value)
1356 :value-to-external (lambda (widget value) value)
1357 :button-prefix 'widget-button-prefix
1358 :button-suffix 'widget-button-suffix
1359 :complete 'widget-default-complete
1360 :create 'widget-default-create
1363 :format-handler 'widget-default-format-handler
1364 :button-face-get 'widget-default-button-face-get
1365 :sample-face-get 'widget-default-sample-face-get
1366 :delete 'widget-default-delete
1368 :value-set 'widget-default-value-set
1369 :value-inline 'widget-default-value-inline
1370 :value-delete 'ignore
1371 :default-get 'widget-default-default-get
1372 :menu-tag-get 'widget-default-menu-tag-get
1374 :active 'widget-default-active
1375 :activate 'widget-specify-active
1376 :deactivate 'widget-default-deactivate
1377 :mouse-down-action #'ignore
1378 :action 'widget-default-action
1379 :notify 'widget-default-notify
1380 :prompt-value 'widget-default-prompt-value)
1382 (defun widget-default-complete (widget)
1383 "Call the value of the :complete-function property of WIDGET.
1384 If that does not exists, call the value of `widget-complete-field'."
1385 (call-interactively (or (widget-get widget :complete-function)
1386 widget-complete-field)))
1388 (defun widget-default-create (widget)
1389 "Create WIDGET at point in the current buffer."
1390 (widget-specify-insert
1391 (let ((from (point))
1392 button-begin button-end
1393 sample-begin sample-end
1396 (insert (widget-get widget :format))
1398 ;; Parse escapes in format.
1399 (while (re-search-forward "%\\(.\\)" nil t)
1400 (let ((escape (char-after (match-beginning 1))))
1401 (delete-backward-char 2)
1402 (cond ((eq escape ?%)
1405 (setq button-begin (point))
1406 (insert (widget-get-indirect widget :button-prefix)))
1408 (insert (widget-get-indirect widget :button-suffix))
1409 (setq button-end (point)))
1411 (setq sample-begin (point)))
1413 (setq sample-end (point)))
1415 (when (widget-get widget :indent)
1417 (insert-char ? (widget-get widget :indent))))
1419 (let ((image (widget-get widget :tag-glyph))
1420 (tag (widget-get widget :tag)))
1422 (widget-image-insert widget (or tag "image") image))
1426 (princ (widget-get widget :value)
1427 (current-buffer))))))
1429 (let ((doc (widget-get widget :doc)))
1431 (setq doc-begin (point))
1433 (while (eq (preceding-char) ?\n)
1434 (delete-backward-char 1))
1436 (setq doc-end (point)))))
1438 (if (and button-begin (not button-end))
1439 (widget-apply widget :value-create)
1440 (setq value-pos (point))))
1442 (widget-apply widget :format-handler escape)))))
1443 ;; Specify button, sample, and doc, and insert value.
1444 (and button-begin button-end
1445 (widget-specify-button widget button-begin button-end))
1446 (and sample-begin sample-end
1447 (widget-specify-sample widget sample-begin sample-end))
1448 (and doc-begin doc-end
1449 (widget-specify-doc widget doc-begin doc-end))
1451 (goto-char value-pos)
1452 (widget-apply widget :value-create)))
1453 (let ((from (point-min-marker))
1454 (to (point-max-marker)))
1455 (set-marker-insertion-type from t)
1456 (set-marker-insertion-type to nil)
1457 (widget-put widget :from from)
1458 (widget-put widget :to to)))
1459 (widget-clear-undo))
1461 (defun widget-default-format-handler (widget escape)
1462 ;; We recognize the %h escape by default.
1463 (let* ((buttons (widget-get widget :buttons)))
1464 (cond ((eq escape ?h)
1465 (let* ((doc-property (widget-get widget :documentation-property))
1466 (doc-try (cond ((widget-get widget :doc))
1467 ((functionp doc-property)
1468 (funcall doc-property
1469 (widget-get widget :value)))
1470 ((symbolp doc-property)
1471 (documentation-property
1472 (widget-get widget :value)
1474 (doc-text (and (stringp doc-try)
1475 (> (length doc-try) 1)
1477 (doc-indent (widget-get widget :documentation-indent)))
1479 (and (eq (preceding-char) ?\n)
1480 (widget-get widget :indent)
1481 (insert-char ? (widget-get widget :indent)))
1482 ;; The `*' in the beginning is redundant.
1483 (when (eq (aref doc-text 0) ?*)
1484 (setq doc-text (substring doc-text 1)))
1485 ;; Get rid of trailing newlines.
1486 (when (string-match "\n+\\'" doc-text)
1487 (setq doc-text (substring doc-text 0 (match-beginning 0))))
1488 (push (widget-create-child-and-convert
1489 widget 'documentation-string
1490 :indent (cond ((numberp doc-indent )
1498 (error "Unknown escape `%c'" escape)))
1499 (widget-put widget :buttons buttons)))
1501 (defun widget-default-button-face-get (widget)
1502 ;; Use :button-face or widget-button-face
1503 (or (widget-get widget :button-face)
1504 (let ((parent (widget-get widget :parent)))
1506 (widget-apply parent :button-face-get)
1507 widget-button-face))))
1509 (defun widget-default-sample-face-get (widget)
1510 ;; Use :sample-face.
1511 (widget-get widget :sample-face))
1513 (defun widget-default-delete (widget)
1514 "Remove widget from the buffer."
1515 (let ((from (widget-get widget :from))
1516 (to (widget-get widget :to))
1517 (inactive-overlay (widget-get widget :inactive))
1518 (button-overlay (widget-get widget :button-overlay))
1519 (sample-overlay (widget-get widget :sample-overlay))
1520 (doc-overlay (widget-get widget :doc-overlay))
1521 (inhibit-modification-hooks t)
1522 (inhibit-read-only t))
1523 (widget-apply widget :value-delete)
1524 (widget-children-value-delete widget)
1525 (when inactive-overlay
1526 (delete-overlay inactive-overlay))
1527 (when button-overlay
1528 (delete-overlay button-overlay))
1529 (when sample-overlay
1530 (delete-overlay sample-overlay))
1532 (delete-overlay doc-overlay))
1534 ;; Kludge: this doesn't need to be true for empty formats.
1535 (delete-region from to))
1536 (set-marker from nil)
1537 (set-marker to nil))
1538 (widget-clear-undo))
1540 (defun widget-default-value-set (widget value)
1541 "Recreate widget with new value."
1542 (let* ((old-pos (point))
1543 (from (copy-marker (widget-get widget :from)))
1544 (to (copy-marker (widget-get widget :to)))
1545 (offset (if (and (<= from old-pos) (<= old-pos to))
1546 (if (>= old-pos (1- to))
1548 (- old-pos from)))))
1549 ;;??? Bug: this ought to insert the new value before deleting the old one,
1550 ;; so that markers on either side of the value automatically
1551 ;; stay on the same side. -- rms.
1553 (goto-char (widget-get widget :from))
1554 (widget-apply widget :delete)
1555 (widget-put widget :value value)
1556 (widget-apply widget :create))
1559 (goto-char (+ (widget-get widget :to) offset 1))
1560 (goto-char (min (+ from offset) (1- (widget-get widget :to))))))))
1562 (defun widget-default-value-inline (widget)
1563 "Wrap value in a list unless it is inline."
1564 (if (widget-get widget :inline)
1565 (widget-value widget)
1566 (list (widget-value widget))))
1568 (defun widget-default-default-get (widget)
1570 (widget-get widget :value))
1572 (defun widget-default-menu-tag-get (widget)
1573 "Use tag or value for menus."
1574 (or (widget-get widget :menu-tag)
1575 (widget-get widget :tag)
1576 (widget-princ-to-string (widget-get widget :value))))
1578 (defun widget-default-active (widget)
1579 "Return t iff this widget active (user modifiable)."
1580 (or (widget-get widget :always-active)
1581 (and (not (widget-get widget :inactive))
1582 (let ((parent (widget-get widget :parent)))
1584 (widget-apply parent :active))))))
1586 (defun widget-default-deactivate (widget)
1587 "Make WIDGET inactive for user modifications."
1588 (widget-specify-inactive widget
1589 (widget-get widget :from)
1590 (widget-get widget :to)))
1592 (defun widget-default-action (widget &optional event)
1593 "Notify the parent when a widget changes."
1594 (let ((parent (widget-get widget :parent)))
1596 (widget-apply parent :notify widget event))))
1598 (defun widget-default-notify (widget child &optional event)
1599 "Pass notification to parent."
1600 (widget-default-action widget event))
1602 (defun widget-default-prompt-value (widget prompt value unbound)
1603 "Read an arbitrary value. Stolen from `set-variable'."
1604 ;; (let ((initial (if unbound
1606 ;; It would be nice if we could do a `(cons val 1)' here.
1607 ;; (prin1-to-string (custom-quote value))))))
1608 (eval-minibuffer prompt))
1610 ;;; The `item' Widget.
1612 (define-widget 'item 'default
1613 "Constant items for inclusion in other widgets."
1614 :convert-widget 'widget-value-convert-widget
1615 :value-create 'widget-item-value-create
1616 :value-delete 'ignore
1617 :value-get 'widget-value-value-get
1618 :match 'widget-item-match
1619 :match-inline 'widget-item-match-inline
1620 :action 'widget-item-action
1623 (defun widget-item-value-create (widget)
1624 "Insert the printed representation of the value."
1625 (princ (widget-get widget :value) (current-buffer)))
1627 (defun widget-item-match (widget value)
1628 ;; Match if the value is the same.
1629 (equal (widget-get widget :value) value))
1631 (defun widget-item-match-inline (widget values)
1632 ;; Match if the value is the same.
1633 (let ((value (widget-get widget :value)))
1635 (<= (length value) (length values))
1636 (let ((head (widget-sublist values 0 (length value))))
1637 (and (equal head value)
1638 (cons head (widget-sublist values (length value))))))))
1640 (defun widget-sublist (list start &optional end)
1641 "Return the sublist of LIST from START to END.
1642 If END is omitted, it defaults to the length of LIST."
1643 (if (> start 0) (setq list (nthcdr start list)))
1645 (unless (<= end start)
1646 (setq list (copy-sequence list))
1647 (setcdr (nthcdr (- end start 1) list) nil)
1649 (copy-sequence list)))
1651 (defun widget-item-action (widget &optional event)
1652 ;; Just notify itself.
1653 (widget-apply widget :notify widget event))
1655 ;;; The `push-button' Widget.
1657 ;; (defcustom widget-push-button-gui t
1658 ;; "If non nil, use GUI push buttons when available."
1662 ;; Cache already created GUI objects.
1663 ;; (defvar widget-push-button-cache nil)
1665 (defcustom widget-push-button-prefix "["
1666 "String used as prefix for buttons."
1668 :group 'widget-button)
1670 (defcustom widget-push-button-suffix "]"
1671 "String used as suffix for buttons."
1673 :group 'widget-button)
1675 (define-widget 'push-button 'item
1676 "A pushable button."
1679 :value-create 'widget-push-button-value-create
1682 (defun widget-push-button-value-create (widget)
1683 "Insert text representing the `on' and `off' states."
1684 (let* ((tag (or (widget-get widget :tag)
1685 (widget-get widget :value)))
1686 (tag-glyph (widget-get widget :tag-glyph))
1687 (text (concat widget-push-button-prefix
1688 tag widget-push-button-suffix)))
1690 (widget-image-insert widget text tag-glyph)
1693 ;; (defun widget-gui-action (widget)
1694 ;; "Apply :action for WIDGET."
1695 ;; (widget-apply-action widget (this-command-keys)))
1697 ;;; The `link' Widget.
1699 (defcustom widget-link-prefix "["
1700 "String used as prefix for links."
1702 :group 'widget-button)
1704 (defcustom widget-link-suffix "]"
1705 "String used as suffix for links."
1707 :group 'widget-button)
1709 (define-widget 'link 'item
1711 :button-prefix 'widget-link-prefix
1712 :button-suffix 'widget-link-suffix
1714 :help-echo "Follow the link."
1717 ;;; The `info-link' Widget.
1719 (define-widget 'info-link 'link
1720 "A link to an info file."
1721 :action 'widget-info-link-action)
1723 (defun widget-info-link-action (widget &optional event)
1724 "Open the info node specified by WIDGET."
1725 (info (widget-value widget)))
1727 ;;; The `url-link' Widget.
1729 (define-widget 'url-link 'link
1730 "A link to an www page."
1731 :action 'widget-url-link-action)
1733 (defun widget-url-link-action (widget &optional event)
1734 "Open the url specified by WIDGET."
1735 (browse-url (widget-value widget)))
1737 ;;; The `function-link' Widget.
1739 (define-widget 'function-link 'link
1740 "A link to an Emacs function."
1741 :action 'widget-function-link-action)
1743 (defun widget-function-link-action (widget &optional event)
1744 "Show the function specified by WIDGET."
1745 (describe-function (widget-value widget)))
1747 ;;; The `variable-link' Widget.
1749 (define-widget 'variable-link 'link
1750 "A link to an Emacs variable."
1751 :action 'widget-variable-link-action)
1753 (defun widget-variable-link-action (widget &optional event)
1754 "Show the variable specified by WIDGET."
1755 (describe-variable (widget-value widget)))
1757 ;;; The `file-link' Widget.
1759 (define-widget 'file-link 'link
1761 :action 'widget-file-link-action)
1763 (defun widget-file-link-action (widget &optional event)
1764 "Find the file specified by WIDGET."
1765 (find-file (widget-value widget)))
1767 ;;; The `emacs-library-link' Widget.
1769 (define-widget 'emacs-library-link 'link
1770 "A link to an Emacs Lisp library file."
1771 :action 'widget-emacs-library-link-action)
1773 (defun widget-emacs-library-link-action (widget &optional event)
1774 "Find the Emacs Library file specified by WIDGET."
1775 (find-file (locate-library (widget-value widget))))
1777 ;;; The `emacs-commentary-link' Widget.
1779 (define-widget 'emacs-commentary-link 'link
1780 "A link to Commentary in an Emacs Lisp library file."
1781 :action 'widget-emacs-commentary-link-action)
1783 (defun widget-emacs-commentary-link-action (widget &optional event)
1784 "Find the Commentary section of the Emacs file specified by WIDGET."
1785 (finder-commentary (widget-value widget)))
1787 ;;; The `editable-field' Widget.
1789 (define-widget 'editable-field 'default
1790 "An editable text field."
1791 :convert-widget 'widget-value-convert-widget
1792 :keymap widget-field-keymap
1794 :help-echo "M-TAB: complete field; RET: enter value"
1796 :prompt-internal 'widget-field-prompt-internal
1797 :prompt-history 'widget-field-history
1798 :prompt-value 'widget-field-prompt-value
1799 :action 'widget-field-action
1800 :validate 'widget-field-validate
1802 :error "Field's value doesn't match allowed forms"
1803 :value-create 'widget-field-value-create
1804 :value-delete 'widget-field-value-delete
1805 :value-get 'widget-field-value-get
1806 :match 'widget-field-match)
1808 (defvar widget-field-history nil
1809 "History of field minibuffer edits.")
1811 (defun widget-field-prompt-internal (widget prompt initial history)
1812 "Read string for WIDGET promptinhg with PROMPT.
1813 INITIAL is the initial input and HISTORY is a symbol containing
1815 (read-string prompt initial history))
1817 (defun widget-field-prompt-value (widget prompt value unbound)
1818 "Prompt for a string."
1819 (widget-apply widget
1821 (widget-apply widget
1822 :prompt-internal prompt
1824 (cons (widget-apply widget
1825 :value-to-internal value)
1827 (widget-get widget :prompt-history))))
1829 (defvar widget-edit-functions nil)
1831 (defun widget-field-action (widget &optional event)
1832 "Move to next field."
1834 (run-hook-with-args 'widget-edit-functions widget))
1836 (defun widget-field-validate (widget)
1837 "Valid if the content matches `:valid-regexp'."
1838 (unless (string-match (widget-get widget :valid-regexp)
1839 (widget-apply widget :value-get))
1842 (defun widget-field-value-create (widget)
1843 "Create an editable text field."
1844 (let ((size (widget-get widget :size))
1845 (value (widget-get widget :value))
1847 ;; This is changed to a real overlay in `widget-setup'. We
1848 ;; need the end points to behave differently until
1849 ;; `widget-setup' is called.
1850 (overlay (cons (make-marker) (make-marker))))
1851 (widget-put widget :field-overlay overlay)
1854 (< (length value) size)
1855 (insert-char ?\ (- size (length value))))
1856 (unless (memq widget widget-field-list)
1857 (setq widget-field-new (cons widget widget-field-new)))
1858 (move-marker (cdr overlay) (point))
1859 (set-marker-insertion-type (cdr overlay) nil)
1862 (move-marker (car overlay) from)
1863 (set-marker-insertion-type (car overlay) t)))
1865 (defun widget-field-value-delete (widget)
1866 "Remove the widget from the list of active editing fields."
1867 (setq widget-field-list (delq widget widget-field-list))
1868 (setq widget-field-new (delq widget widget-field-new))
1869 ;; These are nil if the :format string doesn't contain `%v'.
1870 (let ((overlay (widget-get widget :field-overlay)))
1871 (when (overlayp overlay)
1872 (delete-overlay overlay))))
1874 (defun widget-field-value-get (widget)
1875 "Return current text in editing field."
1876 (let ((from (widget-field-start widget))
1877 (to (widget-field-end widget))
1878 (buffer (widget-field-buffer widget))
1879 (size (widget-get widget :size))
1880 (secret (widget-get widget :secret))
1881 (old (current-buffer)))
1888 (eq (char-after (1- to)) ?\ ))
1890 (let ((result (buffer-substring-no-properties from to)))
1893 (while (< (+ from index) to)
1895 (get-char-property (+ from index) 'secret))
1896 (setq index (1+ index)))))
1899 (widget-get widget :value))))
1901 (defun widget-field-match (widget value)
1902 ;; Match any string.
1905 ;;; The `text' Widget.
1907 (define-widget 'text 'editable-field
1908 "A multiline text area."
1909 :keymap widget-text-keymap)
1911 ;;; The `menu-choice' Widget.
1913 (define-widget 'menu-choice 'default
1914 "A menu of options."
1915 :convert-widget 'widget-types-convert-widget
1916 :copy 'widget-types-copy
1917 :format "%[%t%]: %v"
1920 :void '(item :format "invalid (%t)\n")
1921 :value-create 'widget-choice-value-create
1922 :value-get 'widget-child-value-get
1923 :value-inline 'widget-child-value-inline
1924 :default-get 'widget-choice-default-get
1925 :mouse-down-action 'widget-choice-mouse-down-action
1926 :action 'widget-choice-action
1927 :error "Make a choice"
1928 :validate 'widget-choice-validate
1929 :match 'widget-choice-match
1930 :match-inline 'widget-choice-match-inline)
1932 (defun widget-choice-value-create (widget)
1933 "Insert the first choice that matches the value."
1934 (let ((value (widget-get widget :value))
1935 (args (widget-get widget :args))
1936 (explicit (widget-get widget :explicit-choice))
1938 (if (and explicit (equal value (widget-get widget :explicit-choice-value)))
1940 ;; If the user specified the choice for this value,
1941 ;; respect that choice as long as the value is the same.
1942 (widget-put widget :children (list (widget-create-child-value
1943 widget explicit value)))
1944 (widget-put widget :choice explicit))
1946 (setq current (car args)
1948 (when (widget-apply current :match value)
1949 (widget-put widget :children (list (widget-create-child-value
1950 widget current value)))
1951 (widget-put widget :choice current)
1955 (let ((void (widget-get widget :void)))
1956 (widget-put widget :children (list (widget-create-child-and-convert
1957 widget void :value value)))
1958 (widget-put widget :choice void))))))
1960 (defun widget-choice-default-get (widget)
1961 ;; Get default for the first choice.
1962 (widget-default-get (car (widget-get widget :args))))
1964 (defcustom widget-choice-toggle nil
1965 "If non-nil, a binary choice will just toggle between the values.
1966 Otherwise, the user will explicitly have to choose between the values
1967 when he invoked the menu."
1971 (defun widget-choice-mouse-down-action (widget &optional event)
1972 ;; Return non-nil if we need a menu.
1973 (let ((args (widget-get widget :args))
1974 (old (widget-get widget :choice)))
1975 (cond ((not (display-popup-menus-p))
1976 ;; No place to pop up a menu.
1978 ((< (length args) 2)
1979 ;; Empty or singleton list, just return the value.
1981 ((> (length args) widget-menu-max-size)
1982 ;; Too long, prompt.
1984 ((> (length args) 2)
1985 ;; Reasonable sized list, use menu.
1987 ((and widget-choice-toggle (memq old args))
1991 ;; Ask which of the two.
1994 (defun widget-choice-action (widget &optional event)
1996 (let ((args (widget-get widget :args))
1997 (old (widget-get widget :choice))
1998 (tag (widget-apply widget :menu-tag-get))
1999 (completion-ignore-case (widget-get widget :case-fold))
2002 ;; Remember old value.
2003 (if (and old (not (widget-apply widget :validate)))
2004 (let* ((external (widget-value widget))
2005 (internal (widget-apply old :value-to-internal external)))
2006 (widget-put old :value internal)))
2009 (cond ((= (length args) 0)
2011 ((= (length args) 1)
2013 ((and widget-choice-toggle
2016 (if (eq old (nth 0 args))
2021 (setq current (car args)
2024 (cons (cons (widget-apply current :menu-tag-get)
2027 (setq this-explicit t)
2028 (widget-choose tag (reverse choices) event))))
2030 ;; If this was an explicit user choice,
2031 ;; record the choice, and the record the value it was made for.
2032 ;; widget-choice-value-create will respect this choice,
2033 ;; as long as the value is the same.
2035 (widget-put widget :explicit-choice current)
2036 (widget-put widget :explicit-choice-value (widget-get widget :value)))
2037 (widget-value-set widget (widget-default-get current))
2039 (widget-apply widget :notify widget event)))
2040 (run-hook-with-args 'widget-edit-functions widget))
2042 (defun widget-choice-validate (widget)
2043 ;; Valid if we have made a valid choice.
2044 (if (eq (widget-get widget :void) (widget-get widget :choice))
2046 (widget-apply (car (widget-get widget :children)) :validate)))
2048 (defun widget-choice-match (widget value)
2049 ;; Matches if one of the choices matches.
2050 (let ((args (widget-get widget :args))
2052 (while (and args (not found))
2053 (setq current (car args)
2055 found (widget-apply current :match value)))
2058 (defun widget-choice-match-inline (widget values)
2059 ;; Matches if one of the choices matches.
2060 (let ((args (widget-get widget :args))
2062 (while (and args (null found))
2063 (setq current (car args)
2065 found (widget-match-inline current values)))
2068 ;;; The `toggle' Widget.
2070 (define-widget 'toggle 'item
2071 "Toggle between two states."
2073 :value-create 'widget-toggle-value-create
2074 :action 'widget-toggle-action
2075 :match (lambda (widget value) t)
2079 (defun widget-toggle-value-create (widget)
2080 "Insert text representing the `on' and `off' states."
2081 (if (widget-value widget)
2082 (let ((image (widget-get widget :on-glyph)))
2083 (and (display-graphic-p)
2085 (not (eq (car image) 'image))
2086 (widget-put widget :on-glyph (setq image (eval image))))
2087 (widget-image-insert widget
2088 (widget-get widget :on)
2090 (let ((image (widget-get widget :off-glyph)))
2091 (and (display-graphic-p)
2093 (not (eq (car image) 'image))
2094 (widget-put widget :off-glyph (setq image (eval image))))
2095 (widget-image-insert widget (widget-get widget :off) image))))
2097 (defun widget-toggle-action (widget &optional event)
2099 (widget-value-set widget (not (widget-value widget)))
2100 (widget-apply widget :notify widget event)
2101 (run-hook-with-args 'widget-edit-functions widget))
2103 ;;; The `checkbox' Widget.
2105 (define-widget 'checkbox 'toggle
2106 "A checkbox toggle."
2111 ;; We could probably do the same job as the images using single
2112 ;; space characters in a boxed face with a stretch specification to
2113 ;; make them square.
2114 :on-glyph '(create-image "\300\300\141\143\067\076\034\030"
2115 'xbm t :width 8 :height 8
2116 :background "grey75" ; like default mode line
2121 :off-glyph '(create-image (make-string 8 0)
2122 'xbm t :width 8 :height 8
2123 :background "grey75"
2127 :help-echo "Toggle this item."
2128 :action 'widget-checkbox-action)
2130 (defun widget-checkbox-action (widget &optional event)
2131 "Toggle checkbox, notify parent, and set active state of sibling."
2132 (widget-toggle-action widget event)
2133 (let ((sibling (widget-get-sibling widget)))
2135 (if (widget-value widget)
2136 (widget-apply sibling :activate)
2137 (widget-apply sibling :deactivate)))))
2139 ;;; The `checklist' Widget.
2141 (define-widget 'checklist 'default
2142 "A multiple choice widget."
2143 :convert-widget 'widget-types-convert-widget
2144 :copy 'widget-types-copy
2147 :entry-format "%b %v"
2149 :value-create 'widget-checklist-value-create
2150 :value-get 'widget-checklist-value-get
2151 :validate 'widget-checklist-validate
2152 :match 'widget-checklist-match
2153 :match-inline 'widget-checklist-match-inline)
2155 (defun widget-checklist-value-create (widget)
2156 ;; Insert all values
2157 (let ((alist (widget-checklist-match-find widget (widget-get widget :value)))
2158 (args (widget-get widget :args)))
2160 (widget-checklist-add-item widget (car args) (assq (car args) alist))
2161 (setq args (cdr args)))
2162 (widget-put widget :children (nreverse (widget-get widget :children)))))
2164 (defun widget-checklist-add-item (widget type chosen)
2165 "Create checklist item in WIDGET of type TYPE.
2166 If the item is checked, CHOSEN is a cons whose cdr is the value."
2167 (and (eq (preceding-char) ?\n)
2168 (widget-get widget :indent)
2169 (insert-char ? (widget-get widget :indent)))
2170 (widget-specify-insert
2171 (let* ((children (widget-get widget :children))
2172 (buttons (widget-get widget :buttons))
2173 (button-args (or (widget-get type :sibling-args)
2174 (widget-get widget :button-args)))
2177 (insert (widget-get widget :entry-format))
2179 ;; Parse % escapes in format.
2180 (while (re-search-forward "%\\([bv%]\\)" nil t)
2181 (let ((escape (char-after (match-beginning 1))))
2182 (delete-backward-char 2)
2183 (cond ((eq escape ?%)
2186 (setq button (apply 'widget-create-child-and-convert
2188 :value (not (null chosen))
2193 (let ((child (widget-create-child widget type)))
2194 (widget-apply child :deactivate)
2196 ((widget-get type :inline)
2197 (widget-create-child-value
2198 widget type (cdr chosen)))
2200 (widget-create-child-value
2201 widget type (car (cdr chosen)))))))
2203 (error "Unknown escape `%c'" escape)))))
2204 ;; Update properties.
2205 (and button child (widget-put child :button button))
2206 (and button (widget-put widget :buttons (cons button buttons)))
2207 (and child (widget-put widget :children (cons child children))))))
2209 (defun widget-checklist-match (widget values)
2210 ;; All values must match a type in the checklist.
2212 (null (cdr (widget-checklist-match-inline widget values)))))
2214 (defun widget-checklist-match-inline (widget values)
2215 ;; Find the values which match a type in the checklist.
2216 (let ((greedy (widget-get widget :greedy))
2217 (args (copy-sequence (widget-get widget :args)))
2220 (let ((answer (widget-checklist-match-up args values)))
2222 (let ((vals (widget-match-inline answer values)))
2223 (setq found (append found (car vals))
2225 args (delq answer args))))
2227 (setq rest (append rest (list (car values)))
2228 values (cdr values)))
2230 (setq rest (append rest values)
2234 (defun widget-checklist-match-find (widget vals)
2235 "Find the vals which match a type in the checklist.
2236 Return an alist of (TYPE MATCH)."
2237 (let ((greedy (widget-get widget :greedy))
2238 (args (copy-sequence (widget-get widget :args)))
2241 (let ((answer (widget-checklist-match-up args vals)))
2243 (let ((match (widget-match-inline answer vals)))
2244 (setq found (cons (cons answer (car match)) found)
2246 args (delq answer args))))
2248 (setq vals (cdr vals)))
2253 (defun widget-checklist-match-up (args vals)
2254 "Return the first type from ARGS that matches VALS."
2255 (let (current found)
2256 (while (and args (null found))
2257 (setq current (car args)
2259 found (widget-match-inline current vals)))
2263 (defun widget-checklist-value-get (widget)
2264 ;; The values of all selected items.
2265 (let ((children (widget-get widget :children))
2268 (setq child (car children)
2269 children (cdr children))
2270 (if (widget-value (widget-get child :button))
2271 (setq result (append result (widget-apply child :value-inline)))))
2274 (defun widget-checklist-validate (widget)
2275 ;; Ticked chilren must be valid.
2276 (let ((children (widget-get widget :children))
2278 (while (and children (not found))
2279 (setq child (car children)
2280 children (cdr children)
2281 button (widget-get child :button)
2282 found (and (widget-value button)
2283 (widget-apply child :validate))))
2286 ;;; The `option' Widget
2288 (define-widget 'option 'checklist
2289 "An widget with an optional item."
2292 ;;; The `choice-item' Widget.
2294 (define-widget 'choice-item 'item
2295 "Button items that delegate action events to their parents."
2296 :action 'widget-parent-action
2297 :format "%[%t%] \n")
2299 ;;; The `radio-button' Widget.
2301 (define-widget 'radio-button 'toggle
2302 "A radio button for use in the `radio' widget."
2303 :notify 'widget-radio-button-notify
2310 :off-glyph "radio0")
2312 (defun widget-radio-button-notify (widget child &optional event)
2314 (widget-apply (widget-get widget :parent) :action widget event))
2316 ;;; The `radio-button-choice' Widget.
2318 (define-widget 'radio-button-choice 'default
2319 "Select one of multiple options."
2320 :convert-widget 'widget-types-convert-widget
2321 :copy 'widget-types-copy
2324 :entry-format "%b %v"
2325 :value-create 'widget-radio-value-create
2326 :value-get 'widget-radio-value-get
2327 :value-inline 'widget-radio-value-inline
2328 :value-set 'widget-radio-value-set
2329 :error "You must push one of the buttons"
2330 :validate 'widget-radio-validate
2331 :match 'widget-choice-match
2332 :match-inline 'widget-choice-match-inline
2333 :action 'widget-radio-action)
2335 (defun widget-radio-value-create (widget)
2336 ;; Insert all values
2337 (let ((args (widget-get widget :args))
2340 (setq arg (car args)
2342 (widget-radio-add-item widget arg))))
2344 (defun widget-radio-add-item (widget type)
2345 "Add to radio widget WIDGET a new radio button item of type TYPE."
2346 ;; (setq type (widget-convert type))
2347 (and (eq (preceding-char) ?\n)
2348 (widget-get widget :indent)
2349 (insert-char ? (widget-get widget :indent)))
2350 (widget-specify-insert
2351 (let* ((value (widget-get widget :value))
2352 (children (widget-get widget :children))
2353 (buttons (widget-get widget :buttons))
2354 (button-args (or (widget-get type :sibling-args)
2355 (widget-get widget :button-args)))
2357 (chosen (and (null (widget-get widget :choice))
2358 (widget-apply type :match value)))
2360 (insert (widget-get widget :entry-format))
2362 ;; Parse % escapes in format.
2363 (while (re-search-forward "%\\([bv%]\\)" nil t)
2364 (let ((escape (char-after (match-beginning 1))))
2365 (delete-backward-char 2)
2366 (cond ((eq escape ?%)
2369 (setq button (apply 'widget-create-child-and-convert
2370 widget 'radio-button
2371 :value (not (null chosen))
2374 (setq child (if chosen
2375 (widget-create-child-value
2377 (widget-create-child widget type)))
2379 (widget-apply child :deactivate)))
2381 (error "Unknown escape `%c'" escape)))))
2382 ;; Update properties.
2384 (widget-put widget :choice type))
2386 (widget-put child :button button)
2387 (widget-put widget :buttons (nconc buttons (list button))))
2389 (widget-put widget :children (nconc children (list child))))
2392 (defun widget-radio-value-get (widget)
2393 ;; Get value of the child widget.
2394 (let ((chosen (widget-radio-chosen widget)))
2395 (and chosen (widget-value chosen))))
2397 (defun widget-radio-chosen (widget)
2398 "Return the widget representing the chosen radio button."
2399 (let ((children (widget-get widget :children))
2402 (setq current (car children)
2403 children (cdr children))
2404 (when (widget-apply (widget-get current :button) :value-get)
2409 (defun widget-radio-value-inline (widget)
2410 ;; Get value of the child widget.
2411 (let ((children (widget-get widget :children))
2414 (setq current (car children)
2415 children (cdr children))
2416 (when (widget-apply (widget-get current :button) :value-get)
2417 (setq found (widget-apply current :value-inline)
2421 (defun widget-radio-value-set (widget value)
2422 ;; We can't just delete and recreate a radio widget, since children
2423 ;; can be added after the original creation and won't be recreated
2425 (let ((children (widget-get widget :children))
2428 (setq current (car children)
2429 children (cdr children))
2430 (let* ((button (widget-get current :button))
2431 (match (and (not found)
2432 (widget-apply current :match value))))
2433 (widget-value-set button match)
2436 (widget-value-set current value)
2437 (widget-apply current :activate))
2438 (widget-apply current :deactivate))
2439 (setq found (or found match))))))
2441 (defun widget-radio-validate (widget)
2442 ;; Valid if we have made a valid choice.
2443 (let ((children (widget-get widget :children))
2444 current found button)
2445 (while (and children (not found))
2446 (setq current (car children)
2447 children (cdr children)
2448 button (widget-get current :button)
2449 found (widget-apply button :value-get)))
2451 (widget-apply current :validate)
2454 (defun widget-radio-action (widget child event)
2455 ;; Check if a radio button was pressed.
2456 (let ((children (widget-get widget :children))
2457 (buttons (widget-get widget :buttons))
2459 (when (memq child buttons)
2461 (setq current (car children)
2462 children (cdr children))
2463 (let* ((button (widget-get current :button)))
2464 (cond ((eq child button)
2465 (widget-value-set button t)
2466 (widget-apply current :activate))
2467 ((widget-value button)
2468 (widget-value-set button nil)
2469 (widget-apply current :deactivate)))))))
2470 ;; Pass notification to parent.
2471 (widget-apply widget :notify child event))
2473 ;;; The `insert-button' Widget.
2475 (define-widget 'insert-button 'push-button
2476 "An insert button for the `editable-list' widget."
2478 :help-echo "Insert a new item into the list at this position."
2479 :action 'widget-insert-button-action)
2481 (defun widget-insert-button-action (widget &optional event)
2482 ;; Ask the parent to insert a new item.
2483 (widget-apply (widget-get widget :parent)
2484 :insert-before (widget-get widget :widget)))
2486 ;;; The `delete-button' Widget.
2488 (define-widget 'delete-button 'push-button
2489 "A delete button for the `editable-list' widget."
2491 :help-echo "Delete this item from the list."
2492 :action 'widget-delete-button-action)
2494 (defun widget-delete-button-action (widget &optional event)
2495 ;; Ask the parent to insert a new item.
2496 (widget-apply (widget-get widget :parent)
2497 :delete-at (widget-get widget :widget)))
2499 ;;; The `editable-list' Widget.
2501 ;; (defcustom widget-editable-list-gui nil
2502 ;; "If non nil, use GUI push-buttons in editable list when available."
2506 (define-widget 'editable-list 'default
2507 "A variable list of widgets of the same type."
2508 :convert-widget 'widget-types-convert-widget
2509 :copy 'widget-types-copy
2512 :format-handler 'widget-editable-list-format-handler
2513 :entry-format "%i %d %v"
2514 :value-create 'widget-editable-list-value-create
2515 :value-get 'widget-editable-list-value-get
2516 :validate 'widget-children-validate
2517 :match 'widget-editable-list-match
2518 :match-inline 'widget-editable-list-match-inline
2519 :insert-before 'widget-editable-list-insert-before
2520 :delete-at 'widget-editable-list-delete-at)
2522 (defun widget-editable-list-format-handler (widget escape)
2523 ;; We recognize the insert button.
2524 ;; (let ((widget-push-button-gui widget-editable-list-gui))
2525 (cond ((eq escape ?i)
2526 (and (widget-get widget :indent)
2527 (insert-char ?\ (widget-get widget :indent)))
2528 (apply 'widget-create-child-and-convert
2529 widget 'insert-button
2530 (widget-get widget :append-button-args)))
2532 (widget-default-format-handler widget escape)))
2536 (defun widget-editable-list-value-create (widget)
2537 ;; Insert all values
2538 (let* ((value (widget-get widget :value))
2539 (type (nth 0 (widget-get widget :args)))
2541 (widget-put widget :value-pos (copy-marker (point)))
2542 (set-marker-insertion-type (widget-get widget :value-pos) t)
2544 (let ((answer (widget-match-inline type value)))
2546 (setq children (cons (widget-editable-list-entry-create
2548 (if (widget-get type :inline)
2555 (widget-put widget :children (nreverse children))))
2557 (defun widget-editable-list-value-get (widget)
2558 ;; Get value of the child widget.
2559 (apply 'append (mapcar (lambda (child) (widget-apply child :value-inline))
2560 (widget-get widget :children))))
2562 (defun widget-editable-list-match (widget value)
2563 ;; Value must be a list and all the members must match the type.
2565 (null (cdr (widget-editable-list-match-inline widget value)))))
2567 (defun widget-editable-list-match-inline (widget value)
2568 (let ((type (nth 0 (widget-get widget :args)))
2571 (while (and value ok)
2572 (let ((answer (widget-match-inline type value)))
2574 (setq found (append found (car answer))
2577 (cons found value)))
2579 (defun widget-editable-list-insert-before (widget before)
2580 ;; Insert a new child in the list of children.
2582 (let ((children (widget-get widget :children))
2583 (inhibit-read-only t)
2584 before-change-functions
2585 after-change-functions)
2587 (goto-char (widget-get before :entry-from)))
2589 (goto-char (widget-get widget :value-pos))))
2590 (let ((child (widget-editable-list-entry-create
2592 (when (< (widget-get child :entry-from) (widget-get widget :from))
2593 (set-marker (widget-get widget :from)
2594 (widget-get child :entry-from)))
2595 (if (eq (car children) before)
2596 (widget-put widget :children (cons child children))
2597 (while (not (eq (car (cdr children)) before))
2598 (setq children (cdr children)))
2599 (setcdr children (cons child (cdr children)))))))
2601 (widget-apply widget :notify widget))
2603 (defun widget-editable-list-delete-at (widget child)
2604 ;; Delete child from list of children.
2606 (let ((buttons (copy-sequence (widget-get widget :buttons)))
2608 (inhibit-read-only t)
2609 before-change-functions
2610 after-change-functions)
2612 (setq button (car buttons)
2613 buttons (cdr buttons))
2614 (when (eq (widget-get button :widget) child)
2616 :buttons (delq button (widget-get widget :buttons)))
2617 (widget-delete button))))
2618 (let ((entry-from (widget-get child :entry-from))
2619 (entry-to (widget-get child :entry-to))
2620 (inhibit-read-only t)
2621 before-change-functions
2622 after-change-functions)
2623 (widget-delete child)
2624 (delete-region entry-from entry-to)
2625 (set-marker entry-from nil)
2626 (set-marker entry-to nil))
2627 (widget-put widget :children (delq child (widget-get widget :children))))
2629 (widget-apply widget :notify widget))
2631 (defun widget-editable-list-entry-create (widget value conv)
2632 ;; Create a new entry to the list.
2633 (let ((type (nth 0 (widget-get widget :args)))
2634 ;; (widget-push-button-gui widget-editable-list-gui)
2635 child delete insert)
2636 (widget-specify-insert
2638 (and (widget-get widget :indent)
2639 (insert-char ?\ (widget-get widget :indent)))
2640 (insert (widget-get widget :entry-format)))
2641 ;; Parse % escapes in format.
2642 (while (re-search-forward "%\\(.\\)" nil t)
2643 (let ((escape (char-after (match-beginning 1))))
2644 (delete-backward-char 2)
2645 (cond ((eq escape ?%)
2648 (setq insert (apply 'widget-create-child-and-convert
2649 widget 'insert-button
2650 (widget-get widget :insert-button-args))))
2652 (setq delete (apply 'widget-create-child-and-convert
2653 widget 'delete-button
2654 (widget-get widget :delete-button-args))))
2657 (setq child (widget-create-child-value
2659 (setq child (widget-create-child-value
2660 widget type (widget-default-get type)))))
2662 (error "Unknown escape `%c'" escape)))))
2663 (let ((buttons (widget-get widget :buttons)))
2664 (if insert (push insert buttons))
2665 (if delete (push delete buttons))
2666 (widget-put widget :buttons buttons))
2667 (let ((entry-from (point-min-marker))
2668 (entry-to (point-max-marker)))
2669 (set-marker-insertion-type entry-from t)
2670 (set-marker-insertion-type entry-to nil)
2671 (widget-put child :entry-from entry-from)
2672 (widget-put child :entry-to entry-to)))
2673 (if insert (widget-put insert :widget child))
2674 (if delete (widget-put delete :widget child))
2677 ;;; The `group' Widget.
2679 (define-widget 'group 'default
2680 "A widget which groups other widgets inside."
2681 :convert-widget 'widget-types-convert-widget
2682 :copy 'widget-types-copy
2684 :value-create 'widget-group-value-create
2685 :value-get 'widget-editable-list-value-get
2686 :default-get 'widget-group-default-get
2687 :validate 'widget-children-validate
2688 :match 'widget-group-match
2689 :match-inline 'widget-group-match-inline)
2691 (defun widget-group-value-create (widget)
2692 ;; Create each component.
2693 (let ((args (widget-get widget :args))
2694 (value (widget-get widget :value))
2695 arg answer children)
2697 (setq arg (car args)
2699 answer (widget-match-inline arg value)
2701 (and (eq (preceding-char) ?\n)
2702 (widget-get widget :indent)
2703 (insert-char ?\ (widget-get widget :indent)))
2704 (push (cond ((null answer)
2705 (widget-create-child widget arg))
2706 ((widget-get arg :inline)
2707 (widget-create-child-value widget arg (car answer)))
2709 (widget-create-child-value widget arg (car (car answer)))))
2711 (widget-put widget :children (nreverse children))))
2713 (defun widget-group-default-get (widget)
2714 ;; Get the default of the components.
2715 (mapcar 'widget-default-get (widget-get widget :args)))
2717 (defun widget-group-match (widget values)
2718 ;; Match if the components match.
2720 (let ((match (widget-group-match-inline widget values)))
2721 (and match (null (cdr match))))))
2723 (defun widget-group-match-inline (widget vals)
2724 ;; Match if the components match.
2725 (let ((args (widget-get widget :args))
2726 argument answer found)
2728 (setq argument (car args)
2730 answer (widget-match-inline argument vals))
2732 (setq vals (cdr answer)
2733 found (append found (car answer)))
2737 (cons found vals))))
2739 ;;; The `visibility' Widget.
2741 (define-widget 'visibility 'item
2742 "An indicator and manipulator for hidden items."
2748 :value-create 'widget-visibility-value-create
2749 :action 'widget-toggle-action
2750 :match (lambda (widget value) t))
2752 (defun widget-visibility-value-create (widget)
2753 ;; Insert text representing the `on' and `off' states.
2754 (let ((on (widget-get widget :on))
2755 (off (widget-get widget :off)))
2757 (setq on (concat widget-push-button-prefix
2759 widget-push-button-suffix))
2762 (setq off (concat widget-push-button-prefix
2764 widget-push-button-suffix))
2766 (if (widget-value widget)
2767 (widget-image-insert widget on "down" "down-pushed")
2768 (widget-image-insert widget off "right" "right-pushed"))))
2770 ;;; The `documentation-link' Widget.
2772 ;; This is a helper widget for `documentation-string'.
2774 (define-widget 'documentation-link 'link
2775 "Link type used in documentation strings."
2777 :help-echo "Describe this symbol"
2778 :action 'widget-documentation-link-action)
2780 (defun widget-documentation-link-action (widget &optional event)
2781 "Display documentation for WIDGET's value. Ignore optional argument EVENT."
2782 (let* ((string (widget-get widget :value))
2783 (symbol (intern string)))
2784 (if (and (fboundp symbol) (boundp symbol))
2785 ;; If there are two doc strings, give the user a way to pick one.
2786 (apropos (concat "\\`" (regexp-quote string) "\\'"))
2787 (if (fboundp symbol)
2788 (describe-function symbol)
2789 (describe-variable symbol)))))
2791 (defcustom widget-documentation-links t
2792 "Add hyperlinks to documentation strings when non-nil."
2794 :group 'widget-documentation)
2796 (defcustom widget-documentation-link-regexp "`\\([^\n`' ]+\\)'"
2797 "Regexp for matching potential links in documentation strings.
2798 The first group should be the link itself."
2800 :group 'widget-documentation)
2802 (defcustom widget-documentation-link-p 'intern-soft
2803 "Predicate used to test if a string is useful as a link.
2804 The value should be a function. The function will be called one
2805 argument, a string, and should return non-nil if there should be a
2806 link for that string."
2808 :options '(widget-documentation-link-p)
2809 :group 'widget-documentation)
2811 (defcustom widget-documentation-link-type 'documentation-link
2812 "Widget type used for links in documentation strings."
2814 :group 'widget-documentation)
2816 (defun widget-documentation-link-add (widget from to)
2817 (widget-specify-doc widget from to)
2818 (when widget-documentation-links
2819 (let ((regexp widget-documentation-link-regexp)
2820 (buttons (widget-get widget :buttons))
2821 (widget-mouse-face (default-value 'widget-mouse-face))
2822 (widget-button-face widget-documentation-face)
2823 (widget-button-pressed-face widget-documentation-face))
2826 (while (re-search-forward regexp to t)
2827 (let ((name (match-string 1))
2828 (begin (match-beginning 1))
2829 (end (match-end 1)))
2830 (when (funcall widget-documentation-link-p name)
2831 (push (widget-convert-button widget-documentation-link-type
2832 begin end :value name)
2834 (widget-put widget :buttons buttons)))
2835 (let ((indent (widget-get widget :indent)))
2836 (when (and indent (not (zerop indent)))
2839 (narrow-to-region from to)
2840 (goto-char (point-min))
2841 (while (search-forward "\n" nil t)
2842 (insert-char ?\ indent)))))))
2844 ;;; The `documentation-string' Widget.
2846 (define-widget 'documentation-string 'item
2847 "A documentation string."
2849 :action 'widget-documentation-string-action
2850 :value-create 'widget-documentation-string-value-create)
2852 (defun widget-documentation-string-value-create (widget)
2853 ;; Insert documentation string.
2854 (let ((doc (widget-value widget))
2855 (indent (widget-get widget :indent))
2856 (shown (widget-get (widget-get widget :parent) :documentation-shown))
2858 (if (string-match "\n" doc)
2859 (let ((before (substring doc 0 (match-beginning 0)))
2860 (after (substring doc (match-beginning 0)))
2863 (widget-documentation-link-add widget start (point))
2865 (widget-create-child-and-convert
2867 :help-echo "Show or hide rest of the documentation."
2871 :action 'widget-parent-action
2874 (setq start (point))
2875 (when (and indent (not (zerop indent)))
2876 (insert-char ?\ indent))
2878 (widget-documentation-link-add widget start (point)))
2879 (widget-put widget :buttons (list button)))
2881 (widget-documentation-link-add widget start (point))))
2884 (defun widget-documentation-string-action (widget &rest ignore)
2885 ;; Toggle documentation.
2886 (let ((parent (widget-get widget :parent)))
2887 (widget-put parent :documentation-shown
2888 (not (widget-get parent :documentation-shown))))
2890 (widget-value-set widget (widget-value widget)))
2892 ;;; The Sexp Widgets.
2894 (define-widget 'const 'item
2895 "An immutable sexp."
2896 :prompt-value 'widget-const-prompt-value
2899 (defun widget-const-prompt-value (widget prompt value unbound)
2900 ;; Return the value of the const.
2901 (widget-value widget))
2903 (define-widget 'function-item 'const
2904 "An immutable function name."
2906 :documentation-property (lambda (symbol)
2908 (documentation symbol t)
2911 (define-widget 'variable-item 'const
2912 "An immutable variable name."
2914 :documentation-property 'variable-documentation)
2916 (define-widget 'other 'sexp
2917 "Matches any value, but doesn't let the user edit the value.
2918 This is useful as last item in a `choice' widget.
2919 You should use this widget type with a default value,
2920 as in (other DEFAULT) or (other :tag \"NAME\" DEFAULT).
2921 If the user selects this alternative, that specifies DEFAULT
2927 (defvar widget-string-prompt-value-history nil
2928 "History of input to `widget-string-prompt-value'.")
2930 (define-widget 'string 'editable-field
2933 :format "%{%t%}: %v"
2934 :complete-function 'ispell-complete-word
2935 :prompt-history 'widget-string-prompt-value-history)
2937 (define-widget 'regexp 'string
2938 "A regular expression."
2939 :match 'widget-regexp-match
2940 :validate 'widget-regexp-validate
2941 ;; Doesn't work well with terminating newline.
2942 ;; :value-face 'widget-single-line-field-face
2945 (defun widget-regexp-match (widget value)
2946 ;; Match valid regexps.
2947 (and (stringp value)
2950 (string-match value ""))
2953 (defun widget-regexp-validate (widget)
2954 "Check that the value of WIDGET is a valid regexp."
2955 (condition-case data
2957 (string-match (widget-value widget) ""))
2958 (error (widget-put widget :error (error-message-string data))
2961 (define-widget 'file 'string
2963 It will read a file name from the minibuffer when invoked."
2964 :complete-function 'widget-file-complete
2965 :prompt-value 'widget-file-prompt-value
2966 :format "%{%t%}: %v"
2967 ;; Doesn't work well with terminating newline.
2968 ;; :value-face 'widget-single-line-field-face
2971 (defun widget-file-complete ()
2972 "Perform completion on file name preceding point."
2974 (let* ((end (point))
2975 (beg (save-excursion
2976 (skip-chars-backward "^ ")
2978 (pattern (buffer-substring beg end))
2979 (name-part (file-name-nondirectory pattern))
2980 (directory (file-name-directory pattern))
2981 (completion (file-name-completion name-part directory)))
2982 (cond ((eq completion t))
2984 (message "Can't find completion for \"%s\"" pattern)
2986 ((not (string= name-part completion))
2987 (delete-region beg end)
2988 (insert (expand-file-name completion directory)))
2990 (message "Making completion list...")
2991 (with-output-to-temp-buffer "*Completions*"
2992 (display-completion-list
2993 (sort (file-name-all-completions name-part directory)
2995 (message "Making completion list...%s" "done")))))
2997 (defun widget-file-prompt-value (widget prompt value unbound)
2998 ;; Read file from minibuffer.
2999 (abbreviate-file-name
3001 (read-file-name prompt)
3002 (let ((prompt2 (format "%s (default %s) " prompt value))
3003 (dir (file-name-directory value))
3004 (file (file-name-nondirectory value))
3005 (must-match (widget-get widget :must-match)))
3006 (read-file-name prompt2 dir nil must-match file)))))
3008 ;;;(defun widget-file-action (widget &optional event)
3009 ;;; ;; Read a file name from the minibuffer.
3010 ;;; (let* ((value (widget-value widget))
3011 ;;; (dir (file-name-directory value))
3012 ;;; (file (file-name-nondirectory value))
3013 ;;; (menu-tag (widget-apply widget :menu-tag-get))
3014 ;;; (must-match (widget-get widget :must-match))
3015 ;;; (answer (read-file-name (concat menu-tag ": (default `" value "') ")
3016 ;;; dir nil must-match file)))
3017 ;;; (widget-value-set widget (abbreviate-file-name answer))
3019 ;;; (widget-apply widget :notify widget event)))
3021 ;; Fixme: use file-name-as-directory.
3022 (define-widget 'directory 'file
3023 "A directory widget.
3024 It will read a directory name from the minibuffer when invoked."
3027 (defvar widget-symbol-prompt-value-history nil
3028 "History of input to `widget-symbol-prompt-value'.")
3030 (define-widget 'symbol 'editable-field
3034 :format "%{%t%}: %v"
3035 :match (lambda (widget value) (symbolp value))
3036 :complete-function 'lisp-complete-symbol
3037 :prompt-internal 'widget-symbol-prompt-internal
3038 :prompt-match 'symbolp
3039 :prompt-history 'widget-symbol-prompt-value-history
3040 :value-to-internal (lambda (widget value)
3044 :value-to-external (lambda (widget value)
3049 (defun widget-symbol-prompt-internal (widget prompt initial history)
3050 ;; Read file from minibuffer.
3051 (let ((answer (completing-read prompt obarray
3052 (widget-get widget :prompt-match)
3053 nil initial history)))
3054 (if (and (stringp answer)
3055 (not (zerop (length answer))))
3057 (error "No value"))))
3059 (defvar widget-function-prompt-value-history nil
3060 "History of input to `widget-function-prompt-value'.")
3062 (define-widget 'function 'sexp
3064 :complete-function (lambda ()
3066 (lisp-complete-symbol 'fboundp))
3067 :prompt-value 'widget-field-prompt-value
3068 :prompt-internal 'widget-symbol-prompt-internal
3069 :prompt-match 'fboundp
3070 :prompt-history 'widget-function-prompt-value-history
3071 :action 'widget-field-action
3072 :match-alternatives '(functionp)
3073 :validate (lambda (widget)
3074 (unless (functionp (widget-value widget))
3075 (widget-put widget :error (format "Invalid function: %S"
3076 (widget-value widget)))
3081 (defvar widget-variable-prompt-value-history nil
3082 "History of input to `widget-variable-prompt-value'.")
3084 (define-widget 'variable 'symbol
3086 :prompt-match 'boundp
3087 :prompt-history 'widget-variable-prompt-value-history
3088 :complete-function (lambda ()
3090 (lisp-complete-symbol 'boundp))
3093 (defvar widget-coding-system-prompt-value-history nil
3094 "History of input to `widget-coding-system-prompt-value'.")
3096 (define-widget 'coding-system 'symbol
3097 "A MULE coding-system."
3098 :format "%{%t%}: %v"
3099 :tag "Coding system"
3101 :prompt-history 'widget-coding-system-prompt-value-history
3102 :prompt-value 'widget-coding-system-prompt-value
3103 :action 'widget-coding-system-action
3104 :complete-function (lambda ()
3106 (lisp-complete-symbol 'coding-system-p))
3107 :validate (lambda (widget)
3108 (unless (coding-system-p (widget-value widget))
3109 (widget-put widget :error (format "Invalid coding system: %S"
3110 (widget-value widget)))
3113 :prompt-match 'coding-system-p)
3115 (defun widget-coding-system-prompt-value (widget prompt value unbound)
3116 "Read coding-system from minibuffer."
3117 (if (widget-get widget :base-only)
3119 (completing-read (format "%s (default %s) " prompt value)
3120 (mapcar #'list (coding-system-list t)) nil nil nil
3121 coding-system-history))
3122 (read-coding-system (format "%s (default %s) " prompt value) value)))
3124 (defun widget-coding-system-action (widget &optional event)
3126 (widget-coding-system-prompt-value
3128 (widget-apply widget :menu-tag-get)
3129 (widget-value widget)
3131 (widget-value-set widget answer)
3132 (widget-apply widget :notify widget event)
3135 (define-widget 'sexp 'editable-field
3136 "An arbitrary Lisp expression."
3137 :tag "Lisp expression"
3138 :format "%{%t%}: %v"
3140 :validate 'widget-sexp-validate
3141 :match (lambda (widget value) t)
3142 :value-to-internal 'widget-sexp-value-to-internal
3143 :value-to-external (lambda (widget value) (read value))
3144 :prompt-history 'widget-sexp-prompt-value-history
3145 :prompt-value 'widget-sexp-prompt-value)
3147 (defun widget-sexp-value-to-internal (widget value)
3148 ;; Use pp for printer representation.
3149 (let ((pp (if (symbolp value)
3150 (prin1-to-string value)
3151 (pp-to-string value))))
3152 (while (string-match "\n\\'" pp)
3153 (setq pp (substring pp 0 -1)))
3154 (if (or (string-match "\n\\'" pp)
3159 (defun widget-sexp-validate (widget)
3160 ;; Valid if we can read the string and there is no junk left after it.
3162 (insert (widget-apply widget :value-get))
3163 (goto-char (point-min))
3165 (condition-case data
3167 ;; Avoid a confusing end-of-file error.
3168 (skip-syntax-forward "\\s-")
3170 (setq err "Empty sexp -- use `nil'?")
3171 (unless (widget-apply widget :match (read (current-buffer)))
3172 (setq err (widget-get widget :type-error))))
3173 ;; Allow whitespace after expression.
3174 (skip-syntax-forward "\\s-")
3175 (if (and (not (eobp))
3177 (setq err (format "Junk at end of expression: %s"
3178 (buffer-substring (point)
3180 (end-of-file ; Avoid confusing error message.
3181 (setq err "Unbalanced sexp"))
3182 (error (setq err (error-message-string data))))
3185 (widget-put widget :error err)
3188 (defvar widget-sexp-prompt-value-history nil
3189 "History of input to `widget-sexp-prompt-value'.")
3191 (defun widget-sexp-prompt-value (widget prompt value unbound)
3192 ;; Read an arbitrary sexp.
3193 (let ((found (read-string prompt
3194 (if unbound nil (cons (prin1-to-string value) 0))
3195 (widget-get widget :prompt-history))))
3196 (let ((answer (read-from-string found)))
3197 (unless (= (cdr answer) (length found))
3198 (error "Junk at end of expression: %s"
3199 (substring found (cdr answer))))
3202 (define-widget 'restricted-sexp 'sexp
3203 "A Lisp expression restricted to values that match.
3204 To use this type, you must define :match or :match-alternatives."
3205 :type-error "The specified value is not valid"
3206 :match 'widget-restricted-sexp-match
3207 :value-to-internal (lambda (widget value)
3208 (if (widget-apply widget :match value)
3209 (prin1-to-string value)
3212 (defun widget-restricted-sexp-match (widget value)
3213 (let ((alternatives (widget-get widget :match-alternatives))
3215 (while (and alternatives (not matched))
3216 (if (cond ((functionp (car alternatives))
3217 (funcall (car alternatives) value))
3218 ((and (consp (car alternatives))
3219 (eq (car (car alternatives)) 'quote))
3220 (eq value (nth 1 (car alternatives)))))
3222 (setq alternatives (cdr alternatives)))
3225 (define-widget 'integer 'restricted-sexp
3229 :type-error "This field should contain an integer"
3230 :match-alternatives '(integerp))
3232 (define-widget 'number 'restricted-sexp
3233 "A number (floating point or integer)."
3236 :type-error "This field should contain a number (floating point or integer)"
3237 :match-alternatives '(numberp))
3239 (define-widget 'float 'restricted-sexp
3240 "A floating point number."
3241 :tag "Floating point number"
3243 :type-error "This field should contain a floating point number"
3244 :match-alternatives '(floatp))
3246 (define-widget 'character 'editable-field
3251 :format "%{%t%}: %v\n"
3252 :valid-regexp "\\`.\\'"
3253 :error "This field should contain a single character"
3254 :value-to-internal (lambda (widget value)
3257 (char-to-string value)))
3258 :value-to-external (lambda (widget value)
3262 :match (lambda (widget value)
3263 (char-valid-p value)))
3265 (define-widget 'list 'group
3268 :format "%{%t%}:\n%v")
3270 (define-widget 'vector 'group
3273 :format "%{%t%}:\n%v"
3274 :match 'widget-vector-match
3275 :value-to-internal (lambda (widget value) (append value nil))
3276 :value-to-external (lambda (widget value) (apply 'vector value)))
3278 (defun widget-vector-match (widget value)
3279 (and (vectorp value)
3280 (widget-group-match widget
3281 (widget-apply widget :value-to-internal value))))
3283 (define-widget 'cons 'group
3286 :format "%{%t%}:\n%v"
3287 :match 'widget-cons-match
3288 :value-to-internal (lambda (widget value)
3289 (list (car value) (cdr value)))
3290 :value-to-external (lambda (widget value)
3291 (apply 'cons value)))
3293 (defun widget-cons-match (widget value)
3295 (widget-group-match widget
3296 (widget-apply widget :value-to-internal value))))
3298 ;;; The `lazy' Widget.
3300 ;; Recursive datatypes.
3302 (define-widget 'lazy 'default
3303 "Base widget for recursive datastructures.
3305 The `lazy' widget will, when instantiated, contain a single inferior
3306 widget, of the widget type specified by the :type parameter. The
3307 value of the `lazy' widget is the same as the value of the inferior
3308 widget. When deriving a new widget from the 'lazy' widget, the :type
3309 parameter is allowed to refer to the widget currently being defined,
3310 thus allowing recursive datastructures to be described.
3312 The :type parameter takes the same arguments as the defcustom
3313 parameter with the same name.
3315 Most composite widgets, i.e. widgets containing other widgets, does
3316 not allow recursion. That is, when you define a new widget type, none
3317 of the inferior widgets may be of the same type you are currently
3320 In Lisp, however, it is custom to define datastructures in terms of
3321 themselves. A list, for example, is defined as either nil, or a cons
3322 cell whose cdr itself is a list. The obvious way to translate this
3323 into a widget type would be
3325 (define-widget 'my-list 'choice
3326 \"A list of sexps.\"
3328 :args '((const nil) (cons :value (nil) sexp my-list)))
3330 Here we attempt to define my-list as a choice of either the constant
3331 nil, or a cons-cell containing a sexp and my-lisp. This will not work
3332 because the `choice' widget does not allow recursion.
3334 Using the `lazy' widget you can overcome this problem, as in this
3337 (define-widget 'sexp-list 'lazy
3338 \"A list of sexps.\"
3340 :type '(choice (const nil) (cons :value (nil) sexp sexp-list)))"
3341 :format "%{%t%}: %v"
3342 ;; We don't convert :type because we want to allow recursive
3343 ;; datastructures. This is slow, so we should not create speed
3344 ;; critical widgets by deriving from this.
3345 :convert-widget 'widget-value-convert-widget
3346 :value-create 'widget-type-value-create
3347 :value-get 'widget-child-value-get
3348 :value-inline 'widget-child-value-inline
3349 :default-get 'widget-type-default-get
3350 :match 'widget-type-match
3351 :validate 'widget-child-validate)
3354 ;;; The `plist' Widget.
3358 (define-widget 'plist 'list
3360 :key-type '(symbol :tag "Key")
3361 :value-type '(sexp :tag "Value")
3362 :convert-widget 'widget-plist-convert-widget
3365 (defvar widget-plist-value-type) ;Dynamic variable
3367 (defun widget-plist-convert-widget (widget)
3368 ;; Handle `:options'.
3369 (let* ((options (widget-get widget :options))
3370 (widget-plist-value-type (widget-get widget :value-type))
3371 (other `(editable-list :inline t
3373 ,(widget-get widget :key-type)
3374 ,widget-plist-value-type)))
3376 (list `(checklist :inline t
3378 ,@(mapcar 'widget-plist-convert-option
3382 (widget-put widget :args args)
3385 (defun widget-plist-convert-option (option)
3386 ;; Convert a single plist option.
3387 (let (key-type value-type)
3389 (let ((key (nth 0 option)))
3390 (setq value-type (nth 1 option))
3393 (setq key-type `(const ,key))))
3394 (setq key-type `(const ,option)
3395 value-type widget-plist-value-type))
3396 `(group :format "Key: %v" :inline t ,key-type ,value-type)))
3399 ;;; The `alist' Widget.
3401 ;; Association lists.
3403 (define-widget 'alist 'list
3404 "An association list."
3405 :key-type '(sexp :tag "Key")
3406 :value-type '(sexp :tag "Value")
3407 :convert-widget 'widget-alist-convert-widget
3410 (defvar widget-alist-value-type) ;Dynamic variable
3412 (defun widget-alist-convert-widget (widget)
3413 ;; Handle `:options'.
3414 (let* ((options (widget-get widget :options))
3415 (widget-alist-value-type (widget-get widget :value-type))
3416 (other `(editable-list :inline t
3418 ,(widget-get widget :key-type)
3419 ,widget-alist-value-type)))
3421 (list `(checklist :inline t
3423 ,@(mapcar 'widget-alist-convert-option
3427 (widget-put widget :args args)
3430 (defun widget-alist-convert-option (option)
3431 ;; Convert a single alist option.
3432 (let (key-type value-type)
3434 (let ((key (nth 0 option)))
3435 (setq value-type (nth 1 option))
3438 (setq key-type `(const ,key))))
3439 (setq key-type `(const ,option)
3440 value-type widget-alist-value-type))
3441 `(cons :format "Key: %v" ,key-type ,value-type)))
3443 (define-widget 'choice 'menu-choice
3444 "A union of several sexp types."
3446 :format "%{%t%}: %[Value Menu%] %v"
3447 :button-prefix 'widget-push-button-prefix
3448 :button-suffix 'widget-push-button-suffix
3449 :prompt-value 'widget-choice-prompt-value)
3451 (defun widget-choice-prompt-value (widget prompt value unbound)
3453 (let ((args (widget-get widget :args))
3454 (completion-ignore-case (widget-get widget :case-fold))
3455 current choices old)
3456 ;; Find the first arg that matches VALUE.
3459 (if (widget-apply (car look) :match value)
3460 (setq old (car look)
3462 (setq look (cdr look)))))
3465 (cond ((= (length args) 0)
3467 ((= (length args) 1)
3469 ((and (= (length args) 2)
3471 (if (eq old (nth 0 args))
3476 (setq current (car args)
3479 (cons (cons (widget-apply current :menu-tag-get)
3482 (let ((val (completing-read prompt choices nil t)))
3484 (let ((try (try-completion val choices)))
3487 (cdr (assoc val choices)))
3490 (widget-prompt-value current prompt nil t)
3493 (define-widget 'radio 'radio-button-choice
3494 "A union of several sexp types."
3496 :format "%{%t%}:\n%v"
3497 :prompt-value 'widget-choice-prompt-value)
3499 (define-widget 'repeat 'editable-list
3500 "A variable length homogeneous list."
3502 :format "%{%t%}:\n%v%i\n")
3504 (define-widget 'set 'checklist
3505 "A list of members from a fixed set."
3507 :format "%{%t%}:\n%v")
3509 (define-widget 'boolean 'toggle
3510 "To be nil or non-nil, that is the question."
3512 :prompt-value 'widget-boolean-prompt-value
3513 :button-prefix 'widget-push-button-prefix
3514 :button-suffix 'widget-push-button-suffix
3515 :format "%{%t%}: %[Toggle%] %v\n"
3519 (defun widget-boolean-prompt-value (widget prompt value unbound)
3520 ;; Toggle a boolean.
3523 ;;; The `color' Widget.
3526 (define-widget 'color 'editable-field
3527 "Choose a color name (with sample)."
3528 :format "%t: %v (%{sample%})\n"
3532 :complete 'widget-color-complete
3533 :sample-face-get 'widget-color-sample-face-get
3534 :notify 'widget-color-notify
3535 :action 'widget-color-action)
3537 (defun widget-color-complete (widget)
3538 "Complete the color in WIDGET."
3539 (require 'facemenu) ; for facemenu-color-alist
3540 (let* ((prefix (buffer-substring-no-properties (widget-field-start widget)
3542 (list (or facemenu-color-alist (defined-colors)))
3543 (completion (try-completion prefix list)))
3544 (cond ((eq completion t)
3545 (message "Exact match."))
3547 (error "Can't find completion for \"%s\"" prefix))
3548 ((not (string-equal prefix completion))
3549 (insert-and-inherit (substring completion (length prefix))))
3551 (message "Making completion list...")
3552 (with-output-to-temp-buffer "*Completions*"
3553 (display-completion-list (all-completions prefix list nil)))
3554 (message "Making completion list...done")))))
3556 (defun widget-color-sample-face-get (widget)
3557 (let* ((value (condition-case nil
3558 (widget-value widget)
3559 (error (widget-get widget :value)))))
3560 (if (color-defined-p value)
3561 (list (cons 'foreground-color value))
3564 (defun widget-color-action (widget &optional event)
3565 "Prompt for a color."
3566 (let* ((tag (widget-apply widget :menu-tag-get))
3567 (prompt (concat tag ": "))
3568 (value (widget-value widget))
3569 (start (widget-field-start widget))
3570 (answer (facemenu-read-color prompt)))
3571 (unless (zerop (length answer))
3572 (widget-value-set widget answer)
3574 (widget-apply widget :notify widget event))))
3576 (defun widget-color-notify (widget child &optional event)
3577 "Update the sample, and notofy the parent."
3578 (overlay-put (widget-get widget :sample-overlay)
3579 'face (widget-apply widget :sample-face-get))
3580 (widget-default-notify widget child event))
3584 (defun widget-echo-help (pos)
3585 "Display help-echo text for widget at POS."
3586 (let* ((widget (widget-at pos))
3587 (help-echo (and widget (widget-get widget :help-echo))))
3588 (if (functionp help-echo)
3589 (setq help-echo (funcall help-echo widget)))
3590 (if help-echo (message "%s" (eval help-echo)))))
3596 ;;; arch-tag: a076e75e-18a1-4b46-8be5-3f317bcbc707
3597 ;;; wid-edit.el ends here