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