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