(widget-color-sample-face-get): Don't make
[emacs/old-mirror.git] / lisp / wid-edit.el
bloba3821dcfe0a9a4e19f75017b5a2b75303232c15f
1 ;;; wid-edit.el --- Functions for creating and using widgets -*-byte-compile-dynamic: t;-*-
2 ;;
3 ;; Copyright (C) 1996, 1997, 1999, 2000, 2001 Free Software Foundation, Inc.
4 ;;
5 ;; Author: Per Abrahamsen <abraham@dina.kvl.dk>
6 ;; Maintainer: FSF
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)
14 ;; any later version.
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
36 ;; the browser.
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.]
54 ;;; Commentary:
56 ;; See `widget.el'.
58 ;;; Code:
60 ;;; Compatibility.
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 (autoload 'pp-to-string "pp")
67 (autoload 'Info-goto-node "info")
69 (defun widget-button-release-event-p (event)
70 "Non-nil if EVENT is a mouse-button-release event object."
71 (and (eventp event)
72 (memq (event-basic-type event) '(mouse-1 mouse-2 mouse-3))
73 (or (memq 'click (event-modifiers event))
74 (memq 'drag (event-modifiers event)))))
76 ;;; Customization.
78 (defgroup widgets nil
79 "Customization support for the Widget Library."
80 :link '(custom-manual "(widget)Top")
81 :link '(emacs-library-link :tag "Lisp File" "widget.el")
82 :prefix "widget-"
83 :group 'extensions
84 :group 'hypermedia)
86 (defgroup widget-documentation nil
87 "Options controling the display of documentation strings."
88 :group 'widgets)
90 (defgroup widget-faces nil
91 "Faces used by the widget library."
92 :group 'widgets
93 :group 'faces)
95 (defvar widget-documentation-face 'widget-documentation-face
96 "Face used for documentation strings in widgets.
97 This exists as a variable so it can be set locally in certain buffers.")
99 (defface widget-documentation-face '((((class color)
100 (background dark))
101 (:foreground "lime green"))
102 (((class color)
103 (background light))
104 (:foreground "dark green"))
105 (t nil))
106 "Face used for documentation text."
107 :group 'widget-documentation
108 :group 'widget-faces)
110 (defvar widget-button-face 'widget-button-face
111 "Face used for buttons in widgets.
112 This exists as a variable so it can be set locally in certain buffers.")
114 (defface widget-button-face '((t (:bold t)))
115 "Face used for widget buttons."
116 :group 'widget-faces)
118 (defcustom widget-mouse-face 'highlight
119 "Face used for widget buttons when the mouse is above them."
120 :type 'face
121 :group 'widget-faces)
123 (defface widget-field-face '((((class grayscale color)
124 (background light))
125 (:background "gray85"))
126 (((class grayscale color)
127 (background dark))
128 (:background "dim gray"))
130 (:italic t)))
131 "Face used for editable fields."
132 :group 'widget-faces)
134 (defface widget-single-line-field-face '((((class grayscale color)
135 (background light))
136 (:background "gray85"))
137 (((class grayscale color)
138 (background dark))
139 (:background "dim gray"))
141 (:italic t)))
142 "Face used for editable fields spanning only a single line."
143 :group 'widget-faces)
145 ;;; This causes display-table to be loaded, and not usefully.
146 ;;;(defvar widget-single-line-display-table
147 ;;; (let ((table (make-display-table)))
148 ;;; (aset table 9 "^I")
149 ;;; (aset table 10 "^J")
150 ;;; table)
151 ;;; "Display table used for single-line editable fields.")
153 ;;;(when (fboundp 'set-face-display-table)
154 ;;; (set-face-display-table 'widget-single-line-field-face
155 ;;; widget-single-line-display-table))
157 ;;; Utility functions.
159 ;; These are not really widget specific.
161 (defun widget-princ-to-string (object)
162 "Return string representation of OBJECT, any Lisp object.
163 No quoting characters are used; no delimiters are printed around
164 the contents of strings."
165 (with-output-to-string
166 (princ object)))
168 (defun widget-clear-undo ()
169 "Clear all undo information."
170 (buffer-disable-undo (current-buffer))
171 (buffer-enable-undo))
173 (defcustom widget-menu-max-size 40
174 "Largest number of items allowed in a popup-menu.
175 Larger menus are read through the minibuffer."
176 :group 'widgets
177 :type 'integer)
179 (defcustom widget-menu-max-shortcuts 40
180 "Largest number of items for which it works to choose one with a character.
181 For a larger number of items, the minibuffer is used."
182 :group 'widgets
183 :type 'integer)
185 (defcustom widget-menu-minibuffer-flag nil
186 "*Control how to ask for a choice from the keyboard.
187 Non-nil means use the minibuffer;
188 nil means read a single character."
189 :group 'widgets
190 :type 'boolean)
192 (defun widget-choose (title items &optional event)
193 "Choose an item from a list.
195 First argument TITLE is the name of the list.
196 Second argument ITEMS is an list whose members are either
197 (NAME . VALUE), to indicate selectable items, or just strings to
198 indicate unselectable items.
199 Optional third argument EVENT is an input event.
201 The user is asked to choose between each NAME from the items alist,
202 and the VALUE of the chosen element will be returned. If EVENT is a
203 mouse event, and the number of elements in items is less than
204 `widget-menu-max-size', a popup menu will be used, otherwise the
205 minibuffer."
206 (cond ((and (< (length items) widget-menu-max-size)
207 event (display-popup-menus-p))
208 ;; Mouse click.
209 (x-popup-menu event
210 (list title (cons "" items))))
211 ((or widget-menu-minibuffer-flag
212 (> (length items) widget-menu-max-shortcuts))
213 ;; Read the choice of name from the minibuffer.
214 (setq items (widget-remove-if 'stringp items))
215 (let ((val (completing-read (concat title ": ") items nil t)))
216 (if (stringp val)
217 (let ((try (try-completion val items)))
218 (when (stringp try)
219 (setq val try))
220 (cdr (assoc val items))))))
222 ;; Construct a menu of the choices
223 ;; and then use it for prompting for a single character.
224 (let* ((overriding-terminal-local-map (make-sparse-keymap))
225 (next-digit ?0)
226 map choice some-choice-enabled value)
227 ;; Define SPC as a prefix char to get to this menu.
228 (define-key overriding-terminal-local-map " "
229 (setq map (make-sparse-keymap title)))
230 (save-excursion
231 (set-buffer (get-buffer-create " widget-choose"))
232 (erase-buffer)
233 (insert "Available choices:\n\n")
234 (while items
235 (setq choice (car items) items (cdr items))
236 (if (consp choice)
237 (let* ((name (car choice))
238 (function (cdr choice)))
239 (insert (format "%c = %s\n" next-digit name))
240 (define-key map (vector next-digit) function)
241 (setq some-choice-enabled t)))
242 ;; Allocate digits to disabled alternatives
243 ;; so that the digit of a given alternative never varies.
244 (setq next-digit (1+ next-digit)))
245 (insert "\nC-g = Quit"))
246 (or some-choice-enabled
247 (error "None of the choices is currently meaningful"))
248 (define-key map [?\C-g] 'keyboard-quit)
249 (define-key map [t] 'keyboard-quit)
250 (define-key map [?\M-\C-v] 'scroll-other-window)
251 (define-key map [?\M--] 'negative-argument)
252 (setcdr map (nreverse (cdr map)))
253 ;; Read a char with the menu, and return the result
254 ;; that corresponds to it.
255 (save-window-excursion
256 (let ((buf (get-buffer " widget-choose")))
257 (fit-window-to-buffer (display-buffer buf))
258 (let ((cursor-in-echo-area t)
259 keys
260 (char 0)
261 (arg 1))
262 (while (not (or (and (>= char ?0) (< char next-digit))
263 (eq value 'keyboard-quit)))
264 ;; Unread a SPC to lead to our new menu.
265 (setq unread-command-events (cons ?\ unread-command-events))
266 (setq keys (read-key-sequence title))
267 (setq value
268 (lookup-key overriding-terminal-local-map keys t)
269 char (string-to-char (substring keys 1)))
270 (cond ((eq value 'scroll-other-window)
271 (let ((minibuffer-scroll-window
272 (get-buffer-window buf)))
273 (if (> 0 arg)
274 (scroll-other-window-down
275 (window-height minibuffer-scroll-window))
276 (scroll-other-window))
277 (setq arg 1)))
278 ((eq value 'negative-argument)
279 (setq arg -1))
281 (setq arg 1)))))))
282 (when (eq value 'keyboard-quit)
283 (error "Canceled"))
284 value))))
286 (defun widget-remove-if (predictate list)
287 (let (result (tail list))
288 (while tail
289 (or (funcall predictate (car tail))
290 (setq result (cons (car tail) result)))
291 (setq tail (cdr tail)))
292 (nreverse result)))
294 ;;; Widget text specifications.
296 ;; These functions are for specifying text properties.
298 (defvar widget-field-add-space t
299 "Non-nil means add extra space at the end of editable text fields.
300 If you don't add the space, it will become impossible to edit a zero
301 size field.")
303 (defvar widget-field-use-before-change t
304 "Non-nil means use `before-change-functions' to track editable fields.
305 This enables the use of undo, but doesn't work on Emacs 19.34 and earlier.
306 Using before hooks also means that the :notify function can't know the
307 new value.")
309 (defun widget-specify-field (widget from to)
310 "Specify editable button for WIDGET between FROM and TO."
311 ;; Terminating space is not part of the field, but necessary in
312 ;; order for local-map to work. Remove next sexp if local-map works
313 ;; at the end of the overlay.
314 (save-excursion
315 (goto-char to)
316 (cond ((null (widget-get widget :size))
317 (forward-char 1))
318 (widget-field-add-space
319 (insert-and-inherit " ")))
320 (setq to (point)))
321 (let ((keymap (widget-get widget :keymap))
322 (face (or (widget-get widget :value-face) 'widget-field-face))
323 (help-echo (widget-get widget :help-echo))
324 (rear-sticky
325 (or (not widget-field-add-space) (widget-get widget :size))))
326 (if (functionp help-echo)
327 (setq help-echo 'widget-mouse-help))
328 (when (= (char-before to) ?\n)
329 ;; When the last character in the field is a newline, we want to
330 ;; give it a `field' char-property of `boundary', which helps the
331 ;; C-n/C-p act more naturally when entering/leaving the field. We
332 ;; do this by making a small secondary overlay to contain just that
333 ;; one character.
334 (let ((overlay (make-overlay (1- to) to nil t nil)))
335 (overlay-put overlay 'field 'boundary)
336 ;; Use `local-map' here, not `keymap', so that normal editing
337 ;; works in the field when, say, Custom uses `suppress-keymap'.
338 (overlay-put overlay 'local-map keymap)
339 (overlay-put overlay 'face face)
340 (overlay-put overlay 'help-echo help-echo))
341 (setq to (1- to))
342 (setq rear-sticky t))
343 (let ((overlay (make-overlay from to nil nil rear-sticky)))
344 (widget-put widget :field-overlay overlay)
345 ;;(overlay-put overlay 'detachable nil)
346 (overlay-put overlay 'field widget)
347 (overlay-put overlay 'local-map keymap)
348 (overlay-put overlay 'face face)
349 (overlay-put overlay 'help-echo help-echo)))
350 (widget-specify-secret widget))
352 (defun widget-specify-secret (field)
353 "Replace text in FIELD with value of `:secret', if non-nil."
354 (let ((secret (widget-get field :secret))
355 (size (widget-get field :size)))
356 (when secret
357 (let ((begin (widget-field-start field))
358 (end (widget-field-end field)))
359 (when size
360 (while (and (> end begin)
361 (eq (char-after (1- end)) ?\ ))
362 (setq end (1- end))))
363 (while (< begin end)
364 (let ((old (char-after begin)))
365 (unless (eq old secret)
366 (subst-char-in-region begin (1+ begin) old secret)
367 (put-text-property begin (1+ begin) 'secret old))
368 (setq begin (1+ begin))))))))
370 (defun widget-specify-button (widget from to)
371 "Specify button for WIDGET between FROM and TO."
372 (let ((overlay (make-overlay from to nil t nil))
373 (help-echo (widget-get widget :help-echo)))
374 (widget-put widget :button-overlay overlay)
375 (if (functionp help-echo)
376 (setq help-echo 'widget-mouse-help))
377 (overlay-put overlay 'button widget)
378 (overlay-put overlay 'keymap (widget-get widget :keymap))
379 ;; We want to avoid the face with image buttons.
380 (unless (widget-get widget :suppress-face)
381 (overlay-put overlay 'face (widget-apply widget :button-face-get))
382 (overlay-put overlay 'mouse-face widget-mouse-face))
383 (overlay-put overlay 'help-echo help-echo)))
385 (defun widget-mouse-help (window overlay point)
386 "Help-echo callback for widgets whose :help-echo is a function."
387 (with-current-buffer (overlay-buffer overlay)
388 (let* ((widget (widget-at (overlay-start overlay)))
389 (help-echo (if widget (widget-get widget :help-echo))))
390 (if (functionp help-echo)
391 (funcall help-echo widget)
392 help-echo))))
394 (defun widget-specify-sample (widget from to)
395 "Specify sample for WIDGET between FROM and TO."
396 (let ((overlay (make-overlay from to nil t nil)))
397 (overlay-put overlay 'face (widget-apply widget :sample-face-get))
398 (widget-put widget :sample-overlay overlay)))
400 (defun widget-specify-doc (widget from to)
401 "Specify documentation for WIDGET between FROM and TO."
402 (let ((overlay (make-overlay from to nil t nil)))
403 (overlay-put overlay 'widget-doc widget)
404 (overlay-put overlay 'face widget-documentation-face)
405 (widget-put widget :doc-overlay overlay)))
407 (defmacro widget-specify-insert (&rest form)
408 "Execute FORM without inheriting any text properties."
409 `(save-restriction
410 (let ((inhibit-read-only t)
411 (inhibit-modification-hooks t)
412 result)
413 (insert "<>")
414 (narrow-to-region (- (point) 2) (point))
415 (goto-char (1+ (point-min)))
416 (setq result (progn ,@form))
417 (delete-region (point-min) (1+ (point-min)))
418 (delete-region (1- (point-max)) (point-max))
419 (goto-char (point-max))
420 result)))
422 (defface widget-inactive-face '((((class grayscale color)
423 (background dark))
424 (:foreground "light gray"))
425 (((class grayscale color)
426 (background light))
427 (:foreground "dim gray"))
429 (:italic t)))
430 "Face used for inactive widgets."
431 :group 'widget-faces)
433 (defun widget-specify-inactive (widget from to)
434 "Make WIDGET inactive for user modifications."
435 (unless (widget-get widget :inactive)
436 (let ((overlay (make-overlay from to nil t nil)))
437 (overlay-put overlay 'face 'widget-inactive-face)
438 ;; This is disabled, as it makes the mouse cursor change shape.
439 ;; (overlay-put overlay 'mouse-face 'widget-inactive-face)
440 (overlay-put overlay 'evaporate t)
441 (overlay-put overlay 'priority 100)
442 (overlay-put overlay 'modification-hooks '(widget-overlay-inactive))
443 (widget-put widget :inactive overlay))))
445 (defun widget-overlay-inactive (&rest junk)
446 "Ignoring the arguments, signal an error."
447 (unless inhibit-read-only
448 (error "The widget here is not active")))
451 (defun widget-specify-active (widget)
452 "Make WIDGET active for user modifications."
453 (let ((inactive (widget-get widget :inactive)))
454 (when inactive
455 (delete-overlay inactive)
456 (widget-put widget :inactive nil))))
458 ;;; Widget Properties.
460 (defsubst widget-type (widget)
461 "Return the type of WIDGET, a symbol."
462 (car widget))
464 (defun widget-get-indirect (widget property)
465 "In WIDGET, get the value of PROPERTY.
466 If the value is a symbol, return its binding.
467 Otherwise, just return the value."
468 (let ((value (widget-get widget property)))
469 (if (symbolp value)
470 (symbol-value value)
471 value)))
473 (defun widget-member (widget property)
474 "Non-nil iff there is a definition in WIDGET for PROPERTY."
475 (cond ((plist-member (cdr widget) property)
477 ((car widget)
478 (widget-member (get (car widget) 'widget-type) property))
479 (t nil)))
481 (defun widget-value (widget)
482 "Extract the current value of WIDGET."
483 (widget-apply widget
484 :value-to-external (widget-apply widget :value-get)))
486 (defun widget-value-set (widget value)
487 "Set the current value of WIDGET to VALUE."
488 (widget-apply widget
489 :value-set (widget-apply widget
490 :value-to-internal value)))
492 (defun widget-default-get (widget)
493 "Extract the default value of WIDGET."
494 (or (widget-get widget :value)
495 (widget-apply widget :default-get)))
497 (defun widget-match-inline (widget vals)
498 "In WIDGET, match the start of VALS."
499 (cond ((widget-get widget :inline)
500 (widget-apply widget :match-inline vals))
501 ((and (listp vals)
502 (widget-apply widget :match (car vals)))
503 (cons (list (car vals)) (cdr vals)))
504 (t nil)))
506 (defun widget-apply-action (widget &optional event)
507 "Apply :action in WIDGET in response to EVENT."
508 (if (widget-apply widget :active)
509 (widget-apply widget :action event)
510 (error "Attempt to perform action on inactive widget")))
512 ;;; Helper functions.
514 ;; These are widget specific.
516 ;;;###autoload
517 (defun widget-prompt-value (widget prompt &optional value unbound)
518 "Prompt for a value matching WIDGET, using PROMPT.
519 The current value is assumed to be VALUE, unless UNBOUND is non-nil."
520 (unless (listp widget)
521 (setq widget (list widget)))
522 (setq prompt (format "[%s] %s" (widget-type widget) prompt))
523 (setq widget (widget-convert widget))
524 (let ((answer (widget-apply widget :prompt-value prompt value unbound)))
525 (unless (widget-apply widget :match answer)
526 (error "Value does not match %S type" (car widget)))
527 answer))
529 (defun widget-get-sibling (widget)
530 "Get the item WIDGET is assumed to toggle.
531 This is only meaningful for radio buttons or checkboxes in a list."
532 (let* ((children (widget-get (widget-get widget :parent) :children))
533 child)
534 (catch 'child
535 (while children
536 (setq child (car children)
537 children (cdr children))
538 (when (eq (widget-get child :button) widget)
539 (throw 'child child)))
540 nil)))
542 (defun widget-map-buttons (function &optional buffer maparg)
543 "Map FUNCTION over the buttons in BUFFER.
544 FUNCTION is called with the arguments WIDGET and MAPARG.
546 If FUNCTION returns non-nil, the walk is cancelled.
548 The arguments MAPARG, and BUFFER default to nil and (current-buffer),
549 respectively."
550 (let ((cur (point-min))
551 (widget nil)
552 (parent nil)
553 (overlays (if buffer
554 (save-excursion (set-buffer buffer) (overlay-lists))
555 (overlay-lists))))
556 (setq overlays (append (car overlays) (cdr overlays)))
557 (while (setq cur (pop overlays))
558 (setq widget (overlay-get cur 'button))
559 (if (and widget (funcall function widget maparg))
560 (setq overlays nil)))))
562 ;;; Images.
564 (defcustom widget-image-directory (file-name-as-directory
565 (expand-file-name "custom" data-directory))
566 "Where widget button images are located.
567 If this variable is nil, widget will try to locate the directory
568 automatically."
569 :group 'widgets
570 :type 'directory)
572 (defcustom widget-image-enable t
573 "If non nil, use image buttons in widgets when available."
574 :version "21.1"
575 :group 'widgets
576 :type 'boolean)
578 (defcustom widget-image-conversion
579 '((xpm ".xpm") (gif ".gif") (png ".png") (jpeg ".jpg" ".jpeg")
580 (xbm ".xbm"))
581 "Conversion alist from image formats to file name suffixes."
582 :group 'widgets
583 :type '(repeat (cons :format "%v"
584 (symbol :tag "Image Format" unknown)
585 (repeat :tag "Suffixes"
586 (string :format "%v")))))
588 (defun widget-image-find (image)
589 "Create a graphical button from IMAGE.
590 IMAGE should either already be an image, or be a file name sans
591 extension (xpm, xbm, gif, jpg, or png) located in
592 `widget-image-directory' or otherwise where `find-image' will find it."
593 (cond ((not (and image widget-image-enable (display-graphic-p)))
594 ;; We don't want or can't use images.
595 nil)
596 ((and (consp image)
597 (eq 'image (car image)))
598 ;; Already an image spec. Use it.
599 image)
600 ((stringp image)
601 ;; A string. Look it up in relevant directories.
602 (let* ((load-path (cons widget-image-directory load-path))
603 specs)
604 (dolist (elt widget-image-conversion)
605 (dolist (ext (cdr elt))
606 (push (list :type (car elt) :file (concat image ext)) specs)))
607 (setq specs (nreverse specs))
608 (find-image specs)))
610 ;; Oh well.
611 nil)))
613 (defvar widget-button-pressed-face 'widget-button-pressed-face
614 "Face used for pressed buttons in widgets.
615 This exists as a variable so it can be set locally in certain
616 buffers.")
618 (defun widget-image-insert (widget tag image &optional down inactive)
619 "In WIDGET, insert the text TAG or, if supported, IMAGE.
620 IMAGE should either be an image or an image file name sans extension
621 \(xpm, xbm, gif, jpg, or png) located in `widget-image-directory'.
623 Optional arguments DOWN and INACTIVE are used instead of IMAGE when the
624 button is pressed or inactive, respectively. These are currently ignored."
625 (if (and (display-graphic-p)
626 (setq image (widget-image-find image)))
627 (progn (widget-put widget :suppress-face t)
628 (insert-image image
629 (propertize
630 tag 'mouse-face widget-button-pressed-face)))
631 (insert tag)))
633 ;;; Buttons.
635 (defgroup widget-button nil
636 "The look of various kinds of buttons."
637 :group 'widgets)
639 (defcustom widget-button-prefix ""
640 "String used as prefix for buttons."
641 :type 'string
642 :group 'widget-button)
644 (defcustom widget-button-suffix ""
645 "String used as suffix for buttons."
646 :type 'string
647 :group 'widget-button)
649 ;;; Creating Widgets.
651 ;;;###autoload
652 (defun widget-create (type &rest args)
653 "Create widget of TYPE.
654 The optional ARGS are additional keyword arguments."
655 (let ((widget (apply 'widget-convert type args)))
656 (widget-apply widget :create)
657 widget))
659 (defun widget-create-child-and-convert (parent type &rest args)
660 "As part of the widget PARENT, create a child widget TYPE.
661 The child is converted, using the keyword arguments ARGS."
662 (let ((widget (apply 'widget-convert type args)))
663 (widget-put widget :parent parent)
664 (unless (widget-get widget :indent)
665 (widget-put widget :indent (+ (or (widget-get parent :indent) 0)
666 (or (widget-get widget :extra-offset) 0)
667 (widget-get parent :offset))))
668 (widget-apply widget :create)
669 widget))
671 (defun widget-create-child (parent type)
672 "Create widget of TYPE."
673 (let ((widget (copy-sequence type)))
674 (widget-put widget :parent parent)
675 (unless (widget-get widget :indent)
676 (widget-put widget :indent (+ (or (widget-get parent :indent) 0)
677 (or (widget-get widget :extra-offset) 0)
678 (widget-get parent :offset))))
679 (widget-apply widget :create)
680 widget))
682 (defun widget-create-child-value (parent type value)
683 "Create widget of TYPE with value VALUE."
684 (let ((widget (copy-sequence type)))
685 (widget-put widget :value (widget-apply widget :value-to-internal value))
686 (widget-put widget :parent parent)
687 (unless (widget-get widget :indent)
688 (widget-put widget :indent (+ (or (widget-get parent :indent) 0)
689 (or (widget-get widget :extra-offset) 0)
690 (widget-get parent :offset))))
691 (widget-apply widget :create)
692 widget))
694 ;;;###autoload
695 (defun widget-delete (widget)
696 "Delete WIDGET."
697 (widget-apply widget :delete))
699 (defun widget-convert (type &rest args)
700 "Convert TYPE to a widget without inserting it in the buffer.
701 The optional ARGS are additional keyword arguments."
702 ;; Don't touch the type.
703 (let* ((widget (if (symbolp type)
704 (list type)
705 (copy-sequence type)))
706 (current widget)
707 (keys args))
708 ;; First set the :args keyword.
709 (while (cdr current) ;Look in the type.
710 (if (keywordp (car (cdr current)))
711 (setq current (cdr (cdr current)))
712 (setcdr current (list :args (cdr current)))
713 (setq current nil)))
714 (while args ;Look in the args.
715 (if (keywordp (nth 0 args))
716 (setq args (nthcdr 2 args))
717 (widget-put widget :args args)
718 (setq args nil)))
719 ;; Then Convert the widget.
720 (setq type widget)
721 (while type
722 (let ((convert-widget (plist-get (cdr type) :convert-widget)))
723 (if convert-widget
724 (setq widget (funcall convert-widget widget))))
725 (setq type (get (car type) 'widget-type)))
726 ;; Finally set the keyword args.
727 (while keys
728 (let ((next (nth 0 keys)))
729 (if (keywordp next)
730 (progn
731 (widget-put widget next (nth 1 keys))
732 (setq keys (nthcdr 2 keys)))
733 (setq keys nil))))
734 ;; Convert the :value to internal format.
735 (if (widget-member widget :value)
736 (widget-put widget
737 :value (widget-apply widget
738 :value-to-internal
739 (widget-get widget :value))))
740 ;; Return the newly create widget.
741 widget))
743 (defun widget-insert (&rest args)
744 "Call `insert' with ARGS even if surrounding text is read only."
745 (let ((inhibit-read-only t)
746 (inhibit-modification-hooks t))
747 (apply 'insert args)))
749 (defun widget-convert-text (type from to
750 &optional button-from button-to
751 &rest args)
752 "Return a widget of type TYPE with endpoint FROM TO.
753 Optional ARGS are extra keyword arguments for TYPE.
754 and TO will be used as the widgets end points. If optional arguments
755 BUTTON-FROM and BUTTON-TO are given, these will be used as the widgets
756 button end points.
757 Optional ARGS are extra keyword arguments for TYPE."
758 (let ((widget (apply 'widget-convert type :delete 'widget-leave-text args))
759 (from (copy-marker from))
760 (to (copy-marker to)))
761 (set-marker-insertion-type from t)
762 (set-marker-insertion-type to nil)
763 (widget-put widget :from from)
764 (widget-put widget :to to)
765 (when button-from
766 (widget-specify-button widget button-from button-to))
767 widget))
769 (defun widget-convert-button (type from to &rest args)
770 "Return a widget of type TYPE with endpoint FROM TO.
771 Optional ARGS are extra keyword arguments for TYPE.
772 No text will be inserted to the buffer, instead the text between FROM
773 and TO will be used as the widgets end points, as well as the widgets
774 button end points."
775 (apply 'widget-convert-text type from to from to args))
777 (defun widget-leave-text (widget)
778 "Remove markers and overlays from WIDGET and its children."
779 (let ((button (widget-get widget :button-overlay))
780 (sample (widget-get widget :sample-overlay))
781 (doc (widget-get widget :doc-overlay))
782 (field (widget-get widget :field-overlay)))
783 (set-marker (widget-get widget :from) nil)
784 (set-marker (widget-get widget :to) nil)
785 (when button
786 (delete-overlay button))
787 (when sample
788 (delete-overlay sample))
789 (when doc
790 (delete-overlay doc))
791 (when field
792 (delete-overlay field))
793 (mapc 'widget-leave-text (widget-get widget :children))))
795 ;;; Keymap and Commands.
797 (defvar widget-keymap
798 (let ((map (make-sparse-keymap)))
799 (define-key map "\t" 'widget-forward)
800 (define-key map [(shift tab)] 'widget-backward)
801 (define-key map [backtab] 'widget-backward)
802 (define-key map [down-mouse-2] 'widget-button-click)
803 (define-key map "\C-m" 'widget-button-press)
804 map)
805 "Keymap containing useful binding for buffers containing widgets.
806 Recommended as a parent keymap for modes using widgets.")
808 (defvar widget-global-map global-map
809 "Keymap used for events a widget does not handle itself.")
810 (make-variable-buffer-local 'widget-global-map)
812 (defvar widget-field-keymap
813 (let ((map (copy-keymap widget-keymap)))
814 (define-key map "\C-k" 'widget-kill-line)
815 (define-key map "\M-\t" 'widget-complete)
816 (define-key map "\C-m" 'widget-field-activate)
817 ;; Since the widget code uses a `field' property to identify fields,
818 ;; ordinary beginning-of-line does the right thing.
819 ;; (define-key map "\C-a" 'widget-beginning-of-line)
820 (define-key map "\C-e" 'widget-end-of-line)
821 map)
822 "Keymap used inside an editable field.")
824 (defvar widget-text-keymap
825 (let ((map (copy-keymap widget-keymap)))
826 ;; Since the widget code uses a `field' property to identify fields,
827 ;; ordinary beginning-of-line does the right thing.
828 ;; (define-key map "\C-a" 'widget-beginning-of-line)
829 (define-key map "\C-e" 'widget-end-of-line)
830 map)
831 "Keymap used inside a text field.")
833 (defun widget-field-activate (pos &optional event)
834 "Invoke the editable field at point."
835 (interactive "@d")
836 (let ((field (widget-field-at pos)))
837 (if field
838 (widget-apply-action field event)
839 (call-interactively
840 (lookup-key widget-global-map (this-command-keys))))))
842 (defface widget-button-pressed-face
843 '((((class color))
844 (:foreground "red"))
846 (:bold t :underline t)))
847 "Face used for pressed buttons."
848 :group 'widget-faces)
850 (defun widget-button-click (event)
851 "Invoke the button that the mouse is pointing at."
852 (interactive "@e")
853 (if (widget-event-point event)
854 (let* ((pos (widget-event-point event))
855 (button (get-char-property pos 'button)))
856 (if button
857 ;; Mouse click on a widget button. Do the following
858 ;; in a save-excursion so that the click on the button
859 ;; doesn't change point.
860 (save-selected-window
861 (save-excursion
862 (mouse-set-point event)
863 (let* ((overlay (widget-get button :button-overlay))
864 (face (overlay-get overlay 'face))
865 (mouse-face (overlay-get overlay 'mouse-face)))
866 (unwind-protect
867 ;; Read events, including mouse-movement events
868 ;; until we receive a release event. Highlight/
869 ;; unhighlight the button the mouse was initially
870 ;; on when we move over it.
871 (let ((track-mouse t))
872 (save-excursion
873 (when face ; avoid changing around image
874 (overlay-put overlay
875 'face widget-button-pressed-face)
876 (overlay-put overlay
877 'mouse-face widget-button-pressed-face))
878 (unless (widget-apply button :mouse-down-action event)
879 (while (not (widget-button-release-event-p event))
880 (setq event (read-event)
881 pos (widget-event-point event))
882 (if (and pos
883 (eq (get-char-property pos 'button)
884 button))
885 (when face
886 (overlay-put overlay
887 'face
888 widget-button-pressed-face)
889 (overlay-put overlay
890 'mouse-face
891 widget-button-pressed-face))
892 (overlay-put overlay 'face face)
893 (overlay-put overlay 'mouse-face mouse-face))))
895 ;; When mouse is released over the button, run
896 ;; its action function.
897 (when (and pos
898 (eq (get-char-property pos 'button) button))
899 (widget-apply-action button event))))
900 (overlay-put overlay 'face face)
901 (overlay-put overlay 'mouse-face mouse-face))))
903 (unless (pos-visible-in-window-p (widget-event-point event))
904 (mouse-set-point event)
905 (beginning-of-line)
906 (recenter)))
908 (let ((up t) command)
909 ;; Mouse click not on a widget button. Find the global
910 ;; command to run, and check whether it is bound to an
911 ;; up event.
912 (mouse-set-point event)
913 (if (memq (event-basic-type event) '(mouse-1 down-mouse-1))
914 (cond ((setq command ;down event
915 (lookup-key widget-global-map [down-mouse-1]))
916 (setq up nil))
917 ((setq command ;up event
918 (lookup-key widget-global-map [mouse-1]))))
919 (cond ((setq command ;down event
920 (lookup-key widget-global-map [down-mouse-2]))
921 (setq up nil))
922 ((setq command ;up event
923 (lookup-key widget-global-map [mouse-2])))))
924 (when up
925 ;; Don't execute up events twice.
926 (while (not (widget-button-release-event-p event))
927 (setq event (read-event))))
928 (when command
929 (call-interactively command)))))
930 (message "You clicked somewhere weird.")))
932 (defun widget-button-press (pos &optional event)
933 "Invoke button at POS."
934 (interactive "@d")
935 (let ((button (get-char-property pos 'button)))
936 (if button
937 (widget-apply-action button event)
938 (let ((command (lookup-key widget-global-map (this-command-keys))))
939 (when (commandp command)
940 (call-interactively command))))))
942 (defun widget-tabable-at (&optional pos)
943 "Return the tabable widget at POS, or nil.
944 POS defaults to the value of (point)."
945 (let ((widget (widget-at pos)))
946 (if widget
947 (let ((order (widget-get widget :tab-order)))
948 (if order
949 (if (>= order 0)
950 widget)
951 widget)))))
953 (defvar widget-use-overlay-change t
954 "If non-nil, use overlay change functions to tab around in the buffer.
955 This is much faster, but doesn't work reliably on Emacs 19.34.")
957 (defun widget-move (arg)
958 "Move point to the ARG next field or button.
959 ARG may be negative to move backward."
960 (or (bobp) (> arg 0) (backward-char))
961 (let ((pos (point))
962 (number arg)
963 (old (widget-tabable-at))
964 new)
965 ;; Forward.
966 (while (> arg 0)
967 (cond ((eobp)
968 (goto-char (point-min)))
969 (widget-use-overlay-change
970 (goto-char (next-overlay-change (point))))
972 (forward-char 1)))
973 (and (eq pos (point))
974 (eq arg number)
975 (error "No buttons or fields found"))
976 (let ((new (widget-tabable-at)))
977 (when new
978 (unless (eq new old)
979 (setq arg (1- arg))
980 (setq old new)))))
981 ;; Backward.
982 (while (< arg 0)
983 (cond ((bobp)
984 (goto-char (point-max)))
985 (widget-use-overlay-change
986 (goto-char (previous-overlay-change (point))))
988 (backward-char 1)))
989 (and (eq pos (point))
990 (eq arg number)
991 (error "No buttons or fields found"))
992 (let ((new (widget-tabable-at)))
993 (when new
994 (unless (eq new old)
995 (setq arg (1+ arg))))))
996 (let ((new (widget-tabable-at)))
997 (while (eq (widget-tabable-at) new)
998 (backward-char)))
999 (forward-char))
1000 (widget-echo-help (point))
1001 (run-hooks 'widget-move-hook))
1003 (defun widget-forward (arg)
1004 "Move point to the next field or button.
1005 With optional ARG, move across that many fields."
1006 (interactive "p")
1007 (run-hooks 'widget-forward-hook)
1008 (widget-move arg))
1010 (defun widget-backward (arg)
1011 "Move point to the previous field or button.
1012 With optional ARG, move across that many fields."
1013 (interactive "p")
1014 (run-hooks 'widget-backward-hook)
1015 (widget-move (- arg)))
1017 ;; Since the widget code uses a `field' property to identify fields,
1018 ;; ordinary beginning-of-line does the right thing.
1019 (defalias 'widget-beginning-of-line 'beginning-of-line)
1021 (defun widget-end-of-line ()
1022 "Go to end of field or end of line, whichever is first.
1023 Trailing spaces at the end of padded fields are not considered part of
1024 the field."
1025 (interactive)
1026 ;; Ordinary end-of-line does the right thing, because we're inside
1027 ;; text with a `field' property.
1028 (end-of-line)
1029 (unless (eolp)
1030 ;; ... except that we want to ignore trailing spaces in fields that
1031 ;; aren't terminated by a newline, because they are used as padding,
1032 ;; and ignored when extracting the entered value of the field.
1033 (skip-chars-backward " " (field-beginning (1- (point))))))
1035 (defun widget-kill-line ()
1036 "Kill to end of field or end of line, whichever is first."
1037 (interactive)
1038 (let* ((field (widget-field-find (point)))
1039 (end (and field (widget-field-end field))))
1040 (if (and field (> (line-beginning-position 2) end))
1041 (kill-region (point) end)
1042 (call-interactively 'kill-line))))
1044 (defcustom widget-complete-field (lookup-key global-map "\M-\t")
1045 "Default function to call for completion inside fields."
1046 :options '(ispell-complete-word complete-tag lisp-complete-symbol)
1047 :type 'function
1048 :group 'widgets)
1050 (defun widget-complete ()
1051 "Complete content of editable field from point.
1052 When not inside a field, move to the previous button or field."
1053 (interactive)
1054 (let ((field (widget-field-find (point))))
1055 (if field
1056 (widget-apply field :complete)
1057 (error "Not in an editable field"))))
1059 ;;; Setting up the buffer.
1061 (defvar widget-field-new nil)
1062 ;; List of all newly created editable fields in the buffer.
1063 (make-variable-buffer-local 'widget-field-new)
1065 (defvar widget-field-list nil)
1066 ;; List of all editable fields in the buffer.
1067 (make-variable-buffer-local 'widget-field-list)
1069 (defun widget-at (&optional pos)
1070 "The button or field at POS (default, point)."
1071 (or (get-char-property (or pos (point)) 'button)
1072 (widget-field-at pos)))
1074 (defun widget-setup ()
1075 "Setup current buffer so editing string widgets works."
1076 (let ((inhibit-read-only t)
1077 (inhibit-modification-hooks t)
1078 field)
1079 (while widget-field-new
1080 (setq field (car widget-field-new)
1081 widget-field-new (cdr widget-field-new)
1082 widget-field-list (cons field widget-field-list))
1083 (let ((from (car (widget-get field :field-overlay)))
1084 (to (cdr (widget-get field :field-overlay))))
1085 (widget-specify-field field
1086 (marker-position from) (marker-position to))
1087 (set-marker from nil)
1088 (set-marker to nil))))
1089 (widget-clear-undo)
1090 (widget-add-change))
1092 (defvar widget-field-last nil)
1093 ;; Last field containing point.
1094 (make-variable-buffer-local 'widget-field-last)
1096 (defvar widget-field-was nil)
1097 ;; The widget data before the change.
1098 (make-variable-buffer-local 'widget-field-was)
1100 (defun widget-field-at (pos)
1101 "Return the widget field at POS, or nil if none."
1102 (let ((field (get-char-property (or pos (point)) 'field)))
1103 (if (eq field 'boundary)
1105 field)))
1107 (defun widget-field-buffer (widget)
1108 "Return the start of WIDGET's editing field."
1109 (let ((overlay (widget-get widget :field-overlay)))
1110 (cond ((overlayp overlay)
1111 (overlay-buffer overlay))
1112 ((consp overlay)
1113 (marker-buffer (car overlay))))))
1115 (defun widget-field-start (widget)
1116 "Return the start of WIDGET's editing field."
1117 (let ((overlay (widget-get widget :field-overlay)))
1118 (if (overlayp overlay)
1119 (overlay-start overlay)
1120 (car overlay))))
1122 (defun widget-field-end (widget)
1123 "Return the end of WIDGET's editing field."
1124 (let ((overlay (widget-get widget :field-overlay)))
1125 ;; Don't subtract one if local-map works at the end of the overlay,
1126 ;; or if a special `boundary' field has been added after the widget
1127 ;; field.
1128 (if (overlayp overlay)
1129 (if (and (not (eq (get-char-property (overlay-end overlay)
1130 'field
1131 (widget-field-buffer widget))
1132 'boundary))
1133 (or widget-field-add-space
1134 (null (widget-get widget :size))))
1135 (1- (overlay-end overlay))
1136 (overlay-end overlay))
1137 (cdr overlay))))
1139 (defun widget-field-find (pos)
1140 "Return the field at POS.
1141 Unlike (get-char-property POS 'field) this, works with empty fields too."
1142 (let ((fields widget-field-list)
1143 field found)
1144 (while fields
1145 (setq field (car fields)
1146 fields (cdr fields))
1147 (when (and (<= (widget-field-start field) pos)
1148 (<= pos (widget-field-end field)))
1149 (when found
1150 (error "Overlapping fields"))
1151 (setq found field)))
1152 found))
1154 (defun widget-before-change (from to)
1155 ;; This is how, for example, a variable changes its state to `modified'.
1156 ;; when it is being edited.
1157 (unless inhibit-read-only
1158 (let ((from-field (widget-field-find from))
1159 (to-field (widget-field-find to)))
1160 (cond ((not (eq from-field to-field))
1161 (add-hook 'post-command-hook 'widget-add-change nil t)
1162 (signal 'text-read-only
1163 '("Change should be restricted to a single field")))
1164 ((null from-field)
1165 (add-hook 'post-command-hook 'widget-add-change nil t)
1166 (signal 'text-read-only
1167 '("Attempt to change text outside editable field")))
1168 (widget-field-use-before-change
1169 (widget-apply from-field :notify from-field))))))
1171 (defun widget-add-change ()
1172 (remove-hook 'post-command-hook 'widget-add-change t)
1173 (add-hook 'before-change-functions 'widget-before-change nil t)
1174 (add-hook 'after-change-functions 'widget-after-change nil t))
1176 (defun widget-after-change (from to old)
1177 "Adjust field size and text properties."
1178 (let ((field (widget-field-find from))
1179 (other (widget-field-find to)))
1180 (when field
1181 (unless (eq field other)
1182 (error "Change in different fields"))
1183 (let ((size (widget-get field :size)))
1184 (when size
1185 (let ((begin (widget-field-start field))
1186 (end (widget-field-end field)))
1187 (cond ((< (- end begin) size)
1188 ;; Field too small.
1189 (save-excursion
1190 (goto-char end)
1191 (insert-char ?\ (- (+ begin size) end))))
1192 ((> (- end begin) size)
1193 ;; Field too large and
1194 (if (or (< (point) (+ begin size))
1195 (> (point) end))
1196 ;; Point is outside extra space.
1197 (setq begin (+ begin size))
1198 ;; Point is within the extra space.
1199 (setq begin (point)))
1200 (save-excursion
1201 (goto-char end)
1202 (while (and (eq (preceding-char) ?\ )
1203 (> (point) begin))
1204 (delete-backward-char 1)))))))
1205 (widget-specify-secret field))
1206 (widget-apply field :notify field))))
1208 ;;; Widget Functions
1210 ;; These functions are used in the definition of multiple widgets.
1212 (defun widget-parent-action (widget &optional event)
1213 "Tell :parent of WIDGET to handle the :action.
1214 Optional EVENT is the event that triggered the action."
1215 (widget-apply (widget-get widget :parent) :action event))
1217 (defun widget-children-value-delete (widget)
1218 "Delete all :children and :buttons in WIDGET."
1219 (mapc 'widget-delete (widget-get widget :children))
1220 (widget-put widget :children nil)
1221 (mapc 'widget-delete (widget-get widget :buttons))
1222 (widget-put widget :buttons nil))
1224 (defun widget-children-validate (widget)
1225 "All the :children must be valid."
1226 (let ((children (widget-get widget :children))
1227 child found)
1228 (while (and children (not found))
1229 (setq child (car children)
1230 children (cdr children)
1231 found (widget-apply child :validate)))
1232 found))
1234 ;; Made defsubst to speed up face editor creation.
1235 (defsubst widget-types-convert-widget (widget)
1236 "Convert :args as widget types in WIDGET."
1237 (widget-put widget :args (mapcar 'widget-convert (widget-get widget :args)))
1238 widget)
1240 (defun widget-value-convert-widget (widget)
1241 "Initialize :value from :args in WIDGET."
1242 (let ((args (widget-get widget :args)))
1243 (when args
1244 (widget-put widget :value (car args))
1245 ;; Don't convert :value here, as this is done in `widget-convert'.
1246 ;; (widget-put widget :value (widget-apply widget
1247 ;; :value-to-internal (car args)))
1248 (widget-put widget :args nil)))
1249 widget)
1251 (defun widget-value-value-get (widget)
1252 "Return the :value property of WIDGET."
1253 (widget-get widget :value))
1255 ;;; The `default' Widget.
1257 (define-widget 'default nil
1258 "Basic widget other widgets are derived from."
1259 :value-to-internal (lambda (widget value) value)
1260 :value-to-external (lambda (widget value) value)
1261 :button-prefix 'widget-button-prefix
1262 :button-suffix 'widget-button-suffix
1263 :complete 'widget-default-complete
1264 :create 'widget-default-create
1265 :indent nil
1266 :offset 0
1267 :format-handler 'widget-default-format-handler
1268 :button-face-get 'widget-default-button-face-get
1269 :sample-face-get 'widget-default-sample-face-get
1270 :delete 'widget-default-delete
1271 :value-set 'widget-default-value-set
1272 :value-inline 'widget-default-value-inline
1273 :default-get 'widget-default-default-get
1274 :menu-tag-get 'widget-default-menu-tag-get
1275 :validate #'ignore
1276 :active 'widget-default-active
1277 :activate 'widget-specify-active
1278 :deactivate 'widget-default-deactivate
1279 :mouse-down-action #'ignore
1280 :action 'widget-default-action
1281 :notify 'widget-default-notify
1282 :prompt-value 'widget-default-prompt-value)
1284 (defun widget-default-complete (widget)
1285 "Call the value of the :complete-function property of WIDGET.
1286 If that does not exists, call the value of `widget-complete-field'."
1287 (call-interactively (or (widget-get widget :complete-function)
1288 widget-complete-field)))
1290 (defun widget-default-create (widget)
1291 "Create WIDGET at point in the current buffer."
1292 (widget-specify-insert
1293 (let ((from (point))
1294 button-begin button-end
1295 sample-begin sample-end
1296 doc-begin doc-end
1297 value-pos)
1298 (insert (widget-get widget :format))
1299 (goto-char from)
1300 ;; Parse escapes in format.
1301 (while (re-search-forward "%\\(.\\)" nil t)
1302 (let ((escape (char-after (match-beginning 1))))
1303 (delete-backward-char 2)
1304 (cond ((eq escape ?%)
1305 (insert ?%))
1306 ((eq escape ?\[)
1307 (setq button-begin (point))
1308 (insert (widget-get-indirect widget :button-prefix)))
1309 ((eq escape ?\])
1310 (insert (widget-get-indirect widget :button-suffix))
1311 (setq button-end (point)))
1312 ((eq escape ?\{)
1313 (setq sample-begin (point)))
1314 ((eq escape ?\})
1315 (setq sample-end (point)))
1316 ((eq escape ?n)
1317 (when (widget-get widget :indent)
1318 (insert ?\n)
1319 (insert-char ? (widget-get widget :indent))))
1320 ((eq escape ?t)
1321 (let ((image (widget-get widget :tag-glyph))
1322 (tag (widget-get widget :tag)))
1323 (cond (image
1324 (widget-image-insert widget (or tag "image") image))
1325 (tag
1326 (insert tag))
1328 (princ (widget-get widget :value)
1329 (current-buffer))))))
1330 ((eq escape ?d)
1331 (let ((doc (widget-get widget :doc)))
1332 (when doc
1333 (setq doc-begin (point))
1334 (insert doc)
1335 (while (eq (preceding-char) ?\n)
1336 (delete-backward-char 1))
1337 (insert ?\n)
1338 (setq doc-end (point)))))
1339 ((eq escape ?v)
1340 (if (and button-begin (not button-end))
1341 (widget-apply widget :value-create)
1342 (setq value-pos (point))))
1344 (widget-apply widget :format-handler escape)))))
1345 ;; Specify button, sample, and doc, and insert value.
1346 (and button-begin button-end
1347 (widget-specify-button widget button-begin button-end))
1348 (and sample-begin sample-end
1349 (widget-specify-sample widget sample-begin sample-end))
1350 (and doc-begin doc-end
1351 (widget-specify-doc widget doc-begin doc-end))
1352 (when value-pos
1353 (goto-char value-pos)
1354 (widget-apply widget :value-create)))
1355 (let ((from (point-min-marker))
1356 (to (point-max-marker)))
1357 (set-marker-insertion-type from t)
1358 (set-marker-insertion-type to nil)
1359 (widget-put widget :from from)
1360 (widget-put widget :to to)))
1361 (widget-clear-undo))
1363 (defun widget-default-format-handler (widget escape)
1364 ;; We recognize the %h escape by default.
1365 (let* ((buttons (widget-get widget :buttons)))
1366 (cond ((eq escape ?h)
1367 (let* ((doc-property (widget-get widget :documentation-property))
1368 (doc-try (cond ((widget-get widget :doc))
1369 ((functionp doc-property)
1370 (funcall doc-property
1371 (widget-get widget :value)))
1372 ((symbolp doc-property)
1373 (documentation-property
1374 (widget-get widget :value)
1375 doc-property))))
1376 (doc-text (and (stringp doc-try)
1377 (> (length doc-try) 1)
1378 doc-try))
1379 (doc-indent (widget-get widget :documentation-indent)))
1380 (when doc-text
1381 (and (eq (preceding-char) ?\n)
1382 (widget-get widget :indent)
1383 (insert-char ? (widget-get widget :indent)))
1384 ;; The `*' in the beginning is redundant.
1385 (when (eq (aref doc-text 0) ?*)
1386 (setq doc-text (substring doc-text 1)))
1387 ;; Get rid of trailing newlines.
1388 (when (string-match "\n+\\'" doc-text)
1389 (setq doc-text (substring doc-text 0 (match-beginning 0))))
1390 (push (widget-create-child-and-convert
1391 widget 'documentation-string
1392 :indent (cond ((numberp doc-indent )
1393 doc-indent)
1394 ((null doc-indent)
1395 nil)
1396 (t 0))
1397 doc-text)
1398 buttons))))
1400 (error "Unknown escape `%c'" escape)))
1401 (widget-put widget :buttons buttons)))
1403 (defun widget-default-button-face-get (widget)
1404 ;; Use :button-face or widget-button-face
1405 (or (widget-get widget :button-face)
1406 (let ((parent (widget-get widget :parent)))
1407 (if parent
1408 (widget-apply parent :button-face-get)
1409 widget-button-face))))
1411 (defun widget-default-sample-face-get (widget)
1412 ;; Use :sample-face.
1413 (widget-get widget :sample-face))
1415 (defun widget-default-delete (widget)
1416 "Remove widget from the buffer."
1417 (let ((from (widget-get widget :from))
1418 (to (widget-get widget :to))
1419 (inactive-overlay (widget-get widget :inactive))
1420 (button-overlay (widget-get widget :button-overlay))
1421 (sample-overlay (widget-get widget :sample-overlay))
1422 (doc-overlay (widget-get widget :doc-overlay))
1423 (inhibit-modification-hooks t)
1424 (inhibit-read-only t))
1425 (widget-apply widget :value-delete)
1426 (when inactive-overlay
1427 (delete-overlay inactive-overlay))
1428 (when button-overlay
1429 (delete-overlay button-overlay))
1430 (when sample-overlay
1431 (delete-overlay sample-overlay))
1432 (when doc-overlay
1433 (delete-overlay doc-overlay))
1434 (when (< from to)
1435 ;; Kludge: this doesn't need to be true for empty formats.
1436 (delete-region from to))
1437 (set-marker from nil)
1438 (set-marker to nil))
1439 (widget-clear-undo))
1441 (defun widget-default-value-set (widget value)
1442 "Recreate widget with new value."
1443 (let* ((old-pos (point))
1444 (from (copy-marker (widget-get widget :from)))
1445 (to (copy-marker (widget-get widget :to)))
1446 (offset (if (and (<= from old-pos) (<= old-pos to))
1447 (if (>= old-pos (1- to))
1448 (- old-pos to 1)
1449 (- old-pos from)))))
1450 ;;??? Bug: this ought to insert the new value before deleting the old one,
1451 ;; so that markers on either side of the value automatically
1452 ;; stay on the same side. -- rms.
1453 (save-excursion
1454 (goto-char (widget-get widget :from))
1455 (widget-apply widget :delete)
1456 (widget-put widget :value value)
1457 (widget-apply widget :create))
1458 (if offset
1459 (if (< offset 0)
1460 (goto-char (+ (widget-get widget :to) offset 1))
1461 (goto-char (min (+ from offset) (1- (widget-get widget :to))))))))
1463 (defun widget-default-value-inline (widget)
1464 "Wrap value in a list unless it is inline."
1465 (if (widget-get widget :inline)
1466 (widget-value widget)
1467 (list (widget-value widget))))
1469 (defun widget-default-default-get (widget)
1470 "Get `:value'."
1471 (widget-get widget :value))
1473 (defun widget-default-menu-tag-get (widget)
1474 "Use tag or value for menus."
1475 (or (widget-get widget :menu-tag)
1476 (widget-get widget :tag)
1477 (widget-princ-to-string (widget-get widget :value))))
1479 (defun widget-default-active (widget)
1480 "Return t iff this widget active (user modifiable)."
1481 (or (widget-get widget :always-active)
1482 (and (not (widget-get widget :inactive))
1483 (let ((parent (widget-get widget :parent)))
1484 (or (null parent)
1485 (widget-apply parent :active))))))
1487 (defun widget-default-deactivate (widget)
1488 "Make WIDGET inactive for user modifications."
1489 (widget-specify-inactive widget
1490 (widget-get widget :from)
1491 (widget-get widget :to)))
1493 (defun widget-default-action (widget &optional event)
1494 "Notify the parent when a widget changes."
1495 (let ((parent (widget-get widget :parent)))
1496 (when parent
1497 (widget-apply parent :notify widget event))))
1499 (defun widget-default-notify (widget child &optional event)
1500 "Pass notification to parent."
1501 (widget-default-action widget event))
1503 (defun widget-default-prompt-value (widget prompt value unbound)
1504 "Read an arbitrary value. Stolen from `set-variable'."
1505 ;; (let ((initial (if unbound
1506 ;; nil
1507 ;; It would be nice if we could do a `(cons val 1)' here.
1508 ;; (prin1-to-string (custom-quote value))))))
1509 (eval-minibuffer prompt))
1511 ;;; The `item' Widget.
1513 (define-widget 'item 'default
1514 "Constant items for inclusion in other widgets."
1515 :convert-widget 'widget-value-convert-widget
1516 :value-create 'widget-item-value-create
1517 :value-delete 'ignore
1518 :value-get 'widget-value-value-get
1519 :match 'widget-item-match
1520 :match-inline 'widget-item-match-inline
1521 :action 'widget-item-action
1522 :format "%t\n")
1524 (defun widget-item-value-create (widget)
1525 "Insert the printed representation of the value."
1526 (princ (widget-get widget :value) (current-buffer)))
1528 (defun widget-item-match (widget value)
1529 ;; Match if the value is the same.
1530 (equal (widget-get widget :value) value))
1532 (defun widget-item-match-inline (widget values)
1533 ;; Match if the value is the same.
1534 (let ((value (widget-get widget :value)))
1535 (and (listp value)
1536 (<= (length value) (length values))
1537 (let ((head (widget-sublist values 0 (length value))))
1538 (and (equal head value)
1539 (cons head (widget-sublist values (length value))))))))
1541 (defun widget-sublist (list start &optional end)
1542 "Return the sublist of LIST from START to END.
1543 If END is omitted, it defaults to the length of LIST."
1544 (if (> start 0) (setq list (nthcdr start list)))
1545 (if end
1546 (unless (<= end start)
1547 (setq list (copy-sequence list))
1548 (setcdr (nthcdr (- end start 1) list) nil)
1549 list)
1550 (copy-sequence list)))
1552 (defun widget-item-action (widget &optional event)
1553 ;; Just notify itself.
1554 (widget-apply widget :notify widget event))
1556 ;;; The `push-button' Widget.
1558 ;; (defcustom widget-push-button-gui t
1559 ;; "If non nil, use GUI push buttons when available."
1560 ;; :group 'widgets
1561 ;; :type 'boolean)
1563 ;; Cache already created GUI objects.
1564 ;; (defvar widget-push-button-cache nil)
1566 (defcustom widget-push-button-prefix "["
1567 "String used as prefix for buttons."
1568 :type 'string
1569 :group 'widget-button)
1571 (defcustom widget-push-button-suffix "]"
1572 "String used as suffix for buttons."
1573 :type 'string
1574 :group 'widget-button)
1576 (define-widget 'push-button 'item
1577 "A pushable button."
1578 :button-prefix ""
1579 :button-suffix ""
1580 :value-create 'widget-push-button-value-create
1581 :format "%[%v%]")
1583 (defun widget-push-button-value-create (widget)
1584 "Insert text representing the `on' and `off' states."
1585 (let* ((tag (or (widget-get widget :tag)
1586 (widget-get widget :value)))
1587 (tag-glyph (widget-get widget :tag-glyph))
1588 (text (concat widget-push-button-prefix
1589 tag widget-push-button-suffix)))
1590 (if tag-glyph
1591 (widget-image-insert widget text tag-glyph)
1592 (insert text))))
1594 ;; (defun widget-gui-action (widget)
1595 ;; "Apply :action for WIDGET."
1596 ;; (widget-apply-action widget (this-command-keys)))
1598 ;;; The `link' Widget.
1600 (defcustom widget-link-prefix "["
1601 "String used as prefix for links."
1602 :type 'string
1603 :group 'widget-button)
1605 (defcustom widget-link-suffix "]"
1606 "String used as suffix for links."
1607 :type 'string
1608 :group 'widget-button)
1610 (define-widget 'link 'item
1611 "An embedded link."
1612 :button-prefix 'widget-link-prefix
1613 :button-suffix 'widget-link-suffix
1614 :help-echo "Follow the link."
1615 :format "%[%t%]")
1617 ;;; The `info-link' Widget.
1619 (define-widget 'info-link 'link
1620 "A link to an info file."
1621 :action 'widget-info-link-action)
1623 (defun widget-info-link-action (widget &optional event)
1624 "Open the info node specified by WIDGET."
1625 (Info-goto-node (widget-value widget)))
1627 ;;; The `url-link' Widget.
1629 (define-widget 'url-link 'link
1630 "A link to an www page."
1631 :action 'widget-url-link-action)
1633 (defun widget-url-link-action (widget &optional event)
1634 "Open the url specified by WIDGET."
1635 (browse-url (widget-value widget)))
1637 ;;; The `function-link' Widget.
1639 (define-widget 'function-link 'link
1640 "A link to an Emacs function."
1641 :action 'widget-function-link-action)
1643 (defun widget-function-link-action (widget &optional event)
1644 "Show the function specified by WIDGET."
1645 (describe-function (widget-value widget)))
1647 ;;; The `variable-link' Widget.
1649 (define-widget 'variable-link 'link
1650 "A link to an Emacs variable."
1651 :action 'widget-variable-link-action)
1653 (defun widget-variable-link-action (widget &optional event)
1654 "Show the variable specified by WIDGET."
1655 (describe-variable (widget-value widget)))
1657 ;;; The `file-link' Widget.
1659 (define-widget 'file-link 'link
1660 "A link to a file."
1661 :action 'widget-file-link-action)
1663 (defun widget-file-link-action (widget &optional event)
1664 "Find the file specified by WIDGET."
1665 (find-file (widget-value widget)))
1667 ;;; The `emacs-library-link' Widget.
1669 (define-widget 'emacs-library-link 'link
1670 "A link to an Emacs Lisp library file."
1671 :action 'widget-emacs-library-link-action)
1673 (defun widget-emacs-library-link-action (widget &optional event)
1674 "Find the Emacs Library file specified by WIDGET."
1675 (find-file (locate-library (widget-value widget))))
1677 ;;; The `emacs-commentary-link' Widget.
1679 (define-widget 'emacs-commentary-link 'link
1680 "A link to Commentary in an Emacs Lisp library file."
1681 :action 'widget-emacs-commentary-link-action)
1683 (defun widget-emacs-commentary-link-action (widget &optional event)
1684 "Find the Commentary section of the Emacs file specified by WIDGET."
1685 (finder-commentary (widget-value widget)))
1687 ;;; The `editable-field' Widget.
1689 (define-widget 'editable-field 'default
1690 "An editable text field."
1691 :convert-widget 'widget-value-convert-widget
1692 :keymap widget-field-keymap
1693 :format "%v"
1694 :help-echo "M-TAB: complete field; RET: enter value"
1695 :value ""
1696 :prompt-internal 'widget-field-prompt-internal
1697 :prompt-history 'widget-field-history
1698 :prompt-value 'widget-field-prompt-value
1699 :action 'widget-field-action
1700 :validate 'widget-field-validate
1701 :valid-regexp ""
1702 :error "Field's value doesn't match allowed forms"
1703 :value-create 'widget-field-value-create
1704 :value-delete 'widget-field-value-delete
1705 :value-get 'widget-field-value-get
1706 :match 'widget-field-match)
1708 (defvar widget-field-history nil
1709 "History of field minibuffer edits.")
1711 (defun widget-field-prompt-internal (widget prompt initial history)
1712 "Read string for WIDGET promptinhg with PROMPT.
1713 INITIAL is the initial input and HISTORY is a symbol containing
1714 the earlier input."
1715 (read-string prompt initial history))
1717 (defun widget-field-prompt-value (widget prompt value unbound)
1718 "Prompt for a string."
1719 (widget-apply widget
1720 :value-to-external
1721 (widget-apply widget
1722 :prompt-internal prompt
1723 (unless unbound
1724 (cons (widget-apply widget
1725 :value-to-internal value)
1727 (widget-get widget :prompt-history))))
1729 (defvar widget-edit-functions nil)
1731 (defun widget-field-action (widget &optional event)
1732 "Move to next field."
1733 (widget-forward 1)
1734 (run-hook-with-args 'widget-edit-functions widget))
1736 (defun widget-field-validate (widget)
1737 "Valid if the content matches `:valid-regexp'."
1738 (unless (string-match (widget-get widget :valid-regexp)
1739 (widget-apply widget :value-get))
1740 widget))
1742 (defun widget-field-value-create (widget)
1743 "Create an editable text field."
1744 (let ((size (widget-get widget :size))
1745 (value (widget-get widget :value))
1746 (from (point))
1747 ;; This is changed to a real overlay in `widget-setup'. We
1748 ;; need the end points to behave differently until
1749 ;; `widget-setup' is called.
1750 (overlay (cons (make-marker) (make-marker))))
1751 (widget-put widget :field-overlay overlay)
1752 (insert value)
1753 (and size
1754 (< (length value) size)
1755 (insert-char ?\ (- size (length value))))
1756 (unless (memq widget widget-field-list)
1757 (setq widget-field-new (cons widget widget-field-new)))
1758 (move-marker (cdr overlay) (point))
1759 (set-marker-insertion-type (cdr overlay) nil)
1760 (when (null size)
1761 (insert ?\n))
1762 (move-marker (car overlay) from)
1763 (set-marker-insertion-type (car overlay) t)))
1765 (defun widget-field-value-delete (widget)
1766 "Remove the widget from the list of active editing fields."
1767 (setq widget-field-list (delq widget widget-field-list))
1768 (setq widget-field-new (delq widget widget-field-new))
1769 ;; These are nil if the :format string doesn't contain `%v'.
1770 (let ((overlay (widget-get widget :field-overlay)))
1771 (when (overlayp overlay)
1772 (delete-overlay overlay))))
1774 (defun widget-field-value-get (widget)
1775 "Return current text in editing field."
1776 (let ((from (widget-field-start widget))
1777 (to (widget-field-end widget))
1778 (buffer (widget-field-buffer widget))
1779 (size (widget-get widget :size))
1780 (secret (widget-get widget :secret))
1781 (old (current-buffer)))
1782 (if (and from to)
1783 (progn
1784 (set-buffer buffer)
1785 (while (and size
1786 (not (zerop size))
1787 (> to from)
1788 (eq (char-after (1- to)) ?\ ))
1789 (setq to (1- to)))
1790 (let ((result (buffer-substring-no-properties from to)))
1791 (when secret
1792 (let ((index 0))
1793 (while (< (+ from index) to)
1794 (aset result index
1795 (get-char-property (+ from index) 'secret))
1796 (setq index (1+ index)))))
1797 (set-buffer old)
1798 result))
1799 (widget-get widget :value))))
1801 (defun widget-field-match (widget value)
1802 ;; Match any string.
1803 (stringp value))
1805 ;;; The `text' Widget.
1807 (define-widget 'text 'editable-field
1808 :keymap widget-text-keymap
1809 "A multiline text area.")
1811 ;;; The `menu-choice' Widget.
1813 (define-widget 'menu-choice 'default
1814 "A menu of options."
1815 :convert-widget 'widget-types-convert-widget
1816 :format "%[%t%]: %v"
1817 :case-fold t
1818 :tag "choice"
1819 :void '(item :format "invalid (%t)\n")
1820 :value-create 'widget-choice-value-create
1821 :value-delete 'widget-children-value-delete
1822 :value-get 'widget-choice-value-get
1823 :value-inline 'widget-choice-value-inline
1824 :default-get 'widget-choice-default-get
1825 :mouse-down-action 'widget-choice-mouse-down-action
1826 :action 'widget-choice-action
1827 :error "Make a choice"
1828 :validate 'widget-choice-validate
1829 :match 'widget-choice-match
1830 :match-inline 'widget-choice-match-inline)
1832 (defun widget-choice-value-create (widget)
1833 "Insert the first choice that matches the value."
1834 (let ((value (widget-get widget :value))
1835 (args (widget-get widget :args))
1836 (explicit (widget-get widget :explicit-choice))
1837 current)
1838 (if (and explicit (equal value (widget-get widget :explicit-choice-value)))
1839 (progn
1840 ;; If the user specified the choice for this value,
1841 ;; respect that choice as long as the value is the same.
1842 (widget-put widget :children (list (widget-create-child-value
1843 widget explicit value)))
1844 (widget-put widget :choice explicit))
1845 (while args
1846 (setq current (car args)
1847 args (cdr args))
1848 (when (widget-apply current :match value)
1849 (widget-put widget :children (list (widget-create-child-value
1850 widget current value)))
1851 (widget-put widget :choice current)
1852 (setq args nil
1853 current nil)))
1854 (when current
1855 (let ((void (widget-get widget :void)))
1856 (widget-put widget :children (list (widget-create-child-and-convert
1857 widget void :value value)))
1858 (widget-put widget :choice void))))))
1860 (defun widget-choice-value-get (widget)
1861 ;; Get value of the child widget.
1862 (widget-value (car (widget-get widget :children))))
1864 (defun widget-choice-value-inline (widget)
1865 ;; Get value of the child widget.
1866 (widget-apply (car (widget-get widget :children)) :value-inline))
1868 (defun widget-choice-default-get (widget)
1869 ;; Get default for the first choice.
1870 (widget-default-get (car (widget-get widget :args))))
1872 (defcustom widget-choice-toggle nil
1873 "If non-nil, a binary choice will just toggle between the values.
1874 Otherwise, the user will explicitly have to choose between the values
1875 when he invoked the menu."
1876 :type 'boolean
1877 :group 'widgets)
1879 (defun widget-choice-mouse-down-action (widget &optional event)
1880 ;; Return non-nil if we need a menu.
1881 (let ((args (widget-get widget :args))
1882 (old (widget-get widget :choice)))
1883 (cond ((not (display-popup-menus-p))
1884 ;; No place to pop up a menu.
1885 nil)
1886 ((< (length args) 2)
1887 ;; Empty or singleton list, just return the value.
1888 nil)
1889 ((> (length args) widget-menu-max-size)
1890 ;; Too long, prompt.
1891 nil)
1892 ((> (length args) 2)
1893 ;; Reasonable sized list, use menu.
1895 ((and widget-choice-toggle (memq old args))
1896 ;; We toggle.
1897 nil)
1899 ;; Ask which of the two.
1900 t))))
1902 (defun widget-choice-action (widget &optional event)
1903 ;; Make a choice.
1904 (let ((args (widget-get widget :args))
1905 (old (widget-get widget :choice))
1906 (tag (widget-apply widget :menu-tag-get))
1907 (completion-ignore-case (widget-get widget :case-fold))
1908 this-explicit
1909 current choices)
1910 ;; Remember old value.
1911 (if (and old (not (widget-apply widget :validate)))
1912 (let* ((external (widget-value widget))
1913 (internal (widget-apply old :value-to-internal external)))
1914 (widget-put old :value internal)))
1915 ;; Find new choice.
1916 (setq current
1917 (cond ((= (length args) 0)
1918 nil)
1919 ((= (length args) 1)
1920 (nth 0 args))
1921 ((and widget-choice-toggle
1922 (= (length args) 2)
1923 (memq old args))
1924 (if (eq old (nth 0 args))
1925 (nth 1 args)
1926 (nth 0 args)))
1928 (while args
1929 (setq current (car args)
1930 args (cdr args))
1931 (setq choices
1932 (cons (cons (widget-apply current :menu-tag-get)
1933 current)
1934 choices)))
1935 (setq this-explicit t)
1936 (widget-choose tag (reverse choices) event))))
1937 (when current
1938 ;; If this was an explicit user choice,
1939 ;; record the choice, and the record the value it was made for.
1940 ;; widget-choice-value-create will respect this choice,
1941 ;; as long as the value is the same.
1942 (when this-explicit
1943 (widget-put widget :explicit-choice current)
1944 (widget-put widget :explicit-choice-value (widget-get widget :value)))
1945 (widget-value-set
1946 widget (widget-apply current
1947 :value-to-external (widget-default-get current)))
1948 (widget-setup)
1949 (widget-apply widget :notify widget event)))
1950 (run-hook-with-args 'widget-edit-functions widget))
1952 (defun widget-choice-validate (widget)
1953 ;; Valid if we have made a valid choice.
1954 (if (eq (widget-get widget :void) (widget-get widget :choice))
1955 widget
1956 (widget-apply (car (widget-get widget :children)) :validate)))
1958 (defun widget-choice-match (widget value)
1959 ;; Matches if one of the choices matches.
1960 (let ((args (widget-get widget :args))
1961 current found)
1962 (while (and args (not found))
1963 (setq current (car args)
1964 args (cdr args)
1965 found (widget-apply current :match value)))
1966 found))
1968 (defun widget-choice-match-inline (widget values)
1969 ;; Matches if one of the choices matches.
1970 (let ((args (widget-get widget :args))
1971 current found)
1972 (while (and args (null found))
1973 (setq current (car args)
1974 args (cdr args)
1975 found (widget-match-inline current values)))
1976 found))
1978 ;;; The `toggle' Widget.
1980 (define-widget 'toggle 'item
1981 "Toggle between two states."
1982 :format "%[%v%]\n"
1983 :value-create 'widget-toggle-value-create
1984 :action 'widget-toggle-action
1985 :match (lambda (widget value) t)
1986 :on "on"
1987 :off "off")
1989 (defun widget-toggle-value-create (widget)
1990 "Insert text representing the `on' and `off' states."
1991 (if (widget-value widget)
1992 (widget-image-insert widget
1993 (widget-get widget :on)
1994 (widget-get widget :on-glyph))
1995 (widget-image-insert widget
1996 (widget-get widget :off)
1997 (widget-get widget :off-glyph))))
1999 (defun widget-toggle-action (widget &optional event)
2000 ;; Toggle value.
2001 (widget-value-set widget (not (widget-value widget)))
2002 (widget-apply widget :notify widget event)
2003 (run-hook-with-args 'widget-edit-functions widget))
2005 ;;; The `checkbox' Widget.
2007 (define-widget 'checkbox 'toggle
2008 "A checkbox toggle."
2009 :button-suffix ""
2010 :button-prefix ""
2011 :format "%[%v%]"
2012 :on "[X]"
2013 ;; We could probably do the same job as the images using single
2014 ;; space characters in a boxed face with a stretch specification to
2015 ;; make them square.
2016 :on-glyph (create-image "\377\311\301\343\301\311\377" ; this is an `X'
2017 'xbm t :width 7 :height 7
2018 :foreground "grey75" ; like default mode line
2019 :background "black"
2020 :relief -3
2021 :ascent 'center)
2022 :off "[ ]"
2023 :off-glyph (create-image (make-bool-vector 49 1)
2024 'xbm t :width 7 :height 7
2025 :foreground "grey75"
2026 :relief 3
2027 :ascent 'center)
2028 :help-echo "Toggle this item."
2029 :action 'widget-checkbox-action)
2031 (defun widget-checkbox-action (widget &optional event)
2032 "Toggle checkbox, notify parent, and set active state of sibling."
2033 (widget-toggle-action widget event)
2034 (let ((sibling (widget-get-sibling widget)))
2035 (when sibling
2036 (if (widget-value widget)
2037 (widget-apply sibling :activate)
2038 (widget-apply sibling :deactivate)))))
2040 ;;; The `checklist' Widget.
2042 (define-widget 'checklist 'default
2043 "A multiple choice widget."
2044 :convert-widget 'widget-types-convert-widget
2045 :format "%v"
2046 :offset 4
2047 :entry-format "%b %v"
2048 :menu-tag "checklist"
2049 :greedy nil
2050 :value-create 'widget-checklist-value-create
2051 :value-delete 'widget-children-value-delete
2052 :value-get 'widget-checklist-value-get
2053 :validate 'widget-checklist-validate
2054 :match 'widget-checklist-match
2055 :match-inline 'widget-checklist-match-inline)
2057 (defun widget-checklist-value-create (widget)
2058 ;; Insert all values
2059 (let ((alist (widget-checklist-match-find widget (widget-get widget :value)))
2060 (args (widget-get widget :args)))
2061 (while args
2062 (widget-checklist-add-item widget (car args) (assq (car args) alist))
2063 (setq args (cdr args)))
2064 (widget-put widget :children (nreverse (widget-get widget :children)))))
2066 (defun widget-checklist-add-item (widget type chosen)
2067 "Create checklist item in WIDGET of type TYPE.
2068 If the item is checked, CHOSEN is a cons whose cdr is the value."
2069 (and (eq (preceding-char) ?\n)
2070 (widget-get widget :indent)
2071 (insert-char ? (widget-get widget :indent)))
2072 (widget-specify-insert
2073 (let* ((children (widget-get widget :children))
2074 (buttons (widget-get widget :buttons))
2075 (button-args (or (widget-get type :sibling-args)
2076 (widget-get widget :button-args)))
2077 (from (point))
2078 child button)
2079 (insert (widget-get widget :entry-format))
2080 (goto-char from)
2081 ;; Parse % escapes in format.
2082 (while (re-search-forward "%\\([bv%]\\)" nil t)
2083 (let ((escape (char-after (match-beginning 1))))
2084 (delete-backward-char 2)
2085 (cond ((eq escape ?%)
2086 (insert ?%))
2087 ((eq escape ?b)
2088 (setq button (apply 'widget-create-child-and-convert
2089 widget 'checkbox
2090 :value (not (null chosen))
2091 button-args)))
2092 ((eq escape ?v)
2093 (setq child
2094 (cond ((not chosen)
2095 (let ((child (widget-create-child widget type)))
2096 (widget-apply child :deactivate)
2097 child))
2098 ((widget-get type :inline)
2099 (widget-create-child-value
2100 widget type (cdr chosen)))
2102 (widget-create-child-value
2103 widget type (car (cdr chosen)))))))
2105 (error "Unknown escape `%c'" escape)))))
2106 ;; Update properties.
2107 (and button child (widget-put child :button button))
2108 (and button (widget-put widget :buttons (cons button buttons)))
2109 (and child (widget-put widget :children (cons child children))))))
2111 (defun widget-checklist-match (widget values)
2112 ;; All values must match a type in the checklist.
2113 (and (listp values)
2114 (null (cdr (widget-checklist-match-inline widget values)))))
2116 (defun widget-checklist-match-inline (widget values)
2117 ;; Find the values which match a type in the checklist.
2118 (let ((greedy (widget-get widget :greedy))
2119 (args (copy-sequence (widget-get widget :args)))
2120 found rest)
2121 (while values
2122 (let ((answer (widget-checklist-match-up args values)))
2123 (cond (answer
2124 (let ((vals (widget-match-inline answer values)))
2125 (setq found (append found (car vals))
2126 values (cdr vals)
2127 args (delq answer args))))
2128 (greedy
2129 (setq rest (append rest (list (car values)))
2130 values (cdr values)))
2132 (setq rest (append rest values)
2133 values nil)))))
2134 (cons found rest)))
2136 (defun widget-checklist-match-find (widget vals)
2137 "Find the vals which match a type in the checklist.
2138 Return an alist of (TYPE MATCH)."
2139 (let ((greedy (widget-get widget :greedy))
2140 (args (copy-sequence (widget-get widget :args)))
2141 found)
2142 (while vals
2143 (let ((answer (widget-checklist-match-up args vals)))
2144 (cond (answer
2145 (let ((match (widget-match-inline answer vals)))
2146 (setq found (cons (cons answer (car match)) found)
2147 vals (cdr match)
2148 args (delq answer args))))
2149 (greedy
2150 (setq vals (cdr vals)))
2152 (setq vals nil)))))
2153 found))
2155 (defun widget-checklist-match-up (args vals)
2156 "Return the first type from ARGS that matches VALS."
2157 (let (current found)
2158 (while (and args (null found))
2159 (setq current (car args)
2160 args (cdr args)
2161 found (widget-match-inline current vals)))
2162 (if found
2163 current)))
2165 (defun widget-checklist-value-get (widget)
2166 ;; The values of all selected items.
2167 (let ((children (widget-get widget :children))
2168 child result)
2169 (while children
2170 (setq child (car children)
2171 children (cdr children))
2172 (if (widget-value (widget-get child :button))
2173 (setq result (append result (widget-apply child :value-inline)))))
2174 result))
2176 (defun widget-checklist-validate (widget)
2177 ;; Ticked chilren must be valid.
2178 (let ((children (widget-get widget :children))
2179 child button found)
2180 (while (and children (not found))
2181 (setq child (car children)
2182 children (cdr children)
2183 button (widget-get child :button)
2184 found (and (widget-value button)
2185 (widget-apply child :validate))))
2186 found))
2188 ;;; The `option' Widget
2190 (define-widget 'option 'checklist
2191 "An widget with an optional item."
2192 :inline t)
2194 ;;; The `choice-item' Widget.
2196 (define-widget 'choice-item 'item
2197 "Button items that delegate action events to their parents."
2198 :action 'widget-parent-action
2199 :format "%[%t%] \n")
2201 ;;; The `radio-button' Widget.
2203 (define-widget 'radio-button 'toggle
2204 "A radio button for use in the `radio' widget."
2205 :notify 'widget-radio-button-notify
2206 :format "%[%v%]"
2207 :button-suffix ""
2208 :button-prefix ""
2209 :on "(*)"
2210 :on-glyph "radio1"
2211 :off "( )"
2212 :off-glyph "radio0")
2214 (defun widget-radio-button-notify (widget child &optional event)
2215 ;; Tell daddy.
2216 (widget-apply (widget-get widget :parent) :action widget event))
2218 ;;; The `radio-button-choice' Widget.
2220 (define-widget 'radio-button-choice 'default
2221 "Select one of multiple options."
2222 :convert-widget 'widget-types-convert-widget
2223 :offset 4
2224 :format "%v"
2225 :entry-format "%b %v"
2226 :menu-tag "radio"
2227 :value-create 'widget-radio-value-create
2228 :value-delete 'widget-children-value-delete
2229 :value-get 'widget-radio-value-get
2230 :value-inline 'widget-radio-value-inline
2231 :value-set 'widget-radio-value-set
2232 :error "You must push one of the buttons"
2233 :validate 'widget-radio-validate
2234 :match 'widget-choice-match
2235 :match-inline 'widget-choice-match-inline
2236 :action 'widget-radio-action)
2238 (defun widget-radio-value-create (widget)
2239 ;; Insert all values
2240 (let ((args (widget-get widget :args))
2241 arg)
2242 (while args
2243 (setq arg (car args)
2244 args (cdr args))
2245 (widget-radio-add-item widget arg))))
2247 (defun widget-radio-add-item (widget type)
2248 "Add to radio widget WIDGET a new radio button item of type TYPE."
2249 ;; (setq type (widget-convert type))
2250 (and (eq (preceding-char) ?\n)
2251 (widget-get widget :indent)
2252 (insert-char ? (widget-get widget :indent)))
2253 (widget-specify-insert
2254 (let* ((value (widget-get widget :value))
2255 (children (widget-get widget :children))
2256 (buttons (widget-get widget :buttons))
2257 (button-args (or (widget-get type :sibling-args)
2258 (widget-get widget :button-args)))
2259 (from (point))
2260 (chosen (and (null (widget-get widget :choice))
2261 (widget-apply type :match value)))
2262 child button)
2263 (insert (widget-get widget :entry-format))
2264 (goto-char from)
2265 ;; Parse % escapes in format.
2266 (while (re-search-forward "%\\([bv%]\\)" nil t)
2267 (let ((escape (char-after (match-beginning 1))))
2268 (delete-backward-char 2)
2269 (cond ((eq escape ?%)
2270 (insert ?%))
2271 ((eq escape ?b)
2272 (setq button (apply 'widget-create-child-and-convert
2273 widget 'radio-button
2274 :value (not (null chosen))
2275 button-args)))
2276 ((eq escape ?v)
2277 (setq child (if chosen
2278 (widget-create-child-value
2279 widget type value)
2280 (widget-create-child widget type)))
2281 (unless chosen
2282 (widget-apply child :deactivate)))
2284 (error "Unknown escape `%c'" escape)))))
2285 ;; Update properties.
2286 (when chosen
2287 (widget-put widget :choice type))
2288 (when button
2289 (widget-put child :button button)
2290 (widget-put widget :buttons (nconc buttons (list button))))
2291 (when child
2292 (widget-put widget :children (nconc children (list child))))
2293 child)))
2295 (defun widget-radio-value-get (widget)
2296 ;; Get value of the child widget.
2297 (let ((chosen (widget-radio-chosen widget)))
2298 (and chosen (widget-value chosen))))
2300 (defun widget-radio-chosen (widget)
2301 "Return the widget representing the chosen radio button."
2302 (let ((children (widget-get widget :children))
2303 current found)
2304 (while children
2305 (setq current (car children)
2306 children (cdr children))
2307 (when (widget-apply (widget-get current :button) :value-get)
2308 (setq found current
2309 children nil)))
2310 found))
2312 (defun widget-radio-value-inline (widget)
2313 ;; Get value of the child widget.
2314 (let ((children (widget-get widget :children))
2315 current found)
2316 (while children
2317 (setq current (car children)
2318 children (cdr children))
2319 (when (widget-apply (widget-get current :button) :value-get)
2320 (setq found (widget-apply current :value-inline)
2321 children nil)))
2322 found))
2324 (defun widget-radio-value-set (widget value)
2325 ;; We can't just delete and recreate a radio widget, since children
2326 ;; can be added after the original creation and won't be recreated
2327 ;; by `:create'.
2328 (let ((children (widget-get widget :children))
2329 current found)
2330 (while children
2331 (setq current (car children)
2332 children (cdr children))
2333 (let* ((button (widget-get current :button))
2334 (match (and (not found)
2335 (widget-apply current :match value))))
2336 (widget-value-set button match)
2337 (if match
2338 (progn
2339 (widget-value-set current value)
2340 (widget-apply current :activate))
2341 (widget-apply current :deactivate))
2342 (setq found (or found match))))))
2344 (defun widget-radio-validate (widget)
2345 ;; Valid if we have made a valid choice.
2346 (let ((children (widget-get widget :children))
2347 current found button)
2348 (while (and children (not found))
2349 (setq current (car children)
2350 children (cdr children)
2351 button (widget-get current :button)
2352 found (widget-apply button :value-get)))
2353 (if found
2354 (widget-apply current :validate)
2355 widget)))
2357 (defun widget-radio-action (widget child event)
2358 ;; Check if a radio button was pressed.
2359 (let ((children (widget-get widget :children))
2360 (buttons (widget-get widget :buttons))
2361 current)
2362 (when (memq child buttons)
2363 (while children
2364 (setq current (car children)
2365 children (cdr children))
2366 (let* ((button (widget-get current :button)))
2367 (cond ((eq child button)
2368 (widget-value-set button t)
2369 (widget-apply current :activate))
2370 ((widget-value button)
2371 (widget-value-set button nil)
2372 (widget-apply current :deactivate)))))))
2373 ;; Pass notification to parent.
2374 (widget-apply widget :notify child event))
2376 ;;; The `insert-button' Widget.
2378 (define-widget 'insert-button 'push-button
2379 "An insert button for the `editable-list' widget."
2380 :tag "INS"
2381 :help-echo "Insert a new item into the list at this position."
2382 :action 'widget-insert-button-action)
2384 (defun widget-insert-button-action (widget &optional event)
2385 ;; Ask the parent to insert a new item.
2386 (widget-apply (widget-get widget :parent)
2387 :insert-before (widget-get widget :widget)))
2389 ;;; The `delete-button' Widget.
2391 (define-widget 'delete-button 'push-button
2392 "A delete button for the `editable-list' widget."
2393 :tag "DEL"
2394 :help-echo "Delete this item from the list."
2395 :action 'widget-delete-button-action)
2397 (defun widget-delete-button-action (widget &optional event)
2398 ;; Ask the parent to insert a new item.
2399 (widget-apply (widget-get widget :parent)
2400 :delete-at (widget-get widget :widget)))
2402 ;;; The `editable-list' Widget.
2404 ;; (defcustom widget-editable-list-gui nil
2405 ;; "If non nil, use GUI push-buttons in editable list when available."
2406 ;; :type 'boolean
2407 ;; :group 'widgets)
2409 (define-widget 'editable-list 'default
2410 "A variable list of widgets of the same type."
2411 :convert-widget 'widget-types-convert-widget
2412 :offset 12
2413 :format "%v%i\n"
2414 :format-handler 'widget-editable-list-format-handler
2415 :entry-format "%i %d %v"
2416 :menu-tag "editable-list"
2417 :value-create 'widget-editable-list-value-create
2418 :value-delete 'widget-children-value-delete
2419 :value-get 'widget-editable-list-value-get
2420 :validate 'widget-children-validate
2421 :match 'widget-editable-list-match
2422 :match-inline 'widget-editable-list-match-inline
2423 :insert-before 'widget-editable-list-insert-before
2424 :delete-at 'widget-editable-list-delete-at)
2426 (defun widget-editable-list-format-handler (widget escape)
2427 ;; We recognize the insert button.
2428 ;;; (let ((widget-push-button-gui widget-editable-list-gui))
2429 (cond ((eq escape ?i)
2430 (and (widget-get widget :indent)
2431 (insert-char ?\ (widget-get widget :indent)))
2432 (apply 'widget-create-child-and-convert
2433 widget 'insert-button
2434 (widget-get widget :append-button-args)))
2436 (widget-default-format-handler widget escape)))
2437 ;;; )
2440 (defun widget-editable-list-value-create (widget)
2441 ;; Insert all values
2442 (let* ((value (widget-get widget :value))
2443 (type (nth 0 (widget-get widget :args)))
2444 children)
2445 (widget-put widget :value-pos (copy-marker (point)))
2446 (set-marker-insertion-type (widget-get widget :value-pos) t)
2447 (while value
2448 (let ((answer (widget-match-inline type value)))
2449 (if answer
2450 (setq children (cons (widget-editable-list-entry-create
2451 widget
2452 (if (widget-get type :inline)
2453 (car answer)
2454 (car (car answer)))
2456 children)
2457 value (cdr answer))
2458 (setq value nil))))
2459 (widget-put widget :children (nreverse children))))
2461 (defun widget-editable-list-value-get (widget)
2462 ;; Get value of the child widget.
2463 (apply 'append (mapcar (lambda (child) (widget-apply child :value-inline))
2464 (widget-get widget :children))))
2466 (defun widget-editable-list-match (widget value)
2467 ;; Value must be a list and all the members must match the type.
2468 (and (listp value)
2469 (null (cdr (widget-editable-list-match-inline widget value)))))
2471 (defun widget-editable-list-match-inline (widget value)
2472 (let ((type (nth 0 (widget-get widget :args)))
2473 (ok t)
2474 found)
2475 (while (and value ok)
2476 (let ((answer (widget-match-inline type value)))
2477 (if answer
2478 (setq found (append found (car answer))
2479 value (cdr answer))
2480 (setq ok nil))))
2481 (cons found value)))
2483 (defun widget-editable-list-insert-before (widget before)
2484 ;; Insert a new child in the list of children.
2485 (save-excursion
2486 (let ((children (widget-get widget :children))
2487 (inhibit-read-only t)
2488 before-change-functions
2489 after-change-functions)
2490 (cond (before
2491 (goto-char (widget-get before :entry-from)))
2493 (goto-char (widget-get widget :value-pos))))
2494 (let ((child (widget-editable-list-entry-create
2495 widget nil nil)))
2496 (when (< (widget-get child :entry-from) (widget-get widget :from))
2497 (set-marker (widget-get widget :from)
2498 (widget-get child :entry-from)))
2499 (if (eq (car children) before)
2500 (widget-put widget :children (cons child children))
2501 (while (not (eq (car (cdr children)) before))
2502 (setq children (cdr children)))
2503 (setcdr children (cons child (cdr children)))))))
2504 (widget-setup)
2505 (widget-apply widget :notify widget))
2507 (defun widget-editable-list-delete-at (widget child)
2508 ;; Delete child from list of children.
2509 (save-excursion
2510 (let ((buttons (copy-sequence (widget-get widget :buttons)))
2511 button
2512 (inhibit-read-only t)
2513 before-change-functions
2514 after-change-functions)
2515 (while buttons
2516 (setq button (car buttons)
2517 buttons (cdr buttons))
2518 (when (eq (widget-get button :widget) child)
2519 (widget-put widget
2520 :buttons (delq button (widget-get widget :buttons)))
2521 (widget-delete button))))
2522 (let ((entry-from (widget-get child :entry-from))
2523 (entry-to (widget-get child :entry-to))
2524 (inhibit-read-only t)
2525 before-change-functions
2526 after-change-functions)
2527 (widget-delete child)
2528 (delete-region entry-from entry-to)
2529 (set-marker entry-from nil)
2530 (set-marker entry-to nil))
2531 (widget-put widget :children (delq child (widget-get widget :children))))
2532 (widget-setup)
2533 (widget-apply widget :notify widget))
2535 (defun widget-editable-list-entry-create (widget value conv)
2536 ;; Create a new entry to the list.
2537 (let ((type (nth 0 (widget-get widget :args)))
2538 ;;; (widget-push-button-gui widget-editable-list-gui)
2539 child delete insert)
2540 (widget-specify-insert
2541 (save-excursion
2542 (and (widget-get widget :indent)
2543 (insert-char ?\ (widget-get widget :indent)))
2544 (insert (widget-get widget :entry-format)))
2545 ;; Parse % escapes in format.
2546 (while (re-search-forward "%\\(.\\)" nil t)
2547 (let ((escape (char-after (match-beginning 1))))
2548 (delete-backward-char 2)
2549 (cond ((eq escape ?%)
2550 (insert ?%))
2551 ((eq escape ?i)
2552 (setq insert (apply 'widget-create-child-and-convert
2553 widget 'insert-button
2554 (widget-get widget :insert-button-args))))
2555 ((eq escape ?d)
2556 (setq delete (apply 'widget-create-child-and-convert
2557 widget 'delete-button
2558 (widget-get widget :delete-button-args))))
2559 ((eq escape ?v)
2560 (if conv
2561 (setq child (widget-create-child-value
2562 widget type value))
2563 (setq child (widget-create-child-value
2564 widget type
2565 (widget-apply type :value-to-external
2566 (widget-default-get type))))))
2568 (error "Unknown escape `%c'" escape)))))
2569 (widget-put widget
2570 :buttons (cons delete
2571 (cons insert
2572 (widget-get widget :buttons))))
2573 (let ((entry-from (point-min-marker))
2574 (entry-to (point-max-marker)))
2575 (set-marker-insertion-type entry-from t)
2576 (set-marker-insertion-type entry-to nil)
2577 (widget-put child :entry-from entry-from)
2578 (widget-put child :entry-to entry-to)))
2579 (widget-put insert :widget child)
2580 (widget-put delete :widget child)
2581 child))
2583 ;;; The `group' Widget.
2585 (define-widget 'group 'default
2586 "A widget which groups other widgets inside."
2587 :convert-widget 'widget-types-convert-widget
2588 :format "%v"
2589 :value-create 'widget-group-value-create
2590 :value-delete 'widget-children-value-delete
2591 :value-get 'widget-editable-list-value-get
2592 :default-get 'widget-group-default-get
2593 :validate 'widget-children-validate
2594 :match 'widget-group-match
2595 :match-inline 'widget-group-match-inline)
2597 (defun widget-group-value-create (widget)
2598 ;; Create each component.
2599 (let ((args (widget-get widget :args))
2600 (value (widget-get widget :value))
2601 arg answer children)
2602 (while args
2603 (setq arg (car args)
2604 args (cdr args)
2605 answer (widget-match-inline arg value)
2606 value (cdr answer))
2607 (and (eq (preceding-char) ?\n)
2608 (widget-get widget :indent)
2609 (insert-char ?\ (widget-get widget :indent)))
2610 (push (cond ((null answer)
2611 (widget-create-child widget arg))
2612 ((widget-get arg :inline)
2613 (widget-create-child-value widget arg (car answer)))
2615 (widget-create-child-value widget arg (car (car answer)))))
2616 children))
2617 (widget-put widget :children (nreverse children))))
2619 (defun widget-group-default-get (widget)
2620 ;; Get the default of the components.
2621 (mapcar 'widget-default-get (widget-get widget :args)))
2623 (defun widget-group-match (widget values)
2624 ;; Match if the components match.
2625 (and (listp values)
2626 (let ((match (widget-group-match-inline widget values)))
2627 (and match (null (cdr match))))))
2629 (defun widget-group-match-inline (widget vals)
2630 ;; Match if the components match.
2631 (let ((args (widget-get widget :args))
2632 argument answer found)
2633 (while args
2634 (setq argument (car args)
2635 args (cdr args)
2636 answer (widget-match-inline argument vals))
2637 (if answer
2638 (setq vals (cdr answer)
2639 found (append found (car answer)))
2640 (setq vals nil
2641 args nil)))
2642 (if answer
2643 (cons found vals))))
2645 ;;; The `visibility' Widget.
2647 (define-widget 'visibility 'item
2648 "An indicator and manipulator for hidden items."
2649 :format "%[%v%]"
2650 :button-prefix ""
2651 :button-suffix ""
2652 :on "Hide"
2653 :off "Show"
2654 :value-create 'widget-visibility-value-create
2655 :action 'widget-toggle-action
2656 :match (lambda (widget value) t))
2658 (defun widget-visibility-value-create (widget)
2659 ;; Insert text representing the `on' and `off' states.
2660 (let ((on (widget-get widget :on))
2661 (off (widget-get widget :off)))
2662 (if on
2663 (setq on (concat widget-push-button-prefix
2665 widget-push-button-suffix))
2666 (setq on ""))
2667 (if off
2668 (setq off (concat widget-push-button-prefix
2670 widget-push-button-suffix))
2671 (setq off ""))
2672 (if (widget-value widget)
2673 (widget-image-insert widget on "down" "down-pushed")
2674 (widget-image-insert widget off "right" "right-pushed"))))
2676 ;;; The `documentation-link' Widget.
2678 ;; This is a helper widget for `documentation-string'.
2680 (define-widget 'documentation-link 'link
2681 "Link type used in documentation strings."
2682 :tab-order -1
2683 :help-echo "Describe this symbol"
2684 :action 'widget-documentation-link-action)
2686 (defun widget-documentation-link-action (widget &optional event)
2687 "Display documentation for WIDGET's value. Ignore optional argument EVENT."
2688 (let* ((string (widget-get widget :value))
2689 (symbol (intern string)))
2690 (if (and (fboundp symbol) (boundp symbol))
2691 ;; If there are two doc strings, give the user a way to pick one.
2692 (apropos (concat "\\`" (regexp-quote string) "\\'"))
2693 (if (fboundp symbol)
2694 (describe-function symbol)
2695 (describe-variable symbol)))))
2697 (defcustom widget-documentation-links t
2698 "Add hyperlinks to documentation strings when non-nil."
2699 :type 'boolean
2700 :group 'widget-documentation)
2702 (defcustom widget-documentation-link-regexp "`\\([^\n`' ]+\\)'"
2703 "Regexp for matching potential links in documentation strings.
2704 The first group should be the link itself."
2705 :type 'regexp
2706 :group 'widget-documentation)
2708 (defcustom widget-documentation-link-p 'intern-soft
2709 "Predicate used to test if a string is useful as a link.
2710 The value should be a function. The function will be called one
2711 argument, a string, and should return non-nil if there should be a
2712 link for that string."
2713 :type 'function
2714 :options '(widget-documentation-link-p)
2715 :group 'widget-documentation)
2717 (defcustom widget-documentation-link-type 'documentation-link
2718 "Widget type used for links in documentation strings."
2719 :type 'symbol
2720 :group 'widget-documentation)
2722 (defun widget-documentation-link-add (widget from to)
2723 (widget-specify-doc widget from to)
2724 (when widget-documentation-links
2725 (let ((regexp widget-documentation-link-regexp)
2726 (buttons (widget-get widget :buttons))
2727 (widget-mouse-face (default-value 'widget-mouse-face))
2728 (widget-button-face widget-documentation-face)
2729 (widget-button-pressed-face widget-documentation-face))
2730 (save-excursion
2731 (goto-char from)
2732 (while (re-search-forward regexp to t)
2733 (let ((name (match-string 1))
2734 (begin (match-beginning 1))
2735 (end (match-end 1)))
2736 (when (funcall widget-documentation-link-p name)
2737 (push (widget-convert-button widget-documentation-link-type
2738 begin end :value name)
2739 buttons)))))
2740 (widget-put widget :buttons buttons)))
2741 (let ((indent (widget-get widget :indent)))
2742 (when (and indent (not (zerop indent)))
2743 (save-excursion
2744 (save-restriction
2745 (narrow-to-region from to)
2746 (goto-char (point-min))
2747 (while (search-forward "\n" nil t)
2748 (insert-char ?\ indent)))))))
2750 ;;; The `documentation-string' Widget.
2752 (define-widget 'documentation-string 'item
2753 "A documentation string."
2754 :format "%v"
2755 :action 'widget-documentation-string-action
2756 :value-delete 'widget-children-value-delete
2757 :value-create 'widget-documentation-string-value-create)
2759 (defun widget-documentation-string-value-create (widget)
2760 ;; Insert documentation string.
2761 (let ((doc (widget-value widget))
2762 (indent (widget-get widget :indent))
2763 (shown (widget-get (widget-get widget :parent) :documentation-shown))
2764 (start (point)))
2765 (if (string-match "\n" doc)
2766 (let ((before (substring doc 0 (match-beginning 0)))
2767 (after (substring doc (match-beginning 0)))
2768 button)
2769 (insert before ?\ )
2770 (widget-documentation-link-add widget start (point))
2771 (setq button
2772 (widget-create-child-and-convert
2773 widget 'visibility
2774 :help-echo "Show or hide rest of the documentation."
2775 :off "More"
2776 :always-active t
2777 :action 'widget-parent-action
2778 shown))
2779 (when shown
2780 (setq start (point))
2781 (when (and indent (not (zerop indent)))
2782 (insert-char ?\ indent))
2783 (insert after)
2784 (widget-documentation-link-add widget start (point)))
2785 (widget-put widget :buttons (list button)))
2786 (insert doc)
2787 (widget-documentation-link-add widget start (point))))
2788 (insert ?\n))
2790 (defun widget-documentation-string-action (widget &rest ignore)
2791 ;; Toggle documentation.
2792 (let ((parent (widget-get widget :parent)))
2793 (widget-put parent :documentation-shown
2794 (not (widget-get parent :documentation-shown))))
2795 ;; Redraw.
2796 (widget-value-set widget (widget-value widget)))
2798 ;;; The Sexp Widgets.
2800 (define-widget 'const 'item
2801 "An immutable sexp."
2802 :prompt-value 'widget-const-prompt-value
2803 :format "%t\n%d")
2805 (defun widget-const-prompt-value (widget prompt value unbound)
2806 ;; Return the value of the const.
2807 (widget-value widget))
2809 (define-widget 'function-item 'const
2810 "An immutable function name."
2811 :format "%v\n%h"
2812 :documentation-property (lambda (symbol)
2813 (condition-case nil
2814 (documentation symbol t)
2815 (error nil))))
2817 (define-widget 'variable-item 'const
2818 "An immutable variable name."
2819 :format "%v\n%h"
2820 :documentation-property 'variable-documentation)
2822 (define-widget 'other 'sexp
2823 "Matches any value, but doesn't let the user edit the value.
2824 This is useful as last item in a `choice' widget.
2825 You should use this widget type with a default value,
2826 as in (other DEFAULT) or (other :tag \"NAME\" DEFAULT).
2827 If the user selects this alternative, that specifies DEFAULT
2828 as the value."
2829 :tag "Other"
2830 :format "%t%n"
2831 :value 'other)
2833 (defvar widget-string-prompt-value-history nil
2834 "History of input to `widget-string-prompt-value'.")
2836 (define-widget 'string 'editable-field
2837 "A string"
2838 :tag "String"
2839 :format "%{%t%}: %v"
2840 :complete-function 'ispell-complete-word
2841 :prompt-history 'widget-string-prompt-value-history)
2843 (define-widget 'regexp 'string
2844 "A regular expression."
2845 :match 'widget-regexp-match
2846 :validate 'widget-regexp-validate
2847 ;; Doesn't work well with terminating newline.
2848 ;; :value-face 'widget-single-line-field-face
2849 :tag "Regexp")
2851 (defun widget-regexp-match (widget value)
2852 ;; Match valid regexps.
2853 (and (stringp value)
2854 (condition-case nil
2855 (prog1 t
2856 (string-match value ""))
2857 (error nil))))
2859 (defun widget-regexp-validate (widget)
2860 "Check that the value of WIDGET is a valid regexp."
2861 (condition-case data
2862 (prog1 nil
2863 (string-match (widget-value widget) ""))
2864 (error (widget-put widget :error (error-message-string data))
2865 widget)))
2867 (define-widget 'file 'string
2868 "A file widget.
2869 It will read a file name from the minibuffer when invoked."
2870 :complete-function 'widget-file-complete
2871 :prompt-value 'widget-file-prompt-value
2872 :format "%{%t%}: %v"
2873 ;; Doesn't work well with terminating newline.
2874 ;; :value-face 'widget-single-line-field-face
2875 :tag "File")
2877 (defun widget-file-complete ()
2878 "Perform completion on file name preceding point."
2879 (interactive)
2880 (let* ((end (point))
2881 (beg (save-excursion
2882 (skip-chars-backward "^ ")
2883 (point)))
2884 (pattern (buffer-substring beg end))
2885 (name-part (file-name-nondirectory pattern))
2886 (directory (file-name-directory pattern))
2887 (completion (file-name-completion name-part directory)))
2888 (cond ((eq completion t))
2889 ((null completion)
2890 (message "Can't find completion for \"%s\"" pattern)
2891 (ding))
2892 ((not (string= name-part completion))
2893 (delete-region beg end)
2894 (insert (expand-file-name completion directory)))
2896 (message "Making completion list...")
2897 (with-output-to-temp-buffer "*Completions*"
2898 (display-completion-list
2899 (sort (file-name-all-completions name-part directory)
2900 'string<)))
2901 (message "Making completion list...%s" "done")))))
2903 (defun widget-file-prompt-value (widget prompt value unbound)
2904 ;; Read file from minibuffer.
2905 (abbreviate-file-name
2906 (if unbound
2907 (read-file-name prompt)
2908 (let ((prompt2 (format "%s (default %s) " prompt value))
2909 (dir (file-name-directory value))
2910 (file (file-name-nondirectory value))
2911 (must-match (widget-get widget :must-match)))
2912 (read-file-name prompt2 dir nil must-match file)))))
2914 ;;;(defun widget-file-action (widget &optional event)
2915 ;;; ;; Read a file name from the minibuffer.
2916 ;;; (let* ((value (widget-value widget))
2917 ;;; (dir (file-name-directory value))
2918 ;;; (file (file-name-nondirectory value))
2919 ;;; (menu-tag (widget-apply widget :menu-tag-get))
2920 ;;; (must-match (widget-get widget :must-match))
2921 ;;; (answer (read-file-name (concat menu-tag ": (default `" value "') ")
2922 ;;; dir nil must-match file)))
2923 ;;; (widget-value-set widget (abbreviate-file-name answer))
2924 ;;; (widget-setup)
2925 ;;; (widget-apply widget :notify widget event)))
2927 ;; Fixme: use file-name-as-directory.
2928 (define-widget 'directory 'file
2929 "A directory widget.
2930 It will read a directory name from the minibuffer when invoked."
2931 :tag "Directory")
2933 (defvar widget-symbol-prompt-value-history nil
2934 "History of input to `widget-symbol-prompt-value'.")
2936 (define-widget 'symbol 'editable-field
2937 "A Lisp symbol."
2938 :value nil
2939 :tag "Symbol"
2940 :format "%{%t%}: %v"
2941 :match (lambda (widget value) (symbolp value))
2942 :complete-function 'lisp-complete-symbol
2943 :prompt-internal 'widget-symbol-prompt-internal
2944 :prompt-match 'symbolp
2945 :prompt-history 'widget-symbol-prompt-value-history
2946 :value-to-internal (lambda (widget value)
2947 (if (symbolp value)
2948 (symbol-name value)
2949 value))
2950 :value-to-external (lambda (widget value)
2951 (if (stringp value)
2952 (intern value)
2953 value)))
2955 (defun widget-symbol-prompt-internal (widget prompt initial history)
2956 ;; Read file from minibuffer.
2957 (let ((answer (completing-read prompt obarray
2958 (widget-get widget :prompt-match)
2959 nil initial history)))
2960 (if (and (stringp answer)
2961 (not (zerop (length answer))))
2962 answer
2963 (error "No value"))))
2965 (defvar widget-function-prompt-value-history nil
2966 "History of input to `widget-function-prompt-value'.")
2968 (define-widget 'function 'sexp
2969 "A Lisp function."
2970 :complete-function (lambda ()
2971 (interactive)
2972 (lisp-complete-symbol 'fboundp))
2973 :prompt-value 'widget-field-prompt-value
2974 :prompt-internal 'widget-symbol-prompt-internal
2975 :prompt-match 'fboundp
2976 :prompt-history 'widget-function-prompt-value-history
2977 :action 'widget-field-action
2978 :match-alternatives '(functionp)
2979 :validate (lambda (widget)
2980 (unless (functionp (widget-value widget))
2981 (widget-put widget :error (format "Invalid function: %S"
2982 (widget-value widget)))
2983 widget))
2984 :value 'ignore
2985 :tag "Function")
2987 (defvar widget-variable-prompt-value-history nil
2988 "History of input to `widget-variable-prompt-value'.")
2990 (define-widget 'variable 'symbol
2991 "A Lisp variable."
2992 :prompt-match 'boundp
2993 :prompt-history 'widget-variable-prompt-value-history
2994 :complete-function (lambda ()
2995 (interactive)
2996 (lisp-complete-symbol 'boundp))
2997 :tag "Variable")
2999 (defvar widget-coding-system-prompt-value-history nil
3000 "History of input to `widget-coding-system-prompt-value'.")
3002 (define-widget 'coding-system 'symbol
3003 "A MULE coding-system."
3004 :format "%{%t%}: %v"
3005 :tag "Coding system"
3006 :base-only nil
3007 :prompt-history 'widget-coding-system-prompt-value-history
3008 :prompt-value 'widget-coding-system-prompt-value
3009 :action 'widget-coding-system-action
3010 :complete-function (lambda ()
3011 (interactive)
3012 (lisp-complete-symbol 'coding-system-p))
3013 :validate (lambda (widget)
3014 (unless (coding-system-p (widget-value widget))
3015 (widget-put widget :error (format "Invalid coding system: %S"
3016 (widget-value widget)))
3017 widget))
3018 :value 'undecided
3019 :prompt-match 'coding-system-p)
3021 (defun widget-coding-system-prompt-value (widget prompt value unbound)
3022 "Read coding-system from minibuffer."
3023 (if (widget-get widget :base-only)
3024 (intern
3025 (completing-read (format "%s (default %s) " prompt value)
3026 (mapcar #'list (coding-system-list t)) nil nil nil
3027 coding-system-history))
3028 (read-coding-system (format "%s (default %s) " prompt value) value)))
3030 (defun widget-coding-system-action (widget &optional event)
3031 (let ((answer
3032 (widget-coding-system-prompt-value
3033 widget
3034 (widget-apply widget :menu-tag-get)
3035 (widget-value widget)
3036 t)))
3037 (widget-value-set widget answer)
3038 (widget-apply widget :notify widget event)
3039 (widget-setup)))
3041 (define-widget 'sexp 'editable-field
3042 "An arbitrary Lisp expression."
3043 :tag "Lisp expression"
3044 :format "%{%t%}: %v"
3045 :value nil
3046 :validate 'widget-sexp-validate
3047 :match (lambda (widget value) t)
3048 :value-to-internal 'widget-sexp-value-to-internal
3049 :value-to-external (lambda (widget value) (read value))
3050 :prompt-history 'widget-sexp-prompt-value-history
3051 :prompt-value 'widget-sexp-prompt-value)
3053 (defun widget-sexp-value-to-internal (widget value)
3054 ;; Use pp for printer representation.
3055 (let ((pp (if (symbolp value)
3056 (prin1-to-string value)
3057 (pp-to-string value))))
3058 (while (string-match "\n\\'" pp)
3059 (setq pp (substring pp 0 -1)))
3060 (if (or (string-match "\n\\'" pp)
3061 (> (length pp) 40))
3062 (concat "\n" pp)
3063 pp)))
3065 (defun widget-sexp-validate (widget)
3066 ;; Valid if we can read the string and there is no junk left after it.
3067 (with-temp-buffer
3068 (insert (widget-apply widget :value-get))
3069 (goto-char (point-min))
3070 (let (err)
3071 (condition-case data
3072 (progn
3073 ;; Avoid a confusing end-of-file error.
3074 (skip-syntax-forward "\\s-")
3075 (if (eobp)
3076 (setq err "Empty sexp -- use `nil'?")
3077 (unless (widget-apply widget :match (read (current-buffer)))
3078 (setq err (widget-get widget :type-error))))
3079 (if (and (not (eobp))
3080 (not err))
3081 (setq err (format "Junk at end of expression: %s"
3082 (buffer-substring (point)
3083 (point-max))))))
3084 (end-of-file ; Avoid confusing error message.
3085 (setq err "Unbalanced sexp"))
3086 (error (setq err (error-message-string data))))
3087 (if (not err)
3089 (widget-put widget :error err)
3090 widget))))
3092 (defvar widget-sexp-prompt-value-history nil
3093 "History of input to `widget-sexp-prompt-value'.")
3095 (defun widget-sexp-prompt-value (widget prompt value unbound)
3096 ;; Read an arbitrary sexp.
3097 (let ((found (read-string prompt
3098 (if unbound nil (cons (prin1-to-string value) 0))
3099 (widget-get widget :prompt-history))))
3100 (let ((answer (read-from-string found)))
3101 (unless (= (cdr answer) (length found))
3102 (error "Junk at end of expression: %s"
3103 (substring found (cdr answer))))
3104 (car answer))))
3106 (define-widget 'restricted-sexp 'sexp
3107 "A Lisp expression restricted to values that match.
3108 To use this type, you must define :match or :match-alternatives."
3109 :type-error "The specified value is not valid"
3110 :match 'widget-restricted-sexp-match
3111 :value-to-internal (lambda (widget value)
3112 (if (widget-apply widget :match value)
3113 (prin1-to-string value)
3114 value)))
3116 (defun widget-restricted-sexp-match (widget value)
3117 (let ((alternatives (widget-get widget :match-alternatives))
3118 matched)
3119 (while (and alternatives (not matched))
3120 (if (cond ((functionp (car alternatives))
3121 (funcall (car alternatives) value))
3122 ((and (consp (car alternatives))
3123 (eq (car (car alternatives)) 'quote))
3124 (eq value (nth 1 (car alternatives)))))
3125 (setq matched t))
3126 (setq alternatives (cdr alternatives)))
3127 matched))
3129 (define-widget 'integer 'restricted-sexp
3130 "An integer."
3131 :tag "Integer"
3132 :value 0
3133 :type-error "This field should contain an integer"
3134 :match-alternatives '(integerp))
3136 (define-widget 'number 'restricted-sexp
3137 "A floating point number."
3138 :tag "Number"
3139 :value 0.0
3140 :type-error "This field should contain a number"
3141 :match-alternatives '(numberp))
3143 (define-widget 'character 'editable-field
3144 "A character."
3145 :tag "Character"
3146 :value 0
3147 :size 1
3148 :format "%{%t%}: %v\n"
3149 :valid-regexp "\\`.\\'"
3150 :error "This field should contain a single character"
3151 :value-to-internal (lambda (widget value)
3152 (if (stringp value)
3153 value
3154 (char-to-string value)))
3155 :value-to-external (lambda (widget value)
3156 (if (stringp value)
3157 (aref value 0)
3158 value))
3159 :match (lambda (widget value)
3160 (char-valid-p value)))
3162 (define-widget 'list 'group
3163 "A Lisp list."
3164 :tag "List"
3165 :format "%{%t%}:\n%v")
3167 (define-widget 'vector 'group
3168 "A Lisp vector."
3169 :tag "Vector"
3170 :format "%{%t%}:\n%v"
3171 :match 'widget-vector-match
3172 :value-to-internal (lambda (widget value) (append value nil))
3173 :value-to-external (lambda (widget value) (apply 'vector value)))
3175 (defun widget-vector-match (widget value)
3176 (and (vectorp value)
3177 (widget-group-match widget
3178 (widget-apply widget :value-to-internal value))))
3180 (define-widget 'cons 'group
3181 "A cons-cell."
3182 :tag "Cons-cell"
3183 :format "%{%t%}:\n%v"
3184 :match 'widget-cons-match
3185 :value-to-internal (lambda (widget value)
3186 (list (car value) (cdr value)))
3187 :value-to-external (lambda (widget value)
3188 (cons (nth 0 value) (nth 1 value))))
3190 (defun widget-cons-match (widget value)
3191 (and (consp value)
3192 (widget-group-match widget
3193 (widget-apply widget :value-to-internal value))))
3195 ;;; The `plist' Widget.
3197 ;; Property lists.
3199 (define-widget 'plist 'list
3200 "A property list."
3201 :key-type '(symbol :tag "Key")
3202 :value-type '(sexp :tag "Value")
3203 :convert-widget 'widget-plist-convert-widget
3204 :tag "Plist")
3206 (defvar widget-plist-value-type) ;Dynamic variable
3208 (defun widget-plist-convert-widget (widget)
3209 ;; Handle `:options'.
3210 (let* ((options (widget-get widget :options))
3211 (widget-plist-value-type (widget-get widget :value-type))
3212 (other `(editable-list :inline t
3213 (group :inline t
3214 ,(widget-get widget :key-type)
3215 ,widget-plist-value-type)))
3216 (args (if options
3217 (list `(checklist :inline t
3218 :greedy t
3219 ,@(mapcar 'widget-plist-convert-option
3220 options))
3221 other)
3222 (list other))))
3223 (widget-put widget :args args)
3224 widget))
3226 (defun widget-plist-convert-option (option)
3227 ;; Convert a single plist option.
3228 (let (key-type value-type)
3229 (if (listp option)
3230 (let ((key (nth 0 option)))
3231 (setq value-type (nth 1 option))
3232 (if (listp key)
3233 (setq key-type key)
3234 (setq key-type `(const ,key))))
3235 (setq key-type `(const ,option)
3236 value-type widget-plist-value-type))
3237 `(group :format "Key: %v" :inline t ,key-type ,value-type)))
3240 ;;; The `alist' Widget.
3242 ;; Association lists.
3244 (define-widget 'alist 'list
3245 "An association list."
3246 :key-type '(sexp :tag "Key")
3247 :value-type '(sexp :tag "Value")
3248 :convert-widget 'widget-alist-convert-widget
3249 :tag "Alist")
3251 (defvar widget-alist-value-type) ;Dynamic variable
3253 (defun widget-alist-convert-widget (widget)
3254 ;; Handle `:options'.
3255 (let* ((options (widget-get widget :options))
3256 (widget-alist-value-type (widget-get widget :value-type))
3257 (other `(editable-list :inline t
3258 (cons :format "%v"
3259 ,(widget-get widget :key-type)
3260 ,widget-alist-value-type)))
3261 (args (if options
3262 (list `(checklist :inline t
3263 :greedy t
3264 ,@(mapcar 'widget-alist-convert-option
3265 options))
3266 other)
3267 (list other))))
3268 (widget-put widget :args args)
3269 widget))
3271 (defun widget-alist-convert-option (option)
3272 ;; Convert a single alist option.
3273 (let (key-type value-type)
3274 (if (listp option)
3275 (let ((key (nth 0 option)))
3276 (setq value-type (nth 1 option))
3277 (if (listp key)
3278 (setq key-type key)
3279 (setq key-type `(const ,key))))
3280 (setq key-type `(const ,option)
3281 value-type widget-alist-value-type))
3282 `(cons :format "Key: %v" ,key-type ,value-type)))
3284 (define-widget 'choice 'menu-choice
3285 "A union of several sexp types."
3286 :tag "Choice"
3287 :format "%{%t%}: %[Value Menu%] %v"
3288 :button-prefix 'widget-push-button-prefix
3289 :button-suffix 'widget-push-button-suffix
3290 :prompt-value 'widget-choice-prompt-value)
3292 (defun widget-choice-prompt-value (widget prompt value unbound)
3293 "Make a choice."
3294 (let ((args (widget-get widget :args))
3295 (completion-ignore-case (widget-get widget :case-fold))
3296 current choices old)
3297 ;; Find the first arg that matches VALUE.
3298 (let ((look args))
3299 (while look
3300 (if (widget-apply (car look) :match value)
3301 (setq old (car look)
3302 look nil)
3303 (setq look (cdr look)))))
3304 ;; Find new choice.
3305 (setq current
3306 (cond ((= (length args) 0)
3307 nil)
3308 ((= (length args) 1)
3309 (nth 0 args))
3310 ((and (= (length args) 2)
3311 (memq old args))
3312 (if (eq old (nth 0 args))
3313 (nth 1 args)
3314 (nth 0 args)))
3316 (while args
3317 (setq current (car args)
3318 args (cdr args))
3319 (setq choices
3320 (cons (cons (widget-apply current :menu-tag-get)
3321 current)
3322 choices)))
3323 (let ((val (completing-read prompt choices nil t)))
3324 (if (stringp val)
3325 (let ((try (try-completion val choices)))
3326 (when (stringp try)
3327 (setq val try))
3328 (cdr (assoc val choices)))
3329 nil)))))
3330 (if current
3331 (widget-prompt-value current prompt nil t)
3332 value)))
3334 (define-widget 'radio 'radio-button-choice
3335 "A union of several sexp types."
3336 :tag "Choice"
3337 :format "%{%t%}:\n%v"
3338 :prompt-value 'widget-choice-prompt-value)
3340 (define-widget 'repeat 'editable-list
3341 "A variable length homogeneous list."
3342 :tag "Repeat"
3343 :format "%{%t%}:\n%v%i\n")
3345 (define-widget 'set 'checklist
3346 "A list of members from a fixed set."
3347 :tag "Set"
3348 :format "%{%t%}:\n%v")
3350 (define-widget 'boolean 'toggle
3351 "To be nil or non-nil, that is the question."
3352 :tag "Boolean"
3353 :prompt-value 'widget-boolean-prompt-value
3354 :button-prefix 'widget-push-button-prefix
3355 :button-suffix 'widget-push-button-suffix
3356 :format "%{%t%}: %[Toggle%] %v\n"
3357 :on "on (non-nil)"
3358 :off "off (nil)")
3360 (defun widget-boolean-prompt-value (widget prompt value unbound)
3361 ;; Toggle a boolean.
3362 (y-or-n-p prompt))
3364 ;;; The `color' Widget.
3366 ;; Fixme: match
3367 (define-widget 'color 'editable-field
3368 "Choose a color name (with sample)."
3369 :format "%t: %v (%{sample%})\n"
3370 :size 10
3371 :tag "Color"
3372 :value "black"
3373 :complete 'widget-color-complete
3374 :sample-face-get 'widget-color-sample-face-get
3375 :notify 'widget-color-notify
3376 :action 'widget-color-action)
3378 (defun widget-color-complete (widget)
3379 "Complete the color in WIDGET."
3380 (require 'facemenu) ; for facemenu-color-alist
3381 (let* ((prefix (buffer-substring-no-properties (widget-field-start widget)
3382 (point)))
3383 (list (or facemenu-color-alist
3384 (mapcar 'list (defined-colors))))
3385 (completion (try-completion prefix list)))
3386 (cond ((eq completion t)
3387 (message "Exact match."))
3388 ((null completion)
3389 (error "Can't find completion for \"%s\"" prefix))
3390 ((not (string-equal prefix completion))
3391 (insert-and-inherit (substring completion (length prefix))))
3393 (message "Making completion list...")
3394 (with-output-to-temp-buffer "*Completions*"
3395 (display-completion-list (all-completions prefix list nil)))
3396 (message "Making completion list...done")))))
3398 (defun widget-color-sample-face-get (widget)
3399 (let* ((value (condition-case nil
3400 (widget-value widget)
3401 (error (widget-get widget :value)))))
3402 (if (color-defined-p value)
3403 (let ((symbol (intern (concat "fg:" value))))
3404 (condition-case nil
3405 (facemenu-get-face symbol)
3406 (error 'default)))
3407 'default)))
3409 (defun widget-color-action (widget &optional event)
3410 "Prompt for a color."
3411 (let* ((tag (widget-apply widget :menu-tag-get))
3412 (prompt (concat tag ": "))
3413 (value (widget-value widget))
3414 (start (widget-field-start widget))
3415 (pos (cond ((< (point) start)
3417 ((> (point) (+ start (length value)))
3418 (length value))
3420 (- (point) start))))
3421 (answer (facemenu-read-color prompt)))
3422 (unless (zerop (length answer))
3423 (widget-value-set widget answer)
3424 (widget-setup)
3425 (widget-apply widget :notify widget event))))
3427 (defun widget-color-notify (widget child &optional event)
3428 "Update the sample, and notofy the parent."
3429 (overlay-put (widget-get widget :sample-overlay)
3430 'face (widget-apply widget :sample-face-get))
3431 (widget-default-notify widget child event))
3433 ;;; The Help Echo
3435 (defun widget-echo-help (pos)
3436 "Display help-echo text for widget at POS."
3437 (let* ((widget (widget-at pos))
3438 (help-echo (and widget (widget-get widget :help-echo))))
3439 (if (functionp help-echo)
3440 (setq help-echo (funcall help-echo widget)))
3441 (if (stringp help-echo)
3442 (message "%s" help-echo))))
3444 ;;; The End:
3446 (provide 'wid-edit)
3448 ;;; wid-edit.el ends here