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