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 (substitute-command-keys (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 (substitute-command-keys
1507 (widget-get widget :tag))))
1509 (widget-image-insert widget (or tag "image") image))
1513 (princ (widget-get widget :value)
1514 (current-buffer))))))
1516 (let ((doc (widget-get widget :doc)))
1518 (setq doc-begin (point))
1519 (insert (substitute-command-keys doc))
1520 (while (eq (preceding-char) ?\n)
1523 (setq doc-end (point)))))
1525 (widget-add-documentation-string-button widget))
1527 (if (and button-begin (not button-end))
1528 (widget-apply widget :value-create)
1529 (setq value-pos (point))))
1531 (widget-apply widget :format-handler escape)))))
1532 ;; Specify button, sample, and doc, and insert value.
1533 (and button-begin button-end
1534 (widget-specify-button widget button-begin button-end))
1535 (and sample-begin sample-end
1536 (widget-specify-sample widget sample-begin sample-end))
1537 (and doc-begin doc-end
1538 (widget-specify-doc widget doc-begin doc-end))
1540 (goto-char value-pos)
1541 (widget-apply widget :value-create)))
1542 (let ((from (point-min-marker))
1543 (to (point-max-marker)))
1544 (set-marker-insertion-type from t)
1545 (set-marker-insertion-type to nil)
1546 (widget-put widget :from from)
1547 (widget-put widget :to to)))
1548 (widget-clear-undo))
1550 (defun widget-default-format-handler (_widget escape)
1551 (error "Unknown escape `%c'" escape))
1553 (defun widget-default-button-face-get (widget)
1554 ;; Use :button-face or widget-button-face
1555 (or (widget-get widget :button-face)
1556 (let ((parent (widget-get widget :parent)))
1558 (widget-apply parent :button-face-get)
1559 widget-button-face))))
1561 (defun widget-default-mouse-face-get (widget)
1562 ;; Use :mouse-face or widget-mouse-face
1563 (or (widget-get widget :mouse-face)
1564 (let ((parent (widget-get widget :parent)))
1566 (widget-apply parent :mouse-face-get)
1567 widget-mouse-face))))
1569 (defun widget-default-sample-face-get (widget)
1570 ;; Use :sample-face.
1571 (widget-get widget :sample-face))
1573 (defun widget-default-delete (widget)
1574 "Remove widget from the buffer."
1575 (let ((from (widget-get widget :from))
1576 (to (widget-get widget :to))
1577 (inactive-overlay (widget-get widget :inactive))
1578 (button-overlay (widget-get widget :button-overlay))
1579 (sample-overlay (widget-get widget :sample-overlay))
1580 (doc-overlay (widget-get widget :doc-overlay))
1581 (inhibit-modification-hooks t)
1582 (inhibit-read-only t))
1583 (widget-apply widget :value-delete)
1584 (widget-children-value-delete widget)
1585 (when inactive-overlay
1586 (delete-overlay inactive-overlay))
1587 (when button-overlay
1588 (delete-overlay button-overlay))
1589 (when sample-overlay
1590 (delete-overlay sample-overlay))
1592 (delete-overlay doc-overlay))
1594 ;; Kludge: this doesn't need to be true for empty formats.
1595 (delete-region from to))
1596 (set-marker from nil)
1597 (set-marker to nil))
1598 (widget-clear-undo))
1600 (defun widget-default-value-set (widget value)
1601 "Recreate widget with new value."
1602 (let* ((old-pos (point))
1603 (from (copy-marker (widget-get widget :from)))
1604 (to (copy-marker (widget-get widget :to)))
1605 (offset (if (and (<= from old-pos) (<= old-pos to))
1606 (if (>= old-pos (1- to))
1608 (- old-pos from)))))
1609 ;;??? Bug: this ought to insert the new value before deleting the old one,
1610 ;; so that markers on either side of the value automatically
1611 ;; stay on the same side. -- rms.
1613 (goto-char (widget-get widget :from))
1614 (widget-apply widget :delete)
1615 (widget-put widget :value value)
1616 (widget-apply widget :create))
1619 (goto-char (+ (widget-get widget :to) offset 1))
1620 (goto-char (min (+ from offset) (1- (widget-get widget :to))))))))
1622 (defun widget-default-value-inline (widget)
1623 "Wrap value in a list unless it is inline."
1624 (if (widget-get widget :inline)
1625 (widget-value widget)
1626 (list (widget-value widget))))
1628 (defun widget-default-default-get (widget)
1630 (widget-get widget :value))
1632 (defun widget-default-menu-tag-get (widget)
1633 "Use tag or value for menus."
1634 (or (widget-get widget :menu-tag)
1635 (widget-get widget :tag)
1636 (widget-princ-to-string (widget-get widget :value))))
1638 (defun widget-default-active (widget)
1639 "Return t if this widget is active (user modifiable)."
1640 (or (widget-get widget :always-active)
1641 (and (not (widget-get widget :inactive))
1642 (let ((parent (widget-get widget :parent)))
1644 (widget-apply parent :active))))))
1646 (defun widget-default-deactivate (widget)
1647 "Make WIDGET inactive for user modifications."
1648 (widget-specify-inactive widget
1649 (widget-get widget :from)
1650 (widget-get widget :to)))
1652 (defun widget-default-action (widget &optional event)
1653 "Notify the parent when a widget changes."
1654 (let ((parent (widget-get widget :parent)))
1656 (widget-apply parent :notify widget event))))
1658 (defun widget-default-notify (widget _child &optional event)
1659 "Pass notification to parent."
1660 (widget-default-action widget event))
1662 (defun widget-default-prompt-value (_widget prompt _value _unbound)
1663 "Read an arbitrary value."
1664 (eval-minibuffer prompt))
1666 (defun widget-docstring (widget)
1667 "Return the documentation string specified by WIDGET, or nil if none.
1668 If WIDGET has a `:doc' property, that specifies the documentation string.
1669 Otherwise, try the `:documentation-property' property. If this
1670 is a function, call it with the widget's value as an argument; if
1671 it is a symbol, use this symbol together with the widget's value
1672 as the argument to `documentation-property'."
1673 (let ((doc (or (widget-get widget :doc)
1674 (let ((doc-prop (widget-get widget :documentation-property))
1675 (value (widget-get widget :value)))
1676 (cond ((functionp doc-prop)
1677 (funcall doc-prop value))
1679 (documentation-property value doc-prop t)))))))
1680 (when (and (stringp doc) (> (length doc) 0))
1681 ;; Remove any redundant `*' in the beginning.
1682 (when (eq (aref doc 0) ?*)
1683 (setq doc (substring doc 1)))
1684 ;; Remove trailing newlines.
1685 (when (string-match "\n+\\'" doc)
1686 (setq doc (substring doc 0 (match-beginning 0))))
1689 ;;; The `item' Widget.
1691 (define-widget 'item 'default
1692 "Constant items for inclusion in other widgets."
1693 :convert-widget 'widget-value-convert-widget
1694 :value-create 'widget-item-value-create
1695 :value-delete 'ignore
1696 :value-get 'widget-value-value-get
1697 :match 'widget-item-match
1698 :match-inline 'widget-item-match-inline
1699 :action 'widget-item-action
1702 (defun widget-item-value-create (widget)
1703 "Insert the printed representation of the value."
1704 (princ (widget-get widget :value) (current-buffer)))
1706 (defun widget-item-match (widget value)
1707 ;; Match if the value is the same.
1708 (equal (widget-get widget :value) value))
1710 (defun widget-item-match-inline (widget vals)
1711 ;; Match if the value is the same.
1712 (let ((value (widget-get widget :value)))
1714 (<= (length value) (length vals))
1715 (let ((head (widget-sublist vals 0 (length value))))
1716 (and (equal head value)
1717 (cons head (widget-sublist vals (length value))))))))
1719 (defun widget-sublist (list start &optional end)
1720 "Return the sublist of LIST from START to END.
1721 If END is omitted, it defaults to the length of LIST."
1722 (if (> start 0) (setq list (nthcdr start list)))
1724 (unless (<= end start)
1725 (setq list (copy-sequence list))
1726 (setcdr (nthcdr (- end start 1) list) nil)
1728 (copy-sequence list)))
1730 (defun widget-item-action (widget &optional event)
1731 ;; Just notify itself.
1732 (widget-apply widget :notify widget event))
1734 ;;; The `push-button' Widget.
1736 ;; (defcustom widget-push-button-gui t
1737 ;; "If non-nil, use GUI push buttons when available."
1741 ;; Cache already created GUI objects.
1742 ;; (defvar widget-push-button-cache nil)
1744 (defcustom widget-push-button-prefix "["
1745 "String used as prefix for buttons."
1747 :group 'widget-button)
1749 (defcustom widget-push-button-suffix "]"
1750 "String used as suffix for buttons."
1752 :group 'widget-button)
1754 (define-widget 'push-button 'item
1755 "A pushable button."
1758 :value-create 'widget-push-button-value-create
1761 (defun widget-push-button-value-create (widget)
1762 "Insert text representing the `on' and `off' states."
1763 (let* ((tag (or (substitute-command-keys (widget-get widget :tag))
1764 (widget-get widget :value)))
1765 (tag-glyph (widget-get widget :tag-glyph))
1766 (text (concat widget-push-button-prefix
1767 tag widget-push-button-suffix)))
1769 (widget-image-insert widget text tag-glyph)
1772 ;; (defun widget-gui-action (widget)
1773 ;; "Apply :action for WIDGET."
1774 ;; (widget-apply-action widget (this-command-keys)))
1776 ;;; The `link' Widget.
1778 (defcustom widget-link-prefix "["
1779 "String used as prefix for links."
1781 :group 'widget-button)
1783 (defcustom widget-link-suffix "]"
1784 "String used as suffix for links."
1786 :group 'widget-button)
1788 (define-widget 'link 'item
1790 :button-prefix 'widget-link-prefix
1791 :button-suffix 'widget-link-suffix
1792 :follow-link 'mouse-face
1793 :help-echo "Follow the link."
1796 ;;; The `info-link' Widget.
1798 (define-widget 'info-link 'link
1799 "A link to an info file."
1800 :action 'widget-info-link-action)
1802 (defun widget-info-link-action (widget &optional _event)
1803 "Open the info node specified by WIDGET."
1804 (info (widget-value widget)))
1806 ;;; The `url-link' Widget.
1808 (define-widget 'url-link 'link
1809 "A link to an www page."
1810 :action 'widget-url-link-action)
1812 (defun widget-url-link-action (widget &optional _event)
1813 "Open the URL specified by WIDGET."
1814 (browse-url (widget-value widget)))
1816 ;;; The `function-link' Widget.
1818 (define-widget 'function-link 'link
1819 "A link to an Emacs function."
1820 :action 'widget-function-link-action)
1822 (defun widget-function-link-action (widget &optional _event)
1823 "Show the function specified by WIDGET."
1824 (describe-function (widget-value widget)))
1826 ;;; The `variable-link' Widget.
1828 (define-widget 'variable-link 'link
1829 "A link to an Emacs variable."
1830 :action 'widget-variable-link-action)
1832 (defun widget-variable-link-action (widget &optional _event)
1833 "Show the variable specified by WIDGET."
1834 (describe-variable (widget-value widget)))
1836 ;;; The `file-link' Widget.
1838 (define-widget 'file-link 'link
1840 :action 'widget-file-link-action)
1842 (defun widget-file-link-action (widget &optional _event)
1843 "Find the file specified by WIDGET."
1844 (find-file (widget-value widget)))
1846 ;;; The `emacs-library-link' Widget.
1848 (define-widget 'emacs-library-link 'link
1849 "A link to an Emacs Lisp library file."
1850 :action 'widget-emacs-library-link-action)
1852 (defun widget-emacs-library-link-action (widget &optional _event)
1853 "Find the Emacs library file specified by WIDGET."
1854 (find-file (locate-library (widget-value widget))))
1856 ;;; The `emacs-commentary-link' Widget.
1858 (define-widget 'emacs-commentary-link 'link
1859 "A link to Commentary in an Emacs Lisp library file."
1860 :action 'widget-emacs-commentary-link-action)
1862 (defun widget-emacs-commentary-link-action (widget &optional _event)
1863 "Find the Commentary section of the Emacs file specified by WIDGET."
1864 (finder-commentary (widget-value widget)))
1866 ;;; The `editable-field' Widget.
1868 (define-widget 'editable-field 'default
1869 "An editable text field.
1870 Note: In an `editable-field' widget, the `%v' escape must be preceded
1871 by some other text in the `:format' string (if specified)."
1872 :convert-widget 'widget-value-convert-widget
1873 :keymap widget-field-keymap
1875 :help-echo "M-TAB: complete field; RET: enter value"
1877 :prompt-internal 'widget-field-prompt-internal
1878 :prompt-history 'widget-field-history
1879 :prompt-value 'widget-field-prompt-value
1880 :action 'widget-field-action
1881 :validate 'widget-field-validate
1883 :error "Field's value doesn't match allowed forms"
1884 :value-create 'widget-field-value-create
1885 :value-set 'widget-field-value-set
1886 :value-delete 'widget-field-value-delete
1887 :value-get 'widget-field-value-get
1888 :match 'widget-field-match)
1890 (defvar widget-field-history nil
1891 "History of field minibuffer edits.")
1893 (defun widget-field-prompt-internal (_widget prompt initial history)
1894 "Read string for WIDGET prompting with PROMPT.
1895 INITIAL is the initial input and HISTORY is a symbol containing
1897 (read-string prompt initial history))
1899 (defun widget-field-prompt-value (widget prompt value unbound)
1900 "Prompt for a string."
1901 (widget-apply widget
1903 (widget-apply widget
1904 :prompt-internal prompt
1906 (cons (widget-apply widget
1907 :value-to-internal value)
1909 (widget-get widget :prompt-history))))
1911 (defvar widget-edit-functions nil)
1913 (defun widget-field-action (widget &optional _event)
1914 "Move to next field."
1916 (run-hook-with-args 'widget-edit-functions widget))
1918 (defun widget-field-validate (widget)
1919 "Valid if the content matches `:valid-regexp'."
1920 (unless (string-match (widget-get widget :valid-regexp)
1921 (widget-apply widget :value-get))
1924 (defun widget-field-value-set (widget value)
1925 "Set an editable text field WIDGET to VALUE"
1926 (let ((from (widget-field-start widget))
1927 (to (widget-field-text-end widget))
1928 (buffer (widget-field-buffer widget)))
1929 (when (and from to (buffer-live-p buffer))
1930 (with-current-buffer buffer
1932 (delete-char (- to from))
1935 (defun widget-field-value-create (widget)
1936 "Create an editable text field."
1937 (let ((size (widget-get widget :size))
1938 (value (widget-get widget :value))
1940 ;; This is changed to a real overlay in `widget-setup'. We
1941 ;; need the end points to behave differently until
1942 ;; `widget-setup' is called.
1943 (overlay (cons (make-marker) (make-marker))))
1944 (widget-put widget :field-overlay overlay)
1947 (< (length value) size)
1948 (insert-char ?\s (- size (length value))))
1949 (unless (memq widget widget-field-list)
1950 (setq widget-field-new (cons widget widget-field-new)))
1951 (move-marker (cdr overlay) (point))
1952 (set-marker-insertion-type (cdr overlay) nil)
1955 (move-marker (car overlay) from)
1956 (set-marker-insertion-type (car overlay) t)))
1958 (defun widget-field-value-delete (widget)
1959 "Remove the widget from the list of active editing fields."
1960 (setq widget-field-list (delq widget widget-field-list))
1961 (setq widget-field-new (delq widget widget-field-new))
1962 ;; These are nil if the :format string doesn't contain `%v'.
1963 (let ((overlay (widget-get widget :field-overlay)))
1964 (when (overlayp overlay)
1965 (delete-overlay overlay))))
1967 (defun widget-field-value-get (widget &optional no-truncate)
1968 "Return current text in editing field.
1969 Normally, trailing spaces within the editing field are truncated.
1970 But if NO-TRUNCATE is non-nil, include them."
1971 (let ((from (widget-field-start widget))
1973 (widget-field-end widget)
1974 (widget-field-text-end widget)))
1975 (buffer (widget-field-buffer widget))
1976 (secret (widget-get widget :secret))
1977 (old (current-buffer)))
1981 (let ((result (buffer-substring-no-properties from to)))
1984 (while (< (+ from index) to)
1986 (get-char-property (+ from index) 'secret))
1987 (setq index (1+ index)))))
1990 (widget-get widget :value))))
1992 (defun widget-field-match (_widget value)
1993 ;; Match any string.
1996 ;;; The `text' Widget.
1998 (define-widget 'text 'editable-field
1999 "A multiline text area."
2000 :keymap widget-text-keymap)
2002 ;;; The `menu-choice' Widget.
2004 (define-widget 'menu-choice 'default
2005 "A menu of options."
2006 :convert-widget 'widget-types-convert-widget
2007 :copy 'widget-types-copy
2008 :format "%[%t%]: %v"
2011 :void '(item :format "invalid (%t)\n")
2012 :value-create 'widget-choice-value-create
2013 :value-get 'widget-child-value-get
2014 :value-inline 'widget-child-value-inline
2015 :default-get 'widget-choice-default-get
2016 :mouse-down-action 'widget-choice-mouse-down-action
2017 :action 'widget-choice-action
2018 :error "Make a choice"
2019 :validate 'widget-choice-validate
2020 :match 'widget-choice-match
2021 :match-inline 'widget-choice-match-inline)
2023 (defun widget-choice-value-create (widget)
2024 "Insert the first choice that matches the value."
2025 (let ((value (widget-get widget :value))
2026 (args (widget-get widget :args))
2027 (explicit (widget-get widget :explicit-choice))
2031 ;; If the user specified the choice for this value,
2032 ;; respect that choice.
2033 (widget-put widget :children (list (widget-create-child-value
2034 widget explicit value)))
2035 (widget-put widget :choice explicit)
2036 (widget-put widget :explicit-choice nil))
2038 (setq current (car args)
2040 (when (widget-apply current :match value)
2041 (widget-put widget :children (list (widget-create-child-value
2042 widget current value)))
2043 (widget-put widget :choice current)
2047 (let ((void (widget-get widget :void)))
2048 (widget-put widget :children (list (widget-create-child-and-convert
2049 widget void :value value)))
2050 (widget-put widget :choice void))))))
2052 (defun widget-choice-default-get (widget)
2053 ;; Get default for the first choice.
2054 (widget-default-get (car (widget-get widget :args))))
2056 (defcustom widget-choice-toggle nil
2057 "If non-nil, a binary choice will just toggle between the values.
2058 Otherwise, the user will explicitly have to choose between the values
2059 when he invoked the menu."
2063 (defun widget-choice-mouse-down-action (widget &optional _event)
2064 ;; Return non-nil if we need a menu.
2065 (let ((args (widget-get widget :args))
2066 (old (widget-get widget :choice)))
2067 (cond ((not (display-popup-menus-p))
2068 ;; No place to pop up a menu.
2070 ((< (length args) 2)
2071 ;; Empty or singleton list, just return the value.
2073 ((> (length args) widget-menu-max-size)
2074 ;; Too long, prompt.
2076 ((> (length args) 2)
2077 ;; Reasonable sized list, use menu.
2079 ((and widget-choice-toggle (memq old args))
2083 ;; Ask which of the two.
2086 (defun widget-choice-action (widget &optional event)
2088 (let ((args (widget-get widget :args))
2089 (old (widget-get widget :choice))
2090 (tag (widget-apply widget :menu-tag-get))
2091 (completion-ignore-case (widget-get widget :case-fold))
2094 ;; Remember old value.
2095 (if (and old (not (widget-apply widget :validate)))
2096 (let* ((external (widget-value widget))
2097 (internal (widget-apply old :value-to-internal external)))
2098 (widget-put old :value internal)))
2101 (cond ((= (length args) 0)
2103 ((= (length args) 1)
2105 ((and widget-choice-toggle
2108 (if (eq old (nth 0 args))
2113 (setq current (car args)
2116 (cons (cons (widget-apply current :menu-tag-get)
2119 (setq this-explicit t)
2120 (widget-choose tag (reverse choices) event))))
2122 ;; If this was an explicit user choice, record the choice,
2123 ;; so that widget-choice-value-create will respect it.
2125 (widget-put widget :explicit-choice current))
2126 (widget-value-set widget (widget-default-get current))
2128 (widget-apply widget :notify widget event)))
2129 (run-hook-with-args 'widget-edit-functions widget))
2131 (defun widget-choice-validate (widget)
2132 ;; Valid if we have made a valid choice.
2133 (if (eq (widget-get widget :void) (widget-get widget :choice))
2135 (widget-apply (car (widget-get widget :children)) :validate)))
2137 (defun widget-choice-match (widget value)
2138 ;; Matches if one of the choices matches.
2139 (let ((args (widget-get widget :args))
2141 (while (and args (not found))
2142 (setq current (car args)
2144 found (widget-apply current :match value)))
2147 (defun widget-choice-match-inline (widget vals)
2148 ;; Matches if one of the choices matches.
2149 (let ((args (widget-get widget :args))
2151 (while (and args (null found))
2152 (setq current (car args)
2154 found (widget-match-inline current vals)))
2157 ;;; The `toggle' Widget.
2159 (define-widget 'toggle 'item
2160 "Toggle between two states."
2162 :value-create 'widget-toggle-value-create
2163 :action 'widget-toggle-action
2164 :match (lambda (_widget _value) t)
2168 (defun widget-toggle-value-create (widget)
2169 "Insert text representing the `on' and `off' states."
2170 (let* ((val (widget-value widget))
2171 (text (substitute-command-keys
2172 (widget-get widget (if val :on :off))))
2173 (img (widget-image-find
2174 (widget-get widget (if val :on-glyph :off-glyph)))))
2175 (widget-image-insert widget (or text "")
2177 (append img '(:ascent center))))))
2179 (defun widget-toggle-action (widget &optional event)
2181 (widget-value-set widget (not (widget-value widget)))
2182 (widget-apply widget :notify widget event)
2183 (run-hook-with-args 'widget-edit-functions widget))
2185 ;;; The `checkbox' Widget.
2187 (define-widget 'checkbox 'toggle
2188 "A checkbox toggle."
2193 ;; We could probably do the same job as the images using single
2194 ;; space characters in a boxed face with a stretch specification to
2195 ;; make them square.
2198 :off-glyph "unchecked"
2199 :help-echo "Toggle this item."
2200 :action 'widget-checkbox-action)
2202 (defun widget-checkbox-action (widget &optional event)
2203 "Toggle checkbox, notify parent, and set active state of sibling."
2204 (widget-toggle-action widget event)
2205 (let ((sibling (widget-get-sibling widget)))
2207 (if (widget-value widget)
2208 (widget-apply sibling :activate)
2209 (widget-apply sibling :deactivate))
2210 (widget-clear-undo))))
2212 ;;; The `checklist' Widget.
2214 (define-widget 'checklist 'default
2215 "A multiple choice widget."
2216 :convert-widget 'widget-types-convert-widget
2217 :copy 'widget-types-copy
2220 :entry-format "%b %v"
2222 :value-create 'widget-checklist-value-create
2223 :value-get 'widget-checklist-value-get
2224 :validate 'widget-checklist-validate
2225 :match 'widget-checklist-match
2226 :match-inline 'widget-checklist-match-inline)
2228 (defun widget-checklist-value-create (widget)
2229 ;; Insert all values
2230 (let ((alist (widget-checklist-match-find widget))
2231 (args (widget-get widget :args)))
2233 (widget-checklist-add-item widget item (assq item alist)))
2234 (widget-put widget :children (nreverse (widget-get widget :children)))))
2236 (defun widget-checklist-add-item (widget type chosen)
2237 "Create checklist item in WIDGET of type TYPE.
2238 If the item is checked, CHOSEN is a cons whose cdr is the value."
2239 (and (eq (preceding-char) ?\n)
2240 (widget-get widget :indent)
2241 (insert-char ?\s (widget-get widget :indent)))
2242 (widget-specify-insert
2243 (let* ((children (widget-get widget :children))
2244 (buttons (widget-get widget :buttons))
2245 (button-args (or (widget-get type :sibling-args)
2246 (widget-get widget :button-args)))
2249 (insert (widget-get widget :entry-format))
2251 ;; Parse % escapes in format.
2252 (while (re-search-forward "%\\([bv%]\\)" nil t)
2253 (let ((escape (char-after (match-beginning 1))))
2255 (cond ((eq escape ?%)
2258 (setq button (apply 'widget-create-child-and-convert
2260 :value (not (null chosen))
2265 (let ((child (widget-create-child widget type)))
2266 (widget-apply child :deactivate)
2268 ((widget-get type :inline)
2269 (widget-create-child-value
2270 widget type (cdr chosen)))
2272 (widget-create-child-value
2273 widget type (car (cdr chosen)))))))
2275 (error "Unknown escape `%c'" escape)))))
2276 ;; Update properties.
2277 (and button child (widget-put child :button button))
2278 (and button (widget-put widget :buttons (cons button buttons)))
2279 (and child (widget-put widget :children (cons child children))))))
2281 (defun widget-checklist-match (widget vals)
2282 ;; All values must match a type in the checklist.
2284 (null (cdr (widget-checklist-match-inline widget vals)))))
2286 (defun widget-checklist-match-inline (widget vals)
2287 ;; Find the values which match a type in the checklist.
2288 (let ((greedy (widget-get widget :greedy))
2289 (args (copy-sequence (widget-get widget :args)))
2292 (let ((answer (widget-checklist-match-up args vals)))
2294 (let ((vals2 (widget-match-inline answer vals)))
2295 (setq found (append found (car vals2))
2297 args (delq answer args))))
2299 (setq rest (append rest (list (car vals)))
2302 (setq rest (append rest vals)
2306 (defun widget-checklist-match-find (widget &optional vals)
2307 "Find the vals which match a type in the checklist.
2308 Return an alist of (TYPE MATCH)."
2309 (or vals (setq vals (widget-get widget :value)))
2310 (let ((greedy (widget-get widget :greedy))
2311 (args (copy-sequence (widget-get widget :args)))
2314 (let ((answer (widget-checklist-match-up args vals)))
2316 (let ((match (widget-match-inline answer vals)))
2317 (setq found (cons (cons answer (car match)) found)
2319 args (delq answer args))))
2321 (setq vals (cdr vals)))
2326 (defun widget-checklist-match-up (args vals)
2327 "Return the first type from ARGS that matches VALS."
2328 (let (current found)
2329 (while (and args (null found))
2330 (setq current (car args)
2332 found (widget-match-inline current vals)))
2336 (defun widget-checklist-value-get (widget)
2337 ;; The values of all selected items.
2338 (let ((children (widget-get widget :children))
2341 (setq child (car children)
2342 children (cdr children))
2343 (if (widget-value (widget-get child :button))
2344 (setq result (append result (widget-apply child :value-inline)))))
2347 (defun widget-checklist-validate (widget)
2348 ;; Ticked children must be valid.
2349 (let ((children (widget-get widget :children))
2351 (while (and children (not found))
2352 (setq child (car children)
2353 children (cdr children)
2354 button (widget-get child :button)
2355 found (and (widget-value button)
2356 (widget-apply child :validate))))
2359 ;;; The `option' Widget
2361 (define-widget 'option 'checklist
2362 "An widget with an optional item."
2365 ;;; The `choice-item' Widget.
2367 (define-widget 'choice-item 'item
2368 "Button items that delegate action events to their parents."
2369 :action 'widget-parent-action
2370 :format "%[%t%] \n")
2372 ;;; The `radio-button' Widget.
2374 (define-widget 'radio-button 'toggle
2375 "A radio button for use in the `radio' widget."
2376 :notify 'widget-radio-button-notify
2383 :off-glyph "radio0")
2385 (defun widget-radio-button-notify (widget _child &optional event)
2387 (widget-apply (widget-get widget :parent) :action widget event))
2389 ;;; The `radio-button-choice' Widget.
2391 (define-widget 'radio-button-choice 'default
2392 "Select one of multiple options."
2393 :convert-widget 'widget-types-convert-widget
2394 :copy 'widget-types-copy
2397 :entry-format "%b %v"
2398 :value-create 'widget-radio-value-create
2399 :value-get 'widget-radio-value-get
2400 :value-inline 'widget-radio-value-inline
2401 :value-set 'widget-radio-value-set
2402 :error "You must push one of the buttons"
2403 :validate 'widget-radio-validate
2404 :match 'widget-choice-match
2405 :match-inline 'widget-choice-match-inline
2406 :action 'widget-radio-action)
2408 (defun widget-radio-value-create (widget)
2409 ;; Insert all values
2410 (let ((args (widget-get widget :args))
2413 (setq arg (car args)
2415 (widget-radio-add-item widget arg))))
2417 (defun widget-radio-add-item (widget type)
2418 "Add to radio widget WIDGET a new radio button item of type TYPE."
2419 ;; (setq type (widget-convert type))
2420 (and (eq (preceding-char) ?\n)
2421 (widget-get widget :indent)
2422 (insert-char ?\s (widget-get widget :indent)))
2423 (widget-specify-insert
2424 (let* ((value (widget-get widget :value))
2425 (children (widget-get widget :children))
2426 (buttons (widget-get widget :buttons))
2427 (button-args (or (widget-get type :sibling-args)
2428 (widget-get widget :button-args)))
2430 (chosen (and (null (widget-get widget :choice))
2431 (widget-apply type :match value)))
2433 (insert (widget-get widget :entry-format))
2435 ;; Parse % escapes in format.
2436 (while (re-search-forward "%\\([bv%]\\)" nil t)
2437 (let ((escape (char-after (match-beginning 1))))
2439 (cond ((eq escape ?%)
2442 (setq button (apply 'widget-create-child-and-convert
2443 widget 'radio-button
2444 :value (not (null chosen))
2447 (setq child (if chosen
2448 (widget-create-child-value
2450 (widget-create-child widget type)))
2452 (widget-apply child :deactivate)))
2454 (error "Unknown escape `%c'" escape)))))
2455 ;; Update properties.
2457 (widget-put widget :choice type))
2459 (widget-put child :button button)
2460 (widget-put widget :buttons (nconc buttons (list button))))
2462 (widget-put widget :children (nconc children (list child))))
2465 (defun widget-radio-value-get (widget)
2466 ;; Get value of the child widget.
2467 (let ((chosen (widget-radio-chosen widget)))
2468 (and chosen (widget-value chosen))))
2470 (defun widget-radio-chosen (widget)
2471 "Return the widget representing the chosen radio button."
2472 (let ((children (widget-get widget :children))
2475 (setq current (car children)
2476 children (cdr children))
2477 (when (widget-apply (widget-get current :button) :value-get)
2482 (defun widget-radio-value-inline (widget)
2483 ;; Get value of the child widget.
2484 (let ((children (widget-get widget :children))
2487 (setq current (car children)
2488 children (cdr children))
2489 (when (widget-apply (widget-get current :button) :value-get)
2490 (setq found (widget-apply current :value-inline)
2494 (defun widget-radio-value-set (widget value)
2495 ;; We can't just delete and recreate a radio widget, since children
2496 ;; can be added after the original creation and won't be recreated
2498 (let ((children (widget-get widget :children))
2501 (setq current (car children)
2502 children (cdr children))
2503 (let* ((button (widget-get current :button))
2504 (match (and (not found)
2505 (widget-apply current :match value))))
2506 (widget-value-set button match)
2509 (widget-value-set current value)
2510 (widget-apply current :activate))
2511 (widget-apply current :deactivate))
2512 (setq found (or found match))))))
2514 (defun widget-radio-validate (widget)
2515 ;; Valid if we have made a valid choice.
2516 (let ((children (widget-get widget :children))
2517 current found button)
2518 (while (and children (not found))
2519 (setq current (car children)
2520 children (cdr children)
2521 button (widget-get current :button)
2522 found (widget-apply button :value-get)))
2524 (widget-apply current :validate)
2527 (defun widget-radio-action (widget child event)
2528 ;; Check if a radio button was pressed.
2529 (let ((children (widget-get widget :children))
2530 (buttons (widget-get widget :buttons))
2532 (when (memq child buttons)
2534 (setq current (car children)
2535 children (cdr children))
2536 (let* ((button (widget-get current :button)))
2537 (cond ((eq child button)
2538 (widget-value-set button t)
2539 (widget-apply current :activate))
2540 ((widget-value button)
2541 (widget-value-set button nil)
2542 (widget-apply current :deactivate)))))))
2543 ;; Pass notification to parent.
2544 (widget-apply widget :notify child event))
2546 ;;; The `insert-button' Widget.
2548 (define-widget 'insert-button 'push-button
2549 "An insert button for the `editable-list' widget."
2551 :help-echo "Insert a new item into the list at this position."
2552 :action 'widget-insert-button-action)
2554 (defun widget-insert-button-action (widget &optional _event)
2555 ;; Ask the parent to insert a new item.
2556 (widget-apply (widget-get widget :parent)
2557 :insert-before (widget-get widget :widget)))
2559 ;;; The `delete-button' Widget.
2561 (define-widget 'delete-button 'push-button
2562 "A delete button for the `editable-list' widget."
2564 :help-echo "Delete this item from the list."
2565 :action 'widget-delete-button-action)
2567 (defun widget-delete-button-action (widget &optional _event)
2568 ;; Ask the parent to insert a new item.
2569 (widget-apply (widget-get widget :parent)
2570 :delete-at (widget-get widget :widget)))
2572 ;;; The `editable-list' Widget.
2574 ;; (defcustom widget-editable-list-gui nil
2575 ;; "If non-nil, use GUI push-buttons in editable list when available."
2579 (define-widget 'editable-list 'default
2580 "A variable list of widgets of the same type."
2581 :convert-widget 'widget-types-convert-widget
2582 :copy 'widget-types-copy
2585 :format-handler 'widget-editable-list-format-handler
2586 :entry-format "%i %d %v"
2587 :value-create 'widget-editable-list-value-create
2588 :value-get 'widget-editable-list-value-get
2589 :validate 'widget-children-validate
2590 :match 'widget-editable-list-match
2591 :match-inline 'widget-editable-list-match-inline
2592 :insert-before 'widget-editable-list-insert-before
2593 :delete-at 'widget-editable-list-delete-at)
2595 (defun widget-editable-list-format-handler (widget escape)
2596 ;; We recognize the insert button.
2597 ;; (let ((widget-push-button-gui widget-editable-list-gui))
2598 (cond ((eq escape ?i)
2599 (and (widget-get widget :indent)
2600 (insert-char ?\s (widget-get widget :indent)))
2601 (apply 'widget-create-child-and-convert
2602 widget 'insert-button
2603 (widget-get widget :append-button-args)))
2605 (widget-default-format-handler widget escape)))
2609 (defun widget-editable-list-value-create (widget)
2610 ;; Insert all values
2611 (let* ((value (widget-get widget :value))
2612 (type (nth 0 (widget-get widget :args)))
2614 (widget-put widget :value-pos (point-marker))
2615 (set-marker-insertion-type (widget-get widget :value-pos) t)
2617 (let ((answer (widget-match-inline type value)))
2619 (setq children (cons (widget-editable-list-entry-create
2621 (if (widget-get type :inline)
2628 (widget-put widget :children (nreverse children))))
2630 (defun widget-editable-list-value-get (widget)
2631 ;; Get value of the child widget.
2632 (apply 'append (mapcar (lambda (child) (widget-apply child :value-inline))
2633 (widget-get widget :children))))
2635 (defun widget-editable-list-match (widget value)
2636 ;; Value must be a list and all the members must match the type.
2638 (null (cdr (widget-editable-list-match-inline widget value)))))
2640 (defun widget-editable-list-match-inline (widget value)
2641 (let ((type (nth 0 (widget-get widget :args)))
2644 (while (and value ok)
2645 (let ((answer (widget-match-inline type value)))
2647 (setq found (append found (car answer))
2650 (cons found value)))
2652 (defun widget-editable-list-insert-before (widget before)
2653 ;; Insert a new child in the list of children.
2655 (let ((children (widget-get widget :children))
2656 (inhibit-read-only t)
2657 before-change-functions
2658 after-change-functions)
2660 (goto-char (widget-get before :entry-from)))
2662 (goto-char (widget-get widget :value-pos))))
2663 (let ((child (widget-editable-list-entry-create
2665 (when (< (widget-get child :entry-from) (widget-get widget :from))
2666 (set-marker (widget-get widget :from)
2667 (widget-get child :entry-from)))
2668 (if (eq (car children) before)
2669 (widget-put widget :children (cons child children))
2670 (while (not (eq (car (cdr children)) before))
2671 (setq children (cdr children)))
2672 (setcdr children (cons child (cdr children)))))))
2674 (widget-apply widget :notify widget))
2676 (defun widget-editable-list-delete-at (widget child)
2677 ;; Delete child from list of children.
2679 (let ((buttons (copy-sequence (widget-get widget :buttons)))
2681 (inhibit-read-only t)
2682 before-change-functions
2683 after-change-functions)
2685 (setq button (car buttons)
2686 buttons (cdr buttons))
2687 (when (eq (widget-get button :widget) child)
2689 :buttons (delq button (widget-get widget :buttons)))
2690 (widget-delete button))))
2691 (let ((entry-from (widget-get child :entry-from))
2692 (entry-to (widget-get child :entry-to))
2693 (inhibit-read-only t)
2694 before-change-functions
2695 after-change-functions)
2696 (widget-delete child)
2697 (delete-region entry-from entry-to)
2698 (set-marker entry-from nil)
2699 (set-marker entry-to nil))
2700 (widget-put widget :children (delq child (widget-get widget :children))))
2702 (widget-apply widget :notify widget))
2704 (defun widget-editable-list-entry-create (widget value conv)
2705 ;; Create a new entry to the list.
2706 (let ((type (nth 0 (widget-get widget :args)))
2707 ;; (widget-push-button-gui widget-editable-list-gui)
2708 child delete insert)
2709 (widget-specify-insert
2711 (and (widget-get widget :indent)
2712 (insert-char ?\s (widget-get widget :indent)))
2713 (insert (widget-get widget :entry-format)))
2714 ;; Parse % escapes in format.
2715 (while (re-search-forward "%\\(.\\)" nil t)
2716 (let ((escape (char-after (match-beginning 1))))
2718 (cond ((eq escape ?%)
2721 (setq insert (apply 'widget-create-child-and-convert
2722 widget 'insert-button
2723 (widget-get widget :insert-button-args))))
2725 (setq delete (apply 'widget-create-child-and-convert
2726 widget 'delete-button
2727 (widget-get widget :delete-button-args))))
2730 (setq child (widget-create-child-value
2732 (setq child (widget-create-child-value
2733 widget type (widget-default-get type)))))
2735 (error "Unknown escape `%c'" escape)))))
2736 (let ((buttons (widget-get widget :buttons)))
2737 (if insert (push insert buttons))
2738 (if delete (push delete buttons))
2739 (widget-put widget :buttons buttons))
2740 (let ((entry-from (point-min-marker))
2741 (entry-to (point-max-marker)))
2742 (set-marker-insertion-type entry-from t)
2743 (set-marker-insertion-type entry-to nil)
2744 (widget-put child :entry-from entry-from)
2745 (widget-put child :entry-to entry-to)))
2746 (if insert (widget-put insert :widget child))
2747 (if delete (widget-put delete :widget child))
2750 ;;; The `group' Widget.
2752 (define-widget 'group 'default
2753 "A widget which groups other widgets inside."
2754 :convert-widget 'widget-types-convert-widget
2755 :copy 'widget-types-copy
2757 :value-create 'widget-group-value-create
2758 :value-get 'widget-editable-list-value-get
2759 :default-get 'widget-group-default-get
2760 :validate 'widget-children-validate
2761 :match 'widget-group-match
2762 :match-inline 'widget-group-match-inline)
2764 (defun widget-group-value-create (widget)
2765 ;; Create each component.
2766 (let ((args (widget-get widget :args))
2767 (value (widget-get widget :value))
2768 arg answer children)
2770 (setq arg (car args)
2772 answer (widget-match-inline arg value)
2774 (and (eq (preceding-char) ?\n)
2775 (widget-get widget :indent)
2776 (insert-char ?\s (widget-get widget :indent)))
2777 (push (cond ((null answer)
2778 (widget-create-child widget arg))
2779 ((widget-get arg :inline)
2780 (widget-create-child-value widget arg (car answer)))
2782 (widget-create-child-value widget arg (car (car answer)))))
2784 (widget-put widget :children (nreverse children))))
2786 (defun widget-group-default-get (widget)
2787 ;; Get the default of the components.
2788 (mapcar 'widget-default-get (widget-get widget :args)))
2790 (defun widget-group-match (widget vals)
2791 ;; Match if the components match.
2793 (let ((match (widget-group-match-inline widget vals)))
2794 (and match (null (cdr match))))))
2796 (defun widget-group-match-inline (widget vals)
2797 ;; Match if the components match.
2798 (let ((args (widget-get widget :args))
2799 argument answer found)
2801 (setq argument (car args)
2803 (if (setq answer (widget-match-inline argument vals))
2804 (setq found (append found (car answer))
2809 (cons found vals))))
2811 ;;; The `visibility' Widget.
2813 (define-widget 'visibility 'item
2814 "An indicator and manipulator for hidden items.
2816 The following properties have special meanings for this widget:
2817 :on-glyph Image filename or spec to display when the item is visible.
2818 :on Text shown if the \"on\" image is nil or cannot be displayed.
2819 :off-glyph Image filename or spec to display when the item is hidden.
2820 :off Text shown if the \"off\" image is nil cannot be displayed."
2828 :value-create 'widget-visibility-value-create
2829 :action 'widget-toggle-action
2830 :match (lambda (_widget _value) t))
2832 (defalias 'widget-visibility-value-create 'widget-toggle-value-create)
2834 ;;; The `documentation-link' Widget.
2836 ;; This is a helper widget for `documentation-string'.
2838 (define-widget 'documentation-link 'link
2839 "Link type used in documentation strings."
2841 :help-echo "Describe this symbol"
2842 :action 'widget-documentation-link-action)
2844 (defun widget-documentation-link-action (widget &optional _event)
2845 "Display documentation for WIDGET's value. Ignore optional argument EVENT."
2846 (let* ((string (widget-get widget :value))
2847 (symbol (intern string)))
2848 (if (and (fboundp symbol) (boundp symbol))
2849 ;; If there are two doc strings, give the user a way to pick one.
2850 (apropos (concat "\\`" (regexp-quote string) "\\'"))
2853 (describe-function symbol))
2855 (describe-face symbol))
2857 (describe-package symbol))
2858 ((or (boundp symbol) (get symbol 'variable-documentation))
2859 (describe-variable symbol))
2861 (message "No documentation available for %s" symbol))))))
2863 (defcustom widget-documentation-links t
2864 "Add hyperlinks to documentation strings when non-nil."
2866 :group 'widget-documentation)
2868 (defcustom widget-documentation-link-regexp "['`‘]\\([^\n `'‘’]+\\)['’]"
2869 "Regexp for matching potential links in documentation strings.
2870 The first group should be the link itself."
2872 :group 'widget-documentation)
2874 (defcustom widget-documentation-link-p 'intern-soft
2875 "Predicate used to test if a string is useful as a link.
2876 The value should be a function. The function will be called with one
2877 argument, a string, and should return non-nil if there should be a
2878 link for that string."
2880 :options '(widget-documentation-link-p)
2881 :group 'widget-documentation)
2883 (defcustom widget-documentation-link-type 'documentation-link
2884 "Widget type used for links in documentation strings."
2886 :group 'widget-documentation)
2888 (defun widget-documentation-link-add (widget from to)
2889 (widget-specify-doc widget from to)
2890 (when widget-documentation-links
2891 (let ((regexp widget-documentation-link-regexp)
2892 (buttons (widget-get widget :buttons))
2893 (widget-mouse-face (default-value 'widget-mouse-face))
2894 (widget-button-face widget-documentation-face)
2895 (widget-button-pressed-face widget-documentation-face))
2898 (while (re-search-forward regexp to t)
2899 (let ((name (match-string 1))
2900 (begin (match-beginning 1))
2901 (end (match-end 1)))
2902 (when (funcall widget-documentation-link-p name)
2903 (push (widget-convert-button widget-documentation-link-type
2904 begin end :value name)
2906 (widget-put widget :buttons buttons))))
2908 ;;; The `documentation-string' Widget.
2910 (define-widget 'documentation-string 'item
2911 "A documentation string."
2913 :action 'widget-documentation-string-action
2914 :value-create 'widget-documentation-string-value-create
2915 :visibility-widget 'visibility)
2917 (defun widget-documentation-string-value-create (widget)
2918 ;; Insert documentation string.
2919 (let ((doc (substitute-command-keys (widget-value widget)))
2920 (indent (widget-get widget :indent))
2921 (shown (widget-get (widget-get widget :parent) :documentation-shown))
2923 (if (string-match "\n" doc)
2924 (let ((before (substring doc 0 (match-beginning 0)))
2925 (after (substring doc (match-end 0)))
2927 (widget-documentation-string-indent-to indent)
2929 (widget-documentation-link-add widget start (point))
2931 (widget-create-child-and-convert
2932 widget (widget-get widget :visibility-widget)
2933 :help-echo "Show or hide rest of the documentation."
2937 :action 'widget-parent-action
2941 (setq start (point))
2942 (when (and indent (not (zerop indent)))
2943 (insert-char ?\s indent))
2946 (widget-documentation-link-add widget start end)
2947 ;; Indent the subsequent lines.
2948 (when (and indent (> indent 0))
2951 (narrow-to-region start end)
2952 (goto-char (point-min))
2953 (while (search-forward "\n" nil t)
2954 (widget-documentation-string-indent-to indent))))))
2955 (widget-put widget :buttons (list button)))
2956 (widget-documentation-string-indent-to indent)
2958 (widget-documentation-link-add widget start (point))))
2961 (defun widget-documentation-string-indent-to (col)
2962 (when (and (numberp col)
2964 (let ((opoint (point)))
2966 (put-text-property opoint (point)
2967 'display `(space :align-to ,col)))))
2969 (defun widget-documentation-string-action (widget &rest _ignore)
2970 ;; Toggle documentation.
2971 (let ((parent (widget-get widget :parent)))
2972 (widget-put parent :documentation-shown
2973 (not (widget-get parent :documentation-shown))))
2975 (widget-value-set widget (widget-value widget)))
2977 (defun widget-add-documentation-string-button (widget &rest args)
2978 "Insert a new `documentation-string' widget based on WIDGET.
2979 The new widget becomes a child of WIDGET, and is also added to
2980 its `:buttons' list. The documentation string is found from
2981 WIDGET using the function `widget-docstring'.
2982 Optional ARGS specifies additional keyword arguments for the
2983 `documentation-string' widget."
2984 (let ((doc (widget-docstring widget))
2985 (indent (widget-get widget :indent))
2986 (doc-indent (widget-get widget :documentation-indent)))
2988 (and (eq (preceding-char) ?\n)
2990 (insert-char ?\s indent))
2991 (unless (or (numberp doc-indent) (null doc-indent))
2992 (setq doc-indent 0))
2993 (widget-put widget :buttons
2994 (cons (apply 'widget-create-child-and-convert
2995 widget 'documentation-string
2997 (nconc args (list doc)))
2998 (widget-get widget :buttons))))))
3000 ;;; The Sexp Widgets.
3002 (define-widget 'const 'item
3003 "An immutable sexp."
3004 :prompt-value 'widget-const-prompt-value
3007 (defun widget-const-prompt-value (widget _prompt _value _unbound)
3008 ;; Return the value of the const.
3009 (widget-value widget))
3011 (define-widget 'function-item 'const
3012 "An immutable function name."
3014 :documentation-property (lambda (symbol)
3016 (documentation symbol t)
3019 (define-widget 'variable-item 'const
3020 "An immutable variable name."
3022 :documentation-property 'variable-documentation)
3024 (define-widget 'other 'sexp
3025 "Matches any value, but doesn't let the user edit the value.
3026 This is useful as last item in a `choice' widget.
3027 You should use this widget type with a default value,
3028 as in (other DEFAULT) or (other :tag \"NAME\" DEFAULT).
3029 If the user selects this alternative, that specifies DEFAULT
3035 (defvar widget-string-prompt-value-history nil
3036 "History of input to `widget-string-prompt-value'.")
3038 (define-widget 'string 'editable-field
3041 :format "%{%t%}: %v"
3042 :complete-function 'ispell-complete-word
3043 :prompt-history 'widget-string-prompt-value-history)
3045 (define-widget 'regexp 'string
3046 "A regular expression."
3047 :match 'widget-regexp-match
3048 :validate 'widget-regexp-validate
3049 ;; Doesn't work well with terminating newline.
3050 ;; :value-face 'widget-single-line-field
3053 (defun widget-regexp-match (_widget value)
3054 ;; Match valid regexps.
3055 (and (stringp value)
3058 (string-match value ""))
3061 (defun widget-regexp-validate (widget)
3062 "Check that the value of WIDGET is a valid regexp."
3063 (condition-case data
3065 (string-match (widget-value widget) ""))
3066 (error (widget-put widget :error (error-message-string data))
3069 (define-widget 'file 'string
3071 It reads a file name from an editable text field."
3072 :completions #'completion-file-name-table
3073 :prompt-value 'widget-file-prompt-value
3074 :format "%{%t%}: %v"
3075 ;; Doesn't work well with terminating newline.
3076 ;; :value-face 'widget-single-line-field
3079 (defun widget-file-prompt-value (widget prompt value unbound)
3080 ;; Read file from minibuffer.
3081 (abbreviate-file-name
3083 (read-file-name prompt)
3084 (let ((prompt2 (format "%s (default %s): " prompt value))
3085 (dir (file-name-directory value))
3086 (file (file-name-nondirectory value))
3087 (must-match (widget-get widget :must-match)))
3088 (read-file-name prompt2 dir nil must-match file)))))
3090 ;;;(defun widget-file-action (widget &optional event)
3091 ;;; ;; Read a file name from the minibuffer.
3092 ;;; (let* ((value (widget-value widget))
3093 ;;; (dir (file-name-directory value))
3094 ;;; (file (file-name-nondirectory value))
3095 ;;; (menu-tag (widget-apply widget :menu-tag-get))
3096 ;;; (must-match (widget-get widget :must-match))
3097 ;;; (answer (read-file-name (concat menu-tag " (default " value "): ")
3098 ;;; dir nil must-match file)))
3099 ;;; (widget-value-set widget (abbreviate-file-name answer))
3101 ;;; (widget-apply widget :notify widget event)))
3103 ;; Fixme: use file-name-as-directory.
3104 (define-widget 'directory 'file
3105 "A directory widget.
3106 It reads a directory name from an editable text field."
3109 (defvar widget-symbol-prompt-value-history nil
3110 "History of input to `widget-symbol-prompt-value'.")
3112 (define-widget 'symbol 'editable-field
3116 :format "%{%t%}: %v"
3117 :match (lambda (_widget value) (symbolp value))
3118 :completions obarray
3119 :prompt-internal 'widget-symbol-prompt-internal
3120 :prompt-match 'symbolp
3121 :prompt-history 'widget-symbol-prompt-value-history
3122 :value-to-internal (lambda (_widget value)
3126 :value-to-external (lambda (_widget value)
3131 (defun widget-symbol-prompt-internal (widget prompt initial history)
3132 ;; Read file from minibuffer.
3133 (let ((answer (completing-read prompt obarray
3134 (widget-get widget :prompt-match)
3135 nil initial history)))
3136 (if (and (stringp answer)
3137 (not (zerop (length answer))))
3139 (error "No value"))))
3141 (defvar widget-function-prompt-value-history nil
3142 "History of input to `widget-function-prompt-value'.")
3144 (define-widget 'function 'restricted-sexp
3146 :completions (apply-partially #'completion-table-with-predicate
3147 obarray #'fboundp 'strict)
3148 :prompt-value 'widget-field-prompt-value
3149 :prompt-internal 'widget-symbol-prompt-internal
3150 :prompt-match 'fboundp
3151 :prompt-history 'widget-function-prompt-value-history
3152 :action 'widget-field-action
3153 :match-alternatives '(functionp)
3154 :validate (lambda (widget)
3155 (unless (functionp (widget-value widget))
3156 (widget-put widget :error (format "Invalid function: %S"
3157 (widget-value widget)))
3162 (defvar widget-variable-prompt-value-history nil
3163 "History of input to `widget-variable-prompt-value'.")
3165 (define-widget 'variable 'symbol
3167 :prompt-match 'boundp
3168 :prompt-history 'widget-variable-prompt-value-history
3169 :completions (apply-partially #'completion-table-with-predicate
3170 obarray #'boundp 'strict)
3173 (define-widget 'coding-system 'symbol
3174 "A MULE coding-system."
3175 :format "%{%t%}: %v"
3176 :tag "Coding system"
3178 :prompt-history 'coding-system-value-history
3179 :prompt-value 'widget-coding-system-prompt-value
3180 :action 'widget-coding-system-action
3181 :completions (apply-partially #'completion-table-with-predicate
3182 obarray #'coding-system-p 'strict)
3183 :validate (lambda (widget)
3184 (unless (coding-system-p (widget-value widget))
3185 (widget-put widget :error (format "Invalid coding system: %S"
3186 (widget-value widget)))
3189 :prompt-match 'coding-system-p)
3191 (defun widget-coding-system-prompt-value (widget prompt value _unbound)
3192 "Read coding-system from minibuffer."
3193 (if (widget-get widget :base-only)
3195 (completing-read (format "%s (default %s): " prompt value)
3196 (mapcar #'list (coding-system-list t)) nil nil nil
3197 coding-system-history))
3198 (read-coding-system (format "%s (default %s): " prompt value) value)))
3200 (defun widget-coding-system-action (widget &optional event)
3202 (widget-coding-system-prompt-value
3204 (widget-apply widget :menu-tag-get)
3205 (widget-value widget)
3207 (widget-value-set widget answer)
3208 (widget-apply widget :notify widget event)
3211 ;;; I'm not sure about what this is good for? KFS.
3212 (defvar widget-key-sequence-prompt-value-history nil
3213 "History of input to `widget-key-sequence-prompt-value'.")
3215 (defvar widget-key-sequence-default-value [ignore]
3216 "Default value for an empty key sequence.")
3218 (defvar widget-key-sequence-map
3219 (let ((map (make-sparse-keymap)))
3220 (set-keymap-parent map widget-field-keymap)
3221 (define-key map [(control ?q)] 'widget-key-sequence-read-event)
3224 (define-widget 'key-sequence 'restricted-sexp
3226 :prompt-value 'widget-field-prompt-value
3227 :prompt-internal 'widget-symbol-prompt-internal
3228 ; :prompt-match 'fboundp ;; What was this good for? KFS
3229 :prompt-history 'widget-key-sequence-prompt-value-history
3230 :action 'widget-field-action
3231 :match-alternatives '(stringp vectorp)
3232 :format "%{%t%}: %v"
3233 :validate 'widget-key-sequence-validate
3234 :value-to-internal 'widget-key-sequence-value-to-internal
3235 :value-to-external 'widget-key-sequence-value-to-external
3236 :value widget-key-sequence-default-value
3237 :keymap widget-key-sequence-map
3238 :help-echo "C-q: insert KEY, EVENT, or CODE; RET: enter value"
3239 :tag "Key sequence")
3241 (defun widget-key-sequence-read-event (ev)
3243 (let ((inhibit-quit t) quit-flag)
3244 (read-event "Insert KEY, EVENT, or CODE: "))))
3245 (let ((ev2 (and (memq 'down (event-modifiers ev))
3247 (tr (and (keymapp function-key-map)
3248 (lookup-key function-key-map (vector ev)))))
3249 (when (and (integerp ev)
3250 (or (and (<= ?0 ev) (< ev (+ ?0 (min 10 read-quoted-char-radix))))
3251 (and (<= ?a (downcase ev))
3252 (< (downcase ev) (+ ?a -10 (min 36 read-quoted-char-radix))))))
3253 (setq unread-command-events (cons ev unread-command-events)
3254 ev (read-quoted-char (format "Enter code (radix %d)" read-quoted-char-radix))
3256 (if (and (integerp ev) (not (characterp ev)))
3257 (insert (char-to-string ev)))) ;; throw invalid char error
3258 (setq ev (key-description (list ev)))
3260 (setq tr (key-description (list (aref tr 0))))
3261 (if (y-or-n-p (format "Key %s is translated to %s -- use %s? " ev tr tr))
3262 (setq ev tr ev2 nil)))
3263 (insert (if (= (char-before) ?\s) "" " ") ev " ")
3265 (insert (key-description (list ev2)) " "))))
3267 (defun widget-key-sequence-validate (widget)
3268 (unless (or (stringp (widget-value widget))
3269 (vectorp (widget-value widget)))
3270 (widget-put widget :error (format "Invalid key sequence: %S"
3271 (widget-value widget)))
3274 (defun widget-key-sequence-value-to-internal (widget value)
3275 (if (widget-apply widget :match value)
3276 (if (equal value widget-key-sequence-default-value)
3278 (key-description value))
3281 (defun widget-key-sequence-value-to-external (_widget value)
3283 (if (string-match "\\`[[:space:]]*\\'" value)
3284 widget-key-sequence-default-value
3285 (read-kbd-macro value))
3289 (define-widget 'sexp 'editable-field
3290 "An arbitrary Lisp expression."
3291 :tag "Lisp expression"
3292 :format "%{%t%}: %v"
3294 :validate 'widget-sexp-validate
3295 :match (lambda (_widget _value) t)
3296 :value-to-internal 'widget-sexp-value-to-internal
3297 :value-to-external (lambda (_widget value) (read value))
3298 :prompt-history 'widget-sexp-prompt-value-history
3299 :prompt-value 'widget-sexp-prompt-value)
3301 (defun widget-sexp-value-to-internal (_widget value)
3302 ;; Use pp for printer representation.
3303 (let ((pp (if (symbolp value)
3304 (prin1-to-string value)
3305 (pp-to-string value))))
3306 (while (string-match "\n\\'" pp)
3307 (setq pp (substring pp 0 -1)))
3308 (if (or (string-match "\n\\'" pp)
3313 (defun widget-sexp-validate (widget)
3314 ;; Valid if we can read the string and there is no junk left after it.
3316 (insert (widget-apply widget :value-get))
3317 (goto-char (point-min))
3319 (condition-case data ;Note: We get a spurious byte-compile warning here.
3321 ;; Avoid a confusing end-of-file error.
3322 (skip-syntax-forward "\\s-")
3324 (setq err "Empty sexp -- use nil?")
3325 (unless (widget-apply widget :match (read (current-buffer)))
3326 (setq err (widget-get widget :type-error))))
3327 ;; Allow whitespace after expression.
3328 (skip-syntax-forward "\\s-")
3329 (if (and (not (eobp))
3331 (setq err (format "Junk at end of expression: %s"
3332 (buffer-substring (point)
3334 (end-of-file ; Avoid confusing error message.
3335 (setq err "Unbalanced sexp"))
3336 (error (setq err (error-message-string data))))
3339 (widget-put widget :error err)
3342 (defvar widget-sexp-prompt-value-history nil
3343 "History of input to `widget-sexp-prompt-value'.")
3345 (defun widget-sexp-prompt-value (widget prompt value unbound)
3346 ;; Read an arbitrary sexp.
3347 (let ((found (read-string prompt
3348 (if unbound nil (cons (prin1-to-string value) 0))
3349 (widget-get widget :prompt-history))))
3350 (let ((answer (read-from-string found)))
3351 (unless (= (cdr answer) (length found))
3352 (error "Junk at end of expression: %s"
3353 (substring found (cdr answer))))
3356 (define-widget 'restricted-sexp 'sexp
3357 "A Lisp expression restricted to values that match.
3358 To use this type, you must define :match or :match-alternatives."
3359 :type-error "The specified value is not valid"
3360 :match 'widget-restricted-sexp-match
3361 :value-to-internal (lambda (widget value)
3362 (if (widget-apply widget :match value)
3363 (prin1-to-string value)
3366 (defun widget-restricted-sexp-match (widget value)
3367 (let ((alternatives (widget-get widget :match-alternatives))
3369 (while (and alternatives (not matched))
3370 (if (cond ((functionp (car alternatives))
3371 (funcall (car alternatives) value))
3372 ((and (consp (car alternatives))
3373 (eq (car (car alternatives)) 'quote))
3374 (eq value (nth 1 (car alternatives)))))
3376 (setq alternatives (cdr alternatives)))
3379 (define-widget 'integer 'restricted-sexp
3383 :type-error "This field should contain an integer"
3384 :match-alternatives '(integerp))
3386 (define-widget 'number 'restricted-sexp
3387 "A number (floating point or integer)."
3390 :type-error "This field should contain a number (floating point or integer)"
3391 :match-alternatives '(numberp))
3393 (define-widget 'float 'restricted-sexp
3394 "A floating point number."
3395 :tag "Floating point number"
3397 :type-error "This field should contain a floating point number"
3398 :match-alternatives '(floatp))
3400 (define-widget 'character 'editable-field
3405 :format "%{%t%}: %v\n"
3406 :valid-regexp "\\`.\\'"
3407 :error "This field should contain a single character"
3408 :value-get (lambda (w) (widget-field-value-get w t))
3409 :value-to-internal (lambda (_widget value)
3412 (char-to-string value)))
3413 :value-to-external (lambda (_widget value)
3417 :match (lambda (_widget value)
3418 (characterp value)))
3420 (define-widget 'list 'group
3423 :format "%{%t%}:\n%v")
3425 (define-widget 'vector 'group
3428 :format "%{%t%}:\n%v"
3429 :match 'widget-vector-match
3430 :value-to-internal (lambda (_widget value) (append value nil))
3431 :value-to-external (lambda (_widget value) (apply 'vector value)))
3433 (defun widget-vector-match (widget value)
3434 (and (vectorp value)
3435 (widget-group-match widget
3436 (widget-apply widget :value-to-internal value))))
3438 (define-widget 'cons 'group
3441 :format "%{%t%}:\n%v"
3442 :match 'widget-cons-match
3443 :value-to-internal (lambda (_widget value)
3444 (list (car value) (cdr value)))
3445 :value-to-external (lambda (_widget value)
3446 (apply 'cons value)))
3448 (defun widget-cons-match (widget value)
3450 (widget-group-match widget
3451 (widget-apply widget :value-to-internal value))))
3453 ;;; The `lazy' Widget.
3455 ;; Recursive datatypes.
3457 (define-widget 'lazy 'default
3458 "Base widget for recursive data structures.
3460 The `lazy' widget will, when instantiated, contain a single inferior
3461 widget, of the widget type specified by the :type parameter. The
3462 value of the `lazy' widget is the same as the value of the inferior
3463 widget. When deriving a new widget from the 'lazy' widget, the :type
3464 parameter is allowed to refer to the widget currently being defined,
3465 thus allowing recursive data structures to be described.
3467 The :type parameter takes the same arguments as the defcustom
3468 parameter with the same name.
3470 Most composite widgets, i.e. widgets containing other widgets, does
3471 not allow recursion. That is, when you define a new widget type, none
3472 of the inferior widgets may be of the same type you are currently
3475 In Lisp, however, it is custom to define data structures in terms of
3476 themselves. A list, for example, is defined as either nil, or a cons
3477 cell whose cdr itself is a list. The obvious way to translate this
3478 into a widget type would be
3480 (define-widget \\='my-list \\='choice
3481 \"A list of sexps.\"
3483 :args \\='((const nil) (cons :value (nil) sexp my-list)))
3485 Here we attempt to define my-list as a choice of either the constant
3486 nil, or a cons-cell containing a sexp and my-lisp. This will not work
3487 because the `choice' widget does not allow recursion.
3489 Using the `lazy' widget you can overcome this problem, as in this
3492 (define-widget \\='sexp-list \\='lazy
3493 \"A list of sexps.\"
3495 :type \\='(choice (const nil) (cons :value (nil) sexp sexp-list)))"
3496 :format "%{%t%}: %v"
3497 ;; We don't convert :type because we want to allow recursive
3498 ;; data structures. This is slow, so we should not create speed
3499 ;; critical widgets by deriving from this.
3500 :convert-widget 'widget-value-convert-widget
3501 :value-create 'widget-type-value-create
3502 :value-get 'widget-child-value-get
3503 :value-inline 'widget-child-value-inline
3504 :default-get 'widget-type-default-get
3505 :match 'widget-type-match
3506 :validate 'widget-child-validate)
3509 ;;; The `plist' Widget.
3513 (define-widget 'plist 'list
3515 :key-type '(symbol :tag "Key")
3516 :value-type '(sexp :tag "Value")
3517 :convert-widget 'widget-plist-convert-widget
3520 (defvar widget-plist-value-type) ;Dynamic variable
3522 (defun widget-plist-convert-widget (widget)
3523 ;; Handle `:options'.
3524 (let* ((options (widget-get widget :options))
3525 (widget-plist-value-type (widget-get widget :value-type))
3526 (other `(editable-list :inline t
3528 ,(widget-get widget :key-type)
3529 ,widget-plist-value-type)))
3531 (list `(checklist :inline t
3533 ,@(mapcar 'widget-plist-convert-option
3537 (widget-put widget :args args)
3540 (defun widget-plist-convert-option (option)
3541 ;; Convert a single plist option.
3542 (let (key-type value-type)
3544 (let ((key (nth 0 option)))
3545 (setq value-type (nth 1 option))
3548 (setq key-type `(const ,key))))
3549 (setq key-type `(const ,option)
3550 value-type widget-plist-value-type))
3551 `(group :format "Key: %v" :inline t ,key-type ,value-type)))
3554 ;;; The `alist' Widget.
3556 ;; Association lists.
3558 (define-widget 'alist 'list
3559 "An association list."
3560 :key-type '(sexp :tag "Key")
3561 :value-type '(sexp :tag "Value")
3562 :convert-widget 'widget-alist-convert-widget
3565 (defvar widget-alist-value-type) ;Dynamic variable
3567 (defun widget-alist-convert-widget (widget)
3568 ;; Handle `:options'.
3569 (let* ((options (widget-get widget :options))
3570 (widget-alist-value-type (widget-get widget :value-type))
3571 (other `(editable-list :inline t
3573 ,(widget-get widget :key-type)
3574 ,widget-alist-value-type)))
3576 (list `(checklist :inline t
3578 ,@(mapcar 'widget-alist-convert-option
3582 (widget-put widget :args args)
3585 (defun widget-alist-convert-option (option)
3586 ;; Convert a single alist option.
3587 (let (key-type value-type)
3589 (let ((key (nth 0 option)))
3590 (setq value-type (nth 1 option))
3593 (setq key-type `(const ,key))))
3594 (setq key-type `(const ,option)
3595 value-type widget-alist-value-type))
3596 `(cons :format "Key: %v" ,key-type ,value-type)))
3598 (define-widget 'choice 'menu-choice
3599 "A union of several sexp types."
3601 :format "%{%t%}: %[Value Menu%] %v"
3602 :button-prefix 'widget-push-button-prefix
3603 :button-suffix 'widget-push-button-suffix
3604 :prompt-value 'widget-choice-prompt-value)
3606 (defun widget-choice-prompt-value (widget prompt value _unbound)
3608 (let ((args (widget-get widget :args))
3609 (completion-ignore-case (widget-get widget :case-fold))
3610 current choices old)
3611 ;; Find the first arg that matches VALUE.
3614 (if (widget-apply (car look) :match value)
3615 (setq old (car look)
3617 (setq look (cdr look)))))
3620 (cond ((= (length args) 0)
3622 ((= (length args) 1)
3624 ((and (= (length args) 2)
3626 (if (eq old (nth 0 args))
3631 (setq current (car args)
3634 (cons (cons (widget-apply current :menu-tag-get)
3637 (let ((val (completing-read prompt choices nil t)))
3639 (let ((try (try-completion val choices)))
3642 (cdr (assoc val choices)))
3645 (widget-prompt-value current prompt nil t)
3648 (define-widget 'radio 'radio-button-choice
3649 "A union of several sexp types."
3651 :format "%{%t%}:\n%v"
3652 :prompt-value 'widget-choice-prompt-value)
3654 (define-widget 'repeat 'editable-list
3655 "A variable length homogeneous list."
3657 :format "%{%t%}:\n%v%i\n")
3659 (define-widget 'set 'checklist
3660 "A list of members from a fixed set."
3662 :format "%{%t%}:\n%v")
3664 (define-widget 'boolean 'toggle
3665 "To be nil or non-nil, that is the question."
3667 :prompt-value 'widget-boolean-prompt-value
3668 :button-prefix 'widget-push-button-prefix
3669 :button-suffix 'widget-push-button-suffix
3670 :format "%{%t%}: %[Toggle%] %v\n"
3674 (defun widget-boolean-prompt-value (_widget prompt _value _unbound)
3675 ;; Toggle a boolean.
3678 ;;; The `color' Widget.
3681 (define-widget 'color 'editable-field
3682 "Choose a color name (with sample)."
3683 :format "%{%t%}: %v (%{sample%})\n"
3684 :value-create 'widget-color-value-create
3688 :completions (or facemenu-color-alist (defined-colors))
3689 :sample-face-get 'widget-color-sample-face-get
3690 :notify 'widget-color-notify
3691 :action 'widget-color-action)
3693 (defun widget-color-value-create (widget)
3694 (widget-field-value-create widget)
3696 (widget-create-child-and-convert
3698 :tag " Choose " :action 'widget-color--choose-action)
3699 (widget-insert " "))
3701 (defun widget-color--choose-action (widget &optional _event)
3702 (list-colors-display
3705 (when (buffer-live-p ,(current-buffer))
3706 (widget-value-set ',(widget-get widget :parent) color)
3707 (let* ((buf (get-buffer "*Colors*"))
3708 (win (get-buffer-window buf 0)))
3710 (quit-window nil win)
3712 (pop-to-buffer ,(current-buffer))))))
3714 (defun widget-color-sample-face-get (widget)
3715 (let* ((value (condition-case nil
3716 (widget-value widget)
3717 (error (widget-get widget :value)))))
3718 (if (color-defined-p value)
3719 (list (cons 'foreground-color value))
3722 (defun widget-color-action (widget &optional event)
3723 "Prompt for a color."
3724 (let* ((tag (widget-apply widget :menu-tag-get))
3725 (prompt (concat tag ": "))
3726 (answer (facemenu-read-color prompt)))
3727 (unless (zerop (length answer))
3728 (widget-value-set widget answer)
3730 (widget-apply widget :notify widget event))))
3732 (defun widget-color-notify (widget child &optional event)
3733 "Update the sample, and notify the parent."
3734 (overlay-put (widget-get widget :sample-overlay)
3735 'face (widget-apply widget :sample-face-get))
3736 (widget-default-notify widget child event))
3740 (defun widget-echo-help (pos)
3741 "Display help-echo text for widget at POS."
3742 (let* ((widget (widget-at pos))
3743 (help-echo (and widget (widget-get widget :help-echo))))
3744 (if (functionp help-echo)
3745 (setq help-echo (funcall help-echo widget)))
3746 (if help-echo (message "%s" (eval help-echo)))))
3752 ;;; wid-edit.el ends here