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