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