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