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