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