1 ;;; wid-edit.el --- Functions for creating and using widgets -*-byte-compile-dynamic: t;-*-
3 ;; Copyright (C) 1996, 1997, 1999, 2000, 2001, 2002, 2003, 2004, 2005,
4 ;; 2006, 2007, 2008, 2009 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 3 of the License, or
15 ;; (at your option) any later version.
17 ;; GNU Emacs is distributed in the hope that it will be useful,
18 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
19 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 ;; GNU General Public License for more details.
22 ;; You should have received a copy of the GNU General Public License
23 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
25 ;;; Wishlist items (from widget.texi):
27 ;; * The `menu-choice' tag should be prettier, something like the
28 ;; abbreviated menus in Open Look.
30 ;; * Finish `:tab-order'.
32 ;; * Make indentation work with glyphs and proportional fonts.
34 ;; * Add commands to show overview of object and class hierarchies to
37 ;; * Find a way to disable mouse highlight for inactive widgets.
39 ;; * Find a way to make glyphs look inactive.
41 ;; * Add `key-binding' widget.
43 ;; * Add `widget' widget for editing widget specifications.
45 ;; * Find clean way to implement variable length list. See
46 ;; `TeX-printer-list' for an explanation.
48 ;; * `C-h' in `widget-prompt-value' should give type specific help.
50 ;; * A mailto widget. [This should work OK as a url-link if with
51 ;; browse-url-browser-function' set up appropriately.]
63 (defun widget-event-point (event)
64 "Character position of the end of event if that exists, or nil."
65 (posn-point (event-end event
)))
67 (defun widget-button-release-event-p (event)
68 "Non-nil if EVENT is a mouse-button-release event object."
70 (memq (event-basic-type event
) '(mouse-1 mouse-2 mouse-3
))
71 (or (memq 'click
(event-modifiers event
))
72 (memq 'drag
(event-modifiers event
)))))
77 "Customization support for the Widget Library."
78 :link
'(custom-manual "(widget)Top")
79 :link
'(emacs-library-link :tag
"Lisp File" "widget.el")
84 (defgroup widget-documentation nil
85 "Options controlling the display of documentation strings."
88 (defgroup widget-faces nil
89 "Faces used by the widget library."
93 (defvar widget-documentation-face
'widget-documentation
94 "Face used for documentation strings in widgets.
95 This exists as a variable so it can be set locally in certain buffers.")
97 (defface widget-documentation
'((((class color
)
99 (:foreground
"lime green"))
102 (:foreground
"dark green"))
104 "Face used for documentation text."
105 :group
'widget-documentation
106 :group
'widget-faces
)
107 (define-obsolete-face-alias 'widget-documentation-face
108 'widget-documentation
"22.1")
110 (defvar widget-button-face
'widget-button
111 "Face used for buttons in widgets.
112 This exists as a variable so it can be set locally in certain buffers.")
114 (defface widget-button
'((t (:weight bold
)))
115 "Face used for widget buttons."
116 :group
'widget-faces
)
117 (define-obsolete-face-alias 'widget-button-face
'widget-button
"22.1")
119 (defcustom widget-mouse-face
'highlight
120 "Face used for widget buttons when the mouse is above them."
122 :group
'widget-faces
)
124 ;; TTY gets special definitions here and in the next defface, because
125 ;; the gray colors defined for other displays cause black text on a black
126 ;; background, at least on light-background TTYs.
127 (defface widget-field
'((((type tty
))
128 :background
"yellow3"
130 (((class grayscale color
)
132 :background
"gray85")
133 (((class grayscale color
)
135 :background
"dim gray")
138 "Face used for editable fields."
139 :group
'widget-faces
)
140 (define-obsolete-face-alias 'widget-field-face
'widget-field
"22.1")
142 (defface widget-single-line-field
'((((type tty
))
145 (((class grayscale color
)
147 :background
"gray85")
148 (((class grayscale color
)
150 :background
"dim gray")
153 "Face used for editable fields spanning only a single line."
154 :group
'widget-faces
)
155 (define-obsolete-face-alias 'widget-single-line-field-face
156 'widget-single-line-field
"22.1")
158 ;;; This causes display-table to be loaded, and not usefully.
159 ;;;(defvar widget-single-line-display-table
160 ;;; (let ((table (make-display-table)))
161 ;;; (aset table 9 "^I")
162 ;;; (aset table 10 "^J")
164 ;;; "Display table used for single-line editable fields.")
166 ;;;(when (fboundp 'set-face-display-table)
167 ;;; (set-face-display-table 'widget-single-line-field-face
168 ;;; widget-single-line-display-table))
170 ;;; Utility functions.
172 ;; These are not really widget specific.
174 (defun widget-princ-to-string (object)
175 "Return string representation of OBJECT, any Lisp object.
176 No quoting characters are used; no delimiters are printed around
177 the contents of strings."
178 (with-output-to-string
181 (defun widget-clear-undo ()
182 "Clear all undo information."
183 (buffer-disable-undo (current-buffer))
184 (buffer-enable-undo))
186 (defcustom widget-menu-max-size
40
187 "Largest number of items allowed in a popup-menu.
188 Larger menus are read through the minibuffer."
192 (defcustom widget-menu-max-shortcuts
40
193 "Largest number of items for which it works to choose one with a character.
194 For a larger number of items, the minibuffer is used."
198 (defcustom widget-menu-minibuffer-flag nil
199 "Control how to ask for a choice from the keyboard.
200 Non-nil means use the minibuffer;
201 nil means read a single character."
205 (defun widget-choose (title items
&optional event
)
206 "Choose an item from a list.
208 First argument TITLE is the name of the list.
209 Second argument ITEMS is a list whose members are either
210 (NAME . VALUE), to indicate selectable items, or just strings to
211 indicate unselectable items.
212 Optional third argument EVENT is an input event.
214 The user is asked to choose between each NAME from the items alist,
215 and the VALUE of the chosen element will be returned. If EVENT is a
216 mouse event, and the number of elements in items is less than
217 `widget-menu-max-size', a popup menu will be used, otherwise the
219 (cond ((and (< (length items
) widget-menu-max-size
)
220 event
(display-popup-menus-p))
223 (list title
(cons "" items
))))
224 ((or widget-menu-minibuffer-flag
225 (> (length items
) widget-menu-max-shortcuts
))
226 ;; Read the choice of name from the minibuffer.
227 (setq items
(widget-remove-if 'stringp items
))
228 (let ((val (completing-read (concat title
": ") items nil t
)))
230 (let ((try (try-completion val items
)))
233 (cdr (assoc val items
))))))
235 ;; Construct a menu of the choices
236 ;; and then use it for prompting for a single character.
237 (let* ((overriding-terminal-local-map (make-sparse-keymap))
239 map choice some-choice-enabled value
)
240 ;; Define SPC as a prefix char to get to this menu.
241 (define-key overriding-terminal-local-map
" "
242 (setq map
(make-sparse-keymap title
)))
243 (with-current-buffer (get-buffer-create " widget-choose")
245 (insert "Available choices:\n\n")
247 (setq choice
(car items
) items
(cdr items
))
249 (let* ((name (car choice
))
250 (function (cdr choice
)))
251 (insert (format "%c = %s\n" next-digit name
))
252 (define-key map
(vector next-digit
) function
)
253 (setq some-choice-enabled t
)))
254 ;; Allocate digits to disabled alternatives
255 ;; so that the digit of a given alternative never varies.
256 (setq next-digit
(1+ next-digit
)))
257 (insert "\nC-g = Quit"))
258 (or some-choice-enabled
259 (error "None of the choices is currently meaningful"))
260 (define-key map
[?\C-g
] 'keyboard-quit
)
261 (define-key map
[t] 'keyboard-quit)
262 (define-key map [?\M-\C-v] 'scroll-other-window)
263 (define-key map [?\M--] 'negative-argument)
264 (setcdr map (nreverse (cdr map)))
265 ;; Read a char with the menu, and return the result
266 ;; that corresponds to it.
267 (save-window-excursion
268 (let ((buf (get-buffer " widget-choose")))
269 (fit-window-to-buffer (display-buffer buf))
270 (let ((cursor-in-echo-area t)
274 (while (not (or (and (integerp char)
275 (>= char ?0) (< char next-digit))
276 (eq value 'keyboard-quit)))
277 ;; Unread a SPC to lead to our new menu.
278 (setq unread-command-events (cons ?\s unread-command-events))
279 (setq keys (read-key-sequence title))
281 (lookup-key overriding-terminal-local-map keys t)
283 (cond ((eq value 'scroll-other-window)
284 (let ((minibuffer-scroll-window
285 (get-buffer-window buf)))
287 (scroll-other-window-down
288 (window-height minibuffer-scroll-window))
289 (scroll-other-window))
291 ((eq value 'negative-argument)
295 (when (eq value 'keyboard-quit)
299 (defun widget-remove-if (predictate list)
300 (let (result (tail list))
302 (or (funcall predictate (car tail))
303 (setq result (cons (car tail) result)))
304 (setq tail (cdr tail)))
307 ;;; Widget text specifications.
309 ;; These functions are for specifying text properties.
311 ;; We can set it to nil now that get_local_map uses get_pos_property.
312 (defconst widget-field-add-space nil
313 "Non-nil means add extra space at the end of editable text fields.
314 If you don't add the space, it will become impossible to edit a zero
317 (defvar widget-field-use-before-change t
318 "Non-nil means use `before-change-functions' to track editable fields.
319 This enables the use of undo, but doesn't work on Emacs 19.34 and earlier.
320 Using before hooks also means that the :notify function can't know the
323 (defun widget-specify-field (widget from to)
324 "Specify editable button for WIDGET between FROM and TO."
325 ;; Terminating space is not part of the field, but necessary in
326 ;; order for local-map to work. Remove next sexp if local-map works
327 ;; at the end of the overlay.
330 (cond ((null (widget-get widget :size))
332 (widget-field-add-space
333 (insert-and-inherit " ")))
335 (let ((keymap (widget-get widget :keymap))
336 (face (or (widget-get widget :value-face) 'widget-field))
337 (help-echo (widget-get widget :help-echo))
338 (follow-link (widget-get widget :follow-link))
340 (or (not widget-field-add-space) (widget-get widget :size))))
341 (if (functionp help-echo)
342 (setq help-echo 'widget-mouse-help))
343 (when (= (char-before to) ?\n)
344 ;; When the last character in the field is a newline, we want to
345 ;; give it a `field' char-property of `boundary', which helps the
346 ;; C-n/C-p act more naturally when entering/leaving the field. We
347 ;; do this by making a small secondary overlay to contain just that
349 (let ((overlay (make-overlay (1- to) to nil t nil)))
350 (overlay-put overlay 'field 'boundary)
351 ;; We need the real field for tabbing.
352 (overlay-put overlay 'real-field widget)
353 ;; Use `local-map' here, not `keymap', so that normal editing
354 ;; works in the field when, say, Custom uses `suppress-keymap'.
355 (overlay-put overlay 'local-map keymap)
356 (overlay-put overlay 'face face)
357 (overlay-put overlay 'follow-link follow-link)
358 (overlay-put overlay 'help-echo help-echo))
360 (setq rear-sticky t))
361 (let ((overlay (make-overlay from to nil nil rear-sticky)))
362 (widget-put widget :field-overlay overlay)
363 ;;(overlay-put overlay 'detachable nil)
364 (overlay-put overlay 'field widget)
365 (overlay-put overlay 'local-map keymap)
366 (overlay-put overlay 'face face)
367 (overlay-put overlay 'follow-link follow-link)
368 (overlay-put overlay 'help-echo help-echo)))
369 (widget-specify-secret widget))
371 (defun widget-specify-secret (field)
372 "Replace text in FIELD with value of `:secret', if non-nil."
373 (let ((secret (widget-get field :secret))
374 (size (widget-get field :size)))
376 (let ((begin (widget-field-start field))
377 (end (widget-field-end field)))
379 (while (and (> end begin)
380 (eq (char-after (1- end)) ?\s))
381 (setq end (1- end))))
383 (let ((old (char-after begin)))
384 (unless (eq old secret)
385 (subst-char-in-region begin (1+ begin) old secret)
386 (put-text-property begin (1+ begin) 'secret old))
387 (setq begin (1+ begin))))))))
389 (defun widget-specify-button (widget from to)
390 "Specify button for WIDGET between FROM and TO."
391 (let ((overlay (make-overlay from to nil t nil))
392 (follow-link (widget-get widget :follow-link))
393 (help-echo (widget-get widget :help-echo)))
394 (widget-put widget :button-overlay overlay)
395 (if (functionp help-echo)
396 (setq help-echo 'widget-mouse-help))
397 (overlay-put overlay 'button widget)
398 (overlay-put overlay 'keymap (widget-get widget :keymap))
399 (overlay-put overlay 'evaporate t)
400 ;; We want to avoid the face with image buttons.
401 (unless (widget-get widget :suppress-face)
402 (overlay-put overlay 'face (widget-apply widget :button-face-get))
403 (overlay-put overlay 'mouse-face
404 ;; Make new list structure for the mouse-face value
405 ;; so that different widgets will have
406 ;; different `mouse-face' property values
407 ;; and will highlight separately.
408 (let ((mouse-face-value
409 (widget-apply widget :mouse-face-get)))
410 ;; If it's a list, copy it.
411 (if (listp mouse-face-value)
412 (copy-sequence mouse-face-value)
413 ;; If it's a symbol, put it in a list.
414 (list mouse-face-value)))))
415 (overlay-put overlay 'pointer 'hand)
416 (overlay-put overlay 'follow-link follow-link)
417 (overlay-put overlay 'help-echo help-echo)))
419 (defun widget-mouse-help (window overlay point)
420 "Help-echo callback for widgets whose :help-echo is a function."
421 (with-current-buffer (overlay-buffer overlay)
422 (let* ((widget (widget-at (overlay-start overlay)))
423 (help-echo (if widget (widget-get widget :help-echo))))
424 (if (functionp help-echo)
425 (funcall help-echo widget)
428 (defun widget-specify-sample (widget from to)
429 "Specify sample for WIDGET between FROM and TO."
430 (let ((overlay (make-overlay from to nil t nil)))
431 (overlay-put overlay 'face (widget-apply widget :sample-face-get))
432 (overlay-put overlay 'evaporate t)
433 (widget-put widget :sample-overlay overlay)))
435 (defun widget-specify-doc (widget from to)
436 "Specify documentation for WIDGET between FROM and TO."
437 (let ((overlay (make-overlay from to nil t nil)))
438 (overlay-put overlay 'widget-doc widget)
439 (overlay-put overlay 'face widget-documentation-face)
440 (overlay-put overlay 'evaporate t)
441 (widget-put widget :doc-overlay overlay)))
443 (defmacro widget-specify-insert (&rest form)
444 "Execute FORM without inheriting any text properties."
446 (let ((inhibit-read-only t)
447 (inhibit-modification-hooks t))
448 (narrow-to-region (point) (point))
449 (prog1 (progn ,@form)
450 (goto-char (point-max))))))
452 (defface widget-inactive
453 '((t :inherit shadow))
454 "Face used for inactive widgets."
455 :group 'widget-faces)
456 (define-obsolete-face-alias 'widget-inactive-face
457 'widget-inactive "22.1")
459 (defun widget-specify-inactive (widget from to)
460 "Make WIDGET inactive for user modifications."
461 (unless (widget-get widget :inactive)
462 (let ((overlay (make-overlay from to nil t nil)))
463 (overlay-put overlay 'face 'widget-inactive)
464 ;; This is disabled, as it makes the mouse cursor change shape.
465 ;; (overlay-put overlay 'mouse-face 'widget-inactive)
466 (overlay-put overlay 'evaporate t)
467 (overlay-put overlay 'priority 100)
468 (overlay-put overlay 'modification-hooks '(widget-overlay-inactive))
469 (widget-put widget :inactive overlay))))
471 (defun widget-overlay-inactive (&rest junk)
472 "Ignoring the arguments, signal an error."
473 (unless inhibit-read-only
474 (error "The widget here is not active")))
477 (defun widget-specify-active (widget)
478 "Make WIDGET active for user modifications."
479 (let ((inactive (widget-get widget :inactive)))
481 (delete-overlay inactive)
482 (widget-put widget :inactive nil))))
484 ;;; Widget Properties.
486 (defsubst widget-type (widget)
487 "Return the type of WIDGET. The type is a symbol."
491 (defun widgetp (widget)
492 "Return non-nil if WIDGET is a widget."
494 (get widget 'widget-type)
496 (symbolp (car widget))
497 (get (car widget) 'widget-type))))
499 (defun widget-get-indirect (widget property)
500 "In WIDGET, get the value of PROPERTY.
501 If the value is a symbol, return its binding.
502 Otherwise, just return the value."
503 (let ((value (widget-get widget property)))
508 (defun widget-member (widget property)
509 "Non-nil if there is a definition in WIDGET for PROPERTY."
510 (cond ((plist-member (cdr widget) property)
513 (widget-member (get (car widget) 'widget-type) property))
516 (defun widget-value (widget)
517 "Extract the current value of WIDGET."
519 :value-to-external (widget-apply widget :value-get)))
521 (defun widget-value-set (widget value)
522 "Set the current value of WIDGET to VALUE."
524 :value-set (widget-apply widget
525 :value-to-internal value)))
527 (defun widget-default-get (widget)
528 "Extract the default external value of WIDGET."
529 (widget-apply widget :value-to-external
530 (or (widget-get widget :value)
531 (widget-apply widget :default-get))))
533 (defun widget-match-inline (widget vals)
534 "In WIDGET, match the start of VALS."
535 (cond ((widget-get widget :inline)
536 (widget-apply widget :match-inline vals))
538 (widget-apply widget :match (car vals)))
539 (cons (list (car vals)) (cdr vals)))
542 (defun widget-apply-action (widget &optional event)
543 "Apply :action in WIDGET in response to EVENT."
544 (if (widget-apply widget :active)
545 (widget-apply widget :action event)
546 (error "Attempt to perform action on inactive widget")))
548 ;;; Helper functions.
550 ;; These are widget specific.
553 (defun widget-prompt-value (widget prompt &optional value unbound)
554 "Prompt for a value matching WIDGET, using PROMPT.
555 The current value is assumed to be VALUE, unless UNBOUND is non-nil."
556 (unless (listp widget)
557 (setq widget (list widget)))
558 (setq prompt (format "[%s] %s" (widget-type widget) prompt))
559 (setq widget (widget-convert widget))
560 (let ((answer (widget-apply widget :prompt-value prompt value unbound)))
561 (unless (widget-apply widget :match answer)
562 (error "Value does not match %S type" (car widget)))
565 (defun widget-get-sibling (widget)
566 "Get the item WIDGET is assumed to toggle.
567 This is only meaningful for radio buttons or checkboxes in a list."
568 (let* ((children (widget-get (widget-get widget :parent) :children))
572 (setq child (car children)
573 children (cdr children))
574 (when (eq (widget-get child :button) widget)
575 (throw 'child child)))
578 (defun widget-map-buttons (function &optional buffer maparg)
579 "Map FUNCTION over the buttons in BUFFER.
580 FUNCTION is called with the arguments WIDGET and MAPARG.
582 If FUNCTION returns non-nil, the walk is cancelled.
584 The arguments MAPARG, and BUFFER default to nil and (current-buffer),
586 (let ((cur (point-min))
589 (with-current-buffer buffer (overlay-lists))
591 (setq overlays (append (car overlays) (cdr overlays)))
592 (while (setq cur (pop overlays))
593 (setq widget (overlay-get cur 'button))
594 (if (and widget (funcall function widget maparg))
595 (setq overlays nil)))))
599 (defcustom widget-image-directory (file-name-as-directory
600 (expand-file-name "images/custom" data-directory))
601 "Where widget button images are located.
602 If this variable is nil, widget will try to locate the directory
607 (defcustom widget-image-enable t
608 "If non-nil, use image buttons in widgets when available."
613 (defcustom widget-image-conversion
614 '((xpm ".xpm") (gif ".gif") (png ".png") (jpeg ".jpg" ".jpeg")
616 "Conversion alist from image formats to file name suffixes."
618 :type '(repeat (cons :format "%v"
619 (symbol :tag "Image Format" unknown)
620 (repeat :tag "Suffixes"
621 (string :format "%v")))))
623 (defun widget-image-find (image)
624 "Create a graphical button from IMAGE.
625 IMAGE should either already be an image, or be a file name sans
626 extension (xpm, xbm, gif, jpg, or png) located in
627 `widget-image-directory' or otherwise where `find-image' will find it."
628 (cond ((not (and image widget-image-enable (display-graphic-p)))
629 ;; We don't want or can't use images.
632 (eq 'image (car image)))
633 ;; Already an image spec. Use it.
636 ;; A string. Look it up in relevant directories.
637 (let* ((load-path (cons widget-image-directory load-path))
639 (dolist (elt widget-image-conversion)
640 (dolist (ext (cdr elt))
641 (push (list :type (car elt) :file (concat image ext)) specs)))
642 (setq specs (nreverse specs))
648 (defvar widget-button-pressed-face 'widget-button-pressed
649 "Face used for pressed buttons in widgets.
650 This exists as a variable so it can be set locally in certain
653 (defun widget-image-insert (widget tag image &optional down inactive)
654 "In WIDGET, insert the text TAG or, if supported, IMAGE.
655 IMAGE should either be an image or an image file name sans extension
656 \(xpm, xbm, gif, jpg, or png) located in `widget-image-directory'.
658 Optional arguments DOWN and INACTIVE are used instead of IMAGE when the
659 button is pressed or inactive, respectively. These are currently ignored."
660 (if (and (display-graphic-p)
661 (setq image (widget-image-find image)))
662 (progn (widget-put widget :suppress-face t)
663 (insert-image image tag))
666 (defun widget-move-and-invoke (event)
667 "Move to where you click, and if it is an active field, invoke it."
669 (mouse-set-point event)
670 (let ((pos (widget-event-point event)))
671 (if (and pos (get-char-property pos 'button))
672 (widget-button-click event))))
676 (defgroup widget-button nil
677 "The look of various kinds of buttons."
680 (defcustom widget-button-prefix ""
681 "String used as prefix for buttons."
683 :group 'widget-button)
685 (defcustom widget-button-suffix ""
686 "String used as suffix for buttons."
688 :group 'widget-button)
690 ;;; Creating Widgets.
693 (defun widget-create (type &rest args)
694 "Create widget of TYPE.
695 The optional ARGS are additional keyword arguments."
696 (let ((widget (apply 'widget-convert type args)))
697 (widget-apply widget :create)
700 (defun widget-create-child-and-convert (parent type &rest args)
701 "As part of the widget PARENT, create a child widget TYPE.
702 The child is converted, using the keyword arguments ARGS."
703 (let ((widget (apply 'widget-convert type args)))
704 (widget-put widget :parent parent)
705 (unless (widget-get widget :indent)
706 (widget-put widget :indent (+ (or (widget-get parent :indent) 0)
707 (or (widget-get widget :extra-offset) 0)
708 (widget-get parent :offset))))
709 (widget-apply widget :create)
712 (defun widget-create-child (parent type)
713 "Create widget of TYPE."
714 (let ((widget (widget-copy type)))
715 (widget-put widget :parent parent)
716 (unless (widget-get widget :indent)
717 (widget-put widget :indent (+ (or (widget-get parent :indent) 0)
718 (or (widget-get widget :extra-offset) 0)
719 (widget-get parent :offset))))
720 (widget-apply widget :create)
723 (defun widget-create-child-value (parent type value)
724 "Create widget of TYPE with value VALUE."
725 (let ((widget (widget-copy type)))
726 (widget-put widget :value (widget-apply widget :value-to-internal value))
727 (widget-put widget :parent parent)
728 (unless (widget-get widget :indent)
729 (widget-put widget :indent (+ (or (widget-get parent :indent) 0)
730 (or (widget-get widget :extra-offset) 0)
731 (widget-get parent :offset))))
732 (widget-apply widget :create)
736 (defun widget-delete (widget)
738 (widget-apply widget :delete))
740 (defun widget-copy (widget)
741 "Make a deep copy of WIDGET."
742 (widget-apply (copy-sequence widget) :copy))
744 (defun widget-convert (type &rest args)
745 "Convert TYPE to a widget without inserting it in the buffer.
746 The optional ARGS are additional keyword arguments."
747 ;; Don't touch the type.
748 (let* ((widget (if (symbolp type)
750 (copy-sequence type)))
754 ;; First set the :args keyword.
755 (while (cdr current) ;Look in the type.
756 (if (and (keywordp (cadr current))
757 ;; If the last element is a keyword,
758 ;; it is still the :args element,
759 ;; even though it is a keyword.
761 (if (eq (cadr current) :args)
762 ;; If :args is explicitly specified, obey it.
764 ;; Some other irrelevant keyword.
765 (setq current (cdr (cdr current))))
766 (setcdr current (list :args (cdr current)))
768 (while (and args (not done)) ;Look in ARGS.
769 (cond ((eq (car args) :args)
770 ;; Handle explicit specification of :args.
771 (setq args (cadr args)
773 ((keywordp (car args))
774 (setq args (cddr args)))
777 (widget-put widget :args args))
778 ;; Then Convert the widget.
781 (let ((convert-widget (plist-get (cdr type) :convert-widget)))
783 (setq widget (funcall convert-widget widget))))
784 (setq type (get (car type) 'widget-type)))
785 ;; Finally set the keyword args.
787 (let ((next (nth 0 keys)))
790 (widget-put widget next (nth 1 keys))
791 (setq keys (nthcdr 2 keys)))
793 ;; Convert the :value to internal format.
794 (if (widget-member widget :value)
796 :value (widget-apply widget
798 (widget-get widget :value))))
799 ;; Return the newly create widget.
803 (defun widget-insert (&rest args)
804 "Call `insert' with ARGS even if surrounding text is read only."
805 (let ((inhibit-read-only t)
806 (inhibit-modification-hooks t))
807 (apply 'insert args)))
809 (defun widget-convert-text (type from to
810 &optional button-from button-to
812 "Return a widget of type TYPE with endpoint FROM TO.
813 No text will be inserted to the buffer, instead the text between FROM
814 and TO will be used as the widgets end points. If optional arguments
815 BUTTON-FROM and BUTTON-TO are given, these will be used as the widgets
817 Optional ARGS are extra keyword arguments for TYPE."
818 (let ((widget (apply 'widget-convert type :delete 'widget-leave-text args))
819 (from (copy-marker from))
820 (to (copy-marker to)))
821 (set-marker-insertion-type from t)
822 (set-marker-insertion-type to nil)
823 (widget-put widget :from from)
824 (widget-put widget :to to)
826 (widget-specify-button widget button-from button-to))
829 (defun widget-convert-button (type from to &rest args)
830 "Return a widget of type TYPE with endpoint FROM TO.
831 Optional ARGS are extra keyword arguments for TYPE.
832 No text will be inserted to the buffer, instead the text between FROM
833 and TO will be used as the widgets end points, as well as the widgets
835 (apply 'widget-convert-text type from to from to args))
837 (defun widget-leave-text (widget)
838 "Remove markers and overlays from WIDGET and its children."
839 (let ((button (widget-get widget :button-overlay))
840 (sample (widget-get widget :sample-overlay))
841 (doc (widget-get widget :doc-overlay))
842 (field (widget-get widget :field-overlay)))
843 (set-marker (widget-get widget :from) nil)
844 (set-marker (widget-get widget :to) nil)
846 (delete-overlay button))
848 (delete-overlay sample))
850 (delete-overlay doc))
852 (delete-overlay field))
853 (mapc 'widget-leave-text (widget-get widget :children))))
855 ;;; Keymap and Commands.
857 ;; This alias exists only so that one can choose in doc-strings (e.g.
858 ;; Custom-mode) which key-binding of widget-keymap one wants to refer to.
859 ;; http://lists.gnu.org/archive/html/emacs-devel/2008-11/msg00480.html
860 (define-obsolete-function-alias 'advertised-widget-backward
861 'widget-backward "23.2")
864 (defvar widget-keymap
865 (let ((map (make-sparse-keymap)))
866 (define-key map "\t" 'widget-forward)
867 (define-key map "\e\t" 'widget-backward)
868 (define-key map [(shift tab)] 'widget-backward)
869 (put 'widget-backward :advertised-binding [(shift tab)])
870 (define-key map [backtab] 'widget-backward)
871 (define-key map [down-mouse-2] 'widget-button-click)
872 (define-key map [down-mouse-1] 'widget-button-click)
873 ;; The following definition needs to avoid using escape sequences that
874 ;; might get converted to ^M when building loaddefs.el
875 (define-key map [(control ?m)] 'widget-button-press)
877 "Keymap containing useful binding for buffers containing widgets.
878 Recommended as a parent keymap for modes using widgets.")
880 (defvar widget-global-map global-map
881 "Keymap used for events a widget does not handle itself.")
882 (make-variable-buffer-local 'widget-global-map)
884 (defvar widget-field-keymap
885 (let ((map (copy-keymap widget-keymap)))
886 (define-key map "\C-k" 'widget-kill-line)
887 (define-key map "\M-\t" 'widget-complete)
888 (define-key map "\C-m" 'widget-field-activate)
889 ;; Since the widget code uses a `field' property to identify fields,
890 ;; ordinary beginning-of-line does the right thing.
891 ;; (define-key map "\C-a" 'widget-beginning-of-line)
892 (define-key map "\C-e" 'widget-end-of-line)
894 "Keymap used inside an editable field.")
896 (defvar widget-text-keymap
897 (let ((map (copy-keymap widget-keymap)))
898 ;; Since the widget code uses a `field' property to identify fields,
899 ;; ordinary beginning-of-line does the right thing.
900 ;; (define-key map "\C-a" 'widget-beginning-of-line)
901 (define-key map "\C-e" 'widget-end-of-line)
903 "Keymap used inside a text field.")
905 (defun widget-field-activate (pos &optional event)
906 "Invoke the editable field at point."
908 (let ((field (widget-field-at pos)))
910 (widget-apply-action field event)
912 (lookup-key widget-global-map (this-command-keys))))))
914 (defface widget-button-pressed
915 '((((min-colors 88) (class color))
916 (:foreground "red1"))
920 (:weight bold :underline t)))
921 "Face used for pressed buttons."
922 :group 'widget-faces)
923 (define-obsolete-face-alias 'widget-button-pressed-face
924 'widget-button-pressed "22.1")
926 (defvar widget-button-click-moves-point nil
927 "If non-nil, `widget-button-click' moves point to a button after invoking it.
928 If nil, point returns to its original position after invoking a button.")
930 (defun widget-button-click (event)
931 "Invoke the button that the mouse is pointing at."
933 (if (widget-event-point event)
934 (let* ((oevent event)
935 (mouse-1 (memq (event-basic-type event) '(mouse-1 down-mouse-1)))
936 (pos (widget-event-point event))
937 (start (event-start event))
938 (button (get-char-property
939 pos 'button (and (windowp (posn-window start))
940 (window-buffer (posn-window start)))))
942 (when (or (null button)
943 (catch 'button-press-cancelled
944 ;; Mouse click on a widget button. Do the following
945 ;; in a save-excursion so that the click on the button
946 ;; doesn't change point.
947 (save-selected-window
948 (select-window (posn-window (event-start event)))
950 (goto-char (posn-point (event-start event)))
951 (let* ((overlay (widget-get button :button-overlay))
952 (pressed-face (or (widget-get button :pressed-face)
953 widget-button-pressed-face))
954 (face (overlay-get overlay 'face))
955 (mouse-face (overlay-get overlay 'mouse-face)))
957 ;; Read events, including mouse-movement
958 ;; events, waiting for a release event. If we
959 ;; began with a mouse-1 event and receive a
960 ;; movement event, that means the user wants
961 ;; to perform drag-selection, so cancel the
962 ;; button press and do the default mouse-1
963 ;; action. For mouse-2, just highlight/
964 ;; unhighlight the button the mouse was
965 ;; initially on when we move over it.
967 (when face ; avoid changing around image
968 (overlay-put overlay 'face pressed-face)
969 (overlay-put overlay 'mouse-face pressed-face))
970 (unless (widget-apply button :mouse-down-action event)
971 (let ((track-mouse t))
972 (while (not (widget-button-release-event-p event))
973 (setq event (read-event))
974 (when (and mouse-1 (mouse-movement-p event))
975 (push event unread-command-events)
977 (throw 'button-press-cancelled t))
978 (unless (or (integerp event)
979 (memq (car event) '(switch-frame select-window))
980 (eq (car event) 'scroll-bar-movement))
981 (setq pos (widget-event-point event))
983 (eq (get-char-property pos 'button)
986 (overlay-put overlay 'face pressed-face)
987 (overlay-put overlay 'mouse-face pressed-face))
988 (overlay-put overlay 'face face)
989 (overlay-put overlay 'mouse-face mouse-face))))))
991 ;; When mouse is released over the button, run
992 ;; its action function.
993 (when (and pos (eq (get-char-property pos 'button) button))
995 (widget-apply-action button event)
996 (if widget-button-click-moves-point
997 (setq newpoint (point)))))
998 (overlay-put overlay 'face face)
999 (overlay-put overlay 'mouse-face mouse-face))))
1001 (if newpoint (goto-char newpoint))
1002 ;; This loses if the widget action switches windows. -- cyd
1003 ;; (unless (pos-visible-in-window-p (widget-event-point event))
1004 ;; (mouse-set-point event)
1005 ;; (beginning-of-line)
1009 (let ((up t) command)
1010 ;; Mouse click not on a widget button. Find the global
1011 ;; command to run, and check whether it is bound to an
1014 (cond ((setq command ;down event
1015 (lookup-key widget-global-map [down-mouse-1]))
1017 ((setq command ;up event
1018 (lookup-key widget-global-map [mouse-1]))))
1019 (cond ((setq command ;down event
1020 (lookup-key widget-global-map [down-mouse-2]))
1022 ((setq command ;up event
1023 (lookup-key widget-global-map [mouse-2])))))
1025 ;; Don't execute up events twice.
1026 (while (not (widget-button-release-event-p event))
1027 (setq event (read-event))))
1029 (call-interactively command)))))
1030 (message "You clicked somewhere weird.")))
1032 (defun widget-button-press (pos &optional event)
1033 "Invoke button at POS."
1035 (let ((button (get-char-property pos 'button)))
1037 (widget-apply-action button event)
1038 (let ((command (lookup-key widget-global-map (this-command-keys))))
1039 (when (commandp command)
1040 (call-interactively command))))))
1042 (defun widget-tabable-at (&optional pos)
1043 "Return the tabable widget at POS, or nil.
1044 POS defaults to the value of (point)."
1045 (let ((widget (widget-at pos)))
1047 (let ((order (widget-get widget :tab-order)))
1053 (defvar widget-use-overlay-change t
1054 "If non-nil, use overlay change functions to tab around in the buffer.
1055 This is much faster, but doesn't work reliably on Emacs 19.34.")
1057 (defun widget-move (arg)
1058 "Move point to the ARG next field or button.
1059 ARG may be negative to move backward."
1060 (or (bobp) (> arg 0) (backward-char))
1063 (old (widget-tabable-at)))
1067 (goto-char (point-min))
1068 (setq wrapped (1+ wrapped)))
1069 (widget-use-overlay-change
1070 (goto-char (next-overlay-change (point))))
1075 (error "No buttons or fields found"))
1076 (let ((new (widget-tabable-at)))
1078 (unless (eq new old)
1084 (goto-char (point-max))
1085 (setq wrapped (1+ wrapped)))
1086 (widget-use-overlay-change
1087 (goto-char (previous-overlay-change (point))))
1092 (error "No buttons or fields found"))
1093 (let ((new (widget-tabable-at)))
1095 (unless (eq new old)
1096 (setq arg (1+ arg))))))
1097 (let ((new (widget-tabable-at)))
1098 (while (eq (widget-tabable-at) new)
1101 (widget-echo-help (point))
1102 (run-hooks 'widget-move-hook))
1104 (defun widget-forward (arg)
1105 "Move point to the next field or button.
1106 With optional ARG, move across that many fields."
1108 (run-hooks 'widget-forward-hook)
1111 (defun widget-backward (arg)
1112 "Move point to the previous field or button.
1113 With optional ARG, move across that many fields."
1115 (run-hooks 'widget-backward-hook)
1116 (widget-move (- arg)))
1118 ;; Since the widget code uses a `field' property to identify fields,
1119 ;; ordinary beginning-of-line does the right thing.
1120 (defalias 'widget-beginning-of-line 'beginning-of-line)
1122 (defun widget-end-of-line ()
1123 "Go to end of field or end of line, whichever is first.
1124 Trailing spaces at the end of padded fields are not considered part of
1127 ;; Ordinary end-of-line does the right thing, because we're inside
1128 ;; text with a `field' property.
1131 ;; ... except that we want to ignore trailing spaces in fields that
1132 ;; aren't terminated by a newline, because they are used as padding,
1133 ;; and ignored when extracting the entered value of the field.
1134 (skip-chars-backward " " (field-beginning (1- (point))))))
1136 (defun widget-kill-line ()
1137 "Kill to end of field or end of line, whichever is first."
1139 (let* ((field (widget-field-find (point)))
1140 (end (and field (widget-field-end field))))
1141 (if (and field (> (line-beginning-position 2) end))
1142 (kill-region (point) end)
1143 (call-interactively 'kill-line))))
1145 (defcustom widget-complete-field (lookup-key global-map "\M-\t")
1146 "Default function to call for completion inside fields."
1147 :options '(ispell-complete-word complete-tag lisp-complete-symbol)
1151 (defun widget-narrow-to-field ()
1154 (let ((field (widget-field-find (point))))
1156 (narrow-to-region (line-beginning-position) (line-end-position)))))
1158 (defun widget-complete ()
1159 "Complete content of editable field from point.
1160 When not inside a field, move to the previous button or field."
1162 (let ((field (widget-field-find (point))))
1164 (widget-apply field :complete))
1165 (error "Not in an editable field")))
1167 ;;; Setting up the buffer.
1169 (defvar widget-field-new nil
1170 "List of all newly created editable fields in the buffer.")
1171 (make-variable-buffer-local 'widget-field-new)
1173 (defvar widget-field-list nil
1174 "List of all editable fields in the buffer.")
1175 (make-variable-buffer-local 'widget-field-list)
1177 (defun widget-at (&optional pos)
1178 "The button or field at POS (default, point)."
1179 (or (get-char-property (or pos (point)) 'button)
1180 (widget-field-at pos)))
1183 (defun widget-setup ()
1184 "Setup current buffer so editing string widgets works."
1185 (let ((inhibit-read-only t)
1186 (inhibit-modification-hooks t)
1188 (while widget-field-new
1189 (setq field (car widget-field-new)
1190 widget-field-new (cdr widget-field-new)
1191 widget-field-list (cons field widget-field-list))
1192 (let ((from (car (widget-get field :field-overlay)))
1193 (to (cdr (widget-get field :field-overlay))))
1194 (widget-specify-field field
1195 (marker-position from) (marker-position to))
1196 (set-marker from nil)
1197 (set-marker to nil))))
1199 (widget-add-change))
1201 (defvar widget-field-last nil)
1202 ;; Last field containing point.
1203 (make-variable-buffer-local 'widget-field-last)
1205 (defvar widget-field-was nil)
1206 ;; The widget data before the change.
1207 (make-variable-buffer-local 'widget-field-was)
1209 (defun widget-field-at (pos)
1210 "Return the widget field at POS, or nil if none."
1211 (let ((field (get-char-property (or pos (point)) 'field)))
1212 (if (eq field 'boundary)
1213 (get-char-property (or pos (point)) 'real-field)
1216 (defun widget-field-buffer (widget)
1217 "Return the buffer of WIDGET's editing field."
1218 (let ((overlay (widget-get widget :field-overlay)))
1219 (cond ((overlayp overlay)
1220 (overlay-buffer overlay))
1222 (marker-buffer (car overlay))))))
1224 (defun widget-field-start (widget)
1225 "Return the start of WIDGET's editing field."
1226 (let ((overlay (widget-get widget :field-overlay)))
1227 (if (overlayp overlay)
1228 (overlay-start overlay)
1231 (defun widget-field-end (widget)
1232 "Return the end of WIDGET's editing field."
1233 (let ((overlay (widget-get widget :field-overlay)))
1234 ;; Don't subtract one if local-map works at the end of the overlay,
1235 ;; or if a special `boundary' field has been added after the widget
1237 (if (overlayp overlay)
1238 ;; Don't proceed if overlay has been removed from buffer.
1239 (when (overlay-buffer overlay)
1240 (if (and (not (eq (with-current-buffer
1241 (widget-field-buffer widget)
1243 ;; `widget-narrow-to-field' can be
1244 ;; active when this function is called
1245 ;; from an change-functions hook. So
1246 ;; temporarily remove field narrowing
1247 ;; before to call `get-char-property'.
1249 (get-char-property (overlay-end overlay)
1252 (or widget-field-add-space
1253 (null (widget-get widget :size))))
1254 (1- (overlay-end overlay))
1255 (overlay-end overlay)))
1258 (defun widget-field-text-end (widget)
1259 (let ((to (widget-field-end widget))
1260 (size (widget-get widget :size)))
1261 (if (or (null size) (zerop size))
1263 (let ((from (widget-field-start widget)))
1265 (with-current-buffer (widget-field-buffer widget)
1266 (while (and (> to from)
1267 (eq (char-after (1- to)) ?\s))
1271 (defun widget-field-find (pos)
1272 "Return the field at POS.
1273 Unlike (get-char-property POS 'field), this works with empty fields too."
1274 (let ((fields widget-field-list)
1277 (setq field (car fields)
1278 fields (cdr fields))
1279 (when (and (<= (widget-field-start field) pos)
1280 (<= pos (widget-field-end field)))
1282 (error "Overlapping fields"))
1283 (setq found field)))
1286 (defun widget-before-change (from to)
1287 ;; This is how, for example, a variable changes its state to `modified'.
1288 ;; when it is being edited.
1289 (unless inhibit-read-only
1290 (let ((from-field (widget-field-find from))
1291 (to-field (widget-field-find to)))
1292 (cond ((not (eq from-field to-field))
1293 (add-hook 'post-command-hook 'widget-add-change nil t)
1294 (signal 'text-read-only
1295 '("Change should be restricted to a single field")))
1297 (add-hook 'post-command-hook 'widget-add-change nil t)
1298 (signal 'text-read-only
1299 '("Attempt to change text outside editable field")))
1300 (widget-field-use-before-change
1301 (widget-apply from-field :notify from-field))))))
1303 (defun widget-add-change ()
1304 (remove-hook 'post-command-hook 'widget-add-change t)
1305 (add-hook 'before-change-functions 'widget-before-change nil t)
1306 (add-hook 'after-change-functions 'widget-after-change nil t))
1308 (defun widget-after-change (from to old)
1309 "Adjust field size and text properties."
1310 (let ((field (widget-field-find from))
1311 (other (widget-field-find to)))
1313 (unless (eq field other)
1314 (error "Change in different fields"))
1315 (let ((size (widget-get field :size)))
1317 (let ((begin (widget-field-start field))
1318 (end (widget-field-end field)))
1319 (cond ((< (- end begin) size)
1323 (insert-char ?\s (- (+ begin size) end))))
1324 ((> (- end begin) size)
1325 ;; Field too large and
1326 (if (or (< (point) (+ begin size))
1328 ;; Point is outside extra space.
1329 (setq begin (+ begin size))
1330 ;; Point is within the extra space.
1331 (setq begin (point)))
1334 (while (and (eq (preceding-char) ?\s)
1336 (delete-backward-char 1)))))))
1337 (widget-specify-secret field))
1338 (widget-apply field :notify field))))
1340 ;;; Widget Functions
1342 ;; These functions are used in the definition of multiple widgets.
1344 (defun widget-parent-action (widget &optional event)
1345 "Tell :parent of WIDGET to handle the :action.
1346 Optional EVENT is the event that triggered the action."
1347 (widget-apply (widget-get widget :parent) :action event))
1349 (defun widget-children-value-delete (widget)
1350 "Delete all :children and :buttons in WIDGET."
1351 (mapc 'widget-delete (widget-get widget :children))
1352 (widget-put widget :children nil)
1353 (mapc 'widget-delete (widget-get widget :buttons))
1354 (widget-put widget :buttons nil))
1356 (defun widget-children-validate (widget)
1357 "All the :children must be valid."
1358 (let ((children (widget-get widget :children))
1360 (while (and children (not found))
1361 (setq child (car children)
1362 children (cdr children)
1363 found (widget-apply child :validate)))
1366 (defun widget-child-value-get (widget)
1367 "Get the value of the first member of :children in WIDGET."
1368 (widget-value (car (widget-get widget :children))))
1370 (defun widget-child-value-inline (widget)
1371 "Get the inline value of the first member of :children in WIDGET."
1372 (widget-apply (car (widget-get widget :children)) :value-inline))
1374 (defun widget-child-validate (widget)
1375 "The result of validating the first member of :children in WIDGET."
1376 (widget-apply (car (widget-get widget :children)) :validate))
1378 (defun widget-type-value-create (widget)
1379 "Convert and instantiate the value of the :type attribute of WIDGET.
1380 Store the newly created widget in the :children attribute.
1382 The value of the :type attribute should be an unconverted widget type."
1383 (let ((value (widget-get widget :value))
1384 (type (widget-get widget :type)))
1385 (widget-put widget :children
1386 (list (widget-create-child-value widget
1387 (widget-convert type)
1390 (defun widget-type-default-get (widget)
1391 "Get default value from the :type attribute of WIDGET.
1393 The value of the :type attribute should be an unconverted widget type."
1394 (widget-default-get (widget-convert (widget-get widget :type))))
1396 (defun widget-type-match (widget value)
1397 "Non-nil if the :type value of WIDGET matches VALUE.
1399 The value of the :type attribute should be an unconverted widget type."
1400 (widget-apply (widget-convert (widget-get widget :type)) :match value))
1402 (defun widget-types-copy (widget)
1403 "Copy :args as widget types in WIDGET."
1404 (widget-put widget :args (mapcar 'widget-copy (widget-get widget :args)))
1407 ;; Made defsubst to speed up face editor creation.
1408 (defsubst widget-types-convert-widget (widget)
1409 "Convert :args as widget types in WIDGET."
1410 (widget-put widget :args (mapcar 'widget-convert (widget-get widget :args)))
1413 (defun widget-value-convert-widget (widget)
1414 "Initialize :value from :args in WIDGET."
1415 (let ((args (widget-get widget :args)))
1417 (widget-put widget :value (car args))
1418 ;; Don't convert :value here, as this is done in `widget-convert'.
1419 ;; (widget-put widget :value (widget-apply widget
1420 ;; :value-to-internal (car args)))
1421 (widget-put widget :args nil)))
1424 (defun widget-value-value-get (widget)
1425 "Return the :value property of WIDGET."
1426 (widget-get widget :value))
1428 ;;; The `default' Widget.
1430 (define-widget 'default nil
1431 "Basic widget other widgets are derived from."
1432 :value-to-internal (lambda (widget value) value)
1433 :value-to-external (lambda (widget value) value)
1434 :button-prefix 'widget-button-prefix
1435 :button-suffix 'widget-button-suffix
1436 :complete 'widget-default-complete
1437 :create 'widget-default-create
1440 :format-handler 'widget-default-format-handler
1441 :button-face-get 'widget-default-button-face-get
1442 :mouse-face-get 'widget-default-mouse-face-get
1443 :sample-face-get 'widget-default-sample-face-get
1444 :delete 'widget-default-delete
1446 :value-set 'widget-default-value-set
1447 :value-inline 'widget-default-value-inline
1448 :value-delete 'ignore
1449 :default-get 'widget-default-default-get
1450 :menu-tag-get 'widget-default-menu-tag-get
1452 :active 'widget-default-active
1453 :activate 'widget-specify-active
1454 :deactivate 'widget-default-deactivate
1455 :mouse-down-action #'ignore
1456 :action 'widget-default-action
1457 :notify 'widget-default-notify
1458 :prompt-value 'widget-default-prompt-value)
1460 (defun widget-default-complete (widget)
1461 "Call the value of the :complete-function property of WIDGET.
1462 If that does not exist, call the value of `widget-complete-field'."
1463 (call-interactively (or (widget-get widget :complete-function)
1464 widget-complete-field)))
1466 (defun widget-default-create (widget)
1467 "Create WIDGET at point in the current buffer."
1468 (widget-specify-insert
1469 (let ((from (point))
1470 button-begin button-end
1471 sample-begin sample-end
1474 (insert (widget-get widget :format))
1476 ;; Parse escapes in format.
1477 (while (re-search-forward "%\\(.\\)" nil t)
1478 (let ((escape (char-after (match-beginning 1))))
1479 (delete-backward-char 2)
1480 (cond ((eq escape ?%)
1483 (setq button-begin (point))
1484 (insert (widget-get-indirect widget :button-prefix)))
1486 (insert (widget-get-indirect widget :button-suffix))
1487 (setq button-end (point)))
1489 (setq sample-begin (point)))
1491 (setq sample-end (point)))
1493 (when (widget-get widget :indent)
1495 (insert-char ?\s (widget-get widget :indent))))
1497 (let ((image (widget-get widget :tag-glyph))
1498 (tag (widget-get widget :tag)))
1500 (widget-image-insert widget (or tag "image") image))
1504 (princ (widget-get widget :value)
1505 (current-buffer))))))
1507 (let ((doc (widget-get widget :doc)))
1509 (setq doc-begin (point))
1511 (while (eq (preceding-char) ?\n)
1512 (delete-backward-char 1))
1514 (setq doc-end (point)))))
1516 (widget-add-documentation-string-button widget))
1518 (if (and button-begin (not button-end))
1519 (widget-apply widget :value-create)
1520 (setq value-pos (point))))
1522 (widget-apply widget :format-handler escape)))))
1523 ;; Specify button, sample, and doc, and insert value.
1524 (and button-begin button-end
1525 (widget-specify-button widget button-begin button-end))
1526 (and sample-begin sample-end
1527 (widget-specify-sample widget sample-begin sample-end))
1528 (and doc-begin doc-end
1529 (widget-specify-doc widget doc-begin doc-end))
1531 (goto-char value-pos)
1532 (widget-apply widget :value-create)))
1533 (let ((from (point-min-marker))
1534 (to (point-max-marker)))
1535 (set-marker-insertion-type from t)
1536 (set-marker-insertion-type to nil)
1537 (widget-put widget :from from)
1538 (widget-put widget :to to)))
1539 (widget-clear-undo))
1541 (defun widget-default-format-handler (widget escape)
1542 (error "Unknown escape `%c'" escape))
1544 (defun widget-default-button-face-get (widget)
1545 ;; Use :button-face or widget-button-face
1546 (or (widget-get widget :button-face)
1547 (let ((parent (widget-get widget :parent)))
1549 (widget-apply parent :button-face-get)
1550 widget-button-face))))
1552 (defun widget-default-mouse-face-get (widget)
1553 ;; Use :mouse-face or widget-mouse-face
1554 (or (widget-get widget :mouse-face)
1555 (let ((parent (widget-get widget :parent)))
1557 (widget-apply parent :mouse-face-get)
1558 widget-mouse-face))))
1560 (defun widget-default-sample-face-get (widget)
1561 ;; Use :sample-face.
1562 (widget-get widget :sample-face))
1564 (defun widget-default-delete (widget)
1565 "Remove widget from the buffer."
1566 (let ((from (widget-get widget :from))
1567 (to (widget-get widget :to))
1568 (inactive-overlay (widget-get widget :inactive))
1569 (button-overlay (widget-get widget :button-overlay))
1570 (sample-overlay (widget-get widget :sample-overlay))
1571 (doc-overlay (widget-get widget :doc-overlay))
1572 (inhibit-modification-hooks t)
1573 (inhibit-read-only t))
1574 (widget-apply widget :value-delete)
1575 (widget-children-value-delete widget)
1576 (when inactive-overlay
1577 (delete-overlay inactive-overlay))
1578 (when button-overlay
1579 (delete-overlay button-overlay))
1580 (when sample-overlay
1581 (delete-overlay sample-overlay))
1583 (delete-overlay doc-overlay))
1585 ;; Kludge: this doesn't need to be true for empty formats.
1586 (delete-region from to))
1587 (set-marker from nil)
1588 (set-marker to nil))
1589 (widget-clear-undo))
1591 (defun widget-default-value-set (widget value)
1592 "Recreate widget with new value."
1593 (let* ((old-pos (point))
1594 (from (copy-marker (widget-get widget :from)))
1595 (to (copy-marker (widget-get widget :to)))
1596 (offset (if (and (<= from old-pos) (<= old-pos to))
1597 (if (>= old-pos (1- to))
1599 (- old-pos from)))))
1600 ;;??? Bug: this ought to insert the new value before deleting the old one,
1601 ;; so that markers on either side of the value automatically
1602 ;; stay on the same side. -- rms.
1604 (goto-char (widget-get widget :from))
1605 (widget-apply widget :delete)
1606 (widget-put widget :value value)
1607 (widget-apply widget :create))
1610 (goto-char (+ (widget-get widget :to) offset 1))
1611 (goto-char (min (+ from offset) (1- (widget-get widget :to))))))))
1613 (defun widget-default-value-inline (widget)
1614 "Wrap value in a list unless it is inline."
1615 (if (widget-get widget :inline)
1616 (widget-value widget)
1617 (list (widget-value widget))))
1619 (defun widget-default-default-get (widget)
1621 (widget-get widget :value))
1623 (defun widget-default-menu-tag-get (widget)
1624 "Use tag or value for menus."
1625 (or (widget-get widget :menu-tag)
1626 (widget-get widget :tag)
1627 (widget-princ-to-string (widget-get widget :value))))
1629 (defun widget-default-active (widget)
1630 "Return t if this widget is active (user modifiable)."
1631 (or (widget-get widget :always-active)
1632 (and (not (widget-get widget :inactive))
1633 (let ((parent (widget-get widget :parent)))
1635 (widget-apply parent :active))))))
1637 (defun widget-default-deactivate (widget)
1638 "Make WIDGET inactive for user modifications."
1639 (widget-specify-inactive widget
1640 (widget-get widget :from)
1641 (widget-get widget :to)))
1643 (defun widget-default-action (widget &optional event)
1644 "Notify the parent when a widget changes."
1645 (let ((parent (widget-get widget :parent)))
1647 (widget-apply parent :notify widget event))))
1649 (defun widget-default-notify (widget child &optional event)
1650 "Pass notification to parent."
1651 (widget-default-action widget event))
1653 (defun widget-default-prompt-value (widget prompt value unbound)
1654 "Read an arbitrary value."
1655 (eval-minibuffer prompt))
1657 (defun widget-docstring (widget)
1658 "Return the documentation string specificied by WIDGET, or nil if none.
1659 If WIDGET has a `:doc' property, that specifies the documentation string.
1660 Otherwise, try the `:documentation-property' property. If this
1661 is a function, call it with the widget's value as an argument; if
1662 it is a symbol, use this symbol together with the widget's value
1663 as the argument to `documentation-property'."
1664 (let ((doc (or (widget-get widget :doc)
1665 (let ((doc-prop (widget-get widget :documentation-property))
1666 (value (widget-get widget :value)))
1667 (cond ((functionp doc-prop)
1668 (funcall doc-prop value))
1670 (documentation-property value doc-prop)))))))
1671 (when (and (stringp doc) (> (length doc) 0))
1672 ;; Remove any redundant `*' in the beginning.
1673 (when (eq (aref doc 0) ?*)
1674 (setq doc (substring doc 1)))
1675 ;; Remove trailing newlines.
1676 (when (string-match "\n+\\'" doc)
1677 (setq doc (substring doc 0 (match-beginning 0))))
1680 ;;; The `item' Widget.
1682 (define-widget 'item 'default
1683 "Constant items for inclusion in other widgets."
1684 :convert-widget 'widget-value-convert-widget
1685 :value-create 'widget-item-value-create
1686 :value-delete 'ignore
1687 :value-get 'widget-value-value-get
1688 :match 'widget-item-match
1689 :match-inline 'widget-item-match-inline
1690 :action 'widget-item-action
1693 (defun widget-item-value-create (widget)
1694 "Insert the printed representation of the value."
1695 (princ (widget-get widget :value) (current-buffer)))
1697 (defun widget-item-match (widget value)
1698 ;; Match if the value is the same.
1699 (equal (widget-get widget :value) value))
1701 (defun widget-item-match-inline (widget values)
1702 ;; Match if the value is the same.
1703 (let ((value (widget-get widget :value)))
1705 (<= (length value) (length values))
1706 (let ((head (widget-sublist values 0 (length value))))
1707 (and (equal head value)
1708 (cons head (widget-sublist values (length value))))))))
1710 (defun widget-sublist (list start &optional end)
1711 "Return the sublist of LIST from START to END.
1712 If END is omitted, it defaults to the length of LIST."
1713 (if (> start 0) (setq list (nthcdr start list)))
1715 (unless (<= end start)
1716 (setq list (copy-sequence list))
1717 (setcdr (nthcdr (- end start 1) list) nil)
1719 (copy-sequence list)))
1721 (defun widget-item-action (widget &optional event)
1722 ;; Just notify itself.
1723 (widget-apply widget :notify widget event))
1725 ;;; The `push-button' Widget.
1727 ;; (defcustom widget-push-button-gui t
1728 ;; "If non-nil, use GUI push buttons when available."
1732 ;; Cache already created GUI objects.
1733 ;; (defvar widget-push-button-cache nil)
1735 (defcustom widget-push-button-prefix "["
1736 "String used as prefix for buttons."
1738 :group 'widget-button)
1740 (defcustom widget-push-button-suffix "]"
1741 "String used as suffix for buttons."
1743 :group 'widget-button)
1745 (define-widget 'push-button 'item
1746 "A pushable button."
1749 :value-create 'widget-push-button-value-create
1752 (defun widget-push-button-value-create (widget)
1753 "Insert text representing the `on' and `off' states."
1754 (let* ((tag (or (widget-get widget :tag)
1755 (widget-get widget :value)))
1756 (tag-glyph (widget-get widget :tag-glyph))
1757 (text (concat widget-push-button-prefix
1758 tag widget-push-button-suffix)))
1760 (widget-image-insert widget text tag-glyph)
1763 ;; (defun widget-gui-action (widget)
1764 ;; "Apply :action for WIDGET."
1765 ;; (widget-apply-action widget (this-command-keys)))
1767 ;;; The `link' Widget.
1769 (defcustom widget-link-prefix "["
1770 "String used as prefix for links."
1772 :group 'widget-button)
1774 (defcustom widget-link-suffix "]"
1775 "String used as suffix for links."
1777 :group 'widget-button)
1779 (define-widget 'link 'item
1781 :button-prefix 'widget-link-prefix
1782 :button-suffix 'widget-link-suffix
1783 :follow-link 'mouse-face
1784 :help-echo "Follow the link."
1787 ;;; The `info-link' Widget.
1789 (define-widget 'info-link 'link
1790 "A link to an info file."
1791 :action 'widget-info-link-action)
1793 (defun widget-info-link-action (widget &optional event)
1794 "Open the info node specified by WIDGET."
1795 (info (widget-value widget)))
1797 ;;; The `url-link' Widget.
1799 (define-widget 'url-link 'link
1800 "A link to an www page."
1801 :action 'widget-url-link-action)
1803 (defun widget-url-link-action (widget &optional event)
1804 "Open the URL specified by WIDGET."
1805 (browse-url (widget-value widget)))
1807 ;;; The `function-link' Widget.
1809 (define-widget 'function-link 'link
1810 "A link to an Emacs function."
1811 :action 'widget-function-link-action)
1813 (defun widget-function-link-action (widget &optional event)
1814 "Show the function specified by WIDGET."
1815 (describe-function (widget-value widget)))
1817 ;;; The `variable-link' Widget.
1819 (define-widget 'variable-link 'link
1820 "A link to an Emacs variable."
1821 :action 'widget-variable-link-action)
1823 (defun widget-variable-link-action (widget &optional event)
1824 "Show the variable specified by WIDGET."
1825 (describe-variable (widget-value widget)))
1827 ;;; The `file-link' Widget.
1829 (define-widget 'file-link 'link
1831 :action 'widget-file-link-action)
1833 (defun widget-file-link-action (widget &optional event)
1834 "Find the file specified by WIDGET."
1835 (find-file (widget-value widget)))
1837 ;;; The `emacs-library-link' Widget.
1839 (define-widget 'emacs-library-link 'link
1840 "A link to an Emacs Lisp library file."
1841 :action 'widget-emacs-library-link-action)
1843 (defun widget-emacs-library-link-action (widget &optional event)
1844 "Find the Emacs library file specified by WIDGET."
1845 (find-file (locate-library (widget-value widget))))
1847 ;;; The `emacs-commentary-link' Widget.
1849 (define-widget 'emacs-commentary-link 'link
1850 "A link to Commentary in an Emacs Lisp library file."
1851 :action 'widget-emacs-commentary-link-action)
1853 (defun widget-emacs-commentary-link-action (widget &optional event)
1854 "Find the Commentary section of the Emacs file specified by WIDGET."
1855 (finder-commentary (widget-value widget)))
1857 ;;; The `editable-field' Widget.
1859 (define-widget 'editable-field 'default
1860 "An editable text field.
1861 Note: In an `editable-field' widget, the `%v' escape must be preceded
1862 by some other text in the `:format' string (if specified)."
1863 :convert-widget 'widget-value-convert-widget
1864 :keymap widget-field-keymap
1866 :help-echo "M-TAB: complete field; RET: enter value"
1868 :prompt-internal 'widget-field-prompt-internal
1869 :prompt-history 'widget-field-history
1870 :prompt-value 'widget-field-prompt-value
1871 :action 'widget-field-action
1872 :validate 'widget-field-validate
1874 :error "Field's value doesn't match allowed forms"
1875 :value-create 'widget-field-value-create
1876 :value-delete 'widget-field-value-delete
1877 :value-get 'widget-field-value-get
1878 :match 'widget-field-match)
1880 (defvar widget-field-history nil
1881 "History of field minibuffer edits.")
1883 (defun widget-field-prompt-internal (widget prompt initial history)
1884 "Read string for WIDGET prompting with PROMPT.
1885 INITIAL is the initial input and HISTORY is a symbol containing
1887 (read-string prompt initial history))
1889 (defun widget-field-prompt-value (widget prompt value unbound)
1890 "Prompt for a string."
1891 (widget-apply widget
1893 (widget-apply widget
1894 :prompt-internal prompt
1896 (cons (widget-apply widget
1897 :value-to-internal value)
1899 (widget-get widget :prompt-history))))
1901 (defvar widget-edit-functions nil)
1903 (defun widget-field-action (widget &optional event)
1904 "Move to next field."
1906 (run-hook-with-args 'widget-edit-functions widget))
1908 (defun widget-field-validate (widget)
1909 "Valid if the content matches `:valid-regexp'."
1910 (unless (string-match (widget-get widget :valid-regexp)
1911 (widget-apply widget :value-get))
1914 (defun widget-field-value-create (widget)
1915 "Create an editable text field."
1916 (let ((size (widget-get widget :size))
1917 (value (widget-get widget :value))
1919 ;; This is changed to a real overlay in `widget-setup'. We
1920 ;; need the end points to behave differently until
1921 ;; `widget-setup' is called.
1922 (overlay (cons (make-marker) (make-marker))))
1923 (widget-put widget :field-overlay overlay)
1926 (< (length value) size)
1927 (insert-char ?\s (- size (length value))))
1928 (unless (memq widget widget-field-list)
1929 (setq widget-field-new (cons widget widget-field-new)))
1930 (move-marker (cdr overlay) (point))
1931 (set-marker-insertion-type (cdr overlay) nil)
1934 (move-marker (car overlay) from)
1935 (set-marker-insertion-type (car overlay) t)))
1937 (defun widget-field-value-delete (widget)
1938 "Remove the widget from the list of active editing fields."
1939 (setq widget-field-list (delq widget widget-field-list))
1940 (setq widget-field-new (delq widget widget-field-new))
1941 ;; These are nil if the :format string doesn't contain `%v'.
1942 (let ((overlay (widget-get widget :field-overlay)))
1943 (when (overlayp overlay)
1944 (delete-overlay overlay))))
1946 (defun widget-field-value-get (widget)
1947 "Return current text in editing field."
1948 (let ((from (widget-field-start widget))
1949 (to (widget-field-text-end widget))
1950 (buffer (widget-field-buffer widget))
1951 (size (widget-get widget :size))
1952 (secret (widget-get widget :secret))
1953 (old (current-buffer)))
1957 (let ((result (buffer-substring-no-properties from to)))
1960 (while (< (+ from index) to)
1962 (get-char-property (+ from index) 'secret))
1963 (setq index (1+ index)))))
1966 (widget-get widget :value))))
1968 (defun widget-field-match (widget value)
1969 ;; Match any string.
1972 ;;; The `text' Widget.
1974 (define-widget 'text 'editable-field
1975 "A multiline text area."
1976 :keymap widget-text-keymap)
1978 ;;; The `menu-choice' Widget.
1980 (define-widget 'menu-choice 'default
1981 "A menu of options."
1982 :convert-widget 'widget-types-convert-widget
1983 :copy 'widget-types-copy
1984 :format "%[%t%]: %v"
1987 :void '(item :format "invalid (%t)\n")
1988 :value-create 'widget-choice-value-create
1989 :value-get 'widget-child-value-get
1990 :value-inline 'widget-child-value-inline
1991 :default-get 'widget-choice-default-get
1992 :mouse-down-action 'widget-choice-mouse-down-action
1993 :action 'widget-choice-action
1994 :error "Make a choice"
1995 :validate 'widget-choice-validate
1996 :match 'widget-choice-match
1997 :match-inline 'widget-choice-match-inline)
1999 (defun widget-choice-value-create (widget)
2000 "Insert the first choice that matches the value."
2001 (let ((value (widget-get widget :value))
2002 (args (widget-get widget :args))
2003 (explicit (widget-get widget :explicit-choice))
2007 ;; If the user specified the choice for this value,
2008 ;; respect that choice.
2009 (widget-put widget :children (list (widget-create-child-value
2010 widget explicit value)))
2011 (widget-put widget :choice explicit)
2012 (widget-put widget :explicit-choice nil))
2014 (setq current (car args)
2016 (when (widget-apply current :match value)
2017 (widget-put widget :children (list (widget-create-child-value
2018 widget current value)))
2019 (widget-put widget :choice current)
2023 (let ((void (widget-get widget :void)))
2024 (widget-put widget :children (list (widget-create-child-and-convert
2025 widget void :value value)))
2026 (widget-put widget :choice void))))))
2028 (defun widget-choice-default-get (widget)
2029 ;; Get default for the first choice.
2030 (widget-default-get (car (widget-get widget :args))))
2032 (defcustom widget-choice-toggle nil
2033 "If non-nil, a binary choice will just toggle between the values.
2034 Otherwise, the user will explicitly have to choose between the values
2035 when he invoked the menu."
2039 (defun widget-choice-mouse-down-action (widget &optional event)
2040 ;; Return non-nil if we need a menu.
2041 (let ((args (widget-get widget :args))
2042 (old (widget-get widget :choice)))
2043 (cond ((not (display-popup-menus-p))
2044 ;; No place to pop up a menu.
2046 ((< (length args) 2)
2047 ;; Empty or singleton list, just return the value.
2049 ((> (length args) widget-menu-max-size)
2050 ;; Too long, prompt.
2052 ((> (length args) 2)
2053 ;; Reasonable sized list, use menu.
2055 ((and widget-choice-toggle (memq old args))
2059 ;; Ask which of the two.
2062 (defun widget-choice-action (widget &optional event)
2064 (let ((args (widget-get widget :args))
2065 (old (widget-get widget :choice))
2066 (tag (widget-apply widget :menu-tag-get))
2067 (completion-ignore-case (widget-get widget :case-fold))
2070 ;; Remember old value.
2071 (if (and old (not (widget-apply widget :validate)))
2072 (let* ((external (widget-value widget))
2073 (internal (widget-apply old :value-to-internal external)))
2074 (widget-put old :value internal)))
2077 (cond ((= (length args) 0)
2079 ((= (length args) 1)
2081 ((and widget-choice-toggle
2084 (if (eq old (nth 0 args))
2089 (setq current (car args)
2092 (cons (cons (widget-apply current :menu-tag-get)
2095 (setq this-explicit t)
2096 (widget-choose tag (reverse choices) event))))
2098 ;; If this was an explicit user choice, record the choice,
2099 ;; so that widget-choice-value-create will respect it.
2101 (widget-put widget :explicit-choice current))
2102 (widget-value-set widget (widget-default-get current))
2104 (widget-apply widget :notify widget event)))
2105 (run-hook-with-args 'widget-edit-functions widget))
2107 (defun widget-choice-validate (widget)
2108 ;; Valid if we have made a valid choice.
2109 (if (eq (widget-get widget :void) (widget-get widget :choice))
2111 (widget-apply (car (widget-get widget :children)) :validate)))
2113 (defun widget-choice-match (widget value)
2114 ;; Matches if one of the choices matches.
2115 (let ((args (widget-get widget :args))
2117 (while (and args (not found))
2118 (setq current (car args)
2120 found (widget-apply current :match value)))
2123 (defun widget-choice-match-inline (widget values)
2124 ;; Matches if one of the choices matches.
2125 (let ((args (widget-get widget :args))
2127 (while (and args (null found))
2128 (setq current (car args)
2130 found (widget-match-inline current values)))
2133 ;;; The `toggle' Widget.
2135 (define-widget 'toggle 'item
2136 "Toggle between two states."
2138 :value-create 'widget-toggle-value-create
2139 :action 'widget-toggle-action
2140 :match (lambda (widget value) t)
2144 (defun widget-toggle-value-create (widget)
2145 "Insert text representing the `on' and `off' states."
2146 (if (widget-value widget)
2147 (let ((image (widget-get widget :on-glyph)))
2148 (and (display-graphic-p)
2150 (not (eq (car image) 'image))
2151 (widget-put widget :on-glyph (setq image (eval image))))
2152 (widget-image-insert widget
2153 (widget-get widget :on)
2155 (let ((image (widget-get widget :off-glyph)))
2156 (and (display-graphic-p)
2158 (not (eq (car image) 'image))
2159 (widget-put widget :off-glyph (setq image (eval image))))
2160 (widget-image-insert widget (widget-get widget :off) image))))
2162 (defun widget-toggle-action (widget &optional event)
2164 (widget-value-set widget (not (widget-value widget)))
2165 (widget-apply widget :notify widget event)
2166 (run-hook-with-args 'widget-edit-functions widget))
2168 ;;; The `checkbox' Widget.
2170 (define-widget 'checkbox 'toggle
2171 "A checkbox toggle."
2176 ;; We could probably do the same job as the images using single
2177 ;; space characters in a boxed face with a stretch specification to
2178 ;; make them square.
2179 :on-glyph '(create-image "\300\300\141\143\067\076\034\030"
2180 'xbm t :width 8 :height 8
2181 :background "grey75" ; like default mode line
2186 :off-glyph '(create-image (make-string 8 0)
2187 'xbm t :width 8 :height 8
2188 :background "grey75"
2192 :help-echo "Toggle this item."
2193 :action 'widget-checkbox-action)
2195 (defun widget-checkbox-action (widget &optional event)
2196 "Toggle checkbox, notify parent, and set active state of sibling."
2197 (widget-toggle-action widget event)
2198 (let ((sibling (widget-get-sibling widget)))
2200 (if (widget-value widget)
2201 (widget-apply sibling :activate)
2202 (widget-apply sibling :deactivate))
2203 (widget-clear-undo))))
2205 ;;; The `checklist' Widget.
2207 (define-widget 'checklist 'default
2208 "A multiple choice widget."
2209 :convert-widget 'widget-types-convert-widget
2210 :copy 'widget-types-copy
2213 :entry-format "%b %v"
2215 :value-create 'widget-checklist-value-create
2216 :value-get 'widget-checklist-value-get
2217 :validate 'widget-checklist-validate
2218 :match 'widget-checklist-match
2219 :match-inline 'widget-checklist-match-inline)
2221 (defun widget-checklist-value-create (widget)
2222 ;; Insert all values
2223 (let ((alist (widget-checklist-match-find widget (widget-get widget :value)))
2224 (args (widget-get widget :args)))
2226 (widget-checklist-add-item widget (car args) (assq (car args) alist))
2227 (setq args (cdr args)))
2228 (widget-put widget :children (nreverse (widget-get widget :children)))))
2230 (defun widget-checklist-add-item (widget type chosen)
2231 "Create checklist item in WIDGET of type TYPE.
2232 If the item is checked, CHOSEN is a cons whose cdr is the value."
2233 (and (eq (preceding-char) ?\n)
2234 (widget-get widget :indent)
2235 (insert-char ?\s (widget-get widget :indent)))
2236 (widget-specify-insert
2237 (let* ((children (widget-get widget :children))
2238 (buttons (widget-get widget :buttons))
2239 (button-args (or (widget-get type :sibling-args)
2240 (widget-get widget :button-args)))
2243 (insert (widget-get widget :entry-format))
2245 ;; Parse % escapes in format.
2246 (while (re-search-forward "%\\([bv%]\\)" nil t)
2247 (let ((escape (char-after (match-beginning 1))))
2248 (delete-backward-char 2)
2249 (cond ((eq escape ?%)
2252 (setq button (apply 'widget-create-child-and-convert
2254 :value (not (null chosen))
2259 (let ((child (widget-create-child widget type)))
2260 (widget-apply child :deactivate)
2262 ((widget-get type :inline)
2263 (widget-create-child-value
2264 widget type (cdr chosen)))
2266 (widget-create-child-value
2267 widget type (car (cdr chosen)))))))
2269 (error "Unknown escape `%c'" escape)))))
2270 ;; Update properties.
2271 (and button child (widget-put child :button button))
2272 (and button (widget-put widget :buttons (cons button buttons)))
2273 (and child (widget-put widget :children (cons child children))))))
2275 (defun widget-checklist-match (widget values)
2276 ;; All values must match a type in the checklist.
2278 (null (cdr (widget-checklist-match-inline widget values)))))
2280 (defun widget-checklist-match-inline (widget values)
2281 ;; Find the values which match a type in the checklist.
2282 (let ((greedy (widget-get widget :greedy))
2283 (args (copy-sequence (widget-get widget :args)))
2286 (let ((answer (widget-checklist-match-up args values)))
2288 (let ((vals (widget-match-inline answer values)))
2289 (setq found (append found (car vals))
2291 args (delq answer args))))
2293 (setq rest (append rest (list (car values)))
2294 values (cdr values)))
2296 (setq rest (append rest values)
2300 (defun widget-checklist-match-find (widget vals)
2301 "Find the vals which match a type in the checklist.
2302 Return an alist of (TYPE MATCH)."
2303 (let ((greedy (widget-get widget :greedy))
2304 (args (copy-sequence (widget-get widget :args)))
2307 (let ((answer (widget-checklist-match-up args vals)))
2309 (let ((match (widget-match-inline answer vals)))
2310 (setq found (cons (cons answer (car match)) found)
2312 args (delq answer args))))
2314 (setq vals (cdr vals)))
2319 (defun widget-checklist-match-up (args vals)
2320 "Return the first type from ARGS that matches VALS."
2321 (let (current found)
2322 (while (and args (null found))
2323 (setq current (car args)
2325 found (widget-match-inline current vals)))
2329 (defun widget-checklist-value-get (widget)
2330 ;; The values of all selected items.
2331 (let ((children (widget-get widget :children))
2334 (setq child (car children)
2335 children (cdr children))
2336 (if (widget-value (widget-get child :button))
2337 (setq result (append result (widget-apply child :value-inline)))))
2340 (defun widget-checklist-validate (widget)
2341 ;; Ticked chilren must be valid.
2342 (let ((children (widget-get widget :children))
2344 (while (and children (not found))
2345 (setq child (car children)
2346 children (cdr children)
2347 button (widget-get child :button)
2348 found (and (widget-value button)
2349 (widget-apply child :validate))))
2352 ;;; The `option' Widget
2354 (define-widget 'option 'checklist
2355 "An widget with an optional item."
2358 ;;; The `choice-item' Widget.
2360 (define-widget 'choice-item 'item
2361 "Button items that delegate action events to their parents."
2362 :action 'widget-parent-action
2363 :format "%[%t%] \n")
2365 ;;; The `radio-button' Widget.
2367 (define-widget 'radio-button 'toggle
2368 "A radio button for use in the `radio' widget."
2369 :notify 'widget-radio-button-notify
2376 :off-glyph "radio0")
2378 (defun widget-radio-button-notify (widget child &optional event)
2380 (widget-apply (widget-get widget :parent) :action widget event))
2382 ;;; The `radio-button-choice' Widget.
2384 (define-widget 'radio-button-choice 'default
2385 "Select one of multiple options."
2386 :convert-widget 'widget-types-convert-widget
2387 :copy 'widget-types-copy
2390 :entry-format "%b %v"
2391 :value-create 'widget-radio-value-create
2392 :value-get 'widget-radio-value-get
2393 :value-inline 'widget-radio-value-inline
2394 :value-set 'widget-radio-value-set
2395 :error "You must push one of the buttons"
2396 :validate 'widget-radio-validate
2397 :match 'widget-choice-match
2398 :match-inline 'widget-choice-match-inline
2399 :action 'widget-radio-action)
2401 (defun widget-radio-value-create (widget)
2402 ;; Insert all values
2403 (let ((args (widget-get widget :args))
2406 (setq arg (car args)
2408 (widget-radio-add-item widget arg))))
2410 (defun widget-radio-add-item (widget type)
2411 "Add to radio widget WIDGET a new radio button item of type TYPE."
2412 ;; (setq type (widget-convert type))
2413 (and (eq (preceding-char) ?\n)
2414 (widget-get widget :indent)
2415 (insert-char ?\s (widget-get widget :indent)))
2416 (widget-specify-insert
2417 (let* ((value (widget-get widget :value))
2418 (children (widget-get widget :children))
2419 (buttons (widget-get widget :buttons))
2420 (button-args (or (widget-get type :sibling-args)
2421 (widget-get widget :button-args)))
2423 (chosen (and (null (widget-get widget :choice))
2424 (widget-apply type :match value)))
2426 (insert (widget-get widget :entry-format))
2428 ;; Parse % escapes in format.
2429 (while (re-search-forward "%\\([bv%]\\)" nil t)
2430 (let ((escape (char-after (match-beginning 1))))
2431 (delete-backward-char 2)
2432 (cond ((eq escape ?%)
2435 (setq button (apply 'widget-create-child-and-convert
2436 widget 'radio-button
2437 :value (not (null chosen))
2440 (setq child (if chosen
2441 (widget-create-child-value
2443 (widget-create-child widget type)))
2445 (widget-apply child :deactivate)))
2447 (error "Unknown escape `%c'" escape)))))
2448 ;; Update properties.
2450 (widget-put widget :choice type))
2452 (widget-put child :button button)
2453 (widget-put widget :buttons (nconc buttons (list button))))
2455 (widget-put widget :children (nconc children (list child))))
2458 (defun widget-radio-value-get (widget)
2459 ;; Get value of the child widget.
2460 (let ((chosen (widget-radio-chosen widget)))
2461 (and chosen (widget-value chosen))))
2463 (defun widget-radio-chosen (widget)
2464 "Return the widget representing the chosen radio button."
2465 (let ((children (widget-get widget :children))
2468 (setq current (car children)
2469 children (cdr children))
2470 (when (widget-apply (widget-get current :button) :value-get)
2475 (defun widget-radio-value-inline (widget)
2476 ;; Get value of the child widget.
2477 (let ((children (widget-get widget :children))
2480 (setq current (car children)
2481 children (cdr children))
2482 (when (widget-apply (widget-get current :button) :value-get)
2483 (setq found (widget-apply current :value-inline)
2487 (defun widget-radio-value-set (widget value)
2488 ;; We can't just delete and recreate a radio widget, since children
2489 ;; can be added after the original creation and won't be recreated
2491 (let ((children (widget-get widget :children))
2494 (setq current (car children)
2495 children (cdr children))
2496 (let* ((button (widget-get current :button))
2497 (match (and (not found)
2498 (widget-apply current :match value))))
2499 (widget-value-set button match)
2502 (widget-value-set current value)
2503 (widget-apply current :activate))
2504 (widget-apply current :deactivate))
2505 (setq found (or found match))))))
2507 (defun widget-radio-validate (widget)
2508 ;; Valid if we have made a valid choice.
2509 (let ((children (widget-get widget :children))
2510 current found button)
2511 (while (and children (not found))
2512 (setq current (car children)
2513 children (cdr children)
2514 button (widget-get current :button)
2515 found (widget-apply button :value-get)))
2517 (widget-apply current :validate)
2520 (defun widget-radio-action (widget child event)
2521 ;; Check if a radio button was pressed.
2522 (let ((children (widget-get widget :children))
2523 (buttons (widget-get widget :buttons))
2525 (when (memq child buttons)
2527 (setq current (car children)
2528 children (cdr children))
2529 (let* ((button (widget-get current :button)))
2530 (cond ((eq child button)
2531 (widget-value-set button t)
2532 (widget-apply current :activate))
2533 ((widget-value button)
2534 (widget-value-set button nil)
2535 (widget-apply current :deactivate)))))))
2536 ;; Pass notification to parent.
2537 (widget-apply widget :notify child event))
2539 ;;; The `insert-button' Widget.
2541 (define-widget 'insert-button 'push-button
2542 "An insert button for the `editable-list' widget."
2544 :help-echo "Insert a new item into the list at this position."
2545 :action 'widget-insert-button-action)
2547 (defun widget-insert-button-action (widget &optional event)
2548 ;; Ask the parent to insert a new item.
2549 (widget-apply (widget-get widget :parent)
2550 :insert-before (widget-get widget :widget)))
2552 ;;; The `delete-button' Widget.
2554 (define-widget 'delete-button 'push-button
2555 "A delete button for the `editable-list' widget."
2557 :help-echo "Delete this item from the list."
2558 :action 'widget-delete-button-action)
2560 (defun widget-delete-button-action (widget &optional event)
2561 ;; Ask the parent to insert a new item.
2562 (widget-apply (widget-get widget :parent)
2563 :delete-at (widget-get widget :widget)))
2565 ;;; The `editable-list' Widget.
2567 ;; (defcustom widget-editable-list-gui nil
2568 ;; "If non-nil, use GUI push-buttons in editable list when available."
2572 (define-widget 'editable-list 'default
2573 "A variable list of widgets of the same type."
2574 :convert-widget 'widget-types-convert-widget
2575 :copy 'widget-types-copy
2578 :format-handler 'widget-editable-list-format-handler
2579 :entry-format "%i %d %v"
2580 :value-create 'widget-editable-list-value-create
2581 :value-get 'widget-editable-list-value-get
2582 :validate 'widget-children-validate
2583 :match 'widget-editable-list-match
2584 :match-inline 'widget-editable-list-match-inline
2585 :insert-before 'widget-editable-list-insert-before
2586 :delete-at 'widget-editable-list-delete-at)
2588 (defun widget-editable-list-format-handler (widget escape)
2589 ;; We recognize the insert button.
2590 ;; (let ((widget-push-button-gui widget-editable-list-gui))
2591 (cond ((eq escape ?i)
2592 (and (widget-get widget :indent)
2593 (insert-char ?\s (widget-get widget :indent)))
2594 (apply 'widget-create-child-and-convert
2595 widget 'insert-button
2596 (widget-get widget :append-button-args)))
2598 (widget-default-format-handler widget escape)))
2602 (defun widget-editable-list-value-create (widget)
2603 ;; Insert all values
2604 (let* ((value (widget-get widget :value))
2605 (type (nth 0 (widget-get widget :args)))
2607 (widget-put widget :value-pos (copy-marker (point)))
2608 (set-marker-insertion-type (widget-get widget :value-pos) t)
2610 (let ((answer (widget-match-inline type value)))
2612 (setq children (cons (widget-editable-list-entry-create
2614 (if (widget-get type :inline)
2621 (widget-put widget :children (nreverse children))))
2623 (defun widget-editable-list-value-get (widget)
2624 ;; Get value of the child widget.
2625 (apply 'append (mapcar (lambda (child) (widget-apply child :value-inline))
2626 (widget-get widget :children))))
2628 (defun widget-editable-list-match (widget value)
2629 ;; Value must be a list and all the members must match the type.
2631 (null (cdr (widget-editable-list-match-inline widget value)))))
2633 (defun widget-editable-list-match-inline (widget value)
2634 (let ((type (nth 0 (widget-get widget :args)))
2637 (while (and value ok)
2638 (let ((answer (widget-match-inline type value)))
2640 (setq found (append found (car answer))
2643 (cons found value)))
2645 (defun widget-editable-list-insert-before (widget before)
2646 ;; Insert a new child in the list of children.
2648 (let ((children (widget-get widget :children))
2649 (inhibit-read-only t)
2650 before-change-functions
2651 after-change-functions)
2653 (goto-char (widget-get before :entry-from)))
2655 (goto-char (widget-get widget :value-pos))))
2656 (let ((child (widget-editable-list-entry-create
2658 (when (< (widget-get child :entry-from) (widget-get widget :from))
2659 (set-marker (widget-get widget :from)
2660 (widget-get child :entry-from)))
2661 (if (eq (car children) before)
2662 (widget-put widget :children (cons child children))
2663 (while (not (eq (car (cdr children)) before))
2664 (setq children (cdr children)))
2665 (setcdr children (cons child (cdr children)))))))
2667 (widget-apply widget :notify widget))
2669 (defun widget-editable-list-delete-at (widget child)
2670 ;; Delete child from list of children.
2672 (let ((buttons (copy-sequence (widget-get widget :buttons)))
2674 (inhibit-read-only t)
2675 before-change-functions
2676 after-change-functions)
2678 (setq button (car buttons)
2679 buttons (cdr buttons))
2680 (when (eq (widget-get button :widget) child)
2682 :buttons (delq button (widget-get widget :buttons)))
2683 (widget-delete button))))
2684 (let ((entry-from (widget-get child :entry-from))
2685 (entry-to (widget-get child :entry-to))
2686 (inhibit-read-only t)
2687 before-change-functions
2688 after-change-functions)
2689 (widget-delete child)
2690 (delete-region entry-from entry-to)
2691 (set-marker entry-from nil)
2692 (set-marker entry-to nil))
2693 (widget-put widget :children (delq child (widget-get widget :children))))
2695 (widget-apply widget :notify widget))
2697 (defun widget-editable-list-entry-create (widget value conv)
2698 ;; Create a new entry to the list.
2699 (let ((type (nth 0 (widget-get widget :args)))
2700 ;; (widget-push-button-gui widget-editable-list-gui)
2701 child delete insert)
2702 (widget-specify-insert
2704 (and (widget-get widget :indent)
2705 (insert-char ?\s (widget-get widget :indent)))
2706 (insert (widget-get widget :entry-format)))
2707 ;; Parse % escapes in format.
2708 (while (re-search-forward "%\\(.\\)" nil t)
2709 (let ((escape (char-after (match-beginning 1))))
2710 (delete-backward-char 2)
2711 (cond ((eq escape ?%)
2714 (setq insert (apply 'widget-create-child-and-convert
2715 widget 'insert-button
2716 (widget-get widget :insert-button-args))))
2718 (setq delete (apply 'widget-create-child-and-convert
2719 widget 'delete-button
2720 (widget-get widget :delete-button-args))))
2723 (setq child (widget-create-child-value
2725 (setq child (widget-create-child-value
2726 widget type (widget-default-get type)))))
2728 (error "Unknown escape `%c'" escape)))))
2729 (let ((buttons (widget-get widget :buttons)))
2730 (if insert (push insert buttons))
2731 (if delete (push delete buttons))
2732 (widget-put widget :buttons buttons))
2733 (let ((entry-from (point-min-marker))
2734 (entry-to (point-max-marker)))
2735 (set-marker-insertion-type entry-from t)
2736 (set-marker-insertion-type entry-to nil)
2737 (widget-put child :entry-from entry-from)
2738 (widget-put child :entry-to entry-to)))
2739 (if insert (widget-put insert :widget child))
2740 (if delete (widget-put delete :widget child))
2743 ;;; The `group' Widget.
2745 (define-widget 'group 'default
2746 "A widget which groups other widgets inside."
2747 :convert-widget 'widget-types-convert-widget
2748 :copy 'widget-types-copy
2750 :value-create 'widget-group-value-create
2751 :value-get 'widget-editable-list-value-get
2752 :default-get 'widget-group-default-get
2753 :validate 'widget-children-validate
2754 :match 'widget-group-match
2755 :match-inline 'widget-group-match-inline)
2757 (defun widget-group-value-create (widget)
2758 ;; Create each component.
2759 (let ((args (widget-get widget :args))
2760 (value (widget-get widget :value))
2761 arg answer children)
2763 (setq arg (car args)
2765 answer (widget-match-inline arg value)
2767 (and (eq (preceding-char) ?\n)
2768 (widget-get widget :indent)
2769 (insert-char ?\s (widget-get widget :indent)))
2770 (push (cond ((null answer)
2771 (widget-create-child widget arg))
2772 ((widget-get arg :inline)
2773 (widget-create-child-value widget arg (car answer)))
2775 (widget-create-child-value widget arg (car (car answer)))))
2777 (widget-put widget :children (nreverse children))))
2779 (defun widget-group-default-get (widget)
2780 ;; Get the default of the components.
2781 (mapcar 'widget-default-get (widget-get widget :args)))
2783 (defun widget-group-match (widget values)
2784 ;; Match if the components match.
2786 (let ((match (widget-group-match-inline widget values)))
2787 (and match (null (cdr match))))))
2789 (defun widget-group-match-inline (widget vals)
2790 ;; Match if the components match.
2791 (let ((args (widget-get widget :args))
2792 argument answer found)
2794 (setq argument (car args)
2796 answer (widget-match-inline argument vals))
2798 (setq vals (cdr answer)
2799 found (append found (car answer)))
2803 (cons found vals))))
2805 ;;; The `visibility' Widget.
2807 (define-widget 'visibility 'item
2808 "An indicator and manipulator for hidden items."
2814 :value-create 'widget-visibility-value-create
2815 :action 'widget-toggle-action
2816 :match (lambda (widget value) t))
2818 (defun widget-visibility-value-create (widget)
2819 ;; Insert text representing the `on' and `off' states.
2820 (let ((on (widget-get widget :on))
2821 (off (widget-get widget :off)))
2823 (setq on (concat widget-push-button-prefix
2825 widget-push-button-suffix))
2828 (setq off (concat widget-push-button-prefix
2830 widget-push-button-suffix))
2832 (if (widget-value widget)
2833 (widget-image-insert widget on "down" "down-pushed")
2834 (widget-image-insert widget off "right" "right-pushed"))))
2836 ;;; The `documentation-link' Widget.
2838 ;; This is a helper widget for `documentation-string'.
2840 (define-widget 'documentation-link 'link
2841 "Link type used in documentation strings."
2843 :help-echo "Describe this symbol"
2844 :action 'widget-documentation-link-action)
2846 (defun widget-documentation-link-action (widget &optional event)
2847 "Display documentation for WIDGET's value. Ignore optional argument EVENT."
2848 (let* ((string (widget-get widget :value))
2849 (symbol (intern string)))
2850 (if (and (fboundp symbol) (boundp symbol))
2851 ;; If there are two doc strings, give the user a way to pick one.
2852 (apropos (concat "\\`" (regexp-quote string) "\\'"))
2853 (if (fboundp symbol)
2854 (describe-function symbol)
2855 (describe-variable symbol)))))
2857 (defcustom widget-documentation-links t
2858 "Add hyperlinks to documentation strings when non-nil."
2860 :group 'widget-documentation)
2862 (defcustom widget-documentation-link-regexp "`\\([^\n`' ]+\\)'"
2863 "Regexp for matching potential links in documentation strings.
2864 The first group should be the link itself."
2866 :group 'widget-documentation)
2868 (defcustom widget-documentation-link-p 'intern-soft
2869 "Predicate used to test if a string is useful as a link.
2870 The value should be a function. The function will be called with one
2871 argument, a string, and should return non-nil if there should be a
2872 link for that string."
2874 :options '(widget-documentation-link-p)
2875 :group 'widget-documentation)
2877 (defcustom widget-documentation-link-type 'documentation-link
2878 "Widget type used for links in documentation strings."
2880 :group 'widget-documentation)
2882 (defun widget-documentation-link-add (widget from to)
2883 (widget-specify-doc widget from to)
2884 (when widget-documentation-links
2885 (let ((regexp widget-documentation-link-regexp)
2886 (buttons (widget-get widget :buttons))
2887 (widget-mouse-face (default-value 'widget-mouse-face))
2888 (widget-button-face widget-documentation-face)
2889 (widget-button-pressed-face widget-documentation-face))
2892 (while (re-search-forward regexp to t)
2893 (let ((name (match-string 1))
2894 (begin (match-beginning 1))
2895 (end (match-end 1)))
2896 (when (funcall widget-documentation-link-p name)
2897 (push (widget-convert-button widget-documentation-link-type
2898 begin end :value name)
2900 (widget-put widget :buttons buttons)))
2901 (let ((indent (widget-get widget :indent)))
2902 (when (and indent (not (zerop indent)))
2905 (narrow-to-region from to)
2906 (goto-char (point-min))
2907 (while (search-forward "\n" nil t)
2908 (insert-char ?\s indent)))))))
2910 ;;; The `documentation-string' Widget.
2912 (define-widget 'documentation-string 'item
2913 "A documentation string."
2915 :action 'widget-documentation-string-action
2916 :value-create 'widget-documentation-string-value-create
2917 :visibility-widget 'visibility)
2919 (defun widget-documentation-string-value-create (widget)
2920 ;; Insert documentation string.
2921 (let ((doc (widget-value widget))
2922 (indent (widget-get widget :indent))
2923 (shown (widget-get (widget-get widget :parent) :documentation-shown))
2925 (if (string-match "\n" doc)
2926 (let ((before (substring doc 0 (match-beginning 0)))
2927 (after (substring doc (match-beginning 0)))
2929 (when (and indent (not (zerop indent)))
2930 (insert-char ?\s indent))
2932 (widget-documentation-link-add widget start (point))
2934 (widget-create-child-and-convert
2935 widget (widget-get widget :visibility-widget)
2936 :help-echo "Show or hide rest of the documentation."
2940 :action 'widget-parent-action
2943 (setq start (point))
2944 (when (and indent (not (zerop indent)))
2945 (insert-char ?\s indent))
2947 (widget-documentation-link-add widget start (point)))
2948 (widget-put widget :buttons (list button)))
2949 (when (and indent (not (zerop indent)))
2950 (insert-char ?\s indent))
2952 (widget-documentation-link-add widget start (point))))
2955 (defun widget-documentation-string-action (widget &rest ignore)
2956 ;; Toggle documentation.
2957 (let ((parent (widget-get widget :parent)))
2958 (widget-put parent :documentation-shown
2959 (not (widget-get parent :documentation-shown))))
2961 (widget-value-set widget (widget-value widget)))
2963 (defun widget-add-documentation-string-button (widget &rest args)
2964 "Insert a new `documentation-string' widget based on WIDGET.
2965 The new widget becomes a child of WIDGET, and is also added to
2966 its `:buttons' list. The documentation string is found from
2967 WIDGET using the function `widget-docstring'.
2968 Optional ARGS specifies additional keyword arguments for the
2969 `documentation-string' widget."
2970 (let ((doc (widget-docstring widget))
2971 (indent (widget-get widget :indent))
2972 (doc-indent (widget-get widget :documentation-indent)))
2974 (and (eq (preceding-char) ?\n)
2976 (insert-char ?\s indent))
2977 (unless (or (numberp doc-indent) (null doc-indent))
2978 (setq doc-indent 0))
2979 (widget-put widget :buttons
2980 (cons (apply 'widget-create-child-and-convert
2981 widget 'documentation-string
2983 (nconc args (list doc)))
2984 (widget-get widget :buttons))))))
2986 ;;; The Sexp Widgets.
2988 (define-widget 'const 'item
2989 "An immutable sexp."
2990 :prompt-value 'widget-const-prompt-value
2993 (defun widget-const-prompt-value (widget prompt value unbound)
2994 ;; Return the value of the const.
2995 (widget-value widget))
2997 (define-widget 'function-item 'const
2998 "An immutable function name."
3000 :documentation-property (lambda (symbol)
3002 (documentation symbol t)
3005 (define-widget 'variable-item 'const
3006 "An immutable variable name."
3008 :documentation-property 'variable-documentation)
3010 (define-widget 'other 'sexp
3011 "Matches any value, but doesn't let the user edit the value.
3012 This is useful as last item in a `choice' widget.
3013 You should use this widget type with a default value,
3014 as in (other DEFAULT) or (other :tag \"NAME\" DEFAULT).
3015 If the user selects this alternative, that specifies DEFAULT
3021 (defvar widget-string-prompt-value-history nil
3022 "History of input to `widget-string-prompt-value'.")
3024 (define-widget 'string 'editable-field
3027 :format "%{%t%}: %v"
3028 :complete-function 'ispell-complete-word
3029 :prompt-history 'widget-string-prompt-value-history)
3033 (defun widget-string-complete ()
3034 "Complete contents of string field.
3035 Completions are taken from the :completion-alist property of the
3036 widget. If that isn't a list, it's evalled and expected to yield a list."
3038 (let* ((completion-ignore-case (widget-get widget :completion-ignore-case))
3039 (alist (widget-get widget :completion-alist))
3040 (_ (unless (listp alist)
3041 (setq alist (eval alist)))))
3042 (completion-in-region (widget-field-start widget)
3043 (max (point) (widget-field-text-end widget))
3046 (define-widget 'regexp 'string
3047 "A regular expression."
3048 :match 'widget-regexp-match
3049 :validate 'widget-regexp-validate
3050 ;; Doesn't work well with terminating newline.
3051 ;; :value-face 'widget-single-line-field
3054 (defun widget-regexp-match (widget value)
3055 ;; Match valid regexps.
3056 (and (stringp value)
3059 (string-match value ""))
3062 (defun widget-regexp-validate (widget)
3063 "Check that the value of WIDGET is a valid regexp."
3064 (condition-case data
3066 (string-match (widget-value widget) ""))
3067 (error (widget-put widget :error (error-message-string data))
3070 (define-widget 'file 'string
3072 It reads a file name from an editable text field."
3073 :complete-function 'widget-file-complete
3074 :prompt-value 'widget-file-prompt-value
3075 :format "%{%t%}: %v"
3076 ;; Doesn't work well with terminating newline.
3077 ;; :value-face 'widget-single-line-field
3080 (defun widget-file-complete ()
3081 "Perform completion on file name preceding point."
3083 (completion-in-region (widget-field-start widget)
3084 (max (point) (widget-field-text-end widget))
3085 'completion-file-name-table))
3087 (defun widget-file-prompt-value (widget prompt value unbound)
3088 ;; Read file from minibuffer.
3089 (abbreviate-file-name
3091 (read-file-name prompt)
3092 (let ((prompt2 (format "%s (default %s): " prompt value))
3093 (dir (file-name-directory value))
3094 (file (file-name-nondirectory value))
3095 (must-match (widget-get widget :must-match)))
3096 (read-file-name prompt2 dir nil must-match file)))))
3098 ;;;(defun widget-file-action (widget &optional event)
3099 ;;; ;; Read a file name from the minibuffer.
3100 ;;; (let* ((value (widget-value widget))
3101 ;;; (dir (file-name-directory value))
3102 ;;; (file (file-name-nondirectory value))
3103 ;;; (menu-tag (widget-apply widget :menu-tag-get))
3104 ;;; (must-match (widget-get widget :must-match))
3105 ;;; (answer (read-file-name (concat menu-tag " (default " value "): ")
3106 ;;; dir nil must-match file)))
3107 ;;; (widget-value-set widget (abbreviate-file-name answer))
3109 ;;; (widget-apply widget :notify widget event)))
3111 ;; Fixme: use file-name-as-directory.
3112 (define-widget 'directory 'file
3113 "A directory widget.
3114 It reads a directory name from an editable text field."
3117 (defvar widget-symbol-prompt-value-history nil
3118 "History of input to `widget-symbol-prompt-value'.")
3120 (define-widget 'symbol 'editable-field
3124 :format "%{%t%}: %v"
3125 :match (lambda (widget value) (symbolp value))
3126 :complete-function 'lisp-complete-symbol
3127 :prompt-internal 'widget-symbol-prompt-internal
3128 :prompt-match 'symbolp
3129 :prompt-history 'widget-symbol-prompt-value-history
3130 :value-to-internal (lambda (widget value)
3134 :value-to-external (lambda (widget value)
3139 (defun widget-symbol-prompt-internal (widget prompt initial history)
3140 ;; Read file from minibuffer.
3141 (let ((answer (completing-read prompt obarray
3142 (widget-get widget :prompt-match)
3143 nil initial history)))
3144 (if (and (stringp answer)
3145 (not (zerop (length answer))))
3147 (error "No value"))))
3149 (defvar widget-function-prompt-value-history nil
3150 "History of input to `widget-function-prompt-value'.")
3152 (define-widget 'function 'restricted-sexp
3154 :complete-function (lambda ()
3156 (lisp-complete-symbol 'fboundp))
3157 :prompt-value 'widget-field-prompt-value
3158 :prompt-internal 'widget-symbol-prompt-internal
3159 :prompt-match 'fboundp
3160 :prompt-history 'widget-function-prompt-value-history
3161 :action 'widget-field-action
3162 :match-alternatives '(functionp)
3163 :validate (lambda (widget)
3164 (unless (functionp (widget-value widget))
3165 (widget-put widget :error (format "Invalid function: %S"
3166 (widget-value widget)))
3171 (defvar widget-variable-prompt-value-history nil
3172 "History of input to `widget-variable-prompt-value'.")
3174 (define-widget 'variable 'symbol
3176 :prompt-match 'boundp
3177 :prompt-history 'widget-variable-prompt-value-history
3178 :complete-function (lambda ()
3180 (lisp-complete-symbol 'boundp))
3183 (define-widget 'coding-system 'symbol
3184 "A MULE coding-system."
3185 :format "%{%t%}: %v"
3186 :tag "Coding system"
3188 :prompt-history 'coding-system-value-history
3189 :prompt-value 'widget-coding-system-prompt-value
3190 :action 'widget-coding-system-action
3191 :complete-function (lambda ()
3193 (lisp-complete-symbol 'coding-system-p))
3194 :validate (lambda (widget)
3195 (unless (coding-system-p (widget-value widget))
3196 (widget-put widget :error (format "Invalid coding system: %S"
3197 (widget-value widget)))
3200 :prompt-match 'coding-system-p)
3202 (defun widget-coding-system-prompt-value (widget prompt value unbound)
3203 "Read coding-system from minibuffer."
3204 (if (widget-get widget :base-only)
3206 (completing-read (format "%s (default %s): " prompt value)
3207 (mapcar #'list (coding-system-list t)) nil nil nil
3208 coding-system-history))
3209 (read-coding-system (format "%s (default %s): " prompt value) value)))
3211 (defun widget-coding-system-action (widget &optional event)
3213 (widget-coding-system-prompt-value
3215 (widget-apply widget :menu-tag-get)
3216 (widget-value widget)
3218 (widget-value-set widget answer)
3219 (widget-apply widget :notify widget event)
3222 ;;; I'm not sure about what this is good for? KFS.
3223 (defvar widget-key-sequence-prompt-value-history nil
3224 "History of input to `widget-key-sequence-prompt-value'.")
3226 (defvar widget-key-sequence-default-value [ignore]
3227 "Default value for an empty key sequence.")
3229 (defvar widget-key-sequence-map
3230 (let ((map (make-sparse-keymap)))
3231 (set-keymap-parent map widget-field-keymap)
3232 (define-key map [(control ?q)] 'widget-key-sequence-read-event)
3235 (define-widget 'key-sequence 'restricted-sexp
3237 :prompt-value 'widget-field-prompt-value
3238 :prompt-internal 'widget-symbol-prompt-internal
3239 ; :prompt-match 'fboundp ;; What was this good for? KFS
3240 :prompt-history 'widget-key-sequence-prompt-value-history
3241 :action 'widget-field-action
3242 :match-alternatives '(stringp vectorp)
3243 :format "%{%t%}: %v"
3244 :validate 'widget-key-sequence-validate
3245 :value-to-internal 'widget-key-sequence-value-to-internal
3246 :value-to-external 'widget-key-sequence-value-to-external
3247 :value widget-key-sequence-default-value
3248 :keymap widget-key-sequence-map
3249 :help-echo "C-q: insert KEY, EVENT, or CODE; RET: enter value"
3250 :tag "Key sequence")
3252 (defun widget-key-sequence-read-event (ev)
3254 (let ((inhibit-quit t) quit-flag)
3255 (read-event "Insert KEY, EVENT, or CODE: "))))
3256 (let ((ev2 (and (memq 'down (event-modifiers ev))
3258 (tr (and (keymapp function-key-map)
3259 (lookup-key function-key-map (vector ev)))))
3260 (when (and (integerp ev)
3261 (or (and (<= ?0 ev) (< ev (+ ?0 (min 10 read-quoted-char-radix))))
3262 (and (<= ?a (downcase ev))
3263 (< (downcase ev) (+ ?a -10 (min 36 read-quoted-char-radix))))))
3264 (setq unread-command-events (cons ev unread-command-events)
3265 ev (read-quoted-char (format "Enter code (radix %d)" read-quoted-char-radix))
3267 (if (and (integerp ev) (not (characterp ev)))
3268 (insert (char-to-string ev)))) ;; throw invalid char error
3269 (setq ev (key-description (list ev)))
3271 (setq tr (key-description (list (aref tr 0))))
3272 (if (y-or-n-p (format "Key %s is translated to %s -- use %s? " ev tr tr))
3273 (setq ev tr ev2 nil)))
3274 (insert (if (= (char-before) ?\s) "" " ") ev " ")
3276 (insert (key-description (list ev2)) " "))))
3278 (defun widget-key-sequence-validate (widget)
3279 (unless (or (stringp (widget-value widget))
3280 (vectorp (widget-value widget)))
3281 (widget-put widget :error (format "Invalid key sequence: %S"
3282 (widget-value widget)))
3285 (defun widget-key-sequence-value-to-internal (widget value)
3286 (if (widget-apply widget :match value)
3287 (if (equal value widget-key-sequence-default-value)
3289 (key-description value))
3292 (defun widget-key-sequence-value-to-external (widget value)
3294 (if (string-match "\\`[[:space:]]*\\'" value)
3295 widget-key-sequence-default-value
3296 (read-kbd-macro value))
3300 (define-widget 'sexp 'editable-field
3301 "An arbitrary Lisp expression."
3302 :tag "Lisp expression"
3303 :format "%{%t%}: %v"
3305 :validate 'widget-sexp-validate
3306 :match (lambda (widget value) t)
3307 :value-to-internal 'widget-sexp-value-to-internal
3308 :value-to-external (lambda (widget value) (read value))
3309 :prompt-history 'widget-sexp-prompt-value-history
3310 :prompt-value 'widget-sexp-prompt-value)
3312 (defun widget-sexp-value-to-internal (widget value)
3313 ;; Use pp for printer representation.
3314 (let ((pp (if (symbolp value)
3315 (prin1-to-string value)
3316 (pp-to-string value))))
3317 (while (string-match "\n\\'" pp)
3318 (setq pp (substring pp 0 -1)))
3319 (if (or (string-match "\n\\'" pp)
3324 (defun widget-sexp-validate (widget)
3325 ;; Valid if we can read the string and there is no junk left after it.
3327 (insert (widget-apply widget :value-get))
3328 (goto-char (point-min))
3330 (condition-case data
3332 ;; Avoid a confusing end-of-file error.
3333 (skip-syntax-forward "\\s-")
3335 (setq err "Empty sexp -- use `nil'?")
3336 (unless (widget-apply widget :match (read (current-buffer)))
3337 (setq err (widget-get widget :type-error))))
3338 ;; Allow whitespace after expression.
3339 (skip-syntax-forward "\\s-")
3340 (if (and (not (eobp))
3342 (setq err (format "Junk at end of expression: %s"
3343 (buffer-substring (point)
3345 (end-of-file ; Avoid confusing error message.
3346 (setq err "Unbalanced sexp"))
3347 (error (setq err (error-message-string data))))
3350 (widget-put widget :error err)
3353 (defvar widget-sexp-prompt-value-history nil
3354 "History of input to `widget-sexp-prompt-value'.")
3356 (defun widget-sexp-prompt-value (widget prompt value unbound)
3357 ;; Read an arbitrary sexp.
3358 (let ((found (read-string prompt
3359 (if unbound nil (cons (prin1-to-string value) 0))
3360 (widget-get widget :prompt-history))))
3361 (let ((answer (read-from-string found)))
3362 (unless (= (cdr answer) (length found))
3363 (error "Junk at end of expression: %s"
3364 (substring found (cdr answer))))
3367 (define-widget 'restricted-sexp 'sexp
3368 "A Lisp expression restricted to values that match.
3369 To use this type, you must define :match or :match-alternatives."
3370 :type-error "The specified value is not valid"
3371 :match 'widget-restricted-sexp-match
3372 :value-to-internal (lambda (widget value)
3373 (if (widget-apply widget :match value)
3374 (prin1-to-string value)
3377 (defun widget-restricted-sexp-match (widget value)
3378 (let ((alternatives (widget-get widget :match-alternatives))
3380 (while (and alternatives (not matched))
3381 (if (cond ((functionp (car alternatives))
3382 (funcall (car alternatives) value))
3383 ((and (consp (car alternatives))
3384 (eq (car (car alternatives)) 'quote))
3385 (eq value (nth 1 (car alternatives)))))
3387 (setq alternatives (cdr alternatives)))
3390 (define-widget 'integer 'restricted-sexp
3394 :type-error "This field should contain an integer"
3395 :match-alternatives '(integerp))
3397 (define-widget 'number 'restricted-sexp
3398 "A number (floating point or integer)."
3401 :type-error "This field should contain a number (floating point or integer)"
3402 :match-alternatives '(numberp))
3404 (define-widget 'float 'restricted-sexp
3405 "A floating point number."
3406 :tag "Floating point number"
3408 :type-error "This field should contain a floating point number"
3409 :match-alternatives '(floatp))
3411 (define-widget 'character 'editable-field
3416 :format "%{%t%}: %v\n"
3417 :valid-regexp "\\`.\\'"
3418 :error "This field should contain a single character"
3419 :value-to-internal (lambda (widget value)
3422 (char-to-string value)))
3423 :value-to-external (lambda (widget value)
3427 :match (lambda (widget value)
3428 (characterp value)))
3430 (define-widget 'list 'group
3433 :format "%{%t%}:\n%v")
3435 (define-widget 'vector 'group
3438 :format "%{%t%}:\n%v"
3439 :match 'widget-vector-match
3440 :value-to-internal (lambda (widget value) (append value nil))
3441 :value-to-external (lambda (widget value) (apply 'vector value)))
3443 (defun widget-vector-match (widget value)
3444 (and (vectorp value)
3445 (widget-group-match widget
3446 (widget-apply widget :value-to-internal value))))
3448 (define-widget 'cons 'group
3451 :format "%{%t%}:\n%v"
3452 :match 'widget-cons-match
3453 :value-to-internal (lambda (widget value)
3454 (list (car value) (cdr value)))
3455 :value-to-external (lambda (widget value)
3456 (apply 'cons value)))
3458 (defun widget-cons-match (widget value)
3460 (widget-group-match widget
3461 (widget-apply widget :value-to-internal value))))
3463 ;;; The `lazy' Widget.
3465 ;; Recursive datatypes.
3467 (define-widget 'lazy 'default
3468 "Base widget for recursive datastructures.
3470 The `lazy' widget will, when instantiated, contain a single inferior
3471 widget, of the widget type specified by the :type parameter. The
3472 value of the `lazy' widget is the same as the value of the inferior
3473 widget. When deriving a new widget from the 'lazy' widget, the :type
3474 parameter is allowed to refer to the widget currently being defined,
3475 thus allowing recursive datastructures to be described.
3477 The :type parameter takes the same arguments as the defcustom
3478 parameter with the same name.
3480 Most composite widgets, i.e. widgets containing other widgets, does
3481 not allow recursion. That is, when you define a new widget type, none
3482 of the inferior widgets may be of the same type you are currently
3485 In Lisp, however, it is custom to define datastructures in terms of
3486 themselves. A list, for example, is defined as either nil, or a cons
3487 cell whose cdr itself is a list. The obvious way to translate this
3488 into a widget type would be
3490 (define-widget 'my-list 'choice
3491 \"A list of sexps.\"
3493 :args '((const nil) (cons :value (nil) sexp my-list)))
3495 Here we attempt to define my-list as a choice of either the constant
3496 nil, or a cons-cell containing a sexp and my-lisp. This will not work
3497 because the `choice' widget does not allow recursion.
3499 Using the `lazy' widget you can overcome this problem, as in this
3502 (define-widget 'sexp-list 'lazy
3503 \"A list of sexps.\"
3505 :type '(choice (const nil) (cons :value (nil) sexp sexp-list)))"
3506 :format "%{%t%}: %v"
3507 ;; We don't convert :type because we want to allow recursive
3508 ;; datastructures. This is slow, so we should not create speed
3509 ;; critical widgets by deriving from this.
3510 :convert-widget 'widget-value-convert-widget
3511 :value-create 'widget-type-value-create
3512 :value-get 'widget-child-value-get
3513 :value-inline 'widget-child-value-inline
3514 :default-get 'widget-type-default-get
3515 :match 'widget-type-match
3516 :validate 'widget-child-validate)
3519 ;;; The `plist' Widget.
3523 (define-widget 'plist 'list
3525 :key-type '(symbol :tag "Key")
3526 :value-type '(sexp :tag "Value")
3527 :convert-widget 'widget-plist-convert-widget
3530 (defvar widget-plist-value-type) ;Dynamic variable
3532 (defun widget-plist-convert-widget (widget)
3533 ;; Handle `:options'.
3534 (let* ((options (widget-get widget :options))
3535 (widget-plist-value-type (widget-get widget :value-type))
3536 (other `(editable-list :inline t
3538 ,(widget-get widget :key-type)
3539 ,widget-plist-value-type)))
3541 (list `(checklist :inline t
3543 ,@(mapcar 'widget-plist-convert-option
3547 (widget-put widget :args args)
3550 (defun widget-plist-convert-option (option)
3551 ;; Convert a single plist option.
3552 (let (key-type value-type)
3554 (let ((key (nth 0 option)))
3555 (setq value-type (nth 1 option))
3558 (setq key-type `(const ,key))))
3559 (setq key-type `(const ,option)
3560 value-type widget-plist-value-type))
3561 `(group :format "Key: %v" :inline t ,key-type ,value-type)))
3564 ;;; The `alist' Widget.
3566 ;; Association lists.
3568 (define-widget 'alist 'list
3569 "An association list."
3570 :key-type '(sexp :tag "Key")
3571 :value-type '(sexp :tag "Value")
3572 :convert-widget 'widget-alist-convert-widget
3575 (defvar widget-alist-value-type) ;Dynamic variable
3577 (defun widget-alist-convert-widget (widget)
3578 ;; Handle `:options'.
3579 (let* ((options (widget-get widget :options))
3580 (widget-alist-value-type (widget-get widget :value-type))
3581 (other `(editable-list :inline t
3583 ,(widget-get widget :key-type)
3584 ,widget-alist-value-type)))
3586 (list `(checklist :inline t
3588 ,@(mapcar 'widget-alist-convert-option
3592 (widget-put widget :args args)
3595 (defun widget-alist-convert-option (option)
3596 ;; Convert a single alist option.
3597 (let (key-type value-type)
3599 (let ((key (nth 0 option)))
3600 (setq value-type (nth 1 option))
3603 (setq key-type `(const ,key))))
3604 (setq key-type `(const ,option)
3605 value-type widget-alist-value-type))
3606 `(cons :format "Key: %v" ,key-type ,value-type)))
3608 (define-widget 'choice 'menu-choice
3609 "A union of several sexp types."
3611 :format "%{%t%}: %[Value Menu%] %v"
3612 :button-prefix 'widget-push-button-prefix
3613 :button-suffix 'widget-push-button-suffix
3614 :prompt-value 'widget-choice-prompt-value)
3616 (defun widget-choice-prompt-value (widget prompt value unbound)
3618 (let ((args (widget-get widget :args))
3619 (completion-ignore-case (widget-get widget :case-fold))
3620 current choices old)
3621 ;; Find the first arg that matches VALUE.
3624 (if (widget-apply (car look) :match value)
3625 (setq old (car look)
3627 (setq look (cdr look)))))
3630 (cond ((= (length args) 0)
3632 ((= (length args) 1)
3634 ((and (= (length args) 2)
3636 (if (eq old (nth 0 args))
3641 (setq current (car args)
3644 (cons (cons (widget-apply current :menu-tag-get)
3647 (let ((val (completing-read prompt choices nil t)))
3649 (let ((try (try-completion val choices)))
3652 (cdr (assoc val choices)))
3655 (widget-prompt-value current prompt nil t)
3658 (define-widget 'radio 'radio-button-choice
3659 "A union of several sexp types."
3661 :format "%{%t%}:\n%v"
3662 :prompt-value 'widget-choice-prompt-value)
3664 (define-widget 'repeat 'editable-list
3665 "A variable length homogeneous list."
3667 :format "%{%t%}:\n%v%i\n")
3669 (define-widget 'set 'checklist
3670 "A list of members from a fixed set."
3672 :format "%{%t%}:\n%v")
3674 (define-widget 'boolean 'toggle
3675 "To be nil or non-nil, that is the question."
3677 :prompt-value 'widget-boolean-prompt-value
3678 :button-prefix 'widget-push-button-prefix
3679 :button-suffix 'widget-push-button-suffix
3680 :format "%{%t%}: %[Toggle%] %v\n"
3684 (defun widget-boolean-prompt-value (widget prompt value unbound)
3685 ;; Toggle a boolean.
3688 ;;; The `color' Widget.
3691 (define-widget 'color 'editable-field
3692 "Choose a color name (with sample)."
3693 :format "%{%t%}: %v (%{sample%})\n"
3697 :complete 'widget-color-complete
3698 :sample-face-get 'widget-color-sample-face-get
3699 :notify 'widget-color-notify
3700 :action 'widget-color-action)
3702 (defun widget-color-complete (widget)
3703 "Complete the color in WIDGET."
3704 (require 'facemenu) ; for facemenu-color-alist
3705 (completion-in-region (widget-field-start widget)
3706 (max (point) (widget-field-text-end widget))
3707 (or facemenu-color-alist
3708 (sort (defined-colors) 'string-lessp))))
3710 (defun widget-color-sample-face-get (widget)
3711 (let* ((value (condition-case nil
3712 (widget-value widget)
3713 (error (widget-get widget :value)))))
3714 (if (color-defined-p value)
3715 (list (cons 'foreground-color value))
3718 (defun widget-color-action (widget &optional event)
3719 "Prompt for a color."
3720 (let* ((tag (widget-apply widget :menu-tag-get))
3721 (prompt (concat tag ": "))
3722 (value (widget-value widget))
3723 (start (widget-field-start widget))
3724 (answer (facemenu-read-color prompt)))
3725 (unless (zerop (length answer))
3726 (widget-value-set widget answer)
3728 (widget-apply widget :notify widget event))))
3730 (defun widget-color-notify (widget child &optional event)
3731 "Update the sample, and notify the parent."
3732 (overlay-put (widget-get widget :sample-overlay)
3733 'face (widget-apply widget :sample-face-get))
3734 (widget-default-notify widget child event))
3738 (defun widget-echo-help (pos)
3739 "Display help-echo text for widget at POS."
3740 (let* ((widget (widget-at pos))
3741 (help-echo (and widget (widget-get widget :help-echo))))
3742 (if (functionp help-echo)
3743 (setq help-echo (funcall help-echo widget)))
3744 (if help-echo (message "%s" (eval help-echo)))))
3750 ;; arch-tag: a076e75e-18a1-4b46-8be5-3f317bcbc707
3751 ;;; wid-edit.el ends here