1 ;;; wid-edit.el --- Functions for creating and using widgets -*-byte-compile-dynamic: t; lexical-binding:t -*-
3 ;; Copyright (C) 1996-1997, 1999-2015 Free Software Foundation, Inc.
5 ;; Author: Per Abrahamsen <abraham@dina.kvl.dk>
6 ;; Maintainer: emacs-devel@gnu.org
7 ;; 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.]
62 (defun widget-event-point (event)
63 "Character position of the end of event if that exists, or nil."
64 (posn-point (event-end event
)))
66 (defun widget-button-release-event-p (event)
67 "Non-nil if EVENT is a mouse-button-release event object."
69 (memq (event-basic-type event
) '(mouse-1 mouse-2 mouse-3
))
70 (or (memq 'click
(event-modifiers event
))
71 (memq 'drag
(event-modifiers event
)))))
76 "Customization support for the Widget Library."
77 :link
'(custom-manual "(widget)Top")
78 :link
'(emacs-library-link :tag
"Lisp File" "widget.el")
82 (defgroup widget-documentation nil
83 "Options controlling the display of documentation strings."
86 (defgroup widget-faces nil
87 "Faces used by the widget library."
91 (defvar widget-documentation-face
'widget-documentation
92 "Face used for documentation strings in widgets.
93 This exists as a variable so it can be set locally in certain buffers.")
95 (defface widget-documentation
'((((class color
)
97 (:foreground
"lime green"))
100 (:foreground
"dark green"))
102 "Face used for documentation text."
103 :group
'widget-documentation
104 :group
'widget-faces
)
105 (define-obsolete-face-alias 'widget-documentation-face
106 'widget-documentation
"22.1")
108 (defvar widget-button-face
'widget-button
109 "Face used for buttons in widgets.
110 This exists as a variable so it can be set locally in certain buffers.")
112 (defface widget-button
'((t (:weight bold
)))
113 "Face used for widget buttons."
114 :group
'widget-faces
)
115 (define-obsolete-face-alias 'widget-button-face
'widget-button
"22.1")
117 (defcustom widget-mouse-face
'highlight
118 "Face used for widget buttons when the mouse is above them."
120 :group
'widget-faces
)
122 ;; TTY gets special definitions here and in the next defface, because
123 ;; the gray colors defined for other displays cause black text on a black
124 ;; background, at least on light-background TTYs.
125 (defface widget-field
'((((type tty
))
126 :background
"yellow3"
128 (((class grayscale color
)
130 :background
"gray85")
131 (((class grayscale color
)
133 :background
"dim gray")
136 "Face used for editable fields."
137 :group
'widget-faces
)
138 (define-obsolete-face-alias 'widget-field-face
'widget-field
"22.1")
140 (defface widget-single-line-field
'((((type tty
))
143 (((class grayscale color
)
145 :background
"gray85")
146 (((class grayscale color
)
148 :background
"dim gray")
151 "Face used for editable fields spanning only a single line."
152 :group
'widget-faces
)
153 (define-obsolete-face-alias 'widget-single-line-field-face
154 'widget-single-line-field
"22.1")
156 ;;; This causes display-table to be loaded, and not usefully.
157 ;;;(defvar widget-single-line-display-table
158 ;;; (let ((table (make-display-table)))
159 ;;; (aset table 9 "^I")
160 ;;; (aset table 10 "^J")
162 ;;; "Display table used for single-line editable fields.")
164 ;;;(when (fboundp 'set-face-display-table)
165 ;;; (set-face-display-table 'widget-single-line-field-face
166 ;;; widget-single-line-display-table))
168 ;;; Utility functions.
170 ;; These are not really widget specific.
172 (defun widget-princ-to-string (object)
173 "Return string representation of OBJECT, any Lisp object.
174 No quoting characters are used; no delimiters are printed around
175 the contents of strings."
176 (with-output-to-string
179 (defun widget-clear-undo ()
180 "Clear all undo information."
181 (buffer-disable-undo (current-buffer))
182 (buffer-enable-undo))
184 (defcustom widget-menu-max-size
40
185 "Largest number of items allowed in a popup-menu.
186 Larger menus are read through the minibuffer."
190 (defcustom widget-menu-max-shortcuts
40
191 "Largest number of items for which it works to choose one with a character.
192 For a larger number of items, the minibuffer is used."
196 (defcustom widget-menu-minibuffer-flag nil
197 "Control how to ask for a choice from the keyboard.
198 Non-nil means use the minibuffer;
199 nil means read a single character."
203 (defun widget-choose (title items
&optional event
)
204 "Choose an item from a list.
206 First argument TITLE is the name of the list.
207 Second argument ITEMS is a list whose members are either
208 (NAME . VALUE), to indicate selectable items, or just strings to
209 indicate unselectable items.
210 Optional third argument EVENT is an input event.
212 The user is asked to choose between each NAME from the items alist,
213 and the VALUE of the chosen element will be returned. If EVENT is a
214 mouse event, and the number of elements in items is less than
215 `widget-menu-max-size', a popup menu will be used, otherwise the
217 (cond ((and (< (length items
) widget-menu-max-size
)
218 event
(display-popup-menus-p))
221 (list title
(cons "" items
))))
222 ((or widget-menu-minibuffer-flag
223 (> (length items
) widget-menu-max-shortcuts
))
224 ;; Read the choice of name from the minibuffer.
225 (setq items
(cl-remove-if 'stringp items
))
226 (let ((val (completing-read (concat title
": ") items nil t
)))
228 (let ((try (try-completion val items
)))
231 (cdr (assoc val items
))))))
233 ;; Construct a menu of the choices
234 ;; and then use it for prompting for a single character.
235 (let* ((next-digit ?
0)
236 (map (make-sparse-keymap))
237 choice some-choice-enabled value
)
238 (with-current-buffer (get-buffer-create " widget-choose")
240 (insert "Available choices:\n\n")
242 (setq choice
(pop items
))
244 (let* ((name (car choice
))
245 (function (cdr choice
)))
246 (insert (format "%c = %s\n" next-digit name
))
247 (define-key map
(vector next-digit
) function
)
248 (setq some-choice-enabled t
)))
249 ;; Allocate digits to disabled alternatives
250 ;; so that the digit of a given alternative never varies.
251 (setq next-digit
(1+ next-digit
)))
252 (insert "\nC-g = Quit")
253 (goto-char (point-min))
255 (or some-choice-enabled
256 (error "None of the choices is currently meaningful"))
257 (define-key map
[?\M-\C-v
] 'scroll-other-window
)
258 (define-key map
[?\M--
] 'negative-argument
)
259 (save-window-excursion
260 (let ((buf (get-buffer " widget-choose")))
261 (fit-window-to-buffer (display-buffer buf
))
262 (let ((cursor-in-echo-area t
)
265 (setq value
(lookup-key map
(read-key-sequence (format "%s: " title
))))
267 (user-error "Canceled"))
269 (cond ((eq value
'scroll-other-window
)
270 (let ((minibuffer-scroll-window
271 (get-buffer-window buf
)))
273 (scroll-other-window-down
274 (window-height minibuffer-scroll-window
))
275 (scroll-other-window))
277 ((eq value
'negative-argument
)
279 (setq value nil
))))))
282 ;;; Widget text specifications.
284 ;; These functions are for specifying text properties.
286 ;; We can set it to nil now that get_local_map uses get_pos_property.
287 (defconst widget-field-add-space nil
288 "Non-nil means add extra space at the end of editable text fields.
289 If you don't add the space, it will become impossible to edit a zero
292 (defvar widget-field-use-before-change t
293 "Non-nil means use `before-change-functions' to track editable fields.
294 This enables the use of undo. Using before hooks also means that
295 the :notify function can't know the new value.")
297 (defun widget-specify-field (widget from to
)
298 "Specify editable button for WIDGET between FROM and TO."
299 ;; Terminating space is not part of the field, but necessary in
300 ;; order for local-map to work. Remove next sexp if local-map works
301 ;; at the end of the overlay.
304 (cond ((null (widget-get widget
:size
))
306 (widget-field-add-space
307 (insert-and-inherit " ")))
309 (let ((keymap (widget-get widget
:keymap
))
310 (face (or (widget-get widget
:value-face
) 'widget-field
))
311 (help-echo (widget-get widget
:help-echo
))
312 (follow-link (widget-get widget
:follow-link
))
314 (or (not widget-field-add-space
) (widget-get widget
:size
))))
315 (if (functionp help-echo
)
316 (setq help-echo
'widget-mouse-help
))
317 (when (= (char-before to
) ?
\n)
318 ;; When the last character in the field is a newline, we want to
319 ;; give it a `field' char-property of `boundary', which helps the
320 ;; C-n/C-p act more naturally when entering/leaving the field. We
321 ;; do this by making a small secondary overlay to contain just that
323 (let ((overlay (make-overlay (1- to
) to nil t nil
)))
324 (overlay-put overlay
'field
'boundary
)
325 ;; We need the real field for tabbing.
326 (overlay-put overlay
'real-field widget
)
327 ;; Use `local-map' here, not `keymap', so that normal editing
328 ;; works in the field when, say, Custom uses `suppress-keymap'.
329 (overlay-put overlay
'local-map keymap
)
330 (overlay-put overlay
'face face
)
331 (overlay-put overlay
'follow-link follow-link
)
332 (overlay-put overlay
'help-echo help-echo
))
334 (setq rear-sticky t
))
335 (let ((overlay (make-overlay from to nil nil rear-sticky
)))
336 (widget-put widget
:field-overlay overlay
)
337 ;;(overlay-put overlay 'detachable nil)
338 (overlay-put overlay
'field widget
)
339 (overlay-put overlay
'local-map keymap
)
340 (overlay-put overlay
'face face
)
341 (overlay-put overlay
'follow-link follow-link
)
342 (overlay-put overlay
'help-echo help-echo
)))
343 (widget-specify-secret widget
))
345 (defun widget-specify-secret (field)
346 "Replace text in FIELD with value of `:secret', if non-nil."
347 (let ((secret (widget-get field
:secret
))
348 (size (widget-get field
:size
)))
350 (let ((begin (widget-field-start field
))
351 (end (widget-field-end field
)))
353 (while (and (> end begin
)
354 (eq (char-after (1- end
)) ?\s
))
355 (setq end
(1- end
))))
357 (let ((old (char-after begin
)))
358 (unless (eq old secret
)
359 (subst-char-in-region begin
(1+ begin
) old secret
)
360 (put-text-property begin
(1+ begin
) 'secret old
))
361 (setq begin
(1+ begin
))))))))
363 (defun widget-specify-button (widget from to
)
364 "Specify button for WIDGET between FROM and TO."
365 (let ((overlay (make-overlay from to nil t nil
))
366 (follow-link (widget-get widget
:follow-link
))
367 (help-echo (widget-get widget
:help-echo
)))
368 (widget-put widget
:button-overlay overlay
)
369 (if (functionp help-echo
)
370 (setq help-echo
'widget-mouse-help
))
371 (overlay-put overlay
'button widget
)
372 (overlay-put overlay
'keymap
(widget-get widget
:keymap
))
373 (overlay-put overlay
'evaporate t
)
374 ;; We want to avoid the face with image buttons.
375 (unless (widget-get widget
:suppress-face
)
376 (overlay-put overlay
'face
(widget-apply widget
:button-face-get
))
377 (overlay-put overlay
'mouse-face
378 ;; Make new list structure for the mouse-face value
379 ;; so that different widgets will have
380 ;; different `mouse-face' property values
381 ;; and will highlight separately.
382 (let ((mouse-face-value
383 (widget-apply widget
:mouse-face-get
)))
384 ;; If it's a list, copy it.
385 (if (listp mouse-face-value
)
386 (copy-sequence mouse-face-value
)
387 ;; If it's a symbol, put it in a list.
388 (list mouse-face-value
)))))
389 (overlay-put overlay
'pointer
'hand
)
390 (overlay-put overlay
'follow-link follow-link
)
391 (overlay-put overlay
'help-echo help-echo
)))
393 (defun widget-mouse-help (_window overlay _point
)
394 "Help-echo callback for widgets whose :help-echo is a function."
395 (with-current-buffer (overlay-buffer overlay
)
396 (let* ((widget (widget-at (overlay-start overlay
)))
397 (help-echo (if widget
(widget-get widget
:help-echo
))))
398 (if (functionp help-echo
)
399 (funcall help-echo widget
)
402 (defun widget-specify-sample (widget from to
)
403 "Specify sample for WIDGET between FROM and TO."
404 (let ((overlay (make-overlay from to nil t nil
)))
405 (overlay-put overlay
'face
(widget-apply widget
:sample-face-get
))
406 (overlay-put overlay
'evaporate t
)
407 (widget-put widget
:sample-overlay overlay
)))
409 (defun widget-specify-doc (widget from to
)
410 "Specify documentation for WIDGET between FROM and TO."
411 (let ((overlay (make-overlay from to nil t nil
)))
412 (overlay-put overlay
'widget-doc widget
)
413 (overlay-put overlay
'face widget-documentation-face
)
414 (overlay-put overlay
'evaporate t
)
415 (widget-put widget
:doc-overlay overlay
)))
417 (defmacro widget-specify-insert
(&rest form
)
418 "Execute FORM without inheriting any text properties."
420 (let ((inhibit-read-only t
)
421 (inhibit-modification-hooks t
))
422 (narrow-to-region (point) (point))
423 (prog1 (progn ,@form
)
424 (goto-char (point-max))))))
426 (defface widget-inactive
427 '((t :inherit shadow
))
428 "Face used for inactive widgets."
429 :group
'widget-faces
)
430 (define-obsolete-face-alias 'widget-inactive-face
431 'widget-inactive
"22.1")
433 (defun widget-specify-inactive (widget from to
)
434 "Make WIDGET inactive for user modifications."
435 (unless (widget-get widget
:inactive
)
436 (let ((overlay (make-overlay from to nil t nil
)))
437 (overlay-put overlay
'face
'widget-inactive
)
438 ;; This is disabled, as it makes the mouse cursor change shape.
439 ;; (overlay-put overlay 'mouse-face 'widget-inactive)
440 (overlay-put overlay
'evaporate t
)
441 (overlay-put overlay
'priority
100)
442 (overlay-put overlay
'modification-hooks
'(widget-overlay-inactive))
443 (widget-put widget
:inactive overlay
))))
445 (defun widget-overlay-inactive (&rest _junk
)
446 "Ignoring the arguments, signal an error."
447 (unless inhibit-read-only
448 (error "The widget here is not active")))
451 (defun widget-specify-active (widget)
452 "Make WIDGET active for user modifications."
453 (let ((inactive (widget-get widget
:inactive
)))
455 (delete-overlay inactive
)
456 (widget-put widget
:inactive nil
))))
458 ;;; Widget Properties.
460 (defsubst widget-type
(widget)
461 "Return the type of WIDGET. The type is a symbol."
465 (defun widgetp (widget)
466 "Return non-nil if WIDGET is a widget."
468 (get widget
'widget-type
)
470 (symbolp (car widget
))
471 (get (car widget
) 'widget-type
))))
473 (defun widget-get-indirect (widget property
)
474 "In WIDGET, get the value of PROPERTY.
475 If the value is a symbol, return its binding.
476 Otherwise, just return the value."
477 (let ((value (widget-get widget property
)))
482 (defun widget-member (widget property
)
483 "Non-nil if there is a definition in WIDGET for PROPERTY."
484 (cond ((plist-member (cdr widget
) property
)
487 (widget-member (get (car widget
) 'widget-type
) property
))
490 (defun widget-value (widget)
491 "Extract the current value of WIDGET."
493 :value-to-external
(widget-apply widget
:value-get
)))
495 (defun widget-value-set (widget value
)
496 "Set the current value of WIDGET to VALUE."
498 :value-set
(widget-apply widget
499 :value-to-internal value
)))
501 (defun widget-default-get (widget)
502 "Extract the default external value of WIDGET."
503 (widget-apply widget
:value-to-external
504 (or (widget-get widget
:value
)
506 (when (widget-get widget
:args
)
507 (setq widget
(widget-copy widget
))
509 (dolist (arg (widget-get widget
:args
))
510 (setq args
(append args
511 (if (widget-get arg
:inline
)
512 (widget-get arg
:args
)
514 (widget-put widget
:args args
)))
515 (widget-apply widget
:default-get
)))))
517 (defun widget-match-inline (widget vals
)
518 "In WIDGET, match the start of VALS."
519 (cond ((widget-get widget
:inline
)
520 (widget-apply widget
:match-inline vals
))
522 (widget-apply widget
:match
(car vals
)))
523 (cons (list (car vals
)) (cdr vals
)))
526 (defun widget-apply-action (widget &optional event
)
527 "Apply :action in WIDGET in response to EVENT."
528 (if (widget-apply widget
:active
)
529 (widget-apply widget
:action event
)
530 (error "Attempt to perform action on inactive widget")))
532 ;;; Helper functions.
534 ;; These are widget specific.
537 (defun widget-prompt-value (widget prompt
&optional value unbound
)
538 "Prompt for a value matching WIDGET, using PROMPT.
539 The current value is assumed to be VALUE, unless UNBOUND is non-nil."
540 (unless (listp widget
)
541 (setq widget
(list widget
)))
542 (setq prompt
(format "[%s] %s" (widget-type widget
) prompt
))
543 (setq widget
(widget-convert widget
))
544 (let ((answer (widget-apply widget
:prompt-value prompt value unbound
)))
545 (unless (widget-apply widget
:match answer
)
546 (error "Value does not match %S type" (car widget
)))
549 (defun widget-get-sibling (widget)
550 "Get the item WIDGET is assumed to toggle.
551 This is only meaningful for radio buttons or checkboxes in a list."
552 (let* ((children (widget-get (widget-get widget
:parent
) :children
))
556 (setq child
(car children
)
557 children
(cdr children
))
558 (when (eq (widget-get child
:button
) widget
)
559 (throw 'child child
)))
562 (defun widget-map-buttons (function &optional buffer maparg
)
563 "Map FUNCTION over the buttons in BUFFER.
564 FUNCTION is called with the arguments WIDGET and MAPARG.
566 If FUNCTION returns non-nil, the walk is canceled.
568 The arguments MAPARG, and BUFFER default to nil and (current-buffer),
570 (let ((cur (point-min))
573 (with-current-buffer buffer
(overlay-lists))
575 (setq overlays
(append (car overlays
) (cdr overlays
)))
576 (while (setq cur
(pop overlays
))
577 (setq widget
(overlay-get cur
'button
))
578 (if (and widget
(funcall function widget maparg
))
579 (setq overlays nil
)))))
583 (defcustom widget-image-directory
(file-name-as-directory
584 (expand-file-name "images/custom" data-directory
))
585 "Where widget button images are located.
586 If this variable is nil, widget will try to locate the directory
591 (defcustom widget-image-enable t
592 "If non-nil, use image buttons in widgets when available."
597 (defcustom widget-image-conversion
598 '((xpm ".xpm") (gif ".gif") (png ".png") (jpeg ".jpg" ".jpeg")
600 "Conversion alist from image formats to file name suffixes."
602 :type
'(repeat (cons :format
"%v"
603 (symbol :tag
"Image Format" unknown
)
604 (repeat :tag
"Suffixes"
605 (string :format
"%v")))))
607 (defun widget-image-find (image)
608 "Create a graphical button from IMAGE.
609 IMAGE should either already be an image, or be a file name sans
610 extension (xpm, xbm, gif, jpg, or png) located in
611 `widget-image-directory' or otherwise where `find-image' will find it."
612 (cond ((not (and image widget-image-enable
(display-graphic-p)))
613 ;; We don't want or can't use images.
616 (eq 'image
(car image
)))
617 ;; Already an image spec. Use it.
620 ;; A string. Look it up in relevant directories.
621 (let* ((load-path (cons widget-image-directory load-path
))
623 (dolist (elt widget-image-conversion
)
624 (dolist (ext (cdr elt
))
625 (push (list :type
(car elt
) :file
(concat image ext
))
627 (find-image (nreverse specs
))))
632 (defvar widget-button-pressed-face
'widget-button-pressed
633 "Face used for pressed buttons in widgets.
634 This exists as a variable so it can be set locally in certain
637 (defun widget-image-insert (widget tag image
&optional _down _inactive
)
638 "In WIDGET, insert the text TAG or, if supported, IMAGE.
639 IMAGE should either be an image or an image file name sans extension
640 \(xpm, xbm, gif, jpg, or png) located in `widget-image-directory'.
642 Optional arguments DOWN and INACTIVE are used instead of IMAGE when the
643 button is pressed or inactive, respectively. These are currently ignored."
644 (if (and (featurep 'image
)
645 (setq image
(widget-image-find image
)))
646 (progn (widget-put widget
:suppress-face t
)
647 (insert-image image tag
))
650 (defun widget-move-and-invoke (event)
651 "Move to where you click, and if it is an active field, invoke it."
653 (mouse-set-point event
)
654 (let ((pos (widget-event-point event
)))
655 (if (and pos
(get-char-property pos
'button
))
656 (widget-button-click event
))))
660 (defgroup widget-button nil
661 "The look of various kinds of buttons."
664 (defcustom widget-button-prefix
""
665 "String used as prefix for buttons."
667 :group
'widget-button
)
669 (defcustom widget-button-suffix
""
670 "String used as suffix for buttons."
672 :group
'widget-button
)
674 ;;; Creating Widgets.
677 (defun widget-create (type &rest args
)
678 "Create widget of TYPE.
679 The optional ARGS are additional keyword arguments."
680 (let ((widget (apply 'widget-convert type args
)))
681 (widget-apply widget
:create
)
684 (defun widget-create-child-and-convert (parent type
&rest args
)
685 "As part of the widget PARENT, create a child widget TYPE.
686 The child is converted, using the keyword arguments ARGS."
687 (let ((widget (apply 'widget-convert type args
)))
688 (widget-put widget
:parent parent
)
689 (unless (widget-get widget
:indent
)
690 (widget-put widget
:indent
(+ (or (widget-get parent
:indent
) 0)
691 (or (widget-get widget
:extra-offset
) 0)
692 (widget-get parent
:offset
))))
693 (widget-apply widget
:create
)
696 (defun widget-create-child (parent type
)
697 "Create widget of TYPE."
698 (let ((widget (widget-copy type
)))
699 (widget-put widget
:parent parent
)
700 (unless (widget-get widget
:indent
)
701 (widget-put widget
:indent
(+ (or (widget-get parent
:indent
) 0)
702 (or (widget-get widget
:extra-offset
) 0)
703 (widget-get parent
:offset
))))
704 (widget-apply widget
:create
)
707 (defun widget-create-child-value (parent type value
)
708 "Create widget of TYPE with value VALUE."
709 (let ((widget (widget-copy type
)))
710 (widget-put widget
:value
(widget-apply widget
:value-to-internal value
))
711 (widget-put widget
:parent parent
)
712 (unless (widget-get widget
:indent
)
713 (widget-put widget
:indent
(+ (or (widget-get parent
:indent
) 0)
714 (or (widget-get widget
:extra-offset
) 0)
715 (widget-get parent
:offset
))))
716 (widget-apply widget
:create
)
720 (defun widget-delete (widget)
722 (widget-apply widget
:delete
))
724 (defun widget-copy (widget)
725 "Make a deep copy of WIDGET."
726 (widget-apply (copy-sequence widget
) :copy
))
728 (defun widget-convert (type &rest args
)
729 "Convert TYPE to a widget without inserting it in the buffer.
730 The optional ARGS are additional keyword arguments."
731 ;; Don't touch the type.
732 (let* ((widget (if (symbolp type
)
734 (copy-sequence type
)))
738 ;; First set the :args keyword.
739 (while (cdr current
) ;Look in the type.
740 (if (and (keywordp (cadr current
))
741 ;; If the last element is a keyword,
742 ;; it is still the :args element,
743 ;; even though it is a keyword.
745 (if (eq (cadr current
) :args
)
746 ;; If :args is explicitly specified, obey it.
748 ;; Some other irrelevant keyword.
749 (setq current
(cdr (cdr current
))))
750 (setcdr current
(list :args
(cdr current
)))
752 (while (and args
(not done
)) ;Look in ARGS.
753 (cond ((eq (car args
) :args
)
754 ;; Handle explicit specification of :args.
755 (setq args
(cadr args
)
757 ((keywordp (car args
))
758 (setq args
(cddr args
)))
761 (widget-put widget
:args args
))
762 ;; Then Convert the widget.
765 (let ((convert-widget (plist-get (cdr type
) :convert-widget
)))
767 (setq widget
(funcall convert-widget widget
))))
768 (setq type
(get (car type
) 'widget-type
)))
769 ;; Finally set the keyword args.
771 (let ((next (nth 0 keys
)))
774 (widget-put widget next
(nth 1 keys
))
775 (setq keys
(nthcdr 2 keys
)))
777 ;; Convert the :value to internal format.
778 (if (widget-member widget
:value
)
780 :value
(widget-apply widget
782 (widget-get widget
:value
))))
783 ;; Return the newly create widget.
787 (defun widget-insert (&rest args
)
788 "Call `insert' with ARGS even if surrounding text is read only."
789 (let ((inhibit-read-only t
)
790 (inhibit-modification-hooks t
))
791 (apply 'insert args
)))
793 (defun widget-convert-text (type from to
794 &optional button-from button-to
796 "Return a widget of type TYPE with endpoint FROM TO.
797 No text will be inserted to the buffer, instead the text between FROM
798 and TO will be used as the widgets end points. If optional arguments
799 BUTTON-FROM and BUTTON-TO are given, these will be used as the widgets
801 Optional ARGS are extra keyword arguments for TYPE."
802 (let ((widget (apply 'widget-convert type
:delete
'widget-leave-text args
))
803 (from (copy-marker from
))
804 (to (copy-marker to
)))
805 (set-marker-insertion-type from t
)
806 (set-marker-insertion-type to nil
)
807 (widget-put widget
:from from
)
808 (widget-put widget
:to to
)
810 (widget-specify-button widget button-from button-to
))
813 (defun widget-convert-button (type from to
&rest args
)
814 "Return a widget of type TYPE with endpoint FROM TO.
815 Optional ARGS are extra keyword arguments for TYPE.
816 No text will be inserted to the buffer, instead the text between FROM
817 and TO will be used as the widgets end points, as well as the widgets
819 (apply 'widget-convert-text type from to from to args
))
821 (defun widget-leave-text (widget)
822 "Remove markers and overlays from WIDGET and its children."
823 (let ((button (widget-get widget
:button-overlay
))
824 (sample (widget-get widget
:sample-overlay
))
825 (doc (widget-get widget
:doc-overlay
))
826 (field (widget-get widget
:field-overlay
)))
827 (set-marker (widget-get widget
:from
) nil
)
828 (set-marker (widget-get widget
:to
) nil
)
830 (delete-overlay button
))
832 (delete-overlay sample
))
834 (delete-overlay doc
))
836 (delete-overlay field
))
837 (mapc 'widget-leave-text
(widget-get widget
:children
))))
839 ;;; Keymap and Commands.
841 ;; This alias exists only so that one can choose in doc-strings (e.g.
842 ;; Custom-mode) which key-binding of widget-keymap one wants to refer to.
843 ;; http://lists.gnu.org/archive/html/emacs-devel/2008-11/msg00480.html
844 (define-obsolete-function-alias 'advertised-widget-backward
845 'widget-backward
"23.2")
848 (defvar widget-keymap
849 (let ((map (make-sparse-keymap)))
850 (define-key map
"\t" 'widget-forward
)
851 (define-key map
"\e\t" 'widget-backward
)
852 (define-key map
[(shift tab
)] 'widget-backward
)
853 (put 'widget-backward
:advertised-binding
[(shift tab
)])
854 (define-key map
[backtab] 'widget-backward)
855 (define-key map [down-mouse-2] 'widget-button-click)
856 (define-key map [down-mouse-1] 'widget-button-click)
857 ;; The following definition needs to avoid using escape sequences that
858 ;; might get converted to ^M when building loaddefs.el
859 (define-key map [(control ?m)] 'widget-button-press)
861 "Keymap containing useful binding for buffers containing widgets.
862 Recommended as a parent keymap for modes using widgets.
863 Note that such modes will need to require wid-edit.")
865 (defvar widget-global-map global-map
866 "Keymap used for events a widget does not handle itself.")
867 (make-variable-buffer-local 'widget-global-map)
869 (defvar widget-field-keymap
870 (let ((map (copy-keymap widget-keymap)))
871 (define-key map "\C-k" 'widget-kill-line)
872 (define-key map "\M-\t" 'widget-complete)
873 (define-key map "\C-m" 'widget-field-activate)
874 ;; Since the widget code uses a `field' property to identify fields,
875 ;; ordinary beginning-of-line does the right thing.
876 ;; (define-key map "\C-a" 'widget-beginning-of-line)
877 (define-key map "\C-e" 'widget-end-of-line)
879 "Keymap used inside an editable field.")
881 (defvar widget-text-keymap
882 (let ((map (copy-keymap widget-keymap)))
883 ;; Since the widget code uses a `field' property to identify fields,
884 ;; ordinary beginning-of-line does the right thing.
885 ;; (define-key map "\C-a" 'widget-beginning-of-line)
886 (define-key map "\C-e" 'widget-end-of-line)
888 "Keymap used inside a text field.")
890 (defun widget-field-activate (pos &optional event)
891 "Invoke the editable field at point."
893 (let ((field (widget-field-at pos)))
895 (widget-apply-action field event)
897 (lookup-key widget-global-map (this-command-keys))))))
899 (defface widget-button-pressed
900 '((((min-colors 88) (class color))
901 (:foreground "red1"))
905 (:weight bold :underline t)))
906 "Face used for pressed buttons."
907 :group 'widget-faces)
908 (define-obsolete-face-alias 'widget-button-pressed-face
909 'widget-button-pressed "22.1")
911 (defvar widget-button-click-moves-point nil
912 "If non-nil, `widget-button-click' moves point to a button after invoking it.
913 If nil, point returns to its original position after invoking a button.")
915 (defun widget-button-click (event)
916 "Invoke the button that the mouse is pointing at."
918 (if (widget-event-point event)
919 (let* ((oevent event)
920 (mouse-1 (memq (event-basic-type event) '(mouse-1 down-mouse-1)))
921 (pos (widget-event-point event))
922 (start (event-start event))
923 (button (get-char-property
924 pos 'button (and (windowp (posn-window start))
925 (window-buffer (posn-window start)))))
927 (when (or (null button)
928 (catch 'button-press-cancelled
929 ;; Mouse click on a widget button. Do the following
930 ;; in a save-excursion so that the click on the button
931 ;; doesn't change point.
932 (save-selected-window
933 (select-window (posn-window (event-start event)))
935 (goto-char (posn-point (event-start event)))
936 (let* ((overlay (widget-get button :button-overlay))
937 (pressed-face (or (widget-get button :pressed-face)
938 widget-button-pressed-face))
939 (face (overlay-get overlay 'face))
940 (mouse-face (overlay-get overlay 'mouse-face)))
942 ;; Read events, including mouse-movement
943 ;; events, waiting for a release event. If we
944 ;; began with a mouse-1 event and receive a
945 ;; movement event, that means the user wants
946 ;; to perform drag-selection, so cancel the
947 ;; button press and do the default mouse-1
948 ;; action. For mouse-2, just highlight/
949 ;; unhighlight the button the mouse was
950 ;; initially on when we move over it.
952 (when face ; avoid changing around image
953 (overlay-put overlay 'face pressed-face)
954 (overlay-put overlay 'mouse-face pressed-face))
955 (unless (widget-apply button :mouse-down-action event)
956 (let ((track-mouse t))
957 (while (not (widget-button-release-event-p event))
958 (setq event (read-event))
959 (when (and mouse-1 (mouse-movement-p event))
960 (push event unread-command-events)
962 (throw 'button-press-cancelled t))
963 (unless (or (integerp event)
964 (memq (car event) '(switch-frame select-window))
965 (eq (car event) 'scroll-bar-movement))
966 (setq pos (widget-event-point event))
968 (eq (get-char-property pos 'button)
971 (overlay-put overlay 'face pressed-face)
972 (overlay-put overlay 'mouse-face pressed-face))
973 (overlay-put overlay 'face face)
974 (overlay-put overlay 'mouse-face mouse-face))))))
976 ;; When mouse is released over the button, run
977 ;; its action function.
978 (when (and pos (eq (get-char-property pos 'button) button))
980 (widget-apply-action button event)
981 (if widget-button-click-moves-point
982 (setq newpoint (point)))))
983 (overlay-put overlay 'face face)
984 (overlay-put overlay 'mouse-face mouse-face))))
986 (if newpoint (goto-char newpoint))
987 ;; This loses if the widget action switches windows. -- cyd
988 ;; (unless (pos-visible-in-window-p (widget-event-point event))
989 ;; (mouse-set-point event)
990 ;; (beginning-of-line)
994 (let ((up t) command)
995 ;; Mouse click not on a widget button. Find the global
996 ;; command to run, and check whether it is bound to an
999 (cond ((setq command ;down event
1000 (lookup-key widget-global-map [down-mouse-1]))
1002 ((setq command ;up event
1003 (lookup-key widget-global-map [mouse-1]))))
1004 (cond ((setq command ;down event
1005 (lookup-key widget-global-map [down-mouse-2]))
1007 ((setq command ;up event
1008 (lookup-key widget-global-map [mouse-2])))))
1010 ;; Don't execute up events twice.
1011 (while (not (widget-button-release-event-p event))
1012 (setq event (read-event))))
1014 (call-interactively command)))))
1015 (message "You clicked somewhere weird.")))
1017 (defun widget-button-press (pos &optional event)
1018 "Invoke button at POS."
1020 (let ((button (get-char-property pos 'button)))
1022 (widget-apply-action button event)
1023 (let ((command (lookup-key widget-global-map (this-command-keys))))
1024 (when (commandp command)
1025 (call-interactively command))))))
1027 (defun widget-tabable-at (&optional pos)
1028 "Return the tabable widget at POS, or nil.
1029 POS defaults to the value of (point)."
1030 (let ((widget (widget-at pos)))
1032 (let ((order (widget-get widget :tab-order)))
1038 (defvar widget-use-overlay-change t
1039 "If non-nil, use overlay change functions to tab around in the buffer.
1040 This is much faster.")
1042 (defun widget-move (arg)
1043 "Move point to the ARG next field or button.
1044 ARG may be negative to move backward."
1045 (or (bobp) (> arg 0) (backward-char))
1048 (old (widget-tabable-at)))
1052 (goto-char (point-min))
1053 (setq wrapped (1+ wrapped)))
1054 (widget-use-overlay-change
1055 (goto-char (next-overlay-change (point))))
1060 (error "No buttons or fields found"))
1061 (let ((new (widget-tabable-at)))
1063 (unless (eq new old)
1069 (goto-char (point-max))
1070 (setq wrapped (1+ wrapped)))
1071 (widget-use-overlay-change
1072 (goto-char (previous-overlay-change (point))))
1077 (error "No buttons or fields found"))
1078 (let ((new (widget-tabable-at)))
1080 (unless (eq new old)
1081 (setq arg (1+ arg))))))
1082 (let ((new (widget-tabable-at)))
1083 (while (eq (widget-tabable-at) new)
1086 (widget-echo-help (point))
1087 (run-hooks 'widget-move-hook))
1089 (defun widget-forward (arg)
1090 "Move point to the next field or button.
1091 With optional ARG, move across that many fields."
1093 (run-hooks 'widget-forward-hook)
1096 (defun widget-backward (arg)
1097 "Move point to the previous field or button.
1098 With optional ARG, move across that many fields."
1100 (run-hooks 'widget-backward-hook)
1101 (widget-move (- arg)))
1103 ;; Since the widget code uses a `field' property to identify fields,
1104 ;; ordinary beginning-of-line does the right thing.
1105 (defalias 'widget-beginning-of-line 'beginning-of-line)
1107 (defun widget-end-of-line ()
1108 "Go to end of field or end of line, whichever is first.
1109 Trailing spaces at the end of padded fields are not considered part of
1112 ;; Ordinary end-of-line does the right thing, because we're inside
1113 ;; text with a `field' property.
1116 ;; ... except that we want to ignore trailing spaces in fields that
1117 ;; aren't terminated by a newline, because they are used as padding,
1118 ;; and ignored when extracting the entered value of the field.
1119 (skip-chars-backward " " (field-beginning (1- (point))))))
1121 (defun widget-kill-line ()
1122 "Kill to end of field or end of line, whichever is first."
1124 (let* ((field (widget-field-find (point)))
1125 (end (and field (widget-field-end field))))
1126 (if (and field (> (line-beginning-position 2) end))
1127 (kill-region (point) end)
1128 (call-interactively 'kill-line))))
1130 (defun widget-narrow-to-field ()
1133 (let ((field (widget-field-find (point))))
1135 (narrow-to-region (line-beginning-position) (line-end-position)))))
1137 ;; This used to say:
1138 ;; "When not inside a field, move to the previous button or field."
1139 ;; but AFAICS, it has always just thrown an error.
1140 (defun widget-complete ()
1141 "Complete content of editable field from point.
1142 When not inside a field, signal an error."
1144 (let ((data (widget-completions-at-point)))
1146 ((functionp data) (funcall data))
1148 (let ((completion-extra-properties (nth 3 data)))
1149 (completion-in-region (nth 0 data) (nth 1 data) (nth 2 data)
1150 (plist-get completion-extra-properties
1153 (error "Not in an editable field")))))
1154 ;; We may want to use widget completion in buffers where the major mode
1155 ;; hasn't added widget-completions-at-point to completion-at-point-functions,
1156 ;; so it's not really obsolete (yet).
1157 ;; (make-obsolete 'widget-complete 'completion-at-point "24.1")
1159 (defun widget-completions-at-point ()
1160 (let ((field (widget-field-find (point))))
1162 (widget-apply field :completions-function))))
1164 ;;; Setting up the buffer.
1166 (defvar widget-field-new nil
1167 "List of all newly created editable fields in the buffer.")
1168 (make-variable-buffer-local 'widget-field-new)
1170 (defvar widget-field-list nil
1171 "List of all editable fields in the buffer.")
1172 (make-variable-buffer-local 'widget-field-list)
1174 (defun widget-at (&optional pos)
1175 "The button or field at POS (default, point)."
1176 (or (get-char-property (or pos (point)) 'button)
1177 (widget-field-at pos)))
1180 (defun widget-setup ()
1181 "Setup current buffer so editing string widgets works."
1182 (let ((inhibit-read-only t)
1183 (inhibit-modification-hooks t)
1185 (while widget-field-new
1186 (setq field (car widget-field-new)
1187 widget-field-new (cdr widget-field-new)
1188 widget-field-list (cons field widget-field-list))
1189 (let ((from (car (widget-get field :field-overlay)))
1190 (to (cdr (widget-get field :field-overlay))))
1191 (widget-specify-field field
1192 (marker-position from) (marker-position to))
1193 (set-marker from nil)
1194 (set-marker to nil))))
1196 (widget-add-change))
1198 (defvar widget-field-last nil)
1199 ;; Last field containing point.
1200 (make-variable-buffer-local 'widget-field-last)
1202 (defvar widget-field-was nil)
1203 ;; The widget data before the change.
1204 (make-variable-buffer-local 'widget-field-was)
1206 (defun widget-field-at (pos)
1207 "Return the widget field at POS, or nil if none."
1208 (let ((field (get-char-property (or pos (point)) 'field)))
1209 (if (eq field 'boundary)
1210 (get-char-property (or pos (point)) 'real-field)
1213 (defun widget-field-buffer (widget)
1214 "Return the buffer of WIDGET's editing field."
1215 (let ((overlay (widget-get widget :field-overlay)))
1216 (cond ((overlayp overlay)
1217 (overlay-buffer overlay))
1219 (marker-buffer (car overlay))))))
1221 (defun widget-field-start (widget)
1222 "Return the start of WIDGET's editing field."
1223 (let ((overlay (widget-get widget :field-overlay)))
1224 (if (overlayp overlay)
1225 (overlay-start overlay)
1228 (defun widget-field-end (widget)
1229 "Return the end of WIDGET's editing field."
1230 (let ((overlay (widget-get widget :field-overlay)))
1231 ;; Don't subtract one if local-map works at the end of the overlay,
1232 ;; or if a special `boundary' field has been added after the widget
1234 (if (overlayp overlay)
1235 ;; Don't proceed if overlay has been removed from buffer.
1236 (when (overlay-buffer overlay)
1237 (if (and (not (eq (with-current-buffer
1238 (widget-field-buffer widget)
1240 ;; `widget-narrow-to-field' can be
1241 ;; active when this function is called
1242 ;; from an change-functions hook. So
1243 ;; temporarily remove field narrowing
1244 ;; before to call `get-char-property'.
1246 (get-char-property (overlay-end overlay)
1249 (or widget-field-add-space
1250 (null (widget-get widget :size))))
1251 (1- (overlay-end overlay))
1252 (overlay-end overlay)))
1255 (defun widget-field-text-end (widget)
1256 (let ((to (widget-field-end widget))
1257 (size (widget-get widget :size)))
1258 (if (or (null size) (zerop size))
1260 (let ((from (widget-field-start widget)))
1262 (with-current-buffer (widget-field-buffer widget)
1263 (while (and (> to from)
1264 (eq (char-after (1- to)) ?\s))
1268 (defun widget-field-find (pos)
1269 "Return the field at POS.
1270 Unlike (get-char-property POS 'field), this works with empty fields too."
1271 (let ((fields widget-field-list)
1274 (setq field (car fields)
1275 fields (cdr fields))
1276 (when (and (<= (widget-field-start field) pos)
1277 (<= pos (widget-field-end field)))
1279 (error "Overlapping fields"))
1280 (setq found field)))
1283 (defun widget-before-change (from to)
1284 ;; This is how, for example, a variable changes its state to `modified'.
1285 ;; when it is being edited.
1286 (unless inhibit-read-only
1287 (let ((from-field (widget-field-find from))
1288 (to-field (widget-field-find to)))
1289 (cond ((not (eq from-field to-field))
1290 (add-hook 'post-command-hook 'widget-add-change nil t)
1291 (signal 'text-read-only
1292 '("Change should be restricted to a single field")))
1294 (add-hook 'post-command-hook 'widget-add-change nil t)
1295 (signal 'text-read-only
1296 '("Attempt to change text outside editable field")))
1297 (widget-field-use-before-change
1298 (widget-apply from-field :notify from-field))))))
1300 (defun widget-add-change ()
1301 (remove-hook 'post-command-hook 'widget-add-change t)
1302 (add-hook 'before-change-functions 'widget-before-change nil t)
1303 (add-hook 'after-change-functions 'widget-after-change nil t))
1305 (defun widget-after-change (from to _old)
1306 "Adjust field size and text properties."
1307 (let ((field (widget-field-find from))
1308 (other (widget-field-find to)))
1310 (unless (eq field other)
1311 (error "Change in different fields"))
1312 (let ((size (widget-get field :size)))
1314 (let ((begin (widget-field-start field))
1315 (end (widget-field-end field)))
1316 (cond ((< (- end begin) size)
1320 (insert-char ?\s (- (+ begin size) end))))
1321 ((> (- end begin) size)
1322 ;; Field too large and
1323 (if (or (< (point) (+ begin size))
1325 ;; Point is outside extra space.
1326 (setq begin (+ begin size))
1327 ;; Point is within the extra space.
1328 (setq begin (point)))
1331 (while (and (eq (preceding-char) ?\s)
1333 (delete-char -1)))))))
1334 (widget-specify-secret field))
1335 (widget-apply field :notify field))))
1337 ;;; Widget Functions
1339 ;; These functions are used in the definition of multiple widgets.
1341 (defun widget-parent-action (widget &optional event)
1342 "Tell :parent of WIDGET to handle the :action.
1343 Optional EVENT is the event that triggered the action."
1344 (widget-apply (widget-get widget :parent) :action event))
1346 (defun widget-children-value-delete (widget)
1347 "Delete all :children and :buttons in WIDGET."
1348 (mapc 'widget-delete (widget-get widget :children))
1349 (widget-put widget :children nil)
1350 (mapc 'widget-delete (widget-get widget :buttons))
1351 (widget-put widget :buttons nil))
1353 (defun widget-children-validate (widget)
1354 "All the :children must be valid."
1355 (let ((children (widget-get widget :children))
1357 (while (and children (not found))
1358 (setq child (car children)
1359 children (cdr children)
1360 found (widget-apply child :validate)))
1363 (defun widget-child-value-get (widget)
1364 "Get the value of the first member of :children in WIDGET."
1365 (widget-value (car (widget-get widget :children))))
1367 (defun widget-child-value-inline (widget)
1368 "Get the inline value of the first member of :children in WIDGET."
1369 (widget-apply (car (widget-get widget :children)) :value-inline))
1371 (defun widget-child-validate (widget)
1372 "The result of validating the first member of :children in WIDGET."
1373 (widget-apply (car (widget-get widget :children)) :validate))
1375 (defun widget-type-value-create (widget)
1376 "Convert and instantiate the value of the :type attribute of WIDGET.
1377 Store the newly created widget in the :children attribute.
1379 The value of the :type attribute should be an unconverted widget type."
1380 (let ((value (widget-get widget :value))
1381 (type (widget-get widget :type)))
1382 (widget-put widget :children
1383 (list (widget-create-child-value widget
1384 (widget-convert type)
1387 (defun widget-type-default-get (widget)
1388 "Get default value from the :type attribute of WIDGET.
1390 The value of the :type attribute should be an unconverted widget type."
1391 (widget-default-get (widget-convert (widget-get widget :type))))
1393 (defun widget-type-match (widget value)
1394 "Non-nil if the :type value of WIDGET matches VALUE.
1396 The value of the :type attribute should be an unconverted widget type."
1397 (widget-apply (widget-convert (widget-get widget :type)) :match value))
1399 (defun widget-types-copy (widget)
1400 "Copy :args as widget types in WIDGET."
1401 (widget-put widget :args (mapcar 'widget-copy (widget-get widget :args)))
1404 ;; Made defsubst to speed up face editor creation.
1405 (defsubst widget-types-convert-widget (widget)
1406 "Convert :args as widget types in WIDGET."
1407 (widget-put widget :args (mapcar 'widget-convert (widget-get widget :args)))
1410 (defun widget-value-convert-widget (widget)
1411 "Initialize :value from :args in WIDGET."
1412 (let ((args (widget-get widget :args)))
1414 (widget-put widget :value (car args))
1415 ;; Don't convert :value here, as this is done in `widget-convert'.
1416 ;; (widget-put widget :value (widget-apply widget
1417 ;; :value-to-internal (car args)))
1418 (widget-put widget :args nil)))
1421 (defun widget-value-value-get (widget)
1422 "Return the :value property of WIDGET."
1423 (widget-get widget :value))
1425 ;;; The `default' Widget.
1427 (define-widget 'default nil
1428 "Basic widget other widgets are derived from."
1429 :value-to-internal (lambda (_widget value) value)
1430 :value-to-external (lambda (_widget value) value)
1431 :button-prefix 'widget-button-prefix
1432 :button-suffix 'widget-button-suffix
1433 :completions-function #'widget-default-completions
1434 :create 'widget-default-create
1437 :format-handler 'widget-default-format-handler
1438 :button-face-get 'widget-default-button-face-get
1439 :mouse-face-get 'widget-default-mouse-face-get
1440 :sample-face-get 'widget-default-sample-face-get
1441 :delete 'widget-default-delete
1443 :value-set 'widget-default-value-set
1444 :value-inline 'widget-default-value-inline
1445 :value-delete 'ignore
1446 :default-get 'widget-default-default-get
1447 :menu-tag-get 'widget-default-menu-tag-get
1449 :active 'widget-default-active
1450 :activate 'widget-specify-active
1451 :deactivate 'widget-default-deactivate
1452 :mouse-down-action #'ignore
1453 :action 'widget-default-action
1454 :notify 'widget-default-notify
1455 :prompt-value 'widget-default-prompt-value)
1457 (defvar widget--completing-widget)
1459 (defun widget-default-completions (widget)
1460 "Return completion data, like `completion-at-point-functions' would."
1461 (let ((completions (widget-get widget :completions)))
1463 (list (widget-field-start widget)
1464 (max (point) (widget-field-text-end widget))
1466 (if (widget-get widget :complete)
1467 (lambda () (widget-apply widget :complete))
1468 (if (widget-get widget :complete-function)
1470 (let ((widget--completing-widget widget))
1472 (widget-get widget :complete-function)))))))))
1474 (defun widget-default-create (widget)
1475 "Create WIDGET at point in the current buffer."
1476 (widget-specify-insert
1477 (let ((from (point))
1478 button-begin button-end
1479 sample-begin sample-end
1482 (insert (widget-get widget :format))
1484 ;; Parse escapes in format.
1485 (while (re-search-forward "%\\(.\\)" nil t)
1486 (let ((escape (char-after (match-beginning 1))))
1488 (cond ((eq escape ?%)
1491 (setq button-begin (point))
1492 (insert (widget-get-indirect widget :button-prefix)))
1494 (insert (widget-get-indirect widget :button-suffix))
1495 (setq button-end (point)))
1497 (setq sample-begin (point)))
1499 (setq sample-end (point)))
1501 (when (widget-get widget :indent)
1503 (insert-char ?\s (widget-get widget :indent))))
1505 (let ((image (widget-get widget :tag-glyph))
1506 (tag (widget-get widget :tag)))
1508 (widget-image-insert widget (or tag "image") image))
1512 (princ (widget-get widget :value)
1513 (current-buffer))))))
1515 (let ((doc (widget-get widget :doc)))
1517 (setq doc-begin (point))
1519 (while (eq (preceding-char) ?\n)
1522 (setq doc-end (point)))))
1524 (widget-add-documentation-string-button widget))
1526 (if (and button-begin (not button-end))
1527 (widget-apply widget :value-create)
1528 (setq value-pos (point))))
1530 (widget-apply widget :format-handler escape)))))
1531 ;; Specify button, sample, and doc, and insert value.
1532 (and button-begin button-end
1533 (widget-specify-button widget button-begin button-end))
1534 (and sample-begin sample-end
1535 (widget-specify-sample widget sample-begin sample-end))
1536 (and doc-begin doc-end
1537 (widget-specify-doc widget doc-begin doc-end))
1539 (goto-char value-pos)
1540 (widget-apply widget :value-create)))
1541 (let ((from (point-min-marker))
1542 (to (point-max-marker)))
1543 (set-marker-insertion-type from t)
1544 (set-marker-insertion-type to nil)
1545 (widget-put widget :from from)
1546 (widget-put widget :to to)))
1547 (widget-clear-undo))
1549 (defun widget-default-format-handler (_widget escape)
1550 (error "Unknown escape `%c'" escape))
1552 (defun widget-default-button-face-get (widget)
1553 ;; Use :button-face or widget-button-face
1554 (or (widget-get widget :button-face)
1555 (let ((parent (widget-get widget :parent)))
1557 (widget-apply parent :button-face-get)
1558 widget-button-face))))
1560 (defun widget-default-mouse-face-get (widget)
1561 ;; Use :mouse-face or widget-mouse-face
1562 (or (widget-get widget :mouse-face)
1563 (let ((parent (widget-get widget :parent)))
1565 (widget-apply parent :mouse-face-get)
1566 widget-mouse-face))))
1568 (defun widget-default-sample-face-get (widget)
1569 ;; Use :sample-face.
1570 (widget-get widget :sample-face))
1572 (defun widget-default-delete (widget)
1573 "Remove widget from the buffer."
1574 (let ((from (widget-get widget :from))
1575 (to (widget-get widget :to))
1576 (inactive-overlay (widget-get widget :inactive))
1577 (button-overlay (widget-get widget :button-overlay))
1578 (sample-overlay (widget-get widget :sample-overlay))
1579 (doc-overlay (widget-get widget :doc-overlay))
1580 (inhibit-modification-hooks t)
1581 (inhibit-read-only t))
1582 (widget-apply widget :value-delete)
1583 (widget-children-value-delete widget)
1584 (when inactive-overlay
1585 (delete-overlay inactive-overlay))
1586 (when button-overlay
1587 (delete-overlay button-overlay))
1588 (when sample-overlay
1589 (delete-overlay sample-overlay))
1591 (delete-overlay doc-overlay))
1593 ;; Kludge: this doesn't need to be true for empty formats.
1594 (delete-region from to))
1595 (set-marker from nil)
1596 (set-marker to nil))
1597 (widget-clear-undo))
1599 (defun widget-default-value-set (widget value)
1600 "Recreate widget with new value."
1601 (let* ((old-pos (point))
1602 (from (copy-marker (widget-get widget :from)))
1603 (to (copy-marker (widget-get widget :to)))
1604 (offset (if (and (<= from old-pos) (<= old-pos to))
1605 (if (>= old-pos (1- to))
1607 (- old-pos from)))))
1608 ;;??? Bug: this ought to insert the new value before deleting the old one,
1609 ;; so that markers on either side of the value automatically
1610 ;; stay on the same side. -- rms.
1612 (goto-char (widget-get widget :from))
1613 (widget-apply widget :delete)
1614 (widget-put widget :value value)
1615 (widget-apply widget :create))
1618 (goto-char (+ (widget-get widget :to) offset 1))
1619 (goto-char (min (+ from offset) (1- (widget-get widget :to))))))))
1621 (defun widget-default-value-inline (widget)
1622 "Wrap value in a list unless it is inline."
1623 (if (widget-get widget :inline)
1624 (widget-value widget)
1625 (list (widget-value widget))))
1627 (defun widget-default-default-get (widget)
1629 (widget-get widget :value))
1631 (defun widget-default-menu-tag-get (widget)
1632 "Use tag or value for menus."
1633 (or (widget-get widget :menu-tag)
1634 (widget-get widget :tag)
1635 (widget-princ-to-string (widget-get widget :value))))
1637 (defun widget-default-active (widget)
1638 "Return t if this widget is active (user modifiable)."
1639 (or (widget-get widget :always-active)
1640 (and (not (widget-get widget :inactive))
1641 (let ((parent (widget-get widget :parent)))
1643 (widget-apply parent :active))))))
1645 (defun widget-default-deactivate (widget)
1646 "Make WIDGET inactive for user modifications."
1647 (widget-specify-inactive widget
1648 (widget-get widget :from)
1649 (widget-get widget :to)))
1651 (defun widget-default-action (widget &optional event)
1652 "Notify the parent when a widget changes."
1653 (let ((parent (widget-get widget :parent)))
1655 (widget-apply parent :notify widget event))))
1657 (defun widget-default-notify (widget _child &optional event)
1658 "Pass notification to parent."
1659 (widget-default-action widget event))
1661 (defun widget-default-prompt-value (_widget prompt _value _unbound)
1662 "Read an arbitrary value."
1663 (eval-minibuffer prompt))
1665 (defun widget-docstring (widget)
1666 "Return the documentation string specified by WIDGET, or nil if none.
1667 If WIDGET has a `:doc' property, that specifies the documentation string.
1668 Otherwise, try the `:documentation-property' property. If this
1669 is a function, call it with the widget's value as an argument; if
1670 it is a symbol, use this symbol together with the widget's value
1671 as the argument to `documentation-property'."
1672 (let ((doc (or (widget-get widget :doc)
1673 (let ((doc-prop (widget-get widget :documentation-property))
1674 (value (widget-get widget :value)))
1675 (cond ((functionp doc-prop)
1676 (funcall doc-prop value))
1678 (documentation-property value doc-prop)))))))
1679 (when (and (stringp doc) (> (length doc) 0))
1680 ;; Remove any redundant `*' in the beginning.
1681 (when (eq (aref doc 0) ?*)
1682 (setq doc (substring doc 1)))
1683 ;; Remove trailing newlines.
1684 (when (string-match "\n+\\'" doc)
1685 (setq doc (substring doc 0 (match-beginning 0))))
1688 ;;; The `item' Widget.
1690 (define-widget 'item 'default
1691 "Constant items for inclusion in other widgets."
1692 :convert-widget 'widget-value-convert-widget
1693 :value-create 'widget-item-value-create
1694 :value-delete 'ignore
1695 :value-get 'widget-value-value-get
1696 :match 'widget-item-match
1697 :match-inline 'widget-item-match-inline
1698 :action 'widget-item-action
1701 (defun widget-item-value-create (widget)
1702 "Insert the printed representation of the value."
1703 (princ (widget-get widget :value) (current-buffer)))
1705 (defun widget-item-match (widget value)
1706 ;; Match if the value is the same.
1707 (equal (widget-get widget :value) value))
1709 (defun widget-item-match-inline (widget vals)
1710 ;; Match if the value is the same.
1711 (let ((value (widget-get widget :value)))
1713 (<= (length value) (length vals))
1714 (let ((head (widget-sublist vals 0 (length value))))
1715 (and (equal head value)
1716 (cons head (widget-sublist vals (length value))))))))
1718 (defun widget-sublist (list start &optional end)
1719 "Return the sublist of LIST from START to END.
1720 If END is omitted, it defaults to the length of LIST."
1721 (if (> start 0) (setq list (nthcdr start list)))
1723 (unless (<= end start)
1724 (setq list (copy-sequence list))
1725 (setcdr (nthcdr (- end start 1) list) nil)
1727 (copy-sequence list)))
1729 (defun widget-item-action (widget &optional event)
1730 ;; Just notify itself.
1731 (widget-apply widget :notify widget event))
1733 ;;; The `push-button' Widget.
1735 ;; (defcustom widget-push-button-gui t
1736 ;; "If non-nil, use GUI push buttons when available."
1740 ;; Cache already created GUI objects.
1741 ;; (defvar widget-push-button-cache nil)
1743 (defcustom widget-push-button-prefix "["
1744 "String used as prefix for buttons."
1746 :group 'widget-button)
1748 (defcustom widget-push-button-suffix "]"
1749 "String used as suffix for buttons."
1751 :group 'widget-button)
1753 (define-widget 'push-button 'item
1754 "A pushable button."
1757 :value-create 'widget-push-button-value-create
1760 (defun widget-push-button-value-create (widget)
1761 "Insert text representing the `on' and `off' states."
1762 (let* ((tag (or (widget-get widget :tag)
1763 (widget-get widget :value)))
1764 (tag-glyph (widget-get widget :tag-glyph))
1765 (text (concat widget-push-button-prefix
1766 tag widget-push-button-suffix)))
1768 (widget-image-insert widget text tag-glyph)
1771 ;; (defun widget-gui-action (widget)
1772 ;; "Apply :action for WIDGET."
1773 ;; (widget-apply-action widget (this-command-keys)))
1775 ;;; The `link' Widget.
1777 (defcustom widget-link-prefix "["
1778 "String used as prefix for links."
1780 :group 'widget-button)
1782 (defcustom widget-link-suffix "]"
1783 "String used as suffix for links."
1785 :group 'widget-button)
1787 (define-widget 'link 'item
1789 :button-prefix 'widget-link-prefix
1790 :button-suffix 'widget-link-suffix
1791 :follow-link 'mouse-face
1792 :help-echo "Follow the link."
1795 ;;; The `info-link' Widget.
1797 (define-widget 'info-link 'link
1798 "A link to an info file."
1799 :action 'widget-info-link-action)
1801 (defun widget-info-link-action (widget &optional _event)
1802 "Open the info node specified by WIDGET."
1803 (info (widget-value widget)))
1805 ;;; The `url-link' Widget.
1807 (define-widget 'url-link 'link
1808 "A link to an www page."
1809 :action 'widget-url-link-action)
1811 (defun widget-url-link-action (widget &optional _event)
1812 "Open the URL specified by WIDGET."
1813 (browse-url (widget-value widget)))
1815 ;;; The `function-link' Widget.
1817 (define-widget 'function-link 'link
1818 "A link to an Emacs function."
1819 :action 'widget-function-link-action)
1821 (defun widget-function-link-action (widget &optional _event)
1822 "Show the function specified by WIDGET."
1823 (describe-function (widget-value widget)))
1825 ;;; The `variable-link' Widget.
1827 (define-widget 'variable-link 'link
1828 "A link to an Emacs variable."
1829 :action 'widget-variable-link-action)
1831 (defun widget-variable-link-action (widget &optional _event)
1832 "Show the variable specified by WIDGET."
1833 (describe-variable (widget-value widget)))
1835 ;;; The `file-link' Widget.
1837 (define-widget 'file-link 'link
1839 :action 'widget-file-link-action)
1841 (defun widget-file-link-action (widget &optional _event)
1842 "Find the file specified by WIDGET."
1843 (find-file (widget-value widget)))
1845 ;;; The `emacs-library-link' Widget.
1847 (define-widget 'emacs-library-link 'link
1848 "A link to an Emacs Lisp library file."
1849 :action 'widget-emacs-library-link-action)
1851 (defun widget-emacs-library-link-action (widget &optional _event)
1852 "Find the Emacs library file specified by WIDGET."
1853 (find-file (locate-library (widget-value widget))))
1855 ;;; The `emacs-commentary-link' Widget.
1857 (define-widget 'emacs-commentary-link 'link
1858 "A link to Commentary in an Emacs Lisp library file."
1859 :action 'widget-emacs-commentary-link-action)
1861 (defun widget-emacs-commentary-link-action (widget &optional _event)
1862 "Find the Commentary section of the Emacs file specified by WIDGET."
1863 (finder-commentary (widget-value widget)))
1865 ;;; The `editable-field' Widget.
1867 (define-widget 'editable-field 'default
1868 "An editable text field.
1869 Note: In an `editable-field' widget, the `%v' escape must be preceded
1870 by some other text in the `:format' string (if specified)."
1871 :convert-widget 'widget-value-convert-widget
1872 :keymap widget-field-keymap
1874 :help-echo "M-TAB: complete field; RET: enter value"
1876 :prompt-internal 'widget-field-prompt-internal
1877 :prompt-history 'widget-field-history
1878 :prompt-value 'widget-field-prompt-value
1879 :action 'widget-field-action
1880 :validate 'widget-field-validate
1882 :error "Field's value doesn't match allowed forms"
1883 :value-create 'widget-field-value-create
1884 :value-set 'widget-field-value-set
1885 :value-delete 'widget-field-value-delete
1886 :value-get 'widget-field-value-get
1887 :match 'widget-field-match)
1889 (defvar widget-field-history nil
1890 "History of field minibuffer edits.")
1892 (defun widget-field-prompt-internal (_widget prompt initial history)
1893 "Read string for WIDGET prompting with PROMPT.
1894 INITIAL is the initial input and HISTORY is a symbol containing
1896 (read-string prompt initial history))
1898 (defun widget-field-prompt-value (widget prompt value unbound)
1899 "Prompt for a string."
1900 (widget-apply widget
1902 (widget-apply widget
1903 :prompt-internal prompt
1905 (cons (widget-apply widget
1906 :value-to-internal value)
1908 (widget-get widget :prompt-history))))
1910 (defvar widget-edit-functions nil)
1912 (defun widget-field-action (widget &optional _event)
1913 "Move to next field."
1915 (run-hook-with-args 'widget-edit-functions widget))
1917 (defun widget-field-validate (widget)
1918 "Valid if the content matches `:valid-regexp'."
1919 (unless (string-match (widget-get widget :valid-regexp)
1920 (widget-apply widget :value-get))
1923 (defun widget-field-value-set (widget value)
1924 "Set an editable text field WIDGET to VALUE"
1925 (let ((from (widget-field-start widget))
1926 (to (widget-field-text-end widget))
1927 (buffer (widget-field-buffer widget)))
1928 (when (and from to (buffer-live-p buffer))
1929 (with-current-buffer buffer
1931 (delete-char (- to from))
1934 (defun widget-field-value-create (widget)
1935 "Create an editable text field."
1936 (let ((size (widget-get widget :size))
1937 (value (widget-get widget :value))
1939 ;; This is changed to a real overlay in `widget-setup'. We
1940 ;; need the end points to behave differently until
1941 ;; `widget-setup' is called.
1942 (overlay (cons (make-marker) (make-marker))))
1943 (widget-put widget :field-overlay overlay)
1946 (< (length value) size)
1947 (insert-char ?\s (- size (length value))))
1948 (unless (memq widget widget-field-list)
1949 (setq widget-field-new (cons widget widget-field-new)))
1950 (move-marker (cdr overlay) (point))
1951 (set-marker-insertion-type (cdr overlay) nil)
1954 (move-marker (car overlay) from)
1955 (set-marker-insertion-type (car overlay) t)))
1957 (defun widget-field-value-delete (widget)
1958 "Remove the widget from the list of active editing fields."
1959 (setq widget-field-list (delq widget widget-field-list))
1960 (setq widget-field-new (delq widget widget-field-new))
1961 ;; These are nil if the :format string doesn't contain `%v'.
1962 (let ((overlay (widget-get widget :field-overlay)))
1963 (when (overlayp overlay)
1964 (delete-overlay overlay))))
1966 (defun widget-field-value-get (widget &optional no-truncate)
1967 "Return current text in editing field.
1968 Normally, trailing spaces within the editing field are truncated.
1969 But if NO-TRUNCATE is non-nil, include them."
1970 (let ((from (widget-field-start widget))
1972 (widget-field-end widget)
1973 (widget-field-text-end widget)))
1974 (buffer (widget-field-buffer widget))
1975 (secret (widget-get widget :secret))
1976 (old (current-buffer)))
1980 (let ((result (buffer-substring-no-properties from to)))
1983 (while (< (+ from index) to)
1985 (get-char-property (+ from index) 'secret))
1986 (setq index (1+ index)))))
1989 (widget-get widget :value))))
1991 (defun widget-field-match (_widget value)
1992 ;; Match any string.
1995 ;;; The `text' Widget.
1997 (define-widget 'text 'editable-field
1998 "A multiline text area."
1999 :keymap widget-text-keymap)
2001 ;;; The `menu-choice' Widget.
2003 (define-widget 'menu-choice 'default
2004 "A menu of options."
2005 :convert-widget 'widget-types-convert-widget
2006 :copy 'widget-types-copy
2007 :format "%[%t%]: %v"
2010 :void '(item :format "invalid (%t)\n")
2011 :value-create 'widget-choice-value-create
2012 :value-get 'widget-child-value-get
2013 :value-inline 'widget-child-value-inline
2014 :default-get 'widget-choice-default-get
2015 :mouse-down-action 'widget-choice-mouse-down-action
2016 :action 'widget-choice-action
2017 :error "Make a choice"
2018 :validate 'widget-choice-validate
2019 :match 'widget-choice-match
2020 :match-inline 'widget-choice-match-inline)
2022 (defun widget-choice-value-create (widget)
2023 "Insert the first choice that matches the value."
2024 (let ((value (widget-get widget :value))
2025 (args (widget-get widget :args))
2026 (explicit (widget-get widget :explicit-choice))
2030 ;; If the user specified the choice for this value,
2031 ;; respect that choice.
2032 (widget-put widget :children (list (widget-create-child-value
2033 widget explicit value)))
2034 (widget-put widget :choice explicit)
2035 (widget-put widget :explicit-choice nil))
2037 (setq current (car args)
2039 (when (widget-apply current :match value)
2040 (widget-put widget :children (list (widget-create-child-value
2041 widget current value)))
2042 (widget-put widget :choice current)
2046 (let ((void (widget-get widget :void)))
2047 (widget-put widget :children (list (widget-create-child-and-convert
2048 widget void :value value)))
2049 (widget-put widget :choice void))))))
2051 (defun widget-choice-default-get (widget)
2052 ;; Get default for the first choice.
2053 (widget-default-get (car (widget-get widget :args))))
2055 (defcustom widget-choice-toggle nil
2056 "If non-nil, a binary choice will just toggle between the values.
2057 Otherwise, the user will explicitly have to choose between the values
2058 when he invoked the menu."
2062 (defun widget-choice-mouse-down-action (widget &optional _event)
2063 ;; Return non-nil if we need a menu.
2064 (let ((args (widget-get widget :args))
2065 (old (widget-get widget :choice)))
2066 (cond ((not (display-popup-menus-p))
2067 ;; No place to pop up a menu.
2069 ((< (length args) 2)
2070 ;; Empty or singleton list, just return the value.
2072 ((> (length args) widget-menu-max-size)
2073 ;; Too long, prompt.
2075 ((> (length args) 2)
2076 ;; Reasonable sized list, use menu.
2078 ((and widget-choice-toggle (memq old args))
2082 ;; Ask which of the two.
2085 (defun widget-choice-action (widget &optional event)
2087 (let ((args (widget-get widget :args))
2088 (old (widget-get widget :choice))
2089 (tag (widget-apply widget :menu-tag-get))
2090 (completion-ignore-case (widget-get widget :case-fold))
2093 ;; Remember old value.
2094 (if (and old (not (widget-apply widget :validate)))
2095 (let* ((external (widget-value widget))
2096 (internal (widget-apply old :value-to-internal external)))
2097 (widget-put old :value internal)))
2100 (cond ((= (length args) 0)
2102 ((= (length args) 1)
2104 ((and widget-choice-toggle
2107 (if (eq old (nth 0 args))
2112 (setq current (car args)
2115 (cons (cons (widget-apply current :menu-tag-get)
2118 (setq this-explicit t)
2119 (widget-choose tag (reverse choices) event))))
2121 ;; If this was an explicit user choice, record the choice,
2122 ;; so that widget-choice-value-create will respect it.
2124 (widget-put widget :explicit-choice current))
2125 (widget-value-set widget (widget-default-get current))
2127 (widget-apply widget :notify widget event)))
2128 (run-hook-with-args 'widget-edit-functions widget))
2130 (defun widget-choice-validate (widget)
2131 ;; Valid if we have made a valid choice.
2132 (if (eq (widget-get widget :void) (widget-get widget :choice))
2134 (widget-apply (car (widget-get widget :children)) :validate)))
2136 (defun widget-choice-match (widget value)
2137 ;; Matches if one of the choices matches.
2138 (let ((args (widget-get widget :args))
2140 (while (and args (not found))
2141 (setq current (car args)
2143 found (widget-apply current :match value)))
2146 (defun widget-choice-match-inline (widget vals)
2147 ;; Matches if one of the choices matches.
2148 (let ((args (widget-get widget :args))
2150 (while (and args (null found))
2151 (setq current (car args)
2153 found (widget-match-inline current vals)))
2156 ;;; The `toggle' Widget.
2158 (define-widget 'toggle 'item
2159 "Toggle between two states."
2161 :value-create 'widget-toggle-value-create
2162 :action 'widget-toggle-action
2163 :match (lambda (_widget _value) t)
2167 (defun widget-toggle-value-create (widget)
2168 "Insert text representing the `on' and `off' states."
2169 (let* ((val (widget-value widget))
2170 (text (widget-get widget (if val :on :off)))
2171 (img (widget-image-find
2172 (widget-get widget (if val :on-glyph :off-glyph)))))
2173 (widget-image-insert widget (or text "")
2175 (append img '(:ascent center))))))
2177 (defun widget-toggle-action (widget &optional event)
2179 (widget-value-set widget (not (widget-value widget)))
2180 (widget-apply widget :notify widget event)
2181 (run-hook-with-args 'widget-edit-functions widget))
2183 ;;; The `checkbox' Widget.
2185 (define-widget 'checkbox 'toggle
2186 "A checkbox toggle."
2191 ;; We could probably do the same job as the images using single
2192 ;; space characters in a boxed face with a stretch specification to
2193 ;; make them square.
2196 :off-glyph "unchecked"
2197 :help-echo "Toggle this item."
2198 :action 'widget-checkbox-action)
2200 (defun widget-checkbox-action (widget &optional event)
2201 "Toggle checkbox, notify parent, and set active state of sibling."
2202 (widget-toggle-action widget event)
2203 (let ((sibling (widget-get-sibling widget)))
2205 (if (widget-value widget)
2206 (widget-apply sibling :activate)
2207 (widget-apply sibling :deactivate))
2208 (widget-clear-undo))))
2210 ;;; The `checklist' Widget.
2212 (define-widget 'checklist 'default
2213 "A multiple choice widget."
2214 :convert-widget 'widget-types-convert-widget
2215 :copy 'widget-types-copy
2218 :entry-format "%b %v"
2220 :value-create 'widget-checklist-value-create
2221 :value-get 'widget-checklist-value-get
2222 :validate 'widget-checklist-validate
2223 :match 'widget-checklist-match
2224 :match-inline 'widget-checklist-match-inline)
2226 (defun widget-checklist-value-create (widget)
2227 ;; Insert all values
2228 (let ((alist (widget-checklist-match-find widget))
2229 (args (widget-get widget :args)))
2231 (widget-checklist-add-item widget item (assq item alist)))
2232 (widget-put widget :children (nreverse (widget-get widget :children)))))
2234 (defun widget-checklist-add-item (widget type chosen)
2235 "Create checklist item in WIDGET of type TYPE.
2236 If the item is checked, CHOSEN is a cons whose cdr is the value."
2237 (and (eq (preceding-char) ?\n)
2238 (widget-get widget :indent)
2239 (insert-char ?\s (widget-get widget :indent)))
2240 (widget-specify-insert
2241 (let* ((children (widget-get widget :children))
2242 (buttons (widget-get widget :buttons))
2243 (button-args (or (widget-get type :sibling-args)
2244 (widget-get widget :button-args)))
2247 (insert (widget-get widget :entry-format))
2249 ;; Parse % escapes in format.
2250 (while (re-search-forward "%\\([bv%]\\)" nil t)
2251 (let ((escape (char-after (match-beginning 1))))
2253 (cond ((eq escape ?%)
2256 (setq button (apply 'widget-create-child-and-convert
2258 :value (not (null chosen))
2263 (let ((child (widget-create-child widget type)))
2264 (widget-apply child :deactivate)
2266 ((widget-get type :inline)
2267 (widget-create-child-value
2268 widget type (cdr chosen)))
2270 (widget-create-child-value
2271 widget type (car (cdr chosen)))))))
2273 (error "Unknown escape `%c'" escape)))))
2274 ;; Update properties.
2275 (and button child (widget-put child :button button))
2276 (and button (widget-put widget :buttons (cons button buttons)))
2277 (and child (widget-put widget :children (cons child children))))))
2279 (defun widget-checklist-match (widget vals)
2280 ;; All values must match a type in the checklist.
2282 (null (cdr (widget-checklist-match-inline widget vals)))))
2284 (defun widget-checklist-match-inline (widget vals)
2285 ;; Find the values which match a type in the checklist.
2286 (let ((greedy (widget-get widget :greedy))
2287 (args (copy-sequence (widget-get widget :args)))
2290 (let ((answer (widget-checklist-match-up args vals)))
2292 (let ((vals2 (widget-match-inline answer vals)))
2293 (setq found (append found (car vals2))
2295 args (delq answer args))))
2297 (setq rest (append rest (list (car vals)))
2300 (setq rest (append rest vals)
2304 (defun widget-checklist-match-find (widget &optional vals)
2305 "Find the vals which match a type in the checklist.
2306 Return an alist of (TYPE MATCH)."
2307 (or vals (setq vals (widget-get widget :value)))
2308 (let ((greedy (widget-get widget :greedy))
2309 (args (copy-sequence (widget-get widget :args)))
2312 (let ((answer (widget-checklist-match-up args vals)))
2314 (let ((match (widget-match-inline answer vals)))
2315 (setq found (cons (cons answer (car match)) found)
2317 args (delq answer args))))
2319 (setq vals (cdr vals)))
2324 (defun widget-checklist-match-up (args vals)
2325 "Return the first type from ARGS that matches VALS."
2326 (let (current found)
2327 (while (and args (null found))
2328 (setq current (car args)
2330 found (widget-match-inline current vals)))
2334 (defun widget-checklist-value-get (widget)
2335 ;; The values of all selected items.
2336 (let ((children (widget-get widget :children))
2339 (setq child (car children)
2340 children (cdr children))
2341 (if (widget-value (widget-get child :button))
2342 (setq result (append result (widget-apply child :value-inline)))))
2345 (defun widget-checklist-validate (widget)
2346 ;; Ticked children must be valid.
2347 (let ((children (widget-get widget :children))
2349 (while (and children (not found))
2350 (setq child (car children)
2351 children (cdr children)
2352 button (widget-get child :button)
2353 found (and (widget-value button)
2354 (widget-apply child :validate))))
2357 ;;; The `option' Widget
2359 (define-widget 'option 'checklist
2360 "An widget with an optional item."
2363 ;;; The `choice-item' Widget.
2365 (define-widget 'choice-item 'item
2366 "Button items that delegate action events to their parents."
2367 :action 'widget-parent-action
2368 :format "%[%t%] \n")
2370 ;;; The `radio-button' Widget.
2372 (define-widget 'radio-button 'toggle
2373 "A radio button for use in the `radio' widget."
2374 :notify 'widget-radio-button-notify
2381 :off-glyph "radio0")
2383 (defun widget-radio-button-notify (widget _child &optional event)
2385 (widget-apply (widget-get widget :parent) :action widget event))
2387 ;;; The `radio-button-choice' Widget.
2389 (define-widget 'radio-button-choice 'default
2390 "Select one of multiple options."
2391 :convert-widget 'widget-types-convert-widget
2392 :copy 'widget-types-copy
2395 :entry-format "%b %v"
2396 :value-create 'widget-radio-value-create
2397 :value-get 'widget-radio-value-get
2398 :value-inline 'widget-radio-value-inline
2399 :value-set 'widget-radio-value-set
2400 :error "You must push one of the buttons"
2401 :validate 'widget-radio-validate
2402 :match 'widget-choice-match
2403 :match-inline 'widget-choice-match-inline
2404 :action 'widget-radio-action)
2406 (defun widget-radio-value-create (widget)
2407 ;; Insert all values
2408 (let ((args (widget-get widget :args))
2411 (setq arg (car args)
2413 (widget-radio-add-item widget arg))))
2415 (defun widget-radio-add-item (widget type)
2416 "Add to radio widget WIDGET a new radio button item of type TYPE."
2417 ;; (setq type (widget-convert type))
2418 (and (eq (preceding-char) ?\n)
2419 (widget-get widget :indent)
2420 (insert-char ?\s (widget-get widget :indent)))
2421 (widget-specify-insert
2422 (let* ((value (widget-get widget :value))
2423 (children (widget-get widget :children))
2424 (buttons (widget-get widget :buttons))
2425 (button-args (or (widget-get type :sibling-args)
2426 (widget-get widget :button-args)))
2428 (chosen (and (null (widget-get widget :choice))
2429 (widget-apply type :match value)))
2431 (insert (widget-get widget :entry-format))
2433 ;; Parse % escapes in format.
2434 (while (re-search-forward "%\\([bv%]\\)" nil t)
2435 (let ((escape (char-after (match-beginning 1))))
2437 (cond ((eq escape ?%)
2440 (setq button (apply 'widget-create-child-and-convert
2441 widget 'radio-button
2442 :value (not (null chosen))
2445 (setq child (if chosen
2446 (widget-create-child-value
2448 (widget-create-child widget type)))
2450 (widget-apply child :deactivate)))
2452 (error "Unknown escape `%c'" escape)))))
2453 ;; Update properties.
2455 (widget-put widget :choice type))
2457 (widget-put child :button button)
2458 (widget-put widget :buttons (nconc buttons (list button))))
2460 (widget-put widget :children (nconc children (list child))))
2463 (defun widget-radio-value-get (widget)
2464 ;; Get value of the child widget.
2465 (let ((chosen (widget-radio-chosen widget)))
2466 (and chosen (widget-value chosen))))
2468 (defun widget-radio-chosen (widget)
2469 "Return the widget representing the chosen radio button."
2470 (let ((children (widget-get widget :children))
2473 (setq current (car children)
2474 children (cdr children))
2475 (when (widget-apply (widget-get current :button) :value-get)
2480 (defun widget-radio-value-inline (widget)
2481 ;; Get value of the child widget.
2482 (let ((children (widget-get widget :children))
2485 (setq current (car children)
2486 children (cdr children))
2487 (when (widget-apply (widget-get current :button) :value-get)
2488 (setq found (widget-apply current :value-inline)
2492 (defun widget-radio-value-set (widget value)
2493 ;; We can't just delete and recreate a radio widget, since children
2494 ;; can be added after the original creation and won't be recreated
2496 (let ((children (widget-get widget :children))
2499 (setq current (car children)
2500 children (cdr children))
2501 (let* ((button (widget-get current :button))
2502 (match (and (not found)
2503 (widget-apply current :match value))))
2504 (widget-value-set button match)
2507 (widget-value-set current value)
2508 (widget-apply current :activate))
2509 (widget-apply current :deactivate))
2510 (setq found (or found match))))))
2512 (defun widget-radio-validate (widget)
2513 ;; Valid if we have made a valid choice.
2514 (let ((children (widget-get widget :children))
2515 current found button)
2516 (while (and children (not found))
2517 (setq current (car children)
2518 children (cdr children)
2519 button (widget-get current :button)
2520 found (widget-apply button :value-get)))
2522 (widget-apply current :validate)
2525 (defun widget-radio-action (widget child event)
2526 ;; Check if a radio button was pressed.
2527 (let ((children (widget-get widget :children))
2528 (buttons (widget-get widget :buttons))
2530 (when (memq child buttons)
2532 (setq current (car children)
2533 children (cdr children))
2534 (let* ((button (widget-get current :button)))
2535 (cond ((eq child button)
2536 (widget-value-set button t)
2537 (widget-apply current :activate))
2538 ((widget-value button)
2539 (widget-value-set button nil)
2540 (widget-apply current :deactivate)))))))
2541 ;; Pass notification to parent.
2542 (widget-apply widget :notify child event))
2544 ;;; The `insert-button' Widget.
2546 (define-widget 'insert-button 'push-button
2547 "An insert button for the `editable-list' widget."
2549 :help-echo "Insert a new item into the list at this position."
2550 :action 'widget-insert-button-action)
2552 (defun widget-insert-button-action (widget &optional _event)
2553 ;; Ask the parent to insert a new item.
2554 (widget-apply (widget-get widget :parent)
2555 :insert-before (widget-get widget :widget)))
2557 ;;; The `delete-button' Widget.
2559 (define-widget 'delete-button 'push-button
2560 "A delete button for the `editable-list' widget."
2562 :help-echo "Delete this item from the list."
2563 :action 'widget-delete-button-action)
2565 (defun widget-delete-button-action (widget &optional _event)
2566 ;; Ask the parent to insert a new item.
2567 (widget-apply (widget-get widget :parent)
2568 :delete-at (widget-get widget :widget)))
2570 ;;; The `editable-list' Widget.
2572 ;; (defcustom widget-editable-list-gui nil
2573 ;; "If non-nil, use GUI push-buttons in editable list when available."
2577 (define-widget 'editable-list 'default
2578 "A variable list of widgets of the same type."
2579 :convert-widget 'widget-types-convert-widget
2580 :copy 'widget-types-copy
2583 :format-handler 'widget-editable-list-format-handler
2584 :entry-format "%i %d %v"
2585 :value-create 'widget-editable-list-value-create
2586 :value-get 'widget-editable-list-value-get
2587 :validate 'widget-children-validate
2588 :match 'widget-editable-list-match
2589 :match-inline 'widget-editable-list-match-inline
2590 :insert-before 'widget-editable-list-insert-before
2591 :delete-at 'widget-editable-list-delete-at)
2593 (defun widget-editable-list-format-handler (widget escape)
2594 ;; We recognize the insert button.
2595 ;; (let ((widget-push-button-gui widget-editable-list-gui))
2596 (cond ((eq escape ?i)
2597 (and (widget-get widget :indent)
2598 (insert-char ?\s (widget-get widget :indent)))
2599 (apply 'widget-create-child-and-convert
2600 widget 'insert-button
2601 (widget-get widget :append-button-args)))
2603 (widget-default-format-handler widget escape)))
2607 (defun widget-editable-list-value-create (widget)
2608 ;; Insert all values
2609 (let* ((value (widget-get widget :value))
2610 (type (nth 0 (widget-get widget :args)))
2612 (widget-put widget :value-pos (point-marker))
2613 (set-marker-insertion-type (widget-get widget :value-pos) t)
2615 (let ((answer (widget-match-inline type value)))
2617 (setq children (cons (widget-editable-list-entry-create
2619 (if (widget-get type :inline)
2626 (widget-put widget :children (nreverse children))))
2628 (defun widget-editable-list-value-get (widget)
2629 ;; Get value of the child widget.
2630 (apply 'append (mapcar (lambda (child) (widget-apply child :value-inline))
2631 (widget-get widget :children))))
2633 (defun widget-editable-list-match (widget value)
2634 ;; Value must be a list and all the members must match the type.
2636 (null (cdr (widget-editable-list-match-inline widget value)))))
2638 (defun widget-editable-list-match-inline (widget value)
2639 (let ((type (nth 0 (widget-get widget :args)))
2642 (while (and value ok)
2643 (let ((answer (widget-match-inline type value)))
2645 (setq found (append found (car answer))
2648 (cons found value)))
2650 (defun widget-editable-list-insert-before (widget before)
2651 ;; Insert a new child in the list of children.
2653 (let ((children (widget-get widget :children))
2654 (inhibit-read-only t)
2655 before-change-functions
2656 after-change-functions)
2658 (goto-char (widget-get before :entry-from)))
2660 (goto-char (widget-get widget :value-pos))))
2661 (let ((child (widget-editable-list-entry-create
2663 (when (< (widget-get child :entry-from) (widget-get widget :from))
2664 (set-marker (widget-get widget :from)
2665 (widget-get child :entry-from)))
2666 (if (eq (car children) before)
2667 (widget-put widget :children (cons child children))
2668 (while (not (eq (car (cdr children)) before))
2669 (setq children (cdr children)))
2670 (setcdr children (cons child (cdr children)))))))
2672 (widget-apply widget :notify widget))
2674 (defun widget-editable-list-delete-at (widget child)
2675 ;; Delete child from list of children.
2677 (let ((buttons (copy-sequence (widget-get widget :buttons)))
2679 (inhibit-read-only t)
2680 before-change-functions
2681 after-change-functions)
2683 (setq button (car buttons)
2684 buttons (cdr buttons))
2685 (when (eq (widget-get button :widget) child)
2687 :buttons (delq button (widget-get widget :buttons)))
2688 (widget-delete button))))
2689 (let ((entry-from (widget-get child :entry-from))
2690 (entry-to (widget-get child :entry-to))
2691 (inhibit-read-only t)
2692 before-change-functions
2693 after-change-functions)
2694 (widget-delete child)
2695 (delete-region entry-from entry-to)
2696 (set-marker entry-from nil)
2697 (set-marker entry-to nil))
2698 (widget-put widget :children (delq child (widget-get widget :children))))
2700 (widget-apply widget :notify widget))
2702 (defun widget-editable-list-entry-create (widget value conv)
2703 ;; Create a new entry to the list.
2704 (let ((type (nth 0 (widget-get widget :args)))
2705 ;; (widget-push-button-gui widget-editable-list-gui)
2706 child delete insert)
2707 (widget-specify-insert
2709 (and (widget-get widget :indent)
2710 (insert-char ?\s (widget-get widget :indent)))
2711 (insert (widget-get widget :entry-format)))
2712 ;; Parse % escapes in format.
2713 (while (re-search-forward "%\\(.\\)" nil t)
2714 (let ((escape (char-after (match-beginning 1))))
2716 (cond ((eq escape ?%)
2719 (setq insert (apply 'widget-create-child-and-convert
2720 widget 'insert-button
2721 (widget-get widget :insert-button-args))))
2723 (setq delete (apply 'widget-create-child-and-convert
2724 widget 'delete-button
2725 (widget-get widget :delete-button-args))))
2728 (setq child (widget-create-child-value
2730 (setq child (widget-create-child-value
2731 widget type (widget-default-get type)))))
2733 (error "Unknown escape `%c'" escape)))))
2734 (let ((buttons (widget-get widget :buttons)))
2735 (if insert (push insert buttons))
2736 (if delete (push delete buttons))
2737 (widget-put widget :buttons buttons))
2738 (let ((entry-from (point-min-marker))
2739 (entry-to (point-max-marker)))
2740 (set-marker-insertion-type entry-from t)
2741 (set-marker-insertion-type entry-to nil)
2742 (widget-put child :entry-from entry-from)
2743 (widget-put child :entry-to entry-to)))
2744 (if insert (widget-put insert :widget child))
2745 (if delete (widget-put delete :widget child))
2748 ;;; The `group' Widget.
2750 (define-widget 'group 'default
2751 "A widget which groups other widgets inside."
2752 :convert-widget 'widget-types-convert-widget
2753 :copy 'widget-types-copy
2755 :value-create 'widget-group-value-create
2756 :value-get 'widget-editable-list-value-get
2757 :default-get 'widget-group-default-get
2758 :validate 'widget-children-validate
2759 :match 'widget-group-match
2760 :match-inline 'widget-group-match-inline)
2762 (defun widget-group-value-create (widget)
2763 ;; Create each component.
2764 (let ((args (widget-get widget :args))
2765 (value (widget-get widget :value))
2766 arg answer children)
2768 (setq arg (car args)
2770 answer (widget-match-inline arg value)
2772 (and (eq (preceding-char) ?\n)
2773 (widget-get widget :indent)
2774 (insert-char ?\s (widget-get widget :indent)))
2775 (push (cond ((null answer)
2776 (widget-create-child widget arg))
2777 ((widget-get arg :inline)
2778 (widget-create-child-value widget arg (car answer)))
2780 (widget-create-child-value widget arg (car (car answer)))))
2782 (widget-put widget :children (nreverse children))))
2784 (defun widget-group-default-get (widget)
2785 ;; Get the default of the components.
2786 (mapcar 'widget-default-get (widget-get widget :args)))
2788 (defun widget-group-match (widget vals)
2789 ;; Match if the components match.
2791 (let ((match (widget-group-match-inline widget vals)))
2792 (and match (null (cdr match))))))
2794 (defun widget-group-match-inline (widget vals)
2795 ;; Match if the components match.
2796 (let ((args (widget-get widget :args))
2797 argument answer found)
2799 (setq argument (car args)
2801 (if (setq answer (widget-match-inline argument vals))
2802 (setq found (append found (car answer))
2807 (cons found vals))))
2809 ;;; The `visibility' Widget.
2811 (define-widget 'visibility 'item
2812 "An indicator and manipulator for hidden items.
2814 The following properties have special meanings for this widget:
2815 :on-glyph Image filename or spec to display when the item is visible.
2816 :on Text shown if the \"on\" image is nil or cannot be displayed.
2817 :off-glyph Image filename or spec to display when the item is hidden.
2818 :off Text shown if the \"off\" image is nil cannot be displayed."
2826 :value-create 'widget-visibility-value-create
2827 :action 'widget-toggle-action
2828 :match (lambda (_widget _value) t))
2830 (defalias 'widget-visibility-value-create 'widget-toggle-value-create)
2832 ;;; The `documentation-link' Widget.
2834 ;; This is a helper widget for `documentation-string'.
2836 (define-widget 'documentation-link 'link
2837 "Link type used in documentation strings."
2839 :help-echo "Describe this symbol"
2840 :action 'widget-documentation-link-action)
2842 (defun widget-documentation-link-action (widget &optional _event)
2843 "Display documentation for WIDGET's value. Ignore optional argument EVENT."
2844 (let* ((string (widget-get widget :value))
2845 (symbol (intern string)))
2846 (if (and (fboundp symbol) (boundp symbol))
2847 ;; If there are two doc strings, give the user a way to pick one.
2848 (apropos (concat "\\`" (regexp-quote string) "\\'"))
2849 (if (fboundp symbol)
2850 (describe-function symbol)
2851 (describe-variable symbol)))))
2853 (defcustom widget-documentation-links t
2854 "Add hyperlinks to documentation strings when non-nil."
2856 :group 'widget-documentation)
2858 (defcustom widget-documentation-link-regexp "`\\([^\n`' ]+\\)'"
2859 "Regexp for matching potential links in documentation strings.
2860 The first group should be the link itself."
2862 :group 'widget-documentation)
2864 (defcustom widget-documentation-link-p 'intern-soft
2865 "Predicate used to test if a string is useful as a link.
2866 The value should be a function. The function will be called with one
2867 argument, a string, and should return non-nil if there should be a
2868 link for that string."
2870 :options '(widget-documentation-link-p)
2871 :group 'widget-documentation)
2873 (defcustom widget-documentation-link-type 'documentation-link
2874 "Widget type used for links in documentation strings."
2876 :group 'widget-documentation)
2878 (defun widget-documentation-link-add (widget from to)
2879 (widget-specify-doc widget from to)
2880 (when widget-documentation-links
2881 (let ((regexp widget-documentation-link-regexp)
2882 (buttons (widget-get widget :buttons))
2883 (widget-mouse-face (default-value 'widget-mouse-face))
2884 (widget-button-face widget-documentation-face)
2885 (widget-button-pressed-face widget-documentation-face))
2888 (while (re-search-forward regexp to t)
2889 (let ((name (match-string 1))
2890 (begin (match-beginning 1))
2891 (end (match-end 1)))
2892 (when (funcall widget-documentation-link-p name)
2893 (push (widget-convert-button widget-documentation-link-type
2894 begin end :value name)
2896 (widget-put widget :buttons buttons))))
2898 ;;; The `documentation-string' Widget.
2900 (define-widget 'documentation-string 'item
2901 "A documentation string."
2903 :action 'widget-documentation-string-action
2904 :value-create 'widget-documentation-string-value-create
2905 :visibility-widget 'visibility)
2907 (defun widget-documentation-string-value-create (widget)
2908 ;; Insert documentation string.
2909 (let ((doc (widget-value widget))
2910 (indent (widget-get widget :indent))
2911 (shown (widget-get (widget-get widget :parent) :documentation-shown))
2913 (if (string-match "\n" doc)
2914 (let ((before (substring doc 0 (match-beginning 0)))
2915 (after (substring doc (match-end 0)))
2917 (widget-documentation-string-indent-to indent)
2919 (widget-documentation-link-add widget start (point))
2921 (widget-create-child-and-convert
2922 widget (widget-get widget :visibility-widget)
2923 :help-echo "Show or hide rest of the documentation."
2927 :action 'widget-parent-action
2931 (setq start (point))
2932 (when (and indent (not (zerop indent)))
2933 (insert-char ?\s indent))
2936 (widget-documentation-link-add widget start end)
2937 ;; Indent the subsequent lines.
2938 (when (and indent (> indent 0))
2941 (narrow-to-region start end)
2942 (goto-char (point-min))
2943 (while (search-forward "\n" nil t)
2944 (widget-documentation-string-indent-to indent))))))
2945 (widget-put widget :buttons (list button)))
2946 (widget-documentation-string-indent-to indent)
2948 (widget-documentation-link-add widget start (point))))
2951 (defun widget-documentation-string-indent-to (col)
2952 (when (and (numberp col)
2954 (let ((opoint (point)))
2956 (put-text-property opoint (point)
2957 'display `(space :align-to ,col)))))
2959 (defun widget-documentation-string-action (widget &rest _ignore)
2960 ;; Toggle documentation.
2961 (let ((parent (widget-get widget :parent)))
2962 (widget-put parent :documentation-shown
2963 (not (widget-get parent :documentation-shown))))
2965 (widget-value-set widget (widget-value widget)))
2967 (defun widget-add-documentation-string-button (widget &rest args)
2968 "Insert a new `documentation-string' widget based on WIDGET.
2969 The new widget becomes a child of WIDGET, and is also added to
2970 its `:buttons' list. The documentation string is found from
2971 WIDGET using the function `widget-docstring'.
2972 Optional ARGS specifies additional keyword arguments for the
2973 `documentation-string' widget."
2974 (let ((doc (widget-docstring widget))
2975 (indent (widget-get widget :indent))
2976 (doc-indent (widget-get widget :documentation-indent)))
2978 (and (eq (preceding-char) ?\n)
2980 (insert-char ?\s indent))
2981 (unless (or (numberp doc-indent) (null doc-indent))
2982 (setq doc-indent 0))
2983 (widget-put widget :buttons
2984 (cons (apply 'widget-create-child-and-convert
2985 widget 'documentation-string
2987 (nconc args (list doc)))
2988 (widget-get widget :buttons))))))
2990 ;;; The Sexp Widgets.
2992 (define-widget 'const 'item
2993 "An immutable sexp."
2994 :prompt-value 'widget-const-prompt-value
2997 (defun widget-const-prompt-value (widget _prompt _value _unbound)
2998 ;; Return the value of the const.
2999 (widget-value widget))
3001 (define-widget 'function-item 'const
3002 "An immutable function name."
3004 :documentation-property (lambda (symbol)
3006 (documentation symbol t)
3009 (define-widget 'variable-item 'const
3010 "An immutable variable name."
3012 :documentation-property 'variable-documentation)
3014 (define-widget 'other 'sexp
3015 "Matches any value, but doesn't let the user edit the value.
3016 This is useful as last item in a `choice' widget.
3017 You should use this widget type with a default value,
3018 as in (other DEFAULT) or (other :tag \"NAME\" DEFAULT).
3019 If the user selects this alternative, that specifies DEFAULT
3025 (defvar widget-string-prompt-value-history nil
3026 "History of input to `widget-string-prompt-value'.")
3028 (define-widget 'string 'editable-field
3031 :format "%{%t%}: %v"
3032 :complete-function 'ispell-complete-word
3033 :prompt-history 'widget-string-prompt-value-history)
3035 (define-widget 'regexp 'string
3036 "A regular expression."
3037 :match 'widget-regexp-match
3038 :validate 'widget-regexp-validate
3039 ;; Doesn't work well with terminating newline.
3040 ;; :value-face 'widget-single-line-field
3043 (defun widget-regexp-match (_widget value)
3044 ;; Match valid regexps.
3045 (and (stringp value)
3048 (string-match value ""))
3051 (defun widget-regexp-validate (widget)
3052 "Check that the value of WIDGET is a valid regexp."
3053 (condition-case data
3055 (string-match (widget-value widget) ""))
3056 (error (widget-put widget :error (error-message-string data))
3059 (define-widget 'file 'string
3061 It reads a file name from an editable text field."
3062 :completions #'completion-file-name-table
3063 :prompt-value 'widget-file-prompt-value
3064 :format "%{%t%}: %v"
3065 ;; Doesn't work well with terminating newline.
3066 ;; :value-face 'widget-single-line-field
3069 (defun widget-file-prompt-value (widget prompt value unbound)
3070 ;; Read file from minibuffer.
3071 (abbreviate-file-name
3073 (read-file-name prompt)
3074 (let ((prompt2 (format "%s (default %s): " prompt value))
3075 (dir (file-name-directory value))
3076 (file (file-name-nondirectory value))
3077 (must-match (widget-get widget :must-match)))
3078 (read-file-name prompt2 dir nil must-match file)))))
3080 ;;;(defun widget-file-action (widget &optional event)
3081 ;;; ;; Read a file name from the minibuffer.
3082 ;;; (let* ((value (widget-value widget))
3083 ;;; (dir (file-name-directory value))
3084 ;;; (file (file-name-nondirectory value))
3085 ;;; (menu-tag (widget-apply widget :menu-tag-get))
3086 ;;; (must-match (widget-get widget :must-match))
3087 ;;; (answer (read-file-name (concat menu-tag " (default " value "): ")
3088 ;;; dir nil must-match file)))
3089 ;;; (widget-value-set widget (abbreviate-file-name answer))
3091 ;;; (widget-apply widget :notify widget event)))
3093 ;; Fixme: use file-name-as-directory.
3094 (define-widget 'directory 'file
3095 "A directory widget.
3096 It reads a directory name from an editable text field."
3099 (defvar widget-symbol-prompt-value-history nil
3100 "History of input to `widget-symbol-prompt-value'.")
3102 (define-widget 'symbol 'editable-field
3106 :format "%{%t%}: %v"
3107 :match (lambda (_widget value) (symbolp value))
3108 :completions obarray
3109 :prompt-internal 'widget-symbol-prompt-internal
3110 :prompt-match 'symbolp
3111 :prompt-history 'widget-symbol-prompt-value-history
3112 :value-to-internal (lambda (_widget value)
3116 :value-to-external (lambda (_widget value)
3121 (defun widget-symbol-prompt-internal (widget prompt initial history)
3122 ;; Read file from minibuffer.
3123 (let ((answer (completing-read prompt obarray
3124 (widget-get widget :prompt-match)
3125 nil initial history)))
3126 (if (and (stringp answer)
3127 (not (zerop (length answer))))
3129 (error "No value"))))
3131 (defvar widget-function-prompt-value-history nil
3132 "History of input to `widget-function-prompt-value'.")
3134 (define-widget 'function 'restricted-sexp
3136 :completions (apply-partially #'completion-table-with-predicate
3137 obarray #'fboundp 'strict)
3138 :prompt-value 'widget-field-prompt-value
3139 :prompt-internal 'widget-symbol-prompt-internal
3140 :prompt-match 'fboundp
3141 :prompt-history 'widget-function-prompt-value-history
3142 :action 'widget-field-action
3143 :match-alternatives '(functionp)
3144 :validate (lambda (widget)
3145 (unless (functionp (widget-value widget))
3146 (widget-put widget :error (format "Invalid function: %S"
3147 (widget-value widget)))
3152 (defvar widget-variable-prompt-value-history nil
3153 "History of input to `widget-variable-prompt-value'.")
3155 (define-widget 'variable 'symbol
3157 :prompt-match 'boundp
3158 :prompt-history 'widget-variable-prompt-value-history
3159 :completions (apply-partially #'completion-table-with-predicate
3160 obarray #'boundp 'strict)
3163 (define-widget 'coding-system 'symbol
3164 "A MULE coding-system."
3165 :format "%{%t%}: %v"
3166 :tag "Coding system"
3168 :prompt-history 'coding-system-value-history
3169 :prompt-value 'widget-coding-system-prompt-value
3170 :action 'widget-coding-system-action
3171 :completions (apply-partially #'completion-table-with-predicate
3172 obarray #'coding-system-p 'strict)
3173 :validate (lambda (widget)
3174 (unless (coding-system-p (widget-value widget))
3175 (widget-put widget :error (format "Invalid coding system: %S"
3176 (widget-value widget)))
3179 :prompt-match 'coding-system-p)
3181 (defun widget-coding-system-prompt-value (widget prompt value _unbound)
3182 "Read coding-system from minibuffer."
3183 (if (widget-get widget :base-only)
3185 (completing-read (format "%s (default %s): " prompt value)
3186 (mapcar #'list (coding-system-list t)) nil nil nil
3187 coding-system-history))
3188 (read-coding-system (format "%s (default %s): " prompt value) value)))
3190 (defun widget-coding-system-action (widget &optional event)
3192 (widget-coding-system-prompt-value
3194 (widget-apply widget :menu-tag-get)
3195 (widget-value widget)
3197 (widget-value-set widget answer)
3198 (widget-apply widget :notify widget event)
3201 ;;; I'm not sure about what this is good for? KFS.
3202 (defvar widget-key-sequence-prompt-value-history nil
3203 "History of input to `widget-key-sequence-prompt-value'.")
3205 (defvar widget-key-sequence-default-value [ignore]
3206 "Default value for an empty key sequence.")
3208 (defvar widget-key-sequence-map
3209 (let ((map (make-sparse-keymap)))
3210 (set-keymap-parent map widget-field-keymap)
3211 (define-key map [(control ?q)] 'widget-key-sequence-read-event)
3214 (define-widget 'key-sequence 'restricted-sexp
3216 :prompt-value 'widget-field-prompt-value
3217 :prompt-internal 'widget-symbol-prompt-internal
3218 ; :prompt-match 'fboundp ;; What was this good for? KFS
3219 :prompt-history 'widget-key-sequence-prompt-value-history
3220 :action 'widget-field-action
3221 :match-alternatives '(stringp vectorp)
3222 :format "%{%t%}: %v"
3223 :validate 'widget-key-sequence-validate
3224 :value-to-internal 'widget-key-sequence-value-to-internal
3225 :value-to-external 'widget-key-sequence-value-to-external
3226 :value widget-key-sequence-default-value
3227 :keymap widget-key-sequence-map
3228 :help-echo "C-q: insert KEY, EVENT, or CODE; RET: enter value"
3229 :tag "Key sequence")
3231 (defun widget-key-sequence-read-event (ev)
3233 (let ((inhibit-quit t) quit-flag)
3234 (read-event "Insert KEY, EVENT, or CODE: "))))
3235 (let ((ev2 (and (memq 'down (event-modifiers ev))
3237 (tr (and (keymapp function-key-map)
3238 (lookup-key function-key-map (vector ev)))))
3239 (when (and (integerp ev)
3240 (or (and (<= ?0 ev) (< ev (+ ?0 (min 10 read-quoted-char-radix))))
3241 (and (<= ?a (downcase ev))
3242 (< (downcase ev) (+ ?a -10 (min 36 read-quoted-char-radix))))))
3243 (setq unread-command-events (cons ev unread-command-events)
3244 ev (read-quoted-char (format "Enter code (radix %d)" read-quoted-char-radix))
3246 (if (and (integerp ev) (not (characterp ev)))
3247 (insert (char-to-string ev)))) ;; throw invalid char error
3248 (setq ev (key-description (list ev)))
3250 (setq tr (key-description (list (aref tr 0))))
3251 (if (y-or-n-p (format "Key %s is translated to %s -- use %s? " ev tr tr))
3252 (setq ev tr ev2 nil)))
3253 (insert (if (= (char-before) ?\s) "" " ") ev " ")
3255 (insert (key-description (list ev2)) " "))))
3257 (defun widget-key-sequence-validate (widget)
3258 (unless (or (stringp (widget-value widget))
3259 (vectorp (widget-value widget)))
3260 (widget-put widget :error (format "Invalid key sequence: %S"
3261 (widget-value widget)))
3264 (defun widget-key-sequence-value-to-internal (widget value)
3265 (if (widget-apply widget :match value)
3266 (if (equal value widget-key-sequence-default-value)
3268 (key-description value))
3271 (defun widget-key-sequence-value-to-external (_widget value)
3273 (if (string-match "\\`[[:space:]]*\\'" value)
3274 widget-key-sequence-default-value
3275 (read-kbd-macro value))
3279 (define-widget 'sexp 'editable-field
3280 "An arbitrary Lisp expression."
3281 :tag "Lisp expression"
3282 :format "%{%t%}: %v"
3284 :validate 'widget-sexp-validate
3285 :match (lambda (_widget _value) t)
3286 :value-to-internal 'widget-sexp-value-to-internal
3287 :value-to-external (lambda (_widget value) (read value))
3288 :prompt-history 'widget-sexp-prompt-value-history
3289 :prompt-value 'widget-sexp-prompt-value)
3291 (defun widget-sexp-value-to-internal (_widget value)
3292 ;; Use pp for printer representation.
3293 (let ((pp (if (symbolp value)
3294 (prin1-to-string value)
3295 (pp-to-string value))))
3296 (while (string-match "\n\\'" pp)
3297 (setq pp (substring pp 0 -1)))
3298 (if (or (string-match "\n\\'" pp)
3303 (defun widget-sexp-validate (widget)
3304 ;; Valid if we can read the string and there is no junk left after it.
3306 (insert (widget-apply widget :value-get))
3307 (goto-char (point-min))
3309 (condition-case data ;Note: We get a spurious byte-compile warning here.
3311 ;; Avoid a confusing end-of-file error.
3312 (skip-syntax-forward "\\s-")
3314 (setq err "Empty sexp -- use `nil'?")
3315 (unless (widget-apply widget :match (read (current-buffer)))
3316 (setq err (widget-get widget :type-error))))
3317 ;; Allow whitespace after expression.
3318 (skip-syntax-forward "\\s-")
3319 (if (and (not (eobp))
3321 (setq err (format "Junk at end of expression: %s"
3322 (buffer-substring (point)
3324 (end-of-file ; Avoid confusing error message.
3325 (setq err "Unbalanced sexp"))
3326 (error (setq err (error-message-string data))))
3329 (widget-put widget :error err)
3332 (defvar widget-sexp-prompt-value-history nil
3333 "History of input to `widget-sexp-prompt-value'.")
3335 (defun widget-sexp-prompt-value (widget prompt value unbound)
3336 ;; Read an arbitrary sexp.
3337 (let ((found (read-string prompt
3338 (if unbound nil (cons (prin1-to-string value) 0))
3339 (widget-get widget :prompt-history))))
3340 (let ((answer (read-from-string found)))
3341 (unless (= (cdr answer) (length found))
3342 (error "Junk at end of expression: %s"
3343 (substring found (cdr answer))))
3346 (define-widget 'restricted-sexp 'sexp
3347 "A Lisp expression restricted to values that match.
3348 To use this type, you must define :match or :match-alternatives."
3349 :type-error "The specified value is not valid"
3350 :match 'widget-restricted-sexp-match
3351 :value-to-internal (lambda (widget value)
3352 (if (widget-apply widget :match value)
3353 (prin1-to-string value)
3356 (defun widget-restricted-sexp-match (widget value)
3357 (let ((alternatives (widget-get widget :match-alternatives))
3359 (while (and alternatives (not matched))
3360 (if (cond ((functionp (car alternatives))
3361 (funcall (car alternatives) value))
3362 ((and (consp (car alternatives))
3363 (eq (car (car alternatives)) 'quote))
3364 (eq value (nth 1 (car alternatives)))))
3366 (setq alternatives (cdr alternatives)))
3369 (define-widget 'integer 'restricted-sexp
3373 :type-error "This field should contain an integer"
3374 :match-alternatives '(integerp))
3376 (define-widget 'number 'restricted-sexp
3377 "A number (floating point or integer)."
3380 :type-error "This field should contain a number (floating point or integer)"
3381 :match-alternatives '(numberp))
3383 (define-widget 'float 'restricted-sexp
3384 "A floating point number."
3385 :tag "Floating point number"
3387 :type-error "This field should contain a floating point number"
3388 :match-alternatives '(floatp))
3390 (define-widget 'character 'editable-field
3395 :format "%{%t%}: %v\n"
3396 :valid-regexp "\\`.\\'"
3397 :error "This field should contain a single character"
3398 :value-get (lambda (w) (widget-field-value-get w t))
3399 :value-to-internal (lambda (_widget value)
3402 (char-to-string value)))
3403 :value-to-external (lambda (_widget value)
3407 :match (lambda (_widget value)
3408 (characterp value)))
3410 (define-widget 'list 'group
3413 :format "%{%t%}:\n%v")
3415 (define-widget 'vector 'group
3418 :format "%{%t%}:\n%v"
3419 :match 'widget-vector-match
3420 :value-to-internal (lambda (_widget value) (append value nil))
3421 :value-to-external (lambda (_widget value) (apply 'vector value)))
3423 (defun widget-vector-match (widget value)
3424 (and (vectorp value)
3425 (widget-group-match widget
3426 (widget-apply widget :value-to-internal value))))
3428 (define-widget 'cons 'group
3431 :format "%{%t%}:\n%v"
3432 :match 'widget-cons-match
3433 :value-to-internal (lambda (_widget value)
3434 (list (car value) (cdr value)))
3435 :value-to-external (lambda (_widget value)
3436 (apply 'cons value)))
3438 (defun widget-cons-match (widget value)
3440 (widget-group-match widget
3441 (widget-apply widget :value-to-internal value))))
3443 ;;; The `lazy' Widget.
3445 ;; Recursive datatypes.
3447 (define-widget 'lazy 'default
3448 "Base widget for recursive data structures.
3450 The `lazy' widget will, when instantiated, contain a single inferior
3451 widget, of the widget type specified by the :type parameter. The
3452 value of the `lazy' widget is the same as the value of the inferior
3453 widget. When deriving a new widget from the 'lazy' widget, the :type
3454 parameter is allowed to refer to the widget currently being defined,
3455 thus allowing recursive data structures to be described.
3457 The :type parameter takes the same arguments as the defcustom
3458 parameter with the same name.
3460 Most composite widgets, i.e. widgets containing other widgets, does
3461 not allow recursion. That is, when you define a new widget type, none
3462 of the inferior widgets may be of the same type you are currently
3465 In Lisp, however, it is custom to define data structures in terms of
3466 themselves. A list, for example, is defined as either nil, or a cons
3467 cell whose cdr itself is a list. The obvious way to translate this
3468 into a widget type would be
3470 (define-widget 'my-list 'choice
3471 \"A list of sexps.\"
3473 :args '((const nil) (cons :value (nil) sexp my-list)))
3475 Here we attempt to define my-list as a choice of either the constant
3476 nil, or a cons-cell containing a sexp and my-lisp. This will not work
3477 because the `choice' widget does not allow recursion.
3479 Using the `lazy' widget you can overcome this problem, as in this
3482 (define-widget 'sexp-list 'lazy
3483 \"A list of sexps.\"
3485 :type '(choice (const nil) (cons :value (nil) sexp sexp-list)))"
3486 :format "%{%t%}: %v"
3487 ;; We don't convert :type because we want to allow recursive
3488 ;; data structures. This is slow, so we should not create speed
3489 ;; critical widgets by deriving from this.
3490 :convert-widget 'widget-value-convert-widget
3491 :value-create 'widget-type-value-create
3492 :value-get 'widget-child-value-get
3493 :value-inline 'widget-child-value-inline
3494 :default-get 'widget-type-default-get
3495 :match 'widget-type-match
3496 :validate 'widget-child-validate)
3499 ;;; The `plist' Widget.
3503 (define-widget 'plist 'list
3505 :key-type '(symbol :tag "Key")
3506 :value-type '(sexp :tag "Value")
3507 :convert-widget 'widget-plist-convert-widget
3510 (defvar widget-plist-value-type) ;Dynamic variable
3512 (defun widget-plist-convert-widget (widget)
3513 ;; Handle `:options'.
3514 (let* ((options (widget-get widget :options))
3515 (widget-plist-value-type (widget-get widget :value-type))
3516 (other `(editable-list :inline t
3518 ,(widget-get widget :key-type)
3519 ,widget-plist-value-type)))
3521 (list `(checklist :inline t
3523 ,@(mapcar 'widget-plist-convert-option
3527 (widget-put widget :args args)
3530 (defun widget-plist-convert-option (option)
3531 ;; Convert a single plist option.
3532 (let (key-type value-type)
3534 (let ((key (nth 0 option)))
3535 (setq value-type (nth 1 option))
3538 (setq key-type `(const ,key))))
3539 (setq key-type `(const ,option)
3540 value-type widget-plist-value-type))
3541 `(group :format "Key: %v" :inline t ,key-type ,value-type)))
3544 ;;; The `alist' Widget.
3546 ;; Association lists.
3548 (define-widget 'alist 'list
3549 "An association list."
3550 :key-type '(sexp :tag "Key")
3551 :value-type '(sexp :tag "Value")
3552 :convert-widget 'widget-alist-convert-widget
3555 (defvar widget-alist-value-type) ;Dynamic variable
3557 (defun widget-alist-convert-widget (widget)
3558 ;; Handle `:options'.
3559 (let* ((options (widget-get widget :options))
3560 (widget-alist-value-type (widget-get widget :value-type))
3561 (other `(editable-list :inline t
3563 ,(widget-get widget :key-type)
3564 ,widget-alist-value-type)))
3566 (list `(checklist :inline t
3568 ,@(mapcar 'widget-alist-convert-option
3572 (widget-put widget :args args)
3575 (defun widget-alist-convert-option (option)
3576 ;; Convert a single alist option.
3577 (let (key-type value-type)
3579 (let ((key (nth 0 option)))
3580 (setq value-type (nth 1 option))
3583 (setq key-type `(const ,key))))
3584 (setq key-type `(const ,option)
3585 value-type widget-alist-value-type))
3586 `(cons :format "Key: %v" ,key-type ,value-type)))
3588 (define-widget 'choice 'menu-choice
3589 "A union of several sexp types."
3591 :format "%{%t%}: %[Value Menu%] %v"
3592 :button-prefix 'widget-push-button-prefix
3593 :button-suffix 'widget-push-button-suffix
3594 :prompt-value 'widget-choice-prompt-value)
3596 (defun widget-choice-prompt-value (widget prompt value _unbound)
3598 (let ((args (widget-get widget :args))
3599 (completion-ignore-case (widget-get widget :case-fold))
3600 current choices old)
3601 ;; Find the first arg that matches VALUE.
3604 (if (widget-apply (car look) :match value)
3605 (setq old (car look)
3607 (setq look (cdr look)))))
3610 (cond ((= (length args) 0)
3612 ((= (length args) 1)
3614 ((and (= (length args) 2)
3616 (if (eq old (nth 0 args))
3621 (setq current (car args)
3624 (cons (cons (widget-apply current :menu-tag-get)
3627 (let ((val (completing-read prompt choices nil t)))
3629 (let ((try (try-completion val choices)))
3632 (cdr (assoc val choices)))
3635 (widget-prompt-value current prompt nil t)
3638 (define-widget 'radio 'radio-button-choice
3639 "A union of several sexp types."
3641 :format "%{%t%}:\n%v"
3642 :prompt-value 'widget-choice-prompt-value)
3644 (define-widget 'repeat 'editable-list
3645 "A variable length homogeneous list."
3647 :format "%{%t%}:\n%v%i\n")
3649 (define-widget 'set 'checklist
3650 "A list of members from a fixed set."
3652 :format "%{%t%}:\n%v")
3654 (define-widget 'boolean 'toggle
3655 "To be nil or non-nil, that is the question."
3657 :prompt-value 'widget-boolean-prompt-value
3658 :button-prefix 'widget-push-button-prefix
3659 :button-suffix 'widget-push-button-suffix
3660 :format "%{%t%}: %[Toggle%] %v\n"
3664 (defun widget-boolean-prompt-value (_widget prompt _value _unbound)
3665 ;; Toggle a boolean.
3668 ;;; The `color' Widget.
3671 (define-widget 'color 'editable-field
3672 "Choose a color name (with sample)."
3673 :format "%{%t%}: %v (%{sample%})\n"
3674 :value-create 'widget-color-value-create
3678 :completions (or facemenu-color-alist (defined-colors))
3679 :sample-face-get 'widget-color-sample-face-get
3680 :notify 'widget-color-notify
3681 :action 'widget-color-action)
3683 (defun widget-color-value-create (widget)
3684 (widget-field-value-create widget)
3686 (widget-create-child-and-convert
3688 :tag " Choose " :action 'widget-color--choose-action)
3689 (widget-insert " "))
3691 (defun widget-color--choose-action (widget &optional _event)
3692 (list-colors-display
3695 (when (buffer-live-p ,(current-buffer))
3696 (widget-value-set ',(widget-get widget :parent) color)
3697 (let* ((buf (get-buffer "*Colors*"))
3698 (win (get-buffer-window buf 0)))
3700 (and win (> (length (window-list)) 1)
3701 (delete-window win)))
3702 (pop-to-buffer ,(current-buffer))))))
3704 (defun widget-color-sample-face-get (widget)
3705 (let* ((value (condition-case nil
3706 (widget-value widget)
3707 (error (widget-get widget :value)))))
3708 (if (color-defined-p value)
3709 (list (cons 'foreground-color value))
3712 (defun widget-color-action (widget &optional event)
3713 "Prompt for a color."
3714 (let* ((tag (widget-apply widget :menu-tag-get))
3715 (prompt (concat tag ": "))
3716 (answer (facemenu-read-color prompt)))
3717 (unless (zerop (length answer))
3718 (widget-value-set widget answer)
3720 (widget-apply widget :notify widget event))))
3722 (defun widget-color-notify (widget child &optional event)
3723 "Update the sample, and notify the parent."
3724 (overlay-put (widget-get widget :sample-overlay)
3725 'face (widget-apply widget :sample-face-get))
3726 (widget-default-notify widget child event))
3730 (defun widget-echo-help (pos)
3731 "Display help-echo text for widget at POS."
3732 (let* ((widget (widget-at pos))
3733 (help-echo (and widget (widget-get widget :help-echo))))
3734 (if (functionp help-echo)
3735 (setq help-echo (funcall help-echo widget)))
3736 (if help-echo (message "%s" (eval help-echo)))))
3742 ;;; wid-edit.el ends here