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