1 ;;; wid-edit.el --- Functions for creating and using widgets -*-byte-compile-dynamic: t; lexical-binding:t -*-
3 ;; Copyright (C) 1996-1997, 1999-2012 Free Software Foundation, Inc.
5 ;; Author: Per Abrahamsen <abraham@dina.kvl.dk>
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.]
61 (defun widget-event-point (event)
62 "Character position of the end of event if that exists, or nil."
63 (posn-point (event-end event
)))
65 (defun widget-button-release-event-p (event)
66 "Non-nil if EVENT is a mouse-button-release event object."
68 (memq (event-basic-type event
) '(mouse-1 mouse-2 mouse-3
))
69 (or (memq 'click
(event-modifiers event
))
70 (memq 'drag
(event-modifiers event
)))))
75 "Customization support for the Widget Library."
76 :link
'(custom-manual "(widget)Top")
77 :link
'(emacs-library-link :tag
"Lisp File" "widget.el")
81 (defgroup widget-documentation nil
82 "Options controlling the display of documentation strings."
85 (defgroup widget-faces nil
86 "Faces used by the widget library."
90 (defvar widget-documentation-face
'widget-documentation
91 "Face used for documentation strings in widgets.
92 This exists as a variable so it can be set locally in certain buffers.")
94 (defface widget-documentation
'((((class color
)
96 (:foreground
"lime green"))
99 (:foreground
"dark green"))
101 "Face used for documentation text."
102 :group
'widget-documentation
103 :group
'widget-faces
)
104 (define-obsolete-face-alias 'widget-documentation-face
105 'widget-documentation
"22.1")
107 (defvar widget-button-face
'widget-button
108 "Face used for buttons in widgets.
109 This exists as a variable so it can be set locally in certain buffers.")
111 (defface widget-button
'((t (:weight bold
)))
112 "Face used for widget buttons."
113 :group
'widget-faces
)
114 (define-obsolete-face-alias 'widget-button-face
'widget-button
"22.1")
116 (defcustom widget-mouse-face
'highlight
117 "Face used for widget buttons when the mouse is above them."
119 :group
'widget-faces
)
121 ;; TTY gets special definitions here and in the next defface, because
122 ;; the gray colors defined for other displays cause black text on a black
123 ;; background, at least on light-background TTYs.
124 (defface widget-field
'((((type tty
))
125 :background
"yellow3"
127 (((class grayscale color
)
129 :background
"gray85")
130 (((class grayscale color
)
132 :background
"dim gray")
135 "Face used for editable fields."
136 :group
'widget-faces
)
137 (define-obsolete-face-alias 'widget-field-face
'widget-field
"22.1")
139 (defface widget-single-line-field
'((((type tty
))
142 (((class grayscale color
)
144 :background
"gray85")
145 (((class grayscale color
)
147 :background
"dim gray")
150 "Face used for editable fields spanning only a single line."
151 :group
'widget-faces
)
152 (define-obsolete-face-alias 'widget-single-line-field-face
153 'widget-single-line-field
"22.1")
155 ;;; This causes display-table to be loaded, and not usefully.
156 ;;;(defvar widget-single-line-display-table
157 ;;; (let ((table (make-display-table)))
158 ;;; (aset table 9 "^I")
159 ;;; (aset table 10 "^J")
161 ;;; "Display table used for single-line editable fields.")
163 ;;;(when (fboundp 'set-face-display-table)
164 ;;; (set-face-display-table 'widget-single-line-field-face
165 ;;; widget-single-line-display-table))
167 ;;; Utility functions.
169 ;; These are not really widget specific.
171 (defun widget-princ-to-string (object)
172 "Return string representation of OBJECT, any Lisp object.
173 No quoting characters are used; no delimiters are printed around
174 the contents of strings."
175 (with-output-to-string
178 (defun widget-clear-undo ()
179 "Clear all undo information."
180 (buffer-disable-undo (current-buffer))
181 (buffer-enable-undo))
183 (defcustom widget-menu-max-size
40
184 "Largest number of items allowed in a popup-menu.
185 Larger menus are read through the minibuffer."
189 (defcustom widget-menu-max-shortcuts
40
190 "Largest number of items for which it works to choose one with a character.
191 For a larger number of items, the minibuffer is used."
195 (defcustom widget-menu-minibuffer-flag nil
196 "Control how to ask for a choice from the keyboard.
197 Non-nil means use the minibuffer;
198 nil means read a single character."
202 (defun widget-choose (title items
&optional event
)
203 "Choose an item from a list.
205 First argument TITLE is the name of the list.
206 Second argument ITEMS is a list whose members are either
207 (NAME . VALUE), to indicate selectable items, or just strings to
208 indicate unselectable items.
209 Optional third argument EVENT is an input event.
211 The user is asked to choose between each NAME from the items alist,
212 and the VALUE of the chosen element will be returned. If EVENT is a
213 mouse event, and the number of elements in items is less than
214 `widget-menu-max-size', a popup menu will be used, otherwise the
216 (cond ((and (< (length items
) widget-menu-max-size
)
217 event
(display-popup-menus-p))
220 (list title
(cons "" items
))))
221 ((or widget-menu-minibuffer-flag
222 (> (length items
) widget-menu-max-shortcuts
))
223 ;; Read the choice of name from the minibuffer.
224 (setq items
(widget-remove-if 'stringp items
))
225 (let ((val (completing-read (concat title
": ") items nil t
)))
227 (let ((try (try-completion val items
)))
230 (cdr (assoc val items
))))))
232 ;; Construct a menu of the choices
233 ;; and then use it for prompting for a single character.
234 (let* ((overriding-terminal-local-map (make-sparse-keymap))
236 map choice some-choice-enabled value
)
237 ;; Define SPC as a prefix char to get to this menu.
238 (define-key overriding-terminal-local-map
" "
239 (setq map
(make-sparse-keymap title
)))
240 (with-current-buffer (get-buffer-create " widget-choose")
242 (insert "Available choices:\n\n")
244 (setq choice
(car items
) items
(cdr items
))
246 (let* ((name (car choice
))
247 (function (cdr choice
)))
248 (insert (format "%c = %s\n" next-digit name
))
249 (define-key map
(vector next-digit
) function
)
250 (setq some-choice-enabled t
)))
251 ;; Allocate digits to disabled alternatives
252 ;; so that the digit of a given alternative never varies.
253 (setq next-digit
(1+ next-digit
)))
254 (insert "\nC-g = Quit")
255 (goto-char (point-min))
257 (or some-choice-enabled
258 (error "None of the choices is currently meaningful"))
259 (define-key map
[?\C-g
] 'keyboard-quit
)
260 (define-key map
[t] 'keyboard-quit)
261 (define-key map [?\M-\C-v] 'scroll-other-window)
262 (define-key map [?\M--] 'negative-argument)
263 (setcdr map (nreverse (cdr map)))
264 ;; Read a char with the menu, and return the result
265 ;; that corresponds to it.
266 (save-window-excursion
267 (let ((buf (get-buffer " widget-choose")))
268 (fit-window-to-buffer (display-buffer buf))
269 (let ((cursor-in-echo-area t)
273 (while (not (or (and (integerp char)
274 (>= char ?0) (< char next-digit))
275 (eq value 'keyboard-quit)))
276 ;; Unread a SPC to lead to our new menu.
277 (setq unread-command-events (cons ?\s unread-command-events))
278 (setq keys (read-key-sequence title))
280 (lookup-key overriding-terminal-local-map keys t)
282 (cond ((eq value 'scroll-other-window)
283 (let ((minibuffer-scroll-window
284 (get-buffer-window buf)))
286 (scroll-other-window-down
287 (window-height minibuffer-scroll-window))
288 (scroll-other-window))
290 ((eq value 'negative-argument)
294 (when (eq value 'keyboard-quit)
298 (defun widget-remove-if (predicate list)
299 (let (result (tail list))
301 (or (funcall predicate (car tail))
302 (setq result (cons (car tail) result)))
303 (setq tail (cdr tail)))
306 ;;; Widget text specifications.
308 ;; These functions are for specifying text properties.
310 ;; We can set it to nil now that get_local_map uses get_pos_property.
311 (defconst widget-field-add-space nil
312 "Non-nil means add extra space at the end of editable text fields.
313 If you don't add the space, it will become impossible to edit a zero
316 (defvar widget-field-use-before-change t
317 "Non-nil means use `before-change-functions' to track editable fields.
318 This enables the use of undo. Using before hooks also means that
319 the :notify function can't know the new value.")
321 (defun widget-specify-field (widget from to)
322 "Specify editable button for WIDGET between FROM and TO."
323 ;; Terminating space is not part of the field, but necessary in
324 ;; order for local-map to work. Remove next sexp if local-map works
325 ;; at the end of the overlay.
328 (cond ((null (widget-get widget :size))
330 (widget-field-add-space
331 (insert-and-inherit " ")))
333 (let ((keymap (widget-get widget :keymap))
334 (face (or (widget-get widget :value-face) 'widget-field))
335 (help-echo (widget-get widget :help-echo))
336 (follow-link (widget-get widget :follow-link))
338 (or (not widget-field-add-space) (widget-get widget :size))))
339 (if (functionp help-echo)
340 (setq help-echo 'widget-mouse-help))
341 (when (= (char-before to) ?\n)
342 ;; When the last character in the field is a newline, we want to
343 ;; give it a `field' char-property of `boundary', which helps the
344 ;; C-n/C-p act more naturally when entering/leaving the field. We
345 ;; do this by making a small secondary overlay to contain just that
347 (let ((overlay (make-overlay (1- to) to nil t nil)))
348 (overlay-put overlay 'field 'boundary)
349 ;; We need the real field for tabbing.
350 (overlay-put overlay 'real-field widget)
351 ;; Use `local-map' here, not `keymap', so that normal editing
352 ;; works in the field when, say, Custom uses `suppress-keymap'.
353 (overlay-put overlay 'local-map keymap)
354 (overlay-put overlay 'face face)
355 (overlay-put overlay 'follow-link follow-link)
356 (overlay-put overlay 'help-echo help-echo))
358 (setq rear-sticky t))
359 (let ((overlay (make-overlay from to nil nil rear-sticky)))
360 (widget-put widget :field-overlay overlay)
361 ;;(overlay-put overlay 'detachable nil)
362 (overlay-put overlay 'field widget)
363 (overlay-put overlay 'local-map keymap)
364 (overlay-put overlay 'face face)
365 (overlay-put overlay 'follow-link follow-link)
366 (overlay-put overlay 'help-echo help-echo)))
367 (widget-specify-secret widget))
369 (defun widget-specify-secret (field)
370 "Replace text in FIELD with value of `:secret', if non-nil."
371 (let ((secret (widget-get field :secret))
372 (size (widget-get field :size)))
374 (let ((begin (widget-field-start field))
375 (end (widget-field-end field)))
377 (while (and (> end begin)
378 (eq (char-after (1- end)) ?\s))
379 (setq end (1- end))))
381 (let ((old (char-after begin)))
382 (unless (eq old secret)
383 (subst-char-in-region begin (1+ begin) old secret)
384 (put-text-property begin (1+ begin) 'secret old))
385 (setq begin (1+ begin))))))))
387 (defun widget-specify-button (widget from to)
388 "Specify button for WIDGET between FROM and TO."
389 (let ((overlay (make-overlay from to nil t nil))
390 (follow-link (widget-get widget :follow-link))
391 (help-echo (widget-get widget :help-echo)))
392 (widget-put widget :button-overlay overlay)
393 (if (functionp help-echo)
394 (setq help-echo 'widget-mouse-help))
395 (overlay-put overlay 'button widget)
396 (overlay-put overlay 'keymap (widget-get widget :keymap))
397 (overlay-put overlay 'evaporate t)
398 ;; We want to avoid the face with image buttons.
399 (unless (widget-get widget :suppress-face)
400 (overlay-put overlay 'face (widget-apply widget :button-face-get))
401 (overlay-put overlay 'mouse-face
402 ;; Make new list structure for the mouse-face value
403 ;; so that different widgets will have
404 ;; different `mouse-face' property values
405 ;; and will highlight separately.
406 (let ((mouse-face-value
407 (widget-apply widget :mouse-face-get)))
408 ;; If it's a list, copy it.
409 (if (listp mouse-face-value)
410 (copy-sequence mouse-face-value)
411 ;; If it's a symbol, put it in a list.
412 (list mouse-face-value)))))
413 (overlay-put overlay 'pointer 'hand)
414 (overlay-put overlay 'follow-link follow-link)
415 (overlay-put overlay 'help-echo help-echo)))
417 (defun widget-mouse-help (_window overlay _point)
418 "Help-echo callback for widgets whose :help-echo is a function."
419 (with-current-buffer (overlay-buffer overlay)
420 (let* ((widget (widget-at (overlay-start overlay)))
421 (help-echo (if widget (widget-get widget :help-echo))))
422 (if (functionp help-echo)
423 (funcall help-echo widget)
426 (defun widget-specify-sample (widget from to)
427 "Specify sample for WIDGET between FROM and TO."
428 (let ((overlay (make-overlay from to nil t nil)))
429 (overlay-put overlay 'face (widget-apply widget :sample-face-get))
430 (overlay-put overlay 'evaporate t)
431 (widget-put widget :sample-overlay overlay)))
433 (defun widget-specify-doc (widget from to)
434 "Specify documentation for WIDGET between FROM and TO."
435 (let ((overlay (make-overlay from to nil t nil)))
436 (overlay-put overlay 'widget-doc widget)
437 (overlay-put overlay 'face widget-documentation-face)
438 (overlay-put overlay 'evaporate t)
439 (widget-put widget :doc-overlay overlay)))
441 (defmacro widget-specify-insert (&rest form)
442 "Execute FORM without inheriting any text properties."
444 (let ((inhibit-read-only t)
445 (inhibit-modification-hooks t))
446 (narrow-to-region (point) (point))
447 (prog1 (progn ,@form)
448 (goto-char (point-max))))))
450 (defface widget-inactive
451 '((t :inherit shadow))
452 "Face used for inactive widgets."
453 :group 'widget-faces)
454 (define-obsolete-face-alias 'widget-inactive-face
455 'widget-inactive "22.1")
457 (defun widget-specify-inactive (widget from to)
458 "Make WIDGET inactive for user modifications."
459 (unless (widget-get widget :inactive)
460 (let ((overlay (make-overlay from to nil t nil)))
461 (overlay-put overlay 'face 'widget-inactive)
462 ;; This is disabled, as it makes the mouse cursor change shape.
463 ;; (overlay-put overlay 'mouse-face 'widget-inactive)
464 (overlay-put overlay 'evaporate t)
465 (overlay-put overlay 'priority 100)
466 (overlay-put overlay 'modification-hooks '(widget-overlay-inactive))
467 (widget-put widget :inactive overlay))))
469 (defun widget-overlay-inactive (&rest _junk)
470 "Ignoring the arguments, signal an error."
471 (unless inhibit-read-only
472 (error "The widget here is not active")))
475 (defun widget-specify-active (widget)
476 "Make WIDGET active for user modifications."
477 (let ((inactive (widget-get widget :inactive)))
479 (delete-overlay inactive)
480 (widget-put widget :inactive nil))))
482 ;;; Widget Properties.
484 (defsubst widget-type (widget)
485 "Return the type of WIDGET. The type is a symbol."
489 (defun widgetp (widget)
490 "Return non-nil if WIDGET is a widget."
492 (get widget 'widget-type)
494 (symbolp (car widget))
495 (get (car widget) 'widget-type))))
497 (defun widget-get-indirect (widget property)
498 "In WIDGET, get the value of PROPERTY.
499 If the value is a symbol, return its binding.
500 Otherwise, just return the value."
501 (let ((value (widget-get widget property)))
506 (defun widget-member (widget property)
507 "Non-nil if there is a definition in WIDGET for PROPERTY."
508 (cond ((plist-member (cdr widget) property)
511 (widget-member (get (car widget) 'widget-type) property))
514 (defun widget-value (widget)
515 "Extract the current value of WIDGET."
517 :value-to-external (widget-apply widget :value-get)))
519 (defun widget-value-set (widget value)
520 "Set the current value of WIDGET to VALUE."
522 :value-set (widget-apply widget
523 :value-to-internal value)))
525 (defun widget-default-get (widget)
526 "Extract the default external value of WIDGET."
527 (widget-apply widget :value-to-external
528 (or (widget-get widget :value)
529 (widget-apply widget :default-get))))
531 (defun widget-match-inline (widget vals)
532 "In WIDGET, match the start of VALS."
533 (cond ((widget-get widget :inline)
534 (widget-apply widget :match-inline vals))
536 (widget-apply widget :match (car vals)))
537 (cons (list (car vals)) (cdr vals)))
540 (defun widget-apply-action (widget &optional event)
541 "Apply :action in WIDGET in response to EVENT."
542 (if (widget-apply widget :active)
543 (widget-apply widget :action event)
544 (error "Attempt to perform action on inactive widget")))
546 ;;; Helper functions.
548 ;; These are widget specific.
551 (defun widget-prompt-value (widget prompt &optional value unbound)
552 "Prompt for a value matching WIDGET, using PROMPT.
553 The current value is assumed to be VALUE, unless UNBOUND is non-nil."
554 (unless (listp widget)
555 (setq widget (list widget)))
556 (setq prompt (format "[%s] %s" (widget-type widget) prompt))
557 (setq widget (widget-convert widget))
558 (let ((answer (widget-apply widget :prompt-value prompt value unbound)))
559 (unless (widget-apply widget :match answer)
560 (error "Value does not match %S type" (car widget)))
563 (defun widget-get-sibling (widget)
564 "Get the item WIDGET is assumed to toggle.
565 This is only meaningful for radio buttons or checkboxes in a list."
566 (let* ((children (widget-get (widget-get widget :parent) :children))
570 (setq child (car children)
571 children (cdr children))
572 (when (eq (widget-get child :button) widget)
573 (throw 'child child)))
576 (defun widget-map-buttons (function &optional buffer maparg)
577 "Map FUNCTION over the buttons in BUFFER.
578 FUNCTION is called with the arguments WIDGET and MAPARG.
580 If FUNCTION returns non-nil, the walk is canceled.
582 The arguments MAPARG, and BUFFER default to nil and (current-buffer),
584 (let ((cur (point-min))
587 (with-current-buffer buffer (overlay-lists))
589 (setq overlays (append (car overlays) (cdr overlays)))
590 (while (setq cur (pop overlays))
591 (setq widget (overlay-get cur 'button))
592 (if (and widget (funcall function widget maparg))
593 (setq overlays nil)))))
597 (defcustom widget-image-directory (file-name-as-directory
598 (expand-file-name "images/custom" data-directory))
599 "Where widget button images are located.
600 If this variable is nil, widget will try to locate the directory
605 (defcustom widget-image-enable t
606 "If non-nil, use image buttons in widgets when available."
611 (defcustom widget-image-conversion
612 '((xpm ".xpm") (gif ".gif") (png ".png") (jpeg ".jpg" ".jpeg")
614 "Conversion alist from image formats to file name suffixes."
616 :type '(repeat (cons :format "%v"
617 (symbol :tag "Image Format" unknown)
618 (repeat :tag "Suffixes"
619 (string :format "%v")))))
621 (defun widget-image-find (image)
622 "Create a graphical button from IMAGE.
623 IMAGE should either already be an image, or be a file name sans
624 extension (xpm, xbm, gif, jpg, or png) located in
625 `widget-image-directory' or otherwise where `find-image' will find it."
626 (cond ((not (and image widget-image-enable (display-graphic-p)))
627 ;; We don't want or can't use images.
630 (eq 'image (car image)))
631 ;; Already an image spec. Use it.
634 ;; A string. Look it up in relevant directories.
635 (let* ((load-path (cons widget-image-directory load-path))
637 (dolist (elt widget-image-conversion)
638 (dolist (ext (cdr elt))
639 (push (list :type (car elt) :file (concat image ext))
641 (find-image (nreverse specs))))
646 (defvar widget-button-pressed-face 'widget-button-pressed
647 "Face used for pressed buttons in widgets.
648 This exists as a variable so it can be set locally in certain
651 (defun widget-image-insert (widget tag image &optional _down _inactive)
652 "In WIDGET, insert the text TAG or, if supported, IMAGE.
653 IMAGE should either be an image or an image file name sans extension
654 \(xpm, xbm, gif, jpg, or png) located in `widget-image-directory'.
656 Optional arguments DOWN and INACTIVE are used instead of IMAGE when the
657 button is pressed or inactive, respectively. These are currently ignored."
658 (if (and (featurep 'image)
659 (setq image (widget-image-find image)))
660 (progn (widget-put widget :suppress-face t)
661 (insert-image image tag))
664 (defun widget-move-and-invoke (event)
665 "Move to where you click, and if it is an active field, invoke it."
667 (mouse-set-point event)
668 (let ((pos (widget-event-point event)))
669 (if (and pos (get-char-property pos 'button))
670 (widget-button-click event))))
674 (defgroup widget-button nil
675 "The look of various kinds of buttons."
678 (defcustom widget-button-prefix ""
679 "String used as prefix for buttons."
681 :group 'widget-button)
683 (defcustom widget-button-suffix ""
684 "String used as suffix for buttons."
686 :group 'widget-button)
688 ;;; Creating Widgets.
691 (defun widget-create (type &rest args)
692 "Create widget of TYPE.
693 The optional ARGS are additional keyword arguments."
694 (let ((widget (apply 'widget-convert type args)))
695 (widget-apply widget :create)
698 (defun widget-create-child-and-convert (parent type &rest args)
699 "As part of the widget PARENT, create a child widget TYPE.
700 The child is converted, using the keyword arguments ARGS."
701 (let ((widget (apply 'widget-convert type args)))
702 (widget-put widget :parent parent)
703 (unless (widget-get widget :indent)
704 (widget-put widget :indent (+ (or (widget-get parent :indent) 0)
705 (or (widget-get widget :extra-offset) 0)
706 (widget-get parent :offset))))
707 (widget-apply widget :create)
710 (defun widget-create-child (parent type)
711 "Create widget of TYPE."
712 (let ((widget (widget-copy type)))
713 (widget-put widget :parent parent)
714 (unless (widget-get widget :indent)
715 (widget-put widget :indent (+ (or (widget-get parent :indent) 0)
716 (or (widget-get widget :extra-offset) 0)
717 (widget-get parent :offset))))
718 (widget-apply widget :create)
721 (defun widget-create-child-value (parent type value)
722 "Create widget of TYPE with value VALUE."
723 (let ((widget (widget-copy type)))
724 (widget-put widget :value (widget-apply widget :value-to-internal value))
725 (widget-put widget :parent parent)
726 (unless (widget-get widget :indent)
727 (widget-put widget :indent (+ (or (widget-get parent :indent) 0)
728 (or (widget-get widget :extra-offset) 0)
729 (widget-get parent :offset))))
730 (widget-apply widget :create)
734 (defun widget-delete (widget)
736 (widget-apply widget :delete))
738 (defun widget-copy (widget)
739 "Make a deep copy of WIDGET."
740 (widget-apply (copy-sequence widget) :copy))
742 (defun widget-convert (type &rest args)
743 "Convert TYPE to a widget without inserting it in the buffer.
744 The optional ARGS are additional keyword arguments."
745 ;; Don't touch the type.
746 (let* ((widget (if (symbolp type)
748 (copy-sequence type)))
752 ;; First set the :args keyword.
753 (while (cdr current) ;Look in the type.
754 (if (and (keywordp (cadr current))
755 ;; If the last element is a keyword,
756 ;; it is still the :args element,
757 ;; even though it is a keyword.
759 (if (eq (cadr current) :args)
760 ;; If :args is explicitly specified, obey it.
762 ;; Some other irrelevant keyword.
763 (setq current (cdr (cdr current))))
764 (setcdr current (list :args (cdr current)))
766 (while (and args (not done)) ;Look in ARGS.
767 (cond ((eq (car args) :args)
768 ;; Handle explicit specification of :args.
769 (setq args (cadr args)
771 ((keywordp (car args))
772 (setq args (cddr args)))
775 (widget-put widget :args args))
776 ;; Then Convert the widget.
779 (let ((convert-widget (plist-get (cdr type) :convert-widget)))
781 (setq widget (funcall convert-widget widget))))
782 (setq type (get (car type) 'widget-type)))
783 ;; Finally set the keyword args.
785 (let ((next (nth 0 keys)))
788 (widget-put widget next (nth 1 keys))
789 (setq keys (nthcdr 2 keys)))
791 ;; Convert the :value to internal format.
792 (if (widget-member widget :value)
794 :value (widget-apply widget
796 (widget-get widget :value))))
797 ;; Return the newly create widget.
801 (defun widget-insert (&rest args)
802 "Call `insert' with ARGS even if surrounding text is read only."
803 (let ((inhibit-read-only t)
804 (inhibit-modification-hooks t))
805 (apply 'insert args)))
807 (defun widget-convert-text (type from to
808 &optional button-from button-to
810 "Return a widget of type TYPE with endpoint FROM TO.
811 No text will be inserted to the buffer, instead the text between FROM
812 and TO will be used as the widgets end points. If optional arguments
813 BUTTON-FROM and BUTTON-TO are given, these will be used as the widgets
815 Optional ARGS are extra keyword arguments for TYPE."
816 (let ((widget (apply 'widget-convert type :delete 'widget-leave-text args))
817 (from (copy-marker from))
818 (to (copy-marker to)))
819 (set-marker-insertion-type from t)
820 (set-marker-insertion-type to nil)
821 (widget-put widget :from from)
822 (widget-put widget :to to)
824 (widget-specify-button widget button-from button-to))
827 (defun widget-convert-button (type from to &rest args)
828 "Return a widget of type TYPE with endpoint FROM TO.
829 Optional ARGS are extra keyword arguments for TYPE.
830 No text will be inserted to the buffer, instead the text between FROM
831 and TO will be used as the widgets end points, as well as the widgets
833 (apply 'widget-convert-text type from to from to args))
835 (defun widget-leave-text (widget)
836 "Remove markers and overlays from WIDGET and its children."
837 (let ((button (widget-get widget :button-overlay))
838 (sample (widget-get widget :sample-overlay))
839 (doc (widget-get widget :doc-overlay))
840 (field (widget-get widget :field-overlay)))
841 (set-marker (widget-get widget :from) nil)
842 (set-marker (widget-get widget :to) nil)
844 (delete-overlay button))
846 (delete-overlay sample))
848 (delete-overlay doc))
850 (delete-overlay field))
851 (mapc 'widget-leave-text (widget-get widget :children))))
853 ;;; Keymap and Commands.
855 ;; This alias exists only so that one can choose in doc-strings (e.g.
856 ;; Custom-mode) which key-binding of widget-keymap one wants to refer to.
857 ;; http://lists.gnu.org/archive/html/emacs-devel/2008-11/msg00480.html
858 (define-obsolete-function-alias 'advertised-widget-backward
859 'widget-backward "23.2")
862 (defvar widget-keymap
863 (let ((map (make-sparse-keymap)))
864 (define-key map "\t" 'widget-forward)
865 (define-key map "\e\t" 'widget-backward)
866 (define-key map [(shift tab)] 'widget-backward)
867 (put 'widget-backward :advertised-binding [(shift tab)])
868 (define-key map [backtab] 'widget-backward)
869 (define-key map [down-mouse-2] 'widget-button-click)
870 (define-key map [down-mouse-1] 'widget-button-click)
871 ;; The following definition needs to avoid using escape sequences that
872 ;; might get converted to ^M when building loaddefs.el
873 (define-key map [(control ?m)] 'widget-button-press)
875 "Keymap containing useful binding for buffers containing widgets.
876 Recommended as a parent keymap for modes using widgets.
877 Note that such modes will need to require wid-edit.")
879 (defvar widget-global-map global-map
880 "Keymap used for events a widget does not handle itself.")
881 (make-variable-buffer-local 'widget-global-map)
883 (defvar widget-field-keymap
884 (let ((map (copy-keymap widget-keymap)))
885 (define-key map "\C-k" 'widget-kill-line)
886 (define-key map "\M-\t" 'widget-complete)
887 (define-key map "\C-m" 'widget-field-activate)
888 ;; Since the widget code uses a `field' property to identify fields,
889 ;; ordinary beginning-of-line does the right thing.
890 ;; (define-key map "\C-a" 'widget-beginning-of-line)
891 (define-key map "\C-e" 'widget-end-of-line)
893 "Keymap used inside an editable field.")
895 (defvar widget-text-keymap
896 (let ((map (copy-keymap widget-keymap)))
897 ;; Since the widget code uses a `field' property to identify fields,
898 ;; ordinary beginning-of-line does the right thing.
899 ;; (define-key map "\C-a" 'widget-beginning-of-line)
900 (define-key map "\C-e" 'widget-end-of-line)
902 "Keymap used inside a text field.")
904 (defun widget-field-activate (pos &optional event)
905 "Invoke the editable field at point."
907 (let ((field (widget-field-at pos)))
909 (widget-apply-action field event)
911 (lookup-key widget-global-map (this-command-keys))))))
913 (defface widget-button-pressed
914 '((((min-colors 88) (class color))
915 (:foreground "red1"))
919 (:weight bold :underline t)))
920 "Face used for pressed buttons."
921 :group 'widget-faces)
922 (define-obsolete-face-alias 'widget-button-pressed-face
923 'widget-button-pressed "22.1")
925 (defvar widget-button-click-moves-point nil
926 "If non-nil, `widget-button-click' moves point to a button after invoking it.
927 If nil, point returns to its original position after invoking a button.")
929 (defun widget-button-click (event)
930 "Invoke the button that the mouse is pointing at."
932 (if (widget-event-point event)
933 (let* ((oevent event)
934 (mouse-1 (memq (event-basic-type event) '(mouse-1 down-mouse-1)))
935 (pos (widget-event-point event))
936 (start (event-start event))
937 (button (get-char-property
938 pos 'button (and (windowp (posn-window start))
939 (window-buffer (posn-window start)))))
941 (when (or (null button)
942 (catch 'button-press-cancelled
943 ;; Mouse click on a widget button. Do the following
944 ;; in a save-excursion so that the click on the button
945 ;; doesn't change point.
946 (save-selected-window
947 (select-window (posn-window (event-start event)))
949 (goto-char (posn-point (event-start event)))
950 (let* ((overlay (widget-get button :button-overlay))
951 (pressed-face (or (widget-get button :pressed-face)
952 widget-button-pressed-face))
953 (face (overlay-get overlay 'face))
954 (mouse-face (overlay-get overlay 'mouse-face)))
956 ;; Read events, including mouse-movement
957 ;; events, waiting for a release event. If we
958 ;; began with a mouse-1 event and receive a
959 ;; movement event, that means the user wants
960 ;; to perform drag-selection, so cancel the
961 ;; button press and do the default mouse-1
962 ;; action. For mouse-2, just highlight/
963 ;; unhighlight the button the mouse was
964 ;; initially on when we move over it.
966 (when face ; avoid changing around image
967 (overlay-put overlay 'face pressed-face)
968 (overlay-put overlay 'mouse-face pressed-face))
969 (unless (widget-apply button :mouse-down-action event)
970 (let ((track-mouse t))
971 (while (not (widget-button-release-event-p event))
972 (setq event (read-event))
973 (when (and mouse-1 (mouse-movement-p event))
974 (push event unread-command-events)
976 (throw 'button-press-cancelled t))
977 (unless (or (integerp event)
978 (memq (car event) '(switch-frame select-window))
979 (eq (car event) 'scroll-bar-movement))
980 (setq pos (widget-event-point event))
982 (eq (get-char-property pos 'button)
985 (overlay-put overlay 'face pressed-face)
986 (overlay-put overlay 'mouse-face pressed-face))
987 (overlay-put overlay 'face face)
988 (overlay-put overlay 'mouse-face mouse-face))))))
990 ;; When mouse is released over the button, run
991 ;; its action function.
992 (when (and pos (eq (get-char-property pos 'button) button))
994 (widget-apply-action button event)
995 (if widget-button-click-moves-point
996 (setq newpoint (point)))))
997 (overlay-put overlay 'face face)
998 (overlay-put overlay 'mouse-face mouse-face))))
1000 (if newpoint (goto-char newpoint))
1001 ;; This loses if the widget action switches windows. -- cyd
1002 ;; (unless (pos-visible-in-window-p (widget-event-point event))
1003 ;; (mouse-set-point event)
1004 ;; (beginning-of-line)
1008 (let ((up t) command)
1009 ;; Mouse click not on a widget button. Find the global
1010 ;; command to run, and check whether it is bound to an
1013 (cond ((setq command ;down event
1014 (lookup-key widget-global-map [down-mouse-1]))
1016 ((setq command ;up event
1017 (lookup-key widget-global-map [mouse-1]))))
1018 (cond ((setq command ;down event
1019 (lookup-key widget-global-map [down-mouse-2]))
1021 ((setq command ;up event
1022 (lookup-key widget-global-map [mouse-2])))))
1024 ;; Don't execute up events twice.
1025 (while (not (widget-button-release-event-p event))
1026 (setq event (read-event))))
1028 (call-interactively command)))))
1029 (message "You clicked somewhere weird.")))
1031 (defun widget-button-press (pos &optional event)
1032 "Invoke button at POS."
1034 (let ((button (get-char-property pos 'button)))
1036 (widget-apply-action button event)
1037 (let ((command (lookup-key widget-global-map (this-command-keys))))
1038 (when (commandp command)
1039 (call-interactively command))))))
1041 (defun widget-tabable-at (&optional pos)
1042 "Return the tabable widget at POS, or nil.
1043 POS defaults to the value of (point)."
1044 (let ((widget (widget-at pos)))
1046 (let ((order (widget-get widget :tab-order)))
1052 (defvar widget-use-overlay-change t
1053 "If non-nil, use overlay change functions to tab around in the buffer.
1054 This is much faster.")
1056 (defun widget-move (arg)
1057 "Move point to the ARG next field or button.
1058 ARG may be negative to move backward."
1059 (or (bobp) (> arg 0) (backward-char))
1062 (old (widget-tabable-at)))
1066 (goto-char (point-min))
1067 (setq wrapped (1+ wrapped)))
1068 (widget-use-overlay-change
1069 (goto-char (next-overlay-change (point))))
1074 (error "No buttons or fields found"))
1075 (let ((new (widget-tabable-at)))
1077 (unless (eq new old)
1083 (goto-char (point-max))
1084 (setq wrapped (1+ wrapped)))
1085 (widget-use-overlay-change
1086 (goto-char (previous-overlay-change (point))))
1091 (error "No buttons or fields found"))
1092 (let ((new (widget-tabable-at)))
1094 (unless (eq new old)
1095 (setq arg (1+ arg))))))
1096 (let ((new (widget-tabable-at)))
1097 (while (eq (widget-tabable-at) new)
1100 (widget-echo-help (point))
1101 (run-hooks 'widget-move-hook))
1103 (defun widget-forward (arg)
1104 "Move point to the next field or button.
1105 With optional ARG, move across that many fields."
1107 (run-hooks 'widget-forward-hook)
1110 (defun widget-backward (arg)
1111 "Move point to the previous field or button.
1112 With optional ARG, move across that many fields."
1114 (run-hooks 'widget-backward-hook)
1115 (widget-move (- arg)))
1117 ;; Since the widget code uses a `field' property to identify fields,
1118 ;; ordinary beginning-of-line does the right thing.
1119 (defalias 'widget-beginning-of-line 'beginning-of-line)
1121 (defun widget-end-of-line ()
1122 "Go to end of field or end of line, whichever is first.
1123 Trailing spaces at the end of padded fields are not considered part of
1126 ;; Ordinary end-of-line does the right thing, because we're inside
1127 ;; text with a `field' property.
1130 ;; ... except that we want to ignore trailing spaces in fields that
1131 ;; aren't terminated by a newline, because they are used as padding,
1132 ;; and ignored when extracting the entered value of the field.
1133 (skip-chars-backward " " (field-beginning (1- (point))))))
1135 (defun widget-kill-line ()
1136 "Kill to end of field or end of line, whichever is first."
1138 (let* ((field (widget-field-find (point)))
1139 (end (and field (widget-field-end field))))
1140 (if (and field (> (line-beginning-position 2) end))
1141 (kill-region (point) end)
1142 (call-interactively 'kill-line))))
1144 (defun widget-narrow-to-field ()
1147 (let ((field (widget-field-find (point))))
1149 (narrow-to-region (line-beginning-position) (line-end-position)))))
1151 ;; This used to say:
1152 ;; "When not inside a field, move to the previous button or field."
1153 ;; but AFAICS, it has always just thrown an error.
1154 (defun widget-complete ()
1155 "Complete content of editable field from point.
1156 When not inside a field, signal an error."
1158 (let ((data (widget-completions-at-point)))
1160 ((functionp data) (funcall data))
1162 (let ((completion-extra-properties (nth 3 data)))
1163 (completion-in-region (nth 0 data) (nth 1 data) (nth 2 data)
1164 (plist-get completion-extra-properties
1167 (error "Not in an editable field")))))
1168 ;; We may want to use widget completion in buffers where the major mode
1169 ;; hasn't added widget-completions-at-point to completion-at-point-functions,
1170 ;; so it's not really obsolete (yet).
1171 ;; (make-obsolete 'widget-complete 'completion-at-point "24.1")
1173 (defun widget-completions-at-point ()
1174 (let ((field (widget-field-find (point))))
1176 (widget-apply field :completions-function))))
1178 ;;; Setting up the buffer.
1180 (defvar widget-field-new nil
1181 "List of all newly created editable fields in the buffer.")
1182 (make-variable-buffer-local 'widget-field-new)
1184 (defvar widget-field-list nil
1185 "List of all editable fields in the buffer.")
1186 (make-variable-buffer-local 'widget-field-list)
1188 (defun widget-at (&optional pos)
1189 "The button or field at POS (default, point)."
1190 (or (get-char-property (or pos (point)) 'button)
1191 (widget-field-at pos)))
1194 (defun widget-setup ()
1195 "Setup current buffer so editing string widgets works."
1196 (let ((inhibit-read-only t)
1197 (inhibit-modification-hooks t)
1199 (while widget-field-new
1200 (setq field (car widget-field-new)
1201 widget-field-new (cdr widget-field-new)
1202 widget-field-list (cons field widget-field-list))
1203 (let ((from (car (widget-get field :field-overlay)))
1204 (to (cdr (widget-get field :field-overlay))))
1205 (widget-specify-field field
1206 (marker-position from) (marker-position to))
1207 (set-marker from nil)
1208 (set-marker to nil))))
1210 (widget-add-change))
1212 (defvar widget-field-last nil)
1213 ;; Last field containing point.
1214 (make-variable-buffer-local 'widget-field-last)
1216 (defvar widget-field-was nil)
1217 ;; The widget data before the change.
1218 (make-variable-buffer-local 'widget-field-was)
1220 (defun widget-field-at (pos)
1221 "Return the widget field at POS, or nil if none."
1222 (let ((field (get-char-property (or pos (point)) 'field)))
1223 (if (eq field 'boundary)
1224 (get-char-property (or pos (point)) 'real-field)
1227 (defun widget-field-buffer (widget)
1228 "Return the buffer of WIDGET's editing field."
1229 (let ((overlay (widget-get widget :field-overlay)))
1230 (cond ((overlayp overlay)
1231 (overlay-buffer overlay))
1233 (marker-buffer (car overlay))))))
1235 (defun widget-field-start (widget)
1236 "Return the start of WIDGET's editing field."
1237 (let ((overlay (widget-get widget :field-overlay)))
1238 (if (overlayp overlay)
1239 (overlay-start overlay)
1242 (defun widget-field-end (widget)
1243 "Return the end of WIDGET's editing field."
1244 (let ((overlay (widget-get widget :field-overlay)))
1245 ;; Don't subtract one if local-map works at the end of the overlay,
1246 ;; or if a special `boundary' field has been added after the widget
1248 (if (overlayp overlay)
1249 ;; Don't proceed if overlay has been removed from buffer.
1250 (when (overlay-buffer overlay)
1251 (if (and (not (eq (with-current-buffer
1252 (widget-field-buffer widget)
1254 ;; `widget-narrow-to-field' can be
1255 ;; active when this function is called
1256 ;; from an change-functions hook. So
1257 ;; temporarily remove field narrowing
1258 ;; before to call `get-char-property'.
1260 (get-char-property (overlay-end overlay)
1263 (or widget-field-add-space
1264 (null (widget-get widget :size))))
1265 (1- (overlay-end overlay))
1266 (overlay-end overlay)))
1269 (defun widget-field-text-end (widget)
1270 (let ((to (widget-field-end widget))
1271 (size (widget-get widget :size)))
1272 (if (or (null size) (zerop size))
1274 (let ((from (widget-field-start widget)))
1276 (with-current-buffer (widget-field-buffer widget)
1277 (while (and (> to from)
1278 (eq (char-after (1- to)) ?\s))
1282 (defun widget-field-find (pos)
1283 "Return the field at POS.
1284 Unlike (get-char-property POS 'field), this works with empty fields too."
1285 (let ((fields widget-field-list)
1288 (setq field (car fields)
1289 fields (cdr fields))
1290 (when (and (<= (widget-field-start field) pos)
1291 (<= pos (widget-field-end field)))
1293 (error "Overlapping fields"))
1294 (setq found field)))
1297 (defun widget-before-change (from to)
1298 ;; This is how, for example, a variable changes its state to `modified'.
1299 ;; when it is being edited.
1300 (unless inhibit-read-only
1301 (let ((from-field (widget-field-find from))
1302 (to-field (widget-field-find to)))
1303 (cond ((not (eq from-field to-field))
1304 (add-hook 'post-command-hook 'widget-add-change nil t)
1305 (signal 'text-read-only
1306 '("Change should be restricted to a single field")))
1308 (add-hook 'post-command-hook 'widget-add-change nil t)
1309 (signal 'text-read-only
1310 '("Attempt to change text outside editable field")))
1311 (widget-field-use-before-change
1312 (widget-apply from-field :notify from-field))))))
1314 (defun widget-add-change ()
1315 (remove-hook 'post-command-hook 'widget-add-change t)
1316 (add-hook 'before-change-functions 'widget-before-change nil t)
1317 (add-hook 'after-change-functions 'widget-after-change nil t))
1319 (defun widget-after-change (from to _old)
1320 "Adjust field size and text properties."
1321 (let ((field (widget-field-find from))
1322 (other (widget-field-find to)))
1324 (unless (eq field other)
1325 (error "Change in different fields"))
1326 (let ((size (widget-get field :size)))
1328 (let ((begin (widget-field-start field))
1329 (end (widget-field-end field)))
1330 (cond ((< (- end begin) size)
1334 (insert-char ?\s (- (+ begin size) end))))
1335 ((> (- end begin) size)
1336 ;; Field too large and
1337 (if (or (< (point) (+ begin size))
1339 ;; Point is outside extra space.
1340 (setq begin (+ begin size))
1341 ;; Point is within the extra space.
1342 (setq begin (point)))
1345 (while (and (eq (preceding-char) ?\s)
1347 (delete-char -1)))))))
1348 (widget-specify-secret field))
1349 (widget-apply field :notify field))))
1351 ;;; Widget Functions
1353 ;; These functions are used in the definition of multiple widgets.
1355 (defun widget-parent-action (widget &optional event)
1356 "Tell :parent of WIDGET to handle the :action.
1357 Optional EVENT is the event that triggered the action."
1358 (widget-apply (widget-get widget :parent) :action event))
1360 (defun widget-children-value-delete (widget)
1361 "Delete all :children and :buttons in WIDGET."
1362 (mapc 'widget-delete (widget-get widget :children))
1363 (widget-put widget :children nil)
1364 (mapc 'widget-delete (widget-get widget :buttons))
1365 (widget-put widget :buttons nil))
1367 (defun widget-children-validate (widget)
1368 "All the :children must be valid."
1369 (let ((children (widget-get widget :children))
1371 (while (and children (not found))
1372 (setq child (car children)
1373 children (cdr children)
1374 found (widget-apply child :validate)))
1377 (defun widget-child-value-get (widget)
1378 "Get the value of the first member of :children in WIDGET."
1379 (widget-value (car (widget-get widget :children))))
1381 (defun widget-child-value-inline (widget)
1382 "Get the inline value of the first member of :children in WIDGET."
1383 (widget-apply (car (widget-get widget :children)) :value-inline))
1385 (defun widget-child-validate (widget)
1386 "The result of validating the first member of :children in WIDGET."
1387 (widget-apply (car (widget-get widget :children)) :validate))
1389 (defun widget-type-value-create (widget)
1390 "Convert and instantiate the value of the :type attribute of WIDGET.
1391 Store the newly created widget in the :children attribute.
1393 The value of the :type attribute should be an unconverted widget type."
1394 (let ((value (widget-get widget :value))
1395 (type (widget-get widget :type)))
1396 (widget-put widget :children
1397 (list (widget-create-child-value widget
1398 (widget-convert type)
1401 (defun widget-type-default-get (widget)
1402 "Get default value from the :type attribute of WIDGET.
1404 The value of the :type attribute should be an unconverted widget type."
1405 (widget-default-get (widget-convert (widget-get widget :type))))
1407 (defun widget-type-match (widget value)
1408 "Non-nil if the :type value of WIDGET matches VALUE.
1410 The value of the :type attribute should be an unconverted widget type."
1411 (widget-apply (widget-convert (widget-get widget :type)) :match value))
1413 (defun widget-types-copy (widget)
1414 "Copy :args as widget types in WIDGET."
1415 (widget-put widget :args (mapcar 'widget-copy (widget-get widget :args)))
1418 ;; Made defsubst to speed up face editor creation.
1419 (defsubst widget-types-convert-widget (widget)
1420 "Convert :args as widget types in WIDGET."
1421 (widget-put widget :args (mapcar 'widget-convert (widget-get widget :args)))
1424 (defun widget-value-convert-widget (widget)
1425 "Initialize :value from :args in WIDGET."
1426 (let ((args (widget-get widget :args)))
1428 (widget-put widget :value (car args))
1429 ;; Don't convert :value here, as this is done in `widget-convert'.
1430 ;; (widget-put widget :value (widget-apply widget
1431 ;; :value-to-internal (car args)))
1432 (widget-put widget :args nil)))
1435 (defun widget-value-value-get (widget)
1436 "Return the :value property of WIDGET."
1437 (widget-get widget :value))
1439 ;;; The `default' Widget.
1441 (define-widget 'default nil
1442 "Basic widget other widgets are derived from."
1443 :value-to-internal (lambda (_widget value) value)
1444 :value-to-external (lambda (_widget value) value)
1445 :button-prefix 'widget-button-prefix
1446 :button-suffix 'widget-button-suffix
1447 :completions-function #'widget-default-completions
1448 :create 'widget-default-create
1451 :format-handler 'widget-default-format-handler
1452 :button-face-get 'widget-default-button-face-get
1453 :mouse-face-get 'widget-default-mouse-face-get
1454 :sample-face-get 'widget-default-sample-face-get
1455 :delete 'widget-default-delete
1457 :value-set 'widget-default-value-set
1458 :value-inline 'widget-default-value-inline
1459 :value-delete 'ignore
1460 :default-get 'widget-default-default-get
1461 :menu-tag-get 'widget-default-menu-tag-get
1463 :active 'widget-default-active
1464 :activate 'widget-specify-active
1465 :deactivate 'widget-default-deactivate
1466 :mouse-down-action #'ignore
1467 :action 'widget-default-action
1468 :notify 'widget-default-notify
1469 :prompt-value 'widget-default-prompt-value)
1471 (defvar widget--completing-widget)
1473 (defun widget-default-completions (widget)
1474 "Return completion data, like `completion-at-point-functions' would."
1475 (let ((completions (widget-get widget :completions)))
1477 (list (widget-field-start widget)
1478 (max (point) (widget-field-text-end widget))
1480 (if (widget-get widget :complete)
1481 (lambda () (widget-apply widget :complete))
1482 (if (widget-get widget :complete-function)
1484 (let ((widget--completing-widget widget))
1486 (widget-get widget :complete-function)))))))))
1488 (defun widget-default-create (widget)
1489 "Create WIDGET at point in the current buffer."
1490 (widget-specify-insert
1491 (let ((from (point))
1492 button-begin button-end
1493 sample-begin sample-end
1496 (insert (widget-get widget :format))
1498 ;; Parse escapes in format.
1499 (while (re-search-forward "%\\(.\\)" nil t)
1500 (let ((escape (char-after (match-beginning 1))))
1502 (cond ((eq escape ?%)
1505 (setq button-begin (point))
1506 (insert (widget-get-indirect widget :button-prefix)))
1508 (insert (widget-get-indirect widget :button-suffix))
1509 (setq button-end (point)))
1511 (setq sample-begin (point)))
1513 (setq sample-end (point)))
1515 (when (widget-get widget :indent)
1517 (insert-char ?\s (widget-get widget :indent))))
1519 (let ((image (widget-get widget :tag-glyph))
1520 (tag (widget-get widget :tag)))
1522 (widget-image-insert widget (or tag "image") image))
1526 (princ (widget-get widget :value)
1527 (current-buffer))))))
1529 (let ((doc (widget-get widget :doc)))
1531 (setq doc-begin (point))
1533 (while (eq (preceding-char) ?\n)
1536 (setq doc-end (point)))))
1538 (widget-add-documentation-string-button widget))
1540 (if (and button-begin (not button-end))
1541 (widget-apply widget :value-create)
1542 (setq value-pos (point))))
1544 (widget-apply widget :format-handler escape)))))
1545 ;; Specify button, sample, and doc, and insert value.
1546 (and button-begin button-end
1547 (widget-specify-button widget button-begin button-end))
1548 (and sample-begin sample-end
1549 (widget-specify-sample widget sample-begin sample-end))
1550 (and doc-begin doc-end
1551 (widget-specify-doc widget doc-begin doc-end))
1553 (goto-char value-pos)
1554 (widget-apply widget :value-create)))
1555 (let ((from (point-min-marker))
1556 (to (point-max-marker)))
1557 (set-marker-insertion-type from t)
1558 (set-marker-insertion-type to nil)
1559 (widget-put widget :from from)
1560 (widget-put widget :to to)))
1561 (widget-clear-undo))
1563 (defun widget-default-format-handler (_widget escape)
1564 (error "Unknown escape `%c'" escape))
1566 (defun widget-default-button-face-get (widget)
1567 ;; Use :button-face or widget-button-face
1568 (or (widget-get widget :button-face)
1569 (let ((parent (widget-get widget :parent)))
1571 (widget-apply parent :button-face-get)
1572 widget-button-face))))
1574 (defun widget-default-mouse-face-get (widget)
1575 ;; Use :mouse-face or widget-mouse-face
1576 (or (widget-get widget :mouse-face)
1577 (let ((parent (widget-get widget :parent)))
1579 (widget-apply parent :mouse-face-get)
1580 widget-mouse-face))))
1582 (defun widget-default-sample-face-get (widget)
1583 ;; Use :sample-face.
1584 (widget-get widget :sample-face))
1586 (defun widget-default-delete (widget)
1587 "Remove widget from the buffer."
1588 (let ((from (widget-get widget :from))
1589 (to (widget-get widget :to))
1590 (inactive-overlay (widget-get widget :inactive))
1591 (button-overlay (widget-get widget :button-overlay))
1592 (sample-overlay (widget-get widget :sample-overlay))
1593 (doc-overlay (widget-get widget :doc-overlay))
1594 (inhibit-modification-hooks t)
1595 (inhibit-read-only t))
1596 (widget-apply widget :value-delete)
1597 (widget-children-value-delete widget)
1598 (when inactive-overlay
1599 (delete-overlay inactive-overlay))
1600 (when button-overlay
1601 (delete-overlay button-overlay))
1602 (when sample-overlay
1603 (delete-overlay sample-overlay))
1605 (delete-overlay doc-overlay))
1607 ;; Kludge: this doesn't need to be true for empty formats.
1608 (delete-region from to))
1609 (set-marker from nil)
1610 (set-marker to nil))
1611 (widget-clear-undo))
1613 (defun widget-default-value-set (widget value)
1614 "Recreate widget with new value."
1615 (let* ((old-pos (point))
1616 (from (copy-marker (widget-get widget :from)))
1617 (to (copy-marker (widget-get widget :to)))
1618 (offset (if (and (<= from old-pos) (<= old-pos to))
1619 (if (>= old-pos (1- to))
1621 (- old-pos from)))))
1622 ;;??? Bug: this ought to insert the new value before deleting the old one,
1623 ;; so that markers on either side of the value automatically
1624 ;; stay on the same side. -- rms.
1626 (goto-char (widget-get widget :from))
1627 (widget-apply widget :delete)
1628 (widget-put widget :value value)
1629 (widget-apply widget :create))
1632 (goto-char (+ (widget-get widget :to) offset 1))
1633 (goto-char (min (+ from offset) (1- (widget-get widget :to))))))))
1635 (defun widget-default-value-inline (widget)
1636 "Wrap value in a list unless it is inline."
1637 (if (widget-get widget :inline)
1638 (widget-value widget)
1639 (list (widget-value widget))))
1641 (defun widget-default-default-get (widget)
1643 (widget-get widget :value))
1645 (defun widget-default-menu-tag-get (widget)
1646 "Use tag or value for menus."
1647 (or (widget-get widget :menu-tag)
1648 (widget-get widget :tag)
1649 (widget-princ-to-string (widget-get widget :value))))
1651 (defun widget-default-active (widget)
1652 "Return t if this widget is active (user modifiable)."
1653 (or (widget-get widget :always-active)
1654 (and (not (widget-get widget :inactive))
1655 (let ((parent (widget-get widget :parent)))
1657 (widget-apply parent :active))))))
1659 (defun widget-default-deactivate (widget)
1660 "Make WIDGET inactive for user modifications."
1661 (widget-specify-inactive widget
1662 (widget-get widget :from)
1663 (widget-get widget :to)))
1665 (defun widget-default-action (widget &optional event)
1666 "Notify the parent when a widget changes."
1667 (let ((parent (widget-get widget :parent)))
1669 (widget-apply parent :notify widget event))))
1671 (defun widget-default-notify (widget _child &optional event)
1672 "Pass notification to parent."
1673 (widget-default-action widget event))
1675 (defun widget-default-prompt-value (_widget prompt _value _unbound)
1676 "Read an arbitrary value."
1677 (eval-minibuffer prompt))
1679 (defun widget-docstring (widget)
1680 "Return the documentation string specified by WIDGET, or nil if none.
1681 If WIDGET has a `:doc' property, that specifies the documentation string.
1682 Otherwise, try the `:documentation-property' property. If this
1683 is a function, call it with the widget's value as an argument; if
1684 it is a symbol, use this symbol together with the widget's value
1685 as the argument to `documentation-property'."
1686 (let ((doc (or (widget-get widget :doc)
1687 (let ((doc-prop (widget-get widget :documentation-property))
1688 (value (widget-get widget :value)))
1689 (cond ((functionp doc-prop)
1690 (funcall doc-prop value))
1692 (documentation-property value doc-prop)))))))
1693 (when (and (stringp doc) (> (length doc) 0))
1694 ;; Remove any redundant `*' in the beginning.
1695 (when (eq (aref doc 0) ?*)
1696 (setq doc (substring doc 1)))
1697 ;; Remove trailing newlines.
1698 (when (string-match "\n+\\'" doc)
1699 (setq doc (substring doc 0 (match-beginning 0))))
1702 ;;; The `item' Widget.
1704 (define-widget 'item 'default
1705 "Constant items for inclusion in other widgets."
1706 :convert-widget 'widget-value-convert-widget
1707 :value-create 'widget-item-value-create
1708 :value-delete 'ignore
1709 :value-get 'widget-value-value-get
1710 :match 'widget-item-match
1711 :match-inline 'widget-item-match-inline
1712 :action 'widget-item-action
1715 (defun widget-item-value-create (widget)
1716 "Insert the printed representation of the value."
1717 (princ (widget-get widget :value) (current-buffer)))
1719 (defun widget-item-match (widget value)
1720 ;; Match if the value is the same.
1721 (equal (widget-get widget :value) value))
1723 (defun widget-item-match-inline (widget vals)
1724 ;; Match if the value is the same.
1725 (let ((value (widget-get widget :value)))
1727 (<= (length value) (length vals))
1728 (let ((head (widget-sublist vals 0 (length value))))
1729 (and (equal head value)
1730 (cons head (widget-sublist vals (length value))))))))
1732 (defun widget-sublist (list start &optional end)
1733 "Return the sublist of LIST from START to END.
1734 If END is omitted, it defaults to the length of LIST."
1735 (if (> start 0) (setq list (nthcdr start list)))
1737 (unless (<= end start)
1738 (setq list (copy-sequence list))
1739 (setcdr (nthcdr (- end start 1) list) nil)
1741 (copy-sequence list)))
1743 (defun widget-item-action (widget &optional event)
1744 ;; Just notify itself.
1745 (widget-apply widget :notify widget event))
1747 ;;; The `push-button' Widget.
1749 ;; (defcustom widget-push-button-gui t
1750 ;; "If non-nil, use GUI push buttons when available."
1754 ;; Cache already created GUI objects.
1755 ;; (defvar widget-push-button-cache nil)
1757 (defcustom widget-push-button-prefix "["
1758 "String used as prefix for buttons."
1760 :group 'widget-button)
1762 (defcustom widget-push-button-suffix "]"
1763 "String used as suffix for buttons."
1765 :group 'widget-button)
1767 (define-widget 'push-button 'item
1768 "A pushable button."
1771 :value-create 'widget-push-button-value-create
1774 (defun widget-push-button-value-create (widget)
1775 "Insert text representing the `on' and `off' states."
1776 (let* ((tag (or (widget-get widget :tag)
1777 (widget-get widget :value)))
1778 (tag-glyph (widget-get widget :tag-glyph))
1779 (text (concat widget-push-button-prefix
1780 tag widget-push-button-suffix)))
1782 (widget-image-insert widget text tag-glyph)
1785 ;; (defun widget-gui-action (widget)
1786 ;; "Apply :action for WIDGET."
1787 ;; (widget-apply-action widget (this-command-keys)))
1789 ;;; The `link' Widget.
1791 (defcustom widget-link-prefix "["
1792 "String used as prefix for links."
1794 :group 'widget-button)
1796 (defcustom widget-link-suffix "]"
1797 "String used as suffix for links."
1799 :group 'widget-button)
1801 (define-widget 'link 'item
1803 :button-prefix 'widget-link-prefix
1804 :button-suffix 'widget-link-suffix
1805 :follow-link 'mouse-face
1806 :help-echo "Follow the link."
1809 ;;; The `info-link' Widget.
1811 (define-widget 'info-link 'link
1812 "A link to an info file."
1813 :action 'widget-info-link-action)
1815 (defun widget-info-link-action (widget &optional _event)
1816 "Open the info node specified by WIDGET."
1817 (info (widget-value widget)))
1819 ;;; The `url-link' Widget.
1821 (define-widget 'url-link 'link
1822 "A link to an www page."
1823 :action 'widget-url-link-action)
1825 (defun widget-url-link-action (widget &optional _event)
1826 "Open the URL specified by WIDGET."
1827 (browse-url (widget-value widget)))
1829 ;;; The `function-link' Widget.
1831 (define-widget 'function-link 'link
1832 "A link to an Emacs function."
1833 :action 'widget-function-link-action)
1835 (defun widget-function-link-action (widget &optional _event)
1836 "Show the function specified by WIDGET."
1837 (describe-function (widget-value widget)))
1839 ;;; The `variable-link' Widget.
1841 (define-widget 'variable-link 'link
1842 "A link to an Emacs variable."
1843 :action 'widget-variable-link-action)
1845 (defun widget-variable-link-action (widget &optional _event)
1846 "Show the variable specified by WIDGET."
1847 (describe-variable (widget-value widget)))
1849 ;;; The `file-link' Widget.
1851 (define-widget 'file-link 'link
1853 :action 'widget-file-link-action)
1855 (defun widget-file-link-action (widget &optional _event)
1856 "Find the file specified by WIDGET."
1857 (find-file (widget-value widget)))
1859 ;;; The `emacs-library-link' Widget.
1861 (define-widget 'emacs-library-link 'link
1862 "A link to an Emacs Lisp library file."
1863 :action 'widget-emacs-library-link-action)
1865 (defun widget-emacs-library-link-action (widget &optional _event)
1866 "Find the Emacs library file specified by WIDGET."
1867 (find-file (locate-library (widget-value widget))))
1869 ;;; The `emacs-commentary-link' Widget.
1871 (define-widget 'emacs-commentary-link 'link
1872 "A link to Commentary in an Emacs Lisp library file."
1873 :action 'widget-emacs-commentary-link-action)
1875 (defun widget-emacs-commentary-link-action (widget &optional _event)
1876 "Find the Commentary section of the Emacs file specified by WIDGET."
1877 (finder-commentary (widget-value widget)))
1879 ;;; The `editable-field' Widget.
1881 (define-widget 'editable-field 'default
1882 "An editable text field.
1883 Note: In an `editable-field' widget, the `%v' escape must be preceded
1884 by some other text in the `:format' string (if specified)."
1885 :convert-widget 'widget-value-convert-widget
1886 :keymap widget-field-keymap
1888 :help-echo "M-TAB: complete field; RET: enter value"
1890 :prompt-internal 'widget-field-prompt-internal
1891 :prompt-history 'widget-field-history
1892 :prompt-value 'widget-field-prompt-value
1893 :action 'widget-field-action
1894 :validate 'widget-field-validate
1896 :error "Field's value doesn't match allowed forms"
1897 :value-create 'widget-field-value-create
1898 :value-set 'widget-field-value-set
1899 :value-delete 'widget-field-value-delete
1900 :value-get 'widget-field-value-get
1901 :match 'widget-field-match)
1903 (defvar widget-field-history nil
1904 "History of field minibuffer edits.")
1906 (defun widget-field-prompt-internal (_widget prompt initial history)
1907 "Read string for WIDGET prompting with PROMPT.
1908 INITIAL is the initial input and HISTORY is a symbol containing
1910 (read-string prompt initial history))
1912 (defun widget-field-prompt-value (widget prompt value unbound)
1913 "Prompt for a string."
1914 (widget-apply widget
1916 (widget-apply widget
1917 :prompt-internal prompt
1919 (cons (widget-apply widget
1920 :value-to-internal value)
1922 (widget-get widget :prompt-history))))
1924 (defvar widget-edit-functions nil)
1926 (defun widget-field-action (widget &optional _event)
1927 "Move to next field."
1929 (run-hook-with-args 'widget-edit-functions widget))
1931 (defun widget-field-validate (widget)
1932 "Valid if the content matches `:valid-regexp'."
1933 (unless (string-match (widget-get widget :valid-regexp)
1934 (widget-apply widget :value-get))
1937 (defun widget-field-value-set (widget value)
1938 "Set an editable text field WIDGET to VALUE"
1939 (let ((from (widget-field-start widget))
1940 (to (widget-field-text-end widget))
1941 (buffer (widget-field-buffer widget)))
1942 (when (and from to (buffer-live-p buffer))
1943 (with-current-buffer buffer
1945 (delete-char (- to from))
1948 (defun widget-field-value-create (widget)
1949 "Create an editable text field."
1950 (let ((size (widget-get widget :size))
1951 (value (widget-get widget :value))
1953 ;; This is changed to a real overlay in `widget-setup'. We
1954 ;; need the end points to behave differently until
1955 ;; `widget-setup' is called.
1956 (overlay (cons (make-marker) (make-marker))))
1957 (widget-put widget :field-overlay overlay)
1960 (< (length value) size)
1961 (insert-char ?\s (- size (length value))))
1962 (unless (memq widget widget-field-list)
1963 (setq widget-field-new (cons widget widget-field-new)))
1964 (move-marker (cdr overlay) (point))
1965 (set-marker-insertion-type (cdr overlay) nil)
1968 (move-marker (car overlay) from)
1969 (set-marker-insertion-type (car overlay) t)))
1971 (defun widget-field-value-delete (widget)
1972 "Remove the widget from the list of active editing fields."
1973 (setq widget-field-list (delq widget widget-field-list))
1974 (setq widget-field-new (delq widget widget-field-new))
1975 ;; These are nil if the :format string doesn't contain `%v'.
1976 (let ((overlay (widget-get widget :field-overlay)))
1977 (when (overlayp overlay)
1978 (delete-overlay overlay))))
1980 (defun widget-field-value-get (widget &optional no-truncate)
1981 "Return current text in editing field.
1982 Normally, trailing spaces within the editing field are truncated.
1983 But if NO-TRUNCATE is non-nil, include them."
1984 (let ((from (widget-field-start widget))
1986 (widget-field-end widget)
1987 (widget-field-text-end widget)))
1988 (buffer (widget-field-buffer widget))
1989 (secret (widget-get widget :secret))
1990 (old (current-buffer)))
1994 (let ((result (buffer-substring-no-properties from to)))
1997 (while (< (+ from index) to)
1999 (get-char-property (+ from index) 'secret))
2000 (setq index (1+ index)))))
2003 (widget-get widget :value))))
2005 (defun widget-field-match (_widget value)
2006 ;; Match any string.
2009 ;;; The `text' Widget.
2011 (define-widget 'text 'editable-field
2012 "A multiline text area."
2013 :keymap widget-text-keymap)
2015 ;;; The `menu-choice' Widget.
2017 (define-widget 'menu-choice 'default
2018 "A menu of options."
2019 :convert-widget 'widget-types-convert-widget
2020 :copy 'widget-types-copy
2021 :format "%[%t%]: %v"
2024 :void '(item :format "invalid (%t)\n")
2025 :value-create 'widget-choice-value-create
2026 :value-get 'widget-child-value-get
2027 :value-inline 'widget-child-value-inline
2028 :default-get 'widget-choice-default-get
2029 :mouse-down-action 'widget-choice-mouse-down-action
2030 :action 'widget-choice-action
2031 :error "Make a choice"
2032 :validate 'widget-choice-validate
2033 :match 'widget-choice-match
2034 :match-inline 'widget-choice-match-inline)
2036 (defun widget-choice-value-create (widget)
2037 "Insert the first choice that matches the value."
2038 (let ((value (widget-get widget :value))
2039 (args (widget-get widget :args))
2040 (explicit (widget-get widget :explicit-choice))
2044 ;; If the user specified the choice for this value,
2045 ;; respect that choice.
2046 (widget-put widget :children (list (widget-create-child-value
2047 widget explicit value)))
2048 (widget-put widget :choice explicit)
2049 (widget-put widget :explicit-choice nil))
2051 (setq current (car args)
2053 (when (widget-apply current :match value)
2054 (widget-put widget :children (list (widget-create-child-value
2055 widget current value)))
2056 (widget-put widget :choice current)
2060 (let ((void (widget-get widget :void)))
2061 (widget-put widget :children (list (widget-create-child-and-convert
2062 widget void :value value)))
2063 (widget-put widget :choice void))))))
2065 (defun widget-choice-default-get (widget)
2066 ;; Get default for the first choice.
2067 (widget-default-get (car (widget-get widget :args))))
2069 (defcustom widget-choice-toggle nil
2070 "If non-nil, a binary choice will just toggle between the values.
2071 Otherwise, the user will explicitly have to choose between the values
2072 when he invoked the menu."
2076 (defun widget-choice-mouse-down-action (widget &optional _event)
2077 ;; Return non-nil if we need a menu.
2078 (let ((args (widget-get widget :args))
2079 (old (widget-get widget :choice)))
2080 (cond ((not (display-popup-menus-p))
2081 ;; No place to pop up a menu.
2083 ((< (length args) 2)
2084 ;; Empty or singleton list, just return the value.
2086 ((> (length args) widget-menu-max-size)
2087 ;; Too long, prompt.
2089 ((> (length args) 2)
2090 ;; Reasonable sized list, use menu.
2092 ((and widget-choice-toggle (memq old args))
2096 ;; Ask which of the two.
2099 (defun widget-choice-action (widget &optional event)
2101 (let ((args (widget-get widget :args))
2102 (old (widget-get widget :choice))
2103 (tag (widget-apply widget :menu-tag-get))
2104 (completion-ignore-case (widget-get widget :case-fold))
2107 ;; Remember old value.
2108 (if (and old (not (widget-apply widget :validate)))
2109 (let* ((external (widget-value widget))
2110 (internal (widget-apply old :value-to-internal external)))
2111 (widget-put old :value internal)))
2114 (cond ((= (length args) 0)
2116 ((= (length args) 1)
2118 ((and widget-choice-toggle
2121 (if (eq old (nth 0 args))
2126 (setq current (car args)
2129 (cons (cons (widget-apply current :menu-tag-get)
2132 (setq this-explicit t)
2133 (widget-choose tag (reverse choices) event))))
2135 ;; If this was an explicit user choice, record the choice,
2136 ;; so that widget-choice-value-create will respect it.
2138 (widget-put widget :explicit-choice current))
2139 (widget-value-set widget (widget-default-get current))
2141 (widget-apply widget :notify widget event)))
2142 (run-hook-with-args 'widget-edit-functions widget))
2144 (defun widget-choice-validate (widget)
2145 ;; Valid if we have made a valid choice.
2146 (if (eq (widget-get widget :void) (widget-get widget :choice))
2148 (widget-apply (car (widget-get widget :children)) :validate)))
2150 (defun widget-choice-match (widget value)
2151 ;; Matches if one of the choices matches.
2152 (let ((args (widget-get widget :args))
2154 (while (and args (not found))
2155 (setq current (car args)
2157 found (widget-apply current :match value)))
2160 (defun widget-choice-match-inline (widget vals)
2161 ;; Matches if one of the choices matches.
2162 (let ((args (widget-get widget :args))
2164 (while (and args (null found))
2165 (setq current (car args)
2167 found (widget-match-inline current vals)))
2170 ;;; The `toggle' Widget.
2172 (define-widget 'toggle 'item
2173 "Toggle between two states."
2175 :value-create 'widget-toggle-value-create
2176 :action 'widget-toggle-action
2177 :match (lambda (_widget _value) t)
2181 (defun widget-toggle-value-create (widget)
2182 "Insert text representing the `on' and `off' states."
2183 (let* ((val (widget-value widget))
2184 (text (widget-get widget (if val :on :off)))
2185 (img (widget-image-find
2186 (widget-get widget (if val :on-glyph :off-glyph)))))
2187 (widget-image-insert widget (or text "")
2189 (append img '(:ascent center))))))
2191 (defun widget-toggle-action (widget &optional event)
2193 (widget-value-set widget (not (widget-value widget)))
2194 (widget-apply widget :notify widget event)
2195 (run-hook-with-args 'widget-edit-functions widget))
2197 ;;; The `checkbox' Widget.
2199 (define-widget 'checkbox 'toggle
2200 "A checkbox toggle."
2205 ;; We could probably do the same job as the images using single
2206 ;; space characters in a boxed face with a stretch specification to
2207 ;; make them square.
2210 :off-glyph "unchecked"
2211 :help-echo "Toggle this item."
2212 :action 'widget-checkbox-action)
2214 (defun widget-checkbox-action (widget &optional event)
2215 "Toggle checkbox, notify parent, and set active state of sibling."
2216 (widget-toggle-action widget event)
2217 (let ((sibling (widget-get-sibling widget)))
2219 (if (widget-value widget)
2220 (widget-apply sibling :activate)
2221 (widget-apply sibling :deactivate))
2222 (widget-clear-undo))))
2224 ;;; The `checklist' Widget.
2226 (define-widget 'checklist 'default
2227 "A multiple choice widget."
2228 :convert-widget 'widget-types-convert-widget
2229 :copy 'widget-types-copy
2232 :entry-format "%b %v"
2234 :value-create 'widget-checklist-value-create
2235 :value-get 'widget-checklist-value-get
2236 :validate 'widget-checklist-validate
2237 :match 'widget-checklist-match
2238 :match-inline 'widget-checklist-match-inline)
2240 (defun widget-checklist-value-create (widget)
2241 ;; Insert all values
2242 (let ((alist (widget-checklist-match-find widget))
2243 (args (widget-get widget :args)))
2245 (widget-checklist-add-item widget item (assq item alist)))
2246 (widget-put widget :children (nreverse (widget-get widget :children)))))
2248 (defun widget-checklist-add-item (widget type chosen)
2249 "Create checklist item in WIDGET of type TYPE.
2250 If the item is checked, CHOSEN is a cons whose cdr is the value."
2251 (and (eq (preceding-char) ?\n)
2252 (widget-get widget :indent)
2253 (insert-char ?\s (widget-get widget :indent)))
2254 (widget-specify-insert
2255 (let* ((children (widget-get widget :children))
2256 (buttons (widget-get widget :buttons))
2257 (button-args (or (widget-get type :sibling-args)
2258 (widget-get widget :button-args)))
2261 (insert (widget-get widget :entry-format))
2263 ;; Parse % escapes in format.
2264 (while (re-search-forward "%\\([bv%]\\)" nil t)
2265 (let ((escape (char-after (match-beginning 1))))
2267 (cond ((eq escape ?%)
2270 (setq button (apply 'widget-create-child-and-convert
2272 :value (not (null chosen))
2277 (let ((child (widget-create-child widget type)))
2278 (widget-apply child :deactivate)
2280 ((widget-get type :inline)
2281 (widget-create-child-value
2282 widget type (cdr chosen)))
2284 (widget-create-child-value
2285 widget type (car (cdr chosen)))))))
2287 (error "Unknown escape `%c'" escape)))))
2288 ;; Update properties.
2289 (and button child (widget-put child :button button))
2290 (and button (widget-put widget :buttons (cons button buttons)))
2291 (and child (widget-put widget :children (cons child children))))))
2293 (defun widget-checklist-match (widget vals)
2294 ;; All values must match a type in the checklist.
2296 (null (cdr (widget-checklist-match-inline widget vals)))))
2298 (defun widget-checklist-match-inline (widget vals)
2299 ;; Find the values which match a type in the checklist.
2300 (let ((greedy (widget-get widget :greedy))
2301 (args (copy-sequence (widget-get widget :args)))
2304 (let ((answer (widget-checklist-match-up args vals)))
2306 (let ((vals2 (widget-match-inline answer vals)))
2307 (setq found (append found (car vals2))
2309 args (delq answer args))))
2311 (setq rest (append rest (list (car vals)))
2314 (setq rest (append rest vals)
2318 (defun widget-checklist-match-find (widget &optional vals)
2319 "Find the vals which match a type in the checklist.
2320 Return an alist of (TYPE MATCH)."
2321 (or vals (setq vals (widget-get widget :value)))
2322 (let ((greedy (widget-get widget :greedy))
2323 (args (copy-sequence (widget-get widget :args)))
2326 (let ((answer (widget-checklist-match-up args vals)))
2328 (let ((match (widget-match-inline answer vals)))
2329 (setq found (cons (cons answer (car match)) found)
2331 args (delq answer args))))
2333 (setq vals (cdr vals)))
2338 (defun widget-checklist-match-up (args vals)
2339 "Return the first type from ARGS that matches VALS."
2340 (let (current found)
2341 (while (and args (null found))
2342 (setq current (car args)
2344 found (widget-match-inline current vals)))
2348 (defun widget-checklist-value-get (widget)
2349 ;; The values of all selected items.
2350 (let ((children (widget-get widget :children))
2353 (setq child (car children)
2354 children (cdr children))
2355 (if (widget-value (widget-get child :button))
2356 (setq result (append result (widget-apply child :value-inline)))))
2359 (defun widget-checklist-validate (widget)
2360 ;; Ticked children must be valid.
2361 (let ((children (widget-get widget :children))
2363 (while (and children (not found))
2364 (setq child (car children)
2365 children (cdr children)
2366 button (widget-get child :button)
2367 found (and (widget-value button)
2368 (widget-apply child :validate))))
2371 ;;; The `option' Widget
2373 (define-widget 'option 'checklist
2374 "An widget with an optional item."
2377 ;;; The `choice-item' Widget.
2379 (define-widget 'choice-item 'item
2380 "Button items that delegate action events to their parents."
2381 :action 'widget-parent-action
2382 :format "%[%t%] \n")
2384 ;;; The `radio-button' Widget.
2386 (define-widget 'radio-button 'toggle
2387 "A radio button for use in the `radio' widget."
2388 :notify 'widget-radio-button-notify
2395 :off-glyph "radio0")
2397 (defun widget-radio-button-notify (widget _child &optional event)
2399 (widget-apply (widget-get widget :parent) :action widget event))
2401 ;;; The `radio-button-choice' Widget.
2403 (define-widget 'radio-button-choice 'default
2404 "Select one of multiple options."
2405 :convert-widget 'widget-types-convert-widget
2406 :copy 'widget-types-copy
2409 :entry-format "%b %v"
2410 :value-create 'widget-radio-value-create
2411 :value-get 'widget-radio-value-get
2412 :value-inline 'widget-radio-value-inline
2413 :value-set 'widget-radio-value-set
2414 :error "You must push one of the buttons"
2415 :validate 'widget-radio-validate
2416 :match 'widget-choice-match
2417 :match-inline 'widget-choice-match-inline
2418 :action 'widget-radio-action)
2420 (defun widget-radio-value-create (widget)
2421 ;; Insert all values
2422 (let ((args (widget-get widget :args))
2425 (setq arg (car args)
2427 (widget-radio-add-item widget arg))))
2429 (defun widget-radio-add-item (widget type)
2430 "Add to radio widget WIDGET a new radio button item of type TYPE."
2431 ;; (setq type (widget-convert type))
2432 (and (eq (preceding-char) ?\n)
2433 (widget-get widget :indent)
2434 (insert-char ?\s (widget-get widget :indent)))
2435 (widget-specify-insert
2436 (let* ((value (widget-get widget :value))
2437 (children (widget-get widget :children))
2438 (buttons (widget-get widget :buttons))
2439 (button-args (or (widget-get type :sibling-args)
2440 (widget-get widget :button-args)))
2442 (chosen (and (null (widget-get widget :choice))
2443 (widget-apply type :match value)))
2445 (insert (widget-get widget :entry-format))
2447 ;; Parse % escapes in format.
2448 (while (re-search-forward "%\\([bv%]\\)" nil t)
2449 (let ((escape (char-after (match-beginning 1))))
2451 (cond ((eq escape ?%)
2454 (setq button (apply 'widget-create-child-and-convert
2455 widget 'radio-button
2456 :value (not (null chosen))
2459 (setq child (if chosen
2460 (widget-create-child-value
2462 (widget-create-child widget type)))
2464 (widget-apply child :deactivate)))
2466 (error "Unknown escape `%c'" escape)))))
2467 ;; Update properties.
2469 (widget-put widget :choice type))
2471 (widget-put child :button button)
2472 (widget-put widget :buttons (nconc buttons (list button))))
2474 (widget-put widget :children (nconc children (list child))))
2477 (defun widget-radio-value-get (widget)
2478 ;; Get value of the child widget.
2479 (let ((chosen (widget-radio-chosen widget)))
2480 (and chosen (widget-value chosen))))
2482 (defun widget-radio-chosen (widget)
2483 "Return the widget representing the chosen radio button."
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)
2494 (defun widget-radio-value-inline (widget)
2495 ;; Get value of the child widget.
2496 (let ((children (widget-get widget :children))
2499 (setq current (car children)
2500 children (cdr children))
2501 (when (widget-apply (widget-get current :button) :value-get)
2502 (setq found (widget-apply current :value-inline)
2506 (defun widget-radio-value-set (widget value)
2507 ;; We can't just delete and recreate a radio widget, since children
2508 ;; can be added after the original creation and won't be recreated
2510 (let ((children (widget-get widget :children))
2513 (setq current (car children)
2514 children (cdr children))
2515 (let* ((button (widget-get current :button))
2516 (match (and (not found)
2517 (widget-apply current :match value))))
2518 (widget-value-set button match)
2521 (widget-value-set current value)
2522 (widget-apply current :activate))
2523 (widget-apply current :deactivate))
2524 (setq found (or found match))))))
2526 (defun widget-radio-validate (widget)
2527 ;; Valid if we have made a valid choice.
2528 (let ((children (widget-get widget :children))
2529 current found button)
2530 (while (and children (not found))
2531 (setq current (car children)
2532 children (cdr children)
2533 button (widget-get current :button)
2534 found (widget-apply button :value-get)))
2536 (widget-apply current :validate)
2539 (defun widget-radio-action (widget child event)
2540 ;; Check if a radio button was pressed.
2541 (let ((children (widget-get widget :children))
2542 (buttons (widget-get widget :buttons))
2544 (when (memq child buttons)
2546 (setq current (car children)
2547 children (cdr children))
2548 (let* ((button (widget-get current :button)))
2549 (cond ((eq child button)
2550 (widget-value-set button t)
2551 (widget-apply current :activate))
2552 ((widget-value button)
2553 (widget-value-set button nil)
2554 (widget-apply current :deactivate)))))))
2555 ;; Pass notification to parent.
2556 (widget-apply widget :notify child event))
2558 ;;; The `insert-button' Widget.
2560 (define-widget 'insert-button 'push-button
2561 "An insert button for the `editable-list' widget."
2563 :help-echo "Insert a new item into the list at this position."
2564 :action 'widget-insert-button-action)
2566 (defun widget-insert-button-action (widget &optional _event)
2567 ;; Ask the parent to insert a new item.
2568 (widget-apply (widget-get widget :parent)
2569 :insert-before (widget-get widget :widget)))
2571 ;;; The `delete-button' Widget.
2573 (define-widget 'delete-button 'push-button
2574 "A delete button for the `editable-list' widget."
2576 :help-echo "Delete this item from the list."
2577 :action 'widget-delete-button-action)
2579 (defun widget-delete-button-action (widget &optional _event)
2580 ;; Ask the parent to insert a new item.
2581 (widget-apply (widget-get widget :parent)
2582 :delete-at (widget-get widget :widget)))
2584 ;;; The `editable-list' Widget.
2586 ;; (defcustom widget-editable-list-gui nil
2587 ;; "If non-nil, use GUI push-buttons in editable list when available."
2591 (define-widget 'editable-list 'default
2592 "A variable list of widgets of the same type."
2593 :convert-widget 'widget-types-convert-widget
2594 :copy 'widget-types-copy
2597 :format-handler 'widget-editable-list-format-handler
2598 :entry-format "%i %d %v"
2599 :value-create 'widget-editable-list-value-create
2600 :value-get 'widget-editable-list-value-get
2601 :validate 'widget-children-validate
2602 :match 'widget-editable-list-match
2603 :match-inline 'widget-editable-list-match-inline
2604 :insert-before 'widget-editable-list-insert-before
2605 :delete-at 'widget-editable-list-delete-at)
2607 (defun widget-editable-list-format-handler (widget escape)
2608 ;; We recognize the insert button.
2609 ;; (let ((widget-push-button-gui widget-editable-list-gui))
2610 (cond ((eq escape ?i)
2611 (and (widget-get widget :indent)
2612 (insert-char ?\s (widget-get widget :indent)))
2613 (apply 'widget-create-child-and-convert
2614 widget 'insert-button
2615 (widget-get widget :append-button-args)))
2617 (widget-default-format-handler widget escape)))
2621 (defun widget-editable-list-value-create (widget)
2622 ;; Insert all values
2623 (let* ((value (widget-get widget :value))
2624 (type (nth 0 (widget-get widget :args)))
2626 (widget-put widget :value-pos (copy-marker (point)))
2627 (set-marker-insertion-type (widget-get widget :value-pos) t)
2629 (let ((answer (widget-match-inline type value)))
2631 (setq children (cons (widget-editable-list-entry-create
2633 (if (widget-get type :inline)
2640 (widget-put widget :children (nreverse children))))
2642 (defun widget-editable-list-value-get (widget)
2643 ;; Get value of the child widget.
2644 (apply 'append (mapcar (lambda (child) (widget-apply child :value-inline))
2645 (widget-get widget :children))))
2647 (defun widget-editable-list-match (widget value)
2648 ;; Value must be a list and all the members must match the type.
2650 (null (cdr (widget-editable-list-match-inline widget value)))))
2652 (defun widget-editable-list-match-inline (widget value)
2653 (let ((type (nth 0 (widget-get widget :args)))
2656 (while (and value ok)
2657 (let ((answer (widget-match-inline type value)))
2659 (setq found (append found (car answer))
2662 (cons found value)))
2664 (defun widget-editable-list-insert-before (widget before)
2665 ;; Insert a new child in the list of children.
2667 (let ((children (widget-get widget :children))
2668 (inhibit-read-only t)
2669 before-change-functions
2670 after-change-functions)
2672 (goto-char (widget-get before :entry-from)))
2674 (goto-char (widget-get widget :value-pos))))
2675 (let ((child (widget-editable-list-entry-create
2677 (when (< (widget-get child :entry-from) (widget-get widget :from))
2678 (set-marker (widget-get widget :from)
2679 (widget-get child :entry-from)))
2680 (if (eq (car children) before)
2681 (widget-put widget :children (cons child children))
2682 (while (not (eq (car (cdr children)) before))
2683 (setq children (cdr children)))
2684 (setcdr children (cons child (cdr children)))))))
2686 (widget-apply widget :notify widget))
2688 (defun widget-editable-list-delete-at (widget child)
2689 ;; Delete child from list of children.
2691 (let ((buttons (copy-sequence (widget-get widget :buttons)))
2693 (inhibit-read-only t)
2694 before-change-functions
2695 after-change-functions)
2697 (setq button (car buttons)
2698 buttons (cdr buttons))
2699 (when (eq (widget-get button :widget) child)
2701 :buttons (delq button (widget-get widget :buttons)))
2702 (widget-delete button))))
2703 (let ((entry-from (widget-get child :entry-from))
2704 (entry-to (widget-get child :entry-to))
2705 (inhibit-read-only t)
2706 before-change-functions
2707 after-change-functions)
2708 (widget-delete child)
2709 (delete-region entry-from entry-to)
2710 (set-marker entry-from nil)
2711 (set-marker entry-to nil))
2712 (widget-put widget :children (delq child (widget-get widget :children))))
2714 (widget-apply widget :notify widget))
2716 (defun widget-editable-list-entry-create (widget value conv)
2717 ;; Create a new entry to the list.
2718 (let ((type (nth 0 (widget-get widget :args)))
2719 ;; (widget-push-button-gui widget-editable-list-gui)
2720 child delete insert)
2721 (widget-specify-insert
2723 (and (widget-get widget :indent)
2724 (insert-char ?\s (widget-get widget :indent)))
2725 (insert (widget-get widget :entry-format)))
2726 ;; Parse % escapes in format.
2727 (while (re-search-forward "%\\(.\\)" nil t)
2728 (let ((escape (char-after (match-beginning 1))))
2730 (cond ((eq escape ?%)
2733 (setq insert (apply 'widget-create-child-and-convert
2734 widget 'insert-button
2735 (widget-get widget :insert-button-args))))
2737 (setq delete (apply 'widget-create-child-and-convert
2738 widget 'delete-button
2739 (widget-get widget :delete-button-args))))
2742 (setq child (widget-create-child-value
2744 (setq child (widget-create-child-value
2745 widget type (widget-default-get type)))))
2747 (error "Unknown escape `%c'" escape)))))
2748 (let ((buttons (widget-get widget :buttons)))
2749 (if insert (push insert buttons))
2750 (if delete (push delete buttons))
2751 (widget-put widget :buttons buttons))
2752 (let ((entry-from (point-min-marker))
2753 (entry-to (point-max-marker)))
2754 (set-marker-insertion-type entry-from t)
2755 (set-marker-insertion-type entry-to nil)
2756 (widget-put child :entry-from entry-from)
2757 (widget-put child :entry-to entry-to)))
2758 (if insert (widget-put insert :widget child))
2759 (if delete (widget-put delete :widget child))
2762 ;;; The `group' Widget.
2764 (define-widget 'group 'default
2765 "A widget which groups other widgets inside."
2766 :convert-widget 'widget-types-convert-widget
2767 :copy 'widget-types-copy
2769 :value-create 'widget-group-value-create
2770 :value-get 'widget-editable-list-value-get
2771 :default-get 'widget-group-default-get
2772 :validate 'widget-children-validate
2773 :match 'widget-group-match
2774 :match-inline 'widget-group-match-inline)
2776 (defun widget-group-value-create (widget)
2777 ;; Create each component.
2778 (let ((args (widget-get widget :args))
2779 (value (widget-get widget :value))
2780 arg answer children)
2782 (setq arg (car args)
2784 answer (widget-match-inline arg value)
2786 (and (eq (preceding-char) ?\n)
2787 (widget-get widget :indent)
2788 (insert-char ?\s (widget-get widget :indent)))
2789 (push (cond ((null answer)
2790 (widget-create-child widget arg))
2791 ((widget-get arg :inline)
2792 (widget-create-child-value widget arg (car answer)))
2794 (widget-create-child-value widget arg (car (car answer)))))
2796 (widget-put widget :children (nreverse children))))
2798 (defun widget-group-default-get (widget)
2799 ;; Get the default of the components.
2800 (mapcar 'widget-default-get (widget-get widget :args)))
2802 (defun widget-group-match (widget vals)
2803 ;; Match if the components match.
2805 (let ((match (widget-group-match-inline widget vals)))
2806 (and match (null (cdr match))))))
2808 (defun widget-group-match-inline (widget vals)
2809 ;; Match if the components match.
2810 (let ((args (widget-get widget :args))
2811 argument answer found)
2813 (setq argument (car args)
2815 (if (setq answer (widget-match-inline argument vals))
2816 (setq found (append found (car answer))
2821 (cons found vals))))
2823 ;;; The `visibility' Widget.
2825 (define-widget 'visibility 'item
2826 "An indicator and manipulator for hidden items.
2828 The following properties have special meanings for this widget:
2829 :on-glyph Image filename or spec to display when the item is visible.
2830 :on Text shown if the \"on\" image is nil or cannot be displayed.
2831 :off-glyph Image filename or spec to display when the item is hidden.
2832 :off Text shown if the \"off\" image is nil cannot be displayed."
2840 :value-create 'widget-visibility-value-create
2841 :action 'widget-toggle-action
2842 :match (lambda (_widget _value) t))
2844 (defalias 'widget-visibility-value-create 'widget-toggle-value-create)
2846 ;;; The `documentation-link' Widget.
2848 ;; This is a helper widget for `documentation-string'.
2850 (define-widget 'documentation-link 'link
2851 "Link type used in documentation strings."
2853 :help-echo "Describe this symbol"
2854 :action 'widget-documentation-link-action)
2856 (defun widget-documentation-link-action (widget &optional _event)
2857 "Display documentation for WIDGET's value. Ignore optional argument EVENT."
2858 (let* ((string (widget-get widget :value))
2859 (symbol (intern string)))
2860 (if (and (fboundp symbol) (boundp symbol))
2861 ;; If there are two doc strings, give the user a way to pick one.
2862 (apropos (concat "\\`" (regexp-quote string) "\\'"))
2863 (if (fboundp symbol)
2864 (describe-function symbol)
2865 (describe-variable symbol)))))
2867 (defcustom widget-documentation-links t
2868 "Add hyperlinks to documentation strings when non-nil."
2870 :group 'widget-documentation)
2872 (defcustom widget-documentation-link-regexp "`\\([^\n`' ]+\\)'"
2873 "Regexp for matching potential links in documentation strings.
2874 The first group should be the link itself."
2876 :group 'widget-documentation)
2878 (defcustom widget-documentation-link-p 'intern-soft
2879 "Predicate used to test if a string is useful as a link.
2880 The value should be a function. The function will be called with one
2881 argument, a string, and should return non-nil if there should be a
2882 link for that string."
2884 :options '(widget-documentation-link-p)
2885 :group 'widget-documentation)
2887 (defcustom widget-documentation-link-type 'documentation-link
2888 "Widget type used for links in documentation strings."
2890 :group 'widget-documentation)
2892 (defun widget-documentation-link-add (widget from to)
2893 (widget-specify-doc widget from to)
2894 (when widget-documentation-links
2895 (let ((regexp widget-documentation-link-regexp)
2896 (buttons (widget-get widget :buttons))
2897 (widget-mouse-face (default-value 'widget-mouse-face))
2898 (widget-button-face widget-documentation-face)
2899 (widget-button-pressed-face widget-documentation-face))
2902 (while (re-search-forward regexp to t)
2903 (let ((name (match-string 1))
2904 (begin (match-beginning 1))
2905 (end (match-end 1)))
2906 (when (funcall widget-documentation-link-p name)
2907 (push (widget-convert-button widget-documentation-link-type
2908 begin end :value name)
2910 (widget-put widget :buttons buttons))))
2912 ;;; The `documentation-string' Widget.
2914 (define-widget 'documentation-string 'item
2915 "A documentation string."
2917 :action 'widget-documentation-string-action
2918 :value-create 'widget-documentation-string-value-create
2919 :visibility-widget 'visibility)
2921 (defun widget-documentation-string-value-create (widget)
2922 ;; Insert documentation string.
2923 (let ((doc (widget-value widget))
2924 (indent (widget-get widget :indent))
2925 (shown (widget-get (widget-get widget :parent) :documentation-shown))
2927 (if (string-match "\n" doc)
2928 (let ((before (substring doc 0 (match-beginning 0)))
2929 (after (substring doc (match-end 0)))
2931 (widget-documentation-string-indent-to indent)
2933 (widget-documentation-link-add widget start (point))
2935 (widget-create-child-and-convert
2936 widget (widget-get widget :visibility-widget)
2937 :help-echo "Show or hide rest of the documentation."
2941 :action 'widget-parent-action
2945 (setq start (point))
2946 (when (and indent (not (zerop indent)))
2947 (insert-char ?\s indent))
2950 (widget-documentation-link-add widget start end)
2951 ;; Indent the subsequent lines.
2952 (when (and indent (> indent 0))
2955 (narrow-to-region start end)
2956 (goto-char (point-min))
2957 (while (search-forward "\n" nil t)
2958 (widget-documentation-string-indent-to indent))))))
2959 (widget-put widget :buttons (list button)))
2960 (widget-documentation-string-indent-to indent)
2962 (widget-documentation-link-add widget start (point))))
2965 (defun widget-documentation-string-indent-to (col)
2966 (when (and (numberp col)
2968 (let ((opoint (point)))
2970 (put-text-property opoint (point)
2971 'display `(space :align-to ,col)))))
2973 (defun widget-documentation-string-action (widget &rest _ignore)
2974 ;; Toggle documentation.
2975 (let ((parent (widget-get widget :parent)))
2976 (widget-put parent :documentation-shown
2977 (not (widget-get parent :documentation-shown))))
2979 (widget-value-set widget (widget-value widget)))
2981 (defun widget-add-documentation-string-button (widget &rest args)
2982 "Insert a new `documentation-string' widget based on WIDGET.
2983 The new widget becomes a child of WIDGET, and is also added to
2984 its `:buttons' list. The documentation string is found from
2985 WIDGET using the function `widget-docstring'.
2986 Optional ARGS specifies additional keyword arguments for the
2987 `documentation-string' widget."
2988 (let ((doc (widget-docstring widget))
2989 (indent (widget-get widget :indent))
2990 (doc-indent (widget-get widget :documentation-indent)))
2992 (and (eq (preceding-char) ?\n)
2994 (insert-char ?\s indent))
2995 (unless (or (numberp doc-indent) (null doc-indent))
2996 (setq doc-indent 0))
2997 (widget-put widget :buttons
2998 (cons (apply 'widget-create-child-and-convert
2999 widget 'documentation-string
3001 (nconc args (list doc)))
3002 (widget-get widget :buttons))))))
3004 ;;; The Sexp Widgets.
3006 (define-widget 'const 'item
3007 "An immutable sexp."
3008 :prompt-value 'widget-const-prompt-value
3011 (defun widget-const-prompt-value (widget _prompt _value _unbound)
3012 ;; Return the value of the const.
3013 (widget-value widget))
3015 (define-widget 'function-item 'const
3016 "An immutable function name."
3018 :documentation-property (lambda (symbol)
3020 (documentation symbol t)
3023 (define-widget 'variable-item 'const
3024 "An immutable variable name."
3026 :documentation-property 'variable-documentation)
3028 (define-widget 'other 'sexp
3029 "Matches any value, but doesn't let the user edit the value.
3030 This is useful as last item in a `choice' widget.
3031 You should use this widget type with a default value,
3032 as in (other DEFAULT) or (other :tag \"NAME\" DEFAULT).
3033 If the user selects this alternative, that specifies DEFAULT
3039 (defvar widget-string-prompt-value-history nil
3040 "History of input to `widget-string-prompt-value'.")
3042 (define-widget 'string 'editable-field
3045 :format "%{%t%}: %v"
3046 :complete-function 'ispell-complete-word
3047 :prompt-history 'widget-string-prompt-value-history)
3049 (define-widget 'regexp 'string
3050 "A regular expression."
3051 :match 'widget-regexp-match
3052 :validate 'widget-regexp-validate
3053 ;; Doesn't work well with terminating newline.
3054 ;; :value-face 'widget-single-line-field
3057 (defun widget-regexp-match (_widget value)
3058 ;; Match valid regexps.
3059 (and (stringp value)
3062 (string-match value ""))
3065 (defun widget-regexp-validate (widget)
3066 "Check that the value of WIDGET is a valid regexp."
3067 (condition-case data
3069 (string-match (widget-value widget) ""))
3070 (error (widget-put widget :error (error-message-string data))
3073 (define-widget 'file 'string
3075 It reads a file name from an editable text field."
3076 :completions #'completion-file-name-table
3077 :prompt-value 'widget-file-prompt-value
3078 :format "%{%t%}: %v"
3079 ;; Doesn't work well with terminating newline.
3080 ;; :value-face 'widget-single-line-field
3083 (defun widget-file-prompt-value (widget prompt value unbound)
3084 ;; Read file from minibuffer.
3085 (abbreviate-file-name
3087 (read-file-name prompt)
3088 (let ((prompt2 (format "%s (default %s): " prompt value))
3089 (dir (file-name-directory value))
3090 (file (file-name-nondirectory value))
3091 (must-match (widget-get widget :must-match)))
3092 (read-file-name prompt2 dir nil must-match file)))))
3094 ;;;(defun widget-file-action (widget &optional event)
3095 ;;; ;; Read a file name from the minibuffer.
3096 ;;; (let* ((value (widget-value widget))
3097 ;;; (dir (file-name-directory value))
3098 ;;; (file (file-name-nondirectory value))
3099 ;;; (menu-tag (widget-apply widget :menu-tag-get))
3100 ;;; (must-match (widget-get widget :must-match))
3101 ;;; (answer (read-file-name (concat menu-tag " (default " value "): ")
3102 ;;; dir nil must-match file)))
3103 ;;; (widget-value-set widget (abbreviate-file-name answer))
3105 ;;; (widget-apply widget :notify widget event)))
3107 ;; Fixme: use file-name-as-directory.
3108 (define-widget 'directory 'file
3109 "A directory widget.
3110 It reads a directory name from an editable text field."
3113 (defvar widget-symbol-prompt-value-history nil
3114 "History of input to `widget-symbol-prompt-value'.")
3116 (define-widget 'symbol 'editable-field
3120 :format "%{%t%}: %v"
3121 :match (lambda (_widget value) (symbolp value))
3122 :completions obarray
3123 :prompt-internal 'widget-symbol-prompt-internal
3124 :prompt-match 'symbolp
3125 :prompt-history 'widget-symbol-prompt-value-history
3126 :value-to-internal (lambda (_widget value)
3130 :value-to-external (lambda (_widget value)
3135 (defun widget-symbol-prompt-internal (widget prompt initial history)
3136 ;; Read file from minibuffer.
3137 (let ((answer (completing-read prompt obarray
3138 (widget-get widget :prompt-match)
3139 nil initial history)))
3140 (if (and (stringp answer)
3141 (not (zerop (length answer))))
3143 (error "No value"))))
3145 (defvar widget-function-prompt-value-history nil
3146 "History of input to `widget-function-prompt-value'.")
3148 (define-widget 'function 'restricted-sexp
3150 :completions (apply-partially #'completion-table-with-predicate
3151 obarray #'fboundp 'strict)
3152 :prompt-value 'widget-field-prompt-value
3153 :prompt-internal 'widget-symbol-prompt-internal
3154 :prompt-match 'fboundp
3155 :prompt-history 'widget-function-prompt-value-history
3156 :action 'widget-field-action
3157 :match-alternatives '(functionp)
3158 :validate (lambda (widget)
3159 (unless (functionp (widget-value widget))
3160 (widget-put widget :error (format "Invalid function: %S"
3161 (widget-value widget)))
3166 (defvar widget-variable-prompt-value-history nil
3167 "History of input to `widget-variable-prompt-value'.")
3169 (define-widget 'variable 'symbol
3171 :prompt-match 'boundp
3172 :prompt-history 'widget-variable-prompt-value-history
3173 :completions (apply-partially #'completion-table-with-predicate
3174 obarray #'boundp 'strict)
3177 (define-widget 'coding-system 'symbol
3178 "A MULE coding-system."
3179 :format "%{%t%}: %v"
3180 :tag "Coding system"
3182 :prompt-history 'coding-system-value-history
3183 :prompt-value 'widget-coding-system-prompt-value
3184 :action 'widget-coding-system-action
3185 :completions (apply-partially #'completion-table-with-predicate
3186 obarray #'coding-system-p 'strict)
3187 :validate (lambda (widget)
3188 (unless (coding-system-p (widget-value widget))
3189 (widget-put widget :error (format "Invalid coding system: %S"
3190 (widget-value widget)))
3193 :prompt-match 'coding-system-p)
3195 (defun widget-coding-system-prompt-value (widget prompt value _unbound)
3196 "Read coding-system from minibuffer."
3197 (if (widget-get widget :base-only)
3199 (completing-read (format "%s (default %s): " prompt value)
3200 (mapcar #'list (coding-system-list t)) nil nil nil
3201 coding-system-history))
3202 (read-coding-system (format "%s (default %s): " prompt value) value)))
3204 (defun widget-coding-system-action (widget &optional event)
3206 (widget-coding-system-prompt-value
3208 (widget-apply widget :menu-tag-get)
3209 (widget-value widget)
3211 (widget-value-set widget answer)
3212 (widget-apply widget :notify widget event)
3215 ;;; I'm not sure about what this is good for? KFS.
3216 (defvar widget-key-sequence-prompt-value-history nil
3217 "History of input to `widget-key-sequence-prompt-value'.")
3219 (defvar widget-key-sequence-default-value [ignore]
3220 "Default value for an empty key sequence.")
3222 (defvar widget-key-sequence-map
3223 (let ((map (make-sparse-keymap)))
3224 (set-keymap-parent map widget-field-keymap)
3225 (define-key map [(control ?q)] 'widget-key-sequence-read-event)
3228 (define-widget 'key-sequence 'restricted-sexp
3230 :prompt-value 'widget-field-prompt-value
3231 :prompt-internal 'widget-symbol-prompt-internal
3232 ; :prompt-match 'fboundp ;; What was this good for? KFS
3233 :prompt-history 'widget-key-sequence-prompt-value-history
3234 :action 'widget-field-action
3235 :match-alternatives '(stringp vectorp)
3236 :format "%{%t%}: %v"
3237 :validate 'widget-key-sequence-validate
3238 :value-to-internal 'widget-key-sequence-value-to-internal
3239 :value-to-external 'widget-key-sequence-value-to-external
3240 :value widget-key-sequence-default-value
3241 :keymap widget-key-sequence-map
3242 :help-echo "C-q: insert KEY, EVENT, or CODE; RET: enter value"
3243 :tag "Key sequence")
3245 (defun widget-key-sequence-read-event (ev)
3247 (let ((inhibit-quit t) quit-flag)
3248 (read-event "Insert KEY, EVENT, or CODE: "))))
3249 (let ((ev2 (and (memq 'down (event-modifiers ev))
3251 (tr (and (keymapp function-key-map)
3252 (lookup-key function-key-map (vector ev)))))
3253 (when (and (integerp ev)
3254 (or (and (<= ?0 ev) (< ev (+ ?0 (min 10 read-quoted-char-radix))))
3255 (and (<= ?a (downcase ev))
3256 (< (downcase ev) (+ ?a -10 (min 36 read-quoted-char-radix))))))
3257 (setq unread-command-events (cons ev unread-command-events)
3258 ev (read-quoted-char (format "Enter code (radix %d)" read-quoted-char-radix))
3260 (if (and (integerp ev) (not (characterp ev)))
3261 (insert (char-to-string ev)))) ;; throw invalid char error
3262 (setq ev (key-description (list ev)))
3264 (setq tr (key-description (list (aref tr 0))))
3265 (if (y-or-n-p (format "Key %s is translated to %s -- use %s? " ev tr tr))
3266 (setq ev tr ev2 nil)))
3267 (insert (if (= (char-before) ?\s) "" " ") ev " ")
3269 (insert (key-description (list ev2)) " "))))
3271 (defun widget-key-sequence-validate (widget)
3272 (unless (or (stringp (widget-value widget))
3273 (vectorp (widget-value widget)))
3274 (widget-put widget :error (format "Invalid key sequence: %S"
3275 (widget-value widget)))
3278 (defun widget-key-sequence-value-to-internal (widget value)
3279 (if (widget-apply widget :match value)
3280 (if (equal value widget-key-sequence-default-value)
3282 (key-description value))
3285 (defun widget-key-sequence-value-to-external (_widget value)
3287 (if (string-match "\\`[[:space:]]*\\'" value)
3288 widget-key-sequence-default-value
3289 (read-kbd-macro value))
3293 (define-widget 'sexp 'editable-field
3294 "An arbitrary Lisp expression."
3295 :tag "Lisp expression"
3296 :format "%{%t%}: %v"
3298 :validate 'widget-sexp-validate
3299 :match (lambda (_widget _value) t)
3300 :value-to-internal 'widget-sexp-value-to-internal
3301 :value-to-external (lambda (_widget value) (read value))
3302 :prompt-history 'widget-sexp-prompt-value-history
3303 :prompt-value 'widget-sexp-prompt-value)
3305 (defun widget-sexp-value-to-internal (_widget value)
3306 ;; Use pp for printer representation.
3307 (let ((pp (if (symbolp value)
3308 (prin1-to-string value)
3309 (pp-to-string value))))
3310 (while (string-match "\n\\'" pp)
3311 (setq pp (substring pp 0 -1)))
3312 (if (or (string-match "\n\\'" pp)
3317 (defun widget-sexp-validate (widget)
3318 ;; Valid if we can read the string and there is no junk left after it.
3320 (insert (widget-apply widget :value-get))
3321 (goto-char (point-min))
3323 (condition-case data ;Note: We get a spurious byte-compile warning here.
3325 ;; Avoid a confusing end-of-file error.
3326 (skip-syntax-forward "\\s-")
3328 (setq err "Empty sexp -- use `nil'?")
3329 (unless (widget-apply widget :match (read (current-buffer)))
3330 (setq err (widget-get widget :type-error))))
3331 ;; Allow whitespace after expression.
3332 (skip-syntax-forward "\\s-")
3333 (if (and (not (eobp))
3335 (setq err (format "Junk at end of expression: %s"
3336 (buffer-substring (point)
3338 (end-of-file ; Avoid confusing error message.
3339 (setq err "Unbalanced sexp"))
3340 (error (setq err (error-message-string data))))
3343 (widget-put widget :error err)
3346 (defvar widget-sexp-prompt-value-history nil
3347 "History of input to `widget-sexp-prompt-value'.")
3349 (defun widget-sexp-prompt-value (widget prompt value unbound)
3350 ;; Read an arbitrary sexp.
3351 (let ((found (read-string prompt
3352 (if unbound nil (cons (prin1-to-string value) 0))
3353 (widget-get widget :prompt-history))))
3354 (let ((answer (read-from-string found)))
3355 (unless (= (cdr answer) (length found))
3356 (error "Junk at end of expression: %s"
3357 (substring found (cdr answer))))
3360 (define-widget 'restricted-sexp 'sexp
3361 "A Lisp expression restricted to values that match.
3362 To use this type, you must define :match or :match-alternatives."
3363 :type-error "The specified value is not valid"
3364 :match 'widget-restricted-sexp-match
3365 :value-to-internal (lambda (widget value)
3366 (if (widget-apply widget :match value)
3367 (prin1-to-string value)
3370 (defun widget-restricted-sexp-match (widget value)
3371 (let ((alternatives (widget-get widget :match-alternatives))
3373 (while (and alternatives (not matched))
3374 (if (cond ((functionp (car alternatives))
3375 (funcall (car alternatives) value))
3376 ((and (consp (car alternatives))
3377 (eq (car (car alternatives)) 'quote))
3378 (eq value (nth 1 (car alternatives)))))
3380 (setq alternatives (cdr alternatives)))
3383 (define-widget 'integer 'restricted-sexp
3387 :type-error "This field should contain an integer"
3388 :match-alternatives '(integerp))
3390 (define-widget 'number 'restricted-sexp
3391 "A number (floating point or integer)."
3394 :type-error "This field should contain a number (floating point or integer)"
3395 :match-alternatives '(numberp))
3397 (define-widget 'float 'restricted-sexp
3398 "A floating point number."
3399 :tag "Floating point number"
3401 :type-error "This field should contain a floating point number"
3402 :match-alternatives '(floatp))
3404 (define-widget 'character 'editable-field
3409 :format "%{%t%}: %v\n"
3410 :valid-regexp "\\`.\\'"
3411 :error "This field should contain a single character"
3412 :value-get (lambda (w) (widget-field-value-get w t))
3413 :value-to-internal (lambda (_widget value)
3416 (char-to-string value)))
3417 :value-to-external (lambda (_widget value)
3421 :match (lambda (_widget value)
3422 (characterp value)))
3424 (define-widget 'list 'group
3427 :format "%{%t%}:\n%v")
3429 (define-widget 'vector 'group
3432 :format "%{%t%}:\n%v"
3433 :match 'widget-vector-match
3434 :value-to-internal (lambda (_widget value) (append value nil))
3435 :value-to-external (lambda (_widget value) (apply 'vector value)))
3437 (defun widget-vector-match (widget value)
3438 (and (vectorp value)
3439 (widget-group-match widget
3440 (widget-apply widget :value-to-internal value))))
3442 (define-widget 'cons 'group
3445 :format "%{%t%}:\n%v"
3446 :match 'widget-cons-match
3447 :value-to-internal (lambda (_widget value)
3448 (list (car value) (cdr value)))
3449 :value-to-external (lambda (_widget value)
3450 (apply 'cons value)))
3452 (defun widget-cons-match (widget value)
3454 (widget-group-match widget
3455 (widget-apply widget :value-to-internal value))))
3457 ;;; The `lazy' Widget.
3459 ;; Recursive datatypes.
3461 (define-widget 'lazy 'default
3462 "Base widget for recursive datastructures.
3464 The `lazy' widget will, when instantiated, contain a single inferior
3465 widget, of the widget type specified by the :type parameter. The
3466 value of the `lazy' widget is the same as the value of the inferior
3467 widget. When deriving a new widget from the 'lazy' widget, the :type
3468 parameter is allowed to refer to the widget currently being defined,
3469 thus allowing recursive datastructures to be described.
3471 The :type parameter takes the same arguments as the defcustom
3472 parameter with the same name.
3474 Most composite widgets, i.e. widgets containing other widgets, does
3475 not allow recursion. That is, when you define a new widget type, none
3476 of the inferior widgets may be of the same type you are currently
3479 In Lisp, however, it is custom to define datastructures in terms of
3480 themselves. A list, for example, is defined as either nil, or a cons
3481 cell whose cdr itself is a list. The obvious way to translate this
3482 into a widget type would be
3484 (define-widget 'my-list 'choice
3485 \"A list of sexps.\"
3487 :args '((const nil) (cons :value (nil) sexp my-list)))
3489 Here we attempt to define my-list as a choice of either the constant
3490 nil, or a cons-cell containing a sexp and my-lisp. This will not work
3491 because the `choice' widget does not allow recursion.
3493 Using the `lazy' widget you can overcome this problem, as in this
3496 (define-widget 'sexp-list 'lazy
3497 \"A list of sexps.\"
3499 :type '(choice (const nil) (cons :value (nil) sexp sexp-list)))"
3500 :format "%{%t%}: %v"
3501 ;; We don't convert :type because we want to allow recursive
3502 ;; datastructures. This is slow, so we should not create speed
3503 ;; critical widgets by deriving from this.
3504 :convert-widget 'widget-value-convert-widget
3505 :value-create 'widget-type-value-create
3506 :value-get 'widget-child-value-get
3507 :value-inline 'widget-child-value-inline
3508 :default-get 'widget-type-default-get
3509 :match 'widget-type-match
3510 :validate 'widget-child-validate)
3513 ;;; The `plist' Widget.
3517 (define-widget 'plist 'list
3519 :key-type '(symbol :tag "Key")
3520 :value-type '(sexp :tag "Value")
3521 :convert-widget 'widget-plist-convert-widget
3524 (defvar widget-plist-value-type) ;Dynamic variable
3526 (defun widget-plist-convert-widget (widget)
3527 ;; Handle `:options'.
3528 (let* ((options (widget-get widget :options))
3529 (widget-plist-value-type (widget-get widget :value-type))
3530 (other `(editable-list :inline t
3532 ,(widget-get widget :key-type)
3533 ,widget-plist-value-type)))
3535 (list `(checklist :inline t
3537 ,@(mapcar 'widget-plist-convert-option
3541 (widget-put widget :args args)
3544 (defun widget-plist-convert-option (option)
3545 ;; Convert a single plist option.
3546 (let (key-type value-type)
3548 (let ((key (nth 0 option)))
3549 (setq value-type (nth 1 option))
3552 (setq key-type `(const ,key))))
3553 (setq key-type `(const ,option)
3554 value-type widget-plist-value-type))
3555 `(group :format "Key: %v" :inline t ,key-type ,value-type)))
3558 ;;; The `alist' Widget.
3560 ;; Association lists.
3562 (define-widget 'alist 'list
3563 "An association list."
3564 :key-type '(sexp :tag "Key")
3565 :value-type '(sexp :tag "Value")
3566 :convert-widget 'widget-alist-convert-widget
3569 (defvar widget-alist-value-type) ;Dynamic variable
3571 (defun widget-alist-convert-widget (widget)
3572 ;; Handle `:options'.
3573 (let* ((options (widget-get widget :options))
3574 (widget-alist-value-type (widget-get widget :value-type))
3575 (other `(editable-list :inline t
3577 ,(widget-get widget :key-type)
3578 ,widget-alist-value-type)))
3580 (list `(checklist :inline t
3582 ,@(mapcar 'widget-alist-convert-option
3586 (widget-put widget :args args)
3589 (defun widget-alist-convert-option (option)
3590 ;; Convert a single alist option.
3591 (let (key-type value-type)
3593 (let ((key (nth 0 option)))
3594 (setq value-type (nth 1 option))
3597 (setq key-type `(const ,key))))
3598 (setq key-type `(const ,option)
3599 value-type widget-alist-value-type))
3600 `(cons :format "Key: %v" ,key-type ,value-type)))
3602 (define-widget 'choice 'menu-choice
3603 "A union of several sexp types."
3605 :format "%{%t%}: %[Value Menu%] %v"
3606 :button-prefix 'widget-push-button-prefix
3607 :button-suffix 'widget-push-button-suffix
3608 :prompt-value 'widget-choice-prompt-value)
3610 (defun widget-choice-prompt-value (widget prompt value _unbound)
3612 (let ((args (widget-get widget :args))
3613 (completion-ignore-case (widget-get widget :case-fold))
3614 current choices old)
3615 ;; Find the first arg that matches VALUE.
3618 (if (widget-apply (car look) :match value)
3619 (setq old (car look)
3621 (setq look (cdr look)))))
3624 (cond ((= (length args) 0)
3626 ((= (length args) 1)
3628 ((and (= (length args) 2)
3630 (if (eq old (nth 0 args))
3635 (setq current (car args)
3638 (cons (cons (widget-apply current :menu-tag-get)
3641 (let ((val (completing-read prompt choices nil t)))
3643 (let ((try (try-completion val choices)))
3646 (cdr (assoc val choices)))
3649 (widget-prompt-value current prompt nil t)
3652 (define-widget 'radio 'radio-button-choice
3653 "A union of several sexp types."
3655 :format "%{%t%}:\n%v"
3656 :prompt-value 'widget-choice-prompt-value)
3658 (define-widget 'repeat 'editable-list
3659 "A variable length homogeneous list."
3661 :format "%{%t%}:\n%v%i\n")
3663 (define-widget 'set 'checklist
3664 "A list of members from a fixed set."
3666 :format "%{%t%}:\n%v")
3668 (define-widget 'boolean 'toggle
3669 "To be nil or non-nil, that is the question."
3671 :prompt-value 'widget-boolean-prompt-value
3672 :button-prefix 'widget-push-button-prefix
3673 :button-suffix 'widget-push-button-suffix
3674 :format "%{%t%}: %[Toggle%] %v\n"
3678 (defun widget-boolean-prompt-value (_widget prompt _value _unbound)
3679 ;; Toggle a boolean.
3682 ;;; The `color' Widget.
3685 (define-widget 'color 'editable-field
3686 "Choose a color name (with sample)."
3687 :format "%{%t%}: %v (%{sample%})\n"
3688 :value-create 'widget-color-value-create
3692 :completions (or facemenu-color-alist (defined-colors))
3693 :sample-face-get 'widget-color-sample-face-get
3694 :notify 'widget-color-notify
3695 :action 'widget-color-action)
3697 (defun widget-color-value-create (widget)
3698 (widget-field-value-create widget)
3700 (widget-create-child-and-convert
3702 :tag " Choose " :action 'widget-color--choose-action)
3703 (widget-insert " "))
3705 (defun widget-color--choose-action (widget &optional _event)
3706 (list-colors-display
3709 (when (buffer-live-p ,(current-buffer))
3710 (widget-value-set ',(widget-get widget :parent) color)
3711 (let* ((buf (get-buffer "*Colors*"))
3712 (win (get-buffer-window buf 0)))
3714 (and win (> (length (window-list)) 1)
3715 (delete-window win)))
3716 (pop-to-buffer ,(current-buffer))))))
3718 (defun widget-color-sample-face-get (widget)
3719 (let* ((value (condition-case nil
3720 (widget-value widget)
3721 (error (widget-get widget :value)))))
3722 (if (color-defined-p value)
3723 (list (cons 'foreground-color value))
3726 (defun widget-color-action (widget &optional event)
3727 "Prompt for a color."
3728 (let* ((tag (widget-apply widget :menu-tag-get))
3729 (prompt (concat tag ": "))
3730 (answer (facemenu-read-color prompt)))
3731 (unless (zerop (length answer))
3732 (widget-value-set widget answer)
3734 (widget-apply widget :notify widget event))))
3736 (defun widget-color-notify (widget child &optional event)
3737 "Update the sample, and notify the parent."
3738 (overlay-put (widget-get widget :sample-overlay)
3739 'face (widget-apply widget :sample-face-get))
3740 (widget-default-notify widget child event))
3744 (defun widget-echo-help (pos)
3745 "Display help-echo text for widget at POS."
3746 (let* ((widget (widget-at pos))
3747 (help-echo (and widget (widget-get widget :help-echo))))
3748 (if (functionp help-echo)
3749 (setq help-echo (funcall help-echo widget)))
3750 (if help-echo (message "%s" (eval help-echo)))))
3756 ;;; wid-edit.el ends here