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.]
64 (defun widget-event-point (event)
65 "Character position of the end of event if that exists, or nil."
66 (posn-point (event-end event
)))
68 (defun widget-button-release-event-p (event)
69 "Non-nil if EVENT is a mouse-button-release event object."
71 (memq (event-basic-type event
) '(mouse-1 mouse-2 mouse-3
))
72 (or (memq 'click
(event-modifiers event
))
73 (memq 'drag
(event-modifiers event
)))))
78 "Customization support for the Widget Library."
79 :link
'(custom-manual "(widget)Top")
80 :link
'(emacs-library-link :tag
"Lisp File" "widget.el")
84 (defgroup widget-documentation nil
85 "Options controlling the display of documentation strings."
88 (defgroup widget-faces nil
89 "Faces used by the widget library."
93 (defvar widget-documentation-face
'widget-documentation
94 "Face used for documentation strings in widgets.
95 This exists as a variable so it can be set locally in certain buffers.")
97 (defface widget-documentation
'((((class color
)
99 (:foreground
"lime green"))
102 (:foreground
"dark green"))
104 "Face used for documentation text."
105 :group
'widget-documentation
106 :group
'widget-faces
)
107 (define-obsolete-face-alias 'widget-documentation-face
108 'widget-documentation
"22.1")
110 (defvar widget-button-face
'widget-button
111 "Face used for buttons in widgets.
112 This exists as a variable so it can be set locally in certain buffers.")
114 (defface widget-button
'((t (:weight bold
)))
115 "Face used for widget buttons."
116 :group
'widget-faces
)
117 (define-obsolete-face-alias 'widget-button-face
'widget-button
"22.1")
119 (defcustom widget-mouse-face
'highlight
120 "Face used for widget buttons when the mouse is above them."
122 :group
'widget-faces
)
124 ;; TTY gets special definitions here and in the next defface, because
125 ;; the gray colors defined for other displays cause black text on a black
126 ;; background, at least on light-background TTYs.
127 (defface widget-field
'((((type tty
))
128 :background
"yellow3"
130 (((class grayscale color
)
132 :background
"gray85")
133 (((class grayscale color
)
135 :background
"dim gray")
138 "Face used for editable fields."
139 :group
'widget-faces
)
140 (define-obsolete-face-alias 'widget-field-face
'widget-field
"22.1")
142 (defface widget-single-line-field
'((((type tty
))
145 (((class grayscale color
)
147 :background
"gray85")
148 (((class grayscale color
)
150 :background
"dim gray")
153 "Face used for editable fields spanning only a single line."
154 :group
'widget-faces
)
155 (define-obsolete-face-alias 'widget-single-line-field-face
156 'widget-single-line-field
"22.1")
158 ;;; This causes display-table to be loaded, and not usefully.
159 ;;;(defvar widget-single-line-display-table
160 ;;; (let ((table (make-display-table)))
161 ;;; (aset table 9 "^I")
162 ;;; (aset table 10 "^J")
164 ;;; "Display table used for single-line editable fields.")
166 ;;;(when (fboundp 'set-face-display-table)
167 ;;; (set-face-display-table 'widget-single-line-field-face
168 ;;; widget-single-line-display-table))
170 ;;; Utility functions.
172 ;; These are not really widget specific.
174 (defun widget-princ-to-string (object)
175 "Return string representation of OBJECT, any Lisp object.
176 No quoting characters are used; no delimiters are printed around
177 the contents of strings."
178 (with-output-to-string
181 (defun widget-clear-undo ()
182 "Clear all undo information."
183 (buffer-disable-undo (current-buffer))
184 (buffer-enable-undo))
186 (defcustom widget-menu-max-size
40
187 "Largest number of items allowed in a popup-menu.
188 Larger menus are read through the minibuffer."
192 (defcustom widget-menu-max-shortcuts
40
193 "Largest number of items for which it works to choose one with a character.
194 For a larger number of items, the minibuffer is used."
198 (defcustom widget-menu-minibuffer-flag nil
199 "Control how to ask for a choice from the keyboard.
200 Non-nil means use the minibuffer;
201 nil means read a single character."
205 (defun widget-choose (title items
&optional event
)
206 "Choose an item from a list.
208 First argument TITLE is the name of the list.
209 Second argument ITEMS is a list whose members are either
210 (NAME . VALUE), to indicate selectable items, or just strings to
211 indicate unselectable items.
212 Optional third argument EVENT is an input event.
214 The user is asked to choose between each NAME from the items alist,
215 and the VALUE of the chosen element will be returned. If EVENT is a
216 mouse event, and the number of elements in items is less than
217 `widget-menu-max-size', a popup menu will be used, otherwise the
219 (cond ((and (< (length items
) widget-menu-max-size
)
220 event
(display-popup-menus-p))
223 (list title
(cons "" items
))))
224 ((or widget-menu-minibuffer-flag
225 (> (length items
) widget-menu-max-shortcuts
))
226 ;; Read the choice of name from the minibuffer.
227 (setq items
(widget-remove-if 'stringp items
))
228 (let ((val (completing-read (concat title
": ") items nil t
)))
230 (let ((try (try-completion val items
)))
233 (cdr (assoc val items
))))))
235 ;; Construct a menu of the choices
236 ;; and then use it for prompting for a single character.
237 (let* ((overriding-terminal-local-map (make-sparse-keymap))
239 map choice some-choice-enabled value
)
240 ;; Define SPC as a prefix char to get to this menu.
241 (define-key overriding-terminal-local-map
" "
242 (setq map
(make-sparse-keymap title
)))
243 (with-current-buffer (get-buffer-create " widget-choose")
245 (insert "Available choices:\n\n")
247 (setq choice
(car items
) items
(cdr items
))
249 (let* ((name (car choice
))
250 (function (cdr choice
)))
251 (insert (format "%c = %s\n" next-digit name
))
252 (define-key map
(vector next-digit
) function
)
253 (setq some-choice-enabled t
)))
254 ;; Allocate digits to disabled alternatives
255 ;; so that the digit of a given alternative never varies.
256 (setq next-digit
(1+ next-digit
)))
257 (insert "\nC-g = Quit")
258 (goto-char (point-min))
260 (or some-choice-enabled
261 (error "None of the choices is currently meaningful"))
262 (define-key map
[?\C-g
] 'keyboard-quit
)
263 (define-key map
[t] 'keyboard-quit)
264 (define-key map [?\M-\C-v] 'scroll-other-window)
265 (define-key map [?\M--] 'negative-argument)
266 (setcdr map (nreverse (cdr map)))
267 ;; Read a char with the menu, and return the result
268 ;; that corresponds to it.
269 (save-window-excursion
270 (let ((buf (get-buffer " widget-choose")))
271 (fit-window-to-buffer (display-buffer buf))
272 (let ((cursor-in-echo-area t)
276 (while (not (or (and (integerp char)
277 (>= char ?0) (< char next-digit))
278 (eq value 'keyboard-quit)))
279 ;; Unread a SPC to lead to our new menu.
280 (setq unread-command-events (cons ?\s unread-command-events))
281 (setq keys (read-key-sequence title))
283 (lookup-key overriding-terminal-local-map keys t)
285 (cond ((eq value 'scroll-other-window)
286 (let ((minibuffer-scroll-window
287 (get-buffer-window buf)))
289 (scroll-other-window-down
290 (window-height minibuffer-scroll-window))
291 (scroll-other-window))
293 ((eq value 'negative-argument)
297 (when (eq value 'keyboard-quit)
301 (defun widget-remove-if (predictate list)
302 (let (result (tail list))
304 (or (funcall predictate (car tail))
305 (setq result (cons (car tail) result)))
306 (setq tail (cdr tail)))
309 ;;; Widget text specifications.
311 ;; These functions are for specifying text properties.
313 ;; We can set it to nil now that get_local_map uses get_pos_property.
314 (defconst widget-field-add-space nil
315 "Non-nil means add extra space at the end of editable text fields.
316 If you don't add the space, it will become impossible to edit a zero
319 (defvar widget-field-use-before-change t
320 "Non-nil means use `before-change-functions' to track editable fields.
321 This enables the use of undo, but doesn't work on Emacs 19.34 and earlier.
322 Using before hooks also means that the :notify function can't know the
325 (defun widget-specify-field (widget from to)
326 "Specify editable button for WIDGET between FROM and TO."
327 ;; Terminating space is not part of the field, but necessary in
328 ;; order for local-map to work. Remove next sexp if local-map works
329 ;; at the end of the overlay.
332 (cond ((null (widget-get widget :size))
334 (widget-field-add-space
335 (insert-and-inherit " ")))
337 (let ((keymap (widget-get widget :keymap))
338 (face (or (widget-get widget :value-face) 'widget-field))
339 (help-echo (widget-get widget :help-echo))
340 (follow-link (widget-get widget :follow-link))
342 (or (not widget-field-add-space) (widget-get widget :size))))
343 (if (functionp help-echo)
344 (setq help-echo 'widget-mouse-help))
345 (when (= (char-before to) ?\n)
346 ;; When the last character in the field is a newline, we want to
347 ;; give it a `field' char-property of `boundary', which helps the
348 ;; C-n/C-p act more naturally when entering/leaving the field. We
349 ;; do this by making a small secondary overlay to contain just that
351 (let ((overlay (make-overlay (1- to) to nil t nil)))
352 (overlay-put overlay 'field 'boundary)
353 ;; We need the real field for tabbing.
354 (overlay-put overlay 'real-field widget)
355 ;; Use `local-map' here, not `keymap', so that normal editing
356 ;; works in the field when, say, Custom uses `suppress-keymap'.
357 (overlay-put overlay 'local-map keymap)
358 (overlay-put overlay 'face face)
359 (overlay-put overlay 'follow-link follow-link)
360 (overlay-put overlay 'help-echo help-echo))
362 (setq rear-sticky t))
363 (let ((overlay (make-overlay from to nil nil rear-sticky)))
364 (widget-put widget :field-overlay overlay)
365 ;;(overlay-put overlay 'detachable nil)
366 (overlay-put overlay 'field widget)
367 (overlay-put overlay 'local-map keymap)
368 (overlay-put overlay 'face face)
369 (overlay-put overlay 'follow-link follow-link)
370 (overlay-put overlay 'help-echo help-echo)))
371 (widget-specify-secret widget))
373 (defun widget-specify-secret (field)
374 "Replace text in FIELD with value of `:secret', if non-nil."
375 (let ((secret (widget-get field :secret))
376 (size (widget-get field :size)))
378 (let ((begin (widget-field-start field))
379 (end (widget-field-end field)))
381 (while (and (> end begin)
382 (eq (char-after (1- end)) ?\s))
383 (setq end (1- end))))
385 (let ((old (char-after begin)))
386 (unless (eq old secret)
387 (subst-char-in-region begin (1+ begin) old secret)
388 (put-text-property begin (1+ begin) 'secret old))
389 (setq begin (1+ begin))))))))
391 (defun widget-specify-button (widget from to)
392 "Specify button for WIDGET between FROM and TO."
393 (let ((overlay (make-overlay from to nil t nil))
394 (follow-link (widget-get widget :follow-link))
395 (help-echo (widget-get widget :help-echo)))
396 (widget-put widget :button-overlay overlay)
397 (if (functionp help-echo)
398 (setq help-echo 'widget-mouse-help))
399 (overlay-put overlay 'button widget)
400 (overlay-put overlay 'keymap (widget-get widget :keymap))
401 (overlay-put overlay 'evaporate t)
402 ;; We want to avoid the face with image buttons.
403 (unless (widget-get widget :suppress-face)
404 (overlay-put overlay 'face (widget-apply widget :button-face-get))
405 (overlay-put overlay 'mouse-face
406 ;; Make new list structure for the mouse-face value
407 ;; so that different widgets will have
408 ;; different `mouse-face' property values
409 ;; and will highlight separately.
410 (let ((mouse-face-value
411 (widget-apply widget :mouse-face-get)))
412 ;; If it's a list, copy it.
413 (if (listp mouse-face-value)
414 (copy-sequence mouse-face-value)
415 ;; If it's a symbol, put it in a list.
416 (list mouse-face-value)))))
417 (overlay-put overlay 'pointer 'hand)
418 (overlay-put overlay 'follow-link follow-link)
419 (overlay-put overlay 'help-echo help-echo)))
421 (defun widget-mouse-help (window overlay point)
422 "Help-echo callback for widgets whose :help-echo is a function."
423 (with-current-buffer (overlay-buffer overlay)
424 (let* ((widget (widget-at (overlay-start overlay)))
425 (help-echo (if widget (widget-get widget :help-echo))))
426 (if (functionp help-echo)
427 (funcall help-echo widget)
430 (defun widget-specify-sample (widget from to)
431 "Specify sample for WIDGET between FROM and TO."
432 (let ((overlay (make-overlay from to nil t nil)))
433 (overlay-put overlay 'face (widget-apply widget :sample-face-get))
434 (overlay-put overlay 'evaporate t)
435 (widget-put widget :sample-overlay overlay)))
437 (defun widget-specify-doc (widget from to)
438 "Specify documentation for WIDGET between FROM and TO."
439 (let ((overlay (make-overlay from to nil t nil)))
440 (overlay-put overlay 'widget-doc widget)
441 (overlay-put overlay 'face widget-documentation-face)
442 (overlay-put overlay 'evaporate t)
443 (widget-put widget :doc-overlay overlay)))
445 (defmacro widget-specify-insert (&rest form)
446 "Execute FORM without inheriting any text properties."
448 (let ((inhibit-read-only t)
449 (inhibit-modification-hooks t))
450 (narrow-to-region (point) (point))
451 (prog1 (progn ,@form)
452 (goto-char (point-max))))))
454 (defface widget-inactive
455 '((t :inherit shadow))
456 "Face used for inactive widgets."
457 :group 'widget-faces)
458 (define-obsolete-face-alias 'widget-inactive-face
459 'widget-inactive "22.1")
461 (defun widget-specify-inactive (widget from to)
462 "Make WIDGET inactive for user modifications."
463 (unless (widget-get widget :inactive)
464 (let ((overlay (make-overlay from to nil t nil)))
465 (overlay-put overlay 'face 'widget-inactive)
466 ;; This is disabled, as it makes the mouse cursor change shape.
467 ;; (overlay-put overlay 'mouse-face 'widget-inactive)
468 (overlay-put overlay 'evaporate t)
469 (overlay-put overlay 'priority 100)
470 (overlay-put overlay 'modification-hooks '(widget-overlay-inactive))
471 (widget-put widget :inactive overlay))))
473 (defun widget-overlay-inactive (&rest junk)
474 "Ignoring the arguments, signal an error."
475 (unless inhibit-read-only
476 (error "The widget here is not active")))
479 (defun widget-specify-active (widget)
480 "Make WIDGET active for user modifications."
481 (let ((inactive (widget-get widget :inactive)))
483 (delete-overlay inactive)
484 (widget-put widget :inactive nil))))
486 ;;; Widget Properties.
488 (defsubst widget-type (widget)
489 "Return the type of WIDGET. The type is a symbol."
493 (defun widgetp (widget)
494 "Return non-nil if WIDGET is a widget."
496 (get widget 'widget-type)
498 (symbolp (car widget))
499 (get (car widget) 'widget-type))))
501 (defun widget-get-indirect (widget property)
502 "In WIDGET, get the value of PROPERTY.
503 If the value is a symbol, return its binding.
504 Otherwise, just return the value."
505 (let ((value (widget-get widget property)))
510 (defun widget-member (widget property)
511 "Non-nil if there is a definition in WIDGET for PROPERTY."
512 (cond ((plist-member (cdr widget) property)
515 (widget-member (get (car widget) 'widget-type) property))
518 (defun widget-value (widget)
519 "Extract the current value of WIDGET."
521 :value-to-external (widget-apply widget :value-get)))
523 (defun widget-value-set (widget value)
524 "Set the current value of WIDGET to VALUE."
526 :value-set (widget-apply widget
527 :value-to-internal value)))
529 (defun widget-default-get (widget)
530 "Extract the default external value of WIDGET."
531 (widget-apply widget :value-to-external
532 (or (widget-get widget :value)
533 (widget-apply widget :default-get))))
535 (defun widget-match-inline (widget vals)
536 "In WIDGET, match the start of VALS."
537 (cond ((widget-get widget :inline)
538 (widget-apply widget :match-inline vals))
540 (widget-apply widget :match (car vals)))
541 (cons (list (car vals)) (cdr vals)))
544 (defun widget-apply-action (widget &optional event)
545 "Apply :action in WIDGET in response to EVENT."
546 (if (widget-apply widget :active)
547 (widget-apply widget :action event)
548 (error "Attempt to perform action on inactive widget")))
550 ;;; Helper functions.
552 ;; These are widget specific.
555 (defun widget-prompt-value (widget prompt &optional value unbound)
556 "Prompt for a value matching WIDGET, using PROMPT.
557 The current value is assumed to be VALUE, unless UNBOUND is non-nil."
558 (unless (listp widget)
559 (setq widget (list widget)))
560 (setq prompt (format "[%s] %s" (widget-type widget) prompt))
561 (setq widget (widget-convert widget))
562 (let ((answer (widget-apply widget :prompt-value prompt value unbound)))
563 (unless (widget-apply widget :match answer)
564 (error "Value does not match %S type" (car widget)))
567 (defun widget-get-sibling (widget)
568 "Get the item WIDGET is assumed to toggle.
569 This is only meaningful for radio buttons or checkboxes in a list."
570 (let* ((children (widget-get (widget-get widget :parent) :children))
574 (setq child (car children)
575 children (cdr children))
576 (when (eq (widget-get child :button) widget)
577 (throw 'child child)))
580 (defun widget-map-buttons (function &optional buffer maparg)
581 "Map FUNCTION over the buttons in BUFFER.
582 FUNCTION is called with the arguments WIDGET and MAPARG.
584 If FUNCTION returns non-nil, the walk is cancelled.
586 The arguments MAPARG, and BUFFER default to nil and (current-buffer),
588 (let ((cur (point-min))
591 (with-current-buffer buffer (overlay-lists))
593 (setq overlays (append (car overlays) (cdr overlays)))
594 (while (setq cur (pop overlays))
595 (setq widget (overlay-get cur 'button))
596 (if (and widget (funcall function widget maparg))
597 (setq overlays nil)))))
601 (defcustom widget-image-directory (file-name-as-directory
602 (expand-file-name "images/custom" data-directory))
603 "Where widget button images are located.
604 If this variable is nil, widget will try to locate the directory
609 (defcustom widget-image-enable t
610 "If non-nil, use image buttons in widgets when available."
615 (defcustom widget-image-conversion
616 '((xpm ".xpm") (gif ".gif") (png ".png") (jpeg ".jpg" ".jpeg")
618 "Conversion alist from image formats to file name suffixes."
620 :type '(repeat (cons :format "%v"
621 (symbol :tag "Image Format" unknown)
622 (repeat :tag "Suffixes"
623 (string :format "%v")))))
625 (defun widget-image-find (image)
626 "Create a graphical button from IMAGE.
627 IMAGE should either already be an image, or be a file name sans
628 extension (xpm, xbm, gif, jpg, or png) located in
629 `widget-image-directory' or otherwise where `find-image' will find it."
630 (cond ((not (and image widget-image-enable (display-graphic-p)))
631 ;; We don't want or can't use images.
634 (eq 'image (car image)))
635 ;; Already an image spec. Use it.
638 ;; A string. Look it up in relevant directories.
639 (let* ((load-path (cons widget-image-directory load-path))
641 (dolist (elt widget-image-conversion)
642 (dolist (ext (cdr elt))
643 (push (list :type (car elt) :file (concat image ext)) specs)))
644 (find-image (nreverse specs))))
649 (defvar widget-button-pressed-face 'widget-button-pressed
650 "Face used for pressed buttons in widgets.
651 This exists as a variable so it can be set locally in certain
654 (defun widget-image-insert (widget tag image &optional down inactive)
655 "In WIDGET, insert the text TAG or, if supported, IMAGE.
656 IMAGE should either be an image or an image file name sans extension
657 \(xpm, xbm, gif, jpg, or png) located in `widget-image-directory'.
659 Optional arguments DOWN and INACTIVE are used instead of IMAGE when the
660 button is pressed or inactive, respectively. These are currently ignored."
661 (if (and (featurep 'image)
662 (setq image (widget-image-find image)))
663 (progn (widget-put widget :suppress-face t)
664 (insert-image image tag))
667 (defun widget-move-and-invoke (event)
668 "Move to where you click, and if it is an active field, invoke it."
670 (mouse-set-point event)
671 (let ((pos (widget-event-point event)))
672 (if (and pos (get-char-property pos 'button))
673 (widget-button-click event))))
677 (defgroup widget-button nil
678 "The look of various kinds of buttons."
681 (defcustom widget-button-prefix ""
682 "String used as prefix for buttons."
684 :group 'widget-button)
686 (defcustom widget-button-suffix ""
687 "String used as suffix for buttons."
689 :group 'widget-button)
691 ;;; Creating Widgets.
694 (defun widget-create (type &rest args)
695 "Create widget of TYPE.
696 The optional ARGS are additional keyword arguments."
697 (let ((widget (apply 'widget-convert type args)))
698 (widget-apply widget :create)
701 (defun widget-create-child-and-convert (parent type &rest args)
702 "As part of the widget PARENT, create a child widget TYPE.
703 The child is converted, using the keyword arguments ARGS."
704 (let ((widget (apply 'widget-convert type args)))
705 (widget-put widget :parent parent)
706 (unless (widget-get widget :indent)
707 (widget-put widget :indent (+ (or (widget-get parent :indent) 0)
708 (or (widget-get widget :extra-offset) 0)
709 (widget-get parent :offset))))
710 (widget-apply widget :create)
713 (defun widget-create-child (parent type)
714 "Create widget of TYPE."
715 (let ((widget (widget-copy type)))
716 (widget-put widget :parent parent)
717 (unless (widget-get widget :indent)
718 (widget-put widget :indent (+ (or (widget-get parent :indent) 0)
719 (or (widget-get widget :extra-offset) 0)
720 (widget-get parent :offset))))
721 (widget-apply widget :create)
724 (defun widget-create-child-value (parent type value)
725 "Create widget of TYPE with value VALUE."
726 (let ((widget (widget-copy type)))
727 (widget-put widget :value (widget-apply widget :value-to-internal value))
728 (widget-put widget :parent parent)
729 (unless (widget-get widget :indent)
730 (widget-put widget :indent (+ (or (widget-get parent :indent) 0)
731 (or (widget-get widget :extra-offset) 0)
732 (widget-get parent :offset))))
733 (widget-apply widget :create)
737 (defun widget-delete (widget)
739 (widget-apply widget :delete))
741 (defun widget-copy (widget)
742 "Make a deep copy of WIDGET."
743 (widget-apply (copy-sequence widget) :copy))
745 (defun widget-convert (type &rest args)
746 "Convert TYPE to a widget without inserting it in the buffer.
747 The optional ARGS are additional keyword arguments."
748 ;; Don't touch the type.
749 (let* ((widget (if (symbolp type)
751 (copy-sequence type)))
755 ;; First set the :args keyword.
756 (while (cdr current) ;Look in the type.
757 (if (and (keywordp (cadr current))
758 ;; If the last element is a keyword,
759 ;; it is still the :args element,
760 ;; even though it is a keyword.
762 (if (eq (cadr current) :args)
763 ;; If :args is explicitly specified, obey it.
765 ;; Some other irrelevant keyword.
766 (setq current (cdr (cdr current))))
767 (setcdr current (list :args (cdr current)))
769 (while (and args (not done)) ;Look in ARGS.
770 (cond ((eq (car args) :args)
771 ;; Handle explicit specification of :args.
772 (setq args (cadr args)
774 ((keywordp (car args))
775 (setq args (cddr args)))
778 (widget-put widget :args args))
779 ;; Then Convert the widget.
782 (let ((convert-widget (plist-get (cdr type) :convert-widget)))
784 (setq widget (funcall convert-widget widget))))
785 (setq type (get (car type) 'widget-type)))
786 ;; Finally set the keyword args.
788 (let ((next (nth 0 keys)))
791 (widget-put widget next (nth 1 keys))
792 (setq keys (nthcdr 2 keys)))
794 ;; Convert the :value to internal format.
795 (if (widget-member widget :value)
797 :value (widget-apply widget
799 (widget-get widget :value))))
800 ;; Return the newly create widget.
804 (defun widget-insert (&rest args)
805 "Call `insert' with ARGS even if surrounding text is read only."
806 (let ((inhibit-read-only t)
807 (inhibit-modification-hooks t))
808 (apply 'insert args)))
810 (defun widget-convert-text (type from to
811 &optional button-from button-to
813 "Return a widget of type TYPE with endpoint FROM TO.
814 No text will be inserted to the buffer, instead the text between FROM
815 and TO will be used as the widgets end points. If optional arguments
816 BUTTON-FROM and BUTTON-TO are given, these will be used as the widgets
818 Optional ARGS are extra keyword arguments for TYPE."
819 (let ((widget (apply 'widget-convert type :delete 'widget-leave-text args))
820 (from (copy-marker from))
821 (to (copy-marker to)))
822 (set-marker-insertion-type from t)
823 (set-marker-insertion-type to nil)
824 (widget-put widget :from from)
825 (widget-put widget :to to)
827 (widget-specify-button widget button-from button-to))
830 (defun widget-convert-button (type from to &rest args)
831 "Return a widget of type TYPE with endpoint FROM TO.
832 Optional ARGS are extra keyword arguments for TYPE.
833 No text will be inserted to the buffer, instead the text between FROM
834 and TO will be used as the widgets end points, as well as the widgets
836 (apply 'widget-convert-text type from to from to args))
838 (defun widget-leave-text (widget)
839 "Remove markers and overlays from WIDGET and its children."
840 (let ((button (widget-get widget :button-overlay))
841 (sample (widget-get widget :sample-overlay))
842 (doc (widget-get widget :doc-overlay))
843 (field (widget-get widget :field-overlay)))
844 (set-marker (widget-get widget :from) nil)
845 (set-marker (widget-get widget :to) nil)
847 (delete-overlay button))
849 (delete-overlay sample))
851 (delete-overlay doc))
853 (delete-overlay field))
854 (mapc 'widget-leave-text (widget-get widget :children))))
856 ;;; Keymap and Commands.
858 ;; This alias exists only so that one can choose in doc-strings (e.g.
859 ;; Custom-mode) which key-binding of widget-keymap one wants to refer to.
860 ;; http://lists.gnu.org/archive/html/emacs-devel/2008-11/msg00480.html
861 (define-obsolete-function-alias 'advertised-widget-backward
862 'widget-backward "23.2")
865 (defvar widget-keymap
866 (let ((map (make-sparse-keymap)))
867 (define-key map "\t" 'widget-forward)
868 (define-key map "\e\t" 'widget-backward)
869 (define-key map [(shift tab)] 'widget-backward)
870 (put 'widget-backward :advertised-binding [(shift tab)])
871 (define-key map [backtab] 'widget-backward)
872 (define-key map [down-mouse-2] 'widget-button-click)
873 (define-key map [down-mouse-1] 'widget-button-click)
874 ;; The following definition needs to avoid using escape sequences that
875 ;; might get converted to ^M when building loaddefs.el
876 (define-key map [(control ?m)] 'widget-button-press)
878 "Keymap containing useful binding for buffers containing widgets.
879 Recommended as a parent keymap for modes using widgets.
880 Note that such modes will need to require wid-edit.")
882 (defvar widget-global-map global-map
883 "Keymap used for events a widget does not handle itself.")
884 (make-variable-buffer-local 'widget-global-map)
886 (defvar widget-field-keymap
887 (let ((map (copy-keymap widget-keymap)))
888 (define-key map "\C-k" 'widget-kill-line)
889 (define-key map "\M-\t" 'widget-complete)
890 (define-key map "\C-m" 'widget-field-activate)
891 ;; Since the widget code uses a `field' property to identify fields,
892 ;; ordinary beginning-of-line does the right thing.
893 ;; (define-key map "\C-a" 'widget-beginning-of-line)
894 (define-key map "\C-e" 'widget-end-of-line)
896 "Keymap used inside an editable field.")
898 (defvar widget-text-keymap
899 (let ((map (copy-keymap widget-keymap)))
900 ;; Since the widget code uses a `field' property to identify fields,
901 ;; ordinary beginning-of-line does the right thing.
902 ;; (define-key map "\C-a" 'widget-beginning-of-line)
903 (define-key map "\C-e" 'widget-end-of-line)
905 "Keymap used inside a text field.")
907 (defun widget-field-activate (pos &optional event)
908 "Invoke the editable field at point."
910 (let ((field (widget-field-at pos)))
912 (widget-apply-action field event)
914 (lookup-key widget-global-map (this-command-keys))))))
916 (defface widget-button-pressed
917 '((((min-colors 88) (class color))
918 (:foreground "red1"))
922 (:weight bold :underline t)))
923 "Face used for pressed buttons."
924 :group 'widget-faces)
925 (define-obsolete-face-alias 'widget-button-pressed-face
926 'widget-button-pressed "22.1")
928 (defvar widget-button-click-moves-point nil
929 "If non-nil, `widget-button-click' moves point to a button after invoking it.
930 If nil, point returns to its original position after invoking a button.")
932 (defun widget-button-click (event)
933 "Invoke the button that the mouse is pointing at."
935 (if (widget-event-point event)
936 (let* ((oevent event)
937 (mouse-1 (memq (event-basic-type event) '(mouse-1 down-mouse-1)))
938 (pos (widget-event-point event))
939 (start (event-start event))
940 (button (get-char-property
941 pos 'button (and (windowp (posn-window start))
942 (window-buffer (posn-window start)))))
944 (when (or (null button)
945 (catch 'button-press-cancelled
946 ;; Mouse click on a widget button. Do the following
947 ;; in a save-excursion so that the click on the button
948 ;; doesn't change point.
949 (save-selected-window
950 (select-window (posn-window (event-start event)))
952 (goto-char (posn-point (event-start event)))
953 (let* ((overlay (widget-get button :button-overlay))
954 (pressed-face (or (widget-get button :pressed-face)
955 widget-button-pressed-face))
956 (face (overlay-get overlay 'face))
957 (mouse-face (overlay-get overlay 'mouse-face)))
959 ;; Read events, including mouse-movement
960 ;; events, waiting for a release event. If we
961 ;; began with a mouse-1 event and receive a
962 ;; movement event, that means the user wants
963 ;; to perform drag-selection, so cancel the
964 ;; button press and do the default mouse-1
965 ;; action. For mouse-2, just highlight/
966 ;; unhighlight the button the mouse was
967 ;; initially on when we move over it.
969 (when face ; avoid changing around image
970 (overlay-put overlay 'face pressed-face)
971 (overlay-put overlay 'mouse-face pressed-face))
972 (unless (widget-apply button :mouse-down-action event)
973 (let ((track-mouse t))
974 (while (not (widget-button-release-event-p event))
975 (setq event (read-event))
976 (when (and mouse-1 (mouse-movement-p event))
977 (push event unread-command-events)
979 (throw 'button-press-cancelled t))
980 (unless (or (integerp event)
981 (memq (car event) '(switch-frame select-window))
982 (eq (car event) 'scroll-bar-movement))
983 (setq pos (widget-event-point event))
985 (eq (get-char-property pos 'button)
988 (overlay-put overlay 'face pressed-face)
989 (overlay-put overlay 'mouse-face pressed-face))
990 (overlay-put overlay 'face face)
991 (overlay-put overlay 'mouse-face mouse-face))))))
993 ;; When mouse is released over the button, run
994 ;; its action function.
995 (when (and pos (eq (get-char-property pos 'button) button))
997 (widget-apply-action button event)
998 (if widget-button-click-moves-point
999 (setq newpoint (point)))))
1000 (overlay-put overlay 'face face)
1001 (overlay-put overlay 'mouse-face mouse-face))))
1003 (if newpoint (goto-char newpoint))
1004 ;; This loses if the widget action switches windows. -- cyd
1005 ;; (unless (pos-visible-in-window-p (widget-event-point event))
1006 ;; (mouse-set-point event)
1007 ;; (beginning-of-line)
1011 (let ((up t) command)
1012 ;; Mouse click not on a widget button. Find the global
1013 ;; command to run, and check whether it is bound to an
1016 (cond ((setq command ;down event
1017 (lookup-key widget-global-map [down-mouse-1]))
1019 ((setq command ;up event
1020 (lookup-key widget-global-map [mouse-1]))))
1021 (cond ((setq command ;down event
1022 (lookup-key widget-global-map [down-mouse-2]))
1024 ((setq command ;up event
1025 (lookup-key widget-global-map [mouse-2])))))
1027 ;; Don't execute up events twice.
1028 (while (not (widget-button-release-event-p event))
1029 (setq event (read-event))))
1031 (call-interactively command)))))
1032 (message "You clicked somewhere weird.")))
1034 (defun widget-button-press (pos &optional event)
1035 "Invoke button at POS."
1037 (let ((button (get-char-property pos 'button)))
1039 (widget-apply-action button event)
1040 (let ((command (lookup-key widget-global-map (this-command-keys))))
1041 (when (commandp command)
1042 (call-interactively command))))))
1044 (defun widget-tabable-at (&optional pos)
1045 "Return the tabable widget at POS, or nil.
1046 POS defaults to the value of (point)."
1047 (let ((widget (widget-at pos)))
1049 (let ((order (widget-get widget :tab-order)))
1055 (defvar widget-use-overlay-change t
1056 "If non-nil, use overlay change functions to tab around in the buffer.
1057 This is much faster, but doesn't work reliably on Emacs 19.34.")
1059 (defun widget-move (arg)
1060 "Move point to the ARG next field or button.
1061 ARG may be negative to move backward."
1062 (or (bobp) (> arg 0) (backward-char))
1065 (old (widget-tabable-at)))
1069 (goto-char (point-min))
1070 (setq wrapped (1+ wrapped)))
1071 (widget-use-overlay-change
1072 (goto-char (next-overlay-change (point))))
1077 (error "No buttons or fields found"))
1078 (let ((new (widget-tabable-at)))
1080 (unless (eq new old)
1086 (goto-char (point-max))
1087 (setq wrapped (1+ wrapped)))
1088 (widget-use-overlay-change
1089 (goto-char (previous-overlay-change (point))))
1094 (error "No buttons or fields found"))
1095 (let ((new (widget-tabable-at)))
1097 (unless (eq new old)
1098 (setq arg (1+ arg))))))
1099 (let ((new (widget-tabable-at)))
1100 (while (eq (widget-tabable-at) new)
1103 (widget-echo-help (point))
1104 (run-hooks 'widget-move-hook))
1106 (defun widget-forward (arg)
1107 "Move point to the next field or button.
1108 With optional ARG, move across that many fields."
1110 (run-hooks 'widget-forward-hook)
1113 (defun widget-backward (arg)
1114 "Move point to the previous field or button.
1115 With optional ARG, move across that many fields."
1117 (run-hooks 'widget-backward-hook)
1118 (widget-move (- arg)))
1120 ;; Since the widget code uses a `field' property to identify fields,
1121 ;; ordinary beginning-of-line does the right thing.
1122 (defalias 'widget-beginning-of-line 'beginning-of-line)
1124 (defun widget-end-of-line ()
1125 "Go to end of field or end of line, whichever is first.
1126 Trailing spaces at the end of padded fields are not considered part of
1129 ;; Ordinary end-of-line does the right thing, because we're inside
1130 ;; text with a `field' property.
1133 ;; ... except that we want to ignore trailing spaces in fields that
1134 ;; aren't terminated by a newline, because they are used as padding,
1135 ;; and ignored when extracting the entered value of the field.
1136 (skip-chars-backward " " (field-beginning (1- (point))))))
1138 (defun widget-kill-line ()
1139 "Kill to end of field or end of line, whichever is first."
1141 (let* ((field (widget-field-find (point)))
1142 (end (and field (widget-field-end field))))
1143 (if (and field (> (line-beginning-position 2) end))
1144 (kill-region (point) end)
1145 (call-interactively 'kill-line))))
1147 (defcustom widget-complete-field (lookup-key global-map "\M-\t")
1148 "Default function to call for completion inside fields."
1149 :options '(ispell-complete-word complete-tag lisp-complete-symbol)
1153 (defun widget-narrow-to-field ()
1156 (let ((field (widget-field-find (point))))
1158 (narrow-to-region (line-beginning-position) (line-end-position)))))
1160 ;; This used to say:
1161 ;; "When not inside a field, move to the previous button or field."
1162 ;; but AFAICS, it has always just thrown an error.
1163 (defun widget-complete ()
1164 "Complete content of editable field from point.
1165 When not inside a field, signal an error."
1167 (let ((field (widget-field-find (point))))
1169 (widget-apply field :complete)
1170 (error "Not in an editable field"))))
1172 ;;; Setting up the buffer.
1174 (defvar widget-field-new nil
1175 "List of all newly created editable fields in the buffer.")
1176 (make-variable-buffer-local 'widget-field-new)
1178 (defvar widget-field-list nil
1179 "List of all editable fields in the buffer.")
1180 (make-variable-buffer-local 'widget-field-list)
1182 (defun widget-at (&optional pos)
1183 "The button or field at POS (default, point)."
1184 (or (get-char-property (or pos (point)) 'button)
1185 (widget-field-at pos)))
1188 (defun widget-setup ()
1189 "Setup current buffer so editing string widgets works."
1190 (let ((inhibit-read-only t)
1191 (inhibit-modification-hooks t)
1193 (while widget-field-new
1194 (setq field (car widget-field-new)
1195 widget-field-new (cdr widget-field-new)
1196 widget-field-list (cons field widget-field-list))
1197 (let ((from (car (widget-get field :field-overlay)))
1198 (to (cdr (widget-get field :field-overlay))))
1199 (widget-specify-field field
1200 (marker-position from) (marker-position to))
1201 (set-marker from nil)
1202 (set-marker to nil))))
1204 (widget-add-change))
1206 (defvar widget-field-last nil)
1207 ;; Last field containing point.
1208 (make-variable-buffer-local 'widget-field-last)
1210 (defvar widget-field-was nil)
1211 ;; The widget data before the change.
1212 (make-variable-buffer-local 'widget-field-was)
1214 (defun widget-field-at (pos)
1215 "Return the widget field at POS, or nil if none."
1216 (let ((field (get-char-property (or pos (point)) 'field)))
1217 (if (eq field 'boundary)
1218 (get-char-property (or pos (point)) 'real-field)
1221 (defun widget-field-buffer (widget)
1222 "Return the buffer of WIDGET's editing field."
1223 (let ((overlay (widget-get widget :field-overlay)))
1224 (cond ((overlayp overlay)
1225 (overlay-buffer overlay))
1227 (marker-buffer (car overlay))))))
1229 (defun widget-field-start (widget)
1230 "Return the start of WIDGET's editing field."
1231 (let ((overlay (widget-get widget :field-overlay)))
1232 (if (overlayp overlay)
1233 (overlay-start overlay)
1236 (defun widget-field-end (widget)
1237 "Return the end of WIDGET's editing field."
1238 (let ((overlay (widget-get widget :field-overlay)))
1239 ;; Don't subtract one if local-map works at the end of the overlay,
1240 ;; or if a special `boundary' field has been added after the widget
1242 (if (overlayp overlay)
1243 ;; Don't proceed if overlay has been removed from buffer.
1244 (when (overlay-buffer overlay)
1245 (if (and (not (eq (with-current-buffer
1246 (widget-field-buffer widget)
1248 ;; `widget-narrow-to-field' can be
1249 ;; active when this function is called
1250 ;; from an change-functions hook. So
1251 ;; temporarily remove field narrowing
1252 ;; before to call `get-char-property'.
1254 (get-char-property (overlay-end overlay)
1257 (or widget-field-add-space
1258 (null (widget-get widget :size))))
1259 (1- (overlay-end overlay))
1260 (overlay-end overlay)))
1263 (defun widget-field-text-end (widget)
1264 (let ((to (widget-field-end widget))
1265 (size (widget-get widget :size)))
1266 (if (or (null size) (zerop size))
1268 (let ((from (widget-field-start widget)))
1270 (with-current-buffer (widget-field-buffer widget)
1271 (while (and (> to from)
1272 (eq (char-after (1- to)) ?\s))
1276 (defun widget-field-find (pos)
1277 "Return the field at POS.
1278 Unlike (get-char-property POS 'field), this works with empty fields too."
1279 (let ((fields widget-field-list)
1282 (setq field (car fields)
1283 fields (cdr fields))
1284 (when (and (<= (widget-field-start field) pos)
1285 (<= pos (widget-field-end field)))
1287 (error "Overlapping fields"))
1288 (setq found field)))
1291 (defun widget-before-change (from to)
1292 ;; This is how, for example, a variable changes its state to `modified'.
1293 ;; when it is being edited.
1294 (unless inhibit-read-only
1295 (let ((from-field (widget-field-find from))
1296 (to-field (widget-field-find to)))
1297 (cond ((not (eq from-field to-field))
1298 (add-hook 'post-command-hook 'widget-add-change nil t)
1299 (signal 'text-read-only
1300 '("Change should be restricted to a single field")))
1302 (add-hook 'post-command-hook 'widget-add-change nil t)
1303 (signal 'text-read-only
1304 '("Attempt to change text outside editable field")))
1305 (widget-field-use-before-change
1306 (widget-apply from-field :notify from-field))))))
1308 (defun widget-add-change ()
1309 (remove-hook 'post-command-hook 'widget-add-change t)
1310 (add-hook 'before-change-functions 'widget-before-change nil t)
1311 (add-hook 'after-change-functions 'widget-after-change nil t))
1313 (defun widget-after-change (from to old)
1314 "Adjust field size and text properties."
1315 (let ((field (widget-field-find from))
1316 (other (widget-field-find to)))
1318 (unless (eq field other)
1319 (error "Change in different fields"))
1320 (let ((size (widget-get field :size)))
1322 (let ((begin (widget-field-start field))
1323 (end (widget-field-end field)))
1324 (cond ((< (- end begin) size)
1328 (insert-char ?\s (- (+ begin size) end))))
1329 ((> (- end begin) size)
1330 ;; Field too large and
1331 (if (or (< (point) (+ begin size))
1333 ;; Point is outside extra space.
1334 (setq begin (+ begin size))
1335 ;; Point is within the extra space.
1336 (setq begin (point)))
1339 (while (and (eq (preceding-char) ?\s)
1341 (delete-char -1)))))))
1342 (widget-specify-secret field))
1343 (widget-apply field :notify field))))
1345 ;;; Widget Functions
1347 ;; These functions are used in the definition of multiple widgets.
1349 (defun widget-parent-action (widget &optional event)
1350 "Tell :parent of WIDGET to handle the :action.
1351 Optional EVENT is the event that triggered the action."
1352 (widget-apply (widget-get widget :parent) :action event))
1354 (defun widget-children-value-delete (widget)
1355 "Delete all :children and :buttons in WIDGET."
1356 (mapc 'widget-delete (widget-get widget :children))
1357 (widget-put widget :children nil)
1358 (mapc 'widget-delete (widget-get widget :buttons))
1359 (widget-put widget :buttons nil))
1361 (defun widget-children-validate (widget)
1362 "All the :children must be valid."
1363 (let ((children (widget-get widget :children))
1365 (while (and children (not found))
1366 (setq child (car children)
1367 children (cdr children)
1368 found (widget-apply child :validate)))
1371 (defun widget-child-value-get (widget)
1372 "Get the value of the first member of :children in WIDGET."
1373 (widget-value (car (widget-get widget :children))))
1375 (defun widget-child-value-inline (widget)
1376 "Get the inline value of the first member of :children in WIDGET."
1377 (widget-apply (car (widget-get widget :children)) :value-inline))
1379 (defun widget-child-validate (widget)
1380 "The result of validating the first member of :children in WIDGET."
1381 (widget-apply (car (widget-get widget :children)) :validate))
1383 (defun widget-type-value-create (widget)
1384 "Convert and instantiate the value of the :type attribute of WIDGET.
1385 Store the newly created widget in the :children attribute.
1387 The value of the :type attribute should be an unconverted widget type."
1388 (let ((value (widget-get widget :value))
1389 (type (widget-get widget :type)))
1390 (widget-put widget :children
1391 (list (widget-create-child-value widget
1392 (widget-convert type)
1395 (defun widget-type-default-get (widget)
1396 "Get default value from the :type attribute of WIDGET.
1398 The value of the :type attribute should be an unconverted widget type."
1399 (widget-default-get (widget-convert (widget-get widget :type))))
1401 (defun widget-type-match (widget value)
1402 "Non-nil if the :type value of WIDGET matches VALUE.
1404 The value of the :type attribute should be an unconverted widget type."
1405 (widget-apply (widget-convert (widget-get widget :type)) :match value))
1407 (defun widget-types-copy (widget)
1408 "Copy :args as widget types in WIDGET."
1409 (widget-put widget :args (mapcar 'widget-copy (widget-get widget :args)))
1412 ;; Made defsubst to speed up face editor creation.
1413 (defsubst widget-types-convert-widget (widget)
1414 "Convert :args as widget types in WIDGET."
1415 (widget-put widget :args (mapcar 'widget-convert (widget-get widget :args)))
1418 (defun widget-value-convert-widget (widget)
1419 "Initialize :value from :args in WIDGET."
1420 (let ((args (widget-get widget :args)))
1422 (widget-put widget :value (car args))
1423 ;; Don't convert :value here, as this is done in `widget-convert'.
1424 ;; (widget-put widget :value (widget-apply widget
1425 ;; :value-to-internal (car args)))
1426 (widget-put widget :args nil)))
1429 (defun widget-value-value-get (widget)
1430 "Return the :value property of WIDGET."
1431 (widget-get widget :value))
1433 ;;; The `default' Widget.
1435 (define-widget 'default nil
1436 "Basic widget other widgets are derived from."
1437 :value-to-internal (lambda (widget value) value)
1438 :value-to-external (lambda (widget value) value)
1439 :button-prefix 'widget-button-prefix
1440 :button-suffix 'widget-button-suffix
1441 :complete 'widget-default-complete
1442 :create 'widget-default-create
1445 :format-handler 'widget-default-format-handler
1446 :button-face-get 'widget-default-button-face-get
1447 :mouse-face-get 'widget-default-mouse-face-get
1448 :sample-face-get 'widget-default-sample-face-get
1449 :delete 'widget-default-delete
1451 :value-set 'widget-default-value-set
1452 :value-inline 'widget-default-value-inline
1453 :value-delete 'ignore
1454 :default-get 'widget-default-default-get
1455 :menu-tag-get 'widget-default-menu-tag-get
1457 :active 'widget-default-active
1458 :activate 'widget-specify-active
1459 :deactivate 'widget-default-deactivate
1460 :mouse-down-action #'ignore
1461 :action 'widget-default-action
1462 :notify 'widget-default-notify
1463 :prompt-value 'widget-default-prompt-value)
1465 (defun widget-default-complete (widget)
1466 "Call the value of the :complete-function property of WIDGET.
1467 If that does not exist, call the value of `widget-complete-field'."
1468 (call-interactively (or (widget-get widget :complete-function)
1469 widget-complete-field)))
1471 (defun widget-default-create (widget)
1472 "Create WIDGET at point in the current buffer."
1473 (widget-specify-insert
1474 (let ((from (point))
1475 button-begin button-end
1476 sample-begin sample-end
1479 (insert (widget-get widget :format))
1481 ;; Parse escapes in format.
1482 (while (re-search-forward "%\\(.\\)" nil t)
1483 (let ((escape (char-after (match-beginning 1))))
1485 (cond ((eq escape ?%)
1488 (setq button-begin (point))
1489 (insert (widget-get-indirect widget :button-prefix)))
1491 (insert (widget-get-indirect widget :button-suffix))
1492 (setq button-end (point)))
1494 (setq sample-begin (point)))
1496 (setq sample-end (point)))
1498 (when (widget-get widget :indent)
1500 (insert-char ?\s (widget-get widget :indent))))
1502 (let ((image (widget-get widget :tag-glyph))
1503 (tag (widget-get widget :tag)))
1505 (widget-image-insert widget (or tag "image") image))
1509 (princ (widget-get widget :value)
1510 (current-buffer))))))
1512 (let ((doc (widget-get widget :doc)))
1514 (setq doc-begin (point))
1516 (while (eq (preceding-char) ?\n)
1519 (setq doc-end (point)))))
1521 (widget-add-documentation-string-button widget))
1523 (if (and button-begin (not button-end))
1524 (widget-apply widget :value-create)
1525 (setq value-pos (point))))
1527 (widget-apply widget :format-handler escape)))))
1528 ;; Specify button, sample, and doc, and insert value.
1529 (and button-begin button-end
1530 (widget-specify-button widget button-begin button-end))
1531 (and sample-begin sample-end
1532 (widget-specify-sample widget sample-begin sample-end))
1533 (and doc-begin doc-end
1534 (widget-specify-doc widget doc-begin doc-end))
1536 (goto-char value-pos)
1537 (widget-apply widget :value-create)))
1538 (let ((from (point-min-marker))
1539 (to (point-max-marker)))
1540 (set-marker-insertion-type from t)
1541 (set-marker-insertion-type to nil)
1542 (widget-put widget :from from)
1543 (widget-put widget :to to)))
1544 (widget-clear-undo))
1546 (defun widget-default-format-handler (widget escape)
1547 (error "Unknown escape `%c'" escape))
1549 (defun widget-default-button-face-get (widget)
1550 ;; Use :button-face or widget-button-face
1551 (or (widget-get widget :button-face)
1552 (let ((parent (widget-get widget :parent)))
1554 (widget-apply parent :button-face-get)
1555 widget-button-face))))
1557 (defun widget-default-mouse-face-get (widget)
1558 ;; Use :mouse-face or widget-mouse-face
1559 (or (widget-get widget :mouse-face)
1560 (let ((parent (widget-get widget :parent)))
1562 (widget-apply parent :mouse-face-get)
1563 widget-mouse-face))))
1565 (defun widget-default-sample-face-get (widget)
1566 ;; Use :sample-face.
1567 (widget-get widget :sample-face))
1569 (defun widget-default-delete (widget)
1570 "Remove widget from the buffer."
1571 (let ((from (widget-get widget :from))
1572 (to (widget-get widget :to))
1573 (inactive-overlay (widget-get widget :inactive))
1574 (button-overlay (widget-get widget :button-overlay))
1575 (sample-overlay (widget-get widget :sample-overlay))
1576 (doc-overlay (widget-get widget :doc-overlay))
1577 (inhibit-modification-hooks t)
1578 (inhibit-read-only t))
1579 (widget-apply widget :value-delete)
1580 (widget-children-value-delete widget)
1581 (when inactive-overlay
1582 (delete-overlay inactive-overlay))
1583 (when button-overlay
1584 (delete-overlay button-overlay))
1585 (when sample-overlay
1586 (delete-overlay sample-overlay))
1588 (delete-overlay doc-overlay))
1590 ;; Kludge: this doesn't need to be true for empty formats.
1591 (delete-region from to))
1592 (set-marker from nil)
1593 (set-marker to nil))
1594 (widget-clear-undo))
1596 (defun widget-default-value-set (widget value)
1597 "Recreate widget with new value."
1598 (let* ((old-pos (point))
1599 (from (copy-marker (widget-get widget :from)))
1600 (to (copy-marker (widget-get widget :to)))
1601 (offset (if (and (<= from old-pos) (<= old-pos to))
1602 (if (>= old-pos (1- to))
1604 (- old-pos from)))))
1605 ;;??? Bug: this ought to insert the new value before deleting the old one,
1606 ;; so that markers on either side of the value automatically
1607 ;; stay on the same side. -- rms.
1609 (goto-char (widget-get widget :from))
1610 (widget-apply widget :delete)
1611 (widget-put widget :value value)
1612 (widget-apply widget :create))
1615 (goto-char (+ (widget-get widget :to) offset 1))
1616 (goto-char (min (+ from offset) (1- (widget-get widget :to))))))))
1618 (defun widget-default-value-inline (widget)
1619 "Wrap value in a list unless it is inline."
1620 (if (widget-get widget :inline)
1621 (widget-value widget)
1622 (list (widget-value widget))))
1624 (defun widget-default-default-get (widget)
1626 (widget-get widget :value))
1628 (defun widget-default-menu-tag-get (widget)
1629 "Use tag or value for menus."
1630 (or (widget-get widget :menu-tag)
1631 (widget-get widget :tag)
1632 (widget-princ-to-string (widget-get widget :value))))
1634 (defun widget-default-active (widget)
1635 "Return t if this widget is active (user modifiable)."
1636 (or (widget-get widget :always-active)
1637 (and (not (widget-get widget :inactive))
1638 (let ((parent (widget-get widget :parent)))
1640 (widget-apply parent :active))))))
1642 (defun widget-default-deactivate (widget)
1643 "Make WIDGET inactive for user modifications."
1644 (widget-specify-inactive widget
1645 (widget-get widget :from)
1646 (widget-get widget :to)))
1648 (defun widget-default-action (widget &optional event)
1649 "Notify the parent when a widget changes."
1650 (let ((parent (widget-get widget :parent)))
1652 (widget-apply parent :notify widget event))))
1654 (defun widget-default-notify (widget child &optional event)
1655 "Pass notification to parent."
1656 (widget-default-action widget event))
1658 (defun widget-default-prompt-value (widget prompt value unbound)
1659 "Read an arbitrary value."
1660 (eval-minibuffer prompt))
1662 (defun widget-docstring (widget)
1663 "Return the documentation string specificied by WIDGET, or nil if none.
1664 If WIDGET has a `:doc' property, that specifies the documentation string.
1665 Otherwise, try the `:documentation-property' property. If this
1666 is a function, call it with the widget's value as an argument; if
1667 it is a symbol, use this symbol together with the widget's value
1668 as the argument to `documentation-property'."
1669 (let ((doc (or (widget-get widget :doc)
1670 (let ((doc-prop (widget-get widget :documentation-property))
1671 (value (widget-get widget :value)))
1672 (cond ((functionp doc-prop)
1673 (funcall doc-prop value))
1675 (documentation-property value doc-prop)))))))
1676 (when (and (stringp doc) (> (length doc) 0))
1677 ;; Remove any redundant `*' in the beginning.
1678 (when (eq (aref doc 0) ?*)
1679 (setq doc (substring doc 1)))
1680 ;; Remove trailing newlines.
1681 (when (string-match "\n+\\'" doc)
1682 (setq doc (substring doc 0 (match-beginning 0))))
1685 ;;; The `item' Widget.
1687 (define-widget 'item 'default
1688 "Constant items for inclusion in other widgets."
1689 :convert-widget 'widget-value-convert-widget
1690 :value-create 'widget-item-value-create
1691 :value-delete 'ignore
1692 :value-get 'widget-value-value-get
1693 :match 'widget-item-match
1694 :match-inline 'widget-item-match-inline
1695 :action 'widget-item-action
1698 (defun widget-item-value-create (widget)
1699 "Insert the printed representation of the value."
1700 (princ (widget-get widget :value) (current-buffer)))
1702 (defun widget-item-match (widget value)
1703 ;; Match if the value is the same.
1704 (equal (widget-get widget :value) value))
1706 (defun widget-item-match-inline (widget values)
1707 ;; Match if the value is the same.
1708 (let ((value (widget-get widget :value)))
1710 (<= (length value) (length values))
1711 (let ((head (widget-sublist values 0 (length value))))
1712 (and (equal head value)
1713 (cons head (widget-sublist values (length value))))))))
1715 (defun widget-sublist (list start &optional end)
1716 "Return the sublist of LIST from START to END.
1717 If END is omitted, it defaults to the length of LIST."
1718 (if (> start 0) (setq list (nthcdr start list)))
1720 (unless (<= end start)
1721 (setq list (copy-sequence list))
1722 (setcdr (nthcdr (- end start 1) list) nil)
1724 (copy-sequence list)))
1726 (defun widget-item-action (widget &optional event)
1727 ;; Just notify itself.
1728 (widget-apply widget :notify widget event))
1730 ;;; The `push-button' Widget.
1732 ;; (defcustom widget-push-button-gui t
1733 ;; "If non-nil, use GUI push buttons when available."
1737 ;; Cache already created GUI objects.
1738 ;; (defvar widget-push-button-cache nil)
1740 (defcustom widget-push-button-prefix "["
1741 "String used as prefix for buttons."
1743 :group 'widget-button)
1745 (defcustom widget-push-button-suffix "]"
1746 "String used as suffix for buttons."
1748 :group 'widget-button)
1750 (define-widget 'push-button 'item
1751 "A pushable button."
1754 :value-create 'widget-push-button-value-create
1757 (defun widget-push-button-value-create (widget)
1758 "Insert text representing the `on' and `off' states."
1759 (let* ((tag (or (widget-get widget :tag)
1760 (widget-get widget :value)))
1761 (tag-glyph (widget-get widget :tag-glyph))
1762 (text (concat widget-push-button-prefix
1763 tag widget-push-button-suffix)))
1765 (widget-image-insert widget text tag-glyph)
1768 ;; (defun widget-gui-action (widget)
1769 ;; "Apply :action for WIDGET."
1770 ;; (widget-apply-action widget (this-command-keys)))
1772 ;;; The `link' Widget.
1774 (defcustom widget-link-prefix "["
1775 "String used as prefix for links."
1777 :group 'widget-button)
1779 (defcustom widget-link-suffix "]"
1780 "String used as suffix for links."
1782 :group 'widget-button)
1784 (define-widget 'link 'item
1786 :button-prefix 'widget-link-prefix
1787 :button-suffix 'widget-link-suffix
1788 :follow-link 'mouse-face
1789 :help-echo "Follow the link."
1792 ;;; The `info-link' Widget.
1794 (define-widget 'info-link 'link
1795 "A link to an info file."
1796 :action 'widget-info-link-action)
1798 (defun widget-info-link-action (widget &optional event)
1799 "Open the info node specified by WIDGET."
1800 (info (widget-value widget)))
1802 ;;; The `url-link' Widget.
1804 (define-widget 'url-link 'link
1805 "A link to an www page."
1806 :action 'widget-url-link-action)
1808 (defun widget-url-link-action (widget &optional event)
1809 "Open the URL specified by WIDGET."
1810 (browse-url (widget-value widget)))
1812 ;;; The `function-link' Widget.
1814 (define-widget 'function-link 'link
1815 "A link to an Emacs function."
1816 :action 'widget-function-link-action)
1818 (defun widget-function-link-action (widget &optional event)
1819 "Show the function specified by WIDGET."
1820 (describe-function (widget-value widget)))
1822 ;;; The `variable-link' Widget.
1824 (define-widget 'variable-link 'link
1825 "A link to an Emacs variable."
1826 :action 'widget-variable-link-action)
1828 (defun widget-variable-link-action (widget &optional event)
1829 "Show the variable specified by WIDGET."
1830 (describe-variable (widget-value widget)))
1832 ;;; The `file-link' Widget.
1834 (define-widget 'file-link 'link
1836 :action 'widget-file-link-action)
1838 (defun widget-file-link-action (widget &optional event)
1839 "Find the file specified by WIDGET."
1840 (find-file (widget-value widget)))
1842 ;;; The `emacs-library-link' Widget.
1844 (define-widget 'emacs-library-link 'link
1845 "A link to an Emacs Lisp library file."
1846 :action 'widget-emacs-library-link-action)
1848 (defun widget-emacs-library-link-action (widget &optional event)
1849 "Find the Emacs library file specified by WIDGET."
1850 (find-file (locate-library (widget-value widget))))
1852 ;;; The `emacs-commentary-link' Widget.
1854 (define-widget 'emacs-commentary-link 'link
1855 "A link to Commentary in an Emacs Lisp library file."
1856 :action 'widget-emacs-commentary-link-action)
1858 (defun widget-emacs-commentary-link-action (widget &optional event)
1859 "Find the Commentary section of the Emacs file specified by WIDGET."
1860 (finder-commentary (widget-value widget)))
1862 ;;; The `editable-field' Widget.
1864 (define-widget 'editable-field 'default
1865 "An editable text field.
1866 Note: In an `editable-field' widget, the `%v' escape must be preceded
1867 by some other text in the `:format' string (if specified)."
1868 :convert-widget 'widget-value-convert-widget
1869 :keymap widget-field-keymap
1871 :help-echo "M-TAB: complete field; RET: enter value"
1873 :prompt-internal 'widget-field-prompt-internal
1874 :prompt-history 'widget-field-history
1875 :prompt-value 'widget-field-prompt-value
1876 :action 'widget-field-action
1877 :validate 'widget-field-validate
1879 :error "Field's value doesn't match allowed forms"
1880 :value-create 'widget-field-value-create
1881 :value-set 'widget-field-value-set
1882 :value-delete 'widget-field-value-delete
1883 :value-get 'widget-field-value-get
1884 :match 'widget-field-match)
1886 (defvar widget-field-history nil
1887 "History of field minibuffer edits.")
1889 (defun widget-field-prompt-internal (widget prompt initial history)
1890 "Read string for WIDGET prompting with PROMPT.
1891 INITIAL is the initial input and HISTORY is a symbol containing
1893 (read-string prompt initial history))
1895 (defun widget-field-prompt-value (widget prompt value unbound)
1896 "Prompt for a string."
1897 (widget-apply widget
1899 (widget-apply widget
1900 :prompt-internal prompt
1902 (cons (widget-apply widget
1903 :value-to-internal value)
1905 (widget-get widget :prompt-history))))
1907 (defvar widget-edit-functions nil)
1909 (defun widget-field-action (widget &optional event)
1910 "Move to next field."
1912 (run-hook-with-args 'widget-edit-functions widget))
1914 (defun widget-field-validate (widget)
1915 "Valid if the content matches `:valid-regexp'."
1916 (unless (string-match (widget-get widget :valid-regexp)
1917 (widget-apply widget :value-get))
1920 (defun widget-field-value-set (widget value)
1921 "Set an editable text field WIDGET to VALUE"
1922 (let ((from (widget-field-start widget))
1923 (to (widget-field-text-end widget))
1924 (buffer (widget-field-buffer widget))
1925 (size (widget-get widget :size)))
1926 (when (and from to (buffer-live-p buffer))
1927 (with-current-buffer buffer
1929 (delete-char (- to from))
1932 (defun widget-field-value-create (widget)
1933 "Create an editable text field."
1934 (let ((size (widget-get widget :size))
1935 (value (widget-get widget :value))
1937 ;; This is changed to a real overlay in `widget-setup'. We
1938 ;; need the end points to behave differently until
1939 ;; `widget-setup' is called.
1940 (overlay (cons (make-marker) (make-marker))))
1941 (widget-put widget :field-overlay overlay)
1944 (< (length value) size)
1945 (insert-char ?\s (- size (length value))))
1946 (unless (memq widget widget-field-list)
1947 (setq widget-field-new (cons widget widget-field-new)))
1948 (move-marker (cdr overlay) (point))
1949 (set-marker-insertion-type (cdr overlay) nil)
1952 (move-marker (car overlay) from)
1953 (set-marker-insertion-type (car overlay) t)))
1955 (defun widget-field-value-delete (widget)
1956 "Remove the widget from the list of active editing fields."
1957 (setq widget-field-list (delq widget widget-field-list))
1958 (setq widget-field-new (delq widget widget-field-new))
1959 ;; These are nil if the :format string doesn't contain `%v'.
1960 (let ((overlay (widget-get widget :field-overlay)))
1961 (when (overlayp overlay)
1962 (delete-overlay overlay))))
1964 (defun widget-field-value-get (widget)
1965 "Return current text in editing field."
1966 (let ((from (widget-field-start widget))
1967 (to (widget-field-text-end widget))
1968 (buffer (widget-field-buffer widget))
1969 (secret (widget-get widget :secret))
1970 (old (current-buffer)))
1974 (let ((result (buffer-substring-no-properties from to)))
1977 (while (< (+ from index) to)
1979 (get-char-property (+ from index) 'secret))
1980 (setq index (1+ index)))))
1983 (widget-get widget :value))))
1985 (defun widget-field-match (widget value)
1986 ;; Match any string.
1989 ;;; The `text' Widget.
1991 (define-widget 'text 'editable-field
1992 "A multiline text area."
1993 :keymap widget-text-keymap)
1995 ;;; The `menu-choice' Widget.
1997 (define-widget 'menu-choice 'default
1998 "A menu of options."
1999 :convert-widget 'widget-types-convert-widget
2000 :copy 'widget-types-copy
2001 :format "%[%t%]: %v"
2004 :void '(item :format "invalid (%t)\n")
2005 :value-create 'widget-choice-value-create
2006 :value-get 'widget-child-value-get
2007 :value-inline 'widget-child-value-inline
2008 :default-get 'widget-choice-default-get
2009 :mouse-down-action 'widget-choice-mouse-down-action
2010 :action 'widget-choice-action
2011 :error "Make a choice"
2012 :validate 'widget-choice-validate
2013 :match 'widget-choice-match
2014 :match-inline 'widget-choice-match-inline)
2016 (defun widget-choice-value-create (widget)
2017 "Insert the first choice that matches the value."
2018 (let ((value (widget-get widget :value))
2019 (args (widget-get widget :args))
2020 (explicit (widget-get widget :explicit-choice))
2024 ;; If the user specified the choice for this value,
2025 ;; respect that choice.
2026 (widget-put widget :children (list (widget-create-child-value
2027 widget explicit value)))
2028 (widget-put widget :choice explicit)
2029 (widget-put widget :explicit-choice nil))
2031 (setq current (car args)
2033 (when (widget-apply current :match value)
2034 (widget-put widget :children (list (widget-create-child-value
2035 widget current value)))
2036 (widget-put widget :choice current)
2040 (let ((void (widget-get widget :void)))
2041 (widget-put widget :children (list (widget-create-child-and-convert
2042 widget void :value value)))
2043 (widget-put widget :choice void))))))
2045 (defun widget-choice-default-get (widget)
2046 ;; Get default for the first choice.
2047 (widget-default-get (car (widget-get widget :args))))
2049 (defcustom widget-choice-toggle nil
2050 "If non-nil, a binary choice will just toggle between the values.
2051 Otherwise, the user will explicitly have to choose between the values
2052 when he invoked the menu."
2056 (defun widget-choice-mouse-down-action (widget &optional event)
2057 ;; Return non-nil if we need a menu.
2058 (let ((args (widget-get widget :args))
2059 (old (widget-get widget :choice)))
2060 (cond ((not (display-popup-menus-p))
2061 ;; No place to pop up a menu.
2063 ((< (length args) 2)
2064 ;; Empty or singleton list, just return the value.
2066 ((> (length args) widget-menu-max-size)
2067 ;; Too long, prompt.
2069 ((> (length args) 2)
2070 ;; Reasonable sized list, use menu.
2072 ((and widget-choice-toggle (memq old args))
2076 ;; Ask which of the two.
2079 (defun widget-choice-action (widget &optional event)
2081 (let ((args (widget-get widget :args))
2082 (old (widget-get widget :choice))
2083 (tag (widget-apply widget :menu-tag-get))
2084 (completion-ignore-case (widget-get widget :case-fold))
2087 ;; Remember old value.
2088 (if (and old (not (widget-apply widget :validate)))
2089 (let* ((external (widget-value widget))
2090 (internal (widget-apply old :value-to-internal external)))
2091 (widget-put old :value internal)))
2094 (cond ((= (length args) 0)
2096 ((= (length args) 1)
2098 ((and widget-choice-toggle
2101 (if (eq old (nth 0 args))
2106 (setq current (car args)
2109 (cons (cons (widget-apply current :menu-tag-get)
2112 (setq this-explicit t)
2113 (widget-choose tag (reverse choices) event))))
2115 ;; If this was an explicit user choice, record the choice,
2116 ;; so that widget-choice-value-create will respect it.
2118 (widget-put widget :explicit-choice current))
2119 (widget-value-set widget (widget-default-get current))
2121 (widget-apply widget :notify widget event)))
2122 (run-hook-with-args 'widget-edit-functions widget))
2124 (defun widget-choice-validate (widget)
2125 ;; Valid if we have made a valid choice.
2126 (if (eq (widget-get widget :void) (widget-get widget :choice))
2128 (widget-apply (car (widget-get widget :children)) :validate)))
2130 (defun widget-choice-match (widget value)
2131 ;; Matches if one of the choices matches.
2132 (let ((args (widget-get widget :args))
2134 (while (and args (not found))
2135 (setq current (car args)
2137 found (widget-apply current :match value)))
2140 (defun widget-choice-match-inline (widget values)
2141 ;; Matches if one of the choices matches.
2142 (let ((args (widget-get widget :args))
2144 (while (and args (null found))
2145 (setq current (car args)
2147 found (widget-match-inline current values)))
2150 ;;; The `toggle' Widget.
2152 (define-widget 'toggle 'item
2153 "Toggle between two states."
2155 :value-create 'widget-toggle-value-create
2156 :action 'widget-toggle-action
2157 :match (lambda (widget value) t)
2161 (defun widget-toggle-value-create (widget)
2162 "Insert text representing the `on' and `off' states."
2163 (if (widget-value widget)
2164 (let ((image (widget-get widget :on-glyph)))
2165 (and (display-graphic-p)
2167 (not (eq (car image) 'image))
2168 (widget-put widget :on-glyph (setq image (eval image))))
2169 (widget-image-insert widget
2170 (widget-get widget :on)
2172 (let ((image (widget-get widget :off-glyph)))
2173 (and (display-graphic-p)
2175 (not (eq (car image) 'image))
2176 (widget-put widget :off-glyph (setq image (eval image))))
2177 (widget-image-insert widget (widget-get widget :off) image))))
2179 (defun widget-toggle-action (widget &optional event)
2181 (widget-value-set widget (not (widget-value widget)))
2182 (widget-apply widget :notify widget event)
2183 (run-hook-with-args 'widget-edit-functions widget))
2185 ;;; The `checkbox' Widget.
2187 (define-widget 'checkbox 'toggle
2188 "A checkbox toggle."
2193 ;; We could probably do the same job as the images using single
2194 ;; space characters in a boxed face with a stretch specification to
2195 ;; make them square.
2196 :on-glyph '(create-image "\300\300\141\143\067\076\034\030"
2197 'xbm t :width 8 :height 8
2198 :background "grey75" ; like default mode line
2203 :off-glyph '(create-image (make-string 8 0)
2204 'xbm t :width 8 :height 8
2205 :background "grey75"
2209 :help-echo "Toggle this item."
2210 :action 'widget-checkbox-action)
2212 (defun widget-checkbox-action (widget &optional event)
2213 "Toggle checkbox, notify parent, and set active state of sibling."
2214 (widget-toggle-action widget event)
2215 (let ((sibling (widget-get-sibling widget)))
2217 (if (widget-value widget)
2218 (widget-apply sibling :activate)
2219 (widget-apply sibling :deactivate))
2220 (widget-clear-undo))))
2222 ;;; The `checklist' Widget.
2224 (define-widget 'checklist 'default
2225 "A multiple choice widget."
2226 :convert-widget 'widget-types-convert-widget
2227 :copy 'widget-types-copy
2230 :entry-format "%b %v"
2232 :value-create 'widget-checklist-value-create
2233 :value-get 'widget-checklist-value-get
2234 :validate 'widget-checklist-validate
2235 :match 'widget-checklist-match
2236 :match-inline 'widget-checklist-match-inline)
2238 (defun widget-checklist-value-create (widget)
2239 ;; Insert all values
2240 (let ((alist (widget-checklist-match-find widget (widget-get widget :value)))
2241 (args (widget-get widget :args)))
2243 (widget-checklist-add-item widget (car args) (assq (car args) alist))
2244 (setq args (cdr args)))
2245 (widget-put widget :children (nreverse (widget-get widget :children)))))
2247 (defun widget-checklist-add-item (widget type chosen)
2248 "Create checklist item in WIDGET of type TYPE.
2249 If the item is checked, CHOSEN is a cons whose cdr is the value."
2250 (and (eq (preceding-char) ?\n)
2251 (widget-get widget :indent)
2252 (insert-char ?\s (widget-get widget :indent)))
2253 (widget-specify-insert
2254 (let* ((children (widget-get widget :children))
2255 (buttons (widget-get widget :buttons))
2256 (button-args (or (widget-get type :sibling-args)
2257 (widget-get widget :button-args)))
2260 (insert (widget-get widget :entry-format))
2262 ;; Parse % escapes in format.
2263 (while (re-search-forward "%\\([bv%]\\)" nil t)
2264 (let ((escape (char-after (match-beginning 1))))
2266 (cond ((eq escape ?%)
2269 (setq button (apply 'widget-create-child-and-convert
2271 :value (not (null chosen))
2276 (let ((child (widget-create-child widget type)))
2277 (widget-apply child :deactivate)
2279 ((widget-get type :inline)
2280 (widget-create-child-value
2281 widget type (cdr chosen)))
2283 (widget-create-child-value
2284 widget type (car (cdr chosen)))))))
2286 (error "Unknown escape `%c'" escape)))))
2287 ;; Update properties.
2288 (and button child (widget-put child :button button))
2289 (and button (widget-put widget :buttons (cons button buttons)))
2290 (and child (widget-put widget :children (cons child children))))))
2292 (defun widget-checklist-match (widget values)
2293 ;; All values must match a type in the checklist.
2295 (null (cdr (widget-checklist-match-inline widget values)))))
2297 (defun widget-checklist-match-inline (widget values)
2298 ;; Find the values which match a type in the checklist.
2299 (let ((greedy (widget-get widget :greedy))
2300 (args (copy-sequence (widget-get widget :args)))
2303 (let ((answer (widget-checklist-match-up args values)))
2305 (let ((vals (widget-match-inline answer values)))
2306 (setq found (append found (car vals))
2308 args (delq answer args))))
2310 (setq rest (append rest (list (car values)))
2311 values (cdr values)))
2313 (setq rest (append rest values)
2317 (defun widget-checklist-match-find (widget vals)
2318 "Find the vals which match a type in the checklist.
2319 Return an alist of (TYPE MATCH)."
2320 (let ((greedy (widget-get widget :greedy))
2321 (args (copy-sequence (widget-get widget :args)))
2324 (let ((answer (widget-checklist-match-up args vals)))
2326 (let ((match (widget-match-inline answer vals)))
2327 (setq found (cons (cons answer (car match)) found)
2329 args (delq answer args))))
2331 (setq vals (cdr vals)))
2336 (defun widget-checklist-match-up (args vals)
2337 "Return the first type from ARGS that matches VALS."
2338 (let (current found)
2339 (while (and args (null found))
2340 (setq current (car args)
2342 found (widget-match-inline current vals)))
2346 (defun widget-checklist-value-get (widget)
2347 ;; The values of all selected items.
2348 (let ((children (widget-get widget :children))
2351 (setq child (car children)
2352 children (cdr children))
2353 (if (widget-value (widget-get child :button))
2354 (setq result (append result (widget-apply child :value-inline)))))
2357 (defun widget-checklist-validate (widget)
2358 ;; Ticked chilren must be valid.
2359 (let ((children (widget-get widget :children))
2361 (while (and children (not found))
2362 (setq child (car children)
2363 children (cdr children)
2364 button (widget-get child :button)
2365 found (and (widget-value button)
2366 (widget-apply child :validate))))
2369 ;;; The `option' Widget
2371 (define-widget 'option 'checklist
2372 "An widget with an optional item."
2375 ;;; The `choice-item' Widget.
2377 (define-widget 'choice-item 'item
2378 "Button items that delegate action events to their parents."
2379 :action 'widget-parent-action
2380 :format "%[%t%] \n")
2382 ;;; The `radio-button' Widget.
2384 (define-widget 'radio-button 'toggle
2385 "A radio button for use in the `radio' widget."
2386 :notify 'widget-radio-button-notify
2393 :off-glyph "radio0")
2395 (defun widget-radio-button-notify (widget child &optional event)
2397 (widget-apply (widget-get widget :parent) :action widget event))
2399 ;;; The `radio-button-choice' Widget.
2401 (define-widget 'radio-button-choice 'default
2402 "Select one of multiple options."
2403 :convert-widget 'widget-types-convert-widget
2404 :copy 'widget-types-copy
2407 :entry-format "%b %v"
2408 :value-create 'widget-radio-value-create
2409 :value-get 'widget-radio-value-get
2410 :value-inline 'widget-radio-value-inline
2411 :value-set 'widget-radio-value-set
2412 :error "You must push one of the buttons"
2413 :validate 'widget-radio-validate
2414 :match 'widget-choice-match
2415 :match-inline 'widget-choice-match-inline
2416 :action 'widget-radio-action)
2418 (defun widget-radio-value-create (widget)
2419 ;; Insert all values
2420 (let ((args (widget-get widget :args))
2423 (setq arg (car args)
2425 (widget-radio-add-item widget arg))))
2427 (defun widget-radio-add-item (widget type)
2428 "Add to radio widget WIDGET a new radio button item of type TYPE."
2429 ;; (setq type (widget-convert type))
2430 (and (eq (preceding-char) ?\n)
2431 (widget-get widget :indent)
2432 (insert-char ?\s (widget-get widget :indent)))
2433 (widget-specify-insert
2434 (let* ((value (widget-get widget :value))
2435 (children (widget-get widget :children))
2436 (buttons (widget-get widget :buttons))
2437 (button-args (or (widget-get type :sibling-args)
2438 (widget-get widget :button-args)))
2440 (chosen (and (null (widget-get widget :choice))
2441 (widget-apply type :match value)))
2443 (insert (widget-get widget :entry-format))
2445 ;; Parse % escapes in format.
2446 (while (re-search-forward "%\\([bv%]\\)" nil t)
2447 (let ((escape (char-after (match-beginning 1))))
2449 (cond ((eq escape ?%)
2452 (setq button (apply 'widget-create-child-and-convert
2453 widget 'radio-button
2454 :value (not (null chosen))
2457 (setq child (if chosen
2458 (widget-create-child-value
2460 (widget-create-child widget type)))
2462 (widget-apply child :deactivate)))
2464 (error "Unknown escape `%c'" escape)))))
2465 ;; Update properties.
2467 (widget-put widget :choice type))
2469 (widget-put child :button button)
2470 (widget-put widget :buttons (nconc buttons (list button))))
2472 (widget-put widget :children (nconc children (list child))))
2475 (defun widget-radio-value-get (widget)
2476 ;; Get value of the child widget.
2477 (let ((chosen (widget-radio-chosen widget)))
2478 (and chosen (widget-value chosen))))
2480 (defun widget-radio-chosen (widget)
2481 "Return the widget representing the chosen radio button."
2482 (let ((children (widget-get widget :children))
2485 (setq current (car children)
2486 children (cdr children))
2487 (when (widget-apply (widget-get current :button) :value-get)
2492 (defun widget-radio-value-inline (widget)
2493 ;; Get value of the child widget.
2494 (let ((children (widget-get widget :children))
2497 (setq current (car children)
2498 children (cdr children))
2499 (when (widget-apply (widget-get current :button) :value-get)
2500 (setq found (widget-apply current :value-inline)
2504 (defun widget-radio-value-set (widget value)
2505 ;; We can't just delete and recreate a radio widget, since children
2506 ;; can be added after the original creation and won't be recreated
2508 (let ((children (widget-get widget :children))
2511 (setq current (car children)
2512 children (cdr children))
2513 (let* ((button (widget-get current :button))
2514 (match (and (not found)
2515 (widget-apply current :match value))))
2516 (widget-value-set button match)
2519 (widget-value-set current value)
2520 (widget-apply current :activate))
2521 (widget-apply current :deactivate))
2522 (setq found (or found match))))))
2524 (defun widget-radio-validate (widget)
2525 ;; Valid if we have made a valid choice.
2526 (let ((children (widget-get widget :children))
2527 current found button)
2528 (while (and children (not found))
2529 (setq current (car children)
2530 children (cdr children)
2531 button (widget-get current :button)
2532 found (widget-apply button :value-get)))
2534 (widget-apply current :validate)
2537 (defun widget-radio-action (widget child event)
2538 ;; Check if a radio button was pressed.
2539 (let ((children (widget-get widget :children))
2540 (buttons (widget-get widget :buttons))
2542 (when (memq child buttons)
2544 (setq current (car children)
2545 children (cdr children))
2546 (let* ((button (widget-get current :button)))
2547 (cond ((eq child button)
2548 (widget-value-set button t)
2549 (widget-apply current :activate))
2550 ((widget-value button)
2551 (widget-value-set button nil)
2552 (widget-apply current :deactivate)))))))
2553 ;; Pass notification to parent.
2554 (widget-apply widget :notify child event))
2556 ;;; The `insert-button' Widget.
2558 (define-widget 'insert-button 'push-button
2559 "An insert button for the `editable-list' widget."
2561 :help-echo "Insert a new item into the list at this position."
2562 :action 'widget-insert-button-action)
2564 (defun widget-insert-button-action (widget &optional event)
2565 ;; Ask the parent to insert a new item.
2566 (widget-apply (widget-get widget :parent)
2567 :insert-before (widget-get widget :widget)))
2569 ;;; The `delete-button' Widget.
2571 (define-widget 'delete-button 'push-button
2572 "A delete button for the `editable-list' widget."
2574 :help-echo "Delete this item from the list."
2575 :action 'widget-delete-button-action)
2577 (defun widget-delete-button-action (widget &optional event)
2578 ;; Ask the parent to insert a new item.
2579 (widget-apply (widget-get widget :parent)
2580 :delete-at (widget-get widget :widget)))
2582 ;;; The `editable-list' Widget.
2584 ;; (defcustom widget-editable-list-gui nil
2585 ;; "If non-nil, use GUI push-buttons in editable list when available."
2589 (define-widget 'editable-list 'default
2590 "A variable list of widgets of the same type."
2591 :convert-widget 'widget-types-convert-widget
2592 :copy 'widget-types-copy
2595 :format-handler 'widget-editable-list-format-handler
2596 :entry-format "%i %d %v"
2597 :value-create 'widget-editable-list-value-create
2598 :value-get 'widget-editable-list-value-get
2599 :validate 'widget-children-validate
2600 :match 'widget-editable-list-match
2601 :match-inline 'widget-editable-list-match-inline
2602 :insert-before 'widget-editable-list-insert-before
2603 :delete-at 'widget-editable-list-delete-at)
2605 (defun widget-editable-list-format-handler (widget escape)
2606 ;; We recognize the insert button.
2607 ;; (let ((widget-push-button-gui widget-editable-list-gui))
2608 (cond ((eq escape ?i)
2609 (and (widget-get widget :indent)
2610 (insert-char ?\s (widget-get widget :indent)))
2611 (apply 'widget-create-child-and-convert
2612 widget 'insert-button
2613 (widget-get widget :append-button-args)))
2615 (widget-default-format-handler widget escape)))
2619 (defun widget-editable-list-value-create (widget)
2620 ;; Insert all values
2621 (let* ((value (widget-get widget :value))
2622 (type (nth 0 (widget-get widget :args)))
2624 (widget-put widget :value-pos (copy-marker (point)))
2625 (set-marker-insertion-type (widget-get widget :value-pos) t)
2627 (let ((answer (widget-match-inline type value)))
2629 (setq children (cons (widget-editable-list-entry-create
2631 (if (widget-get type :inline)
2638 (widget-put widget :children (nreverse children))))
2640 (defun widget-editable-list-value-get (widget)
2641 ;; Get value of the child widget.
2642 (apply 'append (mapcar (lambda (child) (widget-apply child :value-inline))
2643 (widget-get widget :children))))
2645 (defun widget-editable-list-match (widget value)
2646 ;; Value must be a list and all the members must match the type.
2648 (null (cdr (widget-editable-list-match-inline widget value)))))
2650 (defun widget-editable-list-match-inline (widget value)
2651 (let ((type (nth 0 (widget-get widget :args)))
2654 (while (and value ok)
2655 (let ((answer (widget-match-inline type value)))
2657 (setq found (append found (car answer))
2660 (cons found value)))
2662 (defun widget-editable-list-insert-before (widget before)
2663 ;; Insert a new child in the list of children.
2665 (let ((children (widget-get widget :children))
2666 (inhibit-read-only t)
2667 before-change-functions
2668 after-change-functions)
2670 (goto-char (widget-get before :entry-from)))
2672 (goto-char (widget-get widget :value-pos))))
2673 (let ((child (widget-editable-list-entry-create
2675 (when (< (widget-get child :entry-from) (widget-get widget :from))
2676 (set-marker (widget-get widget :from)
2677 (widget-get child :entry-from)))
2678 (if (eq (car children) before)
2679 (widget-put widget :children (cons child children))
2680 (while (not (eq (car (cdr children)) before))
2681 (setq children (cdr children)))
2682 (setcdr children (cons child (cdr children)))))))
2684 (widget-apply widget :notify widget))
2686 (defun widget-editable-list-delete-at (widget child)
2687 ;; Delete child from list of children.
2689 (let ((buttons (copy-sequence (widget-get widget :buttons)))
2691 (inhibit-read-only t)
2692 before-change-functions
2693 after-change-functions)
2695 (setq button (car buttons)
2696 buttons (cdr buttons))
2697 (when (eq (widget-get button :widget) child)
2699 :buttons (delq button (widget-get widget :buttons)))
2700 (widget-delete button))))
2701 (let ((entry-from (widget-get child :entry-from))
2702 (entry-to (widget-get child :entry-to))
2703 (inhibit-read-only t)
2704 before-change-functions
2705 after-change-functions)
2706 (widget-delete child)
2707 (delete-region entry-from entry-to)
2708 (set-marker entry-from nil)
2709 (set-marker entry-to nil))
2710 (widget-put widget :children (delq child (widget-get widget :children))))
2712 (widget-apply widget :notify widget))
2714 (defun widget-editable-list-entry-create (widget value conv)
2715 ;; Create a new entry to the list.
2716 (let ((type (nth 0 (widget-get widget :args)))
2717 ;; (widget-push-button-gui widget-editable-list-gui)
2718 child delete insert)
2719 (widget-specify-insert
2721 (and (widget-get widget :indent)
2722 (insert-char ?\s (widget-get widget :indent)))
2723 (insert (widget-get widget :entry-format)))
2724 ;; Parse % escapes in format.
2725 (while (re-search-forward "%\\(.\\)" nil t)
2726 (let ((escape (char-after (match-beginning 1))))
2728 (cond ((eq escape ?%)
2731 (setq insert (apply 'widget-create-child-and-convert
2732 widget 'insert-button
2733 (widget-get widget :insert-button-args))))
2735 (setq delete (apply 'widget-create-child-and-convert
2736 widget 'delete-button
2737 (widget-get widget :delete-button-args))))
2740 (setq child (widget-create-child-value
2742 (setq child (widget-create-child-value
2743 widget type (widget-default-get type)))))
2745 (error "Unknown escape `%c'" escape)))))
2746 (let ((buttons (widget-get widget :buttons)))
2747 (if insert (push insert buttons))
2748 (if delete (push delete buttons))
2749 (widget-put widget :buttons buttons))
2750 (let ((entry-from (point-min-marker))
2751 (entry-to (point-max-marker)))
2752 (set-marker-insertion-type entry-from t)
2753 (set-marker-insertion-type entry-to nil)
2754 (widget-put child :entry-from entry-from)
2755 (widget-put child :entry-to entry-to)))
2756 (if insert (widget-put insert :widget child))
2757 (if delete (widget-put delete :widget child))
2760 ;;; The `group' Widget.
2762 (define-widget 'group 'default
2763 "A widget which groups other widgets inside."
2764 :convert-widget 'widget-types-convert-widget
2765 :copy 'widget-types-copy
2767 :value-create 'widget-group-value-create
2768 :value-get 'widget-editable-list-value-get
2769 :default-get 'widget-group-default-get
2770 :validate 'widget-children-validate
2771 :match 'widget-group-match
2772 :match-inline 'widget-group-match-inline)
2774 (defun widget-group-value-create (widget)
2775 ;; Create each component.
2776 (let ((args (widget-get widget :args))
2777 (value (widget-get widget :value))
2778 arg answer children)
2780 (setq arg (car args)
2782 answer (widget-match-inline arg value)
2784 (and (eq (preceding-char) ?\n)
2785 (widget-get widget :indent)
2786 (insert-char ?\s (widget-get widget :indent)))
2787 (push (cond ((null answer)
2788 (widget-create-child widget arg))
2789 ((widget-get arg :inline)
2790 (widget-create-child-value widget arg (car answer)))
2792 (widget-create-child-value widget arg (car (car answer)))))
2794 (widget-put widget :children (nreverse children))))
2796 (defun widget-group-default-get (widget)
2797 ;; Get the default of the components.
2798 (mapcar 'widget-default-get (widget-get widget :args)))
2800 (defun widget-group-match (widget values)
2801 ;; Match if the components match.
2803 (let ((match (widget-group-match-inline widget values)))
2804 (and match (null (cdr match))))))
2806 (defun widget-group-match-inline (widget vals)
2807 ;; Match if the components match.
2808 (let ((args (widget-get widget :args))
2809 argument answer found)
2811 (setq argument (car args)
2813 answer (widget-match-inline argument vals))
2815 (setq vals (cdr answer)
2816 found (append found (car answer)))
2820 (cons found vals))))
2822 ;;; The `visibility' Widget.
2824 (define-widget 'visibility 'item
2825 "An indicator and manipulator for hidden items.
2827 The following properties have special meanings for this widget:
2828 :on-image Image filename or spec to display when the item is visible.
2829 :on Text shown if the \"on\" image is nil or cannot be displayed.
2830 :off-image Image filename or spec to display when the item is hidden.
2831 :off Text shown if the \"off\" image is nil cannot be displayed."
2839 :value-create 'widget-visibility-value-create
2840 :action 'widget-toggle-action
2841 :match (lambda (widget value) t))
2843 (defun widget-visibility-value-create (widget)
2844 ;; Insert text representing the `on' and `off' states.
2845 (let* ((val (widget-value widget))
2846 (text (widget-get widget (if val :on :off)))
2847 (img (widget-image-find
2848 (widget-get widget (if val :on-image :off-image)))))
2849 (widget-image-insert widget
2851 (concat widget-push-button-prefix text
2852 widget-push-button-suffix)
2855 (append img '(:ascent center))))))
2857 ;;; The `documentation-link' Widget.
2859 ;; This is a helper widget for `documentation-string'.
2861 (define-widget 'documentation-link 'link
2862 "Link type used in documentation strings."
2864 :help-echo "Describe this symbol"
2865 :action 'widget-documentation-link-action)
2867 (defun widget-documentation-link-action (widget &optional event)
2868 "Display documentation for WIDGET's value. Ignore optional argument EVENT."
2869 (let* ((string (widget-get widget :value))
2870 (symbol (intern string)))
2871 (if (and (fboundp symbol) (boundp symbol))
2872 ;; If there are two doc strings, give the user a way to pick one.
2873 (apropos (concat "\\`" (regexp-quote string) "\\'"))
2874 (if (fboundp symbol)
2875 (describe-function symbol)
2876 (describe-variable symbol)))))
2878 (defcustom widget-documentation-links t
2879 "Add hyperlinks to documentation strings when non-nil."
2881 :group 'widget-documentation)
2883 (defcustom widget-documentation-link-regexp "`\\([^\n`' ]+\\)'"
2884 "Regexp for matching potential links in documentation strings.
2885 The first group should be the link itself."
2887 :group 'widget-documentation)
2889 (defcustom widget-documentation-link-p 'intern-soft
2890 "Predicate used to test if a string is useful as a link.
2891 The value should be a function. The function will be called with one
2892 argument, a string, and should return non-nil if there should be a
2893 link for that string."
2895 :options '(widget-documentation-link-p)
2896 :group 'widget-documentation)
2898 (defcustom widget-documentation-link-type 'documentation-link
2899 "Widget type used for links in documentation strings."
2901 :group 'widget-documentation)
2903 (defun widget-documentation-link-add (widget from to)
2904 (widget-specify-doc widget from to)
2905 (when widget-documentation-links
2906 (let ((regexp widget-documentation-link-regexp)
2907 (buttons (widget-get widget :buttons))
2908 (widget-mouse-face (default-value 'widget-mouse-face))
2909 (widget-button-face widget-documentation-face)
2910 (widget-button-pressed-face widget-documentation-face))
2913 (while (re-search-forward regexp to t)
2914 (let ((name (match-string 1))
2915 (begin (match-beginning 1))
2916 (end (match-end 1)))
2917 (when (funcall widget-documentation-link-p name)
2918 (push (widget-convert-button widget-documentation-link-type
2919 begin end :value name)
2921 (widget-put widget :buttons buttons)))
2922 (let ((indent (widget-get widget :indent)))
2923 (when (and indent (not (zerop indent)))
2926 (narrow-to-region from to)
2927 (goto-char (point-min))
2928 (while (search-forward "\n" nil t)
2929 (insert-char ?\s indent)))))))
2931 ;;; The `documentation-string' Widget.
2933 (define-widget 'documentation-string 'item
2934 "A documentation string."
2936 :action 'widget-documentation-string-action
2937 :value-create 'widget-documentation-string-value-create
2938 :visibility-widget 'visibility)
2940 (defun widget-documentation-string-value-create (widget)
2941 ;; Insert documentation string.
2942 (let ((doc (widget-value widget))
2943 (indent (widget-get widget :indent))
2944 (shown (widget-get (widget-get widget :parent) :documentation-shown))
2946 (if (string-match "\n" doc)
2947 (let ((before (substring doc 0 (match-beginning 0)))
2948 (after (substring doc (match-beginning 0)))
2950 (when (and indent (not (zerop indent)))
2951 (insert-char ?\s indent))
2953 (widget-documentation-link-add widget start (point))
2955 (widget-create-child-and-convert
2956 widget (widget-get widget :visibility-widget)
2957 :help-echo "Show or hide rest of the documentation."
2961 :action 'widget-parent-action
2964 (setq start (point))
2965 (when (and indent (not (zerop indent)))
2966 (insert-char ?\s indent))
2968 (widget-documentation-link-add widget start (point)))
2969 (widget-put widget :buttons (list button)))
2970 (when (and indent (not (zerop indent)))
2971 (insert-char ?\s indent))
2973 (widget-documentation-link-add widget start (point))))
2976 (defun widget-documentation-string-action (widget &rest ignore)
2977 ;; Toggle documentation.
2978 (let ((parent (widget-get widget :parent)))
2979 (widget-put parent :documentation-shown
2980 (not (widget-get parent :documentation-shown))))
2982 (widget-value-set widget (widget-value widget)))
2984 (defun widget-add-documentation-string-button (widget &rest args)
2985 "Insert a new `documentation-string' widget based on WIDGET.
2986 The new widget becomes a child of WIDGET, and is also added to
2987 its `:buttons' list. The documentation string is found from
2988 WIDGET using the function `widget-docstring'.
2989 Optional ARGS specifies additional keyword arguments for the
2990 `documentation-string' widget."
2991 (let ((doc (widget-docstring widget))
2992 (indent (widget-get widget :indent))
2993 (doc-indent (widget-get widget :documentation-indent)))
2995 (and (eq (preceding-char) ?\n)
2997 (insert-char ?\s indent))
2998 (unless (or (numberp doc-indent) (null doc-indent))
2999 (setq doc-indent 0))
3000 (widget-put widget :buttons
3001 (cons (apply 'widget-create-child-and-convert
3002 widget 'documentation-string
3004 (nconc args (list doc)))
3005 (widget-get widget :buttons))))))
3007 ;;; The Sexp Widgets.
3009 (define-widget 'const 'item
3010 "An immutable sexp."
3011 :prompt-value 'widget-const-prompt-value
3014 (defun widget-const-prompt-value (widget prompt value unbound)
3015 ;; Return the value of the const.
3016 (widget-value widget))
3018 (define-widget 'function-item 'const
3019 "An immutable function name."
3021 :documentation-property (lambda (symbol)
3023 (documentation symbol t)
3026 (define-widget 'variable-item 'const
3027 "An immutable variable name."
3029 :documentation-property 'variable-documentation)
3031 (define-widget 'other 'sexp
3032 "Matches any value, but doesn't let the user edit the value.
3033 This is useful as last item in a `choice' widget.
3034 You should use this widget type with a default value,
3035 as in (other DEFAULT) or (other :tag \"NAME\" DEFAULT).
3036 If the user selects this alternative, that specifies DEFAULT
3042 (defvar widget-string-prompt-value-history nil
3043 "History of input to `widget-string-prompt-value'.")
3045 (define-widget 'string 'editable-field
3048 :format "%{%t%}: %v"
3049 :complete-function 'ispell-complete-word
3050 :prompt-history 'widget-string-prompt-value-history)
3054 (defun widget-string-complete ()
3055 "Complete contents of string field.
3056 Completions are taken from the :completion-alist property of the
3057 widget. If that isn't a list, it's evalled and expected to yield a list."
3059 (let* ((completion-ignore-case (widget-get widget :completion-ignore-case))
3060 (alist (widget-get widget :completion-alist))
3061 (_ (unless (listp alist)
3062 (setq alist (eval alist)))))
3063 (completion-in-region (widget-field-start widget)
3064 (max (point) (widget-field-text-end widget))
3067 (define-widget 'regexp 'string
3068 "A regular expression."
3069 :match 'widget-regexp-match
3070 :validate 'widget-regexp-validate
3071 ;; Doesn't work well with terminating newline.
3072 ;; :value-face 'widget-single-line-field
3075 (defun widget-regexp-match (widget value)
3076 ;; Match valid regexps.
3077 (and (stringp value)
3080 (string-match value ""))
3083 (defun widget-regexp-validate (widget)
3084 "Check that the value of WIDGET is a valid regexp."
3085 (condition-case data
3087 (string-match (widget-value widget) ""))
3088 (error (widget-put widget :error (error-message-string data))
3091 (define-widget 'file 'string
3093 It reads a file name from an editable text field."
3094 :complete-function 'widget-file-complete
3095 :prompt-value 'widget-file-prompt-value
3096 :format "%{%t%}: %v"
3097 ;; Doesn't work well with terminating newline.
3098 ;; :value-face 'widget-single-line-field
3101 (defun widget-file-complete ()
3102 "Perform completion on file name preceding point."
3104 (completion-in-region (widget-field-start widget)
3105 (max (point) (widget-field-text-end widget))
3106 'completion-file-name-table))
3108 (defun widget-file-prompt-value (widget prompt value unbound)
3109 ;; Read file from minibuffer.
3110 (abbreviate-file-name
3112 (read-file-name prompt)
3113 (let ((prompt2 (format "%s (default %s): " prompt value))
3114 (dir (file-name-directory value))
3115 (file (file-name-nondirectory value))
3116 (must-match (widget-get widget :must-match)))
3117 (read-file-name prompt2 dir nil must-match file)))))
3119 ;;;(defun widget-file-action (widget &optional event)
3120 ;;; ;; Read a file name from the minibuffer.
3121 ;;; (let* ((value (widget-value widget))
3122 ;;; (dir (file-name-directory value))
3123 ;;; (file (file-name-nondirectory value))
3124 ;;; (menu-tag (widget-apply widget :menu-tag-get))
3125 ;;; (must-match (widget-get widget :must-match))
3126 ;;; (answer (read-file-name (concat menu-tag " (default " value "): ")
3127 ;;; dir nil must-match file)))
3128 ;;; (widget-value-set widget (abbreviate-file-name answer))
3130 ;;; (widget-apply widget :notify widget event)))
3132 ;; Fixme: use file-name-as-directory.
3133 (define-widget 'directory 'file
3134 "A directory widget.
3135 It reads a directory name from an editable text field."
3138 (defvar widget-symbol-prompt-value-history nil
3139 "History of input to `widget-symbol-prompt-value'.")
3141 (define-widget 'symbol 'editable-field
3145 :format "%{%t%}: %v"
3146 :match (lambda (widget value) (symbolp value))
3147 :complete-function 'lisp-complete-symbol
3148 :prompt-internal 'widget-symbol-prompt-internal
3149 :prompt-match 'symbolp
3150 :prompt-history 'widget-symbol-prompt-value-history
3151 :value-to-internal (lambda (widget value)
3155 :value-to-external (lambda (widget value)
3160 (defun widget-symbol-prompt-internal (widget prompt initial history)
3161 ;; Read file from minibuffer.
3162 (let ((answer (completing-read prompt obarray
3163 (widget-get widget :prompt-match)
3164 nil initial history)))
3165 (if (and (stringp answer)
3166 (not (zerop (length answer))))
3168 (error "No value"))))
3170 (defvar widget-function-prompt-value-history nil
3171 "History of input to `widget-function-prompt-value'.")
3173 (define-widget 'function 'restricted-sexp
3175 :complete-function (lambda ()
3177 (lisp-complete-symbol 'fboundp))
3178 :prompt-value 'widget-field-prompt-value
3179 :prompt-internal 'widget-symbol-prompt-internal
3180 :prompt-match 'fboundp
3181 :prompt-history 'widget-function-prompt-value-history
3182 :action 'widget-field-action
3183 :match-alternatives '(functionp)
3184 :validate (lambda (widget)
3185 (unless (functionp (widget-value widget))
3186 (widget-put widget :error (format "Invalid function: %S"
3187 (widget-value widget)))
3192 (defvar widget-variable-prompt-value-history nil
3193 "History of input to `widget-variable-prompt-value'.")
3195 (define-widget 'variable 'symbol
3197 :prompt-match 'boundp
3198 :prompt-history 'widget-variable-prompt-value-history
3199 :complete-function (lambda ()
3201 (lisp-complete-symbol 'boundp))
3204 (define-widget 'coding-system 'symbol
3205 "A MULE coding-system."
3206 :format "%{%t%}: %v"
3207 :tag "Coding system"
3209 :prompt-history 'coding-system-value-history
3210 :prompt-value 'widget-coding-system-prompt-value
3211 :action 'widget-coding-system-action
3212 :complete-function (lambda ()
3214 (lisp-complete-symbol 'coding-system-p))
3215 :validate (lambda (widget)
3216 (unless (coding-system-p (widget-value widget))
3217 (widget-put widget :error (format "Invalid coding system: %S"
3218 (widget-value widget)))
3221 :prompt-match 'coding-system-p)
3223 (defun widget-coding-system-prompt-value (widget prompt value unbound)
3224 "Read coding-system from minibuffer."
3225 (if (widget-get widget :base-only)
3227 (completing-read (format "%s (default %s): " prompt value)
3228 (mapcar #'list (coding-system-list t)) nil nil nil
3229 coding-system-history))
3230 (read-coding-system (format "%s (default %s): " prompt value) value)))
3232 (defun widget-coding-system-action (widget &optional event)
3234 (widget-coding-system-prompt-value
3236 (widget-apply widget :menu-tag-get)
3237 (widget-value widget)
3239 (widget-value-set widget answer)
3240 (widget-apply widget :notify widget event)
3243 ;;; I'm not sure about what this is good for? KFS.
3244 (defvar widget-key-sequence-prompt-value-history nil
3245 "History of input to `widget-key-sequence-prompt-value'.")
3247 (defvar widget-key-sequence-default-value [ignore]
3248 "Default value for an empty key sequence.")
3250 (defvar widget-key-sequence-map
3251 (let ((map (make-sparse-keymap)))
3252 (set-keymap-parent map widget-field-keymap)
3253 (define-key map [(control ?q)] 'widget-key-sequence-read-event)
3256 (define-widget 'key-sequence 'restricted-sexp
3258 :prompt-value 'widget-field-prompt-value
3259 :prompt-internal 'widget-symbol-prompt-internal
3260 ; :prompt-match 'fboundp ;; What was this good for? KFS
3261 :prompt-history 'widget-key-sequence-prompt-value-history
3262 :action 'widget-field-action
3263 :match-alternatives '(stringp vectorp)
3264 :format "%{%t%}: %v"
3265 :validate 'widget-key-sequence-validate
3266 :value-to-internal 'widget-key-sequence-value-to-internal
3267 :value-to-external 'widget-key-sequence-value-to-external
3268 :value widget-key-sequence-default-value
3269 :keymap widget-key-sequence-map
3270 :help-echo "C-q: insert KEY, EVENT, or CODE; RET: enter value"
3271 :tag "Key sequence")
3273 (defun widget-key-sequence-read-event (ev)
3275 (let ((inhibit-quit t) quit-flag)
3276 (read-event "Insert KEY, EVENT, or CODE: "))))
3277 (let ((ev2 (and (memq 'down (event-modifiers ev))
3279 (tr (and (keymapp function-key-map)
3280 (lookup-key function-key-map (vector ev)))))
3281 (when (and (integerp ev)
3282 (or (and (<= ?0 ev) (< ev (+ ?0 (min 10 read-quoted-char-radix))))
3283 (and (<= ?a (downcase ev))
3284 (< (downcase ev) (+ ?a -10 (min 36 read-quoted-char-radix))))))
3285 (setq unread-command-events (cons ev unread-command-events)
3286 ev (read-quoted-char (format "Enter code (radix %d)" read-quoted-char-radix))
3288 (if (and (integerp ev) (not (characterp ev)))
3289 (insert (char-to-string ev)))) ;; throw invalid char error
3290 (setq ev (key-description (list ev)))
3292 (setq tr (key-description (list (aref tr 0))))
3293 (if (y-or-n-p (format "Key %s is translated to %s -- use %s? " ev tr tr))
3294 (setq ev tr ev2 nil)))
3295 (insert (if (= (char-before) ?\s) "" " ") ev " ")
3297 (insert (key-description (list ev2)) " "))))
3299 (defun widget-key-sequence-validate (widget)
3300 (unless (or (stringp (widget-value widget))
3301 (vectorp (widget-value widget)))
3302 (widget-put widget :error (format "Invalid key sequence: %S"
3303 (widget-value widget)))
3306 (defun widget-key-sequence-value-to-internal (widget value)
3307 (if (widget-apply widget :match value)
3308 (if (equal value widget-key-sequence-default-value)
3310 (key-description value))
3313 (defun widget-key-sequence-value-to-external (widget value)
3315 (if (string-match "\\`[[:space:]]*\\'" value)
3316 widget-key-sequence-default-value
3317 (read-kbd-macro value))
3321 (define-widget 'sexp 'editable-field
3322 "An arbitrary Lisp expression."
3323 :tag "Lisp expression"
3324 :format "%{%t%}: %v"
3326 :validate 'widget-sexp-validate
3327 :match (lambda (widget value) t)
3328 :value-to-internal 'widget-sexp-value-to-internal
3329 :value-to-external (lambda (widget value) (read value))
3330 :prompt-history 'widget-sexp-prompt-value-history
3331 :prompt-value 'widget-sexp-prompt-value)
3333 (defun widget-sexp-value-to-internal (widget value)
3334 ;; Use pp for printer representation.
3335 (let ((pp (if (symbolp value)
3336 (prin1-to-string value)
3337 (pp-to-string value))))
3338 (while (string-match "\n\\'" pp)
3339 (setq pp (substring pp 0 -1)))
3340 (if (or (string-match "\n\\'" pp)
3345 (defun widget-sexp-validate (widget)
3346 ;; Valid if we can read the string and there is no junk left after it.
3348 (insert (widget-apply widget :value-get))
3349 (goto-char (point-min))
3351 (condition-case data
3353 ;; Avoid a confusing end-of-file error.
3354 (skip-syntax-forward "\\s-")
3356 (setq err "Empty sexp -- use `nil'?")
3357 (unless (widget-apply widget :match (read (current-buffer)))
3358 (setq err (widget-get widget :type-error))))
3359 ;; Allow whitespace after expression.
3360 (skip-syntax-forward "\\s-")
3361 (if (and (not (eobp))
3363 (setq err (format "Junk at end of expression: %s"
3364 (buffer-substring (point)
3366 (end-of-file ; Avoid confusing error message.
3367 (setq err "Unbalanced sexp"))
3368 (error (setq err (error-message-string data))))
3371 (widget-put widget :error err)
3374 (defvar widget-sexp-prompt-value-history nil
3375 "History of input to `widget-sexp-prompt-value'.")
3377 (defun widget-sexp-prompt-value (widget prompt value unbound)
3378 ;; Read an arbitrary sexp.
3379 (let ((found (read-string prompt
3380 (if unbound nil (cons (prin1-to-string value) 0))
3381 (widget-get widget :prompt-history))))
3382 (let ((answer (read-from-string found)))
3383 (unless (= (cdr answer) (length found))
3384 (error "Junk at end of expression: %s"
3385 (substring found (cdr answer))))
3388 (define-widget 'restricted-sexp 'sexp
3389 "A Lisp expression restricted to values that match.
3390 To use this type, you must define :match or :match-alternatives."
3391 :type-error "The specified value is not valid"
3392 :match 'widget-restricted-sexp-match
3393 :value-to-internal (lambda (widget value)
3394 (if (widget-apply widget :match value)
3395 (prin1-to-string value)
3398 (defun widget-restricted-sexp-match (widget value)
3399 (let ((alternatives (widget-get widget :match-alternatives))
3401 (while (and alternatives (not matched))
3402 (if (cond ((functionp (car alternatives))
3403 (funcall (car alternatives) value))
3404 ((and (consp (car alternatives))
3405 (eq (car (car alternatives)) 'quote))
3406 (eq value (nth 1 (car alternatives)))))
3408 (setq alternatives (cdr alternatives)))
3411 (define-widget 'integer 'restricted-sexp
3415 :type-error "This field should contain an integer"
3416 :match-alternatives '(integerp))
3418 (define-widget 'number 'restricted-sexp
3419 "A number (floating point or integer)."
3422 :type-error "This field should contain a number (floating point or integer)"
3423 :match-alternatives '(numberp))
3425 (define-widget 'float 'restricted-sexp
3426 "A floating point number."
3427 :tag "Floating point number"
3429 :type-error "This field should contain a floating point number"
3430 :match-alternatives '(floatp))
3432 (define-widget 'character 'editable-field
3437 :format "%{%t%}: %v\n"
3438 :valid-regexp "\\`.\\'"
3439 :error "This field should contain a single character"
3440 :value-to-internal (lambda (widget value)
3443 (char-to-string value)))
3444 :value-to-external (lambda (widget value)
3448 :match (lambda (widget value)
3449 (characterp value)))
3451 (define-widget 'list 'group
3454 :format "%{%t%}:\n%v")
3456 (define-widget 'vector 'group
3459 :format "%{%t%}:\n%v"
3460 :match 'widget-vector-match
3461 :value-to-internal (lambda (widget value) (append value nil))
3462 :value-to-external (lambda (widget value) (apply 'vector value)))
3464 (defun widget-vector-match (widget value)
3465 (and (vectorp value)
3466 (widget-group-match widget
3467 (widget-apply widget :value-to-internal value))))
3469 (define-widget 'cons 'group
3472 :format "%{%t%}:\n%v"
3473 :match 'widget-cons-match
3474 :value-to-internal (lambda (widget value)
3475 (list (car value) (cdr value)))
3476 :value-to-external (lambda (widget value)
3477 (apply 'cons value)))
3479 (defun widget-cons-match (widget value)
3481 (widget-group-match widget
3482 (widget-apply widget :value-to-internal value))))
3484 ;;; The `lazy' Widget.
3486 ;; Recursive datatypes.
3488 (define-widget 'lazy 'default
3489 "Base widget for recursive datastructures.
3491 The `lazy' widget will, when instantiated, contain a single inferior
3492 widget, of the widget type specified by the :type parameter. The
3493 value of the `lazy' widget is the same as the value of the inferior
3494 widget. When deriving a new widget from the 'lazy' widget, the :type
3495 parameter is allowed to refer to the widget currently being defined,
3496 thus allowing recursive datastructures to be described.
3498 The :type parameter takes the same arguments as the defcustom
3499 parameter with the same name.
3501 Most composite widgets, i.e. widgets containing other widgets, does
3502 not allow recursion. That is, when you define a new widget type, none
3503 of the inferior widgets may be of the same type you are currently
3506 In Lisp, however, it is custom to define datastructures in terms of
3507 themselves. A list, for example, is defined as either nil, or a cons
3508 cell whose cdr itself is a list. The obvious way to translate this
3509 into a widget type would be
3511 (define-widget 'my-list 'choice
3512 \"A list of sexps.\"
3514 :args '((const nil) (cons :value (nil) sexp my-list)))
3516 Here we attempt to define my-list as a choice of either the constant
3517 nil, or a cons-cell containing a sexp and my-lisp. This will not work
3518 because the `choice' widget does not allow recursion.
3520 Using the `lazy' widget you can overcome this problem, as in this
3523 (define-widget 'sexp-list 'lazy
3524 \"A list of sexps.\"
3526 :type '(choice (const nil) (cons :value (nil) sexp sexp-list)))"
3527 :format "%{%t%}: %v"
3528 ;; We don't convert :type because we want to allow recursive
3529 ;; datastructures. This is slow, so we should not create speed
3530 ;; critical widgets by deriving from this.
3531 :convert-widget 'widget-value-convert-widget
3532 :value-create 'widget-type-value-create
3533 :value-get 'widget-child-value-get
3534 :value-inline 'widget-child-value-inline
3535 :default-get 'widget-type-default-get
3536 :match 'widget-type-match
3537 :validate 'widget-child-validate)
3540 ;;; The `plist' Widget.
3544 (define-widget 'plist 'list
3546 :key-type '(symbol :tag "Key")
3547 :value-type '(sexp :tag "Value")
3548 :convert-widget 'widget-plist-convert-widget
3551 (defvar widget-plist-value-type) ;Dynamic variable
3553 (defun widget-plist-convert-widget (widget)
3554 ;; Handle `:options'.
3555 (let* ((options (widget-get widget :options))
3556 (widget-plist-value-type (widget-get widget :value-type))
3557 (other `(editable-list :inline t
3559 ,(widget-get widget :key-type)
3560 ,widget-plist-value-type)))
3562 (list `(checklist :inline t
3564 ,@(mapcar 'widget-plist-convert-option
3568 (widget-put widget :args args)
3571 (defun widget-plist-convert-option (option)
3572 ;; Convert a single plist option.
3573 (let (key-type value-type)
3575 (let ((key (nth 0 option)))
3576 (setq value-type (nth 1 option))
3579 (setq key-type `(const ,key))))
3580 (setq key-type `(const ,option)
3581 value-type widget-plist-value-type))
3582 `(group :format "Key: %v" :inline t ,key-type ,value-type)))
3585 ;;; The `alist' Widget.
3587 ;; Association lists.
3589 (define-widget 'alist 'list
3590 "An association list."
3591 :key-type '(sexp :tag "Key")
3592 :value-type '(sexp :tag "Value")
3593 :convert-widget 'widget-alist-convert-widget
3596 (defvar widget-alist-value-type) ;Dynamic variable
3598 (defun widget-alist-convert-widget (widget)
3599 ;; Handle `:options'.
3600 (let* ((options (widget-get widget :options))
3601 (widget-alist-value-type (widget-get widget :value-type))
3602 (other `(editable-list :inline t
3604 ,(widget-get widget :key-type)
3605 ,widget-alist-value-type)))
3607 (list `(checklist :inline t
3609 ,@(mapcar 'widget-alist-convert-option
3613 (widget-put widget :args args)
3616 (defun widget-alist-convert-option (option)
3617 ;; Convert a single alist option.
3618 (let (key-type value-type)
3620 (let ((key (nth 0 option)))
3621 (setq value-type (nth 1 option))
3624 (setq key-type `(const ,key))))
3625 (setq key-type `(const ,option)
3626 value-type widget-alist-value-type))
3627 `(cons :format "Key: %v" ,key-type ,value-type)))
3629 (define-widget 'choice 'menu-choice
3630 "A union of several sexp types."
3632 :format "%{%t%}: %[Value Menu%] %v"
3633 :button-prefix 'widget-push-button-prefix
3634 :button-suffix 'widget-push-button-suffix
3635 :prompt-value 'widget-choice-prompt-value)
3637 (defun widget-choice-prompt-value (widget prompt value unbound)
3639 (let ((args (widget-get widget :args))
3640 (completion-ignore-case (widget-get widget :case-fold))
3641 current choices old)
3642 ;; Find the first arg that matches VALUE.
3645 (if (widget-apply (car look) :match value)
3646 (setq old (car look)
3648 (setq look (cdr look)))))
3651 (cond ((= (length args) 0)
3653 ((= (length args) 1)
3655 ((and (= (length args) 2)
3657 (if (eq old (nth 0 args))
3662 (setq current (car args)
3665 (cons (cons (widget-apply current :menu-tag-get)
3668 (let ((val (completing-read prompt choices nil t)))
3670 (let ((try (try-completion val choices)))
3673 (cdr (assoc val choices)))
3676 (widget-prompt-value current prompt nil t)
3679 (define-widget 'radio 'radio-button-choice
3680 "A union of several sexp types."
3682 :format "%{%t%}:\n%v"
3683 :prompt-value 'widget-choice-prompt-value)
3685 (define-widget 'repeat 'editable-list
3686 "A variable length homogeneous list."
3688 :format "%{%t%}:\n%v%i\n")
3690 (define-widget 'set 'checklist
3691 "A list of members from a fixed set."
3693 :format "%{%t%}:\n%v")
3695 (define-widget 'boolean 'toggle
3696 "To be nil or non-nil, that is the question."
3698 :prompt-value 'widget-boolean-prompt-value
3699 :button-prefix 'widget-push-button-prefix
3700 :button-suffix 'widget-push-button-suffix
3701 :format "%{%t%}: %[Toggle%] %v\n"
3705 (defun widget-boolean-prompt-value (widget prompt value unbound)
3706 ;; Toggle a boolean.
3709 ;;; The `color' Widget.
3712 (define-widget 'color 'editable-field
3713 "Choose a color name (with sample)."
3714 :format "%{%t%}: %v (%{sample%})\n"
3715 :value-create 'widget-color-value-create
3719 :complete 'widget-color-complete
3720 :sample-face-get 'widget-color-sample-face-get
3721 :notify 'widget-color-notify
3722 :action 'widget-color-action)
3724 (defun widget-color-value-create (widget)
3725 (widget-field-value-create widget)
3727 (widget-create-child-and-convert
3729 :tag "Choose" :action 'widget-color--choose-action)
3730 (widget-insert " "))
3732 (defun widget-color--choose-action (widget &optional event)
3733 (list-colors-display
3736 (when (buffer-live-p ,(current-buffer))
3737 (widget-value-set ',(widget-get widget :parent) color)
3738 (let* ((buf (get-buffer "*Colors*"))
3739 (win (get-buffer-window buf 0)))
3741 (and win (> (length (window-list)) 1)
3742 (delete-window win)))
3743 (pop-to-buffer ,(current-buffer))))))
3745 (defun widget-color-complete (widget)
3746 "Complete the color in WIDGET."
3747 (require 'facemenu) ; for facemenu-color-alist
3748 (completion-in-region (widget-field-start widget)
3749 (max (point) (widget-field-text-end widget))
3750 (or facemenu-color-alist
3751 (sort (defined-colors) 'string-lessp))))
3753 (defun widget-color-sample-face-get (widget)
3754 (let* ((value (condition-case nil
3755 (widget-value widget)
3756 (error (widget-get widget :value)))))
3757 (if (color-defined-p value)
3758 (list (cons 'foreground-color value))
3761 (defun widget-color-action (widget &optional event)
3762 "Prompt for a color."
3763 (let* ((tag (widget-apply widget :menu-tag-get))
3764 (prompt (concat tag ": "))
3765 (value (widget-value widget))
3766 (start (widget-field-start widget))
3767 (answer (facemenu-read-color prompt)))
3768 (unless (zerop (length answer))
3769 (widget-value-set widget answer)
3771 (widget-apply widget :notify widget event))))
3773 (defun widget-color-notify (widget child &optional event)
3774 "Update the sample, and notify the parent."
3775 (overlay-put (widget-get widget :sample-overlay)
3776 'face (widget-apply widget :sample-face-get))
3777 (widget-default-notify widget child event))
3781 (defun widget-echo-help (pos)
3782 "Display help-echo text for widget at POS."
3783 (let* ((widget (widget-at pos))
3784 (help-echo (and widget (widget-get widget :help-echo))))
3785 (if (functionp help-echo)
3786 (setq help-echo (funcall help-echo widget)))
3787 (if help-echo (message "%s" (eval help-echo)))))
3793 ;; arch-tag: a076e75e-18a1-4b46-8be5-3f317bcbc707
3794 ;;; wid-edit.el ends here