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