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