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