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