(vc-backend-diff): get rid of the autoloaded advice.
[emacs.git] / lisp / wid-edit.el
blob4ac7da42efe654f695789075c3968f97f1daf28e
1 ;;; wid-edit.el --- Functions for creating and using widgets.
2 ;;
3 ;; Copyright (C) 1996, 1997, 1999 Free Software Foundation, Inc.
4 ;;
5 ;; Author: Per Abrahamsen <abraham@dina.kvl.dk>
6 ;; Maintainer: FSF
7 ;; Keywords: extensions
8 ;; Version: 1.9951
9 ;; X-URL: http://www.dina.kvl.dk/~abraham/custom/ (probably obsolete)
11 ;; This file is part of GNU Emacs.
13 ;; GNU Emacs is free software; you can redistribute it and/or modify
14 ;; it under the terms of the GNU General Public License as published by
15 ;; the Free Software Foundation; either version 2, or (at your option)
16 ;; any later version.
18 ;; GNU Emacs is distributed in the hope that it will be useful,
19 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
20 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 ;; GNU General Public License for more details.
23 ;; You should have received a copy of the GNU General Public License
24 ;; along with GNU Emacs; see the file COPYING. If not, write to the
25 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
26 ;; Boston, MA 02111-1307, USA.
28 ;;; Commentary:
30 ;; See `widget.el'.
32 ;;; Code:
34 (require 'widget)
35 (eval-when-compile (require 'cl))
37 ;;; Compatibility.
39 (defun widget-event-point (event)
40 "Character position of the end of event if that exists, or nil."
41 (posn-point (event-end event)))
43 (defalias 'widget-read-event 'read-event)
45 (eval-and-compile
46 (autoload 'pp-to-string "pp")
47 (autoload 'Info-goto-node "info")
48 (autoload 'finder-commentary "finder" nil t)
50 (unless (fboundp 'button-release-event-p)
51 ;; XEmacs function missing from Emacs.
52 (defun button-release-event-p (event)
53 "Non-nil if EVENT is a mouse-button-release event object."
54 (and (eventp event)
55 (memq (event-basic-type event) '(mouse-1 mouse-2 mouse-3))
56 (or (memq 'click (event-modifiers event))
57 (memq 'drag (event-modifiers event)))))))
59 ;;; Customization.
61 (defgroup widgets nil
62 "Customization support for the Widget Library."
63 :link '(custom-manual "(widget)Top")
64 :link '(url-link :tag "Development Page"
65 "http://www.dina.kvl.dk/~abraham/custom/")
66 :link '(emacs-library-link :tag "Lisp File" "widget.el")
67 :prefix "widget-"
68 :group 'extensions
69 :group 'hypermedia)
71 (defgroup widget-documentation nil
72 "Options controling the display of documentation strings."
73 :group 'widgets)
75 (defgroup widget-faces nil
76 "Faces used by the widget library."
77 :group 'widgets
78 :group 'faces)
80 (defvar widget-documentation-face 'widget-documentation-face
81 "Face used for documentation strings in widgets.
82 This exists as a variable so it can be set locally in certain buffers.")
84 (defface widget-documentation-face '((((class color)
85 (background dark))
86 (:foreground "lime green"))
87 (((class color)
88 (background light))
89 (:foreground "dark green"))
90 (t nil))
91 "Face used for documentation text."
92 :group 'widget-documentation
93 :group 'widget-faces)
95 (defvar widget-button-face 'widget-button-face
96 "Face used for buttons in widgets.
97 This exists as a variable so it can be set locally in certain buffers.")
99 (defface widget-button-face '((t (:bold t)))
100 "Face used for widget buttons."
101 :group 'widget-faces)
103 (defcustom widget-mouse-face 'highlight
104 "Face used for widget buttons when the mouse is above them."
105 :type 'face
106 :group 'widget-faces)
108 (defface widget-field-face '((((class grayscale color)
109 (background light))
110 (:background "gray85"))
111 (((class grayscale color)
112 (background dark))
113 (:background "dim gray"))
115 (:italic t)))
116 "Face used for editable fields."
117 :group 'widget-faces)
119 (defface widget-single-line-field-face '((((class grayscale color)
120 (background light))
121 (:background "gray85"))
122 (((class grayscale color)
123 (background dark))
124 (:background "dim gray"))
126 (:italic t)))
127 "Face used for editable fields spanning only a single line."
128 :group 'widget-faces)
130 ;;; This causes display-table to be loaded, and not usefully.
131 ;;;(defvar widget-single-line-display-table
132 ;;; (let ((table (make-display-table)))
133 ;;; (aset table 9 "^I")
134 ;;; (aset table 10 "^J")
135 ;;; table)
136 ;;; "Display table used for single-line editable fields.")
138 ;;;(when (fboundp 'set-face-display-table)
139 ;;; (set-face-display-table 'widget-single-line-field-face
140 ;;; widget-single-line-display-table))
142 ;;; Utility functions.
144 ;; These are not really widget specific.
146 (defun widget-princ-to-string (object)
147 ;; Return string representation of OBJECT, any Lisp object.
148 ;; No quoting characters are used; no delimiters are printed around
149 ;; the contents of strings.
150 (save-excursion
151 (set-buffer (get-buffer-create " *widget-tmp*"))
152 (erase-buffer)
153 (let ((standard-output (current-buffer)))
154 (princ object))
155 (buffer-string)))
157 (defun widget-clear-undo ()
158 "Clear all undo information."
159 (buffer-disable-undo (current-buffer))
160 (buffer-enable-undo))
162 (defcustom widget-menu-max-size 40
163 "Largest number of items allowed in a popup-menu.
164 Larger menus are read through the minibuffer."
165 :group 'widgets
166 :type 'integer)
168 (defcustom widget-menu-max-shortcuts 40
169 "Largest number of items for which it works to choose one with a character.
170 For a larger number of items, the minibuffer is used."
171 :group 'widgets
172 :type 'integer)
174 (defcustom widget-menu-minibuffer-flag nil
175 "*Control how to ask for a choice from the keyboard.
176 Non-nil means use the minibuffer;
177 nil means read a single character."
178 :group 'widgets
179 :type 'boolean)
181 (defun widget-choose (title items &optional event)
182 "Choose an item from a list.
184 First argument TITLE is the name of the list.
185 Second argument ITEMS is an list whose members are either
186 (NAME . VALUE), to indicate selectable items, or just strings to
187 indicate unselectable items.
188 Optional third argument EVENT is an input event.
190 The user is asked to choose between each NAME from the items alist,
191 and the VALUE of the chosen element will be returned. If EVENT is a
192 mouse event, and the number of elements in items is less than
193 `widget-menu-max-size', a popup menu will be used, otherwise the
194 minibuffer."
195 (cond ((and (< (length items) widget-menu-max-size)
196 event (fboundp 'x-popup-menu) window-system)
197 ;; We are in Emacs-19, pressed by the mouse
198 (x-popup-menu event
199 (list title (cons "" items))))
200 ((or widget-menu-minibuffer-flag
201 (> (length items) widget-menu-max-shortcuts))
202 ;; Read the choice of name from the minibuffer.
203 (setq items (widget-remove-if 'stringp items))
204 (let ((val (completing-read (concat title ": ") items nil t)))
205 (if (stringp val)
206 (let ((try (try-completion val items)))
207 (when (stringp try)
208 (setq val try))
209 (cdr (assoc val items)))
210 nil)))
212 ;; Construct a menu of the choices
213 ;; and then use it for prompting for a single character.
214 (let* ((overriding-terminal-local-map
215 (make-sparse-keymap))
216 map choice (next-digit ?0)
217 some-choice-enabled
218 value)
219 ;; Define SPC as a prefix char to get to this menu.
220 (define-key overriding-terminal-local-map " "
221 (setq map (make-sparse-keymap title)))
222 (save-excursion
223 (set-buffer (get-buffer-create " widget-choose"))
224 (erase-buffer)
225 (insert "Available choices:\n\n")
226 (while items
227 (setq choice (car items) items (cdr items))
228 (if (consp choice)
229 (let* ((name (car choice))
230 (function (cdr choice)))
231 (insert (format "%c = %s\n" next-digit name))
232 (define-key map (vector next-digit) function)
233 (setq some-choice-enabled t)))
234 ;; Allocate digits to disabled alternatives
235 ;; so that the digit of a given alternative never varies.
236 (setq next-digit (1+ next-digit)))
237 (insert "\nC-g = Quit"))
238 (or some-choice-enabled
239 (error "None of the choices is currently meaningful"))
240 (define-key map [?\C-g] 'keyboard-quit)
241 (define-key map [t] 'keyboard-quit)
242 (define-key map [?\M-\C-v] 'scroll-other-window)
243 (define-key map [?\M--] 'negative-argument)
244 (setcdr map (nreverse (cdr map)))
245 ;; Read a char with the menu, and return the result
246 ;; that corresponds to it.
247 (save-window-excursion
248 (let ((buf (get-buffer " widget-choose")))
249 (display-buffer buf)
250 (let ((cursor-in-echo-area t)
251 keys
252 (char 0)
253 (arg 1))
254 (while (not (or (and (>= char ?0) (< char next-digit))
255 (eq value 'keyboard-quit)))
256 ;; Unread a SPC to lead to our new menu.
257 (setq unread-command-events (cons ?\ unread-command-events))
258 (setq keys (read-key-sequence title))
259 (setq value (lookup-key overriding-terminal-local-map keys t)
260 char (string-to-char (substring keys 1)))
261 (cond ((eq value 'scroll-other-window)
262 (let ((minibuffer-scroll-window (get-buffer-window buf)))
263 (if (> 0 arg)
264 (scroll-other-window-down (window-height minibuffer-scroll-window))
265 (scroll-other-window))
266 (setq arg 1)))
267 ((eq value 'negative-argument)
268 (setq arg -1))
270 (setq arg 1)))))))
271 (when (eq value 'keyboard-quit)
272 (error "Canceled"))
273 value))))
275 (defun widget-remove-if (predictate list)
276 (let (result (tail list))
277 (while tail
278 (or (funcall predictate (car tail))
279 (setq result (cons (car tail) result)))
280 (setq tail (cdr tail)))
281 (nreverse result)))
283 ;;; Widget text specifications.
285 ;; These functions are for specifying text properties.
287 (defcustom widget-field-add-space
288 (or (< emacs-major-version 20)
289 (and (eq emacs-major-version 20)
290 (< emacs-minor-version 3))
291 (not (string-match "XEmacs" emacs-version)))
292 "Non-nil means add extra space at the end of editable text fields.
294 This is needed on all versions of Emacs, and on XEmacs before 20.3.
295 If you don't add the space, it will become impossible to edit a zero
296 size field."
297 :type 'boolean
298 :group 'widgets)
300 (defcustom widget-field-use-before-change
301 (and (or (> emacs-minor-version 34)
302 (> emacs-major-version 19))
303 (not (string-match "XEmacs" emacs-version)))
304 "Non-nil means use `before-change-functions' to track editable fields.
305 This enables the use of undo, but doesn't work on Emacs 19.34 and earlier.
306 Using before hooks also means that the :notify function can't know the
307 new value."
308 :type 'boolean
309 :group 'widgets)
311 (defun widget-specify-field (widget from to)
312 "Specify editable button for WIDGET between FROM and TO."
313 ;; Terminating space is not part of the field, but necessary in
314 ;; order for local-map to work. Remove next sexp if local-map works
315 ;; at the end of the overlay.
316 (save-excursion
317 (goto-char to)
318 (cond ((null (widget-get widget :size))
319 (forward-char 1))
320 (widget-field-add-space
321 (insert-and-inherit " ")))
322 (setq to (point)))
323 (let ((map (widget-get widget :keymap))
324 (face (or (widget-get widget :value-face) 'widget-field-face))
325 (help-echo (widget-get widget :help-echo))
326 (overlay (make-overlay from to nil
327 nil (or (not widget-field-add-space)
328 (widget-get widget :size)))))
329 (unless (or (stringp help-echo) (null help-echo))
330 (setq help-echo 'widget-mouse-help))
331 (widget-put widget :field-overlay overlay)
332 ;;(overlay-put overlay 'detachable nil)
333 (overlay-put overlay 'field widget)
334 (overlay-put overlay 'local-map map)
335 ;;(overlay-put overlay 'keymap map)
336 (overlay-put overlay 'face face)
337 ;;(overlay-put overlay 'balloon-help help-echo)
338 (overlay-put overlay 'help-echo help-echo))
339 (widget-specify-secret widget))
341 (defun widget-specify-secret (field)
342 "Replace text in FIELD with value of `:secret', if non-nil."
343 (let ((secret (widget-get field :secret))
344 (size (widget-get field :size)))
345 (when secret
346 (let ((begin (widget-field-start field))
347 (end (widget-field-end field)))
348 (when size
349 (while (and (> end begin)
350 (eq (char-after (1- end)) ?\ ))
351 (setq end (1- end))))
352 (while (< begin end)
353 (let ((old (char-after begin)))
354 (unless (eq old secret)
355 (subst-char-in-region begin (1+ begin) old secret)
356 (put-text-property begin (1+ begin) 'secret old))
357 (setq begin (1+ begin))))))))
359 (defun widget-specify-button (widget from to)
360 "Specify button for WIDGET between FROM and TO."
361 (let ((face (widget-apply widget :button-face-get))
362 (help-echo (widget-get widget :help-echo))
363 (overlay (make-overlay from to nil t nil)))
364 (widget-put widget :button-overlay overlay)
365 (unless (or (null help-echo) (stringp help-echo))
366 (setq help-echo 'widget-mouse-help))
367 (overlay-put overlay 'button widget)
368 (overlay-put overlay 'mouse-face widget-mouse-face)
369 ;;(overlay-put overlay 'balloon-help help-echo)
370 (overlay-put overlay 'help-echo help-echo)
371 (overlay-put overlay 'face face)))
373 (defun widget-mouse-help (extent)
374 "Find mouse help string for button in extent."
375 (let* ((widget (widget-at (extent-start-position extent)))
376 (help-echo (and widget (widget-get widget :help-echo))))
377 (cond ((stringp help-echo)
378 help-echo)
379 ((and (symbolp help-echo) (fboundp help-echo)
380 (stringp (setq help-echo (funcall help-echo widget))))
381 help-echo)
383 (format "(widget %S :help-echo %S)" widget help-echo)))))
385 (defun widget-specify-sample (widget from to)
386 ;; Specify sample for WIDGET between FROM and TO.
387 (let ((face (widget-apply widget :sample-face-get))
388 (overlay (make-overlay from to nil t nil)))
389 (overlay-put overlay 'face face)
390 (widget-put widget :sample-overlay overlay)))
392 (defun widget-specify-doc (widget from to)
393 ;; Specify documentation for WIDGET between FROM and TO.
394 (let ((overlay (make-overlay from to nil t nil)))
395 (overlay-put overlay 'widget-doc widget)
396 (overlay-put overlay 'face widget-documentation-face)
397 (widget-put widget :doc-overlay overlay)))
399 (defmacro widget-specify-insert (&rest form)
400 ;; Execute FORM without inheriting any text properties.
402 (save-restriction
403 (let ((inhibit-read-only t)
404 result
405 before-change-functions
406 after-change-functions)
407 (insert "<>")
408 (narrow-to-region (- (point) 2) (point))
409 (goto-char (1+ (point-min)))
410 (setq result (progn (,@ form)))
411 (delete-region (point-min) (1+ (point-min)))
412 (delete-region (1- (point-max)) (point-max))
413 (goto-char (point-max))
414 result))))
416 (defface widget-inactive-face '((((class grayscale color)
417 (background dark))
418 (:foreground "light gray"))
419 (((class grayscale color)
420 (background light))
421 (:foreground "dim gray"))
423 (:italic t)))
424 "Face used for inactive widgets."
425 :group 'widget-faces)
427 (defun widget-specify-inactive (widget from to)
428 "Make WIDGET inactive for user modifications."
429 (unless (widget-get widget :inactive)
430 (let ((overlay (make-overlay from to nil t nil)))
431 (overlay-put overlay 'face 'widget-inactive-face)
432 ;; This is disabled, as it makes the mouse cursor change shape.
433 ;; (overlay-put overlay 'mouse-face 'widget-inactive-face)
434 (overlay-put overlay 'evaporate t)
435 (overlay-put overlay 'priority 100)
436 (overlay-put overlay 'modification-hooks '(widget-overlay-inactive))
437 (widget-put widget :inactive overlay))))
439 (defun widget-overlay-inactive (&rest junk)
440 "Ignoring the arguments, signal an error."
441 (unless inhibit-read-only
442 (error "The widget here is not active")))
445 (defun widget-specify-active (widget)
446 "Make WIDGET active for user modifications."
447 (let ((inactive (widget-get widget :inactive)))
448 (when inactive
449 (delete-overlay inactive)
450 (widget-put widget :inactive nil))))
452 ;;; Widget Properties.
454 (defsubst widget-type (widget)
455 "Return the type of WIDGET, a symbol."
456 (car widget))
458 (defun widget-get-indirect (widget property)
459 "In WIDGET, get the value of PROPERTY.
460 If the value is a symbol, return its binding.
461 Otherwise, just return the value."
462 (let ((value (widget-get widget property)))
463 (if (symbolp value)
464 (symbol-value value)
465 value)))
467 (defun widget-member (widget property)
468 "Non-nil iff there is a definition in WIDGET for PROPERTY."
469 (cond ((widget-plist-member (cdr widget) property)
471 ((car widget)
472 (widget-member (get (car widget) 'widget-type) property))
473 (t nil)))
475 (defun widget-value (widget)
476 "Extract the current value of WIDGET."
477 (widget-apply widget
478 :value-to-external (widget-apply widget :value-get)))
480 (defun widget-value-set (widget value)
481 "Set the current value of WIDGET to VALUE."
482 (widget-apply widget
483 :value-set (widget-apply widget
484 :value-to-internal value)))
486 (defun widget-default-get (widget)
487 "Extract the default value of WIDGET."
488 (or (widget-get widget :value)
489 (widget-apply widget :default-get)))
491 (defun widget-match-inline (widget vals)
492 "In WIDGET, match the start of VALS."
493 (cond ((widget-get widget :inline)
494 (widget-apply widget :match-inline vals))
495 ((and vals
496 (widget-apply widget :match (car vals)))
497 (cons (list (car vals)) (cdr vals)))
498 (t nil)))
500 (defun widget-apply-action (widget &optional event)
501 "Apply :action in WIDGET in response to EVENT."
502 (if (widget-apply widget :active)
503 (widget-apply widget :action event)
504 (error "Attempt to perform action on inactive widget")))
506 ;;; Helper functions.
508 ;; These are widget specific.
510 ;;;###autoload
511 (defun widget-prompt-value (widget prompt &optional value unbound)
512 "Prompt for a value matching WIDGET, using PROMPT.
513 The current value is assumed to be VALUE, unless UNBOUND is non-nil."
514 (unless (listp widget)
515 (setq widget (list widget)))
516 (setq prompt (format "[%s] %s" (widget-type widget) prompt))
517 (setq widget (widget-convert widget))
518 (let ((answer (widget-apply widget :prompt-value prompt value unbound)))
519 (unless (widget-apply widget :match answer)
520 (error "Value does not match %S type." (car widget)))
521 answer))
523 (defun widget-get-sibling (widget)
524 "Get the item WIDGET is assumed to toggle.
525 This is only meaningful for radio buttons or checkboxes in a list."
526 (let* ((parent (widget-get widget :parent))
527 (children (widget-get parent :children))
528 child)
529 (catch 'child
530 (while children
531 (setq child (car children)
532 children (cdr children))
533 (when (eq (widget-get child :button) widget)
534 (throw 'child child)))
535 nil)))
537 (defun widget-map-buttons (function &optional buffer maparg)
538 "Map FUNCTION over the buttons in BUFFER.
539 FUNCTION is called with the arguments WIDGET and MAPARG.
541 If FUNCTION returns non-nil, the walk is cancelled.
543 The arguments MAPARG, and BUFFER default to nil and (current-buffer),
544 respectively."
545 (let ((cur (point-min))
546 (widget nil)
547 (parent nil)
548 (overlays (if buffer
549 (save-excursion (set-buffer buffer) (overlay-lists))
550 (overlay-lists))))
551 (setq overlays (append (car overlays) (cdr overlays)))
552 (while (setq cur (pop overlays))
553 (setq widget (overlay-get cur 'button))
554 (if (and widget (funcall function widget maparg))
555 (setq overlays nil)))))
557 ;;; Glyphs.
559 (defcustom widget-glyph-directory (concat data-directory "custom/")
560 "Where widget glyphs are located.
561 If this variable is nil, widget will try to locate the directory
562 automatically."
563 :group 'widgets
564 :type 'directory)
566 (defcustom widget-glyph-enable t
567 "If non nil, use glyphs in images when available."
568 :group 'widgets
569 :type 'boolean)
571 (defcustom widget-image-conversion
572 '((xpm ".xpm") (gif ".gif") (png ".png") (jpeg ".jpg" ".jpeg")
573 (xbm ".xbm"))
574 "Conversion alist from image formats to file name suffixes."
575 :group 'widgets
576 :type '(repeat (cons :format "%v"
577 (symbol :tag "Image Format" unknown)
578 (repeat :tag "Suffixes"
579 (string :format "%v")))))
581 (defun widget-glyph-find (image tag)
582 "Create a glyph corresponding to IMAGE with string TAG as fallback.
583 IMAGE should either already be a glyph, or be a file name sans
584 extension (xpm, xbm, gif, jpg, or png) located in
585 `widget-glyph-directory'."
586 (cond ((not (and image
587 (string-match "XEmacs" emacs-version)
588 widget-glyph-enable
589 (fboundp 'make-glyph)
590 (fboundp 'locate-file)
591 image))
592 ;; We don't want or can't use glyphs.
593 nil)
594 ((and (fboundp 'glyphp)
595 (glyphp image))
596 ;; Already a glyph. Use it.
597 image)
598 ((stringp image)
599 ;; A string. Look it up in relevant directories.
600 (let* ((dirlist (list (or widget-glyph-directory
601 (concat data-directory
602 "custom/"))
603 data-directory))
604 (formats widget-image-conversion)
605 file)
606 (while (and formats (not file))
607 (when (valid-image-instantiator-format-p (car (car formats)))
608 (setq file (locate-file image dirlist
609 (mapconcat 'identity
610 (cdr (car formats))
611 ":"))))
612 (unless file
613 (setq formats (cdr formats))))
614 (and file
615 ;; We create a glyph with the file as the default image
616 ;; instantiator, and the TAG fallback
617 (make-glyph (list (vector (car (car formats)) ':file file)
618 (vector 'string ':data tag))))))
619 ((valid-instantiator-p image 'image)
620 ;; A valid image instantiator (e.g. [gif :file "somefile"] etc.)
621 (make-glyph (list image
622 (vector 'string ':data tag))))
623 ((consp image)
624 ;; This could be virtually anything. Let `make-glyph' sort it out.
625 (make-glyph image))
627 ;; Oh well.
628 nil)))
630 (defun widget-glyph-insert (widget tag image &optional down inactive)
631 "In WIDGET, insert the text TAG or, if supported, IMAGE.
632 IMAGE should either be a glyph, an image instantiator, or an image file
633 name sans extension (xpm, xbm, gif, jpg, or png) located in
634 `widget-glyph-directory'.
636 Optional arguments DOWN and INACTIVE is used instead of IMAGE when the
637 glyph is pressed or inactive, respectively.
639 WARNING: If you call this with a glyph, and you want the user to be
640 able to invoke the glyph, make sure it is unique. If you use the
641 same glyph for multiple widgets, invoking any of the glyphs will
642 cause the last created widget to be invoked.
644 Instead of an instantiator, you can also use a list of instantiators,
645 or whatever `make-glyph' will accept. However, in that case you must
646 provide the fallback TAG as a part of the instantiator yourself."
647 (let ((glyph (widget-glyph-find image tag)))
648 (if glyph
649 (widget-glyph-insert-glyph widget
650 glyph
651 (widget-glyph-find down tag)
652 (widget-glyph-find inactive tag))
653 (insert tag))))
655 (defun widget-glyph-insert-glyph (widget glyph &optional down inactive)
656 "In WIDGET, insert GLYPH.
657 If optional arguments DOWN and INACTIVE are given, they should be
658 glyphs used when the widget is pushed and inactive, respectively."
659 (when widget
660 (set-glyph-property glyph 'widget widget)
661 (when down
662 (set-glyph-property down 'widget widget))
663 (when inactive
664 (set-glyph-property inactive 'widget widget)))
665 (insert "*")
666 (let ((ext (make-extent (point) (1- (point))))
667 (help-echo (and widget (widget-get widget :help-echo))))
668 (set-extent-property ext 'invisible t)
669 (set-extent-property ext 'start-open t)
670 (set-extent-property ext 'end-open t)
671 (set-extent-end-glyph ext glyph)
672 (when help-echo
673 (set-extent-property ext 'balloon-help help-echo)
674 (set-extent-property ext 'help-echo help-echo)))
675 (when widget
676 (widget-put widget :glyph-up glyph)
677 (when down (widget-put widget :glyph-down down))
678 (when inactive (widget-put widget :glyph-inactive inactive))))
680 ;;; Buttons.
682 (defgroup widget-button nil
683 "The look of various kinds of buttons."
684 :group 'widgets)
686 (defcustom widget-button-prefix ""
687 "String used as prefix for buttons."
688 :type 'string
689 :group 'widget-button)
691 (defcustom widget-button-suffix ""
692 "String used as suffix for buttons."
693 :type 'string
694 :group 'widget-button)
696 ;;; Creating Widgets.
698 ;;;###autoload
699 (defun widget-create (type &rest args)
700 "Create widget of TYPE.
701 The optional ARGS are additional keyword arguments."
702 (let ((widget (apply 'widget-convert type args)))
703 (widget-apply widget :create)
704 widget))
706 (defun widget-create-child-and-convert (parent type &rest args)
707 "As part of the widget PARENT, create a child widget TYPE.
708 The child is converted, using the keyword arguments ARGS."
709 (let ((widget (apply 'widget-convert type args)))
710 (widget-put widget :parent parent)
711 (unless (widget-get widget :indent)
712 (widget-put widget :indent (+ (or (widget-get parent :indent) 0)
713 (or (widget-get widget :extra-offset) 0)
714 (widget-get parent :offset))))
715 (widget-apply widget :create)
716 widget))
718 (defun widget-create-child (parent type)
719 "Create widget of TYPE."
720 (let ((widget (copy-sequence type)))
721 (widget-put widget :parent parent)
722 (unless (widget-get widget :indent)
723 (widget-put widget :indent (+ (or (widget-get parent :indent) 0)
724 (or (widget-get widget :extra-offset) 0)
725 (widget-get parent :offset))))
726 (widget-apply widget :create)
727 widget))
729 (defun widget-create-child-value (parent type value)
730 "Create widget of TYPE with value VALUE."
731 (let ((widget (copy-sequence type)))
732 (widget-put widget :value (widget-apply widget :value-to-internal value))
733 (widget-put widget :parent parent)
734 (unless (widget-get widget :indent)
735 (widget-put widget :indent (+ (or (widget-get parent :indent) 0)
736 (or (widget-get widget :extra-offset) 0)
737 (widget-get parent :offset))))
738 (widget-apply widget :create)
739 widget))
741 ;;;###autoload
742 (defun widget-delete (widget)
743 "Delete WIDGET."
744 (widget-apply widget :delete))
746 (defun widget-convert (type &rest args)
747 "Convert TYPE to a widget without inserting it in the buffer.
748 The optional ARGS are additional keyword arguments."
749 ;; Don't touch the type.
750 (let* ((widget (if (symbolp type)
751 (list type)
752 (copy-sequence type)))
753 (current widget)
754 (keys args))
755 ;; First set the :args keyword.
756 (while (cdr current) ;Look in the type.
757 (let ((next (car (cdr current))))
758 (if (and (symbolp next) (eq (aref (symbol-name next) 0) ?:))
759 (setq current (cdr (cdr current)))
760 (setcdr current (list :args (cdr current)))
761 (setq current nil))))
762 (while args ;Look in the args.
763 (let ((next (nth 0 args)))
764 (if (and (symbolp next) (eq (aref (symbol-name next) 0) ?:))
765 (setq args (nthcdr 2 args))
766 (widget-put widget :args args)
767 (setq args nil))))
768 ;; Then Convert the widget.
769 (setq type widget)
770 (while type
771 (let ((convert-widget (plist-get (cdr type) :convert-widget)))
772 (if convert-widget
773 (setq widget (funcall convert-widget widget))))
774 (setq type (get (car type) 'widget-type)))
775 ;; Finally set the keyword args.
776 (while keys
777 (let ((next (nth 0 keys)))
778 (if (and (symbolp next) (eq (aref (symbol-name next) 0) ?:))
779 (progn
780 (widget-put widget next (nth 1 keys))
781 (setq keys (nthcdr 2 keys)))
782 (setq keys nil))))
783 ;; Convert the :value to internal format.
784 (if (widget-member widget :value)
785 (let ((value (widget-get widget :value)))
786 (widget-put widget
787 :value (widget-apply widget :value-to-internal value))))
788 ;; Return the newly create widget.
789 widget))
791 (defun widget-insert (&rest args)
792 "Call `insert' with ARGS and make the text read only."
793 (let ((inhibit-read-only t)
794 before-change-functions
795 after-change-functions
796 (from (point)))
797 (apply 'insert args)))
799 (defun widget-convert-text (type from to
800 &optional button-from button-to
801 &rest args)
802 "Return a widget of type TYPE with endpoint FROM TO.
803 Optional ARGS are extra keyword arguments for TYPE.
804 and TO will be used as the widgets end points. If optional arguments
805 BUTTON-FROM and BUTTON-TO are given, these will be used as the widgets
806 button end points.
807 Optional ARGS are extra keyword arguments for TYPE."
808 (let ((widget (apply 'widget-convert type :delete 'widget-leave-text args))
809 (from (copy-marker from))
810 (to (copy-marker to)))
811 (set-marker-insertion-type from t)
812 (set-marker-insertion-type to nil)
813 (widget-put widget :from from)
814 (widget-put widget :to to)
815 (when button-from
816 (widget-specify-button widget button-from button-to))
817 widget))
819 (defun widget-convert-button (type from to &rest args)
820 "Return a widget of type TYPE with endpoint FROM TO.
821 Optional ARGS are extra keyword arguments for TYPE.
822 No text will be inserted to the buffer, instead the text between FROM
823 and TO will be used as the widgets end points, as well as the widgets
824 button end points."
825 (apply 'widget-convert-text type from to from to args))
827 (defun widget-leave-text (widget)
828 "Remove markers and overlays from WIDGET and its children."
829 (let ((from (widget-get widget :from))
830 (to (widget-get widget :to))
831 (button (widget-get widget :button-overlay))
832 (sample (widget-get widget :sample-overlay))
833 (doc (widget-get widget :doc-overlay))
834 (field (widget-get widget :field-overlay))
835 (children (widget-get widget :children)))
836 (set-marker from nil)
837 (set-marker to nil)
838 (when button
839 (delete-overlay button))
840 (when sample
841 (delete-overlay sample))
842 (when doc
843 (delete-overlay doc))
844 (when field
845 (delete-overlay field))
846 (mapcar 'widget-leave-text children)))
848 ;;; Keymap and Commands.
850 (defvar widget-keymap nil
851 "Keymap containing useful binding for buffers containing widgets.
852 Recommended as a parent keymap for modes using widgets.")
854 (unless widget-keymap
855 (setq widget-keymap (make-sparse-keymap))
856 (define-key widget-keymap "\t" 'widget-forward)
857 (define-key widget-keymap [(shift tab)] 'widget-backward)
858 (define-key widget-keymap [backtab] 'widget-backward)
859 (if (string-match "XEmacs" emacs-version)
860 (progn
861 ;;Glyph support.
862 (define-key widget-keymap [button1] 'widget-button1-click)
863 (define-key widget-keymap [button2] 'widget-button-click))
864 (define-key widget-keymap [down-mouse-2] 'widget-button-click))
865 (define-key widget-keymap "\C-m" 'widget-button-press))
867 (defvar widget-global-map global-map
868 "Keymap used for events the widget does not handle themselves.")
869 (make-variable-buffer-local 'widget-global-map)
871 (defvar widget-field-keymap nil
872 "Keymap used inside an editable field.")
874 (unless widget-field-keymap
875 (setq widget-field-keymap (copy-keymap widget-keymap))
876 (define-key widget-field-keymap [menu-bar] 'nil)
877 (define-key widget-field-keymap "\C-k" 'widget-kill-line)
878 (define-key widget-field-keymap "\M-\t" 'widget-complete)
879 (define-key widget-field-keymap "\C-m" 'widget-field-activate)
880 (define-key widget-field-keymap "\C-a" 'widget-beginning-of-line)
881 (define-key widget-field-keymap "\C-e" 'widget-end-of-line)
882 (set-keymap-parent widget-field-keymap global-map))
884 (defvar widget-text-keymap nil
885 "Keymap used inside a text field.")
887 (unless widget-text-keymap
888 (setq widget-text-keymap (copy-keymap widget-keymap))
889 (define-key widget-text-keymap [menu-bar] 'nil)
890 (define-key widget-text-keymap "\C-a" 'widget-beginning-of-line)
891 (define-key widget-text-keymap "\C-e" 'widget-end-of-line)
892 (set-keymap-parent widget-text-keymap global-map))
894 (defun widget-field-activate (pos &optional event)
895 "Invoke the ediable field at point."
896 (interactive "@d")
897 (let ((field (get-char-property pos 'field)))
898 (if field
899 (widget-apply-action field event)
900 (call-interactively
901 (lookup-key widget-global-map (this-command-keys))))))
903 (defvar widget-button-pressed-face 'widget-button-pressed-face
904 "Face used for pressed buttons in widgets.
905 This exists as a variable so it can be set locally in certain buffers.")
907 (defface widget-button-pressed-face
908 '((((class color))
909 (:foreground "red"))
911 (:bold t :underline t)))
912 "Face used for pressed buttons."
913 :group 'widget-faces)
915 (defun widget-button-click (event)
916 "Invoke the button that the mouse is pointing at, and move there."
917 (interactive "@e")
918 (mouse-set-point event)
919 (cond ((and (fboundp 'event-glyph)
920 (event-glyph event))
921 (widget-glyph-click event))
922 ((widget-event-point event)
923 (let* ((pos (widget-event-point event))
924 (button (get-char-property pos 'button)))
925 (if button
926 (let* ((overlay (widget-get button :button-overlay))
927 (face (overlay-get overlay 'face))
928 (mouse-face (overlay-get overlay 'mouse-face)))
929 (unwind-protect
930 (let ((track-mouse t))
931 (overlay-put overlay
932 'face widget-button-pressed-face)
933 (overlay-put overlay
934 'mouse-face widget-button-pressed-face)
935 (unless (widget-apply button :mouse-down-action event)
936 (while (not (button-release-event-p event))
937 (setq event (widget-read-event)
938 pos (widget-event-point event))
939 (if (and pos
940 (eq (get-char-property pos 'button)
941 button))
942 (progn
943 (overlay-put overlay
944 'face
945 widget-button-pressed-face)
946 (overlay-put overlay
947 'mouse-face
948 widget-button-pressed-face))
949 (overlay-put overlay 'face face)
950 (overlay-put overlay 'mouse-face mouse-face))))
951 (when (and pos
952 (eq (get-char-property pos 'button) button))
953 (widget-apply-action button event)))
954 (overlay-put overlay 'face face)
955 (overlay-put overlay 'mouse-face mouse-face)))
956 (let ((up t)
957 command)
958 ;; Find the global command to run, and check whether it
959 ;; is bound to an up event.
960 (cond ((setq command ;down event
961 (lookup-key widget-global-map [ button2 ]))
962 (setq up nil))
963 ((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 [ button2up ])))
968 ((setq command ;up event
969 (lookup-key widget-global-map [ mouse-2]))))
970 (when up
971 ;; Don't execute up events twice.
972 (while (not (button-release-event-p event))
973 (setq event (widget-read-event))))
974 (when command
975 (call-interactively command))))))
977 (message "You clicked somewhere weird."))))
979 (defun widget-button1-click (event)
980 "Invoke glyph below mouse pointer."
981 (interactive "@e")
982 (if (and (fboundp 'event-glyph)
983 (event-glyph event))
984 (widget-glyph-click event)
985 (call-interactively (lookup-key widget-global-map (this-command-keys)))))
987 (defun widget-glyph-click (event)
988 "Handle click on a glyph."
989 (let* ((glyph (event-glyph event))
990 (widget (glyph-property glyph 'widget))
991 (extent (event-glyph-extent event))
992 (down-glyph (or (and widget (widget-get widget :glyph-down)) glyph))
993 (up-glyph (or (and widget (widget-get widget :glyph-up)) glyph))
994 (last event))
995 ;; Wait for the release.
996 (while (not (button-release-event-p last))
997 (if (eq extent (event-glyph-extent last))
998 (set-extent-property extent 'end-glyph down-glyph)
999 (set-extent-property extent 'end-glyph up-glyph))
1000 (setq last (read-event event)))
1001 ;; Release glyph.
1002 (when down-glyph
1003 (set-extent-property extent 'end-glyph up-glyph))
1004 ;; Apply widget action.
1005 (when (eq extent (event-glyph-extent last))
1006 (let ((widget (glyph-property (event-glyph event) 'widget)))
1007 (cond ((null widget)
1008 (message "You clicked on a glyph."))
1009 ((not (widget-apply widget :active))
1010 (message "This glyph is inactive."))
1012 (widget-apply-action widget event)))))))
1014 (defun widget-button-press (pos &optional event)
1015 "Invoke button at POS."
1016 (interactive "@d")
1017 (let ((button (get-char-property pos 'button)))
1018 (if button
1019 (widget-apply-action button event)
1020 (let ((command (lookup-key widget-global-map (this-command-keys))))
1021 (when (commandp command)
1022 (call-interactively command))))))
1024 (defun widget-tabable-at (&optional pos)
1025 "Return the tabable widget at POS, or nil.
1026 POS defaults to the value of (point)."
1027 (unless pos
1028 (setq pos (point)))
1029 (let ((widget (or (get-char-property (point) 'button)
1030 (get-char-property (point) 'field))))
1031 (if widget
1032 (let ((order (widget-get widget :tab-order)))
1033 (if order
1034 (if (>= order 0)
1035 widget
1036 nil)
1037 widget))
1038 nil)))
1040 (defvar widget-use-overlay-change t
1041 "If non-nil, use overlay change functions to tab around in the buffer.
1042 This is much faster, but doesn't work reliably on Emacs 19.34.")
1044 (defun widget-move (arg)
1045 "Move point to the ARG next field or button.
1046 ARG may be negative to move backward."
1047 (or (bobp) (> arg 0) (backward-char))
1048 (let ((pos (point))
1049 (number arg)
1050 (old (widget-tabable-at))
1051 new)
1052 ;; Forward.
1053 (while (> arg 0)
1054 (cond ((eobp)
1055 (goto-char (point-min)))
1056 (widget-use-overlay-change
1057 (goto-char (next-overlay-change (point))))
1059 (forward-char 1)))
1060 (and (eq pos (point))
1061 (eq arg number)
1062 (error "No buttons or fields found"))
1063 (let ((new (widget-tabable-at)))
1064 (when new
1065 (unless (eq new old)
1066 (setq arg (1- arg))
1067 (setq old new)))))
1068 ;; Backward.
1069 (while (< arg 0)
1070 (cond ((bobp)
1071 (goto-char (point-max)))
1072 (widget-use-overlay-change
1073 (goto-char (previous-overlay-change (point))))
1075 (backward-char 1)))
1076 (and (eq pos (point))
1077 (eq arg number)
1078 (error "No buttons or fields found"))
1079 (let ((new (widget-tabable-at)))
1080 (when new
1081 (unless (eq new old)
1082 (setq arg (1+ arg))))))
1083 (let ((new (widget-tabable-at)))
1084 (while (eq (widget-tabable-at) new)
1085 (backward-char)))
1086 (forward-char))
1087 (widget-echo-help (point))
1088 (run-hooks 'widget-move-hook))
1090 (defun widget-forward (arg)
1091 "Move point to the next field or button.
1092 With optional ARG, move across that many fields."
1093 (interactive "p")
1094 (run-hooks 'widget-forward-hook)
1095 (widget-move arg))
1097 (defun widget-backward (arg)
1098 "Move point to the previous field or button.
1099 With optional ARG, move across that many fields."
1100 (interactive "p")
1101 (run-hooks 'widget-backward-hook)
1102 (widget-move (- arg)))
1104 (defun widget-beginning-of-line ()
1105 "Go to beginning of field or beginning of line, whichever is first."
1106 (interactive)
1107 (let* ((field (widget-field-find (point)))
1108 (start (and field (widget-field-start field)))
1109 (bol (save-excursion
1110 (beginning-of-line)
1111 (point))))
1112 (goto-char (if start
1113 (max start bol)
1114 bol))))
1116 (defun widget-end-of-line ()
1117 "Go to end of field or end of line, whichever is first."
1118 (interactive)
1119 (let* ((field (widget-field-find (point)))
1120 (end (and field (widget-field-end field)))
1121 (eol (save-excursion
1122 (end-of-line)
1123 (point))))
1124 (goto-char (if end
1125 (min end eol)
1126 eol))))
1128 (defun widget-kill-line ()
1129 "Kill to end of field or end of line, whichever is first."
1130 (interactive)
1131 (let* ((field (widget-field-find (point)))
1132 (newline (save-excursion (forward-line 1) (point)))
1133 (end (and field (widget-field-end field))))
1134 (if (and field (> newline end))
1135 (kill-region (point) end)
1136 (call-interactively 'kill-line))))
1138 (defcustom widget-complete-field (lookup-key global-map "\M-\t")
1139 "Default function to call for completion inside fields."
1140 :options '(ispell-complete-word complete-tag lisp-complete-symbol)
1141 :type 'function
1142 :group 'widgets)
1144 (defun widget-complete ()
1145 "Complete content of editable field from point.
1146 When not inside a field, move to the previous button or field."
1147 (interactive)
1148 (let ((field (widget-field-find (point))))
1149 (if field
1150 (widget-apply field :complete)
1151 (error "Not in an editable field"))))
1153 ;;; Setting up the buffer.
1155 (defvar widget-field-new nil)
1156 ;; List of all newly created editable fields in the buffer.
1157 (make-variable-buffer-local 'widget-field-new)
1159 (defvar widget-field-list nil)
1160 ;; List of all editable fields in the buffer.
1161 (make-variable-buffer-local 'widget-field-list)
1163 (defun widget-setup ()
1164 "Setup current buffer so editing string widgets works."
1165 (let ((inhibit-read-only t)
1166 (after-change-functions nil)
1167 before-change-functions
1168 field)
1169 (while widget-field-new
1170 (setq field (car widget-field-new)
1171 widget-field-new (cdr widget-field-new)
1172 widget-field-list (cons field widget-field-list))
1173 (let ((from (car (widget-get field :field-overlay)))
1174 (to (cdr (widget-get field :field-overlay))))
1175 (widget-specify-field field
1176 (marker-position from) (marker-position to))
1177 (set-marker from nil)
1178 (set-marker to nil))))
1179 (widget-clear-undo)
1180 (widget-add-change))
1182 (defvar widget-field-last nil)
1183 ;; Last field containing point.
1184 (make-variable-buffer-local 'widget-field-last)
1186 (defvar widget-field-was nil)
1187 ;; The widget data before the change.
1188 (make-variable-buffer-local 'widget-field-was)
1190 (defun widget-field-buffer (widget)
1191 "Return the start of WIDGET's editing field."
1192 (let ((overlay (widget-get widget :field-overlay)))
1193 (and overlay (overlay-buffer overlay))))
1195 (defun widget-field-start (widget)
1196 "Return the start of WIDGET's editing field."
1197 (let ((overlay (widget-get widget :field-overlay)))
1198 (and overlay (overlay-start overlay))))
1200 (defun widget-field-end (widget)
1201 "Return the end of WIDGET's editing field."
1202 (let ((overlay (widget-get widget :field-overlay)))
1203 ;; Don't subtract one if local-map works at the end of the overlay.
1204 (and overlay (if (or widget-field-add-space
1205 (null (widget-get widget :size)))
1206 (1- (overlay-end overlay))
1207 (overlay-end overlay)))))
1209 (defun widget-field-find (pos)
1210 "Return the field at POS.
1211 Unlike (get-char-property POS 'field) this, works with empty fields too."
1212 (let ((fields widget-field-list)
1213 field found)
1214 (while fields
1215 (setq field (car fields)
1216 fields (cdr fields))
1217 (let ((start (widget-field-start field))
1218 (end (widget-field-end field)))
1219 (when (and (<= start pos) (<= pos end))
1220 (when found
1221 (debug "Overlapping fields"))
1222 (setq found field))))
1223 found))
1225 (defun widget-before-change (from to)
1226 ;; This is how, for example, a variable changes its state to `modified'.
1227 ;; when it is being edited.
1228 (unless inhibit-read-only
1229 (let ((from-field (widget-field-find from))
1230 (to-field (widget-field-find to)))
1231 (cond ((not (eq from-field to-field))
1232 (add-hook 'post-command-hook 'widget-add-change nil t)
1233 (signal 'text-read-only
1234 '("Change should be restricted to a single field")))
1235 ((null from-field)
1236 (add-hook 'post-command-hook 'widget-add-change nil t)
1237 (signal 'text-read-only
1238 '("Attempt to change text outside editable field")))
1239 (widget-field-use-before-change
1240 (condition-case nil
1241 (widget-apply from-field :notify from-field)
1242 (error (debug "Before Change"))))))))
1244 (defun widget-add-change ()
1245 (make-local-hook 'post-command-hook)
1246 (remove-hook 'post-command-hook 'widget-add-change t)
1247 (make-local-hook 'before-change-functions)
1248 (add-hook 'before-change-functions 'widget-before-change nil t)
1249 (make-local-hook 'after-change-functions)
1250 (add-hook 'after-change-functions 'widget-after-change nil t))
1252 (defun widget-after-change (from to old)
1253 ;; Adjust field size and text properties.
1254 (condition-case nil
1255 (let ((field (widget-field-find from))
1256 (other (widget-field-find to)))
1257 (when field
1258 (unless (eq field other)
1259 (debug "Change in different fields"))
1260 (let ((size (widget-get field :size)))
1261 (when size
1262 (let ((begin (widget-field-start field))
1263 (end (widget-field-end field)))
1264 (cond ((< (- end begin) size)
1265 ;; Field too small.
1266 (save-excursion
1267 (goto-char end)
1268 (insert-char ?\ (- (+ begin size) end))))
1269 ((> (- end begin) size)
1270 ;; Field too large and
1271 (if (or (< (point) (+ begin size))
1272 (> (point) end))
1273 ;; Point is outside extra space.
1274 (setq begin (+ begin size))
1275 ;; Point is within the extra space.
1276 (setq begin (point)))
1277 (save-excursion
1278 (goto-char end)
1279 (while (and (eq (preceding-char) ?\ )
1280 (> (point) begin))
1281 (delete-backward-char 1)))))))
1282 (widget-specify-secret field))
1283 (widget-apply field :notify field)))
1284 (error (debug "After Change"))))
1286 ;;; Widget Functions
1288 ;; These functions are used in the definition of multiple widgets.
1290 (defun widget-parent-action (widget &optional event)
1291 "Tell :parent of WIDGET to handle the :action.
1292 Optional EVENT is the event that triggered the action."
1293 (widget-apply (widget-get widget :parent) :action event))
1295 (defun widget-children-value-delete (widget)
1296 "Delete all :children and :buttons in WIDGET."
1297 (mapcar 'widget-delete (widget-get widget :children))
1298 (widget-put widget :children nil)
1299 (mapcar 'widget-delete (widget-get widget :buttons))
1300 (widget-put widget :buttons nil))
1302 (defun widget-children-validate (widget)
1303 "All the :children must be valid."
1304 (let ((children (widget-get widget :children))
1305 child found)
1306 (while (and children (not found))
1307 (setq child (car children)
1308 children (cdr children)
1309 found (widget-apply child :validate)))
1310 found))
1312 (defun widget-types-convert-widget (widget)
1313 "Convert :args as widget types in WIDGET."
1314 (widget-put widget :args (mapcar 'widget-convert (widget-get widget :args)))
1315 widget)
1317 (defun widget-value-convert-widget (widget)
1318 "Initialize :value from :args in WIDGET."
1319 (let ((args (widget-get widget :args)))
1320 (when args
1321 (widget-put widget :value (car args))
1322 ;; Don't convert :value here, as this is done in `widget-convert'.
1323 ;; (widget-put widget :value (widget-apply widget
1324 ;; :value-to-internal (car args)))
1325 (widget-put widget :args nil)))
1326 widget)
1328 (defun widget-value-value-get (widget)
1329 "Return the :value property of WIDGET."
1330 (widget-get widget :value))
1332 ;;; The `default' Widget.
1334 (define-widget 'default nil
1335 "Basic widget other widgets are derived from."
1336 :value-to-internal (lambda (widget value) value)
1337 :value-to-external (lambda (widget value) value)
1338 :button-prefix 'widget-button-prefix
1339 :button-suffix 'widget-button-suffix
1340 :complete 'widget-default-complete
1341 :create 'widget-default-create
1342 :indent nil
1343 :offset 0
1344 :format-handler 'widget-default-format-handler
1345 :button-face-get 'widget-default-button-face-get
1346 :sample-face-get 'widget-default-sample-face-get
1347 :delete 'widget-default-delete
1348 :value-set 'widget-default-value-set
1349 :value-inline 'widget-default-value-inline
1350 :default-get 'widget-default-default-get
1351 :menu-tag-get 'widget-default-menu-tag-get
1352 :validate (lambda (widget) nil)
1353 :active 'widget-default-active
1354 :activate 'widget-specify-active
1355 :deactivate 'widget-default-deactivate
1356 :mouse-down-action (lambda (widget event) nil)
1357 :action 'widget-default-action
1358 :notify 'widget-default-notify
1359 :prompt-value 'widget-default-prompt-value)
1361 (defun widget-default-complete (widget)
1362 "Call the value of the :complete-function property of WIDGET.
1363 If that does not exists, call the value of `widget-complete-field'."
1364 (let ((fun (widget-get widget :complete-function)))
1365 (call-interactively (or fun widget-complete-field))))
1367 (defun widget-default-create (widget)
1368 "Create WIDGET at point in the current buffer."
1369 (widget-specify-insert
1370 (let ((from (point))
1371 button-begin button-end
1372 sample-begin sample-end
1373 doc-begin doc-end
1374 value-pos)
1375 (insert (widget-get widget :format))
1376 (goto-char from)
1377 ;; Parse escapes in format.
1378 (while (re-search-forward "%\\(.\\)" nil t)
1379 (let ((escape (aref (match-string 1) 0)))
1380 (replace-match "" t t)
1381 (cond ((eq escape ?%)
1382 (insert "%"))
1383 ((eq escape ?\[)
1384 (setq button-begin (point))
1385 (insert (widget-get-indirect widget :button-prefix)))
1386 ((eq escape ?\])
1387 (insert (widget-get-indirect widget :button-suffix))
1388 (setq button-end (point)))
1389 ((eq escape ?\{)
1390 (setq sample-begin (point)))
1391 ((eq escape ?\})
1392 (setq sample-end (point)))
1393 ((eq escape ?n)
1394 (when (widget-get widget :indent)
1395 (insert "\n")
1396 (insert-char ? (widget-get widget :indent))))
1397 ((eq escape ?t)
1398 (let ((glyph (widget-get widget :tag-glyph))
1399 (tag (widget-get widget :tag)))
1400 (cond (glyph
1401 (widget-glyph-insert widget (or tag "image") glyph))
1402 (tag
1403 (insert tag))
1405 (let ((standard-output (current-buffer)))
1406 (princ (widget-get widget :value)))))))
1407 ((eq escape ?d)
1408 (let ((doc (widget-get widget :doc)))
1409 (when doc
1410 (setq doc-begin (point))
1411 (insert doc)
1412 (while (eq (preceding-char) ?\n)
1413 (delete-backward-char 1))
1414 (insert "\n")
1415 (setq doc-end (point)))))
1416 ((eq escape ?v)
1417 (if (and button-begin (not button-end))
1418 (widget-apply widget :value-create)
1419 (setq value-pos (point))))
1421 (widget-apply widget :format-handler escape)))))
1422 ;; Specify button, sample, and doc, and insert value.
1423 (and button-begin button-end
1424 (widget-specify-button widget button-begin button-end))
1425 (and sample-begin sample-end
1426 (widget-specify-sample widget sample-begin sample-end))
1427 (and doc-begin doc-end
1428 (widget-specify-doc widget doc-begin doc-end))
1429 (when value-pos
1430 (goto-char value-pos)
1431 (widget-apply widget :value-create)))
1432 (let ((from (copy-marker (point-min)))
1433 (to (copy-marker (point-max))))
1434 (set-marker-insertion-type from t)
1435 (set-marker-insertion-type to nil)
1436 (widget-put widget :from from)
1437 (widget-put widget :to to)))
1438 (widget-clear-undo))
1440 (defun widget-default-format-handler (widget escape)
1441 ;; We recognize the %h escape by default.
1442 (let* ((buttons (widget-get widget :buttons)))
1443 (cond ((eq escape ?h)
1444 (let* ((doc-property (widget-get widget :documentation-property))
1445 (doc-try (cond ((widget-get widget :doc))
1446 ((symbolp doc-property)
1447 (documentation-property
1448 (widget-get widget :value)
1449 doc-property))
1451 (funcall doc-property
1452 (widget-get widget :value)))))
1453 (doc-text (and (stringp doc-try)
1454 (> (length doc-try) 1)
1455 doc-try))
1456 (doc-indent (widget-get widget :documentation-indent)))
1457 (when doc-text
1458 (and (eq (preceding-char) ?\n)
1459 (widget-get widget :indent)
1460 (insert-char ? (widget-get widget :indent)))
1461 ;; The `*' in the beginning is redundant.
1462 (when (eq (aref doc-text 0) ?*)
1463 (setq doc-text (substring doc-text 1)))
1464 ;; Get rid of trailing newlines.
1465 (when (string-match "\n+\\'" doc-text)
1466 (setq doc-text (substring doc-text 0 (match-beginning 0))))
1467 (push (widget-create-child-and-convert
1468 widget 'documentation-string
1469 :indent (cond ((numberp doc-indent )
1470 doc-indent)
1471 ((null doc-indent)
1472 nil)
1473 (t 0))
1474 doc-text)
1475 buttons))))
1477 (error "Unknown escape `%c'" escape)))
1478 (widget-put widget :buttons buttons)))
1480 (defun widget-default-button-face-get (widget)
1481 ;; Use :button-face or widget-button-face
1482 (or (widget-get widget :button-face)
1483 (let ((parent (widget-get widget :parent)))
1484 (if parent
1485 (widget-apply parent :button-face-get)
1486 widget-button-face))))
1488 (defun widget-default-sample-face-get (widget)
1489 ;; Use :sample-face.
1490 (widget-get widget :sample-face))
1492 (defun widget-default-delete (widget)
1493 ;; Remove widget from the buffer.
1494 (let ((from (widget-get widget :from))
1495 (to (widget-get widget :to))
1496 (inactive-overlay (widget-get widget :inactive))
1497 (button-overlay (widget-get widget :button-overlay))
1498 (sample-overlay (widget-get widget :sample-overlay))
1499 (doc-overlay (widget-get widget :doc-overlay))
1500 before-change-functions
1501 after-change-functions
1502 (inhibit-read-only t))
1503 (widget-apply widget :value-delete)
1504 (when inactive-overlay
1505 (delete-overlay inactive-overlay))
1506 (when button-overlay
1507 (delete-overlay button-overlay))
1508 (when sample-overlay
1509 (delete-overlay sample-overlay))
1510 (when doc-overlay
1511 (delete-overlay doc-overlay))
1512 (when (< from to)
1513 ;; Kludge: this doesn't need to be true for empty formats.
1514 (delete-region from to))
1515 (set-marker from nil)
1516 (set-marker to nil))
1517 (widget-clear-undo))
1519 (defun widget-default-value-set (widget value)
1520 ;; Recreate widget with new value.
1521 (let* ((old-pos (point))
1522 (from (copy-marker (widget-get widget :from)))
1523 (to (copy-marker (widget-get widget :to)))
1524 (offset (if (and (<= from old-pos) (<= old-pos to))
1525 (if (>= old-pos (1- to))
1526 (- old-pos to 1)
1527 (- old-pos from)))))
1528 ;;??? Bug: this ought to insert the new value before deleting the old one,
1529 ;; so that markers on either side of the value automatically
1530 ;; stay on the same side. -- rms.
1531 (save-excursion
1532 (goto-char (widget-get widget :from))
1533 (widget-apply widget :delete)
1534 (widget-put widget :value value)
1535 (widget-apply widget :create))
1536 (if offset
1537 (if (< offset 0)
1538 (goto-char (+ (widget-get widget :to) offset 1))
1539 (goto-char (min (+ from offset) (1- (widget-get widget :to))))))))
1541 (defun widget-default-value-inline (widget)
1542 ;; Wrap value in a list unless it is inline.
1543 (if (widget-get widget :inline)
1544 (widget-value widget)
1545 (list (widget-value widget))))
1547 (defun widget-default-default-get (widget)
1548 ;; Get `:value'.
1549 (widget-get widget :value))
1551 (defun widget-default-menu-tag-get (widget)
1552 ;; Use tag or value for menus.
1553 (or (widget-get widget :menu-tag)
1554 (widget-get widget :tag)
1555 (widget-princ-to-string (widget-get widget :value))))
1557 (defun widget-default-active (widget)
1558 "Return t iff this widget active (user modifiable)."
1559 (and (not (widget-get widget :inactive))
1560 (let ((parent (widget-get widget :parent)))
1561 (or (null parent)
1562 (widget-apply parent :active)))))
1564 (defun widget-default-deactivate (widget)
1565 "Make WIDGET inactive for user modifications."
1566 (widget-specify-inactive widget
1567 (widget-get widget :from)
1568 (widget-get widget :to)))
1570 (defun widget-default-action (widget &optional event)
1571 ;; Notify the parent when a widget change
1572 (let ((parent (widget-get widget :parent)))
1573 (when parent
1574 (widget-apply parent :notify widget event))))
1576 (defun widget-default-notify (widget child &optional event)
1577 ;; Pass notification to parent.
1578 (widget-default-action widget event))
1580 (defun widget-default-prompt-value (widget prompt value unbound)
1581 ;; Read an arbitrary value. Stolen from `set-variable'.
1582 ;; (let ((initial (if unbound
1583 ;; nil
1584 ;; ;; It would be nice if we could do a `(cons val 1)' here.
1585 ;; (prin1-to-string (custom-quote value))))))
1586 (eval-minibuffer prompt ))
1588 ;;; The `item' Widget.
1590 (define-widget 'item 'default
1591 "Constant items for inclusion in other widgets."
1592 :convert-widget 'widget-value-convert-widget
1593 :value-create 'widget-item-value-create
1594 :value-delete 'ignore
1595 :value-get 'widget-value-value-get
1596 :match 'widget-item-match
1597 :match-inline 'widget-item-match-inline
1598 :action 'widget-item-action
1599 :format "%t\n")
1601 (defun widget-item-value-create (widget)
1602 ;; Insert the printed representation of the value.
1603 (let ((standard-output (current-buffer)))
1604 (princ (widget-get widget :value))))
1606 (defun widget-item-match (widget value)
1607 ;; Match if the value is the same.
1608 (equal (widget-get widget :value) value))
1610 (defun widget-item-match-inline (widget values)
1611 ;; Match if the value is the same.
1612 (let ((value (widget-get widget :value)))
1613 (and (listp value)
1614 (<= (length value) (length values))
1615 (let ((head (widget-sublist values 0 (length value))))
1616 (and (equal head value)
1617 (cons head (widget-sublist values (length value))))))))
1619 (defun widget-sublist (list start &optional end)
1620 "Return the sublist of LIST from START to END.
1621 If END is omitted, it defaults to the length of LIST."
1622 (if (> start 0) (setq list (nthcdr start list)))
1623 (if end
1624 (if (<= end start)
1626 (setq list (copy-sequence list))
1627 (setcdr (nthcdr (- end start 1) list) nil)
1628 list)
1629 (copy-sequence list)))
1631 (defun widget-item-action (widget &optional event)
1632 ;; Just notify itself.
1633 (widget-apply widget :notify widget event))
1635 ;;; The `push-button' Widget.
1637 (defcustom widget-push-button-gui t
1638 "If non nil, use GUI push buttons when available."
1639 :group 'widgets
1640 :type 'boolean)
1642 ;; Cache already created GUI objects.
1643 (defvar widget-push-button-cache nil)
1645 (defcustom widget-push-button-prefix "["
1646 "String used as prefix for buttons."
1647 :type 'string
1648 :group 'widget-button)
1650 (defcustom widget-push-button-suffix "]"
1651 "String used as suffix for buttons."
1652 :type 'string
1653 :group 'widget-button)
1655 (define-widget 'push-button 'item
1656 "A pushable button."
1657 :button-prefix ""
1658 :button-suffix ""
1659 :value-create 'widget-push-button-value-create
1660 :format "%[%v%]")
1662 (defun widget-push-button-value-create (widget)
1663 ;; Insert text representing the `on' and `off' states.
1664 (let* ((tag (or (widget-get widget :tag)
1665 (widget-get widget :value)))
1666 (tag-glyph (widget-get widget :tag-glyph))
1667 (text (concat widget-push-button-prefix
1668 tag widget-push-button-suffix))
1669 (gui (cdr (assoc tag widget-push-button-cache))))
1670 (cond (tag-glyph
1671 (widget-glyph-insert widget text tag-glyph))
1672 ((and (fboundp 'make-gui-button)
1673 (fboundp 'make-glyph)
1674 widget-push-button-gui
1675 (fboundp 'device-on-window-system-p)
1676 (device-on-window-system-p)
1677 (string-match "XEmacs" emacs-version))
1678 (unless gui
1679 (setq gui (make-gui-button tag 'widget-gui-action widget))
1680 (push (cons tag gui) widget-push-button-cache))
1681 (widget-glyph-insert-glyph widget
1682 (make-glyph
1683 (list (nth 0 (aref gui 1))
1684 (vector 'string ':data text)))
1685 (make-glyph
1686 (list (nth 1 (aref gui 1))
1687 (vector 'string ':data text)))
1688 (make-glyph
1689 (list (nth 2 (aref gui 1))
1690 (vector 'string ':data text)))))
1692 (insert text)))))
1694 (defun widget-gui-action (widget)
1695 "Apply :action for WIDGET."
1696 (widget-apply-action widget (this-command-keys)))
1698 ;;; The `link' Widget.
1700 (defcustom widget-link-prefix "["
1701 "String used as prefix for links."
1702 :type 'string
1703 :group 'widget-button)
1705 (defcustom widget-link-suffix "]"
1706 "String used as suffix for links."
1707 :type 'string
1708 :group 'widget-button)
1710 (define-widget 'link 'item
1711 "An embedded link."
1712 :button-prefix 'widget-link-prefix
1713 :button-suffix 'widget-link-suffix
1714 :help-echo "Follow the link."
1715 :format "%[%t%]")
1717 ;;; The `info-link' Widget.
1719 (define-widget 'info-link 'link
1720 "A link to an info file."
1721 :action 'widget-info-link-action)
1723 (defun widget-info-link-action (widget &optional event)
1724 "Open the info node specified by WIDGET."
1725 (Info-goto-node (widget-value widget)))
1727 ;;; The `url-link' Widget.
1729 (define-widget 'url-link 'link
1730 "A link to an www page."
1731 :action 'widget-url-link-action)
1733 (defun widget-url-link-action (widget &optional event)
1734 "Open the url specified by WIDGET."
1735 (browse-url (widget-value widget)))
1737 ;;; The `function-link' Widget.
1739 (define-widget 'function-link 'link
1740 "A link to an Emacs function."
1741 :action 'widget-function-link-action)
1743 (defun widget-function-link-action (widget &optional event)
1744 "Show the function specified by WIDGET."
1745 (describe-function (widget-value widget)))
1747 ;;; The `variable-link' Widget.
1749 (define-widget 'variable-link 'link
1750 "A link to an Emacs variable."
1751 :action 'widget-variable-link-action)
1753 (defun widget-variable-link-action (widget &optional event)
1754 "Show the variable specified by WIDGET."
1755 (describe-variable (widget-value widget)))
1757 ;;; The `file-link' Widget.
1759 (define-widget 'file-link 'link
1760 "A link to a file."
1761 :action 'widget-file-link-action)
1763 (defun widget-file-link-action (widget &optional event)
1764 "Find the file specified by WIDGET."
1765 (find-file (widget-value widget)))
1767 ;;; The `emacs-library-link' Widget.
1769 (define-widget 'emacs-library-link 'link
1770 "A link to an Emacs Lisp library file."
1771 :action 'widget-emacs-library-link-action)
1773 (defun widget-emacs-library-link-action (widget &optional event)
1774 "Find the Emacs Library file specified by WIDGET."
1775 (find-file (locate-library (widget-value widget))))
1777 ;;; The `emacs-commentary-link' Widget.
1779 (define-widget 'emacs-commentary-link 'link
1780 "A link to Commentary in an Emacs Lisp library file."
1781 :action 'widget-emacs-commentary-link-action)
1783 (defun widget-emacs-commentary-link-action (widget &optional event)
1784 "Find the Commentary section of the Emacs file specified by WIDGET."
1785 (finder-commentary (widget-value widget)))
1787 ;;; The `editable-field' Widget.
1789 (define-widget 'editable-field 'default
1790 "An editable text field."
1791 :convert-widget 'widget-value-convert-widget
1792 :keymap widget-field-keymap
1793 :format "%v"
1794 :value ""
1795 :prompt-internal 'widget-field-prompt-internal
1796 :prompt-history 'widget-field-history
1797 :prompt-value 'widget-field-prompt-value
1798 :action 'widget-field-action
1799 :validate 'widget-field-validate
1800 :valid-regexp ""
1801 :error "No match"
1802 :value-create 'widget-field-value-create
1803 :value-delete 'widget-field-value-delete
1804 :value-get 'widget-field-value-get
1805 :match 'widget-field-match)
1807 (defvar widget-field-history nil
1808 "History of field minibuffer edits.")
1810 (defun widget-field-prompt-internal (widget prompt initial history)
1811 ;; Read string for WIDGET promptinhg with PROMPT.
1812 ;; INITIAL is the initial input and HISTORY is a symbol containing
1813 ;; the earlier input.
1814 (read-string prompt initial history))
1816 (defun widget-field-prompt-value (widget prompt value unbound)
1817 ;; Prompt for a string.
1818 (let ((initial (if unbound
1820 (cons (widget-apply widget :value-to-internal
1821 value) 0)))
1822 (history (widget-get widget :prompt-history)))
1823 (let ((answer (widget-apply widget
1824 :prompt-internal prompt initial history)))
1825 (widget-apply widget :value-to-external answer))))
1827 (defvar widget-edit-functions nil)
1829 (defun widget-field-action (widget &optional event)
1830 ;; Move to next field.
1831 (widget-forward 1)
1832 (run-hook-with-args 'widget-edit-functions widget))
1834 (defun widget-field-validate (widget)
1835 ;; Valid if the content matches `:valid-regexp'.
1836 (save-excursion
1837 (let ((value (widget-apply widget :value-get))
1838 (regexp (widget-get widget :valid-regexp)))
1839 (if (string-match regexp value)
1841 widget))))
1843 (defun widget-field-value-create (widget)
1844 ;; Create an editable text field.
1845 (let ((size (widget-get widget :size))
1846 (value (widget-get widget :value))
1847 (from (point))
1848 ;; This is changed to a real overlay in `widget-setup'. We
1849 ;; need the end points to behave differently until
1850 ;; `widget-setup' is called.
1851 (overlay (cons (make-marker) (make-marker))))
1852 (widget-put widget :field-overlay overlay)
1853 (insert value)
1854 (and size
1855 (< (length value) size)
1856 (insert-char ?\ (- size (length value))))
1857 (unless (memq widget widget-field-list)
1858 (setq widget-field-new (cons widget widget-field-new)))
1859 (move-marker (cdr overlay) (point))
1860 (set-marker-insertion-type (cdr overlay) nil)
1861 (when (null size)
1862 (insert ?\n))
1863 (move-marker (car overlay) from)
1864 (set-marker-insertion-type (car overlay) t)))
1866 (defun widget-field-value-delete (widget)
1867 ;; Remove the widget from the list of active editing fields.
1868 (setq widget-field-list (delq widget widget-field-list))
1869 ;; These are nil if the :format string doesn't contain `%v'.
1870 (let ((overlay (widget-get widget :field-overlay)))
1871 (when overlay
1872 (delete-overlay overlay))))
1874 (defun widget-field-value-get (widget)
1875 ;; Return current text in editing field.
1876 (let ((from (widget-field-start widget))
1877 (to (widget-field-end widget))
1878 (buffer (widget-field-buffer widget))
1879 (size (widget-get widget :size))
1880 (secret (widget-get widget :secret))
1881 (old (current-buffer)))
1882 (if (and from to)
1883 (progn
1884 (set-buffer buffer)
1885 (while (and size
1886 (not (zerop size))
1887 (> to from)
1888 (eq (char-after (1- to)) ?\ ))
1889 (setq to (1- to)))
1890 (let ((result (buffer-substring-no-properties from to)))
1891 (when secret
1892 (let ((index 0))
1893 (while (< (+ from index) to)
1894 (aset result index
1895 (get-char-property (+ from index) 'secret))
1896 (setq index (1+ index)))))
1897 (set-buffer old)
1898 result))
1899 (widget-get widget :value))))
1901 (defun widget-field-match (widget value)
1902 ;; Match any string.
1903 (stringp value))
1905 ;;; The `text' Widget.
1907 (define-widget 'text 'editable-field
1908 :keymap widget-text-keymap
1909 "A multiline text area.")
1911 ;;; The `menu-choice' Widget.
1913 (define-widget 'menu-choice 'default
1914 "A menu of options."
1915 :convert-widget 'widget-types-convert-widget
1916 :format "%[%t%]: %v"
1917 :case-fold t
1918 :tag "choice"
1919 :void '(item :format "invalid (%t)\n")
1920 :value-create 'widget-choice-value-create
1921 :value-delete 'widget-children-value-delete
1922 :value-get 'widget-choice-value-get
1923 :value-inline 'widget-choice-value-inline
1924 :default-get 'widget-choice-default-get
1925 :mouse-down-action 'widget-choice-mouse-down-action
1926 :action 'widget-choice-action
1927 :error "Make a choice"
1928 :validate 'widget-choice-validate
1929 :match 'widget-choice-match
1930 :match-inline 'widget-choice-match-inline)
1932 (defun widget-choice-value-create (widget)
1933 ;; Insert the first choice that matches the value.
1934 (let ((value (widget-get widget :value))
1935 (args (widget-get widget :args))
1936 (explicit (widget-get widget :explicit-choice))
1937 (explicit-value (widget-get widget :explicit-choice-value))
1938 current)
1939 (if (and explicit (equal value explicit-value))
1940 (progn
1941 ;; If the user specified the choice for this value,
1942 ;; respect that choice as long as the value is the same.
1943 (widget-put widget :children (list (widget-create-child-value
1944 widget explicit value)))
1945 (widget-put widget :choice explicit))
1946 (while args
1947 (setq current (car args)
1948 args (cdr args))
1949 (when (widget-apply current :match value)
1950 (widget-put widget :children (list (widget-create-child-value
1951 widget current value)))
1952 (widget-put widget :choice current)
1953 (setq args nil
1954 current nil)))
1955 (when current
1956 (let ((void (widget-get widget :void)))
1957 (widget-put widget :children (list (widget-create-child-and-convert
1958 widget void :value value)))
1959 (widget-put widget :choice void))))))
1961 (defun widget-choice-value-get (widget)
1962 ;; Get value of the child widget.
1963 (widget-value (car (widget-get widget :children))))
1965 (defun widget-choice-value-inline (widget)
1966 ;; Get value of the child widget.
1967 (widget-apply (car (widget-get widget :children)) :value-inline))
1969 (defun widget-choice-default-get (widget)
1970 ;; Get default for the first choice.
1971 (widget-default-get (car (widget-get widget :args))))
1973 (defcustom widget-choice-toggle nil
1974 "If non-nil, a binary choice will just toggle between the values.
1975 Otherwise, the user will explicitly have to choose between the values
1976 when he invoked the menu."
1977 :type 'boolean
1978 :group 'widgets)
1980 (defun widget-choice-mouse-down-action (widget &optional event)
1981 ;; Return non-nil if we need a menu.
1982 (let ((args (widget-get widget :args))
1983 (old (widget-get widget :choice)))
1984 (cond ((not window-system)
1985 ;; No place to pop up a menu.
1986 nil)
1987 ((not (or (fboundp 'x-popup-menu) (fboundp 'popup-menu)))
1988 ;; No way to pop up a menu.
1989 nil)
1990 ((< (length args) 2)
1991 ;; Empty or singleton list, just return the value.
1992 nil)
1993 ((> (length args) widget-menu-max-size)
1994 ;; Too long, prompt.
1995 nil)
1996 ((> (length args) 2)
1997 ;; Reasonable sized list, use menu.
1999 ((and widget-choice-toggle (memq old args))
2000 ;; We toggle.
2001 nil)
2003 ;; Ask which of the two.
2004 t))))
2006 (defun widget-choice-action (widget &optional event)
2007 ;; Make a choice.
2008 (let ((args (widget-get widget :args))
2009 (old (widget-get widget :choice))
2010 (tag (widget-apply widget :menu-tag-get))
2011 (completion-ignore-case (widget-get widget :case-fold))
2012 this-explicit
2013 current choices)
2014 ;; Remember old value.
2015 (if (and old (not (widget-apply widget :validate)))
2016 (let* ((external (widget-value widget))
2017 (internal (widget-apply old :value-to-internal external)))
2018 (widget-put old :value internal)))
2019 ;; Find new choice.
2020 (setq current
2021 (cond ((= (length args) 0)
2022 nil)
2023 ((= (length args) 1)
2024 (nth 0 args))
2025 ((and widget-choice-toggle
2026 (= (length args) 2)
2027 (memq old args))
2028 (if (eq old (nth 0 args))
2029 (nth 1 args)
2030 (nth 0 args)))
2032 (while args
2033 (setq current (car args)
2034 args (cdr args))
2035 (setq choices
2036 (cons (cons (widget-apply current :menu-tag-get)
2037 current)
2038 choices)))
2039 (setq this-explicit t)
2040 (widget-choose tag (reverse choices) event))))
2041 (when current
2042 ;; If this was an explicit user choice,
2043 ;; record the choice, and the record the value it was made for.
2044 ;; widget-choice-value-create will respect this choice,
2045 ;; as long as the value is the same.
2046 (when this-explicit
2047 (widget-put widget :explicit-choice current)
2048 (widget-put widget :explicit-choice-value (widget-get widget :value)))
2049 (let ((value (widget-default-get current)))
2050 (widget-value-set widget
2051 (widget-apply current :value-to-external value)))
2052 (widget-setup)
2053 (widget-apply widget :notify widget event)))
2054 (run-hook-with-args 'widget-edit-functions widget))
2056 (defun widget-choice-validate (widget)
2057 ;; Valid if we have made a valid choice.
2058 (let ((void (widget-get widget :void))
2059 (choice (widget-get widget :choice))
2060 (child (car (widget-get widget :children))))
2061 (if (eq void choice)
2062 widget
2063 (widget-apply child :validate))))
2065 (defun widget-choice-match (widget value)
2066 ;; Matches if one of the choices matches.
2067 (let ((args (widget-get widget :args))
2068 current found)
2069 (while (and args (not found))
2070 (setq current (car args)
2071 args (cdr args)
2072 found (widget-apply current :match value)))
2073 found))
2075 (defun widget-choice-match-inline (widget values)
2076 ;; Matches if one of the choices matches.
2077 (let ((args (widget-get widget :args))
2078 current found)
2079 (while (and args (null found))
2080 (setq current (car args)
2081 args (cdr args)
2082 found (widget-match-inline current values)))
2083 found))
2085 ;;; The `toggle' Widget.
2087 (define-widget 'toggle 'item
2088 "Toggle between two states."
2089 :format "%[%v%]\n"
2090 :value-create 'widget-toggle-value-create
2091 :action 'widget-toggle-action
2092 :match (lambda (widget value) t)
2093 :on "on"
2094 :off "off")
2096 (defun widget-toggle-value-create (widget)
2097 ;; Insert text representing the `on' and `off' states.
2098 (if (widget-value widget)
2099 (widget-glyph-insert widget
2100 (widget-get widget :on)
2101 (widget-get widget :on-glyph))
2102 (widget-glyph-insert widget
2103 (widget-get widget :off)
2104 (widget-get widget :off-glyph))))
2106 (defun widget-toggle-action (widget &optional event)
2107 ;; Toggle value.
2108 (widget-value-set widget (not (widget-value widget)))
2109 (widget-apply widget :notify widget event)
2110 (run-hook-with-args 'widget-edit-functions widget))
2112 ;;; The `checkbox' Widget.
2114 (define-widget 'checkbox 'toggle
2115 "A checkbox toggle."
2116 :button-suffix ""
2117 :button-prefix ""
2118 :format "%[%v%]"
2119 :on "[X]"
2120 :on-glyph "check1"
2121 :off "[ ]"
2122 :off-glyph "check0"
2123 :action 'widget-checkbox-action)
2125 (defun widget-checkbox-action (widget &optional event)
2126 "Toggle checkbox, notify parent, and set active state of sibling."
2127 (widget-toggle-action widget event)
2128 (let ((sibling (widget-get-sibling widget)))
2129 (when sibling
2130 (if (widget-value widget)
2131 (widget-apply sibling :activate)
2132 (widget-apply sibling :deactivate)))))
2134 ;;; The `checklist' Widget.
2136 (define-widget 'checklist 'default
2137 "A multiple choice widget."
2138 :convert-widget 'widget-types-convert-widget
2139 :format "%v"
2140 :offset 4
2141 :entry-format "%b %v"
2142 :menu-tag "checklist"
2143 :greedy nil
2144 :value-create 'widget-checklist-value-create
2145 :value-delete 'widget-children-value-delete
2146 :value-get 'widget-checklist-value-get
2147 :validate 'widget-checklist-validate
2148 :match 'widget-checklist-match
2149 :match-inline 'widget-checklist-match-inline)
2151 (defun widget-checklist-value-create (widget)
2152 ;; Insert all values
2153 (let ((alist (widget-checklist-match-find widget (widget-get widget :value)))
2154 (args (widget-get widget :args)))
2155 (while args
2156 (widget-checklist-add-item widget (car args) (assq (car args) alist))
2157 (setq args (cdr args)))
2158 (widget-put widget :children (nreverse (widget-get widget :children)))))
2160 (defun widget-checklist-add-item (widget type chosen)
2161 ;; Create checklist item in WIDGET of type TYPE.
2162 ;; If the item is checked, CHOSEN is a cons whose cdr is the value.
2163 (and (eq (preceding-char) ?\n)
2164 (widget-get widget :indent)
2165 (insert-char ? (widget-get widget :indent)))
2166 (widget-specify-insert
2167 (let* ((children (widget-get widget :children))
2168 (buttons (widget-get widget :buttons))
2169 (button-args (or (widget-get type :sibling-args)
2170 (widget-get widget :button-args)))
2171 (from (point))
2172 child button)
2173 (insert (widget-get widget :entry-format))
2174 (goto-char from)
2175 ;; Parse % escapes in format.
2176 (while (re-search-forward "%\\([bv%]\\)" nil t)
2177 (let ((escape (aref (match-string 1) 0)))
2178 (replace-match "" t t)
2179 (cond ((eq escape ?%)
2180 (insert "%"))
2181 ((eq escape ?b)
2182 (setq button (apply 'widget-create-child-and-convert
2183 widget 'checkbox
2184 :value (not (null chosen))
2185 button-args)))
2186 ((eq escape ?v)
2187 (setq child
2188 (cond ((not chosen)
2189 (let ((child (widget-create-child widget type)))
2190 (widget-apply child :deactivate)
2191 child))
2192 ((widget-get type :inline)
2193 (widget-create-child-value
2194 widget type (cdr chosen)))
2196 (widget-create-child-value
2197 widget type (car (cdr chosen)))))))
2199 (error "Unknown escape `%c'" escape)))))
2200 ;; Update properties.
2201 (and button child (widget-put child :button button))
2202 (and button (widget-put widget :buttons (cons button buttons)))
2203 (and child (widget-put widget :children (cons child children))))))
2205 (defun widget-checklist-match (widget values)
2206 ;; All values must match a type in the checklist.
2207 (and (listp values)
2208 (null (cdr (widget-checklist-match-inline widget values)))))
2210 (defun widget-checklist-match-inline (widget values)
2211 ;; Find the values which match a type in the checklist.
2212 (let ((greedy (widget-get widget :greedy))
2213 (args (copy-sequence (widget-get widget :args)))
2214 found rest)
2215 (while values
2216 (let ((answer (widget-checklist-match-up args values)))
2217 (cond (answer
2218 (let ((vals (widget-match-inline answer values)))
2219 (setq found (append found (car vals))
2220 values (cdr vals)
2221 args (delq answer args))))
2222 (greedy
2223 (setq rest (append rest (list (car values)))
2224 values (cdr values)))
2226 (setq rest (append rest values)
2227 values nil)))))
2228 (cons found rest)))
2230 (defun widget-checklist-match-find (widget vals)
2231 ;; Find the vals which match a type in the checklist.
2232 ;; Return an alist of (TYPE MATCH).
2233 (let ((greedy (widget-get widget :greedy))
2234 (args (copy-sequence (widget-get widget :args)))
2235 found)
2236 (while vals
2237 (let ((answer (widget-checklist-match-up args vals)))
2238 (cond (answer
2239 (let ((match (widget-match-inline answer vals)))
2240 (setq found (cons (cons answer (car match)) found)
2241 vals (cdr match)
2242 args (delq answer args))))
2243 (greedy
2244 (setq vals (cdr vals)))
2246 (setq vals nil)))))
2247 found))
2249 (defun widget-checklist-match-up (args vals)
2250 ;; Rerturn the first type from ARGS that matches VALS.
2251 (let (current found)
2252 (while (and args (null found))
2253 (setq current (car args)
2254 args (cdr args)
2255 found (widget-match-inline current vals)))
2256 (if found
2257 current
2258 nil)))
2260 (defun widget-checklist-value-get (widget)
2261 ;; The values of all selected items.
2262 (let ((children (widget-get widget :children))
2263 child result)
2264 (while children
2265 (setq child (car children)
2266 children (cdr children))
2267 (if (widget-value (widget-get child :button))
2268 (setq result (append result (widget-apply child :value-inline)))))
2269 result))
2271 (defun widget-checklist-validate (widget)
2272 ;; Ticked chilren must be valid.
2273 (let ((children (widget-get widget :children))
2274 child button found)
2275 (while (and children (not found))
2276 (setq child (car children)
2277 children (cdr children)
2278 button (widget-get child :button)
2279 found (and (widget-value button)
2280 (widget-apply child :validate))))
2281 found))
2283 ;;; The `option' Widget
2285 (define-widget 'option 'checklist
2286 "An widget with an optional item."
2287 :inline t)
2289 ;;; The `choice-item' Widget.
2291 (define-widget 'choice-item 'item
2292 "Button items that delegate action events to their parents."
2293 :action 'widget-parent-action
2294 :format "%[%t%] \n")
2296 ;;; The `radio-button' Widget.
2298 (define-widget 'radio-button 'toggle
2299 "A radio button for use in the `radio' widget."
2300 :notify 'widget-radio-button-notify
2301 :format "%[%v%]"
2302 :button-suffix ""
2303 :button-prefix ""
2304 :on "(*)"
2305 :on-glyph "radio1"
2306 :off "( )"
2307 :off-glyph "radio0")
2309 (defun widget-radio-button-notify (widget child &optional event)
2310 ;; Tell daddy.
2311 (widget-apply (widget-get widget :parent) :action widget event))
2313 ;;; The `radio-button-choice' Widget.
2315 (define-widget 'radio-button-choice 'default
2316 "Select one of multiple options."
2317 :convert-widget 'widget-types-convert-widget
2318 :offset 4
2319 :format "%v"
2320 :entry-format "%b %v"
2321 :menu-tag "radio"
2322 :value-create 'widget-radio-value-create
2323 :value-delete 'widget-children-value-delete
2324 :value-get 'widget-radio-value-get
2325 :value-inline 'widget-radio-value-inline
2326 :value-set 'widget-radio-value-set
2327 :error "You must push one of the buttons"
2328 :validate 'widget-radio-validate
2329 :match 'widget-choice-match
2330 :match-inline 'widget-choice-match-inline
2331 :action 'widget-radio-action)
2333 (defun widget-radio-value-create (widget)
2334 ;; Insert all values
2335 (let ((args (widget-get widget :args))
2336 arg)
2337 (while args
2338 (setq arg (car args)
2339 args (cdr args))
2340 (widget-radio-add-item widget arg))))
2342 (defun widget-radio-add-item (widget type)
2343 "Add to radio widget WIDGET a new radio button item of type TYPE."
2344 ;; (setq type (widget-convert type))
2345 (and (eq (preceding-char) ?\n)
2346 (widget-get widget :indent)
2347 (insert-char ? (widget-get widget :indent)))
2348 (widget-specify-insert
2349 (let* ((value (widget-get widget :value))
2350 (children (widget-get widget :children))
2351 (buttons (widget-get widget :buttons))
2352 (button-args (or (widget-get type :sibling-args)
2353 (widget-get widget :button-args)))
2354 (from (point))
2355 (chosen (and (null (widget-get widget :choice))
2356 (widget-apply type :match value)))
2357 child button)
2358 (insert (widget-get widget :entry-format))
2359 (goto-char from)
2360 ;; Parse % escapes in format.
2361 (while (re-search-forward "%\\([bv%]\\)" nil t)
2362 (let ((escape (aref (match-string 1) 0)))
2363 (replace-match "" t t)
2364 (cond ((eq escape ?%)
2365 (insert "%"))
2366 ((eq escape ?b)
2367 (setq button (apply 'widget-create-child-and-convert
2368 widget 'radio-button
2369 :value (not (null chosen))
2370 button-args)))
2371 ((eq escape ?v)
2372 (setq child (if chosen
2373 (widget-create-child-value
2374 widget type value)
2375 (widget-create-child widget type)))
2376 (unless chosen
2377 (widget-apply child :deactivate)))
2379 (error "Unknown escape `%c'" escape)))))
2380 ;; Update properties.
2381 (when chosen
2382 (widget-put widget :choice type))
2383 (when button
2384 (widget-put child :button button)
2385 (widget-put widget :buttons (nconc buttons (list button))))
2386 (when child
2387 (widget-put widget :children (nconc children (list child))))
2388 child)))
2390 (defun widget-radio-value-get (widget)
2391 ;; Get value of the child widget.
2392 (let ((chosen (widget-radio-chosen widget)))
2393 (and chosen (widget-value chosen))))
2395 (defun widget-radio-chosen (widget)
2396 "Return the widget representing the chosen radio button."
2397 (let ((children (widget-get widget :children))
2398 current found)
2399 (while children
2400 (setq current (car children)
2401 children (cdr children))
2402 (let* ((button (widget-get current :button))
2403 (value (widget-apply button :value-get)))
2404 (when value
2405 (setq found current
2406 children nil))))
2407 found))
2409 (defun widget-radio-value-inline (widget)
2410 ;; Get value of the child widget.
2411 (let ((children (widget-get widget :children))
2412 current found)
2413 (while children
2414 (setq current (car children)
2415 children (cdr children))
2416 (let* ((button (widget-get current :button))
2417 (value (widget-apply button :value-get)))
2418 (when value
2419 (setq found (widget-apply current :value-inline)
2420 children nil))))
2421 found))
2423 (defun widget-radio-value-set (widget value)
2424 ;; We can't just delete and recreate a radio widget, since children
2425 ;; can be added after the original creation and won't be recreated
2426 ;; by `:create'.
2427 (let ((children (widget-get widget :children))
2428 current found)
2429 (while children
2430 (setq current (car children)
2431 children (cdr children))
2432 (let* ((button (widget-get current :button))
2433 (match (and (not found)
2434 (widget-apply current :match value))))
2435 (widget-value-set button match)
2436 (if match
2437 (progn
2438 (widget-value-set current value)
2439 (widget-apply current :activate))
2440 (widget-apply current :deactivate))
2441 (setq found (or found match))))))
2443 (defun widget-radio-validate (widget)
2444 ;; Valid if we have made a valid choice.
2445 (let ((children (widget-get widget :children))
2446 current found button)
2447 (while (and children (not found))
2448 (setq current (car children)
2449 children (cdr children)
2450 button (widget-get current :button)
2451 found (widget-apply button :value-get)))
2452 (if found
2453 (widget-apply current :validate)
2454 widget)))
2456 (defun widget-radio-action (widget child event)
2457 ;; Check if a radio button was pressed.
2458 (let ((children (widget-get widget :children))
2459 (buttons (widget-get widget :buttons))
2460 current)
2461 (when (memq child buttons)
2462 (while children
2463 (setq current (car children)
2464 children (cdr children))
2465 (let* ((button (widget-get current :button)))
2466 (cond ((eq child button)
2467 (widget-value-set button t)
2468 (widget-apply current :activate))
2469 ((widget-value button)
2470 (widget-value-set button nil)
2471 (widget-apply current :deactivate)))))))
2472 ;; Pass notification to parent.
2473 (widget-apply widget :notify child event))
2475 ;;; The `insert-button' Widget.
2477 (define-widget 'insert-button 'push-button
2478 "An insert button for the `editable-list' widget."
2479 :tag "INS"
2480 :help-echo "Insert a new item into the list at this position."
2481 :action 'widget-insert-button-action)
2483 (defun widget-insert-button-action (widget &optional event)
2484 ;; Ask the parent to insert a new item.
2485 (widget-apply (widget-get widget :parent)
2486 :insert-before (widget-get widget :widget)))
2488 ;;; The `delete-button' Widget.
2490 (define-widget 'delete-button 'push-button
2491 "A delete button for the `editable-list' widget."
2492 :tag "DEL"
2493 :help-echo "Delete this item from the list."
2494 :action 'widget-delete-button-action)
2496 (defun widget-delete-button-action (widget &optional event)
2497 ;; Ask the parent to insert a new item.
2498 (widget-apply (widget-get widget :parent)
2499 :delete-at (widget-get widget :widget)))
2501 ;;; The `editable-list' Widget.
2503 (defcustom widget-editable-list-gui nil
2504 "If non nil, use GUI push-buttons in editable list when available."
2505 :type 'boolean
2506 :group 'widgets)
2508 (define-widget 'editable-list 'default
2509 "A variable list of widgets of the same type."
2510 :convert-widget 'widget-types-convert-widget
2511 :offset 12
2512 :format "%v%i\n"
2513 :format-handler 'widget-editable-list-format-handler
2514 :entry-format "%i %d %v"
2515 :menu-tag "editable-list"
2516 :value-create 'widget-editable-list-value-create
2517 :value-delete 'widget-children-value-delete
2518 :value-get 'widget-editable-list-value-get
2519 :validate 'widget-children-validate
2520 :match 'widget-editable-list-match
2521 :match-inline 'widget-editable-list-match-inline
2522 :insert-before 'widget-editable-list-insert-before
2523 :delete-at 'widget-editable-list-delete-at)
2525 (defun widget-editable-list-format-handler (widget escape)
2526 ;; We recognize the insert button.
2527 (let ((widget-push-button-gui widget-editable-list-gui))
2528 (cond ((eq escape ?i)
2529 (and (widget-get widget :indent)
2530 (insert-char ? (widget-get widget :indent)))
2531 (apply 'widget-create-child-and-convert
2532 widget 'insert-button
2533 (widget-get widget :append-button-args)))
2535 (widget-default-format-handler widget escape)))))
2537 (defun widget-editable-list-value-create (widget)
2538 ;; Insert all values
2539 (let* ((value (widget-get widget :value))
2540 (type (nth 0 (widget-get widget :args)))
2541 (inlinep (widget-get type :inline))
2542 children)
2543 (widget-put widget :value-pos (copy-marker (point)))
2544 (set-marker-insertion-type (widget-get widget :value-pos) t)
2545 (while value
2546 (let ((answer (widget-match-inline type value)))
2547 (if answer
2548 (setq children (cons (widget-editable-list-entry-create
2549 widget
2550 (if inlinep
2551 (car answer)
2552 (car (car answer)))
2554 children)
2555 value (cdr answer))
2556 (setq value nil))))
2557 (widget-put widget :children (nreverse children))))
2559 (defun widget-editable-list-value-get (widget)
2560 ;; Get value of the child widget.
2561 (apply 'append (mapcar (lambda (child) (widget-apply child :value-inline))
2562 (widget-get widget :children))))
2564 (defun widget-editable-list-match (widget value)
2565 ;; Value must be a list and all the members must match the type.
2566 (and (listp value)
2567 (null (cdr (widget-editable-list-match-inline widget value)))))
2569 (defun widget-editable-list-match-inline (widget value)
2570 (let ((type (nth 0 (widget-get widget :args)))
2571 (ok t)
2572 found)
2573 (while (and value ok)
2574 (let ((answer (widget-match-inline type value)))
2575 (if answer
2576 (setq found (append found (car answer))
2577 value (cdr answer))
2578 (setq ok nil))))
2579 (cons found value)))
2581 (defun widget-editable-list-insert-before (widget before)
2582 ;; Insert a new child in the list of children.
2583 (save-excursion
2584 (let ((children (widget-get widget :children))
2585 (inhibit-read-only t)
2586 before-change-functions
2587 after-change-functions)
2588 (cond (before
2589 (goto-char (widget-get before :entry-from)))
2591 (goto-char (widget-get widget :value-pos))))
2592 (let ((child (widget-editable-list-entry-create
2593 widget nil nil)))
2594 (when (< (widget-get child :entry-from) (widget-get widget :from))
2595 (set-marker (widget-get widget :from)
2596 (widget-get child :entry-from)))
2597 (if (eq (car children) before)
2598 (widget-put widget :children (cons child children))
2599 (while (not (eq (car (cdr children)) before))
2600 (setq children (cdr children)))
2601 (setcdr children (cons child (cdr children)))))))
2602 (widget-setup)
2603 (widget-apply widget :notify widget))
2605 (defun widget-editable-list-delete-at (widget child)
2606 ;; Delete child from list of children.
2607 (save-excursion
2608 (let ((buttons (copy-sequence (widget-get widget :buttons)))
2609 button
2610 (inhibit-read-only t)
2611 before-change-functions
2612 after-change-functions)
2613 (while buttons
2614 (setq button (car buttons)
2615 buttons (cdr buttons))
2616 (when (eq (widget-get button :widget) child)
2617 (widget-put widget
2618 :buttons (delq button (widget-get widget :buttons)))
2619 (widget-delete button))))
2620 (let ((entry-from (widget-get child :entry-from))
2621 (entry-to (widget-get child :entry-to))
2622 (inhibit-read-only t)
2623 before-change-functions
2624 after-change-functions)
2625 (widget-delete child)
2626 (delete-region entry-from entry-to)
2627 (set-marker entry-from nil)
2628 (set-marker entry-to nil))
2629 (widget-put widget :children (delq child (widget-get widget :children))))
2630 (widget-setup)
2631 (widget-apply widget :notify widget))
2633 (defun widget-editable-list-entry-create (widget value conv)
2634 ;; Create a new entry to the list.
2635 (let ((type (nth 0 (widget-get widget :args)))
2636 (widget-push-button-gui widget-editable-list-gui)
2637 child delete insert)
2638 (widget-specify-insert
2639 (save-excursion
2640 (and (widget-get widget :indent)
2641 (insert-char ? (widget-get widget :indent)))
2642 (insert (widget-get widget :entry-format)))
2643 ;; Parse % escapes in format.
2644 (while (re-search-forward "%\\(.\\)" nil t)
2645 (let ((escape (aref (match-string 1) 0)))
2646 (replace-match "" t t)
2647 (cond ((eq escape ?%)
2648 (insert "%"))
2649 ((eq escape ?i)
2650 (setq insert (apply 'widget-create-child-and-convert
2651 widget 'insert-button
2652 (widget-get widget :insert-button-args))))
2653 ((eq escape ?d)
2654 (setq delete (apply 'widget-create-child-and-convert
2655 widget 'delete-button
2656 (widget-get widget :delete-button-args))))
2657 ((eq escape ?v)
2658 (if conv
2659 (setq child (widget-create-child-value
2660 widget type value))
2661 (setq child (widget-create-child-value
2662 widget type
2663 (widget-apply type :value-to-external
2664 (widget-default-get type))))))
2666 (error "Unknown escape `%c'" escape)))))
2667 (widget-put widget
2668 :buttons (cons delete
2669 (cons insert
2670 (widget-get widget :buttons))))
2671 (let ((entry-from (copy-marker (point-min)))
2672 (entry-to (copy-marker (point-max))))
2673 (set-marker-insertion-type entry-from t)
2674 (set-marker-insertion-type entry-to nil)
2675 (widget-put child :entry-from entry-from)
2676 (widget-put child :entry-to entry-to)))
2677 (widget-put insert :widget child)
2678 (widget-put delete :widget child)
2679 child))
2681 ;;; The `group' Widget.
2683 (define-widget 'group 'default
2684 "A widget which groups other widgets inside."
2685 :convert-widget 'widget-types-convert-widget
2686 :format "%v"
2687 :value-create 'widget-group-value-create
2688 :value-delete 'widget-children-value-delete
2689 :value-get 'widget-editable-list-value-get
2690 :default-get 'widget-group-default-get
2691 :validate 'widget-children-validate
2692 :match 'widget-group-match
2693 :match-inline 'widget-group-match-inline)
2695 (defun widget-group-value-create (widget)
2696 ;; Create each component.
2697 (let ((args (widget-get widget :args))
2698 (value (widget-get widget :value))
2699 arg answer children)
2700 (while args
2701 (setq arg (car args)
2702 args (cdr args)
2703 answer (widget-match-inline arg value)
2704 value (cdr answer))
2705 (and (eq (preceding-char) ?\n)
2706 (widget-get widget :indent)
2707 (insert-char ? (widget-get widget :indent)))
2708 (push (cond ((null answer)
2709 (widget-create-child widget arg))
2710 ((widget-get arg :inline)
2711 (widget-create-child-value widget arg (car answer)))
2713 (widget-create-child-value widget arg (car (car answer)))))
2714 children))
2715 (widget-put widget :children (nreverse children))))
2717 (defun widget-group-default-get (widget)
2718 ;; Get the default of the components.
2719 (mapcar 'widget-default-get (widget-get widget :args)))
2721 (defun widget-group-match (widget values)
2722 ;; Match if the components match.
2723 (and (listp values)
2724 (let ((match (widget-group-match-inline widget values)))
2725 (and match (null (cdr match))))))
2727 (defun widget-group-match-inline (widget vals)
2728 ;; Match if the components match.
2729 (let ((args (widget-get widget :args))
2730 argument answer found)
2731 (while args
2732 (setq argument (car args)
2733 args (cdr args)
2734 answer (widget-match-inline argument vals))
2735 (if answer
2736 (setq vals (cdr answer)
2737 found (append found (car answer)))
2738 (setq vals nil
2739 args nil)))
2740 (if answer
2741 (cons found vals)
2742 nil)))
2744 ;;; The `visibility' Widget.
2746 (define-widget 'visibility 'item
2747 "An indicator and manipulator for hidden items."
2748 :format "%[%v%]"
2749 :button-prefix ""
2750 :button-suffix ""
2751 :on "Hide"
2752 :off "Show"
2753 :value-create 'widget-visibility-value-create
2754 :action 'widget-toggle-action
2755 :match (lambda (widget value) t))
2757 (defun widget-visibility-value-create (widget)
2758 ;; Insert text representing the `on' and `off' states.
2759 (let ((on (widget-get widget :on))
2760 (off (widget-get widget :off)))
2761 (if on
2762 (setq on (concat widget-push-button-prefix
2764 widget-push-button-suffix))
2765 (setq on ""))
2766 (if off
2767 (setq off (concat widget-push-button-prefix
2769 widget-push-button-suffix))
2770 (setq off ""))
2771 (if (widget-value widget)
2772 (widget-glyph-insert widget on "down" "down-pushed")
2773 (widget-glyph-insert widget off "right" "right-pushed"))))
2775 ;;; The `documentation-link' Widget.
2777 ;; This is a helper widget for `documentation-string'.
2779 (define-widget 'documentation-link 'link
2780 "Link type used in documentation strings."
2781 :tab-order -1
2782 :help-echo 'widget-documentation-link-echo-help
2783 :action 'widget-documentation-link-action)
2785 (defun widget-documentation-link-echo-help (widget)
2786 "Tell what this link will describe."
2787 (concat "Describe the `" (widget-get widget :value) "' symbol."))
2789 (defun widget-documentation-link-action (widget &optional event)
2790 "Display documentation for WIDGET's value. Ignore optional argument EVENT."
2791 (let* ((string (widget-get widget :value))
2792 (symbol (intern string)))
2793 (if (and (fboundp symbol) (boundp symbol))
2794 ;; If there are two doc strings, give the user a way to pick one.
2795 (apropos (concat "\\`" (regexp-quote string) "\\'"))
2796 (if (fboundp symbol)
2797 (describe-function symbol)
2798 (describe-variable symbol)))))
2800 (defcustom widget-documentation-links t
2801 "Add hyperlinks to documentation strings when non-nil."
2802 :type 'boolean
2803 :group 'widget-documentation)
2805 (defcustom widget-documentation-link-regexp "`\\([^\n`' ]+\\)'"
2806 "Regexp for matching potential links in documentation strings.
2807 The first group should be the link itself."
2808 :type 'regexp
2809 :group 'widget-documentation)
2811 (defcustom widget-documentation-link-p 'intern-soft
2812 "Predicate used to test if a string is useful as a link.
2813 The value should be a function. The function will be called one
2814 argument, a string, and should return non-nil if there should be a
2815 link for that string."
2816 :type 'function
2817 :options '(widget-documentation-link-p)
2818 :group 'widget-documentation)
2820 (defcustom widget-documentation-link-type 'documentation-link
2821 "Widget type used for links in documentation strings."
2822 :type 'symbol
2823 :group 'widget-documentation)
2825 (defun widget-documentation-link-add (widget from to)
2826 (widget-specify-doc widget from to)
2827 (when widget-documentation-links
2828 (let ((regexp widget-documentation-link-regexp)
2829 (predicate widget-documentation-link-p)
2830 (type widget-documentation-link-type)
2831 (buttons (widget-get widget :buttons))
2832 (widget-mouse-face (default-value 'widget-mouse-face))
2833 (widget-button-face widget-documentation-face)
2834 (widget-button-pressed-face widget-documentation-face))
2835 (save-excursion
2836 (goto-char from)
2837 (while (re-search-forward regexp to t)
2838 (let ((name (match-string 1))
2839 (begin (match-beginning 1))
2840 (end (match-end 1)))
2841 (when (funcall predicate name)
2842 (push (widget-convert-button type begin end :value name)
2843 buttons)))))
2844 (widget-put widget :buttons buttons)))
2845 (let ((indent (widget-get widget :indent)))
2846 (when (and indent (not (zerop indent)))
2847 (save-excursion
2848 (save-restriction
2849 (narrow-to-region from to)
2850 (goto-char (point-min))
2851 (while (search-forward "\n" nil t)
2852 (insert-char ?\ indent)))))))
2854 ;;; The `documentation-string' Widget.
2856 (define-widget 'documentation-string 'item
2857 "A documentation string."
2858 :format "%v"
2859 :action 'widget-documentation-string-action
2860 :value-delete 'widget-children-value-delete
2861 :value-create 'widget-documentation-string-value-create)
2863 (defun widget-documentation-string-value-create (widget)
2864 ;; Insert documentation string.
2865 (let ((doc (widget-value widget))
2866 (indent (widget-get widget :indent))
2867 (shown (widget-get (widget-get widget :parent) :documentation-shown))
2868 (start (point)))
2869 (if (string-match "\n" doc)
2870 (let ((before (substring doc 0 (match-beginning 0)))
2871 (after (substring doc (match-beginning 0)))
2872 buttons)
2873 (insert before " ")
2874 (widget-documentation-link-add widget start (point))
2875 (push (widget-create-child-and-convert
2876 widget 'visibility
2877 :help-echo "Show or hide rest of the documentation."
2878 :off "More"
2879 :action 'widget-parent-action
2880 shown)
2881 buttons)
2882 (when shown
2883 (setq start (point))
2884 (when (and indent (not (zerop indent)))
2885 (insert-char ?\ indent))
2886 (insert after)
2887 (widget-documentation-link-add widget start (point)))
2888 (widget-put widget :buttons buttons))
2889 (insert doc)
2890 (widget-documentation-link-add widget start (point))))
2891 (insert "\n"))
2893 (defun widget-documentation-string-action (widget &rest ignore)
2894 ;; Toggle documentation.
2895 (let ((parent (widget-get widget :parent)))
2896 (widget-put parent :documentation-shown
2897 (not (widget-get parent :documentation-shown))))
2898 ;; Redraw.
2899 (widget-value-set widget (widget-value widget)))
2901 ;;; The Sexp Widgets.
2903 (define-widget 'const 'item
2904 "An immutable sexp."
2905 :prompt-value 'widget-const-prompt-value
2906 :format "%t\n%d")
2908 (defun widget-const-prompt-value (widget prompt value unbound)
2909 ;; Return the value of the const.
2910 (widget-value widget))
2912 (define-widget 'function-item 'const
2913 "An immutable function name."
2914 :format "%v\n%h"
2915 :documentation-property (lambda (symbol)
2916 (condition-case nil
2917 (documentation symbol t)
2918 (error nil))))
2920 (define-widget 'variable-item 'const
2921 "An immutable variable name."
2922 :format "%v\n%h"
2923 :documentation-property 'variable-documentation)
2925 (define-widget 'other 'sexp
2926 "Matches any value, but doesn't let the user edit the value.
2927 This is useful as last item in a `choice' widget.
2928 You should use this widget type with a default value,
2929 as in (other DEFAULT) or (other :tag \"NAME\" DEFAULT).
2930 If the user selects this alternative, that specifies DEFAULT
2931 as the value."
2932 :tag "Other"
2933 :format "%t%n"
2934 :value 'other)
2936 (defvar widget-string-prompt-value-history nil
2937 "History of input to `widget-string-prompt-value'.")
2939 (define-widget 'string 'editable-field
2940 "A string"
2941 :tag "String"
2942 :format "%{%t%}: %v"
2943 :complete-function 'ispell-complete-word
2944 :prompt-history 'widget-string-prompt-value-history)
2946 (define-widget 'regexp 'string
2947 "A regular expression."
2948 :match 'widget-regexp-match
2949 :validate 'widget-regexp-validate
2950 ;; Doesn't work well with terminating newline.
2951 ;; :value-face 'widget-single-line-field-face
2952 :tag "Regexp")
2954 (defun widget-regexp-match (widget value)
2955 ;; Match valid regexps.
2956 (and (stringp value)
2957 (condition-case nil
2958 (prog1 t
2959 (string-match value ""))
2960 (error nil))))
2962 (defun widget-regexp-validate (widget)
2963 "Check that the value of WIDGET is a valid regexp."
2964 (let ((val (widget-value widget)))
2965 (condition-case data
2966 (prog1 nil
2967 (string-match val ""))
2968 (error (widget-put widget :error (error-message-string data))
2969 widget))))
2971 (define-widget 'file 'string
2972 "A file widget.
2973 It will read a file name from the minibuffer when invoked."
2974 :complete-function 'widget-file-complete
2975 :prompt-value 'widget-file-prompt-value
2976 :format "%{%t%}: %v"
2977 ;; Doesn't work well with terminating newline.
2978 ;; :value-face 'widget-single-line-field-face
2979 :tag "File")
2981 (defun widget-file-complete ()
2982 "Perform completion on file name preceding point."
2983 (interactive)
2984 (let* ((end (point))
2985 (beg (save-excursion
2986 (skip-chars-backward "^ ")
2987 (point)))
2988 (pattern (buffer-substring beg end))
2989 (name-part (file-name-nondirectory pattern))
2990 (directory (file-name-directory pattern))
2991 (completion (file-name-completion name-part directory)))
2992 (cond ((eq completion t))
2993 ((null completion)
2994 (message "Can't find completion for \"%s\"" pattern)
2995 (ding))
2996 ((not (string= name-part completion))
2997 (delete-region beg end)
2998 (insert (expand-file-name completion directory)))
3000 (message "Making completion list...")
3001 (let ((list (file-name-all-completions name-part directory)))
3002 (setq list (sort list 'string<))
3003 (with-output-to-temp-buffer "*Completions*"
3004 (display-completion-list list)))
3005 (message "Making completion list...%s" "done")))))
3007 (defun widget-file-prompt-value (widget prompt value unbound)
3008 ;; Read file from minibuffer.
3009 (abbreviate-file-name
3010 (if unbound
3011 (read-file-name prompt)
3012 (let ((prompt2 (format "%s (default %s) " prompt value))
3013 (dir (file-name-directory value))
3014 (file (file-name-nondirectory value))
3015 (must-match (widget-get widget :must-match)))
3016 (read-file-name prompt2 dir nil must-match file)))))
3018 ;;;(defun widget-file-action (widget &optional event)
3019 ;;; ;; Read a file name from the minibuffer.
3020 ;;; (let* ((value (widget-value widget))
3021 ;;; (dir (file-name-directory value))
3022 ;;; (file (file-name-nondirectory value))
3023 ;;; (menu-tag (widget-apply widget :menu-tag-get))
3024 ;;; (must-match (widget-get widget :must-match))
3025 ;;; (answer (read-file-name (concat menu-tag ": (default `" value "') ")
3026 ;;; dir nil must-match file)))
3027 ;;; (widget-value-set widget (abbreviate-file-name answer))
3028 ;;; (widget-setup)
3029 ;;; (widget-apply widget :notify widget event)))
3031 (define-widget 'directory 'file
3032 "A directory widget.
3033 It will read a directory name from the minibuffer when invoked."
3034 :tag "Directory")
3036 (defvar widget-symbol-prompt-value-history nil
3037 "History of input to `widget-symbol-prompt-value'.")
3039 (define-widget 'symbol 'editable-field
3040 "A Lisp symbol."
3041 :value nil
3042 :tag "Symbol"
3043 :format "%{%t%}: %v"
3044 :match (lambda (widget value) (symbolp value))
3045 :complete-function 'lisp-complete-symbol
3046 :prompt-internal 'widget-symbol-prompt-internal
3047 :prompt-match 'symbolp
3048 :prompt-history 'widget-symbol-prompt-value-history
3049 :value-to-internal (lambda (widget value)
3050 (if (symbolp value)
3051 (symbol-name value)
3052 value))
3053 :value-to-external (lambda (widget value)
3054 (if (stringp value)
3055 (intern value)
3056 value)))
3058 (defun widget-symbol-prompt-internal (widget prompt initial history)
3059 ;; Read file from minibuffer.
3060 (let ((answer (completing-read prompt obarray
3061 (widget-get widget :prompt-match)
3062 nil initial history)))
3063 (if (and (stringp answer)
3064 (not (zerop (length answer))))
3065 answer
3066 (error "No value"))))
3068 (defvar widget-function-prompt-value-history nil
3069 "History of input to `widget-function-prompt-value'.")
3071 (define-widget 'function 'sexp
3072 "A Lisp function."
3073 :complete-function 'lisp-complete-symbol
3074 :prompt-value 'widget-field-prompt-value
3075 :prompt-internal 'widget-symbol-prompt-internal
3076 :prompt-match 'fboundp
3077 :prompt-history 'widget-function-prompt-value-history
3078 :action 'widget-field-action
3079 :tag "Function")
3081 (defvar widget-variable-prompt-value-history nil
3082 "History of input to `widget-variable-prompt-value'.")
3084 (define-widget 'variable 'symbol
3085 ;; Should complete on variables.
3086 "A Lisp variable."
3087 :prompt-match 'boundp
3088 :prompt-history 'widget-variable-prompt-value-history
3089 :tag "Variable")
3091 (defvar widget-coding-system-prompt-value-history nil
3092 "History of input to `widget-coding-system-prompt-value'.")
3094 (define-widget 'coding-system 'symbol
3095 "A MULE coding-system."
3096 :format "%{%t%}: %v"
3097 :tag "Coding system"
3098 :prompt-history 'widget-coding-system-prompt-value-history
3099 :prompt-value 'widget-coding-system-prompt-value
3100 :action 'widget-coding-system-action)
3102 (defun widget-coding-system-prompt-value (widget prompt value unbound)
3103 ;; Read coding-system from minibuffer.
3104 (intern
3105 (completing-read (format "%s (default %s) " prompt value)
3106 (mapcar (function
3107 (lambda (sym)
3108 (list (symbol-name sym))
3110 (coding-system-list)))))
3112 (defun widget-coding-system-action (widget &optional event)
3113 ;; Read a file name from the minibuffer.
3114 (let ((answer
3115 (widget-coding-system-prompt-value
3116 widget
3117 (widget-apply widget :menu-tag-get)
3118 (widget-value widget)
3119 t)))
3120 (widget-value-set widget answer)
3121 (widget-apply widget :notify widget event)
3122 (widget-setup)))
3124 (define-widget 'sexp 'editable-field
3125 "An arbitrary Lisp expression."
3126 :tag "Lisp expression"
3127 :format "%{%t%}: %v"
3128 :value nil
3129 :validate 'widget-sexp-validate
3130 :match (lambda (widget value) t)
3131 :value-to-internal 'widget-sexp-value-to-internal
3132 :value-to-external (lambda (widget value) (read value))
3133 :prompt-history 'widget-sexp-prompt-value-history
3134 :prompt-value 'widget-sexp-prompt-value)
3136 (defun widget-sexp-value-to-internal (widget value)
3137 ;; Use pp for printer representation.
3138 (let ((pp (if (symbolp value)
3139 (prin1-to-string value)
3140 (pp-to-string value))))
3141 (while (string-match "\n\\'" pp)
3142 (setq pp (substring pp 0 -1)))
3143 (if (or (string-match "\n\\'" pp)
3144 (> (length pp) 40))
3145 (concat "\n" pp)
3146 pp)))
3148 (defun widget-sexp-validate (widget)
3149 ;; Valid if we can read the string and there is no junk left after it.
3150 (save-excursion
3151 (let ((buffer (set-buffer (get-buffer-create " *Widget Scratch*"))))
3152 (erase-buffer)
3153 (insert (widget-apply widget :value-get))
3154 (goto-char (point-min))
3155 (condition-case data
3156 (let ((value (read buffer)))
3157 (if (eobp)
3158 (if (widget-apply widget :match value)
3160 (widget-put widget :error (widget-get widget :type-error))
3161 widget)
3162 (widget-put widget
3163 :error (format "Junk at end of expression: %s"
3164 (buffer-substring (point)
3165 (point-max))))
3166 widget))
3167 (error (widget-put widget :error (error-message-string data))
3168 widget)))))
3170 (defvar widget-sexp-prompt-value-history nil
3171 "History of input to `widget-sexp-prompt-value'.")
3173 (defun widget-sexp-prompt-value (widget prompt value unbound)
3174 ;; Read an arbitrary sexp.
3175 (let ((found (read-string prompt
3176 (if unbound nil (cons (prin1-to-string value) 0))
3177 (widget-get widget :prompt-history))))
3178 (save-excursion
3179 (let ((buffer (set-buffer (get-buffer-create " *Widget Scratch*"))))
3180 (erase-buffer)
3181 (insert found)
3182 (goto-char (point-min))
3183 (let ((answer (read buffer)))
3184 (unless (eobp)
3185 (error "Junk at end of expression: %s"
3186 (buffer-substring (point) (point-max))))
3187 answer)))))
3189 (define-widget 'restricted-sexp 'sexp
3190 "A Lisp expression restricted to values that match.
3191 To use this type, you must define :match or :match-alternatives."
3192 :type-error "The specified value is not valid"
3193 :match 'widget-restricted-sexp-match
3194 :value-to-internal (lambda (widget value)
3195 (if (widget-apply widget :match value)
3196 (prin1-to-string value)
3197 value)))
3199 (defun widget-restricted-sexp-match (widget value)
3200 (let ((alternatives (widget-get widget :match-alternatives))
3201 matched)
3202 (while (and alternatives (not matched))
3203 (if (cond ((functionp (car alternatives))
3204 (funcall (car alternatives) value))
3205 ((and (consp (car alternatives))
3206 (eq (car (car alternatives)) 'quote))
3207 (eq value (nth 1 (car alternatives)))))
3208 (setq matched t))
3209 (setq alternatives (cdr alternatives)))
3210 matched))
3212 (define-widget 'integer 'restricted-sexp
3213 "An integer."
3214 :tag "Integer"
3215 :value 0
3216 :type-error "This field should contain an integer"
3217 :match-alternatives '(integerp))
3219 (define-widget 'number 'restricted-sexp
3220 "A floating point number."
3221 :tag "Number"
3222 :value 0.0
3223 :type-error "This field should contain a number"
3224 :match-alternatives '(numberp))
3226 (define-widget 'character 'editable-field
3227 "A character."
3228 :tag "Character"
3229 :value 0
3230 :size 1
3231 :format "%{%t%}: %v\n"
3232 :valid-regexp "\\`.\\'"
3233 :error "This field should contain a single character"
3234 :value-to-internal (lambda (widget value)
3235 (if (stringp value)
3236 value
3237 (char-to-string value)))
3238 :value-to-external (lambda (widget value)
3239 (if (stringp value)
3240 (aref value 0)
3241 value))
3242 :match (lambda (widget value)
3243 (if (fboundp 'characterp)
3244 (characterp value)
3245 (integerp value))))
3247 (define-widget 'list 'group
3248 "A Lisp list."
3249 :tag "List"
3250 :format "%{%t%}:\n%v")
3252 (define-widget 'vector 'group
3253 "A Lisp vector."
3254 :tag "Vector"
3255 :format "%{%t%}:\n%v"
3256 :match 'widget-vector-match
3257 :value-to-internal (lambda (widget value) (append value nil))
3258 :value-to-external (lambda (widget value) (apply 'vector value)))
3260 (defun widget-vector-match (widget value)
3261 (and (vectorp value)
3262 (widget-group-match widget
3263 (widget-apply widget :value-to-internal value))))
3265 (define-widget 'cons 'group
3266 "A cons-cell."
3267 :tag "Cons-cell"
3268 :format "%{%t%}:\n%v"
3269 :match 'widget-cons-match
3270 :value-to-internal (lambda (widget value)
3271 (list (car value) (cdr value)))
3272 :value-to-external (lambda (widget value)
3273 (cons (nth 0 value) (nth 1 value))))
3275 (defun widget-cons-match (widget value)
3276 (and (consp value)
3277 (widget-group-match widget
3278 (widget-apply widget :value-to-internal value))))
3280 ;;; The `plist' Widget.
3282 ;; Property lists.
3284 (define-widget 'plist 'list
3285 "A property list."
3286 :key-type '(symbol :tag "Key")
3287 :value-type '(sexp :tag "Value")
3288 :convert-widget 'widget-plist-convert-widget
3289 :tag "Plist")
3291 (defvar widget-plist-value-type) ;Dynamic variable
3293 (defun widget-plist-convert-widget (widget)
3294 ;; Handle `:options'.
3295 (let* ((options (widget-get widget :options))
3296 (key-type (widget-get widget :key-type))
3297 (widget-plist-value-type (widget-get widget :value-type))
3298 (other `(editable-list :inline t
3299 (group :inline t
3300 ,key-type
3301 ,widget-plist-value-type)))
3302 (args (if options
3303 (list `(checklist :inline t
3304 :greedy t
3305 ,@(mapcar 'widget-plist-convert-option
3306 options))
3307 other)
3308 (list other))))
3309 (widget-put widget :args args)
3310 widget))
3312 (defun widget-plist-convert-option (option)
3313 ;; Convert a single plist option.
3314 (let (key-type value-type)
3315 (if (listp option)
3316 (let ((key (nth 0 option)))
3317 (setq value-type (nth 1 option))
3318 (if (listp key)
3319 (setq key-type key)
3320 (setq key-type `(const ,key))))
3321 (setq key-type `(const ,option)
3322 value-type widget-plist-value-type))
3323 `(group :format "Key: %v" :inline t ,key-type ,value-type)))
3326 ;;; The `alist' Widget.
3328 ;; Association lists.
3330 (define-widget 'alist 'list
3331 "An association list."
3332 :key-type '(sexp :tag "Key")
3333 :value-type '(sexp :tag "Value")
3334 :convert-widget 'widget-alist-convert-widget
3335 :tag "Alist")
3337 (defvar widget-alist-value-type) ;Dynamic variable
3339 (defun widget-alist-convert-widget (widget)
3340 ;; Handle `:options'.
3341 (let* ((options (widget-get widget :options))
3342 (key-type (widget-get widget :key-type))
3343 (widget-alist-value-type (widget-get widget :value-type))
3344 (other `(editable-list :inline t
3345 (cons :format "%v"
3346 ,key-type
3347 ,widget-alist-value-type)))
3348 (args (if options
3349 (list `(checklist :inline t
3350 :greedy t
3351 ,@(mapcar 'widget-alist-convert-option
3352 options))
3353 other)
3354 (list other))))
3355 (widget-put widget :args args)
3356 widget))
3358 (defun widget-alist-convert-option (option)
3359 ;; Convert a single alist option.
3360 (let (key-type value-type)
3361 (if (listp option)
3362 (let ((key (nth 0 option)))
3363 (setq value-type (nth 1 option))
3364 (if (listp key)
3365 (setq key-type key)
3366 (setq key-type `(const ,key))))
3367 (setq key-type `(const ,option)
3368 value-type widget-alist-value-type))
3369 `(cons :format "Key: %v" ,key-type ,value-type)))
3371 (define-widget 'choice 'menu-choice
3372 "A union of several sexp types."
3373 :tag "Choice"
3374 :format "%{%t%}: %[Value Menu%] %v"
3375 :button-prefix 'widget-push-button-prefix
3376 :button-suffix 'widget-push-button-suffix
3377 :prompt-value 'widget-choice-prompt-value)
3379 (defun widget-choice-prompt-value (widget prompt value unbound)
3380 "Make a choice."
3381 (let ((args (widget-get widget :args))
3382 (completion-ignore-case (widget-get widget :case-fold))
3383 current choices old)
3384 ;; Find the first arg that match VALUE.
3385 (let ((look args))
3386 (while look
3387 (if (widget-apply (car look) :match value)
3388 (setq old (car look)
3389 look nil)
3390 (setq look (cdr look)))))
3391 ;; Find new choice.
3392 (setq current
3393 (cond ((= (length args) 0)
3394 nil)
3395 ((= (length args) 1)
3396 (nth 0 args))
3397 ((and (= (length args) 2)
3398 (memq old args))
3399 (if (eq old (nth 0 args))
3400 (nth 1 args)
3401 (nth 0 args)))
3403 (while args
3404 (setq current (car args)
3405 args (cdr args))
3406 (setq choices
3407 (cons (cons (widget-apply current :menu-tag-get)
3408 current)
3409 choices)))
3410 (let ((val (completing-read prompt choices nil t)))
3411 (if (stringp val)
3412 (let ((try (try-completion val choices)))
3413 (when (stringp try)
3414 (setq val try))
3415 (cdr (assoc val choices)))
3416 nil)))))
3417 (if current
3418 (widget-prompt-value current prompt nil t)
3419 value)))
3421 (define-widget 'radio 'radio-button-choice
3422 "A union of several sexp types."
3423 :tag "Choice"
3424 :format "%{%t%}:\n%v"
3425 :prompt-value 'widget-choice-prompt-value)
3427 (define-widget 'repeat 'editable-list
3428 "A variable length homogeneous list."
3429 :tag "Repeat"
3430 :format "%{%t%}:\n%v%i\n")
3432 (define-widget 'set 'checklist
3433 "A list of members from a fixed set."
3434 :tag "Set"
3435 :format "%{%t%}:\n%v")
3437 (define-widget 'boolean 'toggle
3438 "To be nil or non-nil, that is the question."
3439 :tag "Boolean"
3440 :prompt-value 'widget-boolean-prompt-value
3441 :button-prefix 'widget-push-button-prefix
3442 :button-suffix 'widget-push-button-suffix
3443 :format "%{%t%}: %[Toggle%] %v\n"
3444 :on "on (non-nil)"
3445 :off "off (nil)")
3447 (defun widget-boolean-prompt-value (widget prompt value unbound)
3448 ;; Toggle a boolean.
3449 (y-or-n-p prompt))
3451 ;;; The `color' Widget.
3453 (define-widget 'color 'editable-field
3454 "Choose a color name (with sample)."
3455 :format "%t: %v (%{sample%})\n"
3456 :size 10
3457 :tag "Color"
3458 :value "black"
3459 :complete 'widget-color-complete
3460 :sample-face-get 'widget-color-sample-face-get
3461 :notify 'widget-color-notify
3462 :action 'widget-color-action)
3464 (defun widget-color-complete (widget)
3465 "Complete the color in WIDGET."
3466 (let* ((prefix (buffer-substring-no-properties (widget-field-start widget)
3467 (point)))
3468 (list (widget-color-choice-list))
3469 (completion (try-completion prefix list)))
3470 (cond ((eq completion t)
3471 (message "Exact match."))
3472 ((null completion)
3473 (error "Can't find completion for \"%s\"" prefix))
3474 ((not (string-equal prefix completion))
3475 (insert-and-inherit (substring completion (length prefix))))
3477 (message "Making completion list...")
3478 (let ((list (all-completions prefix list nil)))
3479 (with-output-to-temp-buffer "*Completions*"
3480 (display-completion-list list)))
3481 (message "Making completion list...done")))))
3483 (defun widget-color-sample-face-get (widget)
3484 (let* ((value (condition-case nil
3485 (widget-value widget)
3486 (error (widget-get widget :value))))
3487 (symbol (intern (concat "fg:" value))))
3488 (condition-case nil
3489 (facemenu-get-face symbol)
3490 (error 'default))))
3492 (defvar widget-color-choice-list nil)
3493 ;; Variable holding the possible colors.
3495 (defun widget-color-choice-list ()
3496 (unless widget-color-choice-list
3497 (setq widget-color-choice-list
3498 (mapcar '(lambda (color) (list color))
3499 (x-defined-colors))))
3500 widget-color-choice-list)
3502 (defvar widget-color-history nil
3503 "History of entered colors")
3505 (defun widget-color-action (widget &optional event)
3506 ;; Prompt for a color.
3507 (let* ((tag (widget-apply widget :menu-tag-get))
3508 (prompt (concat tag ": "))
3509 (value (widget-value widget))
3510 (start (widget-field-start widget))
3511 (pos (cond ((< (point) start)
3513 ((> (point) (+ start (length value)))
3514 (length value))
3516 (- (point) start))))
3517 (answer (if (commandp 'read-color)
3518 (read-color prompt)
3519 (completing-read (concat tag ": ")
3520 (widget-color-choice-list)
3521 nil nil
3522 (cons value pos)
3523 'widget-color-history))))
3524 (unless (zerop (length answer))
3525 (widget-value-set widget answer)
3526 (widget-setup)
3527 (widget-apply widget :notify widget event))))
3529 (defun widget-color-notify (widget child &optional event)
3530 "Update the sample, and notofy the parent."
3531 (overlay-put (widget-get widget :sample-overlay)
3532 'face (widget-apply widget :sample-face-get))
3533 (widget-default-notify widget child event))
3535 ;;; The Help Echo
3537 (defun widget-at (pos)
3538 "The button or field at POS."
3539 (or (get-char-property pos 'button)
3540 (get-char-property pos 'field)))
3542 (defun widget-echo-help (pos)
3543 "Display the help echo for widget at POS."
3544 (let* ((widget (widget-at pos))
3545 (help-echo (and widget (widget-get widget :help-echo))))
3546 (cond ((stringp help-echo)
3547 (message "%s" help-echo))
3548 ((and (symbolp help-echo) (fboundp help-echo)
3549 (stringp (setq help-echo (funcall help-echo widget))))
3550 (message "%s" help-echo)))))
3552 ;;; The End:
3554 (provide 'wid-edit)
3556 ;; wid-edit.el ends here