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