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