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