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