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