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