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
10 ;; This file is part of GNU Emacs.
12 ;; GNU Emacs is free software: you can redistribute it and/or modify
13 ;; it under the terms of the GNU General Public License as published by
14 ;; the Free Software Foundation, either version 3 of the License, or
15 ;; (at your option) any later version.
17 ;; GNU Emacs is distributed in the hope that it will be useful,
18 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
19 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 ;; GNU General Public License for more details.
22 ;; You should have received a copy of the GNU General Public License
23 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
25 ;;; Wishlist items (from widget.texi):
27 ;; * The `menu-choice' tag should be prettier, something like the
28 ;; abbreviated menus in Open Look.
30 ;; * Finish `:tab-order'.
32 ;; * Make indentation work with glyphs and proportional fonts.
34 ;; * Add commands to show overview of object and class hierarchies to
37 ;; * Find a way to disable mouse highlight for inactive widgets.
39 ;; * Find a way to make glyphs look inactive.
41 ;; * Add `key-binding' widget.
43 ;; * Add `widget' widget for editing widget specifications.
45 ;; * Find clean way to implement variable length list. See
46 ;; `TeX-printer-list' for an explanation.
48 ;; * `C-h' in `widget-prompt-value' should give type specific help.
50 ;; * A mailto widget. [This should work OK as a url-link if with
51 ;; browse-url-browser-function' set up appropriately.]
63 (defun widget-event-point (event)
64 "Character position of the end of event if that exists, or nil."
65 (posn-point (event-end event
)))
67 (defun widget-button-release-event-p (event)
68 "Non-nil if EVENT is a mouse-button-release event object."
70 (memq (event-basic-type event
) '(mouse-1 mouse-2 mouse-3
))
71 (or (memq 'click
(event-modifiers event
))
72 (memq 'drag
(event-modifiers event
)))))
77 "Customization support for the Widget Library."
78 :link
'(custom-manual "(widget)Top")
79 :link
'(emacs-library-link :tag
"Lisp File" "widget.el")
83 (defgroup widget-documentation nil
84 "Options controlling the display of documentation strings."
87 (defgroup widget-faces nil
88 "Faces used by the widget library."
92 (defvar widget-documentation-face
'widget-documentation
93 "Face used for documentation strings in widgets.
94 This exists as a variable so it can be set locally in certain buffers.")
96 (defface widget-documentation
'((((class color
)
98 (:foreground
"lime green"))
101 (:foreground
"dark green"))
103 "Face used for documentation text."
104 :group
'widget-documentation
105 :group
'widget-faces
)
106 (define-obsolete-face-alias 'widget-documentation-face
107 'widget-documentation
"22.1")
109 (defvar widget-button-face
'widget-button
110 "Face used for buttons in widgets.
111 This exists as a variable so it can be set locally in certain buffers.")
113 (defface widget-button
'((t (:weight bold
)))
114 "Face used for widget buttons."
115 :group
'widget-faces
)
116 (define-obsolete-face-alias 'widget-button-face
'widget-button
"22.1")
118 (defcustom widget-mouse-face
'highlight
119 "Face used for widget buttons when the mouse is above them."
121 :group
'widget-faces
)
123 ;; TTY gets special definitions here and in the next defface, because
124 ;; the gray colors defined for other displays cause black text on a black
125 ;; background, at least on light-background TTYs.
126 (defface widget-field
'((((type tty
))
127 :background
"yellow3"
129 (((class grayscale color
)
131 :background
"gray85")
132 (((class grayscale color
)
134 :background
"dim gray")
137 "Face used for editable fields."
138 :group
'widget-faces
)
139 (define-obsolete-face-alias 'widget-field-face
'widget-field
"22.1")
141 (defface widget-single-line-field
'((((type tty
))
144 (((class grayscale color
)
146 :background
"gray85")
147 (((class grayscale color
)
149 :background
"dim gray")
152 "Face used for editable fields spanning only a single line."
153 :group
'widget-faces
)
154 (define-obsolete-face-alias 'widget-single-line-field-face
155 'widget-single-line-field
"22.1")
157 ;;; This causes display-table to be loaded, and not usefully.
158 ;;;(defvar widget-single-line-display-table
159 ;;; (let ((table (make-display-table)))
160 ;;; (aset table 9 "^I")
161 ;;; (aset table 10 "^J")
163 ;;; "Display table used for single-line editable fields.")
165 ;;;(when (fboundp 'set-face-display-table)
166 ;;; (set-face-display-table 'widget-single-line-field-face
167 ;;; widget-single-line-display-table))
169 ;;; Utility functions.
171 ;; These are not really widget specific.
173 (defun widget-princ-to-string (object)
174 "Return string representation of OBJECT, any Lisp object.
175 No quoting characters are used; no delimiters are printed around
176 the contents of strings."
177 (with-output-to-string
180 (defun widget-clear-undo ()
181 "Clear all undo information."
182 (buffer-disable-undo (current-buffer))
183 (buffer-enable-undo))
185 (defcustom widget-menu-max-size
40
186 "Largest number of items allowed in a popup-menu.
187 Larger menus are read through the minibuffer."
191 (defcustom widget-menu-max-shortcuts
40
192 "Largest number of items for which it works to choose one with a character.
193 For a larger number of items, the minibuffer is used."
197 (defcustom widget-menu-minibuffer-flag nil
198 "Control how to ask for a choice from the keyboard.
199 Non-nil means use the minibuffer;
200 nil means read a single character."
204 (defun widget-choose (title items
&optional event
)
205 "Choose an item from a list.
207 First argument TITLE is the name of the list.
208 Second argument ITEMS is a list whose members are either
209 (NAME . VALUE), to indicate selectable items, or just strings to
210 indicate unselectable items.
211 Optional third argument EVENT is an input event.
213 The user is asked to choose between each NAME from the items alist,
214 and the VALUE of the chosen element will be returned. If EVENT is a
215 mouse event, and the number of elements in items is less than
216 `widget-menu-max-size', a popup menu will be used, otherwise the
218 (cond ((and (< (length items
) widget-menu-max-size
)
219 event
(display-popup-menus-p))
222 (list title
(cons "" items
))))
223 ((or widget-menu-minibuffer-flag
224 (> (length items
) widget-menu-max-shortcuts
))
225 ;; Read the choice of name from the minibuffer.
226 (setq items
(widget-remove-if 'stringp items
))
227 (let ((val (completing-read (concat title
": ") items nil t
)))
229 (let ((try (try-completion val items
)))
232 (cdr (assoc val items
))))))
234 ;; Construct a menu of the choices
235 ;; and then use it for prompting for a single character.
236 (let* ((overriding-terminal-local-map (make-sparse-keymap))
238 map choice some-choice-enabled value
)
239 ;; Define SPC as a prefix char to get to this menu.
240 (define-key overriding-terminal-local-map
" "
241 (setq map
(make-sparse-keymap title
)))
242 (with-current-buffer (get-buffer-create " widget-choose")
244 (insert "Available choices:\n\n")
246 (setq choice
(car items
) items
(cdr items
))
248 (let* ((name (car choice
))
249 (function (cdr choice
)))
250 (insert (format "%c = %s\n" next-digit name
))
251 (define-key map
(vector next-digit
) function
)
252 (setq some-choice-enabled t
)))
253 ;; Allocate digits to disabled alternatives
254 ;; so that the digit of a given alternative never varies.
255 (setq next-digit
(1+ next-digit
)))
256 (insert "\nC-g = Quit")
257 (goto-char (point-min))
259 (or some-choice-enabled
260 (error "None of the choices is currently meaningful"))
261 (define-key map
[?\C-g
] 'keyboard-quit
)
262 (define-key map
[t] 'keyboard-quit)
263 (define-key map [?\M-\C-v] 'scroll-other-window)
264 (define-key map [?\M--] 'negative-argument)
265 (setcdr map (nreverse (cdr map)))
266 ;; Read a char with the menu, and return the result
267 ;; that corresponds to it.
268 (save-window-excursion
269 (let ((buf (get-buffer " widget-choose")))
270 (fit-window-to-buffer (display-buffer buf))
271 (let ((cursor-in-echo-area t)
275 (while (not (or (and (integerp char)
276 (>= char ?0) (< char next-digit))
277 (eq value 'keyboard-quit)))
278 ;; Unread a SPC to lead to our new menu.
279 (setq unread-command-events (cons ?\s unread-command-events))
280 (setq keys (read-key-sequence title))
282 (lookup-key overriding-terminal-local-map keys t)
284 (cond ((eq value 'scroll-other-window)
285 (let ((minibuffer-scroll-window
286 (get-buffer-window buf)))
288 (scroll-other-window-down
289 (window-height minibuffer-scroll-window))
290 (scroll-other-window))
292 ((eq value 'negative-argument)
296 (when (eq value 'keyboard-quit)
300 (defun widget-remove-if (predictate list)
301 (let (result (tail list))
303 (or (funcall predictate (car tail))
304 (setq result (cons (car tail) result)))
305 (setq tail (cdr tail)))
308 ;;; Widget text specifications.
310 ;; These functions are for specifying text properties.
312 ;; We can set it to nil now that get_local_map uses get_pos_property.
313 (defconst widget-field-add-space nil
314 "Non-nil means add extra space at the end of editable text fields.
315 If you don't add the space, it will become impossible to edit a zero
318 (defvar widget-field-use-before-change t
319 "Non-nil means use `before-change-functions' to track editable fields.
320 This enables the use of undo, but doesn't work on Emacs 19.34 and earlier.
321 Using before hooks also means that the :notify function can't know the
324 (defun widget-specify-field (widget from to)
325 "Specify editable button for WIDGET between FROM and TO."
326 ;; Terminating space is not part of the field, but necessary in
327 ;; order for local-map to work. Remove next sexp if local-map works
328 ;; at the end of the overlay.
331 (cond ((null (widget-get widget :size))
333 (widget-field-add-space
334 (insert-and-inherit " ")))
336 (let ((keymap (widget-get widget :keymap))
337 (face (or (widget-get widget :value-face) 'widget-field))
338 (help-echo (widget-get widget :help-echo))
339 (follow-link (widget-get widget :follow-link))
341 (or (not widget-field-add-space) (widget-get widget :size))))
342 (if (functionp help-echo)
343 (setq help-echo 'widget-mouse-help))
344 (when (= (char-before to) ?\n)
345 ;; When the last character in the field is a newline, we want to
346 ;; give it a `field' char-property of `boundary', which helps the
347 ;; C-n/C-p act more naturally when entering/leaving the field. We
348 ;; do this by making a small secondary overlay to contain just that
350 (let ((overlay (make-overlay (1- to) to nil t nil)))
351 (overlay-put overlay 'field 'boundary)
352 ;; We need the real field for tabbing.
353 (overlay-put overlay 'real-field widget)
354 ;; Use `local-map' here, not `keymap', so that normal editing
355 ;; works in the field when, say, Custom uses `suppress-keymap'.
356 (overlay-put overlay 'local-map keymap)
357 (overlay-put overlay 'face face)
358 (overlay-put overlay 'follow-link follow-link)
359 (overlay-put overlay 'help-echo help-echo))
361 (setq rear-sticky t))
362 (let ((overlay (make-overlay from to nil nil rear-sticky)))
363 (widget-put widget :field-overlay overlay)
364 ;;(overlay-put overlay 'detachable nil)
365 (overlay-put overlay 'field widget)
366 (overlay-put overlay 'local-map keymap)
367 (overlay-put overlay 'face face)
368 (overlay-put overlay 'follow-link follow-link)
369 (overlay-put overlay 'help-echo help-echo)))
370 (widget-specify-secret widget))
372 (defun widget-specify-secret (field)
373 "Replace text in FIELD with value of `:secret', if non-nil."
374 (let ((secret (widget-get field :secret))
375 (size (widget-get field :size)))
377 (let ((begin (widget-field-start field))
378 (end (widget-field-end field)))
380 (while (and (> end begin)
381 (eq (char-after (1- end)) ?\s))
382 (setq end (1- end))))
384 (let ((old (char-after begin)))
385 (unless (eq old secret)
386 (subst-char-in-region begin (1+ begin) old secret)
387 (put-text-property begin (1+ begin) 'secret old))
388 (setq begin (1+ begin))))))))
390 (defun widget-specify-button (widget from to)
391 "Specify button for WIDGET between FROM and TO."
392 (let ((overlay (make-overlay from to nil t nil))
393 (follow-link (widget-get widget :follow-link))
394 (help-echo (widget-get widget :help-echo)))
395 (widget-put widget :button-overlay overlay)
396 (if (functionp help-echo)
397 (setq help-echo 'widget-mouse-help))
398 (overlay-put overlay 'button widget)
399 (overlay-put overlay 'keymap (widget-get widget :keymap))
400 (overlay-put overlay 'evaporate t)
401 ;; We want to avoid the face with image buttons.
402 (unless (widget-get widget :suppress-face)
403 (overlay-put overlay 'face (widget-apply widget :button-face-get))
404 (overlay-put overlay 'mouse-face
405 ;; Make new list structure for the mouse-face value
406 ;; so that different widgets will have
407 ;; different `mouse-face' property values
408 ;; and will highlight separately.
409 (let ((mouse-face-value
410 (widget-apply widget :mouse-face-get)))
411 ;; If it's a list, copy it.
412 (if (listp mouse-face-value)
413 (copy-sequence mouse-face-value)
414 ;; If it's a symbol, put it in a list.
415 (list mouse-face-value)))))
416 (overlay-put overlay 'pointer 'hand)
417 (overlay-put overlay 'follow-link follow-link)
418 (overlay-put overlay 'help-echo help-echo)))
420 (defun widget-mouse-help (window overlay point)
421 "Help-echo callback for widgets whose :help-echo is a function."
422 (with-current-buffer (overlay-buffer overlay)
423 (let* ((widget (widget-at (overlay-start overlay)))
424 (help-echo (if widget (widget-get widget :help-echo))))
425 (if (functionp help-echo)
426 (funcall help-echo widget)
429 (defun widget-specify-sample (widget from to)
430 "Specify sample for WIDGET between FROM and TO."
431 (let ((overlay (make-overlay from to nil t nil)))
432 (overlay-put overlay 'face (widget-apply widget :sample-face-get))
433 (overlay-put overlay 'evaporate t)
434 (widget-put widget :sample-overlay overlay)))
436 (defun widget-specify-doc (widget from to)
437 "Specify documentation for WIDGET between FROM and TO."
438 (let ((overlay (make-overlay from to nil t nil)))
439 (overlay-put overlay 'widget-doc widget)
440 (overlay-put overlay 'face widget-documentation-face)
441 (overlay-put overlay 'evaporate t)
442 (widget-put widget :doc-overlay overlay)))
444 (defmacro widget-specify-insert (&rest form)
445 "Execute FORM without inheriting any text properties."
447 (let ((inhibit-read-only t)
448 (inhibit-modification-hooks t))
449 (narrow-to-region (point) (point))
450 (prog1 (progn ,@form)
451 (goto-char (point-max))))))
453 (defface widget-inactive
454 '((t :inherit shadow))
455 "Face used for inactive widgets."
456 :group 'widget-faces)
457 (define-obsolete-face-alias 'widget-inactive-face
458 'widget-inactive "22.1")
460 (defun widget-specify-inactive (widget from to)
461 "Make WIDGET inactive for user modifications."
462 (unless (widget-get widget :inactive)
463 (let ((overlay (make-overlay from to nil t nil)))
464 (overlay-put overlay 'face 'widget-inactive)
465 ;; This is disabled, as it makes the mouse cursor change shape.
466 ;; (overlay-put overlay 'mouse-face 'widget-inactive)
467 (overlay-put overlay 'evaporate t)
468 (overlay-put overlay 'priority 100)
469 (overlay-put overlay 'modification-hooks '(widget-overlay-inactive))
470 (widget-put widget :inactive overlay))))
472 (defun widget-overlay-inactive (&rest junk)
473 "Ignoring the arguments, signal an error."
474 (unless inhibit-read-only
475 (error "The widget here is not active")))
478 (defun widget-specify-active (widget)
479 "Make WIDGET active for user modifications."
480 (let ((inactive (widget-get widget :inactive)))
482 (delete-overlay inactive)
483 (widget-put widget :inactive nil))))
485 ;;; Widget Properties.
487 (defsubst widget-type (widget)
488 "Return the type of WIDGET. The type is a symbol."
492 (defun widgetp (widget)
493 "Return non-nil if WIDGET is a widget."
495 (get widget 'widget-type)
497 (symbolp (car widget))
498 (get (car widget) 'widget-type))))
500 (defun widget-get-indirect (widget property)
501 "In WIDGET, get the value of PROPERTY.
502 If the value is a symbol, return its binding.
503 Otherwise, just return the value."
504 (let ((value (widget-get widget property)))
509 (defun widget-member (widget property)
510 "Non-nil if there is a definition in WIDGET for PROPERTY."
511 (cond ((plist-member (cdr widget) property)
514 (widget-member (get (car widget) 'widget-type) property))
517 (defun widget-value (widget)
518 "Extract the current value of WIDGET."
520 :value-to-external (widget-apply widget :value-get)))
522 (defun widget-value-set (widget value)
523 "Set the current value of WIDGET to VALUE."
525 :value-set (widget-apply widget
526 :value-to-internal value)))
528 (defun widget-default-get (widget)
529 "Extract the default external value of WIDGET."
530 (widget-apply widget :value-to-external
531 (or (widget-get widget :value)
532 (widget-apply widget :default-get))))
534 (defun widget-match-inline (widget vals)
535 "In WIDGET, match the start of VALS."
536 (cond ((widget-get widget :inline)
537 (widget-apply widget :match-inline vals))
539 (widget-apply widget :match (car vals)))
540 (cons (list (car vals)) (cdr vals)))
543 (defun widget-apply-action (widget &optional event)
544 "Apply :action in WIDGET in response to EVENT."
545 (if (widget-apply widget :active)
546 (widget-apply widget :action event)
547 (error "Attempt to perform action on inactive widget")))
549 ;;; Helper functions.
551 ;; These are widget specific.
554 (defun widget-prompt-value (widget prompt &optional value unbound)
555 "Prompt for a value matching WIDGET, using PROMPT.
556 The current value is assumed to be VALUE, unless UNBOUND is non-nil."
557 (unless (listp widget)
558 (setq widget (list widget)))
559 (setq prompt (format "[%s] %s" (widget-type widget) prompt))
560 (setq widget (widget-convert widget))
561 (let ((answer (widget-apply widget :prompt-value prompt value unbound)))
562 (unless (widget-apply widget :match answer)
563 (error "Value does not match %S type" (car widget)))
566 (defun widget-get-sibling (widget)
567 "Get the item WIDGET is assumed to toggle.
568 This is only meaningful for radio buttons or checkboxes in a list."
569 (let* ((children (widget-get (widget-get widget :parent) :children))
573 (setq child (car children)
574 children (cdr children))
575 (when (eq (widget-get child :button) widget)
576 (throw 'child child)))
579 (defun widget-map-buttons (function &optional buffer maparg)
580 "Map FUNCTION over the buttons in BUFFER.
581 FUNCTION is called with the arguments WIDGET and MAPARG.
583 If FUNCTION returns non-nil, the walk is cancelled.
585 The arguments MAPARG, and BUFFER default to nil and (current-buffer),
587 (let ((cur (point-min))
590 (with-current-buffer buffer (overlay-lists))
592 (setq overlays (append (car overlays) (cdr overlays)))
593 (while (setq cur (pop overlays))
594 (setq widget (overlay-get cur 'button))
595 (if (and widget (funcall function widget maparg))
596 (setq overlays nil)))))
600 (defcustom widget-image-directory (file-name-as-directory
601 (expand-file-name "images/custom" data-directory))
602 "Where widget button images are located.
603 If this variable is nil, widget will try to locate the directory
608 (defcustom widget-image-enable t
609 "If non-nil, use image buttons in widgets when available."
614 (defcustom widget-image-conversion
615 '((xpm ".xpm") (gif ".gif") (png ".png") (jpeg ".jpg" ".jpeg")
617 "Conversion alist from image formats to file name suffixes."
619 :type '(repeat (cons :format "%v"
620 (symbol :tag "Image Format" unknown)
621 (repeat :tag "Suffixes"
622 (string :format "%v")))))
624 (defun widget-image-find (image)
625 "Create a graphical button from IMAGE.
626 IMAGE should either already be an image, or be a file name sans
627 extension (xpm, xbm, gif, jpg, or png) located in
628 `widget-image-directory' or otherwise where `find-image' will find it."
629 (cond ((not (and image widget-image-enable (display-graphic-p)))
630 ;; We don't want or can't use images.
633 (eq 'image (car image)))
634 ;; Already an image spec. Use it.
637 ;; A string. Look it up in relevant directories.
638 (let* ((load-path (cons widget-image-directory load-path))
640 (dolist (elt widget-image-conversion)
641 (dolist (ext (cdr elt))
642 (push (list :type (car elt) :file (concat image ext)) specs)))
643 (find-image (nreverse specs))))
648 (defvar widget-button-pressed-face 'widget-button-pressed
649 "Face used for pressed buttons in widgets.
650 This exists as a variable so it can be set locally in certain
653 (defun widget-image-insert (widget tag image &optional down inactive)
654 "In WIDGET, insert the text TAG or, if supported, IMAGE.
655 IMAGE should either be an image or an image file name sans extension
656 \(xpm, xbm, gif, jpg, or png) located in `widget-image-directory'.
658 Optional arguments DOWN and INACTIVE are used instead of IMAGE when the
659 button is pressed or inactive, respectively. These are currently ignored."
660 (if (and (featurep 'image)
661 (setq image (widget-image-find image)))
662 (progn (widget-put widget :suppress-face t)
663 (insert-image image tag))
666 (defun widget-move-and-invoke (event)
667 "Move to where you click, and if it is an active field, invoke it."
669 (mouse-set-point event)
670 (let ((pos (widget-event-point event)))
671 (if (and pos (get-char-property pos 'button))
672 (widget-button-click event))))
676 (defgroup widget-button nil
677 "The look of various kinds of buttons."
680 (defcustom widget-button-prefix ""
681 "String used as prefix for buttons."
683 :group 'widget-button)
685 (defcustom widget-button-suffix ""
686 "String used as suffix for buttons."
688 :group 'widget-button)
690 ;;; Creating Widgets.
693 (defun widget-create (type &rest args)
694 "Create widget of TYPE.
695 The optional ARGS are additional keyword arguments."
696 (let ((widget (apply 'widget-convert type args)))
697 (widget-apply widget :create)
700 (defun widget-create-child-and-convert (parent type &rest args)
701 "As part of the widget PARENT, create a child widget TYPE.
702 The child is converted, using the keyword arguments ARGS."
703 (let ((widget (apply 'widget-convert type args)))
704 (widget-put widget :parent parent)
705 (unless (widget-get widget :indent)
706 (widget-put widget :indent (+ (or (widget-get parent :indent) 0)
707 (or (widget-get widget :extra-offset) 0)
708 (widget-get parent :offset))))
709 (widget-apply widget :create)
712 (defun widget-create-child (parent type)
713 "Create widget of TYPE."
714 (let ((widget (widget-copy type)))
715 (widget-put widget :parent parent)
716 (unless (widget-get widget :indent)
717 (widget-put widget :indent (+ (or (widget-get parent :indent) 0)
718 (or (widget-get widget :extra-offset) 0)
719 (widget-get parent :offset))))
720 (widget-apply widget :create)
723 (defun widget-create-child-value (parent type value)
724 "Create widget of TYPE with value VALUE."
725 (let ((widget (widget-copy type)))
726 (widget-put widget :value (widget-apply widget :value-to-internal value))
727 (widget-put widget :parent parent)
728 (unless (widget-get widget :indent)
729 (widget-put widget :indent (+ (or (widget-get parent :indent) 0)
730 (or (widget-get widget :extra-offset) 0)
731 (widget-get parent :offset))))
732 (widget-apply widget :create)
736 (defun widget-delete (widget)
738 (widget-apply widget :delete))
740 (defun widget-copy (widget)
741 "Make a deep copy of WIDGET."
742 (widget-apply (copy-sequence widget) :copy))
744 (defun widget-convert (type &rest args)
745 "Convert TYPE to a widget without inserting it in the buffer.
746 The optional ARGS are additional keyword arguments."
747 ;; Don't touch the type.
748 (let* ((widget (if (symbolp type)
750 (copy-sequence type)))
754 ;; First set the :args keyword.
755 (while (cdr current) ;Look in the type.
756 (if (and (keywordp (cadr current))
757 ;; If the last element is a keyword,
758 ;; it is still the :args element,
759 ;; even though it is a keyword.
761 (if (eq (cadr current) :args)
762 ;; If :args is explicitly specified, obey it.
764 ;; Some other irrelevant keyword.
765 (setq current (cdr (cdr current))))
766 (setcdr current (list :args (cdr current)))
768 (while (and args (not done)) ;Look in ARGS.
769 (cond ((eq (car args) :args)
770 ;; Handle explicit specification of :args.
771 (setq args (cadr args)
773 ((keywordp (car args))
774 (setq args (cddr args)))
777 (widget-put widget :args args))
778 ;; Then Convert the widget.
781 (let ((convert-widget (plist-get (cdr type) :convert-widget)))
783 (setq widget (funcall convert-widget widget))))
784 (setq type (get (car type) 'widget-type)))
785 ;; Finally set the keyword args.
787 (let ((next (nth 0 keys)))
790 (widget-put widget next (nth 1 keys))
791 (setq keys (nthcdr 2 keys)))
793 ;; Convert the :value to internal format.
794 (if (widget-member widget :value)
796 :value (widget-apply widget
798 (widget-get widget :value))))
799 ;; Return the newly create widget.
803 (defun widget-insert (&rest args)
804 "Call `insert' with ARGS even if surrounding text is read only."
805 (let ((inhibit-read-only t)
806 (inhibit-modification-hooks t))
807 (apply 'insert args)))
809 (defun widget-convert-text (type from to
810 &optional button-from button-to
812 "Return a widget of type TYPE with endpoint FROM TO.
813 No text will be inserted to the buffer, instead the text between FROM
814 and TO will be used as the widgets end points. If optional arguments
815 BUTTON-FROM and BUTTON-TO are given, these will be used as the widgets
817 Optional ARGS are extra keyword arguments for TYPE."
818 (let ((widget (apply 'widget-convert type :delete 'widget-leave-text args))
819 (from (copy-marker from))
820 (to (copy-marker to)))
821 (set-marker-insertion-type from t)
822 (set-marker-insertion-type to nil)
823 (widget-put widget :from from)
824 (widget-put widget :to to)
826 (widget-specify-button widget button-from button-to))
829 (defun widget-convert-button (type from to &rest args)
830 "Return a widget of type TYPE with endpoint FROM TO.
831 Optional ARGS are extra keyword arguments for TYPE.
832 No text will be inserted to the buffer, instead the text between FROM
833 and TO will be used as the widgets end points, as well as the widgets
835 (apply 'widget-convert-text type from to from to args))
837 (defun widget-leave-text (widget)
838 "Remove markers and overlays from WIDGET and its children."
839 (let ((button (widget-get widget :button-overlay))
840 (sample (widget-get widget :sample-overlay))
841 (doc (widget-get widget :doc-overlay))
842 (field (widget-get widget :field-overlay)))
843 (set-marker (widget-get widget :from) nil)
844 (set-marker (widget-get widget :to) nil)
846 (delete-overlay button))
848 (delete-overlay sample))
850 (delete-overlay doc))
852 (delete-overlay field))
853 (mapc 'widget-leave-text (widget-get widget :children))))
855 ;;; Keymap and Commands.
857 ;; This alias exists only so that one can choose in doc-strings (e.g.
858 ;; Custom-mode) which key-binding of widget-keymap one wants to refer to.
859 ;; http://lists.gnu.org/archive/html/emacs-devel/2008-11/msg00480.html
860 (define-obsolete-function-alias 'advertised-widget-backward
861 'widget-backward "23.2")
864 (defvar widget-keymap
865 (let ((map (make-sparse-keymap)))
866 (define-key map "\t" 'widget-forward)
867 (define-key map "\e\t" 'widget-backward)
868 (define-key map [(shift tab)] 'widget-backward)
869 (put 'widget-backward :advertised-binding [(shift tab)])
870 (define-key map [backtab] 'widget-backward)
871 (define-key map [down-mouse-2] 'widget-button-click)
872 (define-key map [down-mouse-1] 'widget-button-click)
873 ;; The following definition needs to avoid using escape sequences that
874 ;; might get converted to ^M when building loaddefs.el
875 (define-key map [(control ?m)] 'widget-button-press)
877 "Keymap containing useful binding for buffers containing widgets.
878 Recommended as a parent keymap for modes using widgets.
879 Note that such modes will need to require wid-edit.")
881 (defvar widget-global-map global-map
882 "Keymap used for events a widget does not handle itself.")
883 (make-variable-buffer-local 'widget-global-map)
885 (defvar widget-field-keymap
886 (let ((map (copy-keymap widget-keymap)))
887 (define-key map "\C-k" 'widget-kill-line)
888 (define-key map "\M-\t" 'widget-complete)
889 (define-key map "\C-m" 'widget-field-activate)
890 ;; Since the widget code uses a `field' property to identify fields,
891 ;; ordinary beginning-of-line does the right thing.
892 ;; (define-key map "\C-a" 'widget-beginning-of-line)
893 (define-key map "\C-e" 'widget-end-of-line)
895 "Keymap used inside an editable field.")
897 (defvar widget-text-keymap
898 (let ((map (copy-keymap widget-keymap)))
899 ;; Since the widget code uses a `field' property to identify fields,
900 ;; ordinary beginning-of-line does the right thing.
901 ;; (define-key map "\C-a" 'widget-beginning-of-line)
902 (define-key map "\C-e" 'widget-end-of-line)
904 "Keymap used inside a text field.")
906 (defun widget-field-activate (pos &optional event)
907 "Invoke the editable field at point."
909 (let ((field (widget-field-at pos)))
911 (widget-apply-action field event)
913 (lookup-key widget-global-map (this-command-keys))))))
915 (defface widget-button-pressed
916 '((((min-colors 88) (class color))
917 (:foreground "red1"))
921 (:weight bold :underline t)))
922 "Face used for pressed buttons."
923 :group 'widget-faces)
924 (define-obsolete-face-alias 'widget-button-pressed-face
925 'widget-button-pressed "22.1")
927 (defvar widget-button-click-moves-point nil
928 "If non-nil, `widget-button-click' moves point to a button after invoking it.
929 If nil, point returns to its original position after invoking a button.")
931 (defun widget-button-click (event)
932 "Invoke the button that the mouse is pointing at."
934 (if (widget-event-point event)
935 (let* ((oevent event)
936 (mouse-1 (memq (event-basic-type event) '(mouse-1 down-mouse-1)))
937 (pos (widget-event-point event))
938 (start (event-start event))
939 (button (get-char-property
940 pos 'button (and (windowp (posn-window start))
941 (window-buffer (posn-window start)))))
943 (when (or (null button)
944 (catch 'button-press-cancelled
945 ;; Mouse click on a widget button. Do the following
946 ;; in a save-excursion so that the click on the button
947 ;; doesn't change point.
948 (save-selected-window
949 (select-window (posn-window (event-start event)))
951 (goto-char (posn-point (event-start event)))
952 (let* ((overlay (widget-get button :button-overlay))
953 (pressed-face (or (widget-get button :pressed-face)
954 widget-button-pressed-face))
955 (face (overlay-get overlay 'face))
956 (mouse-face (overlay-get overlay 'mouse-face)))
958 ;; Read events, including mouse-movement
959 ;; events, waiting for a release event. If we
960 ;; began with a mouse-1 event and receive a
961 ;; movement event, that means the user wants
962 ;; to perform drag-selection, so cancel the
963 ;; button press and do the default mouse-1
964 ;; action. For mouse-2, just highlight/
965 ;; unhighlight the button the mouse was
966 ;; initially on when we move over it.
968 (when face ; avoid changing around image
969 (overlay-put overlay 'face pressed-face)
970 (overlay-put overlay 'mouse-face pressed-face))
971 (unless (widget-apply button :mouse-down-action event)
972 (let ((track-mouse t))
973 (while (not (widget-button-release-event-p event))
974 (setq event (read-event))
975 (when (and mouse-1 (mouse-movement-p event))
976 (push event unread-command-events)
978 (throw 'button-press-cancelled t))
979 (unless (or (integerp event)
980 (memq (car event) '(switch-frame select-window))
981 (eq (car event) 'scroll-bar-movement))
982 (setq pos (widget-event-point event))
984 (eq (get-char-property pos 'button)
987 (overlay-put overlay 'face pressed-face)
988 (overlay-put overlay 'mouse-face pressed-face))
989 (overlay-put overlay 'face face)
990 (overlay-put overlay 'mouse-face mouse-face))))))
992 ;; When mouse is released over the button, run
993 ;; its action function.
994 (when (and pos (eq (get-char-property pos 'button) button))
996 (widget-apply-action button event)
997 (if widget-button-click-moves-point
998 (setq newpoint (point)))))
999 (overlay-put overlay 'face face)
1000 (overlay-put overlay 'mouse-face mouse-face))))
1002 (if newpoint (goto-char newpoint))
1003 ;; This loses if the widget action switches windows. -- cyd
1004 ;; (unless (pos-visible-in-window-p (widget-event-point event))
1005 ;; (mouse-set-point event)
1006 ;; (beginning-of-line)
1010 (let ((up t) command)
1011 ;; Mouse click not on a widget button. Find the global
1012 ;; command to run, and check whether it is bound to an
1015 (cond ((setq command ;down event
1016 (lookup-key widget-global-map [down-mouse-1]))
1018 ((setq command ;up event
1019 (lookup-key widget-global-map [mouse-1]))))
1020 (cond ((setq command ;down event
1021 (lookup-key widget-global-map [down-mouse-2]))
1023 ((setq command ;up event
1024 (lookup-key widget-global-map [mouse-2])))))
1026 ;; Don't execute up events twice.
1027 (while (not (widget-button-release-event-p event))
1028 (setq event (read-event))))
1030 (call-interactively command)))))
1031 (message "You clicked somewhere weird.")))
1033 (defun widget-button-press (pos &optional event)
1034 "Invoke button at POS."
1036 (let ((button (get-char-property pos 'button)))
1038 (widget-apply-action button event)
1039 (let ((command (lookup-key widget-global-map (this-command-keys))))
1040 (when (commandp command)
1041 (call-interactively command))))))
1043 (defun widget-tabable-at (&optional pos)
1044 "Return the tabable widget at POS, or nil.
1045 POS defaults to the value of (point)."
1046 (let ((widget (widget-at pos)))
1048 (let ((order (widget-get widget :tab-order)))
1054 (defvar widget-use-overlay-change t
1055 "If non-nil, use overlay change functions to tab around in the buffer.
1056 This is much faster, but doesn't work reliably on Emacs 19.34.")
1058 (defun widget-move (arg)
1059 "Move point to the ARG next field or button.
1060 ARG may be negative to move backward."
1061 (or (bobp) (> arg 0) (backward-char))
1064 (old (widget-tabable-at)))
1068 (goto-char (point-min))
1069 (setq wrapped (1+ wrapped)))
1070 (widget-use-overlay-change
1071 (goto-char (next-overlay-change (point))))
1076 (error "No buttons or fields found"))
1077 (let ((new (widget-tabable-at)))
1079 (unless (eq new old)
1085 (goto-char (point-max))
1086 (setq wrapped (1+ wrapped)))
1087 (widget-use-overlay-change
1088 (goto-char (previous-overlay-change (point))))
1093 (error "No buttons or fields found"))
1094 (let ((new (widget-tabable-at)))
1096 (unless (eq new old)
1097 (setq arg (1+ arg))))))
1098 (let ((new (widget-tabable-at)))
1099 (while (eq (widget-tabable-at) new)
1102 (widget-echo-help (point))
1103 (run-hooks 'widget-move-hook))
1105 (defun widget-forward (arg)
1106 "Move point to the next field or button.
1107 With optional ARG, move across that many fields."
1109 (run-hooks 'widget-forward-hook)
1112 (defun widget-backward (arg)
1113 "Move point to the previous field or button.
1114 With optional ARG, move across that many fields."
1116 (run-hooks 'widget-backward-hook)
1117 (widget-move (- arg)))
1119 ;; Since the widget code uses a `field' property to identify fields,
1120 ;; ordinary beginning-of-line does the right thing.
1121 (defalias 'widget-beginning-of-line 'beginning-of-line)
1123 (defun widget-end-of-line ()
1124 "Go to end of field or end of line, whichever is first.
1125 Trailing spaces at the end of padded fields are not considered part of
1128 ;; Ordinary end-of-line does the right thing, because we're inside
1129 ;; text with a `field' property.
1132 ;; ... except that we want to ignore trailing spaces in fields that
1133 ;; aren't terminated by a newline, because they are used as padding,
1134 ;; and ignored when extracting the entered value of the field.
1135 (skip-chars-backward " " (field-beginning (1- (point))))))
1137 (defun widget-kill-line ()
1138 "Kill to end of field or end of line, whichever is first."
1140 (let* ((field (widget-field-find (point)))
1141 (end (and field (widget-field-end field))))
1142 (if (and field (> (line-beginning-position 2) end))
1143 (kill-region (point) end)
1144 (call-interactively 'kill-line))))
1146 (defcustom widget-complete-field (lookup-key global-map "\M-\t")
1147 "Default function to call for completion inside fields."
1148 :options '(ispell-complete-word complete-tag lisp-complete-symbol)
1152 (defun widget-narrow-to-field ()
1155 (let ((field (widget-field-find (point))))
1157 (narrow-to-region (line-beginning-position) (line-end-position)))))
1159 ;; This used to say:
1160 ;; "When not inside a field, move to the previous button or field."
1161 ;; but AFAICS, it has always just thrown an error.
1162 (defun widget-complete ()
1163 "Complete content of editable field from point.
1164 When not inside a field, signal an error."
1166 (let ((field (widget-field-find (point))))
1168 (widget-apply field :complete)
1169 (error "Not in an editable field"))))
1171 ;;; Setting up the buffer.
1173 (defvar widget-field-new nil
1174 "List of all newly created editable fields in the buffer.")
1175 (make-variable-buffer-local 'widget-field-new)
1177 (defvar widget-field-list nil
1178 "List of all editable fields in the buffer.")
1179 (make-variable-buffer-local 'widget-field-list)
1181 (defun widget-at (&optional pos)
1182 "The button or field at POS (default, point)."
1183 (or (get-char-property (or pos (point)) 'button)
1184 (widget-field-at pos)))
1187 (defun widget-setup ()
1188 "Setup current buffer so editing string widgets works."
1189 (let ((inhibit-read-only t)
1190 (inhibit-modification-hooks t)
1192 (while widget-field-new
1193 (setq field (car widget-field-new)
1194 widget-field-new (cdr widget-field-new)
1195 widget-field-list (cons field widget-field-list))
1196 (let ((from (car (widget-get field :field-overlay)))
1197 (to (cdr (widget-get field :field-overlay))))
1198 (widget-specify-field field
1199 (marker-position from) (marker-position to))
1200 (set-marker from nil)
1201 (set-marker to nil))))
1203 (widget-add-change))
1205 (defvar widget-field-last nil)
1206 ;; Last field containing point.
1207 (make-variable-buffer-local 'widget-field-last)
1209 (defvar widget-field-was nil)
1210 ;; The widget data before the change.
1211 (make-variable-buffer-local 'widget-field-was)
1213 (defun widget-field-at (pos)
1214 "Return the widget field at POS, or nil if none."
1215 (let ((field (get-char-property (or pos (point)) 'field)))
1216 (if (eq field 'boundary)
1217 (get-char-property (or pos (point)) 'real-field)
1220 (defun widget-field-buffer (widget)
1221 "Return the buffer of WIDGET's editing field."
1222 (let ((overlay (widget-get widget :field-overlay)))
1223 (cond ((overlayp overlay)
1224 (overlay-buffer overlay))
1226 (marker-buffer (car overlay))))))
1228 (defun widget-field-start (widget)
1229 "Return the start of WIDGET's editing field."
1230 (let ((overlay (widget-get widget :field-overlay)))
1231 (if (overlayp overlay)
1232 (overlay-start overlay)
1235 (defun widget-field-end (widget)
1236 "Return the end of WIDGET's editing field."
1237 (let ((overlay (widget-get widget :field-overlay)))
1238 ;; Don't subtract one if local-map works at the end of the overlay,
1239 ;; or if a special `boundary' field has been added after the widget
1241 (if (overlayp overlay)
1242 ;; Don't proceed if overlay has been removed from buffer.
1243 (when (overlay-buffer overlay)
1244 (if (and (not (eq (with-current-buffer
1245 (widget-field-buffer widget)
1247 ;; `widget-narrow-to-field' can be
1248 ;; active when this function is called
1249 ;; from an change-functions hook. So
1250 ;; temporarily remove field narrowing
1251 ;; before to call `get-char-property'.
1253 (get-char-property (overlay-end overlay)
1256 (or widget-field-add-space
1257 (null (widget-get widget :size))))
1258 (1- (overlay-end overlay))
1259 (overlay-end overlay)))
1262 (defun widget-field-text-end (widget)
1263 (let ((to (widget-field-end widget))
1264 (size (widget-get widget :size)))
1265 (if (or (null size) (zerop size))
1267 (let ((from (widget-field-start widget)))
1269 (with-current-buffer (widget-field-buffer widget)
1270 (while (and (> to from)
1271 (eq (char-after (1- to)) ?\s))
1275 (defun widget-field-find (pos)
1276 "Return the field at POS.
1277 Unlike (get-char-property POS 'field), this works with empty fields too."
1278 (let ((fields widget-field-list)
1281 (setq field (car fields)
1282 fields (cdr fields))
1283 (when (and (<= (widget-field-start field) pos)
1284 (<= pos (widget-field-end field)))
1286 (error "Overlapping fields"))
1287 (setq found field)))
1290 (defun widget-before-change (from to)
1291 ;; This is how, for example, a variable changes its state to `modified'.
1292 ;; when it is being edited.
1293 (unless inhibit-read-only
1294 (let ((from-field (widget-field-find from))
1295 (to-field (widget-field-find to)))
1296 (cond ((not (eq from-field to-field))
1297 (add-hook 'post-command-hook 'widget-add-change nil t)
1298 (signal 'text-read-only
1299 '("Change should be restricted to a single field")))
1301 (add-hook 'post-command-hook 'widget-add-change nil t)
1302 (signal 'text-read-only
1303 '("Attempt to change text outside editable field")))
1304 (widget-field-use-before-change
1305 (widget-apply from-field :notify from-field))))))
1307 (defun widget-add-change ()
1308 (remove-hook 'post-command-hook 'widget-add-change t)
1309 (add-hook 'before-change-functions 'widget-before-change nil t)
1310 (add-hook 'after-change-functions 'widget-after-change nil t))
1312 (defun widget-after-change (from to old)
1313 "Adjust field size and text properties."
1314 (let ((field (widget-field-find from))
1315 (other (widget-field-find to)))
1317 (unless (eq field other)
1318 (error "Change in different fields"))
1319 (let ((size (widget-get field :size)))
1321 (let ((begin (widget-field-start field))
1322 (end (widget-field-end field)))
1323 (cond ((< (- end begin) size)
1327 (insert-char ?\s (- (+ begin size) end))))
1328 ((> (- end begin) size)
1329 ;; Field too large and
1330 (if (or (< (point) (+ begin size))
1332 ;; Point is outside extra space.
1333 (setq begin (+ begin size))
1334 ;; Point is within the extra space.
1335 (setq begin (point)))
1338 (while (and (eq (preceding-char) ?\s)
1340 (delete-char -1)))))))
1341 (widget-specify-secret field))
1342 (widget-apply field :notify field))))
1344 ;;; Widget Functions
1346 ;; These functions are used in the definition of multiple widgets.
1348 (defun widget-parent-action (widget &optional event)
1349 "Tell :parent of WIDGET to handle the :action.
1350 Optional EVENT is the event that triggered the action."
1351 (widget-apply (widget-get widget :parent) :action event))
1353 (defun widget-children-value-delete (widget)
1354 "Delete all :children and :buttons in WIDGET."
1355 (mapc 'widget-delete (widget-get widget :children))
1356 (widget-put widget :children nil)
1357 (mapc 'widget-delete (widget-get widget :buttons))
1358 (widget-put widget :buttons nil))
1360 (defun widget-children-validate (widget)
1361 "All the :children must be valid."
1362 (let ((children (widget-get widget :children))
1364 (while (and children (not found))
1365 (setq child (car children)
1366 children (cdr children)
1367 found (widget-apply child :validate)))
1370 (defun widget-child-value-get (widget)
1371 "Get the value of the first member of :children in WIDGET."
1372 (widget-value (car (widget-get widget :children))))
1374 (defun widget-child-value-inline (widget)
1375 "Get the inline value of the first member of :children in WIDGET."
1376 (widget-apply (car (widget-get widget :children)) :value-inline))
1378 (defun widget-child-validate (widget)
1379 "The result of validating the first member of :children in WIDGET."
1380 (widget-apply (car (widget-get widget :children)) :validate))
1382 (defun widget-type-value-create (widget)
1383 "Convert and instantiate the value of the :type attribute of WIDGET.
1384 Store the newly created widget in the :children attribute.
1386 The value of the :type attribute should be an unconverted widget type."
1387 (let ((value (widget-get widget :value))
1388 (type (widget-get widget :type)))
1389 (widget-put widget :children
1390 (list (widget-create-child-value widget
1391 (widget-convert type)
1394 (defun widget-type-default-get (widget)
1395 "Get default value from the :type attribute of WIDGET.
1397 The value of the :type attribute should be an unconverted widget type."
1398 (widget-default-get (widget-convert (widget-get widget :type))))
1400 (defun widget-type-match (widget value)
1401 "Non-nil if the :type value of WIDGET matches VALUE.
1403 The value of the :type attribute should be an unconverted widget type."
1404 (widget-apply (widget-convert (widget-get widget :type)) :match value))
1406 (defun widget-types-copy (widget)
1407 "Copy :args as widget types in WIDGET."
1408 (widget-put widget :args (mapcar 'widget-copy (widget-get widget :args)))
1411 ;; Made defsubst to speed up face editor creation.
1412 (defsubst widget-types-convert-widget (widget)
1413 "Convert :args as widget types in WIDGET."
1414 (widget-put widget :args (mapcar 'widget-convert (widget-get widget :args)))
1417 (defun widget-value-convert-widget (widget)
1418 "Initialize :value from :args in WIDGET."
1419 (let ((args (widget-get widget :args)))
1421 (widget-put widget :value (car args))
1422 ;; Don't convert :value here, as this is done in `widget-convert'.
1423 ;; (widget-put widget :value (widget-apply widget
1424 ;; :value-to-internal (car args)))
1425 (widget-put widget :args nil)))
1428 (defun widget-value-value-get (widget)
1429 "Return the :value property of WIDGET."
1430 (widget-get widget :value))
1432 ;;; The `default' Widget.
1434 (define-widget 'default nil
1435 "Basic widget other widgets are derived from."
1436 :value-to-internal (lambda (widget value) value)
1437 :value-to-external (lambda (widget value) value)
1438 :button-prefix 'widget-button-prefix
1439 :button-suffix 'widget-button-suffix
1440 :complete 'widget-default-complete
1441 :create 'widget-default-create
1444 :format-handler 'widget-default-format-handler
1445 :button-face-get 'widget-default-button-face-get
1446 :mouse-face-get 'widget-default-mouse-face-get
1447 :sample-face-get 'widget-default-sample-face-get
1448 :delete 'widget-default-delete
1450 :value-set 'widget-default-value-set
1451 :value-inline 'widget-default-value-inline
1452 :value-delete 'ignore
1453 :default-get 'widget-default-default-get
1454 :menu-tag-get 'widget-default-menu-tag-get
1456 :active 'widget-default-active
1457 :activate 'widget-specify-active
1458 :deactivate 'widget-default-deactivate
1459 :mouse-down-action #'ignore
1460 :action 'widget-default-action
1461 :notify 'widget-default-notify
1462 :prompt-value 'widget-default-prompt-value)
1464 (defun widget-default-complete (widget)
1465 "Call the value of the :complete-function property of WIDGET.
1466 If that does not exist, call the value of `widget-complete-field'."
1467 (call-interactively (or (widget-get widget :complete-function)
1468 widget-complete-field)))
1470 (defun widget-default-create (widget)
1471 "Create WIDGET at point in the current buffer."
1472 (widget-specify-insert
1473 (let ((from (point))
1474 button-begin button-end
1475 sample-begin sample-end
1478 (insert (widget-get widget :format))
1480 ;; Parse escapes in format.
1481 (while (re-search-forward "%\\(.\\)" nil t)
1482 (let ((escape (char-after (match-beginning 1))))
1484 (cond ((eq escape ?%)
1487 (setq button-begin (point))
1488 (insert (widget-get-indirect widget :button-prefix)))
1490 (insert (widget-get-indirect widget :button-suffix))
1491 (setq button-end (point)))
1493 (setq sample-begin (point)))
1495 (setq sample-end (point)))
1497 (when (widget-get widget :indent)
1499 (insert-char ?\s (widget-get widget :indent))))
1501 (let ((image (widget-get widget :tag-glyph))
1502 (tag (widget-get widget :tag)))
1504 (widget-image-insert widget (or tag "image") image))
1508 (princ (widget-get widget :value)
1509 (current-buffer))))))
1511 (let ((doc (widget-get widget :doc)))
1513 (setq doc-begin (point))
1515 (while (eq (preceding-char) ?\n)
1518 (setq doc-end (point)))))
1520 (widget-add-documentation-string-button widget))
1522 (if (and button-begin (not button-end))
1523 (widget-apply widget :value-create)
1524 (setq value-pos (point))))
1526 (widget-apply widget :format-handler escape)))))
1527 ;; Specify button, sample, and doc, and insert value.
1528 (and button-begin button-end
1529 (widget-specify-button widget button-begin button-end))
1530 (and sample-begin sample-end
1531 (widget-specify-sample widget sample-begin sample-end))
1532 (and doc-begin doc-end
1533 (widget-specify-doc widget doc-begin doc-end))
1535 (goto-char value-pos)
1536 (widget-apply widget :value-create)))
1537 (let ((from (point-min-marker))
1538 (to (point-max-marker)))
1539 (set-marker-insertion-type from t)
1540 (set-marker-insertion-type to nil)
1541 (widget-put widget :from from)
1542 (widget-put widget :to to)))
1543 (widget-clear-undo))
1545 (defun widget-default-format-handler (widget escape)
1546 (error "Unknown escape `%c'" escape))
1548 (defun widget-default-button-face-get (widget)
1549 ;; Use :button-face or widget-button-face
1550 (or (widget-get widget :button-face)
1551 (let ((parent (widget-get widget :parent)))
1553 (widget-apply parent :button-face-get)
1554 widget-button-face))))
1556 (defun widget-default-mouse-face-get (widget)
1557 ;; Use :mouse-face or widget-mouse-face
1558 (or (widget-get widget :mouse-face)
1559 (let ((parent (widget-get widget :parent)))
1561 (widget-apply parent :mouse-face-get)
1562 widget-mouse-face))))
1564 (defun widget-default-sample-face-get (widget)
1565 ;; Use :sample-face.
1566 (widget-get widget :sample-face))
1568 (defun widget-default-delete (widget)
1569 "Remove widget from the buffer."
1570 (let ((from (widget-get widget :from))
1571 (to (widget-get widget :to))
1572 (inactive-overlay (widget-get widget :inactive))
1573 (button-overlay (widget-get widget :button-overlay))
1574 (sample-overlay (widget-get widget :sample-overlay))
1575 (doc-overlay (widget-get widget :doc-overlay))
1576 (inhibit-modification-hooks t)
1577 (inhibit-read-only t))
1578 (widget-apply widget :value-delete)
1579 (widget-children-value-delete widget)
1580 (when inactive-overlay
1581 (delete-overlay inactive-overlay))
1582 (when button-overlay
1583 (delete-overlay button-overlay))
1584 (when sample-overlay
1585 (delete-overlay sample-overlay))
1587 (delete-overlay doc-overlay))
1589 ;; Kludge: this doesn't need to be true for empty formats.
1590 (delete-region from to))
1591 (set-marker from nil)
1592 (set-marker to nil))
1593 (widget-clear-undo))
1595 (defun widget-default-value-set (widget value)
1596 "Recreate widget with new value."
1597 (let* ((old-pos (point))
1598 (from (copy-marker (widget-get widget :from)))
1599 (to (copy-marker (widget-get widget :to)))
1600 (offset (if (and (<= from old-pos) (<= old-pos to))
1601 (if (>= old-pos (1- to))
1603 (- old-pos from)))))
1604 ;;??? Bug: this ought to insert the new value before deleting the old one,
1605 ;; so that markers on either side of the value automatically
1606 ;; stay on the same side. -- rms.
1608 (goto-char (widget-get widget :from))
1609 (widget-apply widget :delete)
1610 (widget-put widget :value value)
1611 (widget-apply widget :create))
1614 (goto-char (+ (widget-get widget :to) offset 1))
1615 (goto-char (min (+ from offset) (1- (widget-get widget :to))))))))
1617 (defun widget-default-value-inline (widget)
1618 "Wrap value in a list unless it is inline."
1619 (if (widget-get widget :inline)
1620 (widget-value widget)
1621 (list (widget-value widget))))
1623 (defun widget-default-default-get (widget)
1625 (widget-get widget :value))
1627 (defun widget-default-menu-tag-get (widget)
1628 "Use tag or value for menus."
1629 (or (widget-get widget :menu-tag)
1630 (widget-get widget :tag)
1631 (widget-princ-to-string (widget-get widget :value))))
1633 (defun widget-default-active (widget)
1634 "Return t if this widget is active (user modifiable)."
1635 (or (widget-get widget :always-active)
1636 (and (not (widget-get widget :inactive))
1637 (let ((parent (widget-get widget :parent)))
1639 (widget-apply parent :active))))))
1641 (defun widget-default-deactivate (widget)
1642 "Make WIDGET inactive for user modifications."
1643 (widget-specify-inactive widget
1644 (widget-get widget :from)
1645 (widget-get widget :to)))
1647 (defun widget-default-action (widget &optional event)
1648 "Notify the parent when a widget changes."
1649 (let ((parent (widget-get widget :parent)))
1651 (widget-apply parent :notify widget event))))
1653 (defun widget-default-notify (widget child &optional event)
1654 "Pass notification to parent."
1655 (widget-default-action widget event))
1657 (defun widget-default-prompt-value (widget prompt value unbound)
1658 "Read an arbitrary value."
1659 (eval-minibuffer prompt))
1661 (defun widget-docstring (widget)
1662 "Return the documentation string specificied by WIDGET, or nil if none.
1663 If WIDGET has a `:doc' property, that specifies the documentation string.
1664 Otherwise, try the `:documentation-property' property. If this
1665 is a function, call it with the widget's value as an argument; if
1666 it is a symbol, use this symbol together with the widget's value
1667 as the argument to `documentation-property'."
1668 (let ((doc (or (widget-get widget :doc)
1669 (let ((doc-prop (widget-get widget :documentation-property))
1670 (value (widget-get widget :value)))
1671 (cond ((functionp doc-prop)
1672 (funcall doc-prop value))
1674 (documentation-property value doc-prop)))))))
1675 (when (and (stringp doc) (> (length doc) 0))
1676 ;; Remove any redundant `*' in the beginning.
1677 (when (eq (aref doc 0) ?*)
1678 (setq doc (substring doc 1)))
1679 ;; Remove trailing newlines.
1680 (when (string-match "\n+\\'" doc)
1681 (setq doc (substring doc 0 (match-beginning 0))))
1684 ;;; The `item' Widget.
1686 (define-widget 'item 'default
1687 "Constant items for inclusion in other widgets."
1688 :convert-widget 'widget-value-convert-widget
1689 :value-create 'widget-item-value-create
1690 :value-delete 'ignore
1691 :value-get 'widget-value-value-get
1692 :match 'widget-item-match
1693 :match-inline 'widget-item-match-inline
1694 :action 'widget-item-action
1697 (defun widget-item-value-create (widget)
1698 "Insert the printed representation of the value."
1699 (princ (widget-get widget :value) (current-buffer)))
1701 (defun widget-item-match (widget value)
1702 ;; Match if the value is the same.
1703 (equal (widget-get widget :value) value))
1705 (defun widget-item-match-inline (widget values)
1706 ;; Match if the value is the same.
1707 (let ((value (widget-get widget :value)))
1709 (<= (length value) (length values))
1710 (let ((head (widget-sublist values 0 (length value))))
1711 (and (equal head value)
1712 (cons head (widget-sublist values (length value))))))))
1714 (defun widget-sublist (list start &optional end)
1715 "Return the sublist of LIST from START to END.
1716 If END is omitted, it defaults to the length of LIST."
1717 (if (> start 0) (setq list (nthcdr start list)))
1719 (unless (<= end start)
1720 (setq list (copy-sequence list))
1721 (setcdr (nthcdr (- end start 1) list) nil)
1723 (copy-sequence list)))
1725 (defun widget-item-action (widget &optional event)
1726 ;; Just notify itself.
1727 (widget-apply widget :notify widget event))
1729 ;;; The `push-button' Widget.
1731 ;; (defcustom widget-push-button-gui t
1732 ;; "If non-nil, use GUI push buttons when available."
1736 ;; Cache already created GUI objects.
1737 ;; (defvar widget-push-button-cache nil)
1739 (defcustom widget-push-button-prefix "["
1740 "String used as prefix for buttons."
1742 :group 'widget-button)
1744 (defcustom widget-push-button-suffix "]"
1745 "String used as suffix for buttons."
1747 :group 'widget-button)
1749 (define-widget 'push-button 'item
1750 "A pushable button."
1753 :value-create 'widget-push-button-value-create
1756 (defun widget-push-button-value-create (widget)
1757 "Insert text representing the `on' and `off' states."
1758 (let* ((tag (or (widget-get widget :tag)
1759 (widget-get widget :value)))
1760 (tag-glyph (widget-get widget :tag-glyph))
1761 (text (concat widget-push-button-prefix
1762 tag widget-push-button-suffix)))
1764 (widget-image-insert widget text tag-glyph)
1767 ;; (defun widget-gui-action (widget)
1768 ;; "Apply :action for WIDGET."
1769 ;; (widget-apply-action widget (this-command-keys)))
1771 ;;; The `link' Widget.
1773 (defcustom widget-link-prefix "["
1774 "String used as prefix for links."
1776 :group 'widget-button)
1778 (defcustom widget-link-suffix "]"
1779 "String used as suffix for links."
1781 :group 'widget-button)
1783 (define-widget 'link 'item
1785 :button-prefix 'widget-link-prefix
1786 :button-suffix 'widget-link-suffix
1787 :follow-link 'mouse-face
1788 :help-echo "Follow the link."
1791 ;;; The `info-link' Widget.
1793 (define-widget 'info-link 'link
1794 "A link to an info file."
1795 :action 'widget-info-link-action)
1797 (defun widget-info-link-action (widget &optional event)
1798 "Open the info node specified by WIDGET."
1799 (info (widget-value widget)))
1801 ;;; The `url-link' Widget.
1803 (define-widget 'url-link 'link
1804 "A link to an www page."
1805 :action 'widget-url-link-action)
1807 (defun widget-url-link-action (widget &optional event)
1808 "Open the URL specified by WIDGET."
1809 (browse-url (widget-value widget)))
1811 ;;; The `function-link' Widget.
1813 (define-widget 'function-link 'link
1814 "A link to an Emacs function."
1815 :action 'widget-function-link-action)
1817 (defun widget-function-link-action (widget &optional event)
1818 "Show the function specified by WIDGET."
1819 (describe-function (widget-value widget)))
1821 ;;; The `variable-link' Widget.
1823 (define-widget 'variable-link 'link
1824 "A link to an Emacs variable."
1825 :action 'widget-variable-link-action)
1827 (defun widget-variable-link-action (widget &optional event)
1828 "Show the variable specified by WIDGET."
1829 (describe-variable (widget-value widget)))
1831 ;;; The `file-link' Widget.
1833 (define-widget 'file-link 'link
1835 :action 'widget-file-link-action)
1837 (defun widget-file-link-action (widget &optional event)
1838 "Find the file specified by WIDGET."
1839 (find-file (widget-value widget)))
1841 ;;; The `emacs-library-link' Widget.
1843 (define-widget 'emacs-library-link 'link
1844 "A link to an Emacs Lisp library file."
1845 :action 'widget-emacs-library-link-action)
1847 (defun widget-emacs-library-link-action (widget &optional event)
1848 "Find the Emacs library file specified by WIDGET."
1849 (find-file (locate-library (widget-value widget))))
1851 ;;; The `emacs-commentary-link' Widget.
1853 (define-widget 'emacs-commentary-link 'link
1854 "A link to Commentary in an Emacs Lisp library file."
1855 :action 'widget-emacs-commentary-link-action)
1857 (defun widget-emacs-commentary-link-action (widget &optional event)
1858 "Find the Commentary section of the Emacs file specified by WIDGET."
1859 (finder-commentary (widget-value widget)))
1861 ;;; The `editable-field' Widget.
1863 (define-widget 'editable-field 'default
1864 "An editable text field.
1865 Note: In an `editable-field' widget, the `%v' escape must be preceded
1866 by some other text in the `:format' string (if specified)."
1867 :convert-widget 'widget-value-convert-widget
1868 :keymap widget-field-keymap
1870 :help-echo "M-TAB: complete field; RET: enter value"
1872 :prompt-internal 'widget-field-prompt-internal
1873 :prompt-history 'widget-field-history
1874 :prompt-value 'widget-field-prompt-value
1875 :action 'widget-field-action
1876 :validate 'widget-field-validate
1878 :error "Field's value doesn't match allowed forms"
1879 :value-create 'widget-field-value-create
1880 :value-set 'widget-field-value-set
1881 :value-delete 'widget-field-value-delete
1882 :value-get 'widget-field-value-get
1883 :match 'widget-field-match)
1885 (defvar widget-field-history nil
1886 "History of field minibuffer edits.")
1888 (defun widget-field-prompt-internal (widget prompt initial history)
1889 "Read string for WIDGET prompting with PROMPT.
1890 INITIAL is the initial input and HISTORY is a symbol containing
1892 (read-string prompt initial history))
1894 (defun widget-field-prompt-value (widget prompt value unbound)
1895 "Prompt for a string."
1896 (widget-apply widget
1898 (widget-apply widget
1899 :prompt-internal prompt
1901 (cons (widget-apply widget
1902 :value-to-internal value)
1904 (widget-get widget :prompt-history))))
1906 (defvar widget-edit-functions nil)
1908 (defun widget-field-action (widget &optional event)
1909 "Move to next field."
1911 (run-hook-with-args 'widget-edit-functions widget))
1913 (defun widget-field-validate (widget)
1914 "Valid if the content matches `:valid-regexp'."
1915 (unless (string-match (widget-get widget :valid-regexp)
1916 (widget-apply widget :value-get))
1919 (defun widget-field-value-set (widget value)
1920 "Set an editable text field WIDGET to VALUE"
1921 (let ((from (widget-field-start widget))
1922 (to (widget-field-text-end widget))
1923 (buffer (widget-field-buffer widget))
1924 (size (widget-get widget :size)))
1925 (when (and from to (buffer-live-p buffer))
1926 (with-current-buffer buffer
1928 (delete-char (- to from))
1931 (defun widget-field-value-create (widget)
1932 "Create an editable text field."
1933 (let ((size (widget-get widget :size))
1934 (value (widget-get widget :value))
1936 ;; This is changed to a real overlay in `widget-setup'. We
1937 ;; need the end points to behave differently until
1938 ;; `widget-setup' is called.
1939 (overlay (cons (make-marker) (make-marker))))
1940 (widget-put widget :field-overlay overlay)
1943 (< (length value) size)
1944 (insert-char ?\s (- size (length value))))
1945 (unless (memq widget widget-field-list)
1946 (setq widget-field-new (cons widget widget-field-new)))
1947 (move-marker (cdr overlay) (point))
1948 (set-marker-insertion-type (cdr overlay) nil)
1951 (move-marker (car overlay) from)
1952 (set-marker-insertion-type (car overlay) t)))
1954 (defun widget-field-value-delete (widget)
1955 "Remove the widget from the list of active editing fields."
1956 (setq widget-field-list (delq widget widget-field-list))
1957 (setq widget-field-new (delq widget widget-field-new))
1958 ;; These are nil if the :format string doesn't contain `%v'.
1959 (let ((overlay (widget-get widget :field-overlay)))
1960 (when (overlayp overlay)
1961 (delete-overlay overlay))))
1963 (defun widget-field-value-get (widget)
1964 "Return current text in editing field."
1965 (let ((from (widget-field-start widget))
1966 (to (widget-field-text-end widget))
1967 (buffer (widget-field-buffer widget))
1968 (secret (widget-get widget :secret))
1969 (old (current-buffer)))
1973 (let ((result (buffer-substring-no-properties from to)))
1976 (while (< (+ from index) to)
1978 (get-char-property (+ from index) 'secret))
1979 (setq index (1+ index)))))
1982 (widget-get widget :value))))
1984 (defun widget-field-match (widget value)
1985 ;; Match any string.
1988 ;;; The `text' Widget.
1990 (define-widget 'text 'editable-field
1991 "A multiline text area."
1992 :keymap widget-text-keymap)
1994 ;;; The `menu-choice' Widget.
1996 (define-widget 'menu-choice 'default
1997 "A menu of options."
1998 :convert-widget 'widget-types-convert-widget
1999 :copy 'widget-types-copy
2000 :format "%[%t%]: %v"
2003 :void '(item :format "invalid (%t)\n")
2004 :value-create 'widget-choice-value-create
2005 :value-get 'widget-child-value-get
2006 :value-inline 'widget-child-value-inline
2007 :default-get 'widget-choice-default-get
2008 :mouse-down-action 'widget-choice-mouse-down-action
2009 :action 'widget-choice-action
2010 :error "Make a choice"
2011 :validate 'widget-choice-validate
2012 :match 'widget-choice-match
2013 :match-inline 'widget-choice-match-inline)
2015 (defun widget-choice-value-create (widget)
2016 "Insert the first choice that matches the value."
2017 (let ((value (widget-get widget :value))
2018 (args (widget-get widget :args))
2019 (explicit (widget-get widget :explicit-choice))
2023 ;; If the user specified the choice for this value,
2024 ;; respect that choice.
2025 (widget-put widget :children (list (widget-create-child-value
2026 widget explicit value)))
2027 (widget-put widget :choice explicit)
2028 (widget-put widget :explicit-choice nil))
2030 (setq current (car args)
2032 (when (widget-apply current :match value)
2033 (widget-put widget :children (list (widget-create-child-value
2034 widget current value)))
2035 (widget-put widget :choice current)
2039 (let ((void (widget-get widget :void)))
2040 (widget-put widget :children (list (widget-create-child-and-convert
2041 widget void :value value)))
2042 (widget-put widget :choice void))))))
2044 (defun widget-choice-default-get (widget)
2045 ;; Get default for the first choice.
2046 (widget-default-get (car (widget-get widget :args))))
2048 (defcustom widget-choice-toggle nil
2049 "If non-nil, a binary choice will just toggle between the values.
2050 Otherwise, the user will explicitly have to choose between the values
2051 when he invoked the menu."
2055 (defun widget-choice-mouse-down-action (widget &optional event)
2056 ;; Return non-nil if we need a menu.
2057 (let ((args (widget-get widget :args))
2058 (old (widget-get widget :choice)))
2059 (cond ((not (display-popup-menus-p))
2060 ;; No place to pop up a menu.
2062 ((< (length args) 2)
2063 ;; Empty or singleton list, just return the value.
2065 ((> (length args) widget-menu-max-size)
2066 ;; Too long, prompt.
2068 ((> (length args) 2)
2069 ;; Reasonable sized list, use menu.
2071 ((and widget-choice-toggle (memq old args))
2075 ;; Ask which of the two.
2078 (defun widget-choice-action (widget &optional event)
2080 (let ((args (widget-get widget :args))
2081 (old (widget-get widget :choice))
2082 (tag (widget-apply widget :menu-tag-get))
2083 (completion-ignore-case (widget-get widget :case-fold))
2086 ;; Remember old value.
2087 (if (and old (not (widget-apply widget :validate)))
2088 (let* ((external (widget-value widget))
2089 (internal (widget-apply old :value-to-internal external)))
2090 (widget-put old :value internal)))
2093 (cond ((= (length args) 0)
2095 ((= (length args) 1)
2097 ((and widget-choice-toggle
2100 (if (eq old (nth 0 args))
2105 (setq current (car args)
2108 (cons (cons (widget-apply current :menu-tag-get)
2111 (setq this-explicit t)
2112 (widget-choose tag (reverse choices) event))))
2114 ;; If this was an explicit user choice, record the choice,
2115 ;; so that widget-choice-value-create will respect it.
2117 (widget-put widget :explicit-choice current))
2118 (widget-value-set widget (widget-default-get current))
2120 (widget-apply widget :notify widget event)))
2121 (run-hook-with-args 'widget-edit-functions widget))
2123 (defun widget-choice-validate (widget)
2124 ;; Valid if we have made a valid choice.
2125 (if (eq (widget-get widget :void) (widget-get widget :choice))
2127 (widget-apply (car (widget-get widget :children)) :validate)))
2129 (defun widget-choice-match (widget value)
2130 ;; Matches if one of the choices matches.
2131 (let ((args (widget-get widget :args))
2133 (while (and args (not found))
2134 (setq current (car args)
2136 found (widget-apply current :match value)))
2139 (defun widget-choice-match-inline (widget values)
2140 ;; Matches if one of the choices matches.
2141 (let ((args (widget-get widget :args))
2143 (while (and args (null found))
2144 (setq current (car args)
2146 found (widget-match-inline current values)))
2149 ;;; The `toggle' Widget.
2151 (define-widget 'toggle 'item
2152 "Toggle between two states."
2154 :value-create 'widget-toggle-value-create
2155 :action 'widget-toggle-action
2156 :match (lambda (widget value) t)
2160 (defun widget-toggle-value-create (widget)
2161 "Insert text representing the `on' and `off' states."
2162 (if (widget-value widget)
2163 (let ((image (widget-get widget :on-glyph)))
2164 (and (display-graphic-p)
2166 (not (eq (car image) 'image))
2167 (widget-put widget :on-glyph (setq image (eval image))))
2168 (widget-image-insert widget
2169 (widget-get widget :on)
2171 (let ((image (widget-get widget :off-glyph)))
2172 (and (display-graphic-p)
2174 (not (eq (car image) 'image))
2175 (widget-put widget :off-glyph (setq image (eval image))))
2176 (widget-image-insert widget (widget-get widget :off) image))))
2178 (defun widget-toggle-action (widget &optional event)
2180 (widget-value-set widget (not (widget-value widget)))
2181 (widget-apply widget :notify widget event)
2182 (run-hook-with-args 'widget-edit-functions widget))
2184 ;;; The `checkbox' Widget.
2186 (define-widget 'checkbox 'toggle
2187 "A checkbox toggle."
2192 ;; We could probably do the same job as the images using single
2193 ;; space characters in a boxed face with a stretch specification to
2194 ;; make them square.
2195 :on-glyph '(create-image "\300\300\141\143\067\076\034\030"
2196 'xbm t :width 8 :height 8
2197 :background "grey75" ; like default mode line
2202 :off-glyph '(create-image (make-string 8 0)
2203 'xbm t :width 8 :height 8
2204 :background "grey75"
2208 :help-echo "Toggle this item."
2209 :action 'widget-checkbox-action)
2211 (defun widget-checkbox-action (widget &optional event)
2212 "Toggle checkbox, notify parent, and set active state of sibling."
2213 (widget-toggle-action widget event)
2214 (let ((sibling (widget-get-sibling widget)))
2216 (if (widget-value widget)
2217 (widget-apply sibling :activate)
2218 (widget-apply sibling :deactivate))
2219 (widget-clear-undo))))
2221 ;;; The `checklist' Widget.
2223 (define-widget 'checklist 'default
2224 "A multiple choice widget."
2225 :convert-widget 'widget-types-convert-widget
2226 :copy 'widget-types-copy
2229 :entry-format "%b %v"
2231 :value-create 'widget-checklist-value-create
2232 :value-get 'widget-checklist-value-get
2233 :validate 'widget-checklist-validate
2234 :match 'widget-checklist-match
2235 :match-inline 'widget-checklist-match-inline)
2237 (defun widget-checklist-value-create (widget)
2238 ;; Insert all values
2239 (let ((alist (widget-checklist-match-find widget (widget-get widget :value)))
2240 (args (widget-get widget :args)))
2242 (widget-checklist-add-item widget (car args) (assq (car args) alist))
2243 (setq args (cdr args)))
2244 (widget-put widget :children (nreverse (widget-get widget :children)))))
2246 (defun widget-checklist-add-item (widget type chosen)
2247 "Create checklist item in WIDGET of type TYPE.
2248 If the item is checked, CHOSEN is a cons whose cdr is the value."
2249 (and (eq (preceding-char) ?\n)
2250 (widget-get widget :indent)
2251 (insert-char ?\s (widget-get widget :indent)))
2252 (widget-specify-insert
2253 (let* ((children (widget-get widget :children))
2254 (buttons (widget-get widget :buttons))
2255 (button-args (or (widget-get type :sibling-args)
2256 (widget-get widget :button-args)))
2259 (insert (widget-get widget :entry-format))
2261 ;; Parse % escapes in format.
2262 (while (re-search-forward "%\\([bv%]\\)" nil t)
2263 (let ((escape (char-after (match-beginning 1))))
2265 (cond ((eq escape ?%)
2268 (setq button (apply 'widget-create-child-and-convert
2270 :value (not (null chosen))
2275 (let ((child (widget-create-child widget type)))
2276 (widget-apply child :deactivate)
2278 ((widget-get type :inline)
2279 (widget-create-child-value
2280 widget type (cdr chosen)))
2282 (widget-create-child-value
2283 widget type (car (cdr chosen)))))))
2285 (error "Unknown escape `%c'" escape)))))
2286 ;; Update properties.
2287 (and button child (widget-put child :button button))
2288 (and button (widget-put widget :buttons (cons button buttons)))
2289 (and child (widget-put widget :children (cons child children))))))
2291 (defun widget-checklist-match (widget values)
2292 ;; All values must match a type in the checklist.
2294 (null (cdr (widget-checklist-match-inline widget values)))))
2296 (defun widget-checklist-match-inline (widget values)
2297 ;; Find the values which match a type in the checklist.
2298 (let ((greedy (widget-get widget :greedy))
2299 (args (copy-sequence (widget-get widget :args)))
2302 (let ((answer (widget-checklist-match-up args values)))
2304 (let ((vals (widget-match-inline answer values)))
2305 (setq found (append found (car vals))
2307 args (delq answer args))))
2309 (setq rest (append rest (list (car values)))
2310 values (cdr values)))
2312 (setq rest (append rest values)
2316 (defun widget-checklist-match-find (widget vals)
2317 "Find the vals which match a type in the checklist.
2318 Return an alist of (TYPE MATCH)."
2319 (let ((greedy (widget-get widget :greedy))
2320 (args (copy-sequence (widget-get widget :args)))
2323 (let ((answer (widget-checklist-match-up args vals)))
2325 (let ((match (widget-match-inline answer vals)))
2326 (setq found (cons (cons answer (car match)) found)
2328 args (delq answer args))))
2330 (setq vals (cdr vals)))
2335 (defun widget-checklist-match-up (args vals)
2336 "Return the first type from ARGS that matches VALS."
2337 (let (current found)
2338 (while (and args (null found))
2339 (setq current (car args)
2341 found (widget-match-inline current vals)))
2345 (defun widget-checklist-value-get (widget)
2346 ;; The values of all selected items.
2347 (let ((children (widget-get widget :children))
2350 (setq child (car children)
2351 children (cdr children))
2352 (if (widget-value (widget-get child :button))
2353 (setq result (append result (widget-apply child :value-inline)))))
2356 (defun widget-checklist-validate (widget)
2357 ;; Ticked chilren must be valid.
2358 (let ((children (widget-get widget :children))
2360 (while (and children (not found))
2361 (setq child (car children)
2362 children (cdr children)
2363 button (widget-get child :button)
2364 found (and (widget-value button)
2365 (widget-apply child :validate))))
2368 ;;; The `option' Widget
2370 (define-widget 'option 'checklist
2371 "An widget with an optional item."
2374 ;;; The `choice-item' Widget.
2376 (define-widget 'choice-item 'item
2377 "Button items that delegate action events to their parents."
2378 :action 'widget-parent-action
2379 :format "%[%t%] \n")
2381 ;;; The `radio-button' Widget.
2383 (define-widget 'radio-button 'toggle
2384 "A radio button for use in the `radio' widget."
2385 :notify 'widget-radio-button-notify
2392 :off-glyph "radio0")
2394 (defun widget-radio-button-notify (widget child &optional event)
2396 (widget-apply (widget-get widget :parent) :action widget event))
2398 ;;; The `radio-button-choice' Widget.
2400 (define-widget 'radio-button-choice 'default
2401 "Select one of multiple options."
2402 :convert-widget 'widget-types-convert-widget
2403 :copy 'widget-types-copy
2406 :entry-format "%b %v"
2407 :value-create 'widget-radio-value-create
2408 :value-get 'widget-radio-value-get
2409 :value-inline 'widget-radio-value-inline
2410 :value-set 'widget-radio-value-set
2411 :error "You must push one of the buttons"
2412 :validate 'widget-radio-validate
2413 :match 'widget-choice-match
2414 :match-inline 'widget-choice-match-inline
2415 :action 'widget-radio-action)
2417 (defun widget-radio-value-create (widget)
2418 ;; Insert all values
2419 (let ((args (widget-get widget :args))
2422 (setq arg (car args)
2424 (widget-radio-add-item widget arg))))
2426 (defun widget-radio-add-item (widget type)
2427 "Add to radio widget WIDGET a new radio button item of type TYPE."
2428 ;; (setq type (widget-convert type))
2429 (and (eq (preceding-char) ?\n)
2430 (widget-get widget :indent)
2431 (insert-char ?\s (widget-get widget :indent)))
2432 (widget-specify-insert
2433 (let* ((value (widget-get widget :value))
2434 (children (widget-get widget :children))
2435 (buttons (widget-get widget :buttons))
2436 (button-args (or (widget-get type :sibling-args)
2437 (widget-get widget :button-args)))
2439 (chosen (and (null (widget-get widget :choice))
2440 (widget-apply type :match value)))
2442 (insert (widget-get widget :entry-format))
2444 ;; Parse % escapes in format.
2445 (while (re-search-forward "%\\([bv%]\\)" nil t)
2446 (let ((escape (char-after (match-beginning 1))))
2448 (cond ((eq escape ?%)
2451 (setq button (apply 'widget-create-child-and-convert
2452 widget 'radio-button
2453 :value (not (null chosen))
2456 (setq child (if chosen
2457 (widget-create-child-value
2459 (widget-create-child widget type)))
2461 (widget-apply child :deactivate)))
2463 (error "Unknown escape `%c'" escape)))))
2464 ;; Update properties.
2466 (widget-put widget :choice type))
2468 (widget-put child :button button)
2469 (widget-put widget :buttons (nconc buttons (list button))))
2471 (widget-put widget :children (nconc children (list child))))
2474 (defun widget-radio-value-get (widget)
2475 ;; Get value of the child widget.
2476 (let ((chosen (widget-radio-chosen widget)))
2477 (and chosen (widget-value chosen))))
2479 (defun widget-radio-chosen (widget)
2480 "Return the widget representing the chosen radio button."
2481 (let ((children (widget-get widget :children))
2484 (setq current (car children)
2485 children (cdr children))
2486 (when (widget-apply (widget-get current :button) :value-get)
2491 (defun widget-radio-value-inline (widget)
2492 ;; Get value of the child widget.
2493 (let ((children (widget-get widget :children))
2496 (setq current (car children)
2497 children (cdr children))
2498 (when (widget-apply (widget-get current :button) :value-get)
2499 (setq found (widget-apply current :value-inline)
2503 (defun widget-radio-value-set (widget value)
2504 ;; We can't just delete and recreate a radio widget, since children
2505 ;; can be added after the original creation and won't be recreated
2507 (let ((children (widget-get widget :children))
2510 (setq current (car children)
2511 children (cdr children))
2512 (let* ((button (widget-get current :button))
2513 (match (and (not found)
2514 (widget-apply current :match value))))
2515 (widget-value-set button match)
2518 (widget-value-set current value)
2519 (widget-apply current :activate))
2520 (widget-apply current :deactivate))
2521 (setq found (or found match))))))
2523 (defun widget-radio-validate (widget)
2524 ;; Valid if we have made a valid choice.
2525 (let ((children (widget-get widget :children))
2526 current found button)
2527 (while (and children (not found))
2528 (setq current (car children)
2529 children (cdr children)
2530 button (widget-get current :button)
2531 found (widget-apply button :value-get)))
2533 (widget-apply current :validate)
2536 (defun widget-radio-action (widget child event)
2537 ;; Check if a radio button was pressed.
2538 (let ((children (widget-get widget :children))
2539 (buttons (widget-get widget :buttons))
2541 (when (memq child buttons)
2543 (setq current (car children)
2544 children (cdr children))
2545 (let* ((button (widget-get current :button)))
2546 (cond ((eq child button)
2547 (widget-value-set button t)
2548 (widget-apply current :activate))
2549 ((widget-value button)
2550 (widget-value-set button nil)
2551 (widget-apply current :deactivate)))))))
2552 ;; Pass notification to parent.
2553 (widget-apply widget :notify child event))
2555 ;;; The `insert-button' Widget.
2557 (define-widget 'insert-button 'push-button
2558 "An insert button for the `editable-list' widget."
2560 :help-echo "Insert a new item into the list at this position."
2561 :action 'widget-insert-button-action)
2563 (defun widget-insert-button-action (widget &optional event)
2564 ;; Ask the parent to insert a new item.
2565 (widget-apply (widget-get widget :parent)
2566 :insert-before (widget-get widget :widget)))
2568 ;;; The `delete-button' Widget.
2570 (define-widget 'delete-button 'push-button
2571 "A delete button for the `editable-list' widget."
2573 :help-echo "Delete this item from the list."
2574 :action 'widget-delete-button-action)
2576 (defun widget-delete-button-action (widget &optional event)
2577 ;; Ask the parent to insert a new item.
2578 (widget-apply (widget-get widget :parent)
2579 :delete-at (widget-get widget :widget)))
2581 ;;; The `editable-list' Widget.
2583 ;; (defcustom widget-editable-list-gui nil
2584 ;; "If non-nil, use GUI push-buttons in editable list when available."
2588 (define-widget 'editable-list 'default
2589 "A variable list of widgets of the same type."
2590 :convert-widget 'widget-types-convert-widget
2591 :copy 'widget-types-copy
2594 :format-handler 'widget-editable-list-format-handler
2595 :entry-format "%i %d %v"
2596 :value-create 'widget-editable-list-value-create
2597 :value-get 'widget-editable-list-value-get
2598 :validate 'widget-children-validate
2599 :match 'widget-editable-list-match
2600 :match-inline 'widget-editable-list-match-inline
2601 :insert-before 'widget-editable-list-insert-before
2602 :delete-at 'widget-editable-list-delete-at)
2604 (defun widget-editable-list-format-handler (widget escape)
2605 ;; We recognize the insert button.
2606 ;; (let ((widget-push-button-gui widget-editable-list-gui))
2607 (cond ((eq escape ?i)
2608 (and (widget-get widget :indent)
2609 (insert-char ?\s (widget-get widget :indent)))
2610 (apply 'widget-create-child-and-convert
2611 widget 'insert-button
2612 (widget-get widget :append-button-args)))
2614 (widget-default-format-handler widget escape)))
2618 (defun widget-editable-list-value-create (widget)
2619 ;; Insert all values
2620 (let* ((value (widget-get widget :value))
2621 (type (nth 0 (widget-get widget :args)))
2623 (widget-put widget :value-pos (copy-marker (point)))
2624 (set-marker-insertion-type (widget-get widget :value-pos) t)
2626 (let ((answer (widget-match-inline type value)))
2628 (setq children (cons (widget-editable-list-entry-create
2630 (if (widget-get type :inline)
2637 (widget-put widget :children (nreverse children))))
2639 (defun widget-editable-list-value-get (widget)
2640 ;; Get value of the child widget.
2641 (apply 'append (mapcar (lambda (child) (widget-apply child :value-inline))
2642 (widget-get widget :children))))
2644 (defun widget-editable-list-match (widget value)
2645 ;; Value must be a list and all the members must match the type.
2647 (null (cdr (widget-editable-list-match-inline widget value)))))
2649 (defun widget-editable-list-match-inline (widget value)
2650 (let ((type (nth 0 (widget-get widget :args)))
2653 (while (and value ok)
2654 (let ((answer (widget-match-inline type value)))
2656 (setq found (append found (car answer))
2659 (cons found value)))
2661 (defun widget-editable-list-insert-before (widget before)
2662 ;; Insert a new child in the list of children.
2664 (let ((children (widget-get widget :children))
2665 (inhibit-read-only t)
2666 before-change-functions
2667 after-change-functions)
2669 (goto-char (widget-get before :entry-from)))
2671 (goto-char (widget-get widget :value-pos))))
2672 (let ((child (widget-editable-list-entry-create
2674 (when (< (widget-get child :entry-from) (widget-get widget :from))
2675 (set-marker (widget-get widget :from)
2676 (widget-get child :entry-from)))
2677 (if (eq (car children) before)
2678 (widget-put widget :children (cons child children))
2679 (while (not (eq (car (cdr children)) before))
2680 (setq children (cdr children)))
2681 (setcdr children (cons child (cdr children)))))))
2683 (widget-apply widget :notify widget))
2685 (defun widget-editable-list-delete-at (widget child)
2686 ;; Delete child from list of children.
2688 (let ((buttons (copy-sequence (widget-get widget :buttons)))
2690 (inhibit-read-only t)
2691 before-change-functions
2692 after-change-functions)
2694 (setq button (car buttons)
2695 buttons (cdr buttons))
2696 (when (eq (widget-get button :widget) child)
2698 :buttons (delq button (widget-get widget :buttons)))
2699 (widget-delete button))))
2700 (let ((entry-from (widget-get child :entry-from))
2701 (entry-to (widget-get child :entry-to))
2702 (inhibit-read-only t)
2703 before-change-functions
2704 after-change-functions)
2705 (widget-delete child)
2706 (delete-region entry-from entry-to)
2707 (set-marker entry-from nil)
2708 (set-marker entry-to nil))
2709 (widget-put widget :children (delq child (widget-get widget :children))))
2711 (widget-apply widget :notify widget))
2713 (defun widget-editable-list-entry-create (widget value conv)
2714 ;; Create a new entry to the list.
2715 (let ((type (nth 0 (widget-get widget :args)))
2716 ;; (widget-push-button-gui widget-editable-list-gui)
2717 child delete insert)
2718 (widget-specify-insert
2720 (and (widget-get widget :indent)
2721 (insert-char ?\s (widget-get widget :indent)))
2722 (insert (widget-get widget :entry-format)))
2723 ;; Parse % escapes in format.
2724 (while (re-search-forward "%\\(.\\)" nil t)
2725 (let ((escape (char-after (match-beginning 1))))
2727 (cond ((eq escape ?%)
2730 (setq insert (apply 'widget-create-child-and-convert
2731 widget 'insert-button
2732 (widget-get widget :insert-button-args))))
2734 (setq delete (apply 'widget-create-child-and-convert
2735 widget 'delete-button
2736 (widget-get widget :delete-button-args))))
2739 (setq child (widget-create-child-value
2741 (setq child (widget-create-child-value
2742 widget type (widget-default-get type)))))
2744 (error "Unknown escape `%c'" escape)))))
2745 (let ((buttons (widget-get widget :buttons)))
2746 (if insert (push insert buttons))
2747 (if delete (push delete buttons))
2748 (widget-put widget :buttons buttons))
2749 (let ((entry-from (point-min-marker))
2750 (entry-to (point-max-marker)))
2751 (set-marker-insertion-type entry-from t)
2752 (set-marker-insertion-type entry-to nil)
2753 (widget-put child :entry-from entry-from)
2754 (widget-put child :entry-to entry-to)))
2755 (if insert (widget-put insert :widget child))
2756 (if delete (widget-put delete :widget child))
2759 ;;; The `group' Widget.
2761 (define-widget 'group 'default
2762 "A widget which groups other widgets inside."
2763 :convert-widget 'widget-types-convert-widget
2764 :copy 'widget-types-copy
2766 :value-create 'widget-group-value-create
2767 :value-get 'widget-editable-list-value-get
2768 :default-get 'widget-group-default-get
2769 :validate 'widget-children-validate
2770 :match 'widget-group-match
2771 :match-inline 'widget-group-match-inline)
2773 (defun widget-group-value-create (widget)
2774 ;; Create each component.
2775 (let ((args (widget-get widget :args))
2776 (value (widget-get widget :value))
2777 arg answer children)
2779 (setq arg (car args)
2781 answer (widget-match-inline arg value)
2783 (and (eq (preceding-char) ?\n)
2784 (widget-get widget :indent)
2785 (insert-char ?\s (widget-get widget :indent)))
2786 (push (cond ((null answer)
2787 (widget-create-child widget arg))
2788 ((widget-get arg :inline)
2789 (widget-create-child-value widget arg (car answer)))
2791 (widget-create-child-value widget arg (car (car answer)))))
2793 (widget-put widget :children (nreverse children))))
2795 (defun widget-group-default-get (widget)
2796 ;; Get the default of the components.
2797 (mapcar 'widget-default-get (widget-get widget :args)))
2799 (defun widget-group-match (widget values)
2800 ;; Match if the components match.
2802 (let ((match (widget-group-match-inline widget values)))
2803 (and match (null (cdr match))))))
2805 (defun widget-group-match-inline (widget vals)
2806 ;; Match if the components match.
2807 (let ((args (widget-get widget :args))
2808 argument answer found)
2810 (setq argument (car args)
2812 answer (widget-match-inline argument vals))
2814 (setq vals (cdr answer)
2815 found (append found (car answer)))
2819 (cons found vals))))
2821 ;;; The `visibility' Widget.
2823 (define-widget 'visibility 'item
2824 "An indicator and manipulator for hidden items.
2826 The following properties have special meanings for this widget:
2827 :on-image Image filename or spec to display when the item is visible.
2828 :on Text shown if the \"on\" image is nil or cannot be displayed.
2829 :off-image Image filename or spec to display when the item is hidden.
2830 :off Text shown if the \"off\" image is nil cannot be displayed."
2838 :value-create 'widget-visibility-value-create
2839 :action 'widget-toggle-action
2840 :match (lambda (widget value) t))
2842 (defun widget-visibility-value-create (widget)
2843 ;; Insert text representing the `on' and `off' states.
2844 (let* ((val (widget-value widget))
2845 (text (widget-get widget (if val :on :off)))
2846 (img (widget-image-find
2847 (widget-get widget (if val :on-image :off-image)))))
2848 (widget-image-insert widget
2850 (concat widget-push-button-prefix text
2851 widget-push-button-suffix)
2854 (append img '(:ascent center))))))
2856 ;;; The `documentation-link' Widget.
2858 ;; This is a helper widget for `documentation-string'.
2860 (define-widget 'documentation-link 'link
2861 "Link type used in documentation strings."
2863 :help-echo "Describe this symbol"
2864 :action 'widget-documentation-link-action)
2866 (defun widget-documentation-link-action (widget &optional event)
2867 "Display documentation for WIDGET's value. Ignore optional argument EVENT."
2868 (let* ((string (widget-get widget :value))
2869 (symbol (intern string)))
2870 (if (and (fboundp symbol) (boundp symbol))
2871 ;; If there are two doc strings, give the user a way to pick one.
2872 (apropos (concat "\\`" (regexp-quote string) "\\'"))
2873 (if (fboundp symbol)
2874 (describe-function symbol)
2875 (describe-variable symbol)))))
2877 (defcustom widget-documentation-links t
2878 "Add hyperlinks to documentation strings when non-nil."
2880 :group 'widget-documentation)
2882 (defcustom widget-documentation-link-regexp "`\\([^\n`' ]+\\)'"
2883 "Regexp for matching potential links in documentation strings.
2884 The first group should be the link itself."
2886 :group 'widget-documentation)
2888 (defcustom widget-documentation-link-p 'intern-soft
2889 "Predicate used to test if a string is useful as a link.
2890 The value should be a function. The function will be called with one
2891 argument, a string, and should return non-nil if there should be a
2892 link for that string."
2894 :options '(widget-documentation-link-p)
2895 :group 'widget-documentation)
2897 (defcustom widget-documentation-link-type 'documentation-link
2898 "Widget type used for links in documentation strings."
2900 :group 'widget-documentation)
2902 (defun widget-documentation-link-add (widget from to)
2903 (widget-specify-doc widget from to)
2904 (when widget-documentation-links
2905 (let ((regexp widget-documentation-link-regexp)
2906 (buttons (widget-get widget :buttons))
2907 (widget-mouse-face (default-value 'widget-mouse-face))
2908 (widget-button-face widget-documentation-face)
2909 (widget-button-pressed-face widget-documentation-face))
2912 (while (re-search-forward regexp to t)
2913 (let ((name (match-string 1))
2914 (begin (match-beginning 1))
2915 (end (match-end 1)))
2916 (when (funcall widget-documentation-link-p name)
2917 (push (widget-convert-button widget-documentation-link-type
2918 begin end :value name)
2920 (widget-put widget :buttons buttons)))
2921 (let ((indent (widget-get widget :indent)))
2922 (when (and indent (not (zerop indent)))
2925 (narrow-to-region from to)
2926 (goto-char (point-min))
2927 (while (search-forward "\n" nil t)
2928 (insert-char ?\s indent)))))))
2930 ;;; The `documentation-string' Widget.
2932 (define-widget 'documentation-string 'item
2933 "A documentation string."
2935 :action 'widget-documentation-string-action
2936 :value-create 'widget-documentation-string-value-create
2937 :visibility-widget 'visibility)
2939 (defun widget-documentation-string-value-create (widget)
2940 ;; Insert documentation string.
2941 (let ((doc (widget-value widget))
2942 (indent (widget-get widget :indent))
2943 (shown (widget-get (widget-get widget :parent) :documentation-shown))
2945 (if (string-match "\n" doc)
2946 (let ((before (substring doc 0 (match-beginning 0)))
2947 (after (substring doc (match-beginning 0)))
2949 (when (and indent (not (zerop indent)))
2950 (insert-char ?\s indent))
2952 (widget-documentation-link-add widget start (point))
2954 (widget-create-child-and-convert
2955 widget (widget-get widget :visibility-widget)
2956 :help-echo "Show or hide rest of the documentation."
2960 :action 'widget-parent-action
2963 (setq start (point))
2964 (when (and indent (not (zerop indent)))
2965 (insert-char ?\s indent))
2967 (widget-documentation-link-add widget start (point)))
2968 (widget-put widget :buttons (list button)))
2969 (when (and indent (not (zerop indent)))
2970 (insert-char ?\s indent))
2972 (widget-documentation-link-add widget start (point))))
2975 (defun widget-documentation-string-action (widget &rest ignore)
2976 ;; Toggle documentation.
2977 (let ((parent (widget-get widget :parent)))
2978 (widget-put parent :documentation-shown
2979 (not (widget-get parent :documentation-shown))))
2981 (widget-value-set widget (widget-value widget)))
2983 (defun widget-add-documentation-string-button (widget &rest args)
2984 "Insert a new `documentation-string' widget based on WIDGET.
2985 The new widget becomes a child of WIDGET, and is also added to
2986 its `:buttons' list. The documentation string is found from
2987 WIDGET using the function `widget-docstring'.
2988 Optional ARGS specifies additional keyword arguments for the
2989 `documentation-string' widget."
2990 (let ((doc (widget-docstring widget))
2991 (indent (widget-get widget :indent))
2992 (doc-indent (widget-get widget :documentation-indent)))
2994 (and (eq (preceding-char) ?\n)
2996 (insert-char ?\s indent))
2997 (unless (or (numberp doc-indent) (null doc-indent))
2998 (setq doc-indent 0))
2999 (widget-put widget :buttons
3000 (cons (apply 'widget-create-child-and-convert
3001 widget 'documentation-string
3003 (nconc args (list doc)))
3004 (widget-get widget :buttons))))))
3006 ;;; The Sexp Widgets.
3008 (define-widget 'const 'item
3009 "An immutable sexp."
3010 :prompt-value 'widget-const-prompt-value
3013 (defun widget-const-prompt-value (widget prompt value unbound)
3014 ;; Return the value of the const.
3015 (widget-value widget))
3017 (define-widget 'function-item 'const
3018 "An immutable function name."
3020 :documentation-property (lambda (symbol)
3022 (documentation symbol t)
3025 (define-widget 'variable-item 'const
3026 "An immutable variable name."
3028 :documentation-property 'variable-documentation)
3030 (define-widget 'other 'sexp
3031 "Matches any value, but doesn't let the user edit the value.
3032 This is useful as last item in a `choice' widget.
3033 You should use this widget type with a default value,
3034 as in (other DEFAULT) or (other :tag \"NAME\" DEFAULT).
3035 If the user selects this alternative, that specifies DEFAULT
3041 (defvar widget-string-prompt-value-history nil
3042 "History of input to `widget-string-prompt-value'.")
3044 (define-widget 'string 'editable-field
3047 :format "%{%t%}: %v"
3048 :complete-function 'ispell-complete-word
3049 :prompt-history 'widget-string-prompt-value-history)
3053 (defun widget-string-complete ()
3054 "Complete contents of string field.
3055 Completions are taken from the :completion-alist property of the
3056 widget. If that isn't a list, it's evalled and expected to yield a list."
3058 (let* ((completion-ignore-case (widget-get widget :completion-ignore-case))
3059 (alist (widget-get widget :completion-alist))
3060 (_ (unless (listp alist)
3061 (setq alist (eval alist)))))
3062 (completion-in-region (widget-field-start widget)
3063 (max (point) (widget-field-text-end widget))
3066 (define-widget 'regexp 'string
3067 "A regular expression."
3068 :match 'widget-regexp-match
3069 :validate 'widget-regexp-validate
3070 ;; Doesn't work well with terminating newline.
3071 ;; :value-face 'widget-single-line-field
3074 (defun widget-regexp-match (widget value)
3075 ;; Match valid regexps.
3076 (and (stringp value)
3079 (string-match value ""))
3082 (defun widget-regexp-validate (widget)
3083 "Check that the value of WIDGET is a valid regexp."
3084 (condition-case data
3086 (string-match (widget-value widget) ""))
3087 (error (widget-put widget :error (error-message-string data))
3090 (define-widget 'file 'string
3092 It reads a file name from an editable text field."
3093 :complete-function 'widget-file-complete
3094 :prompt-value 'widget-file-prompt-value
3095 :format "%{%t%}: %v"
3096 ;; Doesn't work well with terminating newline.
3097 ;; :value-face 'widget-single-line-field
3100 (defun widget-file-complete ()
3101 "Perform completion on file name preceding point."
3103 (completion-in-region (widget-field-start widget)
3104 (max (point) (widget-field-text-end widget))
3105 'completion-file-name-table))
3107 (defun widget-file-prompt-value (widget prompt value unbound)
3108 ;; Read file from minibuffer.
3109 (abbreviate-file-name
3111 (read-file-name prompt)
3112 (let ((prompt2 (format "%s (default %s): " prompt value))
3113 (dir (file-name-directory value))
3114 (file (file-name-nondirectory value))
3115 (must-match (widget-get widget :must-match)))
3116 (read-file-name prompt2 dir nil must-match file)))))
3118 ;;;(defun widget-file-action (widget &optional event)
3119 ;;; ;; Read a file name from the minibuffer.
3120 ;;; (let* ((value (widget-value widget))
3121 ;;; (dir (file-name-directory value))
3122 ;;; (file (file-name-nondirectory value))
3123 ;;; (menu-tag (widget-apply widget :menu-tag-get))
3124 ;;; (must-match (widget-get widget :must-match))
3125 ;;; (answer (read-file-name (concat menu-tag " (default " value "): ")
3126 ;;; dir nil must-match file)))
3127 ;;; (widget-value-set widget (abbreviate-file-name answer))
3129 ;;; (widget-apply widget :notify widget event)))
3131 ;; Fixme: use file-name-as-directory.
3132 (define-widget 'directory 'file
3133 "A directory widget.
3134 It reads a directory name from an editable text field."
3137 (defvar widget-symbol-prompt-value-history nil
3138 "History of input to `widget-symbol-prompt-value'.")
3140 (define-widget 'symbol 'editable-field
3144 :format "%{%t%}: %v"
3145 :match (lambda (widget value) (symbolp value))
3146 :complete-function 'lisp-complete-symbol
3147 :prompt-internal 'widget-symbol-prompt-internal
3148 :prompt-match 'symbolp
3149 :prompt-history 'widget-symbol-prompt-value-history
3150 :value-to-internal (lambda (widget value)
3154 :value-to-external (lambda (widget value)
3159 (defun widget-symbol-prompt-internal (widget prompt initial history)
3160 ;; Read file from minibuffer.
3161 (let ((answer (completing-read prompt obarray
3162 (widget-get widget :prompt-match)
3163 nil initial history)))
3164 (if (and (stringp answer)
3165 (not (zerop (length answer))))
3167 (error "No value"))))
3169 (defvar widget-function-prompt-value-history nil
3170 "History of input to `widget-function-prompt-value'.")
3172 (define-widget 'function 'restricted-sexp
3174 :complete-function (lambda ()
3176 (lisp-complete-symbol 'fboundp))
3177 :prompt-value 'widget-field-prompt-value
3178 :prompt-internal 'widget-symbol-prompt-internal
3179 :prompt-match 'fboundp
3180 :prompt-history 'widget-function-prompt-value-history
3181 :action 'widget-field-action
3182 :match-alternatives '(functionp)
3183 :validate (lambda (widget)
3184 (unless (functionp (widget-value widget))
3185 (widget-put widget :error (format "Invalid function: %S"
3186 (widget-value widget)))
3191 (defvar widget-variable-prompt-value-history nil
3192 "History of input to `widget-variable-prompt-value'.")
3194 (define-widget 'variable 'symbol
3196 :prompt-match 'boundp
3197 :prompt-history 'widget-variable-prompt-value-history
3198 :complete-function (lambda ()
3200 (lisp-complete-symbol 'boundp))
3203 (define-widget 'coding-system 'symbol
3204 "A MULE coding-system."
3205 :format "%{%t%}: %v"
3206 :tag "Coding system"
3208 :prompt-history 'coding-system-value-history
3209 :prompt-value 'widget-coding-system-prompt-value
3210 :action 'widget-coding-system-action
3211 :complete-function (lambda ()
3213 (lisp-complete-symbol 'coding-system-p))
3214 :validate (lambda (widget)
3215 (unless (coding-system-p (widget-value widget))
3216 (widget-put widget :error (format "Invalid coding system: %S"
3217 (widget-value widget)))
3220 :prompt-match 'coding-system-p)
3222 (defun widget-coding-system-prompt-value (widget prompt value unbound)
3223 "Read coding-system from minibuffer."
3224 (if (widget-get widget :base-only)
3226 (completing-read (format "%s (default %s): " prompt value)
3227 (mapcar #'list (coding-system-list t)) nil nil nil
3228 coding-system-history))
3229 (read-coding-system (format "%s (default %s): " prompt value) value)))
3231 (defun widget-coding-system-action (widget &optional event)
3233 (widget-coding-system-prompt-value
3235 (widget-apply widget :menu-tag-get)
3236 (widget-value widget)
3238 (widget-value-set widget answer)
3239 (widget-apply widget :notify widget event)
3242 ;;; I'm not sure about what this is good for? KFS.
3243 (defvar widget-key-sequence-prompt-value-history nil
3244 "History of input to `widget-key-sequence-prompt-value'.")
3246 (defvar widget-key-sequence-default-value [ignore]
3247 "Default value for an empty key sequence.")
3249 (defvar widget-key-sequence-map
3250 (let ((map (make-sparse-keymap)))
3251 (set-keymap-parent map widget-field-keymap)
3252 (define-key map [(control ?q)] 'widget-key-sequence-read-event)
3255 (define-widget 'key-sequence 'restricted-sexp
3257 :prompt-value 'widget-field-prompt-value
3258 :prompt-internal 'widget-symbol-prompt-internal
3259 ; :prompt-match 'fboundp ;; What was this good for? KFS
3260 :prompt-history 'widget-key-sequence-prompt-value-history
3261 :action 'widget-field-action
3262 :match-alternatives '(stringp vectorp)
3263 :format "%{%t%}: %v"
3264 :validate 'widget-key-sequence-validate
3265 :value-to-internal 'widget-key-sequence-value-to-internal
3266 :value-to-external 'widget-key-sequence-value-to-external
3267 :value widget-key-sequence-default-value
3268 :keymap widget-key-sequence-map
3269 :help-echo "C-q: insert KEY, EVENT, or CODE; RET: enter value"
3270 :tag "Key sequence")
3272 (defun widget-key-sequence-read-event (ev)
3274 (let ((inhibit-quit t) quit-flag)
3275 (read-event "Insert KEY, EVENT, or CODE: "))))
3276 (let ((ev2 (and (memq 'down (event-modifiers ev))
3278 (tr (and (keymapp function-key-map)
3279 (lookup-key function-key-map (vector ev)))))
3280 (when (and (integerp ev)
3281 (or (and (<= ?0 ev) (< ev (+ ?0 (min 10 read-quoted-char-radix))))
3282 (and (<= ?a (downcase ev))
3283 (< (downcase ev) (+ ?a -10 (min 36 read-quoted-char-radix))))))
3284 (setq unread-command-events (cons ev unread-command-events)
3285 ev (read-quoted-char (format "Enter code (radix %d)" read-quoted-char-radix))
3287 (if (and (integerp ev) (not (characterp ev)))
3288 (insert (char-to-string ev)))) ;; throw invalid char error
3289 (setq ev (key-description (list ev)))
3291 (setq tr (key-description (list (aref tr 0))))
3292 (if (y-or-n-p (format "Key %s is translated to %s -- use %s? " ev tr tr))
3293 (setq ev tr ev2 nil)))
3294 (insert (if (= (char-before) ?\s) "" " ") ev " ")
3296 (insert (key-description (list ev2)) " "))))
3298 (defun widget-key-sequence-validate (widget)
3299 (unless (or (stringp (widget-value widget))
3300 (vectorp (widget-value widget)))
3301 (widget-put widget :error (format "Invalid key sequence: %S"
3302 (widget-value widget)))
3305 (defun widget-key-sequence-value-to-internal (widget value)
3306 (if (widget-apply widget :match value)
3307 (if (equal value widget-key-sequence-default-value)
3309 (key-description value))
3312 (defun widget-key-sequence-value-to-external (widget value)
3314 (if (string-match "\\`[[:space:]]*\\'" value)
3315 widget-key-sequence-default-value
3316 (read-kbd-macro value))
3320 (define-widget 'sexp 'editable-field
3321 "An arbitrary Lisp expression."
3322 :tag "Lisp expression"
3323 :format "%{%t%}: %v"
3325 :validate 'widget-sexp-validate
3326 :match (lambda (widget value) t)
3327 :value-to-internal 'widget-sexp-value-to-internal
3328 :value-to-external (lambda (widget value) (read value))
3329 :prompt-history 'widget-sexp-prompt-value-history
3330 :prompt-value 'widget-sexp-prompt-value)
3332 (defun widget-sexp-value-to-internal (widget value)
3333 ;; Use pp for printer representation.
3334 (let ((pp (if (symbolp value)
3335 (prin1-to-string value)
3336 (pp-to-string value))))
3337 (while (string-match "\n\\'" pp)
3338 (setq pp (substring pp 0 -1)))
3339 (if (or (string-match "\n\\'" pp)
3344 (defun widget-sexp-validate (widget)
3345 ;; Valid if we can read the string and there is no junk left after it.
3347 (insert (widget-apply widget :value-get))
3348 (goto-char (point-min))
3350 (condition-case data
3352 ;; Avoid a confusing end-of-file error.
3353 (skip-syntax-forward "\\s-")
3355 (setq err "Empty sexp -- use `nil'?")
3356 (unless (widget-apply widget :match (read (current-buffer)))
3357 (setq err (widget-get widget :type-error))))
3358 ;; Allow whitespace after expression.
3359 (skip-syntax-forward "\\s-")
3360 (if (and (not (eobp))
3362 (setq err (format "Junk at end of expression: %s"
3363 (buffer-substring (point)
3365 (end-of-file ; Avoid confusing error message.
3366 (setq err "Unbalanced sexp"))
3367 (error (setq err (error-message-string data))))
3370 (widget-put widget :error err)
3373 (defvar widget-sexp-prompt-value-history nil
3374 "History of input to `widget-sexp-prompt-value'.")
3376 (defun widget-sexp-prompt-value (widget prompt value unbound)
3377 ;; Read an arbitrary sexp.
3378 (let ((found (read-string prompt
3379 (if unbound nil (cons (prin1-to-string value) 0))
3380 (widget-get widget :prompt-history))))
3381 (let ((answer (read-from-string found)))
3382 (unless (= (cdr answer) (length found))
3383 (error "Junk at end of expression: %s"
3384 (substring found (cdr answer))))
3387 (define-widget 'restricted-sexp 'sexp
3388 "A Lisp expression restricted to values that match.
3389 To use this type, you must define :match or :match-alternatives."
3390 :type-error "The specified value is not valid"
3391 :match 'widget-restricted-sexp-match
3392 :value-to-internal (lambda (widget value)
3393 (if (widget-apply widget :match value)
3394 (prin1-to-string value)
3397 (defun widget-restricted-sexp-match (widget value)
3398 (let ((alternatives (widget-get widget :match-alternatives))
3400 (while (and alternatives (not matched))
3401 (if (cond ((functionp (car alternatives))
3402 (funcall (car alternatives) value))
3403 ((and (consp (car alternatives))
3404 (eq (car (car alternatives)) 'quote))
3405 (eq value (nth 1 (car alternatives)))))
3407 (setq alternatives (cdr alternatives)))
3410 (define-widget 'integer 'restricted-sexp
3414 :type-error "This field should contain an integer"
3415 :match-alternatives '(integerp))
3417 (define-widget 'number 'restricted-sexp
3418 "A number (floating point or integer)."
3421 :type-error "This field should contain a number (floating point or integer)"
3422 :match-alternatives '(numberp))
3424 (define-widget 'float 'restricted-sexp
3425 "A floating point number."
3426 :tag "Floating point number"
3428 :type-error "This field should contain a floating point number"
3429 :match-alternatives '(floatp))
3431 (define-widget 'character 'editable-field
3436 :format "%{%t%}: %v\n"
3437 :valid-regexp "\\`.\\'"
3438 :error "This field should contain a single character"
3439 :value-to-internal (lambda (widget value)
3442 (char-to-string value)))
3443 :value-to-external (lambda (widget value)
3447 :match (lambda (widget value)
3448 (characterp value)))
3450 (define-widget 'list 'group
3453 :format "%{%t%}:\n%v")
3455 (define-widget 'vector 'group
3458 :format "%{%t%}:\n%v"
3459 :match 'widget-vector-match
3460 :value-to-internal (lambda (widget value) (append value nil))
3461 :value-to-external (lambda (widget value) (apply 'vector value)))
3463 (defun widget-vector-match (widget value)
3464 (and (vectorp value)
3465 (widget-group-match widget
3466 (widget-apply widget :value-to-internal value))))
3468 (define-widget 'cons 'group
3471 :format "%{%t%}:\n%v"
3472 :match 'widget-cons-match
3473 :value-to-internal (lambda (widget value)
3474 (list (car value) (cdr value)))
3475 :value-to-external (lambda (widget value)
3476 (apply 'cons value)))
3478 (defun widget-cons-match (widget value)
3480 (widget-group-match widget
3481 (widget-apply widget :value-to-internal value))))
3483 ;;; The `lazy' Widget.
3485 ;; Recursive datatypes.
3487 (define-widget 'lazy 'default
3488 "Base widget for recursive datastructures.
3490 The `lazy' widget will, when instantiated, contain a single inferior
3491 widget, of the widget type specified by the :type parameter. The
3492 value of the `lazy' widget is the same as the value of the inferior
3493 widget. When deriving a new widget from the 'lazy' widget, the :type
3494 parameter is allowed to refer to the widget currently being defined,
3495 thus allowing recursive datastructures to be described.
3497 The :type parameter takes the same arguments as the defcustom
3498 parameter with the same name.
3500 Most composite widgets, i.e. widgets containing other widgets, does
3501 not allow recursion. That is, when you define a new widget type, none
3502 of the inferior widgets may be of the same type you are currently
3505 In Lisp, however, it is custom to define datastructures in terms of
3506 themselves. A list, for example, is defined as either nil, or a cons
3507 cell whose cdr itself is a list. The obvious way to translate this
3508 into a widget type would be
3510 (define-widget 'my-list 'choice
3511 \"A list of sexps.\"
3513 :args '((const nil) (cons :value (nil) sexp my-list)))
3515 Here we attempt to define my-list as a choice of either the constant
3516 nil, or a cons-cell containing a sexp and my-lisp. This will not work
3517 because the `choice' widget does not allow recursion.
3519 Using the `lazy' widget you can overcome this problem, as in this
3522 (define-widget 'sexp-list 'lazy
3523 \"A list of sexps.\"
3525 :type '(choice (const nil) (cons :value (nil) sexp sexp-list)))"
3526 :format "%{%t%}: %v"
3527 ;; We don't convert :type because we want to allow recursive
3528 ;; datastructures. This is slow, so we should not create speed
3529 ;; critical widgets by deriving from this.
3530 :convert-widget 'widget-value-convert-widget
3531 :value-create 'widget-type-value-create
3532 :value-get 'widget-child-value-get
3533 :value-inline 'widget-child-value-inline
3534 :default-get 'widget-type-default-get
3535 :match 'widget-type-match
3536 :validate 'widget-child-validate)
3539 ;;; The `plist' Widget.
3543 (define-widget 'plist 'list
3545 :key-type '(symbol :tag "Key")
3546 :value-type '(sexp :tag "Value")
3547 :convert-widget 'widget-plist-convert-widget
3550 (defvar widget-plist-value-type) ;Dynamic variable
3552 (defun widget-plist-convert-widget (widget)
3553 ;; Handle `:options'.
3554 (let* ((options (widget-get widget :options))
3555 (widget-plist-value-type (widget-get widget :value-type))
3556 (other `(editable-list :inline t
3558 ,(widget-get widget :key-type)
3559 ,widget-plist-value-type)))
3561 (list `(checklist :inline t
3563 ,@(mapcar 'widget-plist-convert-option
3567 (widget-put widget :args args)
3570 (defun widget-plist-convert-option (option)
3571 ;; Convert a single plist option.
3572 (let (key-type value-type)
3574 (let ((key (nth 0 option)))
3575 (setq value-type (nth 1 option))
3578 (setq key-type `(const ,key))))
3579 (setq key-type `(const ,option)
3580 value-type widget-plist-value-type))
3581 `(group :format "Key: %v" :inline t ,key-type ,value-type)))
3584 ;;; The `alist' Widget.
3586 ;; Association lists.
3588 (define-widget 'alist 'list
3589 "An association list."
3590 :key-type '(sexp :tag "Key")
3591 :value-type '(sexp :tag "Value")
3592 :convert-widget 'widget-alist-convert-widget
3595 (defvar widget-alist-value-type) ;Dynamic variable
3597 (defun widget-alist-convert-widget (widget)
3598 ;; Handle `:options'.
3599 (let* ((options (widget-get widget :options))
3600 (widget-alist-value-type (widget-get widget :value-type))
3601 (other `(editable-list :inline t
3603 ,(widget-get widget :key-type)
3604 ,widget-alist-value-type)))
3606 (list `(checklist :inline t
3608 ,@(mapcar 'widget-alist-convert-option
3612 (widget-put widget :args args)
3615 (defun widget-alist-convert-option (option)
3616 ;; Convert a single alist option.
3617 (let (key-type value-type)
3619 (let ((key (nth 0 option)))
3620 (setq value-type (nth 1 option))
3623 (setq key-type `(const ,key))))
3624 (setq key-type `(const ,option)
3625 value-type widget-alist-value-type))
3626 `(cons :format "Key: %v" ,key-type ,value-type)))
3628 (define-widget 'choice 'menu-choice
3629 "A union of several sexp types."
3631 :format "%{%t%}: %[Value Menu%] %v"
3632 :button-prefix 'widget-push-button-prefix
3633 :button-suffix 'widget-push-button-suffix
3634 :prompt-value 'widget-choice-prompt-value)
3636 (defun widget-choice-prompt-value (widget prompt value unbound)
3638 (let ((args (widget-get widget :args))
3639 (completion-ignore-case (widget-get widget :case-fold))
3640 current choices old)
3641 ;; Find the first arg that matches VALUE.
3644 (if (widget-apply (car look) :match value)
3645 (setq old (car look)
3647 (setq look (cdr look)))))
3650 (cond ((= (length args) 0)
3652 ((= (length args) 1)
3654 ((and (= (length args) 2)
3656 (if (eq old (nth 0 args))
3661 (setq current (car args)
3664 (cons (cons (widget-apply current :menu-tag-get)
3667 (let ((val (completing-read prompt choices nil t)))
3669 (let ((try (try-completion val choices)))
3672 (cdr (assoc val choices)))
3675 (widget-prompt-value current prompt nil t)
3678 (define-widget 'radio 'radio-button-choice
3679 "A union of several sexp types."
3681 :format "%{%t%}:\n%v"
3682 :prompt-value 'widget-choice-prompt-value)
3684 (define-widget 'repeat 'editable-list
3685 "A variable length homogeneous list."
3687 :format "%{%t%}:\n%v%i\n")
3689 (define-widget 'set 'checklist
3690 "A list of members from a fixed set."
3692 :format "%{%t%}:\n%v")
3694 (define-widget 'boolean 'toggle
3695 "To be nil or non-nil, that is the question."
3697 :prompt-value 'widget-boolean-prompt-value
3698 :button-prefix 'widget-push-button-prefix
3699 :button-suffix 'widget-push-button-suffix
3700 :format "%{%t%}: %[Toggle%] %v\n"
3704 (defun widget-boolean-prompt-value (widget prompt value unbound)
3705 ;; Toggle a boolean.
3708 ;;; The `color' Widget.
3711 (define-widget 'color 'editable-field
3712 "Choose a color name (with sample)."
3713 :format "%{%t%}: %v (%{sample%})\n"
3714 :value-create 'widget-color-value-create
3718 :complete 'widget-color-complete
3719 :sample-face-get 'widget-color-sample-face-get
3720 :notify 'widget-color-notify
3721 :action 'widget-color-action)
3723 (defun widget-color-value-create (widget)
3724 (widget-field-value-create widget)
3726 (widget-create-child-and-convert
3728 :tag "Choose" :action 'widget-color--choose-action)
3729 (widget-insert " "))
3731 (defun widget-color--choose-action (widget &optional event)
3732 (list-colors-display
3735 (when (buffer-live-p ,(current-buffer))
3736 (widget-value-set ',(widget-get widget :parent) color)
3737 (let* ((buf (get-buffer "*Colors*"))
3738 (win (get-buffer-window buf 0)))
3740 (and win (> (length (window-list)) 1)
3741 (delete-window win)))
3742 (pop-to-buffer ,(current-buffer))))))
3744 (defun widget-color-complete (widget)
3745 "Complete the color in WIDGET."
3746 (require 'facemenu) ; for facemenu-color-alist
3747 (completion-in-region (widget-field-start widget)
3748 (max (point) (widget-field-text-end widget))
3749 (or facemenu-color-alist
3750 (sort (defined-colors) 'string-lessp))))
3752 (defun widget-color-sample-face-get (widget)
3753 (let* ((value (condition-case nil
3754 (widget-value widget)
3755 (error (widget-get widget :value)))))
3756 (if (color-defined-p value)
3757 (list (cons 'foreground-color value))
3760 (defun widget-color-action (widget &optional event)
3761 "Prompt for a color."
3762 (let* ((tag (widget-apply widget :menu-tag-get))
3763 (prompt (concat tag ": "))
3764 (value (widget-value widget))
3765 (start (widget-field-start widget))
3766 (answer (facemenu-read-color prompt)))
3767 (unless (zerop (length answer))
3768 (widget-value-set widget answer)
3770 (widget-apply widget :notify widget event))))
3772 (defun widget-color-notify (widget child &optional event)
3773 "Update the sample, and notify the parent."
3774 (overlay-put (widget-get widget :sample-overlay)
3775 'face (widget-apply widget :sample-face-get))
3776 (widget-default-notify widget child event))
3780 (defun widget-echo-help (pos)
3781 "Display help-echo text for widget at POS."
3782 (let* ((widget (widget-at pos))
3783 (help-echo (and widget (widget-get widget :help-echo))))
3784 (if (functionp help-echo)
3785 (setq help-echo (funcall help-echo widget)))
3786 (if help-echo (message "%s" (eval help-echo)))))
3792 ;; arch-tag: a076e75e-18a1-4b46-8be5-3f317bcbc707
3793 ;;; wid-edit.el ends here