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