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, 2010 Free Software Foundation, Inc.
6 ;; Author: Per Abrahamsen <abraham@dina.kvl.dk>
8 ;; Keywords: extensions
11 ;; This file is part of GNU Emacs.
13 ;; GNU Emacs is free software: you can redistribute it and/or modify
14 ;; it under the terms of the GNU General Public License as published by
15 ;; the Free Software Foundation, either version 3 of the License, or
16 ;; (at your option) any later version.
18 ;; GNU Emacs is distributed in the hope that it will be useful,
19 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
20 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 ;; GNU General Public License for more details.
23 ;; You should have received a copy of the GNU General Public License
24 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
26 ;;; Wishlist items (from widget.texi):
28 ;; * The `menu-choice' tag should be prettier, something like the
29 ;; abbreviated menus in Open Look.
31 ;; * Finish `:tab-order'.
33 ;; * Make indentation work with glyphs and proportional fonts.
35 ;; * Add commands to show overview of object and class hierarchies to
38 ;; * Find a way to disable mouse highlight for inactive widgets.
40 ;; * Find a way to make glyphs look inactive.
42 ;; * Add `key-binding' widget.
44 ;; * Add `widget' widget for editing widget specifications.
46 ;; * Find clean way to implement variable length list. See
47 ;; `TeX-printer-list' for an explanation.
49 ;; * `C-h' in `widget-prompt-value' should give type specific help.
51 ;; * A mailto widget. [This should work OK as a url-link if with
52 ;; browse-url-browser-function' set up appropriately.]
62 (defun widget-event-point (event)
63 "Character position of the end of event if that exists, or nil."
64 (posn-point (event-end event
)))
66 (defun widget-button-release-event-p (event)
67 "Non-nil if EVENT is a mouse-button-release event object."
69 (memq (event-basic-type event
) '(mouse-1 mouse-2 mouse-3
))
70 (or (memq 'click
(event-modifiers event
))
71 (memq 'drag
(event-modifiers event
)))))
76 "Customization support for the Widget Library."
77 :link
'(custom-manual "(widget)Top")
78 :link
'(emacs-library-link :tag
"Lisp File" "widget.el")
82 (defgroup widget-documentation nil
83 "Options controlling the display of documentation strings."
86 (defgroup widget-faces nil
87 "Faces used by the widget library."
91 (defvar widget-documentation-face
'widget-documentation
92 "Face used for documentation strings in widgets.
93 This exists as a variable so it can be set locally in certain buffers.")
95 (defface widget-documentation
'((((class color
)
97 (:foreground
"lime green"))
100 (:foreground
"dark green"))
102 "Face used for documentation text."
103 :group
'widget-documentation
104 :group
'widget-faces
)
105 (define-obsolete-face-alias 'widget-documentation-face
106 'widget-documentation
"22.1")
108 (defvar widget-button-face
'widget-button
109 "Face used for buttons in widgets.
110 This exists as a variable so it can be set locally in certain buffers.")
112 (defface widget-button
'((t (:weight bold
)))
113 "Face used for widget buttons."
114 :group
'widget-faces
)
115 (define-obsolete-face-alias 'widget-button-face
'widget-button
"22.1")
117 (defcustom widget-mouse-face
'highlight
118 "Face used for widget buttons when the mouse is above them."
120 :group
'widget-faces
)
122 ;; TTY gets special definitions here and in the next defface, because
123 ;; the gray colors defined for other displays cause black text on a black
124 ;; background, at least on light-background TTYs.
125 (defface widget-field
'((((type tty
))
126 :background
"yellow3"
128 (((class grayscale color
)
130 :background
"gray85")
131 (((class grayscale color
)
133 :background
"dim gray")
136 "Face used for editable fields."
137 :group
'widget-faces
)
138 (define-obsolete-face-alias 'widget-field-face
'widget-field
"22.1")
140 (defface widget-single-line-field
'((((type tty
))
143 (((class grayscale color
)
145 :background
"gray85")
146 (((class grayscale color
)
148 :background
"dim gray")
151 "Face used for editable fields spanning only a single line."
152 :group
'widget-faces
)
153 (define-obsolete-face-alias 'widget-single-line-field-face
154 'widget-single-line-field
"22.1")
156 ;;; This causes display-table to be loaded, and not usefully.
157 ;;;(defvar widget-single-line-display-table
158 ;;; (let ((table (make-display-table)))
159 ;;; (aset table 9 "^I")
160 ;;; (aset table 10 "^J")
162 ;;; "Display table used for single-line editable fields.")
164 ;;;(when (fboundp 'set-face-display-table)
165 ;;; (set-face-display-table 'widget-single-line-field-face
166 ;;; widget-single-line-display-table))
168 ;;; Utility functions.
170 ;; These are not really widget specific.
172 (defun widget-princ-to-string (object)
173 "Return string representation of OBJECT, any Lisp object.
174 No quoting characters are used; no delimiters are printed around
175 the contents of strings."
176 (with-output-to-string
179 (defun widget-clear-undo ()
180 "Clear all undo information."
181 (buffer-disable-undo (current-buffer))
182 (buffer-enable-undo))
184 (defcustom widget-menu-max-size
40
185 "Largest number of items allowed in a popup-menu.
186 Larger menus are read through the minibuffer."
190 (defcustom widget-menu-max-shortcuts
40
191 "Largest number of items for which it works to choose one with a character.
192 For a larger number of items, the minibuffer is used."
196 (defcustom widget-menu-minibuffer-flag nil
197 "Control how to ask for a choice from the keyboard.
198 Non-nil means use the minibuffer;
199 nil means read a single character."
203 (defun widget-choose (title items
&optional event
)
204 "Choose an item from a list.
206 First argument TITLE is the name of the list.
207 Second argument ITEMS is a list whose members are either
208 (NAME . VALUE), to indicate selectable items, or just strings to
209 indicate unselectable items.
210 Optional third argument EVENT is an input event.
212 The user is asked to choose between each NAME from the items alist,
213 and the VALUE of the chosen element will be returned. If EVENT is a
214 mouse event, and the number of elements in items is less than
215 `widget-menu-max-size', a popup menu will be used, otherwise the
217 (cond ((and (< (length items
) widget-menu-max-size
)
218 event
(display-popup-menus-p))
221 (list title
(cons "" items
))))
222 ((or widget-menu-minibuffer-flag
223 (> (length items
) widget-menu-max-shortcuts
))
224 ;; Read the choice of name from the minibuffer.
225 (setq items
(widget-remove-if 'stringp items
))
226 (let ((val (completing-read (concat title
": ") items nil t
)))
228 (let ((try (try-completion val items
)))
231 (cdr (assoc val items
))))))
233 ;; Construct a menu of the choices
234 ;; and then use it for prompting for a single character.
235 (let* ((overriding-terminal-local-map (make-sparse-keymap))
237 map choice some-choice-enabled value
)
238 ;; Define SPC as a prefix char to get to this menu.
239 (define-key overriding-terminal-local-map
" "
240 (setq map
(make-sparse-keymap title
)))
241 (with-current-buffer (get-buffer-create " widget-choose")
243 (insert "Available choices:\n\n")
245 (setq choice
(car items
) items
(cdr items
))
247 (let* ((name (car choice
))
248 (function (cdr choice
)))
249 (insert (format "%c = %s\n" next-digit name
))
250 (define-key map
(vector next-digit
) function
)
251 (setq some-choice-enabled t
)))
252 ;; Allocate digits to disabled alternatives
253 ;; so that the digit of a given alternative never varies.
254 (setq next-digit
(1+ next-digit
)))
255 (insert "\nC-g = Quit")
256 (goto-char (point-min))
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)
642 :ascent 'center) specs)))
643 (find-image (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 (featurep 'image)
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.
879 Note that such modes will need to require wid-edit.")
881 (defvar widget-global-map global-map
882 "Keymap used for events a widget does not handle itself.")
883 (make-variable-buffer-local 'widget-global-map)
885 (defvar widget-field-keymap
886 (let ((map (copy-keymap widget-keymap)))
887 (define-key map "\C-k" 'widget-kill-line)
888 (define-key map "\M-\t" 'widget-complete)
889 (define-key map "\C-m" 'widget-field-activate)
890 ;; Since the widget code uses a `field' property to identify fields,
891 ;; ordinary beginning-of-line does the right thing.
892 ;; (define-key map "\C-a" 'widget-beginning-of-line)
893 (define-key map "\C-e" 'widget-end-of-line)
895 "Keymap used inside an editable field.")
897 (defvar widget-text-keymap
898 (let ((map (copy-keymap widget-keymap)))
899 ;; Since the widget code uses a `field' property to identify fields,
900 ;; ordinary beginning-of-line does the right thing.
901 ;; (define-key map "\C-a" 'widget-beginning-of-line)
902 (define-key map "\C-e" 'widget-end-of-line)
904 "Keymap used inside a text field.")
906 (defun widget-field-activate (pos &optional event)
907 "Invoke the editable field at point."
909 (let ((field (widget-field-at pos)))
911 (widget-apply-action field event)
913 (lookup-key widget-global-map (this-command-keys))))))
915 (defface widget-button-pressed
916 '((((min-colors 88) (class color))
917 (:foreground "red1"))
921 (:weight bold :underline t)))
922 "Face used for pressed buttons."
923 :group 'widget-faces)
924 (define-obsolete-face-alias 'widget-button-pressed-face
925 'widget-button-pressed "22.1")
927 (defvar widget-button-click-moves-point nil
928 "If non-nil, `widget-button-click' moves point to a button after invoking it.
929 If nil, point returns to its original position after invoking a button.")
931 (defun widget-button-click (event)
932 "Invoke the button that the mouse is pointing at."
934 (if (widget-event-point event)
935 (let* ((oevent event)
936 (mouse-1 (memq (event-basic-type event) '(mouse-1 down-mouse-1)))
937 (pos (widget-event-point event))
938 (start (event-start event))
939 (button (get-char-property
940 pos 'button (and (windowp (posn-window start))
941 (window-buffer (posn-window start)))))
943 (when (or (null button)
944 (catch 'button-press-cancelled
945 ;; Mouse click on a widget button. Do the following
946 ;; in a save-excursion so that the click on the button
947 ;; doesn't change point.
948 (save-selected-window
949 (select-window (posn-window (event-start event)))
951 (goto-char (posn-point (event-start event)))
952 (let* ((overlay (widget-get button :button-overlay))
953 (pressed-face (or (widget-get button :pressed-face)
954 widget-button-pressed-face))
955 (face (overlay-get overlay 'face))
956 (mouse-face (overlay-get overlay 'mouse-face)))
958 ;; Read events, including mouse-movement
959 ;; events, waiting for a release event. If we
960 ;; began with a mouse-1 event and receive a
961 ;; movement event, that means the user wants
962 ;; to perform drag-selection, so cancel the
963 ;; button press and do the default mouse-1
964 ;; action. For mouse-2, just highlight/
965 ;; unhighlight the button the mouse was
966 ;; initially on when we move over it.
968 (when face ; avoid changing around image
969 (overlay-put overlay 'face pressed-face)
970 (overlay-put overlay 'mouse-face pressed-face))
971 (unless (widget-apply button :mouse-down-action event)
972 (let ((track-mouse t))
973 (while (not (widget-button-release-event-p event))
974 (setq event (read-event))
975 (when (and mouse-1 (mouse-movement-p event))
976 (push event unread-command-events)
978 (throw 'button-press-cancelled t))
979 (unless (or (integerp event)
980 (memq (car event) '(switch-frame select-window))
981 (eq (car event) 'scroll-bar-movement))
982 (setq pos (widget-event-point event))
984 (eq (get-char-property pos 'button)
987 (overlay-put overlay 'face pressed-face)
988 (overlay-put overlay 'mouse-face pressed-face))
989 (overlay-put overlay 'face face)
990 (overlay-put overlay 'mouse-face mouse-face))))))
992 ;; When mouse is released over the button, run
993 ;; its action function.
994 (when (and pos (eq (get-char-property pos 'button) button))
996 (widget-apply-action button event)
997 (if widget-button-click-moves-point
998 (setq newpoint (point)))))
999 (overlay-put overlay 'face face)
1000 (overlay-put overlay 'mouse-face mouse-face))))
1002 (if newpoint (goto-char newpoint))
1003 ;; This loses if the widget action switches windows. -- cyd
1004 ;; (unless (pos-visible-in-window-p (widget-event-point event))
1005 ;; (mouse-set-point event)
1006 ;; (beginning-of-line)
1010 (let ((up t) command)
1011 ;; Mouse click not on a widget button. Find the global
1012 ;; command to run, and check whether it is bound to an
1015 (cond ((setq command ;down event
1016 (lookup-key widget-global-map [down-mouse-1]))
1018 ((setq command ;up event
1019 (lookup-key widget-global-map [mouse-1]))))
1020 (cond ((setq command ;down event
1021 (lookup-key widget-global-map [down-mouse-2]))
1023 ((setq command ;up event
1024 (lookup-key widget-global-map [mouse-2])))))
1026 ;; Don't execute up events twice.
1027 (while (not (widget-button-release-event-p event))
1028 (setq event (read-event))))
1030 (call-interactively command)))))
1031 (message "You clicked somewhere weird.")))
1033 (defun widget-button-press (pos &optional event)
1034 "Invoke button at POS."
1036 (let ((button (get-char-property pos 'button)))
1038 (widget-apply-action button event)
1039 (let ((command (lookup-key widget-global-map (this-command-keys))))
1040 (when (commandp command)
1041 (call-interactively command))))))
1043 (defun widget-tabable-at (&optional pos)
1044 "Return the tabable widget at POS, or nil.
1045 POS defaults to the value of (point)."
1046 (let ((widget (widget-at pos)))
1048 (let ((order (widget-get widget :tab-order)))
1054 (defvar widget-use-overlay-change t
1055 "If non-nil, use overlay change functions to tab around in the buffer.
1056 This is much faster, but doesn't work reliably on Emacs 19.34.")
1058 (defun widget-move (arg)
1059 "Move point to the ARG next field or button.
1060 ARG may be negative to move backward."
1061 (or (bobp) (> arg 0) (backward-char))
1064 (old (widget-tabable-at)))
1068 (goto-char (point-min))
1069 (setq wrapped (1+ wrapped)))
1070 (widget-use-overlay-change
1071 (goto-char (next-overlay-change (point))))
1076 (error "No buttons or fields found"))
1077 (let ((new (widget-tabable-at)))
1079 (unless (eq new old)
1085 (goto-char (point-max))
1086 (setq wrapped (1+ wrapped)))
1087 (widget-use-overlay-change
1088 (goto-char (previous-overlay-change (point))))
1093 (error "No buttons or fields found"))
1094 (let ((new (widget-tabable-at)))
1096 (unless (eq new old)
1097 (setq arg (1+ arg))))))
1098 (let ((new (widget-tabable-at)))
1099 (while (eq (widget-tabable-at) new)
1102 (widget-echo-help (point))
1103 (run-hooks 'widget-move-hook))
1105 (defun widget-forward (arg)
1106 "Move point to the next field or button.
1107 With optional ARG, move across that many fields."
1109 (run-hooks 'widget-forward-hook)
1112 (defun widget-backward (arg)
1113 "Move point to the previous field or button.
1114 With optional ARG, move across that many fields."
1116 (run-hooks 'widget-backward-hook)
1117 (widget-move (- arg)))
1119 ;; Since the widget code uses a `field' property to identify fields,
1120 ;; ordinary beginning-of-line does the right thing.
1121 (defalias 'widget-beginning-of-line 'beginning-of-line)
1123 (defun widget-end-of-line ()
1124 "Go to end of field or end of line, whichever is first.
1125 Trailing spaces at the end of padded fields are not considered part of
1128 ;; Ordinary end-of-line does the right thing, because we're inside
1129 ;; text with a `field' property.
1132 ;; ... except that we want to ignore trailing spaces in fields that
1133 ;; aren't terminated by a newline, because they are used as padding,
1134 ;; and ignored when extracting the entered value of the field.
1135 (skip-chars-backward " " (field-beginning (1- (point))))))
1137 (defun widget-kill-line ()
1138 "Kill to end of field or end of line, whichever is first."
1140 (let* ((field (widget-field-find (point)))
1141 (end (and field (widget-field-end field))))
1142 (if (and field (> (line-beginning-position 2) end))
1143 (kill-region (point) end)
1144 (call-interactively 'kill-line))))
1146 (defcustom widget-complete-field (lookup-key global-map "\M-\t")
1147 "Default function to call for completion inside fields."
1148 :options '(ispell-complete-word complete-tag lisp-complete-symbol)
1152 (defun widget-narrow-to-field ()
1155 (let ((field (widget-field-find (point))))
1157 (narrow-to-region (line-beginning-position) (line-end-position)))))
1159 ;; This used to say:
1160 ;; "When not inside a field, move to the previous button or field."
1161 ;; but AFAICS, it has always just thrown an error.
1162 (defun widget-complete ()
1163 "Complete content of editable field from point.
1164 When not inside a field, signal an error."
1166 (let ((field (widget-field-find (point))))
1168 (widget-apply field :complete)
1169 (error "Not in an editable field"))))
1171 ;;; Setting up the buffer.
1173 (defvar widget-field-new nil
1174 "List of all newly created editable fields in the buffer.")
1175 (make-variable-buffer-local 'widget-field-new)
1177 (defvar widget-field-list nil
1178 "List of all editable fields in the buffer.")
1179 (make-variable-buffer-local 'widget-field-list)
1181 (defun widget-at (&optional pos)
1182 "The button or field at POS (default, point)."
1183 (or (get-char-property (or pos (point)) 'button)
1184 (widget-field-at pos)))
1187 (defun widget-setup ()
1188 "Setup current buffer so editing string widgets works."
1189 (let ((inhibit-read-only t)
1190 (inhibit-modification-hooks t)
1192 (while widget-field-new
1193 (setq field (car widget-field-new)
1194 widget-field-new (cdr widget-field-new)
1195 widget-field-list (cons field widget-field-list))
1196 (let ((from (car (widget-get field :field-overlay)))
1197 (to (cdr (widget-get field :field-overlay))))
1198 (widget-specify-field field
1199 (marker-position from) (marker-position to))
1200 (set-marker from nil)
1201 (set-marker to nil))))
1203 (widget-add-change))
1205 (defvar widget-field-last nil)
1206 ;; Last field containing point.
1207 (make-variable-buffer-local 'widget-field-last)
1209 (defvar widget-field-was nil)
1210 ;; The widget data before the change.
1211 (make-variable-buffer-local 'widget-field-was)
1213 (defun widget-field-at (pos)
1214 "Return the widget field at POS, or nil if none."
1215 (let ((field (get-char-property (or pos (point)) 'field)))
1216 (if (eq field 'boundary)
1217 (get-char-property (or pos (point)) 'real-field)
1220 (defun widget-field-buffer (widget)
1221 "Return the buffer of WIDGET's editing field."
1222 (let ((overlay (widget-get widget :field-overlay)))
1223 (cond ((overlayp overlay)
1224 (overlay-buffer overlay))
1226 (marker-buffer (car overlay))))))
1228 (defun widget-field-start (widget)
1229 "Return the start of WIDGET's editing field."
1230 (let ((overlay (widget-get widget :field-overlay)))
1231 (if (overlayp overlay)
1232 (overlay-start overlay)
1235 (defun widget-field-end (widget)
1236 "Return the end of WIDGET's editing field."
1237 (let ((overlay (widget-get widget :field-overlay)))
1238 ;; Don't subtract one if local-map works at the end of the overlay,
1239 ;; or if a special `boundary' field has been added after the widget
1241 (if (overlayp overlay)
1242 ;; Don't proceed if overlay has been removed from buffer.
1243 (when (overlay-buffer overlay)
1244 (if (and (not (eq (with-current-buffer
1245 (widget-field-buffer widget)
1247 ;; `widget-narrow-to-field' can be
1248 ;; active when this function is called
1249 ;; from an change-functions hook. So
1250 ;; temporarily remove field narrowing
1251 ;; before to call `get-char-property'.
1253 (get-char-property (overlay-end overlay)
1256 (or widget-field-add-space
1257 (null (widget-get widget :size))))
1258 (1- (overlay-end overlay))
1259 (overlay-end overlay)))
1262 (defun widget-field-text-end (widget)
1263 (let ((to (widget-field-end widget))
1264 (size (widget-get widget :size)))
1265 (if (or (null size) (zerop size))
1267 (let ((from (widget-field-start widget)))
1269 (with-current-buffer (widget-field-buffer widget)
1270 (while (and (> to from)
1271 (eq (char-after (1- to)) ?\s))
1275 (defun widget-field-find (pos)
1276 "Return the field at POS.
1277 Unlike (get-char-property POS 'field), this works with empty fields too."
1278 (let ((fields widget-field-list)
1281 (setq field (car fields)
1282 fields (cdr fields))
1283 (when (and (<= (widget-field-start field) pos)
1284 (<= pos (widget-field-end field)))
1286 (error "Overlapping fields"))
1287 (setq found field)))
1290 (defun widget-before-change (from to)
1291 ;; This is how, for example, a variable changes its state to `modified'.
1292 ;; when it is being edited.
1293 (unless inhibit-read-only
1294 (let ((from-field (widget-field-find from))
1295 (to-field (widget-field-find to)))
1296 (cond ((not (eq from-field to-field))
1297 (add-hook 'post-command-hook 'widget-add-change nil t)
1298 (signal 'text-read-only
1299 '("Change should be restricted to a single field")))
1301 (add-hook 'post-command-hook 'widget-add-change nil t)
1302 (signal 'text-read-only
1303 '("Attempt to change text outside editable field")))
1304 (widget-field-use-before-change
1305 (widget-apply from-field :notify from-field))))))
1307 (defun widget-add-change ()
1308 (remove-hook 'post-command-hook 'widget-add-change t)
1309 (add-hook 'before-change-functions 'widget-before-change nil t)
1310 (add-hook 'after-change-functions 'widget-after-change nil t))
1312 (defun widget-after-change (from to old)
1313 "Adjust field size and text properties."
1314 (let ((field (widget-field-find from))
1315 (other (widget-field-find to)))
1317 (unless (eq field other)
1318 (error "Change in different fields"))
1319 (let ((size (widget-get field :size)))
1321 (let ((begin (widget-field-start field))
1322 (end (widget-field-end field)))
1323 (cond ((< (- end begin) size)
1327 (insert-char ?\s (- (+ begin size) end))))
1328 ((> (- end begin) size)
1329 ;; Field too large and
1330 (if (or (< (point) (+ begin size))
1332 ;; Point is outside extra space.
1333 (setq begin (+ begin size))
1334 ;; Point is within the extra space.
1335 (setq begin (point)))
1338 (while (and (eq (preceding-char) ?\s)
1340 (delete-char -1)))))))
1341 (widget-specify-secret field))
1342 (widget-apply field :notify field))))
1344 ;;; Widget Functions
1346 ;; These functions are used in the definition of multiple widgets.
1348 (defun widget-parent-action (widget &optional event)
1349 "Tell :parent of WIDGET to handle the :action.
1350 Optional EVENT is the event that triggered the action."
1351 (widget-apply (widget-get widget :parent) :action event))
1353 (defun widget-children-value-delete (widget)
1354 "Delete all :children and :buttons in WIDGET."
1355 (mapc 'widget-delete (widget-get widget :children))
1356 (widget-put widget :children nil)
1357 (mapc 'widget-delete (widget-get widget :buttons))
1358 (widget-put widget :buttons nil))
1360 (defun widget-children-validate (widget)
1361 "All the :children must be valid."
1362 (let ((children (widget-get widget :children))
1364 (while (and children (not found))
1365 (setq child (car children)
1366 children (cdr children)
1367 found (widget-apply child :validate)))
1370 (defun widget-child-value-get (widget)
1371 "Get the value of the first member of :children in WIDGET."
1372 (widget-value (car (widget-get widget :children))))
1374 (defun widget-child-value-inline (widget)
1375 "Get the inline value of the first member of :children in WIDGET."
1376 (widget-apply (car (widget-get widget :children)) :value-inline))
1378 (defun widget-child-validate (widget)
1379 "The result of validating the first member of :children in WIDGET."
1380 (widget-apply (car (widget-get widget :children)) :validate))
1382 (defun widget-type-value-create (widget)
1383 "Convert and instantiate the value of the :type attribute of WIDGET.
1384 Store the newly created widget in the :children attribute.
1386 The value of the :type attribute should be an unconverted widget type."
1387 (let ((value (widget-get widget :value))
1388 (type (widget-get widget :type)))
1389 (widget-put widget :children
1390 (list (widget-create-child-value widget
1391 (widget-convert type)
1394 (defun widget-type-default-get (widget)
1395 "Get default value from the :type attribute of WIDGET.
1397 The value of the :type attribute should be an unconverted widget type."
1398 (widget-default-get (widget-convert (widget-get widget :type))))
1400 (defun widget-type-match (widget value)
1401 "Non-nil if the :type value of WIDGET matches VALUE.
1403 The value of the :type attribute should be an unconverted widget type."
1404 (widget-apply (widget-convert (widget-get widget :type)) :match value))
1406 (defun widget-types-copy (widget)
1407 "Copy :args as widget types in WIDGET."
1408 (widget-put widget :args (mapcar 'widget-copy (widget-get widget :args)))
1411 ;; Made defsubst to speed up face editor creation.
1412 (defsubst widget-types-convert-widget (widget)
1413 "Convert :args as widget types in WIDGET."
1414 (widget-put widget :args (mapcar 'widget-convert (widget-get widget :args)))
1417 (defun widget-value-convert-widget (widget)
1418 "Initialize :value from :args in WIDGET."
1419 (let ((args (widget-get widget :args)))
1421 (widget-put widget :value (car args))
1422 ;; Don't convert :value here, as this is done in `widget-convert'.
1423 ;; (widget-put widget :value (widget-apply widget
1424 ;; :value-to-internal (car args)))
1425 (widget-put widget :args nil)))
1428 (defun widget-value-value-get (widget)
1429 "Return the :value property of WIDGET."
1430 (widget-get widget :value))
1432 ;;; The `default' Widget.
1434 (define-widget 'default nil
1435 "Basic widget other widgets are derived from."
1436 :value-to-internal (lambda (widget value) value)
1437 :value-to-external (lambda (widget value) value)
1438 :button-prefix 'widget-button-prefix
1439 :button-suffix 'widget-button-suffix
1440 :complete 'widget-default-complete
1441 :create 'widget-default-create
1444 :format-handler 'widget-default-format-handler
1445 :button-face-get 'widget-default-button-face-get
1446 :mouse-face-get 'widget-default-mouse-face-get
1447 :sample-face-get 'widget-default-sample-face-get
1448 :delete 'widget-default-delete
1450 :value-set 'widget-default-value-set
1451 :value-inline 'widget-default-value-inline
1452 :value-delete 'ignore
1453 :default-get 'widget-default-default-get
1454 :menu-tag-get 'widget-default-menu-tag-get
1456 :active 'widget-default-active
1457 :activate 'widget-specify-active
1458 :deactivate 'widget-default-deactivate
1459 :mouse-down-action #'ignore
1460 :action 'widget-default-action
1461 :notify 'widget-default-notify
1462 :prompt-value 'widget-default-prompt-value)
1464 (defvar widget--completing-widget)
1466 (defun widget-default-complete (widget)
1467 "Call the value of the :complete-function property of WIDGET.
1468 If that does not exist, call the value of `widget-complete-field'.
1469 During this call, `widget--completing-widget' is bound to WIDGET."
1470 (let ((widget--completing-widget widget))
1471 (call-interactively (or (widget-get widget :complete-function)
1472 widget-complete-field))))
1474 (defun widget-default-create (widget)
1475 "Create WIDGET at point in the current buffer."
1476 (widget-specify-insert
1477 (let ((from (point))
1478 button-begin button-end
1479 sample-begin sample-end
1482 (insert (widget-get widget :format))
1484 ;; Parse escapes in format.
1485 (while (re-search-forward "%\\(.\\)" nil t)
1486 (let ((escape (char-after (match-beginning 1))))
1488 (cond ((eq escape ?%)
1491 (setq button-begin (point))
1492 (insert (widget-get-indirect widget :button-prefix)))
1494 (insert (widget-get-indirect widget :button-suffix))
1495 (setq button-end (point)))
1497 (setq sample-begin (point)))
1499 (setq sample-end (point)))
1501 (when (widget-get widget :indent)
1503 (insert-char ?\s (widget-get widget :indent))))
1505 (let ((image (widget-get widget :tag-glyph))
1506 (tag (widget-get widget :tag)))
1508 (widget-image-insert widget (or tag "image") image))
1512 (princ (widget-get widget :value)
1513 (current-buffer))))))
1515 (let ((doc (widget-get widget :doc)))
1517 (setq doc-begin (point))
1519 (while (eq (preceding-char) ?\n)
1522 (setq doc-end (point)))))
1524 (widget-add-documentation-string-button widget))
1526 (if (and button-begin (not button-end))
1527 (widget-apply widget :value-create)
1528 (setq value-pos (point))))
1530 (widget-apply widget :format-handler escape)))))
1531 ;; Specify button, sample, and doc, and insert value.
1532 (and button-begin button-end
1533 (widget-specify-button widget button-begin button-end))
1534 (and sample-begin sample-end
1535 (widget-specify-sample widget sample-begin sample-end))
1536 (and doc-begin doc-end
1537 (widget-specify-doc widget doc-begin doc-end))
1539 (goto-char value-pos)
1540 (widget-apply widget :value-create)))
1541 (let ((from (point-min-marker))
1542 (to (point-max-marker)))
1543 (set-marker-insertion-type from t)
1544 (set-marker-insertion-type to nil)
1545 (widget-put widget :from from)
1546 (widget-put widget :to to)))
1547 (widget-clear-undo))
1549 (defun widget-default-format-handler (widget escape)
1550 (error "Unknown escape `%c'" escape))
1552 (defun widget-default-button-face-get (widget)
1553 ;; Use :button-face or widget-button-face
1554 (or (widget-get widget :button-face)
1555 (let ((parent (widget-get widget :parent)))
1557 (widget-apply parent :button-face-get)
1558 widget-button-face))))
1560 (defun widget-default-mouse-face-get (widget)
1561 ;; Use :mouse-face or widget-mouse-face
1562 (or (widget-get widget :mouse-face)
1563 (let ((parent (widget-get widget :parent)))
1565 (widget-apply parent :mouse-face-get)
1566 widget-mouse-face))))
1568 (defun widget-default-sample-face-get (widget)
1569 ;; Use :sample-face.
1570 (widget-get widget :sample-face))
1572 (defun widget-default-delete (widget)
1573 "Remove widget from the buffer."
1574 (let ((from (widget-get widget :from))
1575 (to (widget-get widget :to))
1576 (inactive-overlay (widget-get widget :inactive))
1577 (button-overlay (widget-get widget :button-overlay))
1578 (sample-overlay (widget-get widget :sample-overlay))
1579 (doc-overlay (widget-get widget :doc-overlay))
1580 (inhibit-modification-hooks t)
1581 (inhibit-read-only t))
1582 (widget-apply widget :value-delete)
1583 (widget-children-value-delete widget)
1584 (when inactive-overlay
1585 (delete-overlay inactive-overlay))
1586 (when button-overlay
1587 (delete-overlay button-overlay))
1588 (when sample-overlay
1589 (delete-overlay sample-overlay))
1591 (delete-overlay doc-overlay))
1593 ;; Kludge: this doesn't need to be true for empty formats.
1594 (delete-region from to))
1595 (set-marker from nil)
1596 (set-marker to nil))
1597 (widget-clear-undo))
1599 (defun widget-default-value-set (widget value)
1600 "Recreate widget with new value."
1601 (let* ((old-pos (point))
1602 (from (copy-marker (widget-get widget :from)))
1603 (to (copy-marker (widget-get widget :to)))
1604 (offset (if (and (<= from old-pos) (<= old-pos to))
1605 (if (>= old-pos (1- to))
1607 (- old-pos from)))))
1608 ;;??? Bug: this ought to insert the new value before deleting the old one,
1609 ;; so that markers on either side of the value automatically
1610 ;; stay on the same side. -- rms.
1612 (goto-char (widget-get widget :from))
1613 (widget-apply widget :delete)
1614 (widget-put widget :value value)
1615 (widget-apply widget :create))
1618 (goto-char (+ (widget-get widget :to) offset 1))
1619 (goto-char (min (+ from offset) (1- (widget-get widget :to))))))))
1621 (defun widget-default-value-inline (widget)
1622 "Wrap value in a list unless it is inline."
1623 (if (widget-get widget :inline)
1624 (widget-value widget)
1625 (list (widget-value widget))))
1627 (defun widget-default-default-get (widget)
1629 (widget-get widget :value))
1631 (defun widget-default-menu-tag-get (widget)
1632 "Use tag or value for menus."
1633 (or (widget-get widget :menu-tag)
1634 (widget-get widget :tag)
1635 (widget-princ-to-string (widget-get widget :value))))
1637 (defun widget-default-active (widget)
1638 "Return t if this widget is active (user modifiable)."
1639 (or (widget-get widget :always-active)
1640 (and (not (widget-get widget :inactive))
1641 (let ((parent (widget-get widget :parent)))
1643 (widget-apply parent :active))))))
1645 (defun widget-default-deactivate (widget)
1646 "Make WIDGET inactive for user modifications."
1647 (widget-specify-inactive widget
1648 (widget-get widget :from)
1649 (widget-get widget :to)))
1651 (defun widget-default-action (widget &optional event)
1652 "Notify the parent when a widget changes."
1653 (let ((parent (widget-get widget :parent)))
1655 (widget-apply parent :notify widget event))))
1657 (defun widget-default-notify (widget child &optional event)
1658 "Pass notification to parent."
1659 (widget-default-action widget event))
1661 (defun widget-default-prompt-value (widget prompt value unbound)
1662 "Read an arbitrary value."
1663 (eval-minibuffer prompt))
1665 (defun widget-docstring (widget)
1666 "Return the documentation string specificied by WIDGET, or nil if none.
1667 If WIDGET has a `:doc' property, that specifies the documentation string.
1668 Otherwise, try the `:documentation-property' property. If this
1669 is a function, call it with the widget's value as an argument; if
1670 it is a symbol, use this symbol together with the widget's value
1671 as the argument to `documentation-property'."
1672 (let ((doc (or (widget-get widget :doc)
1673 (let ((doc-prop (widget-get widget :documentation-property))
1674 (value (widget-get widget :value)))
1675 (cond ((functionp doc-prop)
1676 (funcall doc-prop value))
1678 (documentation-property value doc-prop)))))))
1679 (when (and (stringp doc) (> (length doc) 0))
1680 ;; Remove any redundant `*' in the beginning.
1681 (when (eq (aref doc 0) ?*)
1682 (setq doc (substring doc 1)))
1683 ;; Remove trailing newlines.
1684 (when (string-match "\n+\\'" doc)
1685 (setq doc (substring doc 0 (match-beginning 0))))
1688 ;;; The `item' Widget.
1690 (define-widget 'item 'default
1691 "Constant items for inclusion in other widgets."
1692 :convert-widget 'widget-value-convert-widget
1693 :value-create 'widget-item-value-create
1694 :value-delete 'ignore
1695 :value-get 'widget-value-value-get
1696 :match 'widget-item-match
1697 :match-inline 'widget-item-match-inline
1698 :action 'widget-item-action
1701 (defun widget-item-value-create (widget)
1702 "Insert the printed representation of the value."
1703 (princ (widget-get widget :value) (current-buffer)))
1705 (defun widget-item-match (widget value)
1706 ;; Match if the value is the same.
1707 (equal (widget-get widget :value) value))
1709 (defun widget-item-match-inline (widget values)
1710 ;; Match if the value is the same.
1711 (let ((value (widget-get widget :value)))
1713 (<= (length value) (length values))
1714 (let ((head (widget-sublist values 0 (length value))))
1715 (and (equal head value)
1716 (cons head (widget-sublist values (length value))))))))
1718 (defun widget-sublist (list start &optional end)
1719 "Return the sublist of LIST from START to END.
1720 If END is omitted, it defaults to the length of LIST."
1721 (if (> start 0) (setq list (nthcdr start list)))
1723 (unless (<= end start)
1724 (setq list (copy-sequence list))
1725 (setcdr (nthcdr (- end start 1) list) nil)
1727 (copy-sequence list)))
1729 (defun widget-item-action (widget &optional event)
1730 ;; Just notify itself.
1731 (widget-apply widget :notify widget event))
1733 ;;; The `push-button' Widget.
1735 ;; (defcustom widget-push-button-gui t
1736 ;; "If non-nil, use GUI push buttons when available."
1740 ;; Cache already created GUI objects.
1741 ;; (defvar widget-push-button-cache nil)
1743 (defcustom widget-push-button-prefix "["
1744 "String used as prefix for buttons."
1746 :group 'widget-button)
1748 (defcustom widget-push-button-suffix "]"
1749 "String used as suffix for buttons."
1751 :group 'widget-button)
1753 (define-widget 'push-button 'item
1754 "A pushable button."
1757 :value-create 'widget-push-button-value-create
1760 (defun widget-push-button-value-create (widget)
1761 "Insert text representing the `on' and `off' states."
1762 (let* ((tag (or (widget-get widget :tag)
1763 (widget-get widget :value)))
1764 (tag-glyph (widget-get widget :tag-glyph))
1765 (text (concat widget-push-button-prefix
1766 tag widget-push-button-suffix)))
1768 (widget-image-insert widget text tag-glyph)
1771 ;; (defun widget-gui-action (widget)
1772 ;; "Apply :action for WIDGET."
1773 ;; (widget-apply-action widget (this-command-keys)))
1775 ;;; The `link' Widget.
1777 (defcustom widget-link-prefix "["
1778 "String used as prefix for links."
1780 :group 'widget-button)
1782 (defcustom widget-link-suffix "]"
1783 "String used as suffix for links."
1785 :group 'widget-button)
1787 (define-widget 'link 'item
1789 :button-prefix 'widget-link-prefix
1790 :button-suffix 'widget-link-suffix
1791 :follow-link 'mouse-face
1792 :help-echo "Follow the link."
1795 ;;; The `info-link' Widget.
1797 (define-widget 'info-link 'link
1798 "A link to an info file."
1799 :action 'widget-info-link-action)
1801 (defun widget-info-link-action (widget &optional event)
1802 "Open the info node specified by WIDGET."
1803 (info (widget-value widget)))
1805 ;;; The `url-link' Widget.
1807 (define-widget 'url-link 'link
1808 "A link to an www page."
1809 :action 'widget-url-link-action)
1811 (defun widget-url-link-action (widget &optional event)
1812 "Open the URL specified by WIDGET."
1813 (browse-url (widget-value widget)))
1815 ;;; The `function-link' Widget.
1817 (define-widget 'function-link 'link
1818 "A link to an Emacs function."
1819 :action 'widget-function-link-action)
1821 (defun widget-function-link-action (widget &optional event)
1822 "Show the function specified by WIDGET."
1823 (describe-function (widget-value widget)))
1825 ;;; The `variable-link' Widget.
1827 (define-widget 'variable-link 'link
1828 "A link to an Emacs variable."
1829 :action 'widget-variable-link-action)
1831 (defun widget-variable-link-action (widget &optional event)
1832 "Show the variable specified by WIDGET."
1833 (describe-variable (widget-value widget)))
1835 ;;; The `file-link' Widget.
1837 (define-widget 'file-link 'link
1839 :action 'widget-file-link-action)
1841 (defun widget-file-link-action (widget &optional event)
1842 "Find the file specified by WIDGET."
1843 (find-file (widget-value widget)))
1845 ;;; The `emacs-library-link' Widget.
1847 (define-widget 'emacs-library-link 'link
1848 "A link to an Emacs Lisp library file."
1849 :action 'widget-emacs-library-link-action)
1851 (defun widget-emacs-library-link-action (widget &optional event)
1852 "Find the Emacs library file specified by WIDGET."
1853 (find-file (locate-library (widget-value widget))))
1855 ;;; The `emacs-commentary-link' Widget.
1857 (define-widget 'emacs-commentary-link 'link
1858 "A link to Commentary in an Emacs Lisp library file."
1859 :action 'widget-emacs-commentary-link-action)
1861 (defun widget-emacs-commentary-link-action (widget &optional event)
1862 "Find the Commentary section of the Emacs file specified by WIDGET."
1863 (finder-commentary (widget-value widget)))
1865 ;;; The `editable-field' Widget.
1867 (define-widget 'editable-field 'default
1868 "An editable text field.
1869 Note: In an `editable-field' widget, the `%v' escape must be preceded
1870 by some other text in the `:format' string (if specified)."
1871 :convert-widget 'widget-value-convert-widget
1872 :keymap widget-field-keymap
1874 :help-echo "M-TAB: complete field; RET: enter value"
1876 :prompt-internal 'widget-field-prompt-internal
1877 :prompt-history 'widget-field-history
1878 :prompt-value 'widget-field-prompt-value
1879 :action 'widget-field-action
1880 :validate 'widget-field-validate
1882 :error "Field's value doesn't match allowed forms"
1883 :value-create 'widget-field-value-create
1884 :value-set 'widget-field-value-set
1885 :value-delete 'widget-field-value-delete
1886 :value-get 'widget-field-value-get
1887 :match 'widget-field-match)
1889 (defvar widget-field-history nil
1890 "History of field minibuffer edits.")
1892 (defun widget-field-prompt-internal (widget prompt initial history)
1893 "Read string for WIDGET prompting with PROMPT.
1894 INITIAL is the initial input and HISTORY is a symbol containing
1896 (read-string prompt initial history))
1898 (defun widget-field-prompt-value (widget prompt value unbound)
1899 "Prompt for a string."
1900 (widget-apply widget
1902 (widget-apply widget
1903 :prompt-internal prompt
1905 (cons (widget-apply widget
1906 :value-to-internal value)
1908 (widget-get widget :prompt-history))))
1910 (defvar widget-edit-functions nil)
1912 (defun widget-field-action (widget &optional event)
1913 "Move to next field."
1915 (run-hook-with-args 'widget-edit-functions widget))
1917 (defun widget-field-validate (widget)
1918 "Valid if the content matches `:valid-regexp'."
1919 (unless (string-match (widget-get widget :valid-regexp)
1920 (widget-apply widget :value-get))
1923 (defun widget-field-value-set (widget value)
1924 "Set an editable text field WIDGET to VALUE"
1925 (let ((from (widget-field-start widget))
1926 (to (widget-field-text-end widget))
1927 (buffer (widget-field-buffer widget))
1928 (size (widget-get widget :size)))
1929 (when (and from to (buffer-live-p buffer))
1930 (with-current-buffer buffer
1932 (delete-char (- to from))
1935 (defun widget-field-value-create (widget)
1936 "Create an editable text field."
1937 (let ((size (widget-get widget :size))
1938 (value (widget-get widget :value))
1940 ;; This is changed to a real overlay in `widget-setup'. We
1941 ;; need the end points to behave differently until
1942 ;; `widget-setup' is called.
1943 (overlay (cons (make-marker) (make-marker))))
1944 (widget-put widget :field-overlay overlay)
1947 (< (length value) size)
1948 (insert-char ?\s (- size (length value))))
1949 (unless (memq widget widget-field-list)
1950 (setq widget-field-new (cons widget widget-field-new)))
1951 (move-marker (cdr overlay) (point))
1952 (set-marker-insertion-type (cdr overlay) nil)
1955 (move-marker (car overlay) from)
1956 (set-marker-insertion-type (car overlay) t)))
1958 (defun widget-field-value-delete (widget)
1959 "Remove the widget from the list of active editing fields."
1960 (setq widget-field-list (delq widget widget-field-list))
1961 (setq widget-field-new (delq widget widget-field-new))
1962 ;; These are nil if the :format string doesn't contain `%v'.
1963 (let ((overlay (widget-get widget :field-overlay)))
1964 (when (overlayp overlay)
1965 (delete-overlay overlay))))
1967 (defun widget-field-value-get (widget)
1968 "Return current text in editing field."
1969 (let ((from (widget-field-start widget))
1970 (to (widget-field-text-end widget))
1971 (buffer (widget-field-buffer widget))
1972 (secret (widget-get widget :secret))
1973 (old (current-buffer)))
1977 (let ((result (buffer-substring-no-properties from to)))
1980 (while (< (+ from index) to)
1982 (get-char-property (+ from index) 'secret))
1983 (setq index (1+ index)))))
1986 (widget-get widget :value))))
1988 (defun widget-field-match (widget value)
1989 ;; Match any string.
1992 ;;; The `text' Widget.
1994 (define-widget 'text 'editable-field
1995 "A multiline text area."
1996 :keymap widget-text-keymap)
1998 ;;; The `menu-choice' Widget.
2000 (define-widget 'menu-choice 'default
2001 "A menu of options."
2002 :convert-widget 'widget-types-convert-widget
2003 :copy 'widget-types-copy
2004 :format "%[%t%]: %v"
2007 :void '(item :format "invalid (%t)\n")
2008 :value-create 'widget-choice-value-create
2009 :value-get 'widget-child-value-get
2010 :value-inline 'widget-child-value-inline
2011 :default-get 'widget-choice-default-get
2012 :mouse-down-action 'widget-choice-mouse-down-action
2013 :action 'widget-choice-action
2014 :error "Make a choice"
2015 :validate 'widget-choice-validate
2016 :match 'widget-choice-match
2017 :match-inline 'widget-choice-match-inline)
2019 (defun widget-choice-value-create (widget)
2020 "Insert the first choice that matches the value."
2021 (let ((value (widget-get widget :value))
2022 (args (widget-get widget :args))
2023 (explicit (widget-get widget :explicit-choice))
2027 ;; If the user specified the choice for this value,
2028 ;; respect that choice.
2029 (widget-put widget :children (list (widget-create-child-value
2030 widget explicit value)))
2031 (widget-put widget :choice explicit)
2032 (widget-put widget :explicit-choice nil))
2034 (setq current (car args)
2036 (when (widget-apply current :match value)
2037 (widget-put widget :children (list (widget-create-child-value
2038 widget current value)))
2039 (widget-put widget :choice current)
2043 (let ((void (widget-get widget :void)))
2044 (widget-put widget :children (list (widget-create-child-and-convert
2045 widget void :value value)))
2046 (widget-put widget :choice void))))))
2048 (defun widget-choice-default-get (widget)
2049 ;; Get default for the first choice.
2050 (widget-default-get (car (widget-get widget :args))))
2052 (defcustom widget-choice-toggle nil
2053 "If non-nil, a binary choice will just toggle between the values.
2054 Otherwise, the user will explicitly have to choose between the values
2055 when he invoked the menu."
2059 (defun widget-choice-mouse-down-action (widget &optional event)
2060 ;; Return non-nil if we need a menu.
2061 (let ((args (widget-get widget :args))
2062 (old (widget-get widget :choice)))
2063 (cond ((not (display-popup-menus-p))
2064 ;; No place to pop up a menu.
2066 ((< (length args) 2)
2067 ;; Empty or singleton list, just return the value.
2069 ((> (length args) widget-menu-max-size)
2070 ;; Too long, prompt.
2072 ((> (length args) 2)
2073 ;; Reasonable sized list, use menu.
2075 ((and widget-choice-toggle (memq old args))
2079 ;; Ask which of the two.
2082 (defun widget-choice-action (widget &optional event)
2084 (let ((args (widget-get widget :args))
2085 (old (widget-get widget :choice))
2086 (tag (widget-apply widget :menu-tag-get))
2087 (completion-ignore-case (widget-get widget :case-fold))
2090 ;; Remember old value.
2091 (if (and old (not (widget-apply widget :validate)))
2092 (let* ((external (widget-value widget))
2093 (internal (widget-apply old :value-to-internal external)))
2094 (widget-put old :value internal)))
2097 (cond ((= (length args) 0)
2099 ((= (length args) 1)
2101 ((and widget-choice-toggle
2104 (if (eq old (nth 0 args))
2109 (setq current (car args)
2112 (cons (cons (widget-apply current :menu-tag-get)
2115 (setq this-explicit t)
2116 (widget-choose tag (reverse choices) event))))
2118 ;; If this was an explicit user choice, record the choice,
2119 ;; so that widget-choice-value-create will respect it.
2121 (widget-put widget :explicit-choice current))
2122 (widget-value-set widget (widget-default-get current))
2124 (widget-apply widget :notify widget event)))
2125 (run-hook-with-args 'widget-edit-functions widget))
2127 (defun widget-choice-validate (widget)
2128 ;; Valid if we have made a valid choice.
2129 (if (eq (widget-get widget :void) (widget-get widget :choice))
2131 (widget-apply (car (widget-get widget :children)) :validate)))
2133 (defun widget-choice-match (widget value)
2134 ;; Matches if one of the choices matches.
2135 (let ((args (widget-get widget :args))
2137 (while (and args (not found))
2138 (setq current (car args)
2140 found (widget-apply current :match value)))
2143 (defun widget-choice-match-inline (widget values)
2144 ;; Matches if one of the choices matches.
2145 (let ((args (widget-get widget :args))
2147 (while (and args (null found))
2148 (setq current (car args)
2150 found (widget-match-inline current values)))
2153 ;;; The `toggle' Widget.
2155 (define-widget 'toggle 'item
2156 "Toggle between two states."
2158 :value-create 'widget-toggle-value-create
2159 :action 'widget-toggle-action
2160 :match (lambda (widget value) t)
2164 (defun widget-toggle-value-create (widget)
2165 "Insert text representing the `on' and `off' states."
2166 (if (widget-value widget)
2167 (let ((image (widget-get widget :on-glyph)))
2168 (and (display-graphic-p)
2170 (not (eq (car image) 'image))
2171 (widget-put widget :on-glyph (setq image (eval image))))
2172 (widget-image-insert widget
2173 (widget-get widget :on)
2175 (let ((image (widget-get widget :off-glyph)))
2176 (and (display-graphic-p)
2178 (not (eq (car image) 'image))
2179 (widget-put widget :off-glyph (setq image (eval image))))
2180 (widget-image-insert widget (widget-get widget :off) image))))
2182 (defun widget-toggle-action (widget &optional event)
2184 (widget-value-set widget (not (widget-value widget)))
2185 (widget-apply widget :notify widget event)
2186 (run-hook-with-args 'widget-edit-functions widget))
2188 ;;; The `checkbox' Widget.
2190 (define-widget 'checkbox 'toggle
2191 "A checkbox toggle."
2196 ;; We could probably do the same job as the images using single
2197 ;; space characters in a boxed face with a stretch specification to
2198 ;; make them square.
2201 :off-glyph "unchecked"
2202 :help-echo "Toggle this item."
2203 :action 'widget-checkbox-action)
2205 (defun widget-checkbox-action (widget &optional event)
2206 "Toggle checkbox, notify parent, and set active state of sibling."
2207 (widget-toggle-action widget event)
2208 (let ((sibling (widget-get-sibling widget)))
2210 (if (widget-value widget)
2211 (widget-apply sibling :activate)
2212 (widget-apply sibling :deactivate))
2213 (widget-clear-undo))))
2215 ;;; The `checklist' Widget.
2217 (define-widget 'checklist 'default
2218 "A multiple choice widget."
2219 :convert-widget 'widget-types-convert-widget
2220 :copy 'widget-types-copy
2223 :entry-format "%b %v"
2225 :value-create 'widget-checklist-value-create
2226 :value-get 'widget-checklist-value-get
2227 :validate 'widget-checklist-validate
2228 :match 'widget-checklist-match
2229 :match-inline 'widget-checklist-match-inline)
2231 (defun widget-checklist-value-create (widget)
2232 ;; Insert all values
2233 (let ((alist (widget-checklist-match-find widget))
2234 (args (widget-get widget :args)))
2236 (widget-checklist-add-item widget item (assq item alist)))
2237 (widget-put widget :children (nreverse (widget-get widget :children)))))
2239 (defun widget-checklist-add-item (widget type chosen)
2240 "Create checklist item in WIDGET of type TYPE.
2241 If the item is checked, CHOSEN is a cons whose cdr is the value."
2242 (and (eq (preceding-char) ?\n)
2243 (widget-get widget :indent)
2244 (insert-char ?\s (widget-get widget :indent)))
2245 (widget-specify-insert
2246 (let* ((children (widget-get widget :children))
2247 (buttons (widget-get widget :buttons))
2248 (button-args (or (widget-get type :sibling-args)
2249 (widget-get widget :button-args)))
2252 (insert (widget-get widget :entry-format))
2254 ;; Parse % escapes in format.
2255 (while (re-search-forward "%\\([bv%]\\)" nil t)
2256 (let ((escape (char-after (match-beginning 1))))
2258 (cond ((eq escape ?%)
2261 (setq button (apply 'widget-create-child-and-convert
2263 :value (not (null chosen))
2268 (let ((child (widget-create-child widget type)))
2269 (widget-apply child :deactivate)
2271 ((widget-get type :inline)
2272 (widget-create-child-value
2273 widget type (cdr chosen)))
2275 (widget-create-child-value
2276 widget type (car (cdr chosen)))))))
2278 (error "Unknown escape `%c'" escape)))))
2279 ;; Update properties.
2280 (and button child (widget-put child :button button))
2281 (and button (widget-put widget :buttons (cons button buttons)))
2282 (and child (widget-put widget :children (cons child children))))))
2284 (defun widget-checklist-match (widget values)
2285 ;; All values must match a type in the checklist.
2287 (null (cdr (widget-checklist-match-inline widget values)))))
2289 (defun widget-checklist-match-inline (widget values)
2290 ;; Find the values which match a type in the checklist.
2291 (let ((greedy (widget-get widget :greedy))
2292 (args (copy-sequence (widget-get widget :args)))
2295 (let ((answer (widget-checklist-match-up args values)))
2297 (let ((vals (widget-match-inline answer values)))
2298 (setq found (append found (car vals))
2300 args (delq answer args))))
2302 (setq rest (append rest (list (car values)))
2303 values (cdr values)))
2305 (setq rest (append rest values)
2309 (defun widget-checklist-match-find (widget &optional vals)
2310 "Find the vals which match a type in the checklist.
2311 Return an alist of (TYPE MATCH)."
2312 (or vals (setq vals (widget-get widget :value)))
2313 (let ((greedy (widget-get widget :greedy))
2314 (args (copy-sequence (widget-get widget :args)))
2317 (let ((answer (widget-checklist-match-up args vals)))
2319 (let ((match (widget-match-inline answer vals)))
2320 (setq found (cons (cons answer (car match)) found)
2322 args (delq answer args))))
2324 (setq vals (cdr vals)))
2329 (defun widget-checklist-match-up (args vals)
2330 "Return the first type from ARGS that matches VALS."
2331 (let (current found)
2332 (while (and args (null found))
2333 (setq current (car args)
2335 found (widget-match-inline current vals)))
2339 (defun widget-checklist-value-get (widget)
2340 ;; The values of all selected items.
2341 (let ((children (widget-get widget :children))
2344 (setq child (car children)
2345 children (cdr children))
2346 (if (widget-value (widget-get child :button))
2347 (setq result (append result (widget-apply child :value-inline)))))
2350 (defun widget-checklist-validate (widget)
2351 ;; Ticked chilren must be valid.
2352 (let ((children (widget-get widget :children))
2354 (while (and children (not found))
2355 (setq child (car children)
2356 children (cdr children)
2357 button (widget-get child :button)
2358 found (and (widget-value button)
2359 (widget-apply child :validate))))
2362 ;;; The `option' Widget
2364 (define-widget 'option 'checklist
2365 "An widget with an optional item."
2368 ;;; The `choice-item' Widget.
2370 (define-widget 'choice-item 'item
2371 "Button items that delegate action events to their parents."
2372 :action 'widget-parent-action
2373 :format "%[%t%] \n")
2375 ;;; The `radio-button' Widget.
2377 (define-widget 'radio-button 'toggle
2378 "A radio button for use in the `radio' widget."
2379 :notify 'widget-radio-button-notify
2386 :off-glyph "radio0")
2388 (defun widget-radio-button-notify (widget child &optional event)
2390 (widget-apply (widget-get widget :parent) :action widget event))
2392 ;;; The `radio-button-choice' Widget.
2394 (define-widget 'radio-button-choice 'default
2395 "Select one of multiple options."
2396 :convert-widget 'widget-types-convert-widget
2397 :copy 'widget-types-copy
2400 :entry-format "%b %v"
2401 :value-create 'widget-radio-value-create
2402 :value-get 'widget-radio-value-get
2403 :value-inline 'widget-radio-value-inline
2404 :value-set 'widget-radio-value-set
2405 :error "You must push one of the buttons"
2406 :validate 'widget-radio-validate
2407 :match 'widget-choice-match
2408 :match-inline 'widget-choice-match-inline
2409 :action 'widget-radio-action)
2411 (defun widget-radio-value-create (widget)
2412 ;; Insert all values
2413 (let ((args (widget-get widget :args))
2416 (setq arg (car args)
2418 (widget-radio-add-item widget arg))))
2420 (defun widget-radio-add-item (widget type)
2421 "Add to radio widget WIDGET a new radio button item of type TYPE."
2422 ;; (setq type (widget-convert type))
2423 (and (eq (preceding-char) ?\n)
2424 (widget-get widget :indent)
2425 (insert-char ?\s (widget-get widget :indent)))
2426 (widget-specify-insert
2427 (let* ((value (widget-get widget :value))
2428 (children (widget-get widget :children))
2429 (buttons (widget-get widget :buttons))
2430 (button-args (or (widget-get type :sibling-args)
2431 (widget-get widget :button-args)))
2433 (chosen (and (null (widget-get widget :choice))
2434 (widget-apply type :match value)))
2436 (insert (widget-get widget :entry-format))
2438 ;; Parse % escapes in format.
2439 (while (re-search-forward "%\\([bv%]\\)" nil t)
2440 (let ((escape (char-after (match-beginning 1))))
2442 (cond ((eq escape ?%)
2445 (setq button (apply 'widget-create-child-and-convert
2446 widget 'radio-button
2447 :value (not (null chosen))
2450 (setq child (if chosen
2451 (widget-create-child-value
2453 (widget-create-child widget type)))
2455 (widget-apply child :deactivate)))
2457 (error "Unknown escape `%c'" escape)))))
2458 ;; Update properties.
2460 (widget-put widget :choice type))
2462 (widget-put child :button button)
2463 (widget-put widget :buttons (nconc buttons (list button))))
2465 (widget-put widget :children (nconc children (list child))))
2468 (defun widget-radio-value-get (widget)
2469 ;; Get value of the child widget.
2470 (let ((chosen (widget-radio-chosen widget)))
2471 (and chosen (widget-value chosen))))
2473 (defun widget-radio-chosen (widget)
2474 "Return the widget representing the chosen radio button."
2475 (let ((children (widget-get widget :children))
2478 (setq current (car children)
2479 children (cdr children))
2480 (when (widget-apply (widget-get current :button) :value-get)
2485 (defun widget-radio-value-inline (widget)
2486 ;; Get value of the child widget.
2487 (let ((children (widget-get widget :children))
2490 (setq current (car children)
2491 children (cdr children))
2492 (when (widget-apply (widget-get current :button) :value-get)
2493 (setq found (widget-apply current :value-inline)
2497 (defun widget-radio-value-set (widget value)
2498 ;; We can't just delete and recreate a radio widget, since children
2499 ;; can be added after the original creation and won't be recreated
2501 (let ((children (widget-get widget :children))
2504 (setq current (car children)
2505 children (cdr children))
2506 (let* ((button (widget-get current :button))
2507 (match (and (not found)
2508 (widget-apply current :match value))))
2509 (widget-value-set button match)
2512 (widget-value-set current value)
2513 (widget-apply current :activate))
2514 (widget-apply current :deactivate))
2515 (setq found (or found match))))))
2517 (defun widget-radio-validate (widget)
2518 ;; Valid if we have made a valid choice.
2519 (let ((children (widget-get widget :children))
2520 current found button)
2521 (while (and children (not found))
2522 (setq current (car children)
2523 children (cdr children)
2524 button (widget-get current :button)
2525 found (widget-apply button :value-get)))
2527 (widget-apply current :validate)
2530 (defun widget-radio-action (widget child event)
2531 ;; Check if a radio button was pressed.
2532 (let ((children (widget-get widget :children))
2533 (buttons (widget-get widget :buttons))
2535 (when (memq child buttons)
2537 (setq current (car children)
2538 children (cdr children))
2539 (let* ((button (widget-get current :button)))
2540 (cond ((eq child button)
2541 (widget-value-set button t)
2542 (widget-apply current :activate))
2543 ((widget-value button)
2544 (widget-value-set button nil)
2545 (widget-apply current :deactivate)))))))
2546 ;; Pass notification to parent.
2547 (widget-apply widget :notify child event))
2549 ;;; The `insert-button' Widget.
2551 (define-widget 'insert-button 'push-button
2552 "An insert button for the `editable-list' widget."
2554 :help-echo "Insert a new item into the list at this position."
2555 :action 'widget-insert-button-action)
2557 (defun widget-insert-button-action (widget &optional event)
2558 ;; Ask the parent to insert a new item.
2559 (widget-apply (widget-get widget :parent)
2560 :insert-before (widget-get widget :widget)))
2562 ;;; The `delete-button' Widget.
2564 (define-widget 'delete-button 'push-button
2565 "A delete button for the `editable-list' widget."
2567 :help-echo "Delete this item from the list."
2568 :action 'widget-delete-button-action)
2570 (defun widget-delete-button-action (widget &optional event)
2571 ;; Ask the parent to insert a new item.
2572 (widget-apply (widget-get widget :parent)
2573 :delete-at (widget-get widget :widget)))
2575 ;;; The `editable-list' Widget.
2577 ;; (defcustom widget-editable-list-gui nil
2578 ;; "If non-nil, use GUI push-buttons in editable list when available."
2582 (define-widget 'editable-list 'default
2583 "A variable list of widgets of the same type."
2584 :convert-widget 'widget-types-convert-widget
2585 :copy 'widget-types-copy
2588 :format-handler 'widget-editable-list-format-handler
2589 :entry-format "%i %d %v"
2590 :value-create 'widget-editable-list-value-create
2591 :value-get 'widget-editable-list-value-get
2592 :validate 'widget-children-validate
2593 :match 'widget-editable-list-match
2594 :match-inline 'widget-editable-list-match-inline
2595 :insert-before 'widget-editable-list-insert-before
2596 :delete-at 'widget-editable-list-delete-at)
2598 (defun widget-editable-list-format-handler (widget escape)
2599 ;; We recognize the insert button.
2600 ;; (let ((widget-push-button-gui widget-editable-list-gui))
2601 (cond ((eq escape ?i)
2602 (and (widget-get widget :indent)
2603 (insert-char ?\s (widget-get widget :indent)))
2604 (apply 'widget-create-child-and-convert
2605 widget 'insert-button
2606 (widget-get widget :append-button-args)))
2608 (widget-default-format-handler widget escape)))
2612 (defun widget-editable-list-value-create (widget)
2613 ;; Insert all values
2614 (let* ((value (widget-get widget :value))
2615 (type (nth 0 (widget-get widget :args)))
2617 (widget-put widget :value-pos (copy-marker (point)))
2618 (set-marker-insertion-type (widget-get widget :value-pos) t)
2620 (let ((answer (widget-match-inline type value)))
2622 (setq children (cons (widget-editable-list-entry-create
2624 (if (widget-get type :inline)
2631 (widget-put widget :children (nreverse children))))
2633 (defun widget-editable-list-value-get (widget)
2634 ;; Get value of the child widget.
2635 (apply 'append (mapcar (lambda (child) (widget-apply child :value-inline))
2636 (widget-get widget :children))))
2638 (defun widget-editable-list-match (widget value)
2639 ;; Value must be a list and all the members must match the type.
2641 (null (cdr (widget-editable-list-match-inline widget value)))))
2643 (defun widget-editable-list-match-inline (widget value)
2644 (let ((type (nth 0 (widget-get widget :args)))
2647 (while (and value ok)
2648 (let ((answer (widget-match-inline type value)))
2650 (setq found (append found (car answer))
2653 (cons found value)))
2655 (defun widget-editable-list-insert-before (widget before)
2656 ;; Insert a new child in the list of children.
2658 (let ((children (widget-get widget :children))
2659 (inhibit-read-only t)
2660 before-change-functions
2661 after-change-functions)
2663 (goto-char (widget-get before :entry-from)))
2665 (goto-char (widget-get widget :value-pos))))
2666 (let ((child (widget-editable-list-entry-create
2668 (when (< (widget-get child :entry-from) (widget-get widget :from))
2669 (set-marker (widget-get widget :from)
2670 (widget-get child :entry-from)))
2671 (if (eq (car children) before)
2672 (widget-put widget :children (cons child children))
2673 (while (not (eq (car (cdr children)) before))
2674 (setq children (cdr children)))
2675 (setcdr children (cons child (cdr children)))))))
2677 (widget-apply widget :notify widget))
2679 (defun widget-editable-list-delete-at (widget child)
2680 ;; Delete child from list of children.
2682 (let ((buttons (copy-sequence (widget-get widget :buttons)))
2684 (inhibit-read-only t)
2685 before-change-functions
2686 after-change-functions)
2688 (setq button (car buttons)
2689 buttons (cdr buttons))
2690 (when (eq (widget-get button :widget) child)
2692 :buttons (delq button (widget-get widget :buttons)))
2693 (widget-delete button))))
2694 (let ((entry-from (widget-get child :entry-from))
2695 (entry-to (widget-get child :entry-to))
2696 (inhibit-read-only t)
2697 before-change-functions
2698 after-change-functions)
2699 (widget-delete child)
2700 (delete-region entry-from entry-to)
2701 (set-marker entry-from nil)
2702 (set-marker entry-to nil))
2703 (widget-put widget :children (delq child (widget-get widget :children))))
2705 (widget-apply widget :notify widget))
2707 (defun widget-editable-list-entry-create (widget value conv)
2708 ;; Create a new entry to the list.
2709 (let ((type (nth 0 (widget-get widget :args)))
2710 ;; (widget-push-button-gui widget-editable-list-gui)
2711 child delete insert)
2712 (widget-specify-insert
2714 (and (widget-get widget :indent)
2715 (insert-char ?\s (widget-get widget :indent)))
2716 (insert (widget-get widget :entry-format)))
2717 ;; Parse % escapes in format.
2718 (while (re-search-forward "%\\(.\\)" nil t)
2719 (let ((escape (char-after (match-beginning 1))))
2721 (cond ((eq escape ?%)
2724 (setq insert (apply 'widget-create-child-and-convert
2725 widget 'insert-button
2726 (widget-get widget :insert-button-args))))
2728 (setq delete (apply 'widget-create-child-and-convert
2729 widget 'delete-button
2730 (widget-get widget :delete-button-args))))
2733 (setq child (widget-create-child-value
2735 (setq child (widget-create-child-value
2736 widget type (widget-default-get type)))))
2738 (error "Unknown escape `%c'" escape)))))
2739 (let ((buttons (widget-get widget :buttons)))
2740 (if insert (push insert buttons))
2741 (if delete (push delete buttons))
2742 (widget-put widget :buttons buttons))
2743 (let ((entry-from (point-min-marker))
2744 (entry-to (point-max-marker)))
2745 (set-marker-insertion-type entry-from t)
2746 (set-marker-insertion-type entry-to nil)
2747 (widget-put child :entry-from entry-from)
2748 (widget-put child :entry-to entry-to)))
2749 (if insert (widget-put insert :widget child))
2750 (if delete (widget-put delete :widget child))
2753 ;;; The `group' Widget.
2755 (define-widget 'group 'default
2756 "A widget which groups other widgets inside."
2757 :convert-widget 'widget-types-convert-widget
2758 :copy 'widget-types-copy
2760 :value-create 'widget-group-value-create
2761 :value-get 'widget-editable-list-value-get
2762 :default-get 'widget-group-default-get
2763 :validate 'widget-children-validate
2764 :match 'widget-group-match
2765 :match-inline 'widget-group-match-inline)
2767 (defun widget-group-value-create (widget)
2768 ;; Create each component.
2769 (let ((args (widget-get widget :args))
2770 (value (widget-get widget :value))
2771 arg answer children)
2773 (setq arg (car args)
2775 answer (widget-match-inline arg value)
2777 (and (eq (preceding-char) ?\n)
2778 (widget-get widget :indent)
2779 (insert-char ?\s (widget-get widget :indent)))
2780 (push (cond ((null answer)
2781 (widget-create-child widget arg))
2782 ((widget-get arg :inline)
2783 (widget-create-child-value widget arg (car answer)))
2785 (widget-create-child-value widget arg (car (car answer)))))
2787 (widget-put widget :children (nreverse children))))
2789 (defun widget-group-default-get (widget)
2790 ;; Get the default of the components.
2791 (mapcar 'widget-default-get (widget-get widget :args)))
2793 (defun widget-group-match (widget values)
2794 ;; Match if the components match.
2796 (let ((match (widget-group-match-inline widget values)))
2797 (and match (null (cdr match))))))
2799 (defun widget-group-match-inline (widget vals)
2800 ;; Match if the components match.
2801 (let ((args (widget-get widget :args))
2802 argument answer found)
2804 (setq argument (car args)
2806 (if (setq answer (widget-match-inline argument vals))
2807 (setq found (append found (car answer))
2812 (cons found vals))))
2814 ;;; The `visibility' Widget.
2816 (define-widget 'visibility 'item
2817 "An indicator and manipulator for hidden items.
2819 The following properties have special meanings for this widget:
2820 :on-image Image filename or spec to display when the item is visible.
2821 :on Text shown if the \"on\" image is nil or cannot be displayed.
2822 :off-image Image filename or spec to display when the item is hidden.
2823 :off Text shown if the \"off\" image is nil cannot be displayed."
2831 :value-create 'widget-visibility-value-create
2832 :action 'widget-toggle-action
2833 :match (lambda (widget value) t))
2835 (defun widget-visibility-value-create (widget)
2836 ;; Insert text representing the `on' and `off' states.
2837 (let* ((val (widget-value widget))
2838 (text (widget-get widget (if val :on :off)))
2839 (img (widget-image-find
2840 (widget-get widget (if val :on-image :off-image)))))
2841 (widget-image-insert widget
2843 (concat widget-push-button-prefix text
2844 widget-push-button-suffix)
2847 (append img '(:ascent center))))))
2849 ;;; The `documentation-link' Widget.
2851 ;; This is a helper widget for `documentation-string'.
2853 (define-widget 'documentation-link 'link
2854 "Link type used in documentation strings."
2856 :help-echo "Describe this symbol"
2857 :action 'widget-documentation-link-action)
2859 (defun widget-documentation-link-action (widget &optional event)
2860 "Display documentation for WIDGET's value. Ignore optional argument EVENT."
2861 (let* ((string (widget-get widget :value))
2862 (symbol (intern string)))
2863 (if (and (fboundp symbol) (boundp symbol))
2864 ;; If there are two doc strings, give the user a way to pick one.
2865 (apropos (concat "\\`" (regexp-quote string) "\\'"))
2866 (if (fboundp symbol)
2867 (describe-function symbol)
2868 (describe-variable symbol)))))
2870 (defcustom widget-documentation-links t
2871 "Add hyperlinks to documentation strings when non-nil."
2873 :group 'widget-documentation)
2875 (defcustom widget-documentation-link-regexp "`\\([^\n`' ]+\\)'"
2876 "Regexp for matching potential links in documentation strings.
2877 The first group should be the link itself."
2879 :group 'widget-documentation)
2881 (defcustom widget-documentation-link-p 'intern-soft
2882 "Predicate used to test if a string is useful as a link.
2883 The value should be a function. The function will be called with one
2884 argument, a string, and should return non-nil if there should be a
2885 link for that string."
2887 :options '(widget-documentation-link-p)
2888 :group 'widget-documentation)
2890 (defcustom widget-documentation-link-type 'documentation-link
2891 "Widget type used for links in documentation strings."
2893 :group 'widget-documentation)
2895 (defun widget-documentation-link-add (widget from to)
2896 (widget-specify-doc widget from to)
2897 (when widget-documentation-links
2898 (let ((regexp widget-documentation-link-regexp)
2899 (buttons (widget-get widget :buttons))
2900 (widget-mouse-face (default-value 'widget-mouse-face))
2901 (widget-button-face widget-documentation-face)
2902 (widget-button-pressed-face widget-documentation-face))
2905 (while (re-search-forward regexp to t)
2906 (let ((name (match-string 1))
2907 (begin (match-beginning 1))
2908 (end (match-end 1)))
2909 (when (funcall widget-documentation-link-p name)
2910 (push (widget-convert-button widget-documentation-link-type
2911 begin end :value name)
2913 (widget-put widget :buttons buttons)))
2914 (let ((indent (widget-get widget :indent)))
2915 (when (and indent (not (zerop indent)))
2918 (narrow-to-region from to)
2919 (goto-char (point-min))
2920 (while (search-forward "\n" nil t)
2921 (insert-char ?\s indent)))))))
2923 ;;; The `documentation-string' Widget.
2925 (define-widget 'documentation-string 'item
2926 "A documentation string."
2928 :action 'widget-documentation-string-action
2929 :value-create 'widget-documentation-string-value-create
2930 :visibility-widget 'visibility)
2932 (defun widget-documentation-string-value-create (widget)
2933 ;; Insert documentation string.
2934 (let ((doc (widget-value widget))
2935 (indent (widget-get widget :indent))
2936 (shown (widget-get (widget-get widget :parent) :documentation-shown))
2938 (if (string-match "\n" doc)
2939 (let ((before (substring doc 0 (match-beginning 0)))
2940 (after (substring doc (match-beginning 0)))
2942 (when (and indent (not (zerop indent)))
2943 (insert-char ?\s indent))
2945 (widget-documentation-link-add widget start (point))
2947 (widget-create-child-and-convert
2948 widget (widget-get widget :visibility-widget)
2949 :help-echo "Show or hide rest of the documentation."
2953 :action 'widget-parent-action
2956 (setq start (point))
2957 (when (and indent (not (zerop indent)))
2958 (insert-char ?\s indent))
2960 (widget-documentation-link-add widget start (point)))
2961 (widget-put widget :buttons (list button)))
2962 (when (and indent (not (zerop indent)))
2963 (insert-char ?\s indent))
2965 (widget-documentation-link-add widget start (point))))
2968 (defun widget-documentation-string-action (widget &rest ignore)
2969 ;; Toggle documentation.
2970 (let ((parent (widget-get widget :parent)))
2971 (widget-put parent :documentation-shown
2972 (not (widget-get parent :documentation-shown))))
2974 (widget-value-set widget (widget-value widget)))
2976 (defun widget-add-documentation-string-button (widget &rest args)
2977 "Insert a new `documentation-string' widget based on WIDGET.
2978 The new widget becomes a child of WIDGET, and is also added to
2979 its `:buttons' list. The documentation string is found from
2980 WIDGET using the function `widget-docstring'.
2981 Optional ARGS specifies additional keyword arguments for the
2982 `documentation-string' widget."
2983 (let ((doc (widget-docstring widget))
2984 (indent (widget-get widget :indent))
2985 (doc-indent (widget-get widget :documentation-indent)))
2987 (and (eq (preceding-char) ?\n)
2989 (insert-char ?\s indent))
2990 (unless (or (numberp doc-indent) (null doc-indent))
2991 (setq doc-indent 0))
2992 (widget-put widget :buttons
2993 (cons (apply 'widget-create-child-and-convert
2994 widget 'documentation-string
2996 (nconc args (list doc)))
2997 (widget-get widget :buttons))))))
2999 ;;; The Sexp Widgets.
3001 (define-widget 'const 'item
3002 "An immutable sexp."
3003 :prompt-value 'widget-const-prompt-value
3006 (defun widget-const-prompt-value (widget prompt value unbound)
3007 ;; Return the value of the const.
3008 (widget-value widget))
3010 (define-widget 'function-item 'const
3011 "An immutable function name."
3013 :documentation-property (lambda (symbol)
3015 (documentation symbol t)
3018 (define-widget 'variable-item 'const
3019 "An immutable variable name."
3021 :documentation-property 'variable-documentation)
3023 (define-widget 'other 'sexp
3024 "Matches any value, but doesn't let the user edit the value.
3025 This is useful as last item in a `choice' widget.
3026 You should use this widget type with a default value,
3027 as in (other DEFAULT) or (other :tag \"NAME\" DEFAULT).
3028 If the user selects this alternative, that specifies DEFAULT
3034 (defvar widget-string-prompt-value-history nil
3035 "History of input to `widget-string-prompt-value'.")
3037 (define-widget 'string 'editable-field
3040 :format "%{%t%}: %v"
3041 :complete-function 'ispell-complete-word
3042 :prompt-history 'widget-string-prompt-value-history)
3044 (defun widget-string-complete ()
3045 "Complete contents of string field.
3046 Completions are taken from the :completion-alist property of the
3047 widget. If that isn't a list, it's evalled and expected to yield a list."
3049 (let* ((widget widget--completing-widget)
3050 (completion-ignore-case (widget-get widget :completion-ignore-case))
3051 (alist (widget-get widget :completion-alist))
3052 (_ (unless (listp alist)
3053 (setq alist (eval alist)))))
3054 (completion-in-region (widget-field-start widget)
3055 (max (point) (widget-field-text-end widget))
3058 (define-widget 'regexp 'string
3059 "A regular expression."
3060 :match 'widget-regexp-match
3061 :validate 'widget-regexp-validate
3062 ;; Doesn't work well with terminating newline.
3063 ;; :value-face 'widget-single-line-field
3066 (defun widget-regexp-match (widget value)
3067 ;; Match valid regexps.
3068 (and (stringp value)
3071 (string-match value ""))
3074 (defun widget-regexp-validate (widget)
3075 "Check that the value of WIDGET is a valid regexp."
3076 (condition-case data
3078 (string-match (widget-value widget) ""))
3079 (error (widget-put widget :error (error-message-string data))
3082 (define-widget 'file 'string
3084 It reads a file name from an editable text field."
3085 :complete-function 'widget-file-complete
3086 :prompt-value 'widget-file-prompt-value
3087 :format "%{%t%}: %v"
3088 ;; Doesn't work well with terminating newline.
3089 ;; :value-face 'widget-single-line-field
3092 (defun widget-file-complete ()
3093 "Perform completion on file name preceding point."
3095 (let ((widget widget--completing-widget))
3096 (completion-in-region (widget-field-start widget)
3097 (max (point) (widget-field-text-end widget))
3098 'completion-file-name-table)))
3100 (defun widget-file-prompt-value (widget prompt value unbound)
3101 ;; Read file from minibuffer.
3102 (abbreviate-file-name
3104 (read-file-name prompt)
3105 (let ((prompt2 (format "%s (default %s): " prompt value))
3106 (dir (file-name-directory value))
3107 (file (file-name-nondirectory value))
3108 (must-match (widget-get widget :must-match)))
3109 (read-file-name prompt2 dir nil must-match file)))))
3111 ;;;(defun widget-file-action (widget &optional event)
3112 ;;; ;; Read a file name from the minibuffer.
3113 ;;; (let* ((value (widget-value widget))
3114 ;;; (dir (file-name-directory value))
3115 ;;; (file (file-name-nondirectory value))
3116 ;;; (menu-tag (widget-apply widget :menu-tag-get))
3117 ;;; (must-match (widget-get widget :must-match))
3118 ;;; (answer (read-file-name (concat menu-tag " (default " value "): ")
3119 ;;; dir nil must-match file)))
3120 ;;; (widget-value-set widget (abbreviate-file-name answer))
3122 ;;; (widget-apply widget :notify widget event)))
3124 ;; Fixme: use file-name-as-directory.
3125 (define-widget 'directory 'file
3126 "A directory widget.
3127 It reads a directory name from an editable text field."
3130 (defvar widget-symbol-prompt-value-history nil
3131 "History of input to `widget-symbol-prompt-value'.")
3133 (define-widget 'symbol 'editable-field
3137 :format "%{%t%}: %v"
3138 :match (lambda (widget value) (symbolp value))
3139 :complete-function 'lisp-complete-symbol
3140 :prompt-internal 'widget-symbol-prompt-internal
3141 :prompt-match 'symbolp
3142 :prompt-history 'widget-symbol-prompt-value-history
3143 :value-to-internal (lambda (widget value)
3147 :value-to-external (lambda (widget value)
3152 (defun widget-symbol-prompt-internal (widget prompt initial history)
3153 ;; Read file from minibuffer.
3154 (let ((answer (completing-read prompt obarray
3155 (widget-get widget :prompt-match)
3156 nil initial history)))
3157 (if (and (stringp answer)
3158 (not (zerop (length answer))))
3160 (error "No value"))))
3162 (defvar widget-function-prompt-value-history nil
3163 "History of input to `widget-function-prompt-value'.")
3165 (define-widget 'function 'restricted-sexp
3167 :complete-function (lambda ()
3169 (lisp-complete-symbol 'fboundp))
3170 :prompt-value 'widget-field-prompt-value
3171 :prompt-internal 'widget-symbol-prompt-internal
3172 :prompt-match 'fboundp
3173 :prompt-history 'widget-function-prompt-value-history
3174 :action 'widget-field-action
3175 :match-alternatives '(functionp)
3176 :validate (lambda (widget)
3177 (unless (functionp (widget-value widget))
3178 (widget-put widget :error (format "Invalid function: %S"
3179 (widget-value widget)))
3184 (defvar widget-variable-prompt-value-history nil
3185 "History of input to `widget-variable-prompt-value'.")
3187 (define-widget 'variable 'symbol
3189 :prompt-match 'boundp
3190 :prompt-history 'widget-variable-prompt-value-history
3191 :complete-function (lambda ()
3193 (lisp-complete-symbol 'boundp))
3196 (define-widget 'coding-system 'symbol
3197 "A MULE coding-system."
3198 :format "%{%t%}: %v"
3199 :tag "Coding system"
3201 :prompt-history 'coding-system-value-history
3202 :prompt-value 'widget-coding-system-prompt-value
3203 :action 'widget-coding-system-action
3204 :complete-function (lambda ()
3206 (lisp-complete-symbol 'coding-system-p))
3207 :validate (lambda (widget)
3208 (unless (coding-system-p (widget-value widget))
3209 (widget-put widget :error (format "Invalid coding system: %S"
3210 (widget-value widget)))
3213 :prompt-match 'coding-system-p)
3215 (defun widget-coding-system-prompt-value (widget prompt value unbound)
3216 "Read coding-system from minibuffer."
3217 (if (widget-get widget :base-only)
3219 (completing-read (format "%s (default %s): " prompt value)
3220 (mapcar #'list (coding-system-list t)) nil nil nil
3221 coding-system-history))
3222 (read-coding-system (format "%s (default %s): " prompt value) value)))
3224 (defun widget-coding-system-action (widget &optional event)
3226 (widget-coding-system-prompt-value
3228 (widget-apply widget :menu-tag-get)
3229 (widget-value widget)
3231 (widget-value-set widget answer)
3232 (widget-apply widget :notify widget event)
3235 ;;; I'm not sure about what this is good for? KFS.
3236 (defvar widget-key-sequence-prompt-value-history nil
3237 "History of input to `widget-key-sequence-prompt-value'.")
3239 (defvar widget-key-sequence-default-value [ignore]
3240 "Default value for an empty key sequence.")
3242 (defvar widget-key-sequence-map
3243 (let ((map (make-sparse-keymap)))
3244 (set-keymap-parent map widget-field-keymap)
3245 (define-key map [(control ?q)] 'widget-key-sequence-read-event)
3248 (define-widget 'key-sequence 'restricted-sexp
3250 :prompt-value 'widget-field-prompt-value
3251 :prompt-internal 'widget-symbol-prompt-internal
3252 ; :prompt-match 'fboundp ;; What was this good for? KFS
3253 :prompt-history 'widget-key-sequence-prompt-value-history
3254 :action 'widget-field-action
3255 :match-alternatives '(stringp vectorp)
3256 :format "%{%t%}: %v"
3257 :validate 'widget-key-sequence-validate
3258 :value-to-internal 'widget-key-sequence-value-to-internal
3259 :value-to-external 'widget-key-sequence-value-to-external
3260 :value widget-key-sequence-default-value
3261 :keymap widget-key-sequence-map
3262 :help-echo "C-q: insert KEY, EVENT, or CODE; RET: enter value"
3263 :tag "Key sequence")
3265 (defun widget-key-sequence-read-event (ev)
3267 (let ((inhibit-quit t) quit-flag)
3268 (read-event "Insert KEY, EVENT, or CODE: "))))
3269 (let ((ev2 (and (memq 'down (event-modifiers ev))
3271 (tr (and (keymapp function-key-map)
3272 (lookup-key function-key-map (vector ev)))))
3273 (when (and (integerp ev)
3274 (or (and (<= ?0 ev) (< ev (+ ?0 (min 10 read-quoted-char-radix))))
3275 (and (<= ?a (downcase ev))
3276 (< (downcase ev) (+ ?a -10 (min 36 read-quoted-char-radix))))))
3277 (setq unread-command-events (cons ev unread-command-events)
3278 ev (read-quoted-char (format "Enter code (radix %d)" read-quoted-char-radix))
3280 (if (and (integerp ev) (not (characterp ev)))
3281 (insert (char-to-string ev)))) ;; throw invalid char error
3282 (setq ev (key-description (list ev)))
3284 (setq tr (key-description (list (aref tr 0))))
3285 (if (y-or-n-p (format "Key %s is translated to %s -- use %s? " ev tr tr))
3286 (setq ev tr ev2 nil)))
3287 (insert (if (= (char-before) ?\s) "" " ") ev " ")
3289 (insert (key-description (list ev2)) " "))))
3291 (defun widget-key-sequence-validate (widget)
3292 (unless (or (stringp (widget-value widget))
3293 (vectorp (widget-value widget)))
3294 (widget-put widget :error (format "Invalid key sequence: %S"
3295 (widget-value widget)))
3298 (defun widget-key-sequence-value-to-internal (widget value)
3299 (if (widget-apply widget :match value)
3300 (if (equal value widget-key-sequence-default-value)
3302 (key-description value))
3305 (defun widget-key-sequence-value-to-external (widget value)
3307 (if (string-match "\\`[[:space:]]*\\'" value)
3308 widget-key-sequence-default-value
3309 (read-kbd-macro value))
3313 (define-widget 'sexp 'editable-field
3314 "An arbitrary Lisp expression."
3315 :tag "Lisp expression"
3316 :format "%{%t%}: %v"
3318 :validate 'widget-sexp-validate
3319 :match (lambda (widget value) t)
3320 :value-to-internal 'widget-sexp-value-to-internal
3321 :value-to-external (lambda (widget value) (read value))
3322 :prompt-history 'widget-sexp-prompt-value-history
3323 :prompt-value 'widget-sexp-prompt-value)
3325 (defun widget-sexp-value-to-internal (widget value)
3326 ;; Use pp for printer representation.
3327 (let ((pp (if (symbolp value)
3328 (prin1-to-string value)
3329 (pp-to-string value))))
3330 (while (string-match "\n\\'" pp)
3331 (setq pp (substring pp 0 -1)))
3332 (if (or (string-match "\n\\'" pp)
3337 (defun widget-sexp-validate (widget)
3338 ;; Valid if we can read the string and there is no junk left after it.
3340 (insert (widget-apply widget :value-get))
3341 (goto-char (point-min))
3343 (condition-case data
3345 ;; Avoid a confusing end-of-file error.
3346 (skip-syntax-forward "\\s-")
3348 (setq err "Empty sexp -- use `nil'?")
3349 (unless (widget-apply widget :match (read (current-buffer)))
3350 (setq err (widget-get widget :type-error))))
3351 ;; Allow whitespace after expression.
3352 (skip-syntax-forward "\\s-")
3353 (if (and (not (eobp))
3355 (setq err (format "Junk at end of expression: %s"
3356 (buffer-substring (point)
3358 (end-of-file ; Avoid confusing error message.
3359 (setq err "Unbalanced sexp"))
3360 (error (setq err (error-message-string data))))
3363 (widget-put widget :error err)
3366 (defvar widget-sexp-prompt-value-history nil
3367 "History of input to `widget-sexp-prompt-value'.")
3369 (defun widget-sexp-prompt-value (widget prompt value unbound)
3370 ;; Read an arbitrary sexp.
3371 (let ((found (read-string prompt
3372 (if unbound nil (cons (prin1-to-string value) 0))
3373 (widget-get widget :prompt-history))))
3374 (let ((answer (read-from-string found)))
3375 (unless (= (cdr answer) (length found))
3376 (error "Junk at end of expression: %s"
3377 (substring found (cdr answer))))
3380 (define-widget 'restricted-sexp 'sexp
3381 "A Lisp expression restricted to values that match.
3382 To use this type, you must define :match or :match-alternatives."
3383 :type-error "The specified value is not valid"
3384 :match 'widget-restricted-sexp-match
3385 :value-to-internal (lambda (widget value)
3386 (if (widget-apply widget :match value)
3387 (prin1-to-string value)
3390 (defun widget-restricted-sexp-match (widget value)
3391 (let ((alternatives (widget-get widget :match-alternatives))
3393 (while (and alternatives (not matched))
3394 (if (cond ((functionp (car alternatives))
3395 (funcall (car alternatives) value))
3396 ((and (consp (car alternatives))
3397 (eq (car (car alternatives)) 'quote))
3398 (eq value (nth 1 (car alternatives)))))
3400 (setq alternatives (cdr alternatives)))
3403 (define-widget 'integer 'restricted-sexp
3407 :type-error "This field should contain an integer"
3408 :match-alternatives '(integerp))
3410 (define-widget 'number 'restricted-sexp
3411 "A number (floating point or integer)."
3414 :type-error "This field should contain a number (floating point or integer)"
3415 :match-alternatives '(numberp))
3417 (define-widget 'float 'restricted-sexp
3418 "A floating point number."
3419 :tag "Floating point number"
3421 :type-error "This field should contain a floating point number"
3422 :match-alternatives '(floatp))
3424 (define-widget 'character 'editable-field
3429 :format "%{%t%}: %v\n"
3430 :valid-regexp "\\`.\\'"
3431 :error "This field should contain a single character"
3432 :value-to-internal (lambda (widget value)
3435 (char-to-string value)))
3436 :value-to-external (lambda (widget value)
3440 :match (lambda (widget value)
3441 (characterp value)))
3443 (define-widget 'list 'group
3446 :format "%{%t%}:\n%v")
3448 (define-widget 'vector 'group
3451 :format "%{%t%}:\n%v"
3452 :match 'widget-vector-match
3453 :value-to-internal (lambda (widget value) (append value nil))
3454 :value-to-external (lambda (widget value) (apply 'vector value)))
3456 (defun widget-vector-match (widget value)
3457 (and (vectorp value)
3458 (widget-group-match widget
3459 (widget-apply widget :value-to-internal value))))
3461 (define-widget 'cons 'group
3464 :format "%{%t%}:\n%v"
3465 :match 'widget-cons-match
3466 :value-to-internal (lambda (widget value)
3467 (list (car value) (cdr value)))
3468 :value-to-external (lambda (widget value)
3469 (apply 'cons value)))
3471 (defun widget-cons-match (widget value)
3473 (widget-group-match widget
3474 (widget-apply widget :value-to-internal value))))
3476 ;;; The `lazy' Widget.
3478 ;; Recursive datatypes.
3480 (define-widget 'lazy 'default
3481 "Base widget for recursive datastructures.
3483 The `lazy' widget will, when instantiated, contain a single inferior
3484 widget, of the widget type specified by the :type parameter. The
3485 value of the `lazy' widget is the same as the value of the inferior
3486 widget. When deriving a new widget from the 'lazy' widget, the :type
3487 parameter is allowed to refer to the widget currently being defined,
3488 thus allowing recursive datastructures to be described.
3490 The :type parameter takes the same arguments as the defcustom
3491 parameter with the same name.
3493 Most composite widgets, i.e. widgets containing other widgets, does
3494 not allow recursion. That is, when you define a new widget type, none
3495 of the inferior widgets may be of the same type you are currently
3498 In Lisp, however, it is custom to define datastructures in terms of
3499 themselves. A list, for example, is defined as either nil, or a cons
3500 cell whose cdr itself is a list. The obvious way to translate this
3501 into a widget type would be
3503 (define-widget 'my-list 'choice
3504 \"A list of sexps.\"
3506 :args '((const nil) (cons :value (nil) sexp my-list)))
3508 Here we attempt to define my-list as a choice of either the constant
3509 nil, or a cons-cell containing a sexp and my-lisp. This will not work
3510 because the `choice' widget does not allow recursion.
3512 Using the `lazy' widget you can overcome this problem, as in this
3515 (define-widget 'sexp-list 'lazy
3516 \"A list of sexps.\"
3518 :type '(choice (const nil) (cons :value (nil) sexp sexp-list)))"
3519 :format "%{%t%}: %v"
3520 ;; We don't convert :type because we want to allow recursive
3521 ;; datastructures. This is slow, so we should not create speed
3522 ;; critical widgets by deriving from this.
3523 :convert-widget 'widget-value-convert-widget
3524 :value-create 'widget-type-value-create
3525 :value-get 'widget-child-value-get
3526 :value-inline 'widget-child-value-inline
3527 :default-get 'widget-type-default-get
3528 :match 'widget-type-match
3529 :validate 'widget-child-validate)
3532 ;;; The `plist' Widget.
3536 (define-widget 'plist 'list
3538 :key-type '(symbol :tag "Key")
3539 :value-type '(sexp :tag "Value")
3540 :convert-widget 'widget-plist-convert-widget
3543 (defvar widget-plist-value-type) ;Dynamic variable
3545 (defun widget-plist-convert-widget (widget)
3546 ;; Handle `:options'.
3547 (let* ((options (widget-get widget :options))
3548 (widget-plist-value-type (widget-get widget :value-type))
3549 (other `(editable-list :inline t
3551 ,(widget-get widget :key-type)
3552 ,widget-plist-value-type)))
3554 (list `(checklist :inline t
3556 ,@(mapcar 'widget-plist-convert-option
3560 (widget-put widget :args args)
3563 (defun widget-plist-convert-option (option)
3564 ;; Convert a single plist option.
3565 (let (key-type value-type)
3567 (let ((key (nth 0 option)))
3568 (setq value-type (nth 1 option))
3571 (setq key-type `(const ,key))))
3572 (setq key-type `(const ,option)
3573 value-type widget-plist-value-type))
3574 `(group :format "Key: %v" :inline t ,key-type ,value-type)))
3577 ;;; The `alist' Widget.
3579 ;; Association lists.
3581 (define-widget 'alist 'list
3582 "An association list."
3583 :key-type '(sexp :tag "Key")
3584 :value-type '(sexp :tag "Value")
3585 :convert-widget 'widget-alist-convert-widget
3588 (defvar widget-alist-value-type) ;Dynamic variable
3590 (defun widget-alist-convert-widget (widget)
3591 ;; Handle `:options'.
3592 (let* ((options (widget-get widget :options))
3593 (widget-alist-value-type (widget-get widget :value-type))
3594 (other `(editable-list :inline t
3596 ,(widget-get widget :key-type)
3597 ,widget-alist-value-type)))
3599 (list `(checklist :inline t
3601 ,@(mapcar 'widget-alist-convert-option
3605 (widget-put widget :args args)
3608 (defun widget-alist-convert-option (option)
3609 ;; Convert a single alist option.
3610 (let (key-type value-type)
3612 (let ((key (nth 0 option)))
3613 (setq value-type (nth 1 option))
3616 (setq key-type `(const ,key))))
3617 (setq key-type `(const ,option)
3618 value-type widget-alist-value-type))
3619 `(cons :format "Key: %v" ,key-type ,value-type)))
3621 (define-widget 'choice 'menu-choice
3622 "A union of several sexp types."
3624 :format "%{%t%}: %[Value Menu%] %v"
3625 :button-prefix 'widget-push-button-prefix
3626 :button-suffix 'widget-push-button-suffix
3627 :prompt-value 'widget-choice-prompt-value)
3629 (defun widget-choice-prompt-value (widget prompt value unbound)
3631 (let ((args (widget-get widget :args))
3632 (completion-ignore-case (widget-get widget :case-fold))
3633 current choices old)
3634 ;; Find the first arg that matches VALUE.
3637 (if (widget-apply (car look) :match value)
3638 (setq old (car look)
3640 (setq look (cdr look)))))
3643 (cond ((= (length args) 0)
3645 ((= (length args) 1)
3647 ((and (= (length args) 2)
3649 (if (eq old (nth 0 args))
3654 (setq current (car args)
3657 (cons (cons (widget-apply current :menu-tag-get)
3660 (let ((val (completing-read prompt choices nil t)))
3662 (let ((try (try-completion val choices)))
3665 (cdr (assoc val choices)))
3668 (widget-prompt-value current prompt nil t)
3671 (define-widget 'radio 'radio-button-choice
3672 "A union of several sexp types."
3674 :format "%{%t%}:\n%v"
3675 :prompt-value 'widget-choice-prompt-value)
3677 (define-widget 'repeat 'editable-list
3678 "A variable length homogeneous list."
3680 :format "%{%t%}:\n%v%i\n")
3682 (define-widget 'set 'checklist
3683 "A list of members from a fixed set."
3685 :format "%{%t%}:\n%v")
3687 (define-widget 'boolean 'toggle
3688 "To be nil or non-nil, that is the question."
3690 :prompt-value 'widget-boolean-prompt-value
3691 :button-prefix 'widget-push-button-prefix
3692 :button-suffix 'widget-push-button-suffix
3693 :format "%{%t%}: %[Toggle%] %v\n"
3697 (defun widget-boolean-prompt-value (widget prompt value unbound)
3698 ;; Toggle a boolean.
3701 ;;; The `color' Widget.
3704 (define-widget 'color 'editable-field
3705 "Choose a color name (with sample)."
3706 :format "%{%t%}: %v (%{sample%})\n"
3707 :value-create 'widget-color-value-create
3711 :complete 'widget-color-complete
3712 :sample-face-get 'widget-color-sample-face-get
3713 :notify 'widget-color-notify
3714 :action 'widget-color-action)
3716 (defun widget-color-value-create (widget)
3717 (widget-field-value-create widget)
3719 (widget-create-child-and-convert
3721 :tag " Choose " :action 'widget-color--choose-action)
3722 (widget-insert " "))
3724 (defun widget-color--choose-action (widget &optional event)
3725 (list-colors-display
3728 (when (buffer-live-p ,(current-buffer))
3729 (widget-value-set ',(widget-get widget :parent) color)
3730 (let* ((buf (get-buffer "*Colors*"))
3731 (win (get-buffer-window buf 0)))
3733 (and win (> (length (window-list)) 1)
3734 (delete-window win)))
3735 (pop-to-buffer ,(current-buffer))))))
3737 (defun widget-color-complete (widget)
3738 "Complete the color in WIDGET."
3739 (require 'facemenu) ; for facemenu-color-alist
3740 (completion-in-region (widget-field-start widget)
3741 (max (point) (widget-field-text-end widget))
3742 (or facemenu-color-alist
3743 (sort (defined-colors) 'string-lessp))))
3745 (defun widget-color-sample-face-get (widget)
3746 (let* ((value (condition-case nil
3747 (widget-value widget)
3748 (error (widget-get widget :value)))))
3749 (if (color-defined-p value)
3750 (list (cons 'foreground-color value))
3753 (defun widget-color-action (widget &optional event)
3754 "Prompt for a color."
3755 (let* ((tag (widget-apply widget :menu-tag-get))
3756 (prompt (concat tag ": "))
3757 (value (widget-value widget))
3758 (start (widget-field-start widget))
3759 (answer (facemenu-read-color prompt)))
3760 (unless (zerop (length answer))
3761 (widget-value-set widget answer)
3763 (widget-apply widget :notify widget event))))
3765 (defun widget-color-notify (widget child &optional event)
3766 "Update the sample, and notify the parent."
3767 (overlay-put (widget-get widget :sample-overlay)
3768 'face (widget-apply widget :sample-face-get))
3769 (widget-default-notify widget child event))
3773 (defun widget-echo-help (pos)
3774 "Display help-echo text for widget at POS."
3775 (let* ((widget (widget-at pos))
3776 (help-echo (and widget (widget-get widget :help-echo))))
3777 (if (functionp help-echo)
3778 (setq help-echo (funcall help-echo widget)))
3779 (if help-echo (message "%s" (eval help-echo)))))
3785 ;; arch-tag: a076e75e-18a1-4b46-8be5-3f317bcbc707
3786 ;;; wid-edit.el ends here