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