1 ;;; wid-edit.el --- Functions for creating and using widgets -*-byte-compile-dynamic: t;-*-
3 ;; Copyright (C) 1996, 1997, 1999, 2000, 2001, 2002, 2003,
4 ;; 2004, 2005, 2006 Free Software Foundation, Inc.
6 ;; Author: Per Abrahamsen <abraham@dina.kvl.dk>
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)
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
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.]
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."
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
)))))
79 "Customization support for the Widget Library."
80 :link
'(custom-manual "(widget)Top")
81 :link
'(emacs-library-link :tag
"Lisp File" "widget.el")
86 (defgroup widget-documentation nil
87 "Options controling the display of documentation strings."
90 (defgroup widget-faces nil
91 "Faces used by the widget library."
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
)
101 (:foreground
"lime green"))
104 (:foreground
"dark green"))
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."
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"
133 (((class grayscale color
)
135 :background
"gray85")
136 (((class grayscale color
)
138 :background
"dim gray")
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
))
149 (((class grayscale color
)
151 :background
"gray85")
152 (((class grayscale color
)
154 :background
"dim gray")
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")
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
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."
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."
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."
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
223 (cond ((and (< (length items
) widget-menu-max-size
)
224 event
(display-popup-menus-p))
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
)))
234 (let ((try (try-completion val items
)))
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))
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")
249 (insert "Available choices:\n\n")
251 (setq choice
(car items
) items
(cdr items
))
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)
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))
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)))
290 (scroll-other-window-down
291 (window-height minibuffer-scroll-window))
292 (scroll-other-window))
294 ((eq value 'negative-argument)
298 (when (eq value 'keyboard-quit)
302 (defun widget-remove-if (predictate list)
303 (let (result (tail list))
305 (or (funcall predictate (car tail))
306 (setq result (cons (car tail) result)))
307 (setq tail (cdr tail)))
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
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
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.
333 (cond ((null (widget-get widget :size))
335 (widget-field-add-space
336 (insert-and-inherit " ")))
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))
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
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))
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)))
379 (let ((begin (widget-field-start field))
380 (end (widget-field-end field)))
382 (while (and (> end begin)
383 (eq (char-after (1- end)) ?\s))
384 (setq end (1- 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)
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."
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)))
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."
484 (defun widgetp (widget)
485 "Return non-nil iff WIDGET is a widget."
487 (get widget 'widget-type)
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)))
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)
506 (widget-member (get (car widget) 'widget-type) property))
509 (defun widget-value (widget)
510 "Extract the current value of 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."
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))
531 (widget-apply widget :match (car vals)))
532 (cons (list (car vals)) (cdr vals)))
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.
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)))
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))
565 (setq child (car children)
566 children (cdr children))
567 (when (eq (widget-get child :button) widget)
568 (throw 'child child)))
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),
579 (let ((cur (point-min))
582 (with-current-buffer buffer (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)))))
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
600 (defcustom widget-image-enable t
601 "If non nil, use image buttons in widgets when available."
606 (defcustom widget-image-conversion
607 '((xpm ".xpm") (gif ".gif") (png ".png") (jpeg ".jpg" ".jpeg")
609 "Conversion alist from image formats to file name suffixes."
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.
625 (eq 'image (car image)))
626 ;; Already an image spec. Use it.
629 ;; A string. Look it up in relevant directories.
630 (let* ((load-path (cons widget-image-directory load-path))
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))
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
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)
658 tag 'mouse-face widget-button-pressed-face)))
661 (defun widget-move-and-invoke (event)
662 "Move to where you click, and if it is an active field, invoke it."
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))))
671 (defgroup widget-button nil
672 "The look of various kinds of buttons."
675 (defcustom widget-button-prefix ""
676 "String used as prefix for buttons."
678 :group 'widget-button)
680 (defcustom widget-button-suffix ""
681 "String used as suffix for buttons."
683 :group 'widget-button)
685 ;;; Creating Widgets.
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)
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)
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)
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)
731 (defun widget-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)
745 (copy-sequence type)))
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.
756 (if (eq (cadr current) :args)
757 ;; If :args is explicitly specified, obey it.
759 ;; Some other irrelevant keyword.
760 (setq current (cdr (cdr current))))
761 (setcdr current (list :args (cdr current)))
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)
768 ((keywordp (car args))
769 (setq args (cddr args)))
772 (widget-put widget :args args))
773 ;; Then Convert the widget.
776 (let ((convert-widget (plist-get (cdr type) :convert-widget)))
778 (setq widget (funcall convert-widget widget))))
779 (setq type (get (car type) 'widget-type)))
780 ;; Finally set the keyword args.
782 (let ((next (nth 0 keys)))
785 (widget-put widget next (nth 1 keys))
786 (setq keys (nthcdr 2 keys)))
788 ;; Convert the :value to internal format.
789 (if (widget-member widget :value)
791 :value (widget-apply widget
793 (widget-get widget :value))))
794 ;; Return the newly create widget.
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
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
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)
821 (widget-specify-button widget button-from button-to))
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
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)
841 (delete-overlay button))
843 (delete-overlay sample))
845 (delete-overlay doc))
847 (delete-overlay field))
848 (mapc 'widget-leave-text (widget-get widget :children))))
850 ;;; Keymap and Commands.
853 (defalias 'advertised-widget-backward 'widget-backward)
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)
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)
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)
892 "Keymap used inside a text field.")
894 (defun widget-field-activate (pos &optional event)
895 "Invoke the editable field at point."
897 (let ((field (widget-field-at pos)))
899 (widget-apply-action field event)
901 (lookup-key widget-global-map (this-command-keys))))))
903 (defface widget-button-pressed
904 '((((min-colors 88) (class color))
905 (:foreground "red1"))
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 (defun widget-button-click (event)
916 "Invoke the button that the mouse is pointing at."
918 (if (widget-event-point event)
919 (let* ((oevent event)
920 (mouse-1 (memq (event-basic-type event) '(mouse-1 down-mouse-1)))
921 (pos (widget-event-point event))
922 (start (event-start event))
923 (button (get-char-property
924 pos 'button (and (windowp (posn-window start))
925 (window-buffer (posn-window start))))))
926 (when (or (null button)
927 (catch 'button-press-cancelled
928 ;; Mouse click on a widget button. Do the following
929 ;; in a save-excursion so that the click on the button
930 ;; doesn't change point.
931 (save-selected-window
932 (select-window (posn-window (event-start event)))
934 (goto-char (posn-point (event-start event)))
935 (let* ((overlay (widget-get button :button-overlay))
936 (pressed-face (or (widget-get button :pressed-face)
937 widget-button-pressed-face))
938 (face (overlay-get overlay 'face))
939 (mouse-face (overlay-get overlay 'mouse-face)))
941 ;; Read events, including mouse-movement
942 ;; events, waiting for a release event. If we
943 ;; began with a mouse-1 event and receive a
944 ;; movement event, that means the user wants
945 ;; to perform drag-selection, so cancel the
946 ;; button press and do the default mouse-1
947 ;; action. For mouse-2, just highlight/
948 ;; unhighlight the button the mouse was
949 ;; initially on when we move over it.
951 (when face ; avoid changing around image
952 (overlay-put overlay 'face pressed-face)
953 (overlay-put overlay 'mouse-face pressed-face))
954 (unless (widget-apply button :mouse-down-action event)
955 (let ((track-mouse t))
956 (while (not (widget-button-release-event-p event))
957 (setq event (read-event))
958 (when (and mouse-1 (mouse-movement-p event))
959 (push event unread-command-events)
961 (throw 'button-press-cancelled t))
962 (setq pos (widget-event-point event))
964 (eq (get-char-property pos 'button)
967 (overlay-put overlay 'face pressed-face)
968 (overlay-put overlay 'mouse-face pressed-face))
969 (overlay-put overlay 'face face)
970 (overlay-put overlay 'mouse-face mouse-face)))))
972 ;; When mouse is released over the button, run
973 ;; its action function.
975 (eq (get-char-property pos 'button) button))
976 (widget-apply-action button event)))
977 (overlay-put overlay 'face face)
978 (overlay-put overlay 'mouse-face mouse-face))))
980 ;; This loses if the widget action switches windows. -- cyd
981 ;; (unless (pos-visible-in-window-p (widget-event-point event))
982 ;; (mouse-set-point event)
983 ;; (beginning-of-line)
987 (let ((up t) command)
988 ;; Mouse click not on a widget button. Find the global
989 ;; command to run, and check whether it is bound to an
992 (cond ((setq command ;down event
993 (lookup-key widget-global-map [down-mouse-1]))
995 ((setq command ;up event
996 (lookup-key widget-global-map [mouse-1]))))
997 (cond ((setq command ;down event
998 (lookup-key widget-global-map [down-mouse-2]))
1000 ((setq command ;up event
1001 (lookup-key widget-global-map [mouse-2])))))
1003 ;; Don't execute up events twice.
1004 (while (not (widget-button-release-event-p event))
1005 (setq event (read-event))))
1007 (call-interactively command)))))
1008 (message "You clicked somewhere weird.")))
1010 (defun widget-button-press (pos &optional event)
1011 "Invoke button at POS."
1013 (let ((button (get-char-property pos 'button)))
1015 (widget-apply-action button event)
1016 (let ((command (lookup-key widget-global-map (this-command-keys))))
1017 (when (commandp command)
1018 (call-interactively command))))))
1020 (defun widget-tabable-at (&optional pos)
1021 "Return the tabable widget at POS, or nil.
1022 POS defaults to the value of (point)."
1023 (let ((widget (widget-at pos)))
1025 (let ((order (widget-get widget :tab-order)))
1031 (defvar widget-use-overlay-change t
1032 "If non-nil, use overlay change functions to tab around in the buffer.
1033 This is much faster, but doesn't work reliably on Emacs 19.34.")
1035 (defun widget-move (arg)
1036 "Move point to the ARG next field or button.
1037 ARG may be negative to move backward."
1038 (or (bobp) (> arg 0) (backward-char))
1041 (old (widget-tabable-at)))
1045 (goto-char (point-min))
1046 (setq wrapped (1+ wrapped)))
1047 (widget-use-overlay-change
1048 (goto-char (next-overlay-change (point))))
1053 (error "No buttons or fields found"))
1054 (let ((new (widget-tabable-at)))
1056 (unless (eq new old)
1062 (goto-char (point-max))
1063 (setq wrapped (1+ wrapped)))
1064 (widget-use-overlay-change
1065 (goto-char (previous-overlay-change (point))))
1070 (error "No buttons or fields found"))
1071 (let ((new (widget-tabable-at)))
1073 (unless (eq new old)
1074 (setq arg (1+ arg))))))
1075 (let ((new (widget-tabable-at)))
1076 (while (eq (widget-tabable-at) new)
1079 (widget-echo-help (point))
1080 (run-hooks 'widget-move-hook))
1082 (defun widget-forward (arg)
1083 "Move point to the next field or button.
1084 With optional ARG, move across that many fields."
1086 (run-hooks 'widget-forward-hook)
1089 (defun widget-backward (arg)
1090 "Move point to the previous field or button.
1091 With optional ARG, move across that many fields."
1093 (run-hooks 'widget-backward-hook)
1094 (widget-move (- arg)))
1096 ;; Since the widget code uses a `field' property to identify fields,
1097 ;; ordinary beginning-of-line does the right thing.
1098 (defalias 'widget-beginning-of-line 'beginning-of-line)
1100 (defun widget-end-of-line ()
1101 "Go to end of field or end of line, whichever is first.
1102 Trailing spaces at the end of padded fields are not considered part of
1105 ;; Ordinary end-of-line does the right thing, because we're inside
1106 ;; text with a `field' property.
1109 ;; ... except that we want to ignore trailing spaces in fields that
1110 ;; aren't terminated by a newline, because they are used as padding,
1111 ;; and ignored when extracting the entered value of the field.
1112 (skip-chars-backward " " (field-beginning (1- (point))))))
1114 (defun widget-kill-line ()
1115 "Kill to end of field or end of line, whichever is first."
1117 (let* ((field (widget-field-find (point)))
1118 (end (and field (widget-field-end field))))
1119 (if (and field (> (line-beginning-position 2) end))
1120 (kill-region (point) end)
1121 (call-interactively 'kill-line))))
1123 (defcustom widget-complete-field (lookup-key global-map "\M-\t")
1124 "Default function to call for completion inside fields."
1125 :options '(ispell-complete-word complete-tag lisp-complete-symbol)
1129 (defun widget-narrow-to-field ()
1132 (let ((field (widget-field-find (point))))
1134 (narrow-to-region (line-beginning-position) (line-end-position)))))
1136 (defun widget-complete ()
1137 "Complete content of editable field from point.
1138 When not inside a field, move to the previous button or field."
1140 (let ((field (widget-field-find (point))))
1143 (widget-narrow-to-field)
1144 (widget-apply field :complete))
1145 (error "Not in an editable field"))))
1147 ;;; Setting up the buffer.
1149 (defvar widget-field-new nil
1150 "List of all newly created editable fields in the buffer.")
1151 (make-variable-buffer-local 'widget-field-new)
1153 (defvar widget-field-list nil
1154 "List of all editable fields in the buffer.")
1155 (make-variable-buffer-local 'widget-field-list)
1157 (defun widget-at (&optional pos)
1158 "The button or field at POS (default, point)."
1159 (or (get-char-property (or pos (point)) 'button)
1160 (widget-field-at pos)))
1163 (defun widget-setup ()
1164 "Setup current buffer so editing string widgets works."
1165 (let ((inhibit-read-only t)
1166 (inhibit-modification-hooks t)
1168 (while widget-field-new
1169 (setq field (car widget-field-new)
1170 widget-field-new (cdr widget-field-new)
1171 widget-field-list (cons field widget-field-list))
1172 (let ((from (car (widget-get field :field-overlay)))
1173 (to (cdr (widget-get field :field-overlay))))
1174 (widget-specify-field field
1175 (marker-position from) (marker-position to))
1176 (set-marker from nil)
1177 (set-marker to nil))))
1179 (widget-add-change))
1181 (defvar widget-field-last nil)
1182 ;; Last field containing point.
1183 (make-variable-buffer-local 'widget-field-last)
1185 (defvar widget-field-was nil)
1186 ;; The widget data before the change.
1187 (make-variable-buffer-local 'widget-field-was)
1189 (defun widget-field-at (pos)
1190 "Return the widget field at POS, or nil if none."
1191 (let ((field (get-char-property (or pos (point)) 'field)))
1192 (if (eq field 'boundary)
1193 (get-char-property (or pos (point)) 'real-field)
1196 (defun widget-field-buffer (widget)
1197 "Return the buffer of WIDGET's editing field."
1198 (let ((overlay (widget-get widget :field-overlay)))
1199 (cond ((overlayp overlay)
1200 (overlay-buffer overlay))
1202 (marker-buffer (car overlay))))))
1204 (defun widget-field-start (widget)
1205 "Return the start of WIDGET's editing field."
1206 (let ((overlay (widget-get widget :field-overlay)))
1207 (if (overlayp overlay)
1208 (overlay-start overlay)
1211 (defun widget-field-end (widget)
1212 "Return the end of WIDGET's editing field."
1213 (let ((overlay (widget-get widget :field-overlay)))
1214 ;; Don't subtract one if local-map works at the end of the overlay,
1215 ;; or if a special `boundary' field has been added after the widget
1217 (if (overlayp overlay)
1218 ;; Don't proceed if overlay has been removed from buffer.
1219 (when (overlay-buffer overlay)
1220 (if (and (not (eq (with-current-buffer
1221 (widget-field-buffer widget)
1223 ;; `widget-narrow-to-field' can be
1224 ;; active when this function is called
1225 ;; from an change-functions hook. So
1226 ;; temporarily remove field narrowing
1227 ;; before to call `get-char-property'.
1229 (get-char-property (overlay-end overlay)
1232 (or widget-field-add-space
1233 (null (widget-get widget :size))))
1234 (1- (overlay-end overlay))
1235 (overlay-end overlay)))
1238 (defun widget-field-find (pos)
1239 "Return the field at POS.
1240 Unlike (get-char-property POS 'field), this works with empty fields too."
1241 (let ((fields widget-field-list)
1244 (setq field (car fields)
1245 fields (cdr fields))
1246 (when (and (<= (widget-field-start field) pos)
1247 (<= pos (widget-field-end field)))
1249 (error "Overlapping fields"))
1250 (setq found field)))
1253 (defun widget-before-change (from to)
1254 ;; This is how, for example, a variable changes its state to `modified'.
1255 ;; when it is being edited.
1256 (unless inhibit-read-only
1257 (let ((from-field (widget-field-find from))
1258 (to-field (widget-field-find to)))
1259 (cond ((not (eq from-field to-field))
1260 (add-hook 'post-command-hook 'widget-add-change nil t)
1261 (signal 'text-read-only
1262 '("Change should be restricted to a single field")))
1264 (add-hook 'post-command-hook 'widget-add-change nil t)
1265 (signal 'text-read-only
1266 '("Attempt to change text outside editable field")))
1267 (widget-field-use-before-change
1268 (widget-apply from-field :notify from-field))))))
1270 (defun widget-add-change ()
1271 (remove-hook 'post-command-hook 'widget-add-change t)
1272 (add-hook 'before-change-functions 'widget-before-change nil t)
1273 (add-hook 'after-change-functions 'widget-after-change nil t))
1275 (defun widget-after-change (from to old)
1276 "Adjust field size and text properties."
1277 (let ((field (widget-field-find from))
1278 (other (widget-field-find to)))
1280 (unless (eq field other)
1281 (error "Change in different fields"))
1282 (let ((size (widget-get field :size)))
1284 (let ((begin (widget-field-start field))
1285 (end (widget-field-end field)))
1286 (cond ((< (- end begin) size)
1290 (insert-char ?\s (- (+ begin size) end))))
1291 ((> (- end begin) size)
1292 ;; Field too large and
1293 (if (or (< (point) (+ begin size))
1295 ;; Point is outside extra space.
1296 (setq begin (+ begin size))
1297 ;; Point is within the extra space.
1298 (setq begin (point)))
1301 (while (and (eq (preceding-char) ?\s)
1303 (delete-backward-char 1)))))))
1304 (widget-specify-secret field))
1305 (widget-apply field :notify field))))
1307 ;;; Widget Functions
1309 ;; These functions are used in the definition of multiple widgets.
1311 (defun widget-parent-action (widget &optional event)
1312 "Tell :parent of WIDGET to handle the :action.
1313 Optional EVENT is the event that triggered the action."
1314 (widget-apply (widget-get widget :parent) :action event))
1316 (defun widget-children-value-delete (widget)
1317 "Delete all :children and :buttons in WIDGET."
1318 (mapc 'widget-delete (widget-get widget :children))
1319 (widget-put widget :children nil)
1320 (mapc 'widget-delete (widget-get widget :buttons))
1321 (widget-put widget :buttons nil))
1323 (defun widget-children-validate (widget)
1324 "All the :children must be valid."
1325 (let ((children (widget-get widget :children))
1327 (while (and children (not found))
1328 (setq child (car children)
1329 children (cdr children)
1330 found (widget-apply child :validate)))
1333 (defun widget-child-value-get (widget)
1334 "Get the value of the first member of :children in WIDGET."
1335 (widget-value (car (widget-get widget :children))))
1337 (defun widget-child-value-inline (widget)
1338 "Get the inline value of the first member of :children in WIDGET."
1339 (widget-apply (car (widget-get widget :children)) :value-inline))
1341 (defun widget-child-validate (widget)
1342 "The result of validating the first member of :children in WIDGET."
1343 (widget-apply (car (widget-get widget :children)) :validate))
1345 (defun widget-type-value-create (widget)
1346 "Convert and instantiate the value of the :type attribute of WIDGET.
1347 Store the newly created widget in the :children attribute.
1349 The value of the :type attribute should be an unconverted widget type."
1350 (let ((value (widget-get widget :value))
1351 (type (widget-get widget :type)))
1352 (widget-put widget :children
1353 (list (widget-create-child-value widget
1354 (widget-convert type)
1357 (defun widget-type-default-get (widget)
1358 "Get default value from the :type attribute of WIDGET.
1360 The value of the :type attribute should be an unconverted widget type."
1361 (widget-default-get (widget-convert (widget-get widget :type))))
1363 (defun widget-type-match (widget value)
1364 "Non-nil if the :type value of WIDGET matches VALUE.
1366 The value of the :type attribute should be an unconverted widget type."
1367 (widget-apply (widget-convert (widget-get widget :type)) :match value))
1369 (defun widget-types-copy (widget)
1370 "Copy :args as widget types in WIDGET."
1371 (widget-put widget :args (mapcar 'widget-copy (widget-get widget :args)))
1374 ;; Made defsubst to speed up face editor creation.
1375 (defsubst widget-types-convert-widget (widget)
1376 "Convert :args as widget types in WIDGET."
1377 (widget-put widget :args (mapcar 'widget-convert (widget-get widget :args)))
1380 (defun widget-value-convert-widget (widget)
1381 "Initialize :value from :args in WIDGET."
1382 (let ((args (widget-get widget :args)))
1384 (widget-put widget :value (car args))
1385 ;; Don't convert :value here, as this is done in `widget-convert'.
1386 ;; (widget-put widget :value (widget-apply widget
1387 ;; :value-to-internal (car args)))
1388 (widget-put widget :args nil)))
1391 (defun widget-value-value-get (widget)
1392 "Return the :value property of WIDGET."
1393 (widget-get widget :value))
1395 ;;; The `default' Widget.
1397 (define-widget 'default nil
1398 "Basic widget other widgets are derived from."
1399 :value-to-internal (lambda (widget value) value)
1400 :value-to-external (lambda (widget value) value)
1401 :button-prefix 'widget-button-prefix
1402 :button-suffix 'widget-button-suffix
1403 :complete 'widget-default-complete
1404 :create 'widget-default-create
1407 :format-handler 'widget-default-format-handler
1408 :button-face-get 'widget-default-button-face-get
1409 :mouse-face-get 'widget-default-mouse-face-get
1410 :sample-face-get 'widget-default-sample-face-get
1411 :delete 'widget-default-delete
1413 :value-set 'widget-default-value-set
1414 :value-inline 'widget-default-value-inline
1415 :value-delete 'ignore
1416 :default-get 'widget-default-default-get
1417 :menu-tag-get 'widget-default-menu-tag-get
1419 :active 'widget-default-active
1420 :activate 'widget-specify-active
1421 :deactivate 'widget-default-deactivate
1422 :mouse-down-action #'ignore
1423 :action 'widget-default-action
1424 :notify 'widget-default-notify
1425 :prompt-value 'widget-default-prompt-value)
1427 (defun widget-default-complete (widget)
1428 "Call the value of the :complete-function property of WIDGET.
1429 If that does not exists, call the value of `widget-complete-field'."
1430 (call-interactively (or (widget-get widget :complete-function)
1431 widget-complete-field)))
1433 (defun widget-default-create (widget)
1434 "Create WIDGET at point in the current buffer."
1435 (widget-specify-insert
1436 (let ((from (point))
1437 button-begin button-end
1438 sample-begin sample-end
1441 (insert (widget-get widget :format))
1443 ;; Parse escapes in format.
1444 (while (re-search-forward "%\\(.\\)" nil t)
1445 (let ((escape (char-after (match-beginning 1))))
1446 (delete-backward-char 2)
1447 (cond ((eq escape ?%)
1450 (setq button-begin (point))
1451 (insert (widget-get-indirect widget :button-prefix)))
1453 (insert (widget-get-indirect widget :button-suffix))
1454 (setq button-end (point)))
1456 (setq sample-begin (point)))
1458 (setq sample-end (point)))
1460 (when (widget-get widget :indent)
1462 (insert-char ?\s (widget-get widget :indent))))
1464 (let ((image (widget-get widget :tag-glyph))
1465 (tag (widget-get widget :tag)))
1467 (widget-image-insert widget (or tag "image") image))
1471 (princ (widget-get widget :value)
1472 (current-buffer))))))
1474 (let ((doc (widget-get widget :doc)))
1476 (setq doc-begin (point))
1478 (while (eq (preceding-char) ?\n)
1479 (delete-backward-char 1))
1481 (setq doc-end (point)))))
1483 (if (and button-begin (not button-end))
1484 (widget-apply widget :value-create)
1485 (setq value-pos (point))))
1487 (widget-apply widget :format-handler escape)))))
1488 ;; Specify button, sample, and doc, and insert value.
1489 (and button-begin button-end
1490 (widget-specify-button widget button-begin button-end))
1491 (and sample-begin sample-end
1492 (widget-specify-sample widget sample-begin sample-end))
1493 (and doc-begin doc-end
1494 (widget-specify-doc widget doc-begin doc-end))
1496 (goto-char value-pos)
1497 (widget-apply widget :value-create)))
1498 (let ((from (point-min-marker))
1499 (to (point-max-marker)))
1500 (set-marker-insertion-type from t)
1501 (set-marker-insertion-type to nil)
1502 (widget-put widget :from from)
1503 (widget-put widget :to to)))
1504 (widget-clear-undo))
1506 (defun widget-default-format-handler (widget escape)
1507 ;; We recognize the %h escape by default.
1508 (let* ((buttons (widget-get widget :buttons)))
1509 (cond ((eq escape ?h)
1510 (let* ((doc-property (widget-get widget :documentation-property))
1511 (doc-try (cond ((widget-get widget :doc))
1512 ((functionp doc-property)
1513 (funcall doc-property
1514 (widget-get widget :value)))
1515 ((symbolp doc-property)
1516 (documentation-property
1517 (widget-get widget :value)
1519 (doc-text (and (stringp doc-try)
1520 (> (length doc-try) 1)
1522 (doc-indent (widget-get widget :documentation-indent)))
1524 (and (eq (preceding-char) ?\n)
1525 (widget-get widget :indent)
1526 (insert-char ?\s (widget-get widget :indent)))
1527 ;; The `*' in the beginning is redundant.
1528 (when (eq (aref doc-text 0) ?*)
1529 (setq doc-text (substring doc-text 1)))
1530 ;; Get rid of trailing newlines.
1531 (when (string-match "\n+\\'" doc-text)
1532 (setq doc-text (substring doc-text 0 (match-beginning 0))))
1533 (push (widget-create-child-and-convert
1534 widget 'documentation-string
1535 :indent (cond ((numberp doc-indent )
1543 (error "Unknown escape `%c'" escape)))
1544 (widget-put widget :buttons buttons)))
1546 (defun widget-default-button-face-get (widget)
1547 ;; Use :button-face or widget-button-face
1548 (or (widget-get widget :button-face)
1549 (let ((parent (widget-get widget :parent)))
1551 (widget-apply parent :button-face-get)
1552 widget-button-face))))
1554 (defun widget-default-mouse-face-get (widget)
1555 ;; Use :mouse-face or widget-mouse-face
1556 (or (widget-get widget :mouse-face)
1557 (let ((parent (widget-get widget :parent)))
1559 (widget-apply parent :mouse-face-get)
1560 widget-mouse-face))))
1562 (defun widget-default-sample-face-get (widget)
1563 ;; Use :sample-face.
1564 (widget-get widget :sample-face))
1566 (defun widget-default-delete (widget)
1567 "Remove widget from the buffer."
1568 (let ((from (widget-get widget :from))
1569 (to (widget-get widget :to))
1570 (inactive-overlay (widget-get widget :inactive))
1571 (button-overlay (widget-get widget :button-overlay))
1572 (sample-overlay (widget-get widget :sample-overlay))
1573 (doc-overlay (widget-get widget :doc-overlay))
1574 (inhibit-modification-hooks t)
1575 (inhibit-read-only t))
1576 (widget-apply widget :value-delete)
1577 (widget-children-value-delete widget)
1578 (when inactive-overlay
1579 (delete-overlay inactive-overlay))
1580 (when button-overlay
1581 (delete-overlay button-overlay))
1582 (when sample-overlay
1583 (delete-overlay sample-overlay))
1585 (delete-overlay doc-overlay))
1587 ;; Kludge: this doesn't need to be true for empty formats.
1588 (delete-region from to))
1589 (set-marker from nil)
1590 (set-marker to nil))
1591 (widget-clear-undo))
1593 (defun widget-default-value-set (widget value)
1594 "Recreate widget with new value."
1595 (let* ((old-pos (point))
1596 (from (copy-marker (widget-get widget :from)))
1597 (to (copy-marker (widget-get widget :to)))
1598 (offset (if (and (<= from old-pos) (<= old-pos to))
1599 (if (>= old-pos (1- to))
1601 (- old-pos from)))))
1602 ;;??? Bug: this ought to insert the new value before deleting the old one,
1603 ;; so that markers on either side of the value automatically
1604 ;; stay on the same side. -- rms.
1606 (goto-char (widget-get widget :from))
1607 (widget-apply widget :delete)
1608 (widget-put widget :value value)
1609 (widget-apply widget :create))
1612 (goto-char (+ (widget-get widget :to) offset 1))
1613 (goto-char (min (+ from offset) (1- (widget-get widget :to))))))))
1615 (defun widget-default-value-inline (widget)
1616 "Wrap value in a list unless it is inline."
1617 (if (widget-get widget :inline)
1618 (widget-value widget)
1619 (list (widget-value widget))))
1621 (defun widget-default-default-get (widget)
1623 (widget-get widget :value))
1625 (defun widget-default-menu-tag-get (widget)
1626 "Use tag or value for menus."
1627 (or (widget-get widget :menu-tag)
1628 (widget-get widget :tag)
1629 (widget-princ-to-string (widget-get widget :value))))
1631 (defun widget-default-active (widget)
1632 "Return t iff this widget active (user modifiable)."
1633 (or (widget-get widget :always-active)
1634 (and (not (widget-get widget :inactive))
1635 (let ((parent (widget-get widget :parent)))
1637 (widget-apply parent :active))))))
1639 (defun widget-default-deactivate (widget)
1640 "Make WIDGET inactive for user modifications."
1641 (widget-specify-inactive widget
1642 (widget-get widget :from)
1643 (widget-get widget :to)))
1645 (defun widget-default-action (widget &optional event)
1646 "Notify the parent when a widget changes."
1647 (let ((parent (widget-get widget :parent)))
1649 (widget-apply parent :notify widget event))))
1651 (defun widget-default-notify (widget child &optional event)
1652 "Pass notification to parent."
1653 (widget-default-action widget event))
1655 (defun widget-default-prompt-value (widget prompt value unbound)
1656 "Read an arbitrary value. Stolen from `set-variable'."
1657 ;; (let ((initial (if unbound
1659 ;; It would be nice if we could do a `(cons val 1)' here.
1660 ;; (prin1-to-string (custom-quote value))))))
1661 (eval-minibuffer prompt))
1663 ;;; The `item' Widget.
1665 (define-widget 'item 'default
1666 "Constant items for inclusion in other widgets."
1667 :convert-widget 'widget-value-convert-widget
1668 :value-create 'widget-item-value-create
1669 :value-delete 'ignore
1670 :value-get 'widget-value-value-get
1671 :match 'widget-item-match
1672 :match-inline 'widget-item-match-inline
1673 :action 'widget-item-action
1676 (defun widget-item-value-create (widget)
1677 "Insert the printed representation of the value."
1678 (princ (widget-get widget :value) (current-buffer)))
1680 (defun widget-item-match (widget value)
1681 ;; Match if the value is the same.
1682 (equal (widget-get widget :value) value))
1684 (defun widget-item-match-inline (widget values)
1685 ;; Match if the value is the same.
1686 (let ((value (widget-get widget :value)))
1688 (<= (length value) (length values))
1689 (let ((head (widget-sublist values 0 (length value))))
1690 (and (equal head value)
1691 (cons head (widget-sublist values (length value))))))))
1693 (defun widget-sublist (list start &optional end)
1694 "Return the sublist of LIST from START to END.
1695 If END is omitted, it defaults to the length of LIST."
1696 (if (> start 0) (setq list (nthcdr start list)))
1698 (unless (<= end start)
1699 (setq list (copy-sequence list))
1700 (setcdr (nthcdr (- end start 1) list) nil)
1702 (copy-sequence list)))
1704 (defun widget-item-action (widget &optional event)
1705 ;; Just notify itself.
1706 (widget-apply widget :notify widget event))
1708 ;;; The `push-button' Widget.
1710 ;; (defcustom widget-push-button-gui t
1711 ;; "If non nil, use GUI push buttons when available."
1715 ;; Cache already created GUI objects.
1716 ;; (defvar widget-push-button-cache nil)
1718 (defcustom widget-push-button-prefix "["
1719 "String used as prefix for buttons."
1721 :group 'widget-button)
1723 (defcustom widget-push-button-suffix "]"
1724 "String used as suffix for buttons."
1726 :group 'widget-button)
1728 (define-widget 'push-button 'item
1729 "A pushable button."
1732 :value-create 'widget-push-button-value-create
1735 (defun widget-push-button-value-create (widget)
1736 "Insert text representing the `on' and `off' states."
1737 (let* ((tag (or (widget-get widget :tag)
1738 (widget-get widget :value)))
1739 (tag-glyph (widget-get widget :tag-glyph))
1740 (text (concat widget-push-button-prefix
1741 tag widget-push-button-suffix)))
1743 (widget-image-insert widget text tag-glyph)
1746 ;; (defun widget-gui-action (widget)
1747 ;; "Apply :action for WIDGET."
1748 ;; (widget-apply-action widget (this-command-keys)))
1750 ;;; The `link' Widget.
1752 (defcustom widget-link-prefix "["
1753 "String used as prefix for links."
1755 :group 'widget-button)
1757 (defcustom widget-link-suffix "]"
1758 "String used as suffix for links."
1760 :group 'widget-button)
1762 (define-widget 'link 'item
1764 :button-prefix 'widget-link-prefix
1765 :button-suffix 'widget-link-suffix
1767 :help-echo "Follow the link."
1770 ;;; The `info-link' Widget.
1772 (define-widget 'info-link 'link
1773 "A link to an info file."
1774 :action 'widget-info-link-action)
1776 (defun widget-info-link-action (widget &optional event)
1777 "Open the info node specified by WIDGET."
1778 (info (widget-value widget)))
1780 ;;; The `url-link' Widget.
1782 (define-widget 'url-link 'link
1783 "A link to an www page."
1784 :action 'widget-url-link-action)
1786 (defun widget-url-link-action (widget &optional event)
1787 "Open the URL specified by WIDGET."
1788 (browse-url (widget-value widget)))
1790 ;;; The `function-link' Widget.
1792 (define-widget 'function-link 'link
1793 "A link to an Emacs function."
1794 :action 'widget-function-link-action)
1796 (defun widget-function-link-action (widget &optional event)
1797 "Show the function specified by WIDGET."
1798 (describe-function (widget-value widget)))
1800 ;;; The `variable-link' Widget.
1802 (define-widget 'variable-link 'link
1803 "A link to an Emacs variable."
1804 :action 'widget-variable-link-action)
1806 (defun widget-variable-link-action (widget &optional event)
1807 "Show the variable specified by WIDGET."
1808 (describe-variable (widget-value widget)))
1810 ;;; The `file-link' Widget.
1812 (define-widget 'file-link 'link
1814 :action 'widget-file-link-action)
1816 (defun widget-file-link-action (widget &optional event)
1817 "Find the file specified by WIDGET."
1818 (find-file (widget-value widget)))
1820 ;;; The `emacs-library-link' Widget.
1822 (define-widget 'emacs-library-link 'link
1823 "A link to an Emacs Lisp library file."
1824 :action 'widget-emacs-library-link-action)
1826 (defun widget-emacs-library-link-action (widget &optional event)
1827 "Find the Emacs library file specified by WIDGET."
1828 (find-file (locate-library (widget-value widget))))
1830 ;;; The `emacs-commentary-link' Widget.
1832 (define-widget 'emacs-commentary-link 'link
1833 "A link to Commentary in an Emacs Lisp library file."
1834 :action 'widget-emacs-commentary-link-action)
1836 (defun widget-emacs-commentary-link-action (widget &optional event)
1837 "Find the Commentary section of the Emacs file specified by WIDGET."
1838 (finder-commentary (widget-value widget)))
1840 ;;; The `editable-field' Widget.
1842 (define-widget 'editable-field 'default
1843 "An editable text field."
1844 :convert-widget 'widget-value-convert-widget
1845 :keymap widget-field-keymap
1847 :help-echo "M-TAB: complete field; RET: enter value"
1849 :prompt-internal 'widget-field-prompt-internal
1850 :prompt-history 'widget-field-history
1851 :prompt-value 'widget-field-prompt-value
1852 :action 'widget-field-action
1853 :validate 'widget-field-validate
1855 :error "Field's value doesn't match allowed forms"
1856 :value-create 'widget-field-value-create
1857 :value-delete 'widget-field-value-delete
1858 :value-get 'widget-field-value-get
1859 :match 'widget-field-match)
1861 (defvar widget-field-history nil
1862 "History of field minibuffer edits.")
1864 (defun widget-field-prompt-internal (widget prompt initial history)
1865 "Read string for WIDGET promptinhg with PROMPT.
1866 INITIAL is the initial input and HISTORY is a symbol containing
1868 (read-string prompt initial history))
1870 (defun widget-field-prompt-value (widget prompt value unbound)
1871 "Prompt for a string."
1872 (widget-apply widget
1874 (widget-apply widget
1875 :prompt-internal prompt
1877 (cons (widget-apply widget
1878 :value-to-internal value)
1880 (widget-get widget :prompt-history))))
1882 (defvar widget-edit-functions nil)
1884 (defun widget-field-action (widget &optional event)
1885 "Move to next field."
1887 (run-hook-with-args 'widget-edit-functions widget))
1889 (defun widget-field-validate (widget)
1890 "Valid if the content matches `:valid-regexp'."
1891 (unless (string-match (widget-get widget :valid-regexp)
1892 (widget-apply widget :value-get))
1895 (defun widget-field-value-create (widget)
1896 "Create an editable text field."
1897 (let ((size (widget-get widget :size))
1898 (value (widget-get widget :value))
1900 ;; This is changed to a real overlay in `widget-setup'. We
1901 ;; need the end points to behave differently until
1902 ;; `widget-setup' is called.
1903 (overlay (cons (make-marker) (make-marker))))
1904 (widget-put widget :field-overlay overlay)
1907 (< (length value) size)
1908 (insert-char ?\s (- size (length value))))
1909 (unless (memq widget widget-field-list)
1910 (setq widget-field-new (cons widget widget-field-new)))
1911 (move-marker (cdr overlay) (point))
1912 (set-marker-insertion-type (cdr overlay) nil)
1915 (move-marker (car overlay) from)
1916 (set-marker-insertion-type (car overlay) t)))
1918 (defun widget-field-value-delete (widget)
1919 "Remove the widget from the list of active editing fields."
1920 (setq widget-field-list (delq widget widget-field-list))
1921 (setq widget-field-new (delq widget widget-field-new))
1922 ;; These are nil if the :format string doesn't contain `%v'.
1923 (let ((overlay (widget-get widget :field-overlay)))
1924 (when (overlayp overlay)
1925 (delete-overlay overlay))))
1927 (defun widget-field-value-get (widget)
1928 "Return current text in editing field."
1929 (let ((from (widget-field-start widget))
1930 (to (widget-field-end widget))
1931 (buffer (widget-field-buffer widget))
1932 (size (widget-get widget :size))
1933 (secret (widget-get widget :secret))
1934 (old (current-buffer)))
1941 (eq (char-after (1- to)) ?\s))
1943 (let ((result (buffer-substring-no-properties from to)))
1946 (while (< (+ from index) to)
1948 (get-char-property (+ from index) 'secret))
1949 (setq index (1+ index)))))
1952 (widget-get widget :value))))
1954 (defun widget-field-match (widget value)
1955 ;; Match any string.
1958 ;;; The `text' Widget.
1960 (define-widget 'text 'editable-field
1961 "A multiline text area."
1962 :keymap widget-text-keymap)
1964 ;;; The `menu-choice' Widget.
1966 (define-widget 'menu-choice 'default
1967 "A menu of options."
1968 :convert-widget 'widget-types-convert-widget
1969 :copy 'widget-types-copy
1970 :format "%[%t%]: %v"
1973 :void '(item :format "invalid (%t)\n")
1974 :value-create 'widget-choice-value-create
1975 :value-get 'widget-child-value-get
1976 :value-inline 'widget-child-value-inline
1977 :default-get 'widget-choice-default-get
1978 :mouse-down-action 'widget-choice-mouse-down-action
1979 :action 'widget-choice-action
1980 :error "Make a choice"
1981 :validate 'widget-choice-validate
1982 :match 'widget-choice-match
1983 :match-inline 'widget-choice-match-inline)
1985 (defun widget-choice-value-create (widget)
1986 "Insert the first choice that matches the value."
1987 (let ((value (widget-get widget :value))
1988 (args (widget-get widget :args))
1989 (explicit (widget-get widget :explicit-choice))
1993 ;; If the user specified the choice for this value,
1994 ;; respect that choice.
1995 (widget-put widget :children (list (widget-create-child-value
1996 widget explicit value)))
1997 (widget-put widget :choice explicit)
1998 (widget-put widget :explicit-choice nil))
2000 (setq current (car args)
2002 (when (widget-apply current :match value)
2003 (widget-put widget :children (list (widget-create-child-value
2004 widget current value)))
2005 (widget-put widget :choice current)
2009 (let ((void (widget-get widget :void)))
2010 (widget-put widget :children (list (widget-create-child-and-convert
2011 widget void :value value)))
2012 (widget-put widget :choice void))))))
2014 (defun widget-choice-default-get (widget)
2015 ;; Get default for the first choice.
2016 (widget-default-get (car (widget-get widget :args))))
2018 (defcustom widget-choice-toggle nil
2019 "If non-nil, a binary choice will just toggle between the values.
2020 Otherwise, the user will explicitly have to choose between the values
2021 when he invoked the menu."
2025 (defun widget-choice-mouse-down-action (widget &optional event)
2026 ;; Return non-nil if we need a menu.
2027 (let ((args (widget-get widget :args))
2028 (old (widget-get widget :choice)))
2029 (cond ((not (display-popup-menus-p))
2030 ;; No place to pop up a menu.
2032 ((< (length args) 2)
2033 ;; Empty or singleton list, just return the value.
2035 ((> (length args) widget-menu-max-size)
2036 ;; Too long, prompt.
2038 ((> (length args) 2)
2039 ;; Reasonable sized list, use menu.
2041 ((and widget-choice-toggle (memq old args))
2045 ;; Ask which of the two.
2048 (defun widget-choice-action (widget &optional event)
2050 (let ((args (widget-get widget :args))
2051 (old (widget-get widget :choice))
2052 (tag (widget-apply widget :menu-tag-get))
2053 (completion-ignore-case (widget-get widget :case-fold))
2056 ;; Remember old value.
2057 (if (and old (not (widget-apply widget :validate)))
2058 (let* ((external (widget-value widget))
2059 (internal (widget-apply old :value-to-internal external)))
2060 (widget-put old :value internal)))
2063 (cond ((= (length args) 0)
2065 ((= (length args) 1)
2067 ((and widget-choice-toggle
2070 (if (eq old (nth 0 args))
2075 (setq current (car args)
2078 (cons (cons (widget-apply current :menu-tag-get)
2081 (setq this-explicit t)
2082 (widget-choose tag (reverse choices) event))))
2084 ;; If this was an explicit user choice, record the choice,
2085 ;; so that widget-choice-value-create will respect it.
2087 (widget-put widget :explicit-choice current))
2088 (widget-value-set widget (widget-default-get current))
2090 (widget-apply widget :notify widget event)))
2091 (run-hook-with-args 'widget-edit-functions widget))
2093 (defun widget-choice-validate (widget)
2094 ;; Valid if we have made a valid choice.
2095 (if (eq (widget-get widget :void) (widget-get widget :choice))
2097 (widget-apply (car (widget-get widget :children)) :validate)))
2099 (defun widget-choice-match (widget value)
2100 ;; Matches if one of the choices matches.
2101 (let ((args (widget-get widget :args))
2103 (while (and args (not found))
2104 (setq current (car args)
2106 found (widget-apply current :match value)))
2109 (defun widget-choice-match-inline (widget values)
2110 ;; Matches if one of the choices matches.
2111 (let ((args (widget-get widget :args))
2113 (while (and args (null found))
2114 (setq current (car args)
2116 found (widget-match-inline current values)))
2119 ;;; The `toggle' Widget.
2121 (define-widget 'toggle 'item
2122 "Toggle between two states."
2124 :value-create 'widget-toggle-value-create
2125 :action 'widget-toggle-action
2126 :match (lambda (widget value) t)
2130 (defun widget-toggle-value-create (widget)
2131 "Insert text representing the `on' and `off' states."
2132 (if (widget-value widget)
2133 (let ((image (widget-get widget :on-glyph)))
2134 (and (display-graphic-p)
2136 (not (eq (car image) 'image))
2137 (widget-put widget :on-glyph (setq image (eval image))))
2138 (widget-image-insert widget
2139 (widget-get widget :on)
2141 (let ((image (widget-get widget :off-glyph)))
2142 (and (display-graphic-p)
2144 (not (eq (car image) 'image))
2145 (widget-put widget :off-glyph (setq image (eval image))))
2146 (widget-image-insert widget (widget-get widget :off) image))))
2148 (defun widget-toggle-action (widget &optional event)
2150 (widget-value-set widget (not (widget-value widget)))
2151 (widget-apply widget :notify widget event)
2152 (run-hook-with-args 'widget-edit-functions widget))
2154 ;;; The `checkbox' Widget.
2156 (define-widget 'checkbox 'toggle
2157 "A checkbox toggle."
2162 ;; We could probably do the same job as the images using single
2163 ;; space characters in a boxed face with a stretch specification to
2164 ;; make them square.
2165 :on-glyph '(create-image "\300\300\141\143\067\076\034\030"
2166 'xbm t :width 8 :height 8
2167 :background "grey75" ; like default mode line
2172 :off-glyph '(create-image (make-string 8 0)
2173 'xbm t :width 8 :height 8
2174 :background "grey75"
2178 :help-echo "Toggle this item."
2179 :action 'widget-checkbox-action)
2181 (defun widget-checkbox-action (widget &optional event)
2182 "Toggle checkbox, notify parent, and set active state of sibling."
2183 (widget-toggle-action widget event)
2184 (let ((sibling (widget-get-sibling widget)))
2186 (if (widget-value widget)
2187 (widget-apply sibling :activate)
2188 (widget-apply sibling :deactivate))
2189 (widget-clear-undo))))
2191 ;;; The `checklist' Widget.
2193 (define-widget 'checklist 'default
2194 "A multiple choice widget."
2195 :convert-widget 'widget-types-convert-widget
2196 :copy 'widget-types-copy
2199 :entry-format "%b %v"
2201 :value-create 'widget-checklist-value-create
2202 :value-get 'widget-checklist-value-get
2203 :validate 'widget-checklist-validate
2204 :match 'widget-checklist-match
2205 :match-inline 'widget-checklist-match-inline)
2207 (defun widget-checklist-value-create (widget)
2208 ;; Insert all values
2209 (let ((alist (widget-checklist-match-find widget (widget-get widget :value)))
2210 (args (widget-get widget :args)))
2212 (widget-checklist-add-item widget (car args) (assq (car args) alist))
2213 (setq args (cdr args)))
2214 (widget-put widget :children (nreverse (widget-get widget :children)))))
2216 (defun widget-checklist-add-item (widget type chosen)
2217 "Create checklist item in WIDGET of type TYPE.
2218 If the item is checked, CHOSEN is a cons whose cdr is the value."
2219 (and (eq (preceding-char) ?\n)
2220 (widget-get widget :indent)
2221 (insert-char ?\s (widget-get widget :indent)))
2222 (widget-specify-insert
2223 (let* ((children (widget-get widget :children))
2224 (buttons (widget-get widget :buttons))
2225 (button-args (or (widget-get type :sibling-args)
2226 (widget-get widget :button-args)))
2229 (insert (widget-get widget :entry-format))
2231 ;; Parse % escapes in format.
2232 (while (re-search-forward "%\\([bv%]\\)" nil t)
2233 (let ((escape (char-after (match-beginning 1))))
2234 (delete-backward-char 2)
2235 (cond ((eq escape ?%)
2238 (setq button (apply 'widget-create-child-and-convert
2240 :value (not (null chosen))
2245 (let ((child (widget-create-child widget type)))
2246 (widget-apply child :deactivate)
2248 ((widget-get type :inline)
2249 (widget-create-child-value
2250 widget type (cdr chosen)))
2252 (widget-create-child-value
2253 widget type (car (cdr chosen)))))))
2255 (error "Unknown escape `%c'" escape)))))
2256 ;; Update properties.
2257 (and button child (widget-put child :button button))
2258 (and button (widget-put widget :buttons (cons button buttons)))
2259 (and child (widget-put widget :children (cons child children))))))
2261 (defun widget-checklist-match (widget values)
2262 ;; All values must match a type in the checklist.
2264 (null (cdr (widget-checklist-match-inline widget values)))))
2266 (defun widget-checklist-match-inline (widget values)
2267 ;; Find the values which match a type in the checklist.
2268 (let ((greedy (widget-get widget :greedy))
2269 (args (copy-sequence (widget-get widget :args)))
2272 (let ((answer (widget-checklist-match-up args values)))
2274 (let ((vals (widget-match-inline answer values)))
2275 (setq found (append found (car vals))
2277 args (delq answer args))))
2279 (setq rest (append rest (list (car values)))
2280 values (cdr values)))
2282 (setq rest (append rest values)
2286 (defun widget-checklist-match-find (widget vals)
2287 "Find the vals which match a type in the checklist.
2288 Return an alist of (TYPE MATCH)."
2289 (let ((greedy (widget-get widget :greedy))
2290 (args (copy-sequence (widget-get widget :args)))
2293 (let ((answer (widget-checklist-match-up args vals)))
2295 (let ((match (widget-match-inline answer vals)))
2296 (setq found (cons (cons answer (car match)) found)
2298 args (delq answer args))))
2300 (setq vals (cdr vals)))
2305 (defun widget-checklist-match-up (args vals)
2306 "Return the first type from ARGS that matches VALS."
2307 (let (current found)
2308 (while (and args (null found))
2309 (setq current (car args)
2311 found (widget-match-inline current vals)))
2315 (defun widget-checklist-value-get (widget)
2316 ;; The values of all selected items.
2317 (let ((children (widget-get widget :children))
2320 (setq child (car children)
2321 children (cdr children))
2322 (if (widget-value (widget-get child :button))
2323 (setq result (append result (widget-apply child :value-inline)))))
2326 (defun widget-checklist-validate (widget)
2327 ;; Ticked chilren must be valid.
2328 (let ((children (widget-get widget :children))
2330 (while (and children (not found))
2331 (setq child (car children)
2332 children (cdr children)
2333 button (widget-get child :button)
2334 found (and (widget-value button)
2335 (widget-apply child :validate))))
2338 ;;; The `option' Widget
2340 (define-widget 'option 'checklist
2341 "An widget with an optional item."
2344 ;;; The `choice-item' Widget.
2346 (define-widget 'choice-item 'item
2347 "Button items that delegate action events to their parents."
2348 :action 'widget-parent-action
2349 :format "%[%t%] \n")
2351 ;;; The `radio-button' Widget.
2353 (define-widget 'radio-button 'toggle
2354 "A radio button for use in the `radio' widget."
2355 :notify 'widget-radio-button-notify
2362 :off-glyph "radio0")
2364 (defun widget-radio-button-notify (widget child &optional event)
2366 (widget-apply (widget-get widget :parent) :action widget event))
2368 ;;; The `radio-button-choice' Widget.
2370 (define-widget 'radio-button-choice 'default
2371 "Select one of multiple options."
2372 :convert-widget 'widget-types-convert-widget
2373 :copy 'widget-types-copy
2376 :entry-format "%b %v"
2377 :value-create 'widget-radio-value-create
2378 :value-get 'widget-radio-value-get
2379 :value-inline 'widget-radio-value-inline
2380 :value-set 'widget-radio-value-set
2381 :error "You must push one of the buttons"
2382 :validate 'widget-radio-validate
2383 :match 'widget-choice-match
2384 :match-inline 'widget-choice-match-inline
2385 :action 'widget-radio-action)
2387 (defun widget-radio-value-create (widget)
2388 ;; Insert all values
2389 (let ((args (widget-get widget :args))
2392 (setq arg (car args)
2394 (widget-radio-add-item widget arg))))
2396 (defun widget-radio-add-item (widget type)
2397 "Add to radio widget WIDGET a new radio button item of type TYPE."
2398 ;; (setq type (widget-convert type))
2399 (and (eq (preceding-char) ?\n)
2400 (widget-get widget :indent)
2401 (insert-char ?\s (widget-get widget :indent)))
2402 (widget-specify-insert
2403 (let* ((value (widget-get widget :value))
2404 (children (widget-get widget :children))
2405 (buttons (widget-get widget :buttons))
2406 (button-args (or (widget-get type :sibling-args)
2407 (widget-get widget :button-args)))
2409 (chosen (and (null (widget-get widget :choice))
2410 (widget-apply type :match value)))
2412 (insert (widget-get widget :entry-format))
2414 ;; Parse % escapes in format.
2415 (while (re-search-forward "%\\([bv%]\\)" nil t)
2416 (let ((escape (char-after (match-beginning 1))))
2417 (delete-backward-char 2)
2418 (cond ((eq escape ?%)
2421 (setq button (apply 'widget-create-child-and-convert
2422 widget 'radio-button
2423 :value (not (null chosen))
2426 (setq child (if chosen
2427 (widget-create-child-value
2429 (widget-create-child widget type)))
2431 (widget-apply child :deactivate)))
2433 (error "Unknown escape `%c'" escape)))))
2434 ;; Update properties.
2436 (widget-put widget :choice type))
2438 (widget-put child :button button)
2439 (widget-put widget :buttons (nconc buttons (list button))))
2441 (widget-put widget :children (nconc children (list child))))
2444 (defun widget-radio-value-get (widget)
2445 ;; Get value of the child widget.
2446 (let ((chosen (widget-radio-chosen widget)))
2447 (and chosen (widget-value chosen))))
2449 (defun widget-radio-chosen (widget)
2450 "Return the widget representing the chosen radio button."
2451 (let ((children (widget-get widget :children))
2454 (setq current (car children)
2455 children (cdr children))
2456 (when (widget-apply (widget-get current :button) :value-get)
2461 (defun widget-radio-value-inline (widget)
2462 ;; Get value of the child widget.
2463 (let ((children (widget-get widget :children))
2466 (setq current (car children)
2467 children (cdr children))
2468 (when (widget-apply (widget-get current :button) :value-get)
2469 (setq found (widget-apply current :value-inline)
2473 (defun widget-radio-value-set (widget value)
2474 ;; We can't just delete and recreate a radio widget, since children
2475 ;; can be added after the original creation and won't be recreated
2477 (let ((children (widget-get widget :children))
2480 (setq current (car children)
2481 children (cdr children))
2482 (let* ((button (widget-get current :button))
2483 (match (and (not found)
2484 (widget-apply current :match value))))
2485 (widget-value-set button match)
2488 (widget-value-set current value)
2489 (widget-apply current :activate))
2490 (widget-apply current :deactivate))
2491 (setq found (or found match))))))
2493 (defun widget-radio-validate (widget)
2494 ;; Valid if we have made a valid choice.
2495 (let ((children (widget-get widget :children))
2496 current found button)
2497 (while (and children (not found))
2498 (setq current (car children)
2499 children (cdr children)
2500 button (widget-get current :button)
2501 found (widget-apply button :value-get)))
2503 (widget-apply current :validate)
2506 (defun widget-radio-action (widget child event)
2507 ;; Check if a radio button was pressed.
2508 (let ((children (widget-get widget :children))
2509 (buttons (widget-get widget :buttons))
2511 (when (memq child buttons)
2513 (setq current (car children)
2514 children (cdr children))
2515 (let* ((button (widget-get current :button)))
2516 (cond ((eq child button)
2517 (widget-value-set button t)
2518 (widget-apply current :activate))
2519 ((widget-value button)
2520 (widget-value-set button nil)
2521 (widget-apply current :deactivate)))))))
2522 ;; Pass notification to parent.
2523 (widget-apply widget :notify child event))
2525 ;;; The `insert-button' Widget.
2527 (define-widget 'insert-button 'push-button
2528 "An insert button for the `editable-list' widget."
2530 :help-echo "Insert a new item into the list at this position."
2531 :action 'widget-insert-button-action)
2533 (defun widget-insert-button-action (widget &optional event)
2534 ;; Ask the parent to insert a new item.
2535 (widget-apply (widget-get widget :parent)
2536 :insert-before (widget-get widget :widget)))
2538 ;;; The `delete-button' Widget.
2540 (define-widget 'delete-button 'push-button
2541 "A delete button for the `editable-list' widget."
2543 :help-echo "Delete this item from the list."
2544 :action 'widget-delete-button-action)
2546 (defun widget-delete-button-action (widget &optional event)
2547 ;; Ask the parent to insert a new item.
2548 (widget-apply (widget-get widget :parent)
2549 :delete-at (widget-get widget :widget)))
2551 ;;; The `editable-list' Widget.
2553 ;; (defcustom widget-editable-list-gui nil
2554 ;; "If non nil, use GUI push-buttons in editable list when available."
2558 (define-widget 'editable-list 'default
2559 "A variable list of widgets of the same type."
2560 :convert-widget 'widget-types-convert-widget
2561 :copy 'widget-types-copy
2564 :format-handler 'widget-editable-list-format-handler
2565 :entry-format "%i %d %v"
2566 :value-create 'widget-editable-list-value-create
2567 :value-get 'widget-editable-list-value-get
2568 :validate 'widget-children-validate
2569 :match 'widget-editable-list-match
2570 :match-inline 'widget-editable-list-match-inline
2571 :insert-before 'widget-editable-list-insert-before
2572 :delete-at 'widget-editable-list-delete-at)
2574 (defun widget-editable-list-format-handler (widget escape)
2575 ;; We recognize the insert button.
2576 ;; (let ((widget-push-button-gui widget-editable-list-gui))
2577 (cond ((eq escape ?i)
2578 (and (widget-get widget :indent)
2579 (insert-char ?\s (widget-get widget :indent)))
2580 (apply 'widget-create-child-and-convert
2581 widget 'insert-button
2582 (widget-get widget :append-button-args)))
2584 (widget-default-format-handler widget escape)))
2588 (defun widget-editable-list-value-create (widget)
2589 ;; Insert all values
2590 (let* ((value (widget-get widget :value))
2591 (type (nth 0 (widget-get widget :args)))
2593 (widget-put widget :value-pos (copy-marker (point)))
2594 (set-marker-insertion-type (widget-get widget :value-pos) t)
2596 (let ((answer (widget-match-inline type value)))
2598 (setq children (cons (widget-editable-list-entry-create
2600 (if (widget-get type :inline)
2607 (widget-put widget :children (nreverse children))))
2609 (defun widget-editable-list-value-get (widget)
2610 ;; Get value of the child widget.
2611 (apply 'append (mapcar (lambda (child) (widget-apply child :value-inline))
2612 (widget-get widget :children))))
2614 (defun widget-editable-list-match (widget value)
2615 ;; Value must be a list and all the members must match the type.
2617 (null (cdr (widget-editable-list-match-inline widget value)))))
2619 (defun widget-editable-list-match-inline (widget value)
2620 (let ((type (nth 0 (widget-get widget :args)))
2623 (while (and value ok)
2624 (let ((answer (widget-match-inline type value)))
2626 (setq found (append found (car answer))
2629 (cons found value)))
2631 (defun widget-editable-list-insert-before (widget before)
2632 ;; Insert a new child in the list of children.
2634 (let ((children (widget-get widget :children))
2635 (inhibit-read-only t)
2636 before-change-functions
2637 after-change-functions)
2639 (goto-char (widget-get before :entry-from)))
2641 (goto-char (widget-get widget :value-pos))))
2642 (let ((child (widget-editable-list-entry-create
2644 (when (< (widget-get child :entry-from) (widget-get widget :from))
2645 (set-marker (widget-get widget :from)
2646 (widget-get child :entry-from)))
2647 (if (eq (car children) before)
2648 (widget-put widget :children (cons child children))
2649 (while (not (eq (car (cdr children)) before))
2650 (setq children (cdr children)))
2651 (setcdr children (cons child (cdr children)))))))
2653 (widget-apply widget :notify widget))
2655 (defun widget-editable-list-delete-at (widget child)
2656 ;; Delete child from list of children.
2658 (let ((buttons (copy-sequence (widget-get widget :buttons)))
2660 (inhibit-read-only t)
2661 before-change-functions
2662 after-change-functions)
2664 (setq button (car buttons)
2665 buttons (cdr buttons))
2666 (when (eq (widget-get button :widget) child)
2668 :buttons (delq button (widget-get widget :buttons)))
2669 (widget-delete button))))
2670 (let ((entry-from (widget-get child :entry-from))
2671 (entry-to (widget-get child :entry-to))
2672 (inhibit-read-only t)
2673 before-change-functions
2674 after-change-functions)
2675 (widget-delete child)
2676 (delete-region entry-from entry-to)
2677 (set-marker entry-from nil)
2678 (set-marker entry-to nil))
2679 (widget-put widget :children (delq child (widget-get widget :children))))
2681 (widget-apply widget :notify widget))
2683 (defun widget-editable-list-entry-create (widget value conv)
2684 ;; Create a new entry to the list.
2685 (let ((type (nth 0 (widget-get widget :args)))
2686 ;; (widget-push-button-gui widget-editable-list-gui)
2687 child delete insert)
2688 (widget-specify-insert
2690 (and (widget-get widget :indent)
2691 (insert-char ?\s (widget-get widget :indent)))
2692 (insert (widget-get widget :entry-format)))
2693 ;; Parse % escapes in format.
2694 (while (re-search-forward "%\\(.\\)" nil t)
2695 (let ((escape (char-after (match-beginning 1))))
2696 (delete-backward-char 2)
2697 (cond ((eq escape ?%)
2700 (setq insert (apply 'widget-create-child-and-convert
2701 widget 'insert-button
2702 (widget-get widget :insert-button-args))))
2704 (setq delete (apply 'widget-create-child-and-convert
2705 widget 'delete-button
2706 (widget-get widget :delete-button-args))))
2709 (setq child (widget-create-child-value
2711 (setq child (widget-create-child-value
2712 widget type (widget-default-get type)))))
2714 (error "Unknown escape `%c'" escape)))))
2715 (let ((buttons (widget-get widget :buttons)))
2716 (if insert (push insert buttons))
2717 (if delete (push delete buttons))
2718 (widget-put widget :buttons buttons))
2719 (let ((entry-from (point-min-marker))
2720 (entry-to (point-max-marker)))
2721 (set-marker-insertion-type entry-from t)
2722 (set-marker-insertion-type entry-to nil)
2723 (widget-put child :entry-from entry-from)
2724 (widget-put child :entry-to entry-to)))
2725 (if insert (widget-put insert :widget child))
2726 (if delete (widget-put delete :widget child))
2729 ;;; The `group' Widget.
2731 (define-widget 'group 'default
2732 "A widget which groups other widgets inside."
2733 :convert-widget 'widget-types-convert-widget
2734 :copy 'widget-types-copy
2736 :value-create 'widget-group-value-create
2737 :value-get 'widget-editable-list-value-get
2738 :default-get 'widget-group-default-get
2739 :validate 'widget-children-validate
2740 :match 'widget-group-match
2741 :match-inline 'widget-group-match-inline)
2743 (defun widget-group-value-create (widget)
2744 ;; Create each component.
2745 (let ((args (widget-get widget :args))
2746 (value (widget-get widget :value))
2747 arg answer children)
2749 (setq arg (car args)
2751 answer (widget-match-inline arg value)
2753 (and (eq (preceding-char) ?\n)
2754 (widget-get widget :indent)
2755 (insert-char ?\s (widget-get widget :indent)))
2756 (push (cond ((null answer)
2757 (widget-create-child widget arg))
2758 ((widget-get arg :inline)
2759 (widget-create-child-value widget arg (car answer)))
2761 (widget-create-child-value widget arg (car (car answer)))))
2763 (widget-put widget :children (nreverse children))))
2765 (defun widget-group-default-get (widget)
2766 ;; Get the default of the components.
2767 (mapcar 'widget-default-get (widget-get widget :args)))
2769 (defun widget-group-match (widget values)
2770 ;; Match if the components match.
2772 (let ((match (widget-group-match-inline widget values)))
2773 (and match (null (cdr match))))))
2775 (defun widget-group-match-inline (widget vals)
2776 ;; Match if the components match.
2777 (let ((args (widget-get widget :args))
2778 argument answer found)
2780 (setq argument (car args)
2782 answer (widget-match-inline argument vals))
2784 (setq vals (cdr answer)
2785 found (append found (car answer)))
2789 (cons found vals))))
2791 ;;; The `visibility' Widget.
2793 (define-widget 'visibility 'item
2794 "An indicator and manipulator for hidden items."
2800 :value-create 'widget-visibility-value-create
2801 :action 'widget-toggle-action
2802 :match (lambda (widget value) t))
2804 (defun widget-visibility-value-create (widget)
2805 ;; Insert text representing the `on' and `off' states.
2806 (let ((on (widget-get widget :on))
2807 (off (widget-get widget :off)))
2809 (setq on (concat widget-push-button-prefix
2811 widget-push-button-suffix))
2814 (setq off (concat widget-push-button-prefix
2816 widget-push-button-suffix))
2818 (if (widget-value widget)
2819 (widget-image-insert widget on "down" "down-pushed")
2820 (widget-image-insert widget off "right" "right-pushed"))))
2822 ;;; The `documentation-link' Widget.
2824 ;; This is a helper widget for `documentation-string'.
2826 (define-widget 'documentation-link 'link
2827 "Link type used in documentation strings."
2829 :help-echo "Describe this symbol"
2830 :action 'widget-documentation-link-action)
2832 (defun widget-documentation-link-action (widget &optional event)
2833 "Display documentation for WIDGET's value. Ignore optional argument EVENT."
2834 (let* ((string (widget-get widget :value))
2835 (symbol (intern string)))
2836 (if (and (fboundp symbol) (boundp symbol))
2837 ;; If there are two doc strings, give the user a way to pick one.
2838 (apropos (concat "\\`" (regexp-quote string) "\\'"))
2839 (if (fboundp symbol)
2840 (describe-function symbol)
2841 (describe-variable symbol)))))
2843 (defcustom widget-documentation-links t
2844 "Add hyperlinks to documentation strings when non-nil."
2846 :group 'widget-documentation)
2848 (defcustom widget-documentation-link-regexp "`\\([^\n`' ]+\\)'"
2849 "Regexp for matching potential links in documentation strings.
2850 The first group should be the link itself."
2852 :group 'widget-documentation)
2854 (defcustom widget-documentation-link-p 'intern-soft
2855 "Predicate used to test if a string is useful as a link.
2856 The value should be a function. The function will be called one
2857 argument, a string, and should return non-nil if there should be a
2858 link for that string."
2860 :options '(widget-documentation-link-p)
2861 :group 'widget-documentation)
2863 (defcustom widget-documentation-link-type 'documentation-link
2864 "Widget type used for links in documentation strings."
2866 :group 'widget-documentation)
2868 (defun widget-documentation-link-add (widget from to)
2869 (widget-specify-doc widget from to)
2870 (when widget-documentation-links
2871 (let ((regexp widget-documentation-link-regexp)
2872 (buttons (widget-get widget :buttons))
2873 (widget-mouse-face (default-value 'widget-mouse-face))
2874 (widget-button-face widget-documentation-face)
2875 (widget-button-pressed-face widget-documentation-face))
2878 (while (re-search-forward regexp to t)
2879 (let ((name (match-string 1))
2880 (begin (match-beginning 1))
2881 (end (match-end 1)))
2882 (when (funcall widget-documentation-link-p name)
2883 (push (widget-convert-button widget-documentation-link-type
2884 begin end :value name)
2886 (widget-put widget :buttons buttons)))
2887 (let ((indent (widget-get widget :indent)))
2888 (when (and indent (not (zerop indent)))
2891 (narrow-to-region from to)
2892 (goto-char (point-min))
2893 (while (search-forward "\n" nil t)
2894 (insert-char ?\s indent)))))))
2896 ;;; The `documentation-string' Widget.
2898 (define-widget 'documentation-string 'item
2899 "A documentation string."
2901 :action 'widget-documentation-string-action
2902 :value-create 'widget-documentation-string-value-create)
2904 (defun widget-documentation-string-value-create (widget)
2905 ;; Insert documentation string.
2906 (let ((doc (widget-value widget))
2907 (indent (widget-get widget :indent))
2908 (shown (widget-get (widget-get widget :parent) :documentation-shown))
2910 (if (string-match "\n" doc)
2911 (let ((before (substring doc 0 (match-beginning 0)))
2912 (after (substring doc (match-beginning 0)))
2915 (widget-documentation-link-add widget start (point))
2917 (widget-create-child-and-convert
2919 :help-echo "Show or hide rest of the documentation."
2923 :action 'widget-parent-action
2926 (setq start (point))
2927 (when (and indent (not (zerop indent)))
2928 (insert-char ?\s indent))
2930 (widget-documentation-link-add widget start (point)))
2931 (widget-put widget :buttons (list button)))
2933 (widget-documentation-link-add widget start (point))))
2936 (defun widget-documentation-string-action (widget &rest ignore)
2937 ;; Toggle documentation.
2938 (let ((parent (widget-get widget :parent)))
2939 (widget-put parent :documentation-shown
2940 (not (widget-get parent :documentation-shown))))
2942 (widget-value-set widget (widget-value widget)))
2944 ;;; The Sexp Widgets.
2946 (define-widget 'const 'item
2947 "An immutable sexp."
2948 :prompt-value 'widget-const-prompt-value
2951 (defun widget-const-prompt-value (widget prompt value unbound)
2952 ;; Return the value of the const.
2953 (widget-value widget))
2955 (define-widget 'function-item 'const
2956 "An immutable function name."
2958 :documentation-property (lambda (symbol)
2960 (documentation symbol t)
2963 (define-widget 'variable-item 'const
2964 "An immutable variable name."
2966 :documentation-property 'variable-documentation)
2968 (define-widget 'other 'sexp
2969 "Matches any value, but doesn't let the user edit the value.
2970 This is useful as last item in a `choice' widget.
2971 You should use this widget type with a default value,
2972 as in (other DEFAULT) or (other :tag \"NAME\" DEFAULT).
2973 If the user selects this alternative, that specifies DEFAULT
2979 (defvar widget-string-prompt-value-history nil
2980 "History of input to `widget-string-prompt-value'.")
2982 (define-widget 'string 'editable-field
2985 :format "%{%t%}: %v"
2986 :complete-function 'ispell-complete-word
2987 :prompt-history 'widget-string-prompt-value-history)
2989 (define-widget 'regexp 'string
2990 "A regular expression."
2991 :match 'widget-regexp-match
2992 :validate 'widget-regexp-validate
2993 ;; Doesn't work well with terminating newline.
2994 ;; :value-face 'widget-single-line-field
2997 (defun widget-regexp-match (widget value)
2998 ;; Match valid regexps.
2999 (and (stringp value)
3002 (string-match value ""))
3005 (defun widget-regexp-validate (widget)
3006 "Check that the value of WIDGET is a valid regexp."
3007 (condition-case data
3009 (string-match (widget-value widget) ""))
3010 (error (widget-put widget :error (error-message-string data))
3013 (define-widget 'file 'string
3015 It reads a file name from an editable text field."
3016 :complete-function 'widget-file-complete
3017 :prompt-value 'widget-file-prompt-value
3018 :format "%{%t%}: %v"
3019 ;; Doesn't work well with terminating newline.
3020 ;; :value-face 'widget-single-line-field
3023 (defun widget-file-complete ()
3024 "Perform completion on file name preceding point."
3026 (let* ((end (point))
3027 (beg (widget-field-start widget))
3028 (pattern (buffer-substring beg end))
3029 (name-part (file-name-nondirectory pattern))
3030 ;; I think defaulting to root is right
3031 ;; because these really should be absolute file names.
3032 (directory (or (file-name-directory pattern) "/"))
3033 (completion (file-name-completion name-part directory)))
3034 (cond ((eq completion t))
3036 (message "Can't find completion for \"%s\"" pattern)
3038 ((not (string= name-part completion))
3039 (delete-region beg end)
3040 (insert (expand-file-name completion directory)))
3042 (message "Making completion list...")
3043 (with-output-to-temp-buffer "*Completions*"
3044 (display-completion-list
3045 (sort (file-name-all-completions name-part directory)
3048 (message "Making completion list...%s" "done")))))
3050 (defun widget-file-prompt-value (widget prompt value unbound)
3051 ;; Read file from minibuffer.
3052 (abbreviate-file-name
3054 (read-file-name prompt)
3055 (let ((prompt2 (format "%s (default %s): " prompt value))
3056 (dir (file-name-directory value))
3057 (file (file-name-nondirectory value))
3058 (must-match (widget-get widget :must-match)))
3059 (read-file-name prompt2 dir nil must-match file)))))
3061 ;;;(defun widget-file-action (widget &optional event)
3062 ;;; ;; Read a file name from the minibuffer.
3063 ;;; (let* ((value (widget-value widget))
3064 ;;; (dir (file-name-directory value))
3065 ;;; (file (file-name-nondirectory value))
3066 ;;; (menu-tag (widget-apply widget :menu-tag-get))
3067 ;;; (must-match (widget-get widget :must-match))
3068 ;;; (answer (read-file-name (concat menu-tag " (default " value "): ")
3069 ;;; dir nil must-match file)))
3070 ;;; (widget-value-set widget (abbreviate-file-name answer))
3072 ;;; (widget-apply widget :notify widget event)))
3074 ;; Fixme: use file-name-as-directory.
3075 (define-widget 'directory 'file
3076 "A directory widget.
3077 It reads a directory name from an editable text field."
3080 (defvar widget-symbol-prompt-value-history nil
3081 "History of input to `widget-symbol-prompt-value'.")
3083 (define-widget 'symbol 'editable-field
3087 :format "%{%t%}: %v"
3088 :match (lambda (widget value) (symbolp value))
3089 :complete-function 'lisp-complete-symbol
3090 :prompt-internal 'widget-symbol-prompt-internal
3091 :prompt-match 'symbolp
3092 :prompt-history 'widget-symbol-prompt-value-history
3093 :value-to-internal (lambda (widget value)
3097 :value-to-external (lambda (widget value)
3102 (defun widget-symbol-prompt-internal (widget prompt initial history)
3103 ;; Read file from minibuffer.
3104 (let ((answer (completing-read prompt obarray
3105 (widget-get widget :prompt-match)
3106 nil initial history)))
3107 (if (and (stringp answer)
3108 (not (zerop (length answer))))
3110 (error "No value"))))
3112 (defvar widget-function-prompt-value-history nil
3113 "History of input to `widget-function-prompt-value'.")
3115 (define-widget 'function 'restricted-sexp
3117 :complete-function (lambda ()
3119 (lisp-complete-symbol 'fboundp))
3120 :prompt-value 'widget-field-prompt-value
3121 :prompt-internal 'widget-symbol-prompt-internal
3122 :prompt-match 'fboundp
3123 :prompt-history 'widget-function-prompt-value-history
3124 :action 'widget-field-action
3125 :match-alternatives '(functionp)
3126 :validate (lambda (widget)
3127 (unless (functionp (widget-value widget))
3128 (widget-put widget :error (format "Invalid function: %S"
3129 (widget-value widget)))
3134 (defvar widget-variable-prompt-value-history nil
3135 "History of input to `widget-variable-prompt-value'.")
3137 (define-widget 'variable 'symbol
3139 :prompt-match 'boundp
3140 :prompt-history 'widget-variable-prompt-value-history
3141 :complete-function (lambda ()
3143 (lisp-complete-symbol 'boundp))
3146 (defvar widget-coding-system-prompt-value-history nil
3147 "History of input to `widget-coding-system-prompt-value'.")
3149 (define-widget 'coding-system 'symbol
3150 "A MULE coding-system."
3151 :format "%{%t%}: %v"
3152 :tag "Coding system"
3154 :prompt-history 'widget-coding-system-prompt-value-history
3155 :prompt-value 'widget-coding-system-prompt-value
3156 :action 'widget-coding-system-action
3157 :complete-function (lambda ()
3159 (lisp-complete-symbol 'coding-system-p))
3160 :validate (lambda (widget)
3161 (unless (coding-system-p (widget-value widget))
3162 (widget-put widget :error (format "Invalid coding system: %S"
3163 (widget-value widget)))
3166 :prompt-match 'coding-system-p)
3168 (defun widget-coding-system-prompt-value (widget prompt value unbound)
3169 "Read coding-system from minibuffer."
3170 (if (widget-get widget :base-only)
3172 (completing-read (format "%s (default %s): " prompt value)
3173 (mapcar #'list (coding-system-list t)) nil nil nil
3174 coding-system-history))
3175 (read-coding-system (format "%s (default %s): " prompt value) value)))
3177 (defun widget-coding-system-action (widget &optional event)
3179 (widget-coding-system-prompt-value
3181 (widget-apply widget :menu-tag-get)
3182 (widget-value widget)
3184 (widget-value-set widget answer)
3185 (widget-apply widget :notify widget event)
3188 ;;; I'm not sure about what this is good for? KFS.
3189 (defvar widget-key-sequence-prompt-value-history nil
3190 "History of input to `widget-key-sequence-prompt-value'.")
3192 (defvar widget-key-sequence-default-value [ignore]
3193 "Default value for an empty key sequence.")
3195 (defvar widget-key-sequence-map
3196 (let ((map (make-sparse-keymap)))
3197 (set-keymap-parent map widget-field-keymap)
3198 (define-key map [(control ?q)] 'widget-key-sequence-read-event)
3201 (define-widget 'key-sequence 'restricted-sexp
3203 :prompt-value 'widget-field-prompt-value
3204 :prompt-internal 'widget-symbol-prompt-internal
3205 ; :prompt-match 'fboundp ;; What was this good for? KFS
3206 :prompt-history 'widget-key-sequence-prompt-value-history
3207 :action 'widget-field-action
3208 :match-alternatives '(stringp vectorp)
3209 :format "%{%t%}: %v"
3210 :validate 'widget-key-sequence-validate
3211 :value-to-internal 'widget-key-sequence-value-to-internal
3212 :value-to-external 'widget-key-sequence-value-to-external
3213 :value widget-key-sequence-default-value
3214 :keymap widget-key-sequence-map
3215 :help-echo "C-q: insert KEY, EVENT, or CODE; RET: enter value"
3216 :tag "Key sequence")
3218 (defun widget-key-sequence-read-event (ev)
3220 (let ((inhibit-quit t) quit-flag)
3221 (read-event "Insert KEY, EVENT, or CODE: "))))
3222 (let ((ev2 (and (memq 'down (event-modifiers ev))
3224 (tr (and (keymapp function-key-map)
3225 (lookup-key function-key-map (vector ev)))))
3226 (when (and (integerp ev)
3227 (or (and (<= ?0 ev) (< ev (+ ?0 (min 10 read-quoted-char-radix))))
3228 (and (<= ?a (downcase ev))
3229 (< (downcase ev) (+ ?a -10 (min 36 read-quoted-char-radix))))))
3230 (setq unread-command-events (cons ev unread-command-events)
3231 ev (read-quoted-char (format "Enter code (radix %d)" read-quoted-char-radix))
3233 (if (and (integerp ev) (not (char-valid-p ev)))
3234 (insert (char-to-string ev)))) ;; throw invalid char error
3235 (setq ev (key-description (list ev)))
3237 (setq tr (key-description (list (aref tr 0))))
3238 (if (y-or-n-p (format "Key %s is translated to %s -- use %s? " ev tr tr))
3239 (setq ev tr ev2 nil)))
3240 (insert (if (= (char-before) ?\s) "" " ") ev " ")
3242 (insert (key-description (list ev2)) " "))))
3244 (defun widget-key-sequence-validate (widget)
3245 (unless (or (stringp (widget-value widget))
3246 (vectorp (widget-value widget)))
3247 (widget-put widget :error (format "Invalid key sequence: %S"
3248 (widget-value widget)))
3251 (defun widget-key-sequence-value-to-internal (widget value)
3252 (if (widget-apply widget :match value)
3253 (if (equal value widget-key-sequence-default-value)
3255 (key-description value))
3258 (defun widget-key-sequence-value-to-external (widget value)
3260 (if (string-match "\\`[[:space:]]*\\'" value)
3261 widget-key-sequence-default-value
3262 (read-kbd-macro value))
3266 (define-widget 'sexp 'editable-field
3267 "An arbitrary Lisp expression."
3268 :tag "Lisp expression"
3269 :format "%{%t%}: %v"
3271 :validate 'widget-sexp-validate
3272 :match (lambda (widget value) t)
3273 :value-to-internal 'widget-sexp-value-to-internal
3274 :value-to-external (lambda (widget value) (read value))
3275 :prompt-history 'widget-sexp-prompt-value-history
3276 :prompt-value 'widget-sexp-prompt-value)
3278 (defun widget-sexp-value-to-internal (widget value)
3279 ;; Use pp for printer representation.
3280 (let ((pp (if (symbolp value)
3281 (prin1-to-string value)
3282 (pp-to-string value))))
3283 (while (string-match "\n\\'" pp)
3284 (setq pp (substring pp 0 -1)))
3285 (if (or (string-match "\n\\'" pp)
3290 (defun widget-sexp-validate (widget)
3291 ;; Valid if we can read the string and there is no junk left after it.
3293 (insert (widget-apply widget :value-get))
3294 (goto-char (point-min))
3296 (condition-case data
3298 ;; Avoid a confusing end-of-file error.
3299 (skip-syntax-forward "\\s-")
3301 (setq err "Empty sexp -- use `nil'?")
3302 (unless (widget-apply widget :match (read (current-buffer)))
3303 (setq err (widget-get widget :type-error))))
3304 ;; Allow whitespace after expression.
3305 (skip-syntax-forward "\\s-")
3306 (if (and (not (eobp))
3308 (setq err (format "Junk at end of expression: %s"
3309 (buffer-substring (point)
3311 (end-of-file ; Avoid confusing error message.
3312 (setq err "Unbalanced sexp"))
3313 (error (setq err (error-message-string data))))
3316 (widget-put widget :error err)
3319 (defvar widget-sexp-prompt-value-history nil
3320 "History of input to `widget-sexp-prompt-value'.")
3322 (defun widget-sexp-prompt-value (widget prompt value unbound)
3323 ;; Read an arbitrary sexp.
3324 (let ((found (read-string prompt
3325 (if unbound nil (cons (prin1-to-string value) 0))
3326 (widget-get widget :prompt-history))))
3327 (let ((answer (read-from-string found)))
3328 (unless (= (cdr answer) (length found))
3329 (error "Junk at end of expression: %s"
3330 (substring found (cdr answer))))
3333 (define-widget 'restricted-sexp 'sexp
3334 "A Lisp expression restricted to values that match.
3335 To use this type, you must define :match or :match-alternatives."
3336 :type-error "The specified value is not valid"
3337 :match 'widget-restricted-sexp-match
3338 :value-to-internal (lambda (widget value)
3339 (if (widget-apply widget :match value)
3340 (prin1-to-string value)
3343 (defun widget-restricted-sexp-match (widget value)
3344 (let ((alternatives (widget-get widget :match-alternatives))
3346 (while (and alternatives (not matched))
3347 (if (cond ((functionp (car alternatives))
3348 (funcall (car alternatives) value))
3349 ((and (consp (car alternatives))
3350 (eq (car (car alternatives)) 'quote))
3351 (eq value (nth 1 (car alternatives)))))
3353 (setq alternatives (cdr alternatives)))
3356 (define-widget 'integer 'restricted-sexp
3360 :type-error "This field should contain an integer"
3361 :match-alternatives '(integerp))
3363 (define-widget 'number 'restricted-sexp
3364 "A number (floating point or integer)."
3367 :type-error "This field should contain a number (floating point or integer)"
3368 :match-alternatives '(numberp))
3370 (define-widget 'float 'restricted-sexp
3371 "A floating point number."
3372 :tag "Floating point number"
3374 :type-error "This field should contain a floating point number"
3375 :match-alternatives '(floatp))
3377 (define-widget 'character 'editable-field
3382 :format "%{%t%}: %v\n"
3383 :valid-regexp "\\`.\\'"
3384 :error "This field should contain a single character"
3385 :value-to-internal (lambda (widget value)
3388 (char-to-string value)))
3389 :value-to-external (lambda (widget value)
3393 :match (lambda (widget value)
3394 (char-valid-p value)))
3396 (define-widget 'list 'group
3399 :format "%{%t%}:\n%v")
3401 (define-widget 'vector 'group
3404 :format "%{%t%}:\n%v"
3405 :match 'widget-vector-match
3406 :value-to-internal (lambda (widget value) (append value nil))
3407 :value-to-external (lambda (widget value) (apply 'vector value)))
3409 (defun widget-vector-match (widget value)
3410 (and (vectorp value)
3411 (widget-group-match widget
3412 (widget-apply widget :value-to-internal value))))
3414 (define-widget 'cons 'group
3417 :format "%{%t%}:\n%v"
3418 :match 'widget-cons-match
3419 :value-to-internal (lambda (widget value)
3420 (list (car value) (cdr value)))
3421 :value-to-external (lambda (widget value)
3422 (apply 'cons value)))
3424 (defun widget-cons-match (widget value)
3426 (widget-group-match widget
3427 (widget-apply widget :value-to-internal value))))
3429 ;;; The `lazy' Widget.
3431 ;; Recursive datatypes.
3433 (define-widget 'lazy 'default
3434 "Base widget for recursive datastructures.
3436 The `lazy' widget will, when instantiated, contain a single inferior
3437 widget, of the widget type specified by the :type parameter. The
3438 value of the `lazy' widget is the same as the value of the inferior
3439 widget. When deriving a new widget from the 'lazy' widget, the :type
3440 parameter is allowed to refer to the widget currently being defined,
3441 thus allowing recursive datastructures to be described.
3443 The :type parameter takes the same arguments as the defcustom
3444 parameter with the same name.
3446 Most composite widgets, i.e. widgets containing other widgets, does
3447 not allow recursion. That is, when you define a new widget type, none
3448 of the inferior widgets may be of the same type you are currently
3451 In Lisp, however, it is custom to define datastructures in terms of
3452 themselves. A list, for example, is defined as either nil, or a cons
3453 cell whose cdr itself is a list. The obvious way to translate this
3454 into a widget type would be
3456 (define-widget 'my-list 'choice
3457 \"A list of sexps.\"
3459 :args '((const nil) (cons :value (nil) sexp my-list)))
3461 Here we attempt to define my-list as a choice of either the constant
3462 nil, or a cons-cell containing a sexp and my-lisp. This will not work
3463 because the `choice' widget does not allow recursion.
3465 Using the `lazy' widget you can overcome this problem, as in this
3468 (define-widget 'sexp-list 'lazy
3469 \"A list of sexps.\"
3471 :type '(choice (const nil) (cons :value (nil) sexp sexp-list)))"
3472 :format "%{%t%}: %v"
3473 ;; We don't convert :type because we want to allow recursive
3474 ;; datastructures. This is slow, so we should not create speed
3475 ;; critical widgets by deriving from this.
3476 :convert-widget 'widget-value-convert-widget
3477 :value-create 'widget-type-value-create
3478 :value-get 'widget-child-value-get
3479 :value-inline 'widget-child-value-inline
3480 :default-get 'widget-type-default-get
3481 :match 'widget-type-match
3482 :validate 'widget-child-validate)
3485 ;;; The `plist' Widget.
3489 (define-widget 'plist 'list
3491 :key-type '(symbol :tag "Key")
3492 :value-type '(sexp :tag "Value")
3493 :convert-widget 'widget-plist-convert-widget
3496 (defvar widget-plist-value-type) ;Dynamic variable
3498 (defun widget-plist-convert-widget (widget)
3499 ;; Handle `:options'.
3500 (let* ((options (widget-get widget :options))
3501 (widget-plist-value-type (widget-get widget :value-type))
3502 (other `(editable-list :inline t
3504 ,(widget-get widget :key-type)
3505 ,widget-plist-value-type)))
3507 (list `(checklist :inline t
3509 ,@(mapcar 'widget-plist-convert-option
3513 (widget-put widget :args args)
3516 (defun widget-plist-convert-option (option)
3517 ;; Convert a single plist option.
3518 (let (key-type value-type)
3520 (let ((key (nth 0 option)))
3521 (setq value-type (nth 1 option))
3524 (setq key-type `(const ,key))))
3525 (setq key-type `(const ,option)
3526 value-type widget-plist-value-type))
3527 `(group :format "Key: %v" :inline t ,key-type ,value-type)))
3530 ;;; The `alist' Widget.
3532 ;; Association lists.
3534 (define-widget 'alist 'list
3535 "An association list."
3536 :key-type '(sexp :tag "Key")
3537 :value-type '(sexp :tag "Value")
3538 :convert-widget 'widget-alist-convert-widget
3541 (defvar widget-alist-value-type) ;Dynamic variable
3543 (defun widget-alist-convert-widget (widget)
3544 ;; Handle `:options'.
3545 (let* ((options (widget-get widget :options))
3546 (widget-alist-value-type (widget-get widget :value-type))
3547 (other `(editable-list :inline t
3549 ,(widget-get widget :key-type)
3550 ,widget-alist-value-type)))
3552 (list `(checklist :inline t
3554 ,@(mapcar 'widget-alist-convert-option
3558 (widget-put widget :args args)
3561 (defun widget-alist-convert-option (option)
3562 ;; Convert a single alist option.
3563 (let (key-type value-type)
3565 (let ((key (nth 0 option)))
3566 (setq value-type (nth 1 option))
3569 (setq key-type `(const ,key))))
3570 (setq key-type `(const ,option)
3571 value-type widget-alist-value-type))
3572 `(cons :format "Key: %v" ,key-type ,value-type)))
3574 (define-widget 'choice 'menu-choice
3575 "A union of several sexp types."
3577 :format "%{%t%}: %[Value Menu%] %v"
3578 :button-prefix 'widget-push-button-prefix
3579 :button-suffix 'widget-push-button-suffix
3580 :prompt-value 'widget-choice-prompt-value)
3582 (defun widget-choice-prompt-value (widget prompt value unbound)
3584 (let ((args (widget-get widget :args))
3585 (completion-ignore-case (widget-get widget :case-fold))
3586 current choices old)
3587 ;; Find the first arg that matches VALUE.
3590 (if (widget-apply (car look) :match value)
3591 (setq old (car look)
3593 (setq look (cdr look)))))
3596 (cond ((= (length args) 0)
3598 ((= (length args) 1)
3600 ((and (= (length args) 2)
3602 (if (eq old (nth 0 args))
3607 (setq current (car args)
3610 (cons (cons (widget-apply current :menu-tag-get)
3613 (let ((val (completing-read prompt choices nil t)))
3615 (let ((try (try-completion val choices)))
3618 (cdr (assoc val choices)))
3621 (widget-prompt-value current prompt nil t)
3624 (define-widget 'radio 'radio-button-choice
3625 "A union of several sexp types."
3627 :format "%{%t%}:\n%v"
3628 :prompt-value 'widget-choice-prompt-value)
3630 (define-widget 'repeat 'editable-list
3631 "A variable length homogeneous list."
3633 :format "%{%t%}:\n%v%i\n")
3635 (define-widget 'set 'checklist
3636 "A list of members from a fixed set."
3638 :format "%{%t%}:\n%v")
3640 (define-widget 'boolean 'toggle
3641 "To be nil or non-nil, that is the question."
3643 :prompt-value 'widget-boolean-prompt-value
3644 :button-prefix 'widget-push-button-prefix
3645 :button-suffix 'widget-push-button-suffix
3646 :format "%{%t%}: %[Toggle%] %v\n"
3650 (defun widget-boolean-prompt-value (widget prompt value unbound)
3651 ;; Toggle a boolean.
3654 ;;; The `color' Widget.
3657 (define-widget 'color 'editable-field
3658 "Choose a color name (with sample)."
3659 :format "%{%t%}: %v (%{sample%})\n"
3663 :complete 'widget-color-complete
3664 :sample-face-get 'widget-color-sample-face-get
3665 :notify 'widget-color-notify
3666 :action 'widget-color-action)
3668 (defun widget-color-complete (widget)
3669 "Complete the color in WIDGET."
3670 (require 'facemenu) ; for facemenu-color-alist
3671 (let* ((prefix (buffer-substring-no-properties (widget-field-start widget)
3673 (list (or facemenu-color-alist (defined-colors)))
3674 (completion (try-completion prefix list)))
3675 (cond ((eq completion t)
3676 (message "Exact match."))
3678 (error "Can't find completion for \"%s\"" prefix))
3679 ((not (string-equal prefix completion))
3680 (insert-and-inherit (substring completion (length prefix))))
3682 (message "Making completion list...")
3683 (with-output-to-temp-buffer "*Completions*"
3684 (display-completion-list (all-completions prefix list nil)
3686 (message "Making completion list...done")))))
3688 (defun widget-color-sample-face-get (widget)
3689 (let* ((value (condition-case nil
3690 (widget-value widget)
3691 (error (widget-get widget :value)))))
3692 (if (color-defined-p value)
3693 (list (cons 'foreground-color value))
3696 (defun widget-color-action (widget &optional event)
3697 "Prompt for a color."
3698 (let* ((tag (widget-apply widget :menu-tag-get))
3699 (prompt (concat tag ": "))
3700 (value (widget-value widget))
3701 (start (widget-field-start widget))
3702 (answer (facemenu-read-color prompt)))
3703 (unless (zerop (length answer))
3704 (widget-value-set widget answer)
3706 (widget-apply widget :notify widget event))))
3708 (defun widget-color-notify (widget child &optional event)
3709 "Update the sample, and notify the parent."
3710 (overlay-put (widget-get widget :sample-overlay)
3711 'face (widget-apply widget :sample-face-get))
3712 (widget-default-notify widget child event))
3716 (defun widget-echo-help (pos)
3717 "Display help-echo text for widget at POS."
3718 (let* ((widget (widget-at pos))
3719 (help-echo (and widget (widget-get widget :help-echo))))
3720 (if (functionp help-echo)
3721 (setq help-echo (funcall help-echo widget)))
3722 (if help-echo (message "%s" (eval help-echo)))))
3728 ;;; arch-tag: a076e75e-18a1-4b46-8be5-3f317bcbc707
3729 ;;; wid-edit.el ends here