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