Compare and round more carefully
[emacs.git] / lisp / wid-edit.el
blobca402c18e536e05263ae00d22acb6b10efc24752
1 ;;; wid-edit.el --- Functions for creating and using widgets -*-byte-compile-dynamic: t; lexical-binding:t -*-
2 ;;
3 ;; Copyright (C) 1996-1997, 1999-2017 Free Software Foundation, Inc.
4 ;;
5 ;; Author: Per Abrahamsen <abraham@dina.kvl.dk>
6 ;; Maintainer: emacs-devel@gnu.org
7 ;; Keywords: extensions
8 ;; Package: emacs
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
35 ;; the browser.
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.]
53 ;;; Commentary:
55 ;; See `widget.el'.
57 ;;; Code:
58 (require 'cl-lib)
60 ;;; Compatibility.
62 (defun widget-event-point (event)
63 "Character position of the end of event if that exists, or nil."
64 (posn-point (event-end event)))
66 (defun widget-button-release-event-p (event)
67 "Non-nil if EVENT is a mouse-button-release event object."
68 (and (eventp event)
69 (memq (event-basic-type event) '(mouse-1 mouse-2 mouse-3))
70 (or (memq 'click (event-modifiers event))
71 (memq 'drag (event-modifiers event)))))
73 ;;; Customization.
75 (defgroup widgets nil
76 "Customization support for the Widget Library."
77 :link '(custom-manual "(widget)Top")
78 :link '(emacs-library-link :tag "Lisp File" "widget.el")
79 :prefix "widget-"
80 :group 'extensions)
82 (defgroup widget-documentation nil
83 "Options controlling the display of documentation strings."
84 :group 'widgets)
86 (defgroup widget-faces nil
87 "Faces used by the widget library."
88 :group 'widgets
89 :group 'faces)
91 (defvar widget-documentation-face 'widget-documentation
92 "Face used for documentation strings in widgets.
93 This exists as a variable so it can be set locally in certain buffers.")
95 (defface widget-documentation '((((class color)
96 (background dark))
97 (:foreground "lime green"))
98 (((class color)
99 (background light))
100 (:foreground "dark green"))
101 (t nil))
102 "Face used for documentation text."
103 :group 'widget-documentation
104 :group 'widget-faces)
106 (defvar widget-button-face 'widget-button
107 "Face used for buttons in widgets.
108 This exists as a variable so it can be set locally in certain buffers.")
110 (defface widget-button '((t (:weight bold)))
111 "Face used for widget buttons."
112 :group 'widget-faces)
114 (defcustom widget-mouse-face 'highlight
115 "Face used for widget buttons when the mouse is above them."
116 :type 'face
117 :group 'widget-faces)
119 ;; TTY gets special definitions here and in the next defface, because
120 ;; the gray colors defined for other displays cause black text on a black
121 ;; background, at least on light-background TTYs.
122 (defface widget-field '((((type tty))
123 :background "yellow3"
124 :foreground "black")
125 (((class grayscale color)
126 (background light))
127 :background "gray85")
128 (((class grayscale color)
129 (background dark))
130 :background "dim gray")
132 :slant italic))
133 "Face used for editable fields."
134 :group 'widget-faces)
136 (defface widget-single-line-field '((((type tty))
137 :background "green3"
138 :foreground "black")
139 (((class grayscale color)
140 (background light))
141 :background "gray85")
142 (((class grayscale color)
143 (background dark))
144 :background "dim gray")
146 :slant italic))
147 "Face used for editable fields spanning only a single line."
148 :group 'widget-faces)
150 ;;; This causes display-table to be loaded, and not usefully.
151 ;;;(defvar widget-single-line-display-table
152 ;;; (let ((table (make-display-table)))
153 ;;; (aset table 9 "^I")
154 ;;; (aset table 10 "^J")
155 ;;; table)
156 ;;; "Display table used for single-line editable fields.")
158 ;;;(when (fboundp 'set-face-display-table)
159 ;;; (set-face-display-table 'widget-single-line-field-face
160 ;;; widget-single-line-display-table))
162 ;;; Utility functions.
164 ;; These are not really widget specific.
166 (defun widget-princ-to-string (object)
167 "Return string representation of OBJECT, any Lisp object.
168 No quoting characters are used; no delimiters are printed around
169 the contents of strings."
170 (with-output-to-string
171 (princ object)))
173 (defun widget-clear-undo ()
174 "Clear all undo information."
175 (buffer-disable-undo (current-buffer))
176 (buffer-enable-undo))
178 (defcustom widget-menu-max-size 40
179 "Largest number of items allowed in a popup-menu.
180 Larger menus are read through the minibuffer."
181 :group 'widgets
182 :type 'integer)
184 (defcustom widget-menu-max-shortcuts 40
185 "Largest number of items for which it works to choose one with a character.
186 For a larger number of items, the minibuffer is used."
187 :group 'widgets
188 :type 'integer)
190 (defcustom widget-menu-minibuffer-flag nil
191 "Control how to ask for a choice from the keyboard.
192 Non-nil means use the minibuffer;
193 nil means read a single character."
194 :group 'widgets
195 :type 'boolean)
197 (defun widget-choose (title items &optional event)
198 "Choose an item from a list.
200 First argument TITLE is the name of the list.
201 Second argument ITEMS is a list whose members are either
202 (NAME . VALUE), to indicate selectable items, or just strings to
203 indicate unselectable items.
204 Optional third argument EVENT is an input event.
206 The user is asked to choose between each NAME from the items alist,
207 and the VALUE of the chosen element will be returned. If EVENT is a
208 mouse event, and the number of elements in items is less than
209 `widget-menu-max-size', a popup menu will be used, otherwise the
210 minibuffer."
211 (cond ((and (< (length items) widget-menu-max-size)
212 event (display-popup-menus-p))
213 ;; Mouse click.
214 (x-popup-menu event
215 (list title (cons "" items))))
216 ((or widget-menu-minibuffer-flag
217 (> (length items) widget-menu-max-shortcuts))
218 ;; Read the choice of name from the minibuffer.
219 (setq items (cl-remove-if 'stringp items))
220 (let ((val (completing-read (concat title ": ") items nil t)))
221 (if (stringp val)
222 (let ((try (try-completion val items)))
223 (when (stringp try)
224 (setq val try))
225 (cdr (assoc val items))))))
227 ;; Construct a menu of the choices
228 ;; and then use it for prompting for a single character.
229 (let* ((next-digit ?0)
230 (map (make-sparse-keymap))
231 choice some-choice-enabled value)
232 (with-current-buffer (get-buffer-create " widget-choose")
233 (erase-buffer)
234 (insert "Available choices:\n\n")
235 (while items
236 (setq choice (pop items))
237 (when (consp choice)
238 (let* ((name (substitute-command-keys (car choice)))
239 (function (cdr choice)))
240 (insert (format "%c = %s\n" next-digit name))
241 (define-key map (vector next-digit) function)
242 (setq some-choice-enabled t)))
243 ;; Allocate digits to disabled alternatives
244 ;; so that the digit of a given alternative never varies.
245 (setq next-digit (1+ next-digit)))
246 (insert "\nC-g = Quit")
247 (goto-char (point-min))
248 (forward-line))
249 (or some-choice-enabled
250 (error "None of the choices is currently meaningful"))
251 (define-key map [?\M-\C-v] 'scroll-other-window)
252 (define-key map [?\M--] 'negative-argument)
253 (save-window-excursion
254 (let ((buf (get-buffer " widget-choose")))
255 (fit-window-to-buffer (display-buffer buf))
256 (let ((cursor-in-echo-area t)
257 (arg 1))
258 (while (not value)
259 (setq value (lookup-key map (read-key-sequence (format "%s: " title))))
260 (unless value
261 (user-error "Canceled"))
262 (when
263 (cond ((eq value 'scroll-other-window)
264 (let ((minibuffer-scroll-window
265 (get-buffer-window buf)))
266 (if (> 0 arg)
267 (scroll-other-window-down
268 (window-height minibuffer-scroll-window))
269 (scroll-other-window))
270 (setq arg 1)))
271 ((eq value 'negative-argument)
272 (setq arg -1)))
273 (setq value nil))))))
274 value))))
276 ;;; Widget text specifications.
278 ;; These functions are for specifying text properties.
280 ;; We can set it to nil now that get_local_map uses get_pos_property.
281 (defconst widget-field-add-space nil
282 "Non-nil means add extra space at the end of editable text fields.
283 If you don't add the space, it will become impossible to edit a zero
284 size field.")
286 (defvar widget-field-use-before-change t
287 "Non-nil means use `before-change-functions' to track editable fields.
288 This enables the use of undo. Using before hooks also means that
289 the :notify function can't know the new value.")
291 (defun widget-specify-field (widget from to)
292 "Specify editable button for WIDGET between FROM and TO."
293 ;; Terminating space is not part of the field, but necessary in
294 ;; order for local-map to work. Remove next sexp if local-map works
295 ;; at the end of the overlay.
296 (save-excursion
297 (goto-char to)
298 (cond ((null (widget-get widget :size))
299 (forward-char 1))
300 (widget-field-add-space
301 (insert-and-inherit " ")))
302 (setq to (point)))
303 (let ((keymap (widget-get widget :keymap))
304 (face (or (widget-get widget :value-face) 'widget-field))
305 (help-echo (widget-get widget :help-echo))
306 (follow-link (widget-get widget :follow-link))
307 (rear-sticky
308 (or (not widget-field-add-space) (widget-get widget :size))))
309 (if (functionp help-echo)
310 (setq help-echo 'widget-mouse-help))
311 (when (= (char-before to) ?\n)
312 ;; When the last character in the field is a newline, we want to
313 ;; give it a `field' char-property of `boundary', which helps the
314 ;; C-n/C-p act more naturally when entering/leaving the field. We
315 ;; do this by making a small secondary overlay to contain just that
316 ;; one character.
317 (let ((overlay (make-overlay (1- to) to nil t nil)))
318 (overlay-put overlay 'field 'boundary)
319 ;; We need the real field for tabbing.
320 (overlay-put overlay 'real-field widget)
321 ;; Use `local-map' here, not `keymap', so that normal editing
322 ;; works in the field when, say, Custom uses `suppress-keymap'.
323 (overlay-put overlay 'local-map keymap)
324 (overlay-put overlay 'face face)
325 (overlay-put overlay 'follow-link follow-link)
326 (overlay-put overlay 'help-echo help-echo))
327 (setq to (1- to))
328 (setq rear-sticky t))
329 (let ((overlay (make-overlay from to nil nil rear-sticky)))
330 (widget-put widget :field-overlay overlay)
331 ;;(overlay-put overlay 'detachable nil)
332 (overlay-put overlay 'field widget)
333 (overlay-put overlay 'local-map keymap)
334 (overlay-put overlay 'face face)
335 (overlay-put overlay 'follow-link follow-link)
336 (overlay-put overlay 'help-echo help-echo)))
337 (widget-specify-secret widget))
339 (defun widget-specify-secret (field)
340 "Replace text in FIELD with value of `:secret', if non-nil."
341 (let ((secret (widget-get field :secret))
342 (size (widget-get field :size)))
343 (when secret
344 (let ((begin (widget-field-start field))
345 (end (widget-field-end field)))
346 (when size
347 (while (and (> end begin)
348 (eq (char-after (1- end)) ?\s))
349 (setq end (1- end))))
350 (while (< begin end)
351 (let ((old (char-after begin)))
352 (unless (eq old secret)
353 (subst-char-in-region begin (1+ begin) old secret)
354 (put-text-property begin (1+ begin) 'secret old))
355 (setq begin (1+ begin))))))))
357 (defun widget-specify-button (widget from to)
358 "Specify button for WIDGET between FROM and TO."
359 (let ((overlay (make-overlay from to nil t nil))
360 (follow-link (widget-get widget :follow-link))
361 (help-echo (widget-get widget :help-echo)))
362 (widget-put widget :button-overlay overlay)
363 (if (functionp help-echo)
364 (setq help-echo 'widget-mouse-help))
365 (overlay-put overlay 'button widget)
366 (overlay-put overlay 'keymap (widget-get widget :keymap))
367 (overlay-put overlay 'evaporate t)
368 ;; We want to avoid the face with image buttons.
369 (unless (widget-get widget :suppress-face)
370 (overlay-put overlay 'face (widget-apply widget :button-face-get))
371 (overlay-put overlay 'mouse-face
372 ;; Make new list structure for the mouse-face value
373 ;; so that different widgets will have
374 ;; different `mouse-face' property values
375 ;; and will highlight separately.
376 (let ((mouse-face-value
377 (widget-apply widget :mouse-face-get)))
378 ;; If it's a list, copy it.
379 (if (listp mouse-face-value)
380 (copy-sequence mouse-face-value)
381 ;; If it's a symbol, put it in a list.
382 (list mouse-face-value)))))
383 (overlay-put overlay 'pointer 'hand)
384 (overlay-put overlay 'follow-link follow-link)
385 (overlay-put overlay 'help-echo help-echo)))
387 (defun widget-mouse-help (_window overlay _point)
388 "Help-echo callback for widgets whose :help-echo is a function."
389 (with-current-buffer (overlay-buffer overlay)
390 (let* ((widget (widget-at (overlay-start overlay)))
391 (help-echo (if widget (widget-get widget :help-echo))))
392 (if (functionp help-echo)
393 (funcall help-echo widget)
394 help-echo))))
396 (defun widget-specify-sample (widget from to)
397 "Specify sample for WIDGET between FROM and TO."
398 (let ((overlay (make-overlay from to nil t nil)))
399 (overlay-put overlay 'face (widget-apply widget :sample-face-get))
400 (overlay-put overlay 'evaporate t)
401 (widget-put widget :sample-overlay overlay)))
403 (defun widget-specify-doc (widget from to)
404 "Specify documentation for WIDGET between FROM and TO."
405 (let ((overlay (make-overlay from to nil t nil)))
406 (overlay-put overlay 'widget-doc widget)
407 (overlay-put overlay 'face widget-documentation-face)
408 (overlay-put overlay 'evaporate t)
409 (widget-put widget :doc-overlay overlay)))
411 (defmacro widget-specify-insert (&rest form)
412 "Execute FORM without inheriting any text properties."
413 `(save-restriction
414 (let ((inhibit-read-only t)
415 (inhibit-modification-hooks t))
416 (narrow-to-region (point) (point))
417 (prog1 (progn ,@form)
418 (goto-char (point-max))))))
420 (defface widget-inactive
421 '((t :inherit shadow))
422 "Face used for inactive widgets."
423 :group 'widget-faces)
425 (defun widget-specify-inactive (widget from to)
426 "Make WIDGET inactive for user modifications."
427 (unless (widget-get widget :inactive)
428 (let ((overlay (make-overlay from to nil t nil)))
429 (overlay-put overlay 'face 'widget-inactive)
430 ;; This is disabled, as it makes the mouse cursor change shape.
431 ;; (overlay-put overlay 'mouse-face 'widget-inactive)
432 (overlay-put overlay 'evaporate t)
433 (overlay-put overlay 'priority 100)
434 (overlay-put overlay 'modification-hooks '(widget-overlay-inactive))
435 (widget-put widget :inactive overlay))))
437 (defun widget-overlay-inactive (&rest _junk)
438 "Ignoring the arguments, signal an error."
439 (unless inhibit-read-only
440 (error "The widget here is not active")))
443 (defun widget-specify-active (widget)
444 "Make WIDGET active for user modifications."
445 (let ((inactive (widget-get widget :inactive)))
446 (when inactive
447 (delete-overlay inactive)
448 (widget-put widget :inactive nil))))
450 ;;; Widget Properties.
452 (defsubst widget-type (widget)
453 "Return the type of WIDGET. The type is a symbol."
454 (car widget))
456 ;;;###autoload
457 (defun widgetp (widget)
458 "Return non-nil if WIDGET is a widget."
459 (if (symbolp widget)
460 (get widget 'widget-type)
461 (and (consp widget)
462 (symbolp (car widget))
463 (get (car widget) 'widget-type))))
465 (defun widget-get-indirect (widget property)
466 "In WIDGET, get the value of PROPERTY.
467 If the value is a symbol, return its binding.
468 Otherwise, just return the value."
469 (let ((value (widget-get widget property)))
470 (if (symbolp value)
471 (symbol-value value)
472 value)))
474 (defun widget-member (widget property)
475 "Non-nil if there is a definition in WIDGET for PROPERTY."
476 (cond ((plist-member (cdr widget) property)
478 ((car widget)
479 (widget-member (get (car widget) 'widget-type) property))
480 (t nil)))
482 (defun widget-value (widget)
483 "Extract the current value of WIDGET."
484 (widget-apply widget
485 :value-to-external (widget-apply widget :value-get)))
487 (defun widget-value-set (widget value)
488 "Set the current value of WIDGET to VALUE."
489 (widget-apply widget
490 :value-set (widget-apply widget
491 :value-to-internal value)))
493 (defun widget-default-get (widget)
494 "Extract the default external value of WIDGET."
495 (widget-apply widget :value-to-external
496 (or (widget-get widget :value)
497 (progn
498 (when (widget-get widget :args)
499 (setq widget (widget-copy widget))
500 (let (args)
501 (dolist (arg (widget-get widget :args))
502 (setq args (append args
503 (if (widget-get arg :inline)
504 (widget-get arg :args)
505 (list arg)))))
506 (widget-put widget :args args)))
507 (widget-apply widget :default-get)))))
509 (defun widget-match-inline (widget vals)
510 "In WIDGET, match the start of VALS."
511 (cond ((widget-get widget :inline)
512 (widget-apply widget :match-inline vals))
513 ((and (listp vals)
514 (widget-apply widget :match (car vals)))
515 (cons (list (car vals)) (cdr vals)))
516 (t nil)))
518 (defun widget-apply-action (widget &optional event)
519 "Apply :action in WIDGET in response to EVENT."
520 (if (widget-apply widget :active)
521 (widget-apply widget :action event)
522 (error "Attempt to perform action on inactive widget")))
524 ;;; Helper functions.
526 ;; These are widget specific.
528 ;;;###autoload
529 (defun widget-prompt-value (widget prompt &optional value unbound)
530 "Prompt for a value matching WIDGET, using PROMPT.
531 The current value is assumed to be VALUE, unless UNBOUND is non-nil."
532 (unless (listp widget)
533 (setq widget (list widget)))
534 (setq prompt (format "[%s] %s" (widget-type widget) prompt))
535 (setq widget (widget-convert widget))
536 (let ((answer (widget-apply widget :prompt-value prompt value unbound)))
537 (unless (widget-apply widget :match answer)
538 (error "Value does not match %S type" (car widget)))
539 answer))
541 (defun widget-get-sibling (widget)
542 "Get the item WIDGET is assumed to toggle.
543 This is only meaningful for radio buttons or checkboxes in a list."
544 (let* ((children (widget-get (widget-get widget :parent) :children))
545 child)
546 (catch 'child
547 (while children
548 (setq child (car children)
549 children (cdr children))
550 (when (eq (widget-get child :button) widget)
551 (throw 'child child)))
552 nil)))
554 (defun widget-map-buttons (function &optional buffer maparg)
555 "Map FUNCTION over the buttons in BUFFER.
556 FUNCTION is called with the arguments WIDGET and MAPARG.
558 If FUNCTION returns non-nil, the walk is canceled.
560 The arguments MAPARG, and BUFFER default to nil and (current-buffer),
561 respectively."
562 (let ((cur (point-min))
563 (widget nil)
564 (overlays (if buffer
565 (with-current-buffer buffer (overlay-lists))
566 (overlay-lists))))
567 (setq overlays (append (car overlays) (cdr overlays)))
568 (while (setq cur (pop overlays))
569 (setq widget (overlay-get cur 'button))
570 (if (and widget (funcall function widget maparg))
571 (setq overlays nil)))))
573 ;;; Images.
575 (defcustom widget-image-directory (file-name-as-directory
576 (expand-file-name "images/custom" data-directory))
577 "Where widget button images are located.
578 If this variable is nil, widget will try to locate the directory
579 automatically."
580 :group 'widgets
581 :type 'directory)
583 (defcustom widget-image-enable t
584 "If non-nil, use image buttons in widgets when available."
585 :version "21.1"
586 :group 'widgets
587 :type 'boolean)
589 (defcustom widget-image-conversion
590 '((xpm ".xpm") (gif ".gif") (png ".png") (jpeg ".jpg" ".jpeg")
591 (xbm ".xbm"))
592 "Conversion alist from image formats to file name suffixes."
593 :group 'widgets
594 :type '(repeat (cons :format "%v"
595 (symbol :tag "Image Format" unknown)
596 (repeat :tag "Suffixes"
597 (string :format "%v")))))
599 (defun widget-image-find (image)
600 "Create a graphical button from IMAGE.
601 IMAGE should either already be an image, or be a file name sans
602 extension (xpm, xbm, gif, jpg, or png) located in
603 `widget-image-directory' or otherwise where `find-image' will find it."
604 (cond ((not (and image widget-image-enable (display-graphic-p)))
605 ;; We don't want or can't use images.
606 nil)
607 ((and (consp image)
608 (eq 'image (car image)))
609 ;; Already an image spec. Use it.
610 image)
611 ((stringp image)
612 ;; A string. Look it up in relevant directories.
613 (let* ((load-path (cons widget-image-directory load-path))
614 specs)
615 (dolist (elt widget-image-conversion)
616 (dolist (ext (cdr elt))
617 (push (list :type (car elt) :file (concat image ext))
618 specs)))
619 (find-image (nreverse specs))))
621 ;; Oh well.
622 nil)))
624 (defvar widget-button-pressed-face 'widget-button-pressed
625 "Face used for pressed buttons in widgets.
626 This exists as a variable so it can be set locally in certain
627 buffers.")
629 (defun widget-image-insert (widget tag image &optional _down _inactive)
630 "In WIDGET, insert the text TAG or, if supported, IMAGE.
631 IMAGE should either be an image or an image file name sans extension
632 \(xpm, xbm, gif, jpg, or png) located in `widget-image-directory'.
634 Optional arguments DOWN and INACTIVE are used instead of IMAGE when the
635 button is pressed or inactive, respectively. These are currently ignored."
636 (if (and (featurep 'image)
637 (setq image (widget-image-find image)))
638 (progn (widget-put widget :suppress-face t)
639 (insert-image image tag))
640 (insert tag)))
642 (defun widget-move-and-invoke (event)
643 "Move to where you click, and if it is an active field, invoke it."
644 (interactive "e")
645 (mouse-set-point event)
646 (let ((pos (widget-event-point event)))
647 (if (and pos (get-char-property pos 'button))
648 (widget-button-click event))))
650 ;;; Buttons.
652 (defgroup widget-button nil
653 "The look of various kinds of buttons."
654 :group 'widgets)
656 (defcustom widget-button-prefix ""
657 "String used as prefix for buttons."
658 :type 'string
659 :group 'widget-button)
661 (defcustom widget-button-suffix ""
662 "String used as suffix for buttons."
663 :type 'string
664 :group 'widget-button)
666 ;;; Creating Widgets.
668 ;;;###autoload
669 (defun widget-create (type &rest args)
670 "Create widget of TYPE.
671 The optional ARGS are additional keyword arguments."
672 (let ((widget (apply 'widget-convert type args)))
673 (widget-apply widget :create)
674 widget))
676 (defun widget-create-child-and-convert (parent type &rest args)
677 "As part of the widget PARENT, create a child widget TYPE.
678 The child is converted, using the keyword arguments ARGS."
679 (let ((widget (apply 'widget-convert type args)))
680 (widget-put widget :parent parent)
681 (unless (widget-get widget :indent)
682 (widget-put widget :indent (+ (or (widget-get parent :indent) 0)
683 (or (widget-get widget :extra-offset) 0)
684 (widget-get parent :offset))))
685 (widget-apply widget :create)
686 widget))
688 (defun widget-create-child (parent type)
689 "Create widget of TYPE."
690 (let ((widget (widget-copy type)))
691 (widget-put widget :parent parent)
692 (unless (widget-get widget :indent)
693 (widget-put widget :indent (+ (or (widget-get parent :indent) 0)
694 (or (widget-get widget :extra-offset) 0)
695 (widget-get parent :offset))))
696 (widget-apply widget :create)
697 widget))
699 (defun widget-create-child-value (parent type value)
700 "Create widget of TYPE with value VALUE."
701 (let ((widget (widget-copy type)))
702 (widget-put widget :value (widget-apply widget :value-to-internal value))
703 (widget-put widget :parent parent)
704 (unless (widget-get widget :indent)
705 (widget-put widget :indent (+ (or (widget-get parent :indent) 0)
706 (or (widget-get widget :extra-offset) 0)
707 (widget-get parent :offset))))
708 (widget-apply widget :create)
709 widget))
711 ;;;###autoload
712 (defun widget-delete (widget)
713 "Delete WIDGET."
714 (widget-apply widget :delete))
716 (defun widget-copy (widget)
717 "Make a deep copy of WIDGET."
718 (widget-apply (copy-sequence widget) :copy))
720 (defun widget-convert (type &rest args)
721 "Convert TYPE to a widget without inserting it in the buffer.
722 The optional ARGS are additional keyword arguments."
723 ;; Don't touch the type.
724 (let* ((widget (if (symbolp type)
725 (list type)
726 (copy-sequence type)))
727 (current widget)
728 done
729 (keys args))
730 ;; First set the :args keyword.
731 (while (cdr current) ;Look in the type.
732 (if (and (keywordp (cadr current))
733 ;; If the last element is a keyword,
734 ;; it is still the :args element,
735 ;; even though it is a keyword.
736 (cddr current))
737 (if (eq (cadr current) :args)
738 ;; If :args is explicitly specified, obey it.
739 (setq current nil)
740 ;; Some other irrelevant keyword.
741 (setq current (cdr (cdr current))))
742 (setcdr current (list :args (cdr current)))
743 (setq current nil)))
744 (while (and args (not done)) ;Look in ARGS.
745 (cond ((eq (car args) :args)
746 ;; Handle explicit specification of :args.
747 (setq args (cadr args)
748 done t))
749 ((keywordp (car args))
750 (setq args (cddr args)))
751 (t (setq done t))))
752 (when done
753 (widget-put widget :args args))
754 ;; Then Convert the widget.
755 (setq type widget)
756 (while type
757 (let ((convert-widget (plist-get (cdr type) :convert-widget)))
758 (if convert-widget
759 (setq widget (funcall convert-widget widget))))
760 (setq type (get (car type) 'widget-type)))
761 ;; Finally set the keyword args.
762 (while keys
763 (let ((next (nth 0 keys)))
764 (if (keywordp next)
765 (progn
766 (widget-put widget next (nth 1 keys))
767 (setq keys (nthcdr 2 keys)))
768 (setq keys nil))))
769 ;; Convert the :value to internal format.
770 (if (widget-member widget :value)
771 (widget-put widget
772 :value (widget-apply widget
773 :value-to-internal
774 (widget-get widget :value))))
775 ;; Return the newly create widget.
776 widget))
778 ;;;###autoload
779 (defun widget-insert (&rest args)
780 "Call `insert' with ARGS even if surrounding text is read only."
781 (let ((inhibit-read-only t)
782 (inhibit-modification-hooks t))
783 (apply 'insert args)))
785 (defun widget-convert-text (type from to
786 &optional button-from button-to
787 &rest args)
788 "Return a widget of type TYPE with endpoint FROM TO.
789 No text will be inserted to the buffer, instead the text between FROM
790 and TO will be used as the widgets end points. If optional arguments
791 BUTTON-FROM and BUTTON-TO are given, these will be used as the widgets
792 button end points.
793 Optional ARGS are extra keyword arguments for TYPE."
794 (let ((widget (apply 'widget-convert type :delete 'widget-leave-text args))
795 (from (copy-marker from))
796 (to (copy-marker to)))
797 (set-marker-insertion-type from t)
798 (set-marker-insertion-type to nil)
799 (widget-put widget :from from)
800 (widget-put widget :to to)
801 (when button-from
802 (widget-specify-button widget button-from button-to))
803 widget))
805 (defun widget-convert-button (type from to &rest args)
806 "Return a widget of type TYPE with endpoint FROM TO.
807 Optional ARGS are extra keyword arguments for TYPE.
808 No text will be inserted to the buffer, instead the text between FROM
809 and TO will be used as the widgets end points, as well as the widgets
810 button end points."
811 (apply 'widget-convert-text type from to from to args))
813 (defun widget-leave-text (widget)
814 "Remove markers and overlays from WIDGET and its children."
815 (let ((button (widget-get widget :button-overlay))
816 (sample (widget-get widget :sample-overlay))
817 (doc (widget-get widget :doc-overlay))
818 (field (widget-get widget :field-overlay)))
819 (set-marker (widget-get widget :from) nil)
820 (set-marker (widget-get widget :to) nil)
821 (when button
822 (delete-overlay button))
823 (when sample
824 (delete-overlay sample))
825 (when doc
826 (delete-overlay doc))
827 (when field
828 (delete-overlay field))
829 (mapc 'widget-leave-text (widget-get widget :children))))
831 ;;; Keymap and Commands.
833 ;; This alias exists only so that one can choose in doc-strings (e.g.
834 ;; Custom-mode) which key-binding of widget-keymap one wants to refer to.
835 ;; http://lists.gnu.org/archive/html/emacs-devel/2008-11/msg00480.html
836 (define-obsolete-function-alias 'advertised-widget-backward
837 'widget-backward "23.2")
839 ;;;###autoload
840 (defvar widget-keymap
841 (let ((map (make-sparse-keymap)))
842 (define-key map "\t" 'widget-forward)
843 (define-key map "\e\t" 'widget-backward)
844 (define-key map [(shift tab)] 'widget-backward)
845 (put 'widget-backward :advertised-binding [(shift tab)])
846 (define-key map [backtab] 'widget-backward)
847 (define-key map [down-mouse-2] 'widget-button-click)
848 (define-key map [down-mouse-1] 'widget-button-click)
849 ;; The following definition needs to avoid using escape sequences that
850 ;; might get converted to ^M when building loaddefs.el
851 (define-key map [(control ?m)] 'widget-button-press)
852 map)
853 "Keymap containing useful binding for buffers containing widgets.
854 Recommended as a parent keymap for modes using widgets.
855 Note that such modes will need to require wid-edit.")
857 (defvar widget-global-map global-map
858 "Keymap used for events a widget does not handle itself.")
859 (make-variable-buffer-local 'widget-global-map)
861 (defvar widget-field-keymap
862 (let ((map (copy-keymap widget-keymap)))
863 (define-key map "\C-k" 'widget-kill-line)
864 (define-key map "\M-\t" 'widget-complete)
865 (define-key map "\C-m" 'widget-field-activate)
866 ;; Since the widget code uses a `field' property to identify fields,
867 ;; ordinary beginning-of-line does the right thing.
868 ;; (define-key map "\C-a" 'widget-beginning-of-line)
869 (define-key map "\C-e" 'widget-end-of-line)
870 map)
871 "Keymap used inside an editable field.")
873 (defvar widget-text-keymap
874 (let ((map (copy-keymap widget-keymap)))
875 ;; Since the widget code uses a `field' property to identify fields,
876 ;; ordinary beginning-of-line does the right thing.
877 ;; (define-key map "\C-a" 'widget-beginning-of-line)
878 (define-key map "\C-e" 'widget-end-of-line)
879 map)
880 "Keymap used inside a text field.")
882 (defun widget-field-activate (pos &optional event)
883 "Invoke the editable field at point."
884 (interactive "@d")
885 (let ((field (widget-field-at pos)))
886 (if field
887 (widget-apply-action field event)
888 (call-interactively
889 (lookup-key widget-global-map (this-command-keys))))))
891 (defface widget-button-pressed
892 '((((min-colors 88) (class color))
893 (:foreground "red1"))
894 (((class color))
895 (:foreground "red"))
897 (:weight bold :underline t)))
898 "Face used for pressed buttons."
899 :group 'widget-faces)
901 (defvar widget-button-click-moves-point nil
902 "If non-nil, `widget-button-click' moves point to a button after invoking it.
903 If nil, point returns to its original position after invoking a button.")
905 (defun widget-button-click (event)
906 "Invoke the button that the mouse is pointing at."
907 (interactive "e")
908 (if (widget-event-point event)
909 (let* ((oevent event)
910 (mouse-1 (memq (event-basic-type event) '(mouse-1 down-mouse-1)))
911 (pos (widget-event-point event))
912 (start (event-start event))
913 (button (get-char-property
914 pos 'button (and (windowp (posn-window start))
915 (window-buffer (posn-window start)))))
916 newpoint)
917 (when (or (null button)
918 (catch 'button-press-cancelled
919 ;; Mouse click on a widget button. Do the following
920 ;; in a save-excursion so that the click on the button
921 ;; doesn't change point.
922 (save-selected-window
923 (select-window (posn-window (event-start event)))
924 (save-excursion
925 (goto-char (posn-point (event-start event)))
926 (let* ((overlay (widget-get button :button-overlay))
927 (pressed-face (or (widget-get button :pressed-face)
928 widget-button-pressed-face))
929 (face (overlay-get overlay 'face))
930 (mouse-face (overlay-get overlay 'mouse-face)))
931 (unwind-protect
932 ;; Read events, including mouse-movement
933 ;; events, waiting for a release event. If we
934 ;; began with a mouse-1 event and receive a
935 ;; movement event, that means the user wants
936 ;; to perform drag-selection, so cancel the
937 ;; button press and do the default mouse-1
938 ;; action. For mouse-2, just highlight/
939 ;; unhighlight the button the mouse was
940 ;; initially on when we move over it.
941 (save-excursion
942 (when face ; avoid changing around image
943 (overlay-put overlay 'face pressed-face)
944 (overlay-put overlay 'mouse-face pressed-face))
945 (unless (widget-apply button :mouse-down-action event)
946 (let ((track-mouse t))
947 (while (not (widget-button-release-event-p event))
948 (setq event (read-event))
949 (when (and mouse-1 (mouse-movement-p event))
950 (push event unread-command-events)
951 (setq event oevent)
952 (throw 'button-press-cancelled t))
953 (unless (or (integerp event)
954 (memq (car event) '(switch-frame select-window))
955 (eq (car event) 'scroll-bar-movement))
956 (setq pos (widget-event-point event))
957 (if (and pos
958 (eq (get-char-property pos 'button)
959 button))
960 (when face
961 (overlay-put overlay 'face pressed-face)
962 (overlay-put overlay 'mouse-face pressed-face))
963 (overlay-put overlay 'face face)
964 (overlay-put overlay 'mouse-face mouse-face))))))
966 ;; When mouse is released over the button, run
967 ;; its action function.
968 (when (and pos (eq (get-char-property pos 'button) button))
969 (goto-char pos)
970 (widget-apply-action button event)
971 (if widget-button-click-moves-point
972 (setq newpoint (point)))))
973 (overlay-put overlay 'face face)
974 (overlay-put overlay 'mouse-face mouse-face))))
976 (if newpoint (goto-char newpoint))
977 ;; This loses if the widget action switches windows. -- cyd
978 ;; (unless (pos-visible-in-window-p (widget-event-point event))
979 ;; (mouse-set-point event)
980 ;; (beginning-of-line)
981 ;; (recenter))
983 nil))
984 (let ((up t) command)
985 ;; Mouse click not on a widget button. Find the global
986 ;; command to run, and check whether it is bound to an
987 ;; up event.
988 (if mouse-1
989 (cond ((setq command ;down event
990 (lookup-key widget-global-map [down-mouse-1]))
991 (setq up nil))
992 ((setq command ;up event
993 (lookup-key widget-global-map [mouse-1]))))
994 (cond ((setq command ;down event
995 (lookup-key widget-global-map [down-mouse-2]))
996 (setq up nil))
997 ((setq command ;up event
998 (lookup-key widget-global-map [mouse-2])))))
999 (when up
1000 ;; Don't execute up events twice.
1001 (while (not (widget-button-release-event-p event))
1002 (setq event (read-event))))
1003 (when command
1004 (call-interactively command)))))
1005 (message "You clicked somewhere weird.")))
1007 (defun widget-button-press (pos &optional event)
1008 "Invoke button at POS."
1009 (interactive "@d")
1010 (let ((button (get-char-property pos 'button)))
1011 (if button
1012 (widget-apply-action button event)
1013 (let ((command (lookup-key widget-global-map (this-command-keys))))
1014 (when (commandp command)
1015 (call-interactively command))))))
1017 (defun widget-tabable-at (&optional pos)
1018 "Return the tabable widget at POS, or nil.
1019 POS defaults to the value of (point)."
1020 (let ((widget (widget-at pos)))
1021 (if widget
1022 (let ((order (widget-get widget :tab-order)))
1023 (if order
1024 (if (>= order 0)
1025 widget)
1026 widget)))))
1028 (defvar widget-use-overlay-change t
1029 "If non-nil, use overlay change functions to tab around in the buffer.
1030 This is much faster.")
1032 (defun widget-move (arg)
1033 "Move point to the ARG next field or button.
1034 ARG may be negative to move backward."
1035 (or (bobp) (> arg 0) (backward-char))
1036 (let ((wrapped 0)
1037 (number arg)
1038 (old (widget-tabable-at)))
1039 ;; Forward.
1040 (while (> arg 0)
1041 (cond ((eobp)
1042 (goto-char (point-min))
1043 (setq wrapped (1+ wrapped)))
1044 (widget-use-overlay-change
1045 (goto-char (next-overlay-change (point))))
1047 (forward-char 1)))
1048 (and (= wrapped 2)
1049 (eq arg number)
1050 (error "No buttons or fields found"))
1051 (let ((new (widget-tabable-at)))
1052 (when new
1053 (unless (eq new old)
1054 (setq arg (1- arg))
1055 (setq old new)))))
1056 ;; Backward.
1057 (while (< arg 0)
1058 (cond ((bobp)
1059 (goto-char (point-max))
1060 (setq wrapped (1+ wrapped)))
1061 (widget-use-overlay-change
1062 (goto-char (previous-overlay-change (point))))
1064 (backward-char 1)))
1065 (and (= wrapped 2)
1066 (eq arg number)
1067 (error "No buttons or fields found"))
1068 (let ((new (widget-tabable-at)))
1069 (when new
1070 (unless (eq new old)
1071 (setq arg (1+ arg))))))
1072 (let ((new (widget-tabable-at)))
1073 (while (eq (widget-tabable-at) new)
1074 (backward-char)))
1075 (forward-char))
1076 (widget-echo-help (point))
1077 (run-hooks 'widget-move-hook))
1079 (defun widget-forward (arg)
1080 "Move point to the next field or button.
1081 With optional ARG, move across that many fields."
1082 (interactive "p")
1083 (run-hooks 'widget-forward-hook)
1084 (widget-move arg))
1086 (defun widget-backward (arg)
1087 "Move point to the previous field or button.
1088 With optional ARG, move across that many fields."
1089 (interactive "p")
1090 (run-hooks 'widget-backward-hook)
1091 (widget-move (- arg)))
1093 ;; Since the widget code uses a `field' property to identify fields,
1094 ;; ordinary beginning-of-line does the right thing.
1095 (defalias 'widget-beginning-of-line 'beginning-of-line)
1097 (defun widget-end-of-line ()
1098 "Go to end of field or end of line, whichever is first.
1099 Trailing spaces at the end of padded fields are not considered part of
1100 the field."
1101 (interactive)
1102 ;; Ordinary end-of-line does the right thing, because we're inside
1103 ;; text with a `field' property.
1104 (end-of-line)
1105 (unless (eolp)
1106 ;; ... except that we want to ignore trailing spaces in fields that
1107 ;; aren't terminated by a newline, because they are used as padding,
1108 ;; and ignored when extracting the entered value of the field.
1109 (skip-chars-backward " " (field-beginning (1- (point))))))
1111 (defun widget-kill-line ()
1112 "Kill to end of field or end of line, whichever is first."
1113 (interactive)
1114 (let* ((field (widget-field-find (point)))
1115 (end (and field (widget-field-end field))))
1116 (if (and field (> (line-beginning-position 2) end))
1117 (kill-region (point) end)
1118 (call-interactively 'kill-line))))
1120 (defun widget-narrow-to-field ()
1121 "Narrow to field."
1122 (interactive)
1123 (let ((field (widget-field-find (point))))
1124 (if field
1125 (narrow-to-region (line-beginning-position) (line-end-position)))))
1127 ;; This used to say:
1128 ;; "When not inside a field, move to the previous button or field."
1129 ;; but AFAICS, it has always just thrown an error.
1130 (defun widget-complete ()
1131 "Complete content of editable field from point.
1132 When not inside a field, signal an error."
1133 (interactive)
1134 (let ((data (widget-completions-at-point)))
1135 (cond
1136 ((functionp data) (funcall data))
1137 ((consp data)
1138 (let ((completion-extra-properties (nth 3 data)))
1139 (completion-in-region (nth 0 data) (nth 1 data) (nth 2 data)
1140 (plist-get completion-extra-properties
1141 :predicate))))
1143 (error "Not in an editable field")))))
1144 ;; We may want to use widget completion in buffers where the major mode
1145 ;; hasn't added widget-completions-at-point to completion-at-point-functions,
1146 ;; so it's not really obsolete (yet).
1147 ;; (make-obsolete 'widget-complete 'completion-at-point "24.1")
1149 (defun widget-completions-at-point ()
1150 (let ((field (widget-field-find (point))))
1151 (when field
1152 (widget-apply field :completions-function))))
1154 ;;; Setting up the buffer.
1156 (defvar widget-field-new nil
1157 "List of all newly created editable fields in the buffer.")
1158 (make-variable-buffer-local 'widget-field-new)
1160 (defvar widget-field-list nil
1161 "List of all editable fields in the buffer.")
1162 (make-variable-buffer-local 'widget-field-list)
1164 (defun widget-at (&optional pos)
1165 "The button or field at POS (default, point)."
1166 (or (get-char-property (or pos (point)) 'button)
1167 (widget-field-at pos)))
1169 ;;;###autoload
1170 (defun widget-setup ()
1171 "Setup current buffer so editing string widgets works."
1172 (let ((inhibit-read-only t)
1173 (inhibit-modification-hooks t)
1174 field)
1175 (while widget-field-new
1176 (setq field (car widget-field-new)
1177 widget-field-new (cdr widget-field-new)
1178 widget-field-list (cons field widget-field-list))
1179 (let ((from (car (widget-get field :field-overlay)))
1180 (to (cdr (widget-get field :field-overlay))))
1181 (widget-specify-field field
1182 (marker-position from) (marker-position to))
1183 (set-marker from nil)
1184 (set-marker to nil))))
1185 (widget-clear-undo)
1186 (widget-add-change))
1188 (defvar widget-field-last nil)
1189 ;; Last field containing point.
1190 (make-variable-buffer-local 'widget-field-last)
1192 (defvar widget-field-was nil)
1193 ;; The widget data before the change.
1194 (make-variable-buffer-local 'widget-field-was)
1196 (defun widget-field-at (pos)
1197 "Return the widget field at POS, or nil if none."
1198 (let ((field (get-char-property (or pos (point)) 'field)))
1199 (if (eq field 'boundary)
1200 (get-char-property (or pos (point)) 'real-field)
1201 field)))
1203 (defun widget-field-buffer (widget)
1204 "Return the buffer of WIDGET's editing field."
1205 (let ((overlay (widget-get widget :field-overlay)))
1206 (cond ((overlayp overlay)
1207 (overlay-buffer overlay))
1208 ((consp overlay)
1209 (marker-buffer (car overlay))))))
1211 (defun widget-field-start (widget)
1212 "Return the start of WIDGET's editing field."
1213 (let ((overlay (widget-get widget :field-overlay)))
1214 (if (overlayp overlay)
1215 (overlay-start overlay)
1216 (car overlay))))
1218 (defun widget-field-end (widget)
1219 "Return the end of WIDGET's editing field."
1220 (let ((overlay (widget-get widget :field-overlay)))
1221 ;; Don't subtract one if local-map works at the end of the overlay,
1222 ;; or if a special `boundary' field has been added after the widget
1223 ;; field.
1224 (if (overlayp overlay)
1225 ;; Don't proceed if overlay has been removed from buffer.
1226 (when (overlay-buffer overlay)
1227 (if (and (not (eq (with-current-buffer
1228 (widget-field-buffer widget)
1229 (save-restriction
1230 ;; `widget-narrow-to-field' can be
1231 ;; active when this function is called
1232 ;; from an change-functions hook. So
1233 ;; temporarily remove field narrowing
1234 ;; before to call `get-char-property'.
1235 (widen)
1236 (get-char-property (overlay-end overlay)
1237 'field)))
1238 'boundary))
1239 (or widget-field-add-space
1240 (null (widget-get widget :size))))
1241 (1- (overlay-end overlay))
1242 (overlay-end overlay)))
1243 (cdr overlay))))
1245 (defun widget-field-text-end (widget)
1246 (let ((to (widget-field-end widget))
1247 (size (widget-get widget :size)))
1248 (if (or (null size) (zerop size))
1250 (let ((from (widget-field-start widget)))
1251 (if (and from to)
1252 (with-current-buffer (widget-field-buffer widget)
1253 (while (and (> to from)
1254 (eq (char-after (1- to)) ?\s))
1255 (setq to (1- to)))
1256 to))))))
1258 (defun widget-field-find (pos)
1259 "Return the field at POS.
1260 Unlike (get-char-property POS \\='field), this works with empty fields too."
1261 (let ((fields widget-field-list)
1262 field found)
1263 (while fields
1264 (setq field (car fields)
1265 fields (cdr fields))
1266 (when (and (<= (widget-field-start field) pos)
1267 (<= pos (widget-field-end field)))
1268 (when found
1269 (error "Overlapping fields"))
1270 (setq found field)))
1271 found))
1273 (defun widget-before-change (from to)
1274 ;; This is how, for example, a variable changes its state to `modified'.
1275 ;; when it is being edited.
1276 (unless inhibit-read-only
1277 (let ((from-field (widget-field-find from))
1278 (to-field (widget-field-find to)))
1279 (cond ((not (eq from-field to-field))
1280 (add-hook 'post-command-hook 'widget-add-change nil t)
1281 (signal 'text-read-only
1282 '("Change should be restricted to a single field")))
1283 ((null from-field)
1284 (add-hook 'post-command-hook 'widget-add-change nil t)
1285 (signal 'text-read-only
1286 '("Attempt to change text outside editable field")))
1287 (widget-field-use-before-change
1288 (widget-apply from-field :notify from-field))))))
1290 (defun widget-add-change ()
1291 (remove-hook 'post-command-hook 'widget-add-change t)
1292 (add-hook 'before-change-functions 'widget-before-change nil t)
1293 (add-hook 'after-change-functions 'widget-after-change nil t))
1295 (defun widget-after-change (from to _old)
1296 "Adjust field size and text properties."
1297 (let ((field (widget-field-find from))
1298 (other (widget-field-find to)))
1299 (when field
1300 (unless (eq field other)
1301 (error "Change in different fields"))
1302 (let ((size (widget-get field :size)))
1303 (when size
1304 (let ((begin (widget-field-start field))
1305 (end (widget-field-end field)))
1306 (cond ((< (- end begin) size)
1307 ;; Field too small.
1308 (save-excursion
1309 (goto-char end)
1310 (insert-char ?\s (- (+ begin size) end))))
1311 ((> (- end begin) size)
1312 ;; Field too large and
1313 (if (or (< (point) (+ begin size))
1314 (> (point) end))
1315 ;; Point is outside extra space.
1316 (setq begin (+ begin size))
1317 ;; Point is within the extra space.
1318 (setq begin (point)))
1319 (save-excursion
1320 (goto-char end)
1321 (while (and (eq (preceding-char) ?\s)
1322 (> (point) begin))
1323 (delete-char -1)))))))
1324 (widget-specify-secret field))
1325 (widget-apply field :notify field))))
1327 ;;; Widget Functions
1329 ;; These functions are used in the definition of multiple widgets.
1331 (defun widget-parent-action (widget &optional event)
1332 "Tell :parent of WIDGET to handle the :action.
1333 Optional EVENT is the event that triggered the action."
1334 (widget-apply (widget-get widget :parent) :action event))
1336 (defun widget-children-value-delete (widget)
1337 "Delete all :children and :buttons in WIDGET."
1338 (mapc 'widget-delete (widget-get widget :children))
1339 (widget-put widget :children nil)
1340 (mapc 'widget-delete (widget-get widget :buttons))
1341 (widget-put widget :buttons nil))
1343 (defun widget-children-validate (widget)
1344 "All the :children must be valid."
1345 (let ((children (widget-get widget :children))
1346 child found)
1347 (while (and children (not found))
1348 (setq child (car children)
1349 children (cdr children)
1350 found (widget-apply child :validate)))
1351 found))
1353 (defun widget-child-value-get (widget)
1354 "Get the value of the first member of :children in WIDGET."
1355 (widget-value (car (widget-get widget :children))))
1357 (defun widget-child-value-inline (widget)
1358 "Get the inline value of the first member of :children in WIDGET."
1359 (widget-apply (car (widget-get widget :children)) :value-inline))
1361 (defun widget-child-validate (widget)
1362 "The result of validating the first member of :children in WIDGET."
1363 (widget-apply (car (widget-get widget :children)) :validate))
1365 (defun widget-type-value-create (widget)
1366 "Convert and instantiate the value of the :type attribute of WIDGET.
1367 Store the newly created widget in the :children attribute.
1369 The value of the :type attribute should be an unconverted widget type."
1370 (let ((value (widget-get widget :value))
1371 (type (widget-get widget :type)))
1372 (widget-put widget :children
1373 (list (widget-create-child-value widget
1374 (widget-convert type)
1375 value)))))
1377 (defun widget-type-default-get (widget)
1378 "Get default value from the :type attribute of WIDGET.
1380 The value of the :type attribute should be an unconverted widget type."
1381 (widget-default-get (widget-convert (widget-get widget :type))))
1383 (defun widget-type-match (widget value)
1384 "Non-nil if the :type value of WIDGET matches VALUE.
1386 The value of the :type attribute should be an unconverted widget type."
1387 (widget-apply (widget-convert (widget-get widget :type)) :match value))
1389 (defun widget-types-copy (widget)
1390 "Copy :args as widget types in WIDGET."
1391 (widget-put widget :args (mapcar 'widget-copy (widget-get widget :args)))
1392 widget)
1394 ;; Made defsubst to speed up face editor creation.
1395 (defsubst widget-types-convert-widget (widget)
1396 "Convert :args as widget types in WIDGET."
1397 (widget-put widget :args (mapcar 'widget-convert (widget-get widget :args)))
1398 widget)
1400 (defun widget-value-convert-widget (widget)
1401 "Initialize :value from :args in WIDGET."
1402 (let ((args (widget-get widget :args)))
1403 (when args
1404 (widget-put widget :value (car args))
1405 ;; Don't convert :value here, as this is done in `widget-convert'.
1406 ;; (widget-put widget :value (widget-apply widget
1407 ;; :value-to-internal (car args)))
1408 (widget-put widget :args nil)))
1409 widget)
1411 (defun widget-value-value-get (widget)
1412 "Return the :value property of WIDGET."
1413 (widget-get widget :value))
1415 ;;; The `default' Widget.
1417 (define-widget 'default nil
1418 "Basic widget other widgets are derived from."
1419 :value-to-internal (lambda (_widget value) value)
1420 :value-to-external (lambda (_widget value) value)
1421 :button-prefix 'widget-button-prefix
1422 :button-suffix 'widget-button-suffix
1423 :completions-function #'widget-default-completions
1424 :create 'widget-default-create
1425 :indent nil
1426 :offset 0
1427 :format-handler 'widget-default-format-handler
1428 :button-face-get 'widget-default-button-face-get
1429 :mouse-face-get 'widget-default-mouse-face-get
1430 :sample-face-get 'widget-default-sample-face-get
1431 :delete 'widget-default-delete
1432 :copy 'identity
1433 :value-set 'widget-default-value-set
1434 :value-inline 'widget-default-value-inline
1435 :value-delete 'ignore
1436 :default-get 'widget-default-default-get
1437 :menu-tag-get 'widget-default-menu-tag-get
1438 :validate #'ignore
1439 :active 'widget-default-active
1440 :activate 'widget-specify-active
1441 :deactivate 'widget-default-deactivate
1442 :mouse-down-action #'ignore
1443 :action 'widget-default-action
1444 :notify 'widget-default-notify
1445 :prompt-value 'widget-default-prompt-value)
1447 (defvar widget--completing-widget)
1449 (defun widget-default-completions (widget)
1450 "Return completion data, like `completion-at-point-functions' would."
1451 (let ((completions (widget-get widget :completions)))
1452 (if completions
1453 (list (widget-field-start widget)
1454 (max (point) (widget-field-text-end widget))
1455 completions)
1456 (if (widget-get widget :complete)
1457 (lambda () (widget-apply widget :complete))
1458 (if (widget-get widget :complete-function)
1459 (lambda ()
1460 (let ((widget--completing-widget widget))
1461 (call-interactively
1462 (widget-get widget :complete-function)))))))))
1464 (defun widget-default-create (widget)
1465 "Create WIDGET at point in the current buffer."
1466 (widget-specify-insert
1467 (let ((from (point))
1468 button-begin button-end
1469 sample-begin sample-end
1470 doc-begin doc-end
1471 value-pos)
1472 (insert (widget-get widget :format))
1473 (goto-char from)
1474 ;; Parse escapes in format.
1475 (while (re-search-forward "%\\(.\\)" nil t)
1476 (let ((escape (char-after (match-beginning 1))))
1477 (delete-char -2)
1478 (cond ((eq escape ?%)
1479 (insert ?%))
1480 ((eq escape ?\[)
1481 (setq button-begin (point))
1482 (insert (widget-get-indirect widget :button-prefix)))
1483 ((eq escape ?\])
1484 (insert (widget-get-indirect widget :button-suffix))
1485 (setq button-end (point)))
1486 ((eq escape ?\{)
1487 (setq sample-begin (point)))
1488 ((eq escape ?\})
1489 (setq sample-end (point)))
1490 ((eq escape ?n)
1491 (when (widget-get widget :indent)
1492 (insert ?\n)
1493 (insert-char ?\s (widget-get widget :indent))))
1494 ((eq escape ?t)
1495 (let ((image (widget-get widget :tag-glyph))
1496 (tag (substitute-command-keys
1497 (widget-get widget :tag))))
1498 (cond (image
1499 (widget-image-insert widget (or tag "image") image))
1500 (tag
1501 (insert tag))
1503 (princ (widget-get widget :value)
1504 (current-buffer))))))
1505 ((eq escape ?d)
1506 (let ((doc (widget-get widget :doc)))
1507 (when doc
1508 (setq doc-begin (point))
1509 (insert (substitute-command-keys doc))
1510 (while (eq (preceding-char) ?\n)
1511 (delete-char -1))
1512 (insert ?\n)
1513 (setq doc-end (point)))))
1514 ((eq escape ?h)
1515 (widget-add-documentation-string-button widget))
1516 ((eq escape ?v)
1517 (if (and button-begin (not button-end))
1518 (widget-apply widget :value-create)
1519 (setq value-pos (point))))
1521 (widget-apply widget :format-handler escape)))))
1522 ;; Specify button, sample, and doc, and insert value.
1523 (and button-begin button-end
1524 (widget-specify-button widget button-begin button-end))
1525 (and sample-begin sample-end
1526 (widget-specify-sample widget sample-begin sample-end))
1527 (and doc-begin doc-end
1528 (widget-specify-doc widget doc-begin doc-end))
1529 (when value-pos
1530 (goto-char value-pos)
1531 (widget-apply widget :value-create)))
1532 (let ((from (point-min-marker))
1533 (to (point-max-marker)))
1534 (set-marker-insertion-type from t)
1535 (set-marker-insertion-type to nil)
1536 (widget-put widget :from from)
1537 (widget-put widget :to to)))
1538 (widget-clear-undo))
1540 (defun widget-default-format-handler (_widget escape)
1541 (error "Unknown escape `%c'" escape))
1543 (defun widget-default-button-face-get (widget)
1544 ;; Use :button-face or widget-button-face
1545 (or (widget-get widget :button-face)
1546 (let ((parent (widget-get widget :parent)))
1547 (if parent
1548 (widget-apply parent :button-face-get)
1549 widget-button-face))))
1551 (defun widget-default-mouse-face-get (widget)
1552 ;; Use :mouse-face or widget-mouse-face
1553 (or (widget-get widget :mouse-face)
1554 (let ((parent (widget-get widget :parent)))
1555 (if parent
1556 (widget-apply parent :mouse-face-get)
1557 widget-mouse-face))))
1559 (defun widget-default-sample-face-get (widget)
1560 ;; Use :sample-face.
1561 (widget-get widget :sample-face))
1563 (defun widget-default-delete (widget)
1564 "Remove widget from the buffer."
1565 (let ((from (widget-get widget :from))
1566 (to (widget-get widget :to))
1567 (inactive-overlay (widget-get widget :inactive))
1568 (button-overlay (widget-get widget :button-overlay))
1569 (sample-overlay (widget-get widget :sample-overlay))
1570 (doc-overlay (widget-get widget :doc-overlay))
1571 (inhibit-modification-hooks t)
1572 (inhibit-read-only t))
1573 (widget-apply widget :value-delete)
1574 (widget-children-value-delete widget)
1575 (when inactive-overlay
1576 (delete-overlay inactive-overlay))
1577 (when button-overlay
1578 (delete-overlay button-overlay))
1579 (when sample-overlay
1580 (delete-overlay sample-overlay))
1581 (when doc-overlay
1582 (delete-overlay doc-overlay))
1583 (when (< from to)
1584 ;; Kludge: this doesn't need to be true for empty formats.
1585 (delete-region from to))
1586 (set-marker from nil)
1587 (set-marker to nil))
1588 (widget-clear-undo))
1590 (defun widget-default-value-set (widget value)
1591 "Recreate widget with new value."
1592 (let* ((old-pos (point))
1593 (from (copy-marker (widget-get widget :from)))
1594 (to (copy-marker (widget-get widget :to)))
1595 (offset (if (and (<= from old-pos) (<= old-pos to))
1596 (if (>= old-pos (1- to))
1597 (- old-pos to 1)
1598 (- old-pos from)))))
1599 ;;??? Bug: this ought to insert the new value before deleting the old one,
1600 ;; so that markers on either side of the value automatically
1601 ;; stay on the same side. -- rms.
1602 (save-excursion
1603 (goto-char (widget-get widget :from))
1604 (widget-apply widget :delete)
1605 (widget-put widget :value value)
1606 (widget-apply widget :create))
1607 (if offset
1608 (if (< offset 0)
1609 (goto-char (+ (widget-get widget :to) offset 1))
1610 (goto-char (min (+ from offset) (1- (widget-get widget :to))))))))
1612 (defun widget-default-value-inline (widget)
1613 "Wrap value in a list unless it is inline."
1614 (if (widget-get widget :inline)
1615 (widget-value widget)
1616 (list (widget-value widget))))
1618 (defun widget-default-default-get (widget)
1619 "Get `:value'."
1620 (widget-get widget :value))
1622 (defun widget-default-menu-tag-get (widget)
1623 "Use tag or value for menus."
1624 (or (widget-get widget :menu-tag)
1625 (widget-get widget :tag)
1626 (widget-princ-to-string (widget-get widget :value))))
1628 (defun widget-default-active (widget)
1629 "Return t if this widget is active (user modifiable)."
1630 (or (widget-get widget :always-active)
1631 (and (not (widget-get widget :inactive))
1632 (let ((parent (widget-get widget :parent)))
1633 (or (null parent)
1634 (widget-apply parent :active))))))
1636 (defun widget-default-deactivate (widget)
1637 "Make WIDGET inactive for user modifications."
1638 (widget-specify-inactive widget
1639 (widget-get widget :from)
1640 (widget-get widget :to)))
1642 (defun widget-default-action (widget &optional event)
1643 "Notify the parent when a widget changes."
1644 (let ((parent (widget-get widget :parent)))
1645 (when parent
1646 (widget-apply parent :notify widget event))))
1648 (defun widget-default-notify (widget _child &optional event)
1649 "Pass notification to parent."
1650 (widget-default-action widget event))
1652 (defun widget-default-prompt-value (_widget prompt _value _unbound)
1653 "Read an arbitrary value."
1654 (eval-minibuffer prompt))
1656 (defun widget-docstring (widget)
1657 "Return the documentation string specified by WIDGET, or nil if none.
1658 If WIDGET has a `:doc' property, that specifies the documentation string.
1659 Otherwise, try the `:documentation-property' property. If this
1660 is a function, call it with the widget's value as an argument; if
1661 it is a symbol, use this symbol together with the widget's value
1662 as the argument to `documentation-property'."
1663 (let ((doc (or (widget-get widget :doc)
1664 (let ((doc-prop (widget-get widget :documentation-property))
1665 (value (widget-get widget :value)))
1666 (cond ((functionp doc-prop)
1667 (funcall doc-prop value))
1668 ((symbolp doc-prop)
1669 (documentation-property value doc-prop t)))))))
1670 (when (and (stringp doc) (> (length doc) 0))
1671 ;; Remove any redundant `*' in the beginning.
1672 (when (eq (aref doc 0) ?*)
1673 (setq doc (substring doc 1)))
1674 ;; Remove trailing newlines.
1675 (when (string-match "\n+\\'" doc)
1676 (setq doc (substring doc 0 (match-beginning 0))))
1677 doc)))
1679 ;;; The `item' Widget.
1681 (define-widget 'item 'default
1682 "Constant items for inclusion in other widgets."
1683 :convert-widget 'widget-value-convert-widget
1684 :value-create 'widget-item-value-create
1685 :value-delete 'ignore
1686 :value-get 'widget-value-value-get
1687 :match 'widget-item-match
1688 :match-inline 'widget-item-match-inline
1689 :action 'widget-item-action
1690 :format "%t\n")
1692 (defun widget-item-value-create (widget)
1693 "Insert the printed representation of the value."
1694 (princ (widget-get widget :value) (current-buffer)))
1696 (defun widget-item-match (widget value)
1697 ;; Match if the value is the same.
1698 (equal (widget-get widget :value) value))
1700 (defun widget-item-match-inline (widget vals)
1701 ;; Match if the value is the same.
1702 (let ((value (widget-get widget :value)))
1703 (and (listp value)
1704 (<= (length value) (length vals))
1705 (let ((head (widget-sublist vals 0 (length value))))
1706 (and (equal head value)
1707 (cons head (widget-sublist vals (length value))))))))
1709 (defun widget-sublist (list start &optional end)
1710 "Return the sublist of LIST from START to END.
1711 If END is omitted, it defaults to the length of LIST."
1712 (if (> start 0) (setq list (nthcdr start list)))
1713 (if end
1714 (unless (<= end start)
1715 (setq list (copy-sequence list))
1716 (setcdr (nthcdr (- end start 1) list) nil)
1717 list)
1718 (copy-sequence list)))
1720 (defun widget-item-action (widget &optional event)
1721 ;; Just notify itself.
1722 (widget-apply widget :notify widget event))
1724 ;;; The `push-button' Widget.
1726 ;; (defcustom widget-push-button-gui t
1727 ;; "If non-nil, use GUI push buttons when available."
1728 ;; :group 'widgets
1729 ;; :type 'boolean)
1731 ;; Cache already created GUI objects.
1732 ;; (defvar widget-push-button-cache nil)
1734 (defcustom widget-push-button-prefix "["
1735 "String used as prefix for buttons."
1736 :type 'string
1737 :group 'widget-button)
1739 (defcustom widget-push-button-suffix "]"
1740 "String used as suffix for buttons."
1741 :type 'string
1742 :group 'widget-button)
1744 (define-widget 'push-button 'item
1745 "A pushable button."
1746 :button-prefix ""
1747 :button-suffix ""
1748 :value-create 'widget-push-button-value-create
1749 :format "%[%v%]")
1751 (defun widget-push-button-value-create (widget)
1752 "Insert text representing the `on' and `off' states."
1753 (let* ((tag (or (substitute-command-keys (widget-get widget :tag))
1754 (widget-get widget :value)))
1755 (tag-glyph (widget-get widget :tag-glyph))
1756 (text (concat widget-push-button-prefix
1757 tag widget-push-button-suffix)))
1758 (if tag-glyph
1759 (widget-image-insert widget text tag-glyph)
1760 (insert text))))
1762 ;; (defun widget-gui-action (widget)
1763 ;; "Apply :action for WIDGET."
1764 ;; (widget-apply-action widget (this-command-keys)))
1766 ;;; The `link' Widget.
1768 (defcustom widget-link-prefix "["
1769 "String used as prefix for links."
1770 :type 'string
1771 :group 'widget-button)
1773 (defcustom widget-link-suffix "]"
1774 "String used as suffix for links."
1775 :type 'string
1776 :group 'widget-button)
1778 (define-widget 'link 'item
1779 "An embedded link."
1780 :button-prefix 'widget-link-prefix
1781 :button-suffix 'widget-link-suffix
1782 ;; The `follow-link' property should only be used in those contexts where the
1783 ;; mouse-1 event normally doesn't follow the link, yet the `link' widget
1784 ;; seems to almost always be used in contexts where (down-)mouse-1 is bound
1785 ;; to `widget-button-click' and hence the "mouse-1 to mouse-2" remapping is
1786 ;; not necessary (and can even be harmful). So let's not add a :follow-link
1787 ;; by default. See (bug#22434).
1788 ;; :follow-link 'mouse-face
1789 :help-echo "Follow the link."
1790 :format "%[%t%]")
1792 ;;; The `info-link' Widget.
1794 (define-widget 'info-link 'link
1795 "A link to an info file."
1796 :action 'widget-info-link-action)
1798 (defun widget-info-link-action (widget &optional _event)
1799 "Open the info node specified by WIDGET."
1800 (info (widget-value widget)))
1802 ;;; The `url-link' Widget.
1804 (define-widget 'url-link 'link
1805 "A link to an www page."
1806 :action 'widget-url-link-action)
1808 (defun widget-url-link-action (widget &optional _event)
1809 "Open the URL specified by WIDGET."
1810 (browse-url (widget-value widget)))
1812 ;;; The `function-link' Widget.
1814 (define-widget 'function-link 'link
1815 "A link to an Emacs function."
1816 :action 'widget-function-link-action)
1818 (defun widget-function-link-action (widget &optional _event)
1819 "Show the function specified by WIDGET."
1820 (describe-function (widget-value widget)))
1822 ;;; The `variable-link' Widget.
1824 (define-widget 'variable-link 'link
1825 "A link to an Emacs variable."
1826 :action 'widget-variable-link-action)
1828 (defun widget-variable-link-action (widget &optional _event)
1829 "Show the variable specified by WIDGET."
1830 (describe-variable (widget-value widget)))
1832 ;;; The `file-link' Widget.
1834 (define-widget 'file-link 'link
1835 "A link to a file."
1836 :action 'widget-file-link-action)
1838 (defun widget-file-link-action (widget &optional _event)
1839 "Find the file specified by WIDGET."
1840 (find-file (widget-value widget)))
1842 ;;; The `emacs-library-link' Widget.
1844 (define-widget 'emacs-library-link 'link
1845 "A link to an Emacs Lisp library file."
1846 :action 'widget-emacs-library-link-action)
1848 (defun widget-emacs-library-link-action (widget &optional _event)
1849 "Find the Emacs library file specified by WIDGET."
1850 (find-file (locate-library (widget-value widget))))
1852 ;;; The `emacs-commentary-link' Widget.
1854 (define-widget 'emacs-commentary-link 'link
1855 "A link to Commentary in an Emacs Lisp library file."
1856 :action 'widget-emacs-commentary-link-action)
1858 (defun widget-emacs-commentary-link-action (widget &optional _event)
1859 "Find the Commentary section of the Emacs file specified by WIDGET."
1860 (finder-commentary (widget-value widget)))
1862 ;;; The `editable-field' Widget.
1864 (define-widget 'editable-field 'default
1865 "An editable text field.
1866 Note: In an `editable-field' widget, the `%v' escape must be preceded
1867 by some other text in the `:format' string (if specified)."
1868 :convert-widget 'widget-value-convert-widget
1869 :keymap widget-field-keymap
1870 :format "%v"
1871 :help-echo "M-TAB: complete field; RET: enter value"
1872 :value ""
1873 :prompt-internal 'widget-field-prompt-internal
1874 :prompt-history 'widget-field-history
1875 :prompt-value 'widget-field-prompt-value
1876 :action 'widget-field-action
1877 :validate 'widget-field-validate
1878 :valid-regexp ""
1879 :error "Field's value doesn't match allowed forms"
1880 :value-create 'widget-field-value-create
1881 :value-set 'widget-field-value-set
1882 :value-delete 'widget-field-value-delete
1883 :value-get 'widget-field-value-get
1884 :match 'widget-field-match)
1886 (defvar widget-field-history nil
1887 "History of field minibuffer edits.")
1889 (defun widget-field-prompt-internal (_widget prompt initial history)
1890 "Read string for WIDGET prompting with PROMPT.
1891 INITIAL is the initial input and HISTORY is a symbol containing
1892 the earlier input."
1893 (read-string prompt initial history))
1895 (defun widget-field-prompt-value (widget prompt value unbound)
1896 "Prompt for a string."
1897 (widget-apply widget
1898 :value-to-external
1899 (widget-apply widget
1900 :prompt-internal prompt
1901 (unless unbound
1902 (cons (widget-apply widget
1903 :value-to-internal value)
1905 (widget-get widget :prompt-history))))
1907 (defvar widget-edit-functions nil)
1909 (defun widget-field-action (widget &optional _event)
1910 "Move to next field."
1911 (widget-forward 1)
1912 (run-hook-with-args 'widget-edit-functions widget))
1914 (defun widget-field-validate (widget)
1915 "Valid if the content matches `:valid-regexp'."
1916 (unless (string-match (widget-get widget :valid-regexp)
1917 (widget-apply widget :value-get))
1918 widget))
1920 (defun widget-field-value-set (widget value)
1921 "Set an editable text field WIDGET to VALUE"
1922 (let ((from (widget-field-start widget))
1923 (to (widget-field-text-end widget))
1924 (buffer (widget-field-buffer widget)))
1925 (when (and from to (buffer-live-p buffer))
1926 (with-current-buffer buffer
1927 (goto-char from)
1928 (delete-char (- to from))
1929 (insert value)))))
1931 (defun widget-field-value-create (widget)
1932 "Create an editable text field."
1933 (let ((size (widget-get widget :size))
1934 (value (widget-get widget :value))
1935 (from (point))
1936 ;; This is changed to a real overlay in `widget-setup'. We
1937 ;; need the end points to behave differently until
1938 ;; `widget-setup' is called.
1939 (overlay (cons (make-marker) (make-marker))))
1940 (widget-put widget :field-overlay overlay)
1941 (insert value)
1942 (and size
1943 (< (length value) size)
1944 (insert-char ?\s (- size (length value))))
1945 (unless (memq widget widget-field-list)
1946 (setq widget-field-new (cons widget widget-field-new)))
1947 (move-marker (cdr overlay) (point))
1948 (set-marker-insertion-type (cdr overlay) nil)
1949 (when (null size)
1950 (insert ?\n))
1951 (move-marker (car overlay) from)
1952 (set-marker-insertion-type (car overlay) t)))
1954 (defun widget-field-value-delete (widget)
1955 "Remove the widget from the list of active editing fields."
1956 (setq widget-field-list (delq widget widget-field-list))
1957 (setq widget-field-new (delq widget widget-field-new))
1958 ;; These are nil if the :format string doesn't contain `%v'.
1959 (let ((overlay (widget-get widget :field-overlay)))
1960 (when (overlayp overlay)
1961 (delete-overlay overlay))))
1963 (defun widget-field-value-get (widget &optional no-truncate)
1964 "Return current text in editing field.
1965 Normally, trailing spaces within the editing field are truncated.
1966 But if NO-TRUNCATE is non-nil, include them."
1967 (let ((from (widget-field-start widget))
1968 (to (if no-truncate
1969 (widget-field-end widget)
1970 (widget-field-text-end widget)))
1971 (buffer (widget-field-buffer widget))
1972 (secret (widget-get widget :secret))
1973 (old (current-buffer)))
1974 (if (and from to)
1975 (progn
1976 (set-buffer buffer)
1977 (let ((result (buffer-substring-no-properties from to)))
1978 (when secret
1979 (let ((index 0))
1980 (while (< (+ from index) to)
1981 (aset result index
1982 (get-char-property (+ from index) 'secret))
1983 (setq index (1+ index)))))
1984 (set-buffer old)
1985 result))
1986 (widget-get widget :value))))
1988 (defun widget-field-match (_widget value)
1989 ;; Match any string.
1990 (stringp value))
1992 ;;; The `text' Widget.
1994 (define-widget 'text 'editable-field
1995 "A multiline text area."
1996 :keymap widget-text-keymap)
1998 ;;; The `menu-choice' Widget.
2000 (define-widget 'menu-choice 'default
2001 "A menu of options."
2002 :convert-widget 'widget-types-convert-widget
2003 :copy 'widget-types-copy
2004 :format "%[%t%]: %v"
2005 :case-fold t
2006 :tag "choice"
2007 :void '(item :format "invalid (%t)\n")
2008 :value-create 'widget-choice-value-create
2009 :value-get 'widget-child-value-get
2010 :value-inline 'widget-child-value-inline
2011 :default-get 'widget-choice-default-get
2012 :mouse-down-action 'widget-choice-mouse-down-action
2013 :action 'widget-choice-action
2014 :error "Make a choice"
2015 :validate 'widget-choice-validate
2016 :match 'widget-choice-match
2017 :match-inline 'widget-choice-match-inline)
2019 (defun widget-choice-value-create (widget)
2020 "Insert the first choice that matches the value."
2021 (let ((value (widget-get widget :value))
2022 (args (widget-get widget :args))
2023 (explicit (widget-get widget :explicit-choice))
2024 current)
2025 (if explicit
2026 (progn
2027 ;; If the user specified the choice for this value,
2028 ;; respect that choice.
2029 (widget-put widget :children (list (widget-create-child-value
2030 widget explicit value)))
2031 (widget-put widget :choice explicit)
2032 (widget-put widget :explicit-choice nil))
2033 (while args
2034 (setq current (car args)
2035 args (cdr args))
2036 (when (widget-apply current :match value)
2037 (widget-put widget :children (list (widget-create-child-value
2038 widget current value)))
2039 (widget-put widget :choice current)
2040 (setq args nil
2041 current nil)))
2042 (when current
2043 (let ((void (widget-get widget :void)))
2044 (widget-put widget :children (list (widget-create-child-and-convert
2045 widget void :value value)))
2046 (widget-put widget :choice void))))))
2048 (defun widget-choice-default-get (widget)
2049 ;; Get default for the first choice.
2050 (widget-default-get (car (widget-get widget :args))))
2052 (defcustom widget-choice-toggle nil
2053 "If non-nil, a binary choice will just toggle between the values.
2054 Otherwise, the user will explicitly have to choose between the values
2055 when he invoked the menu."
2056 :type 'boolean
2057 :group 'widgets)
2059 (defun widget-choice-mouse-down-action (widget &optional _event)
2060 ;; Return non-nil if we need a menu.
2061 (let ((args (widget-get widget :args))
2062 (old (widget-get widget :choice)))
2063 (cond ((not (display-popup-menus-p))
2064 ;; No place to pop up a menu.
2065 nil)
2066 ((< (length args) 2)
2067 ;; Empty or singleton list, just return the value.
2068 nil)
2069 ((> (length args) widget-menu-max-size)
2070 ;; Too long, prompt.
2071 nil)
2072 ((> (length args) 2)
2073 ;; Reasonable sized list, use menu.
2075 ((and widget-choice-toggle (memq old args))
2076 ;; We toggle.
2077 nil)
2079 ;; Ask which of the two.
2080 t))))
2082 (defun widget-choice-action (widget &optional event)
2083 ;; Make a choice.
2084 (let ((args (widget-get widget :args))
2085 (old (widget-get widget :choice))
2086 (tag (widget-apply widget :menu-tag-get))
2087 (completion-ignore-case (widget-get widget :case-fold))
2088 this-explicit
2089 current choices)
2090 ;; Remember old value.
2091 (if (and old (not (widget-apply widget :validate)))
2092 (let* ((external (widget-value widget))
2093 (internal (widget-apply old :value-to-internal external)))
2094 (widget-put old :value internal)))
2095 ;; Find new choice.
2096 (setq current
2097 (cond ((= (length args) 0)
2098 nil)
2099 ((= (length args) 1)
2100 (nth 0 args))
2101 ((and widget-choice-toggle
2102 (= (length args) 2)
2103 (memq old args))
2104 (if (eq old (nth 0 args))
2105 (nth 1 args)
2106 (nth 0 args)))
2108 (while args
2109 (setq current (car args)
2110 args (cdr args))
2111 (setq choices
2112 (cons (cons (widget-apply current :menu-tag-get)
2113 current)
2114 choices)))
2115 (setq this-explicit t)
2116 (widget-choose tag (reverse choices) event))))
2117 (when current
2118 ;; If this was an explicit user choice, record the choice,
2119 ;; so that widget-choice-value-create will respect it.
2120 (when this-explicit
2121 (widget-put widget :explicit-choice current))
2122 (widget-value-set widget (widget-default-get current))
2123 (widget-setup)
2124 (widget-apply widget :notify widget event)))
2125 (run-hook-with-args 'widget-edit-functions widget))
2127 (defun widget-choice-validate (widget)
2128 ;; Valid if we have made a valid choice.
2129 (if (eq (widget-get widget :void) (widget-get widget :choice))
2130 widget
2131 (widget-apply (car (widget-get widget :children)) :validate)))
2133 (defun widget-choice-match (widget value)
2134 ;; Matches if one of the choices matches.
2135 (let ((args (widget-get widget :args))
2136 current found)
2137 (while (and args (not found))
2138 (setq current (car args)
2139 args (cdr args)
2140 found (widget-apply current :match value)))
2141 found))
2143 (defun widget-choice-match-inline (widget vals)
2144 ;; Matches if one of the choices matches.
2145 (let ((args (widget-get widget :args))
2146 current found)
2147 (while (and args (null found))
2148 (setq current (car args)
2149 args (cdr args)
2150 found (widget-match-inline current vals)))
2151 found))
2153 ;;; The `toggle' Widget.
2155 (define-widget 'toggle 'item
2156 "Toggle between two states."
2157 :format "%[%v%]\n"
2158 :value-create 'widget-toggle-value-create
2159 :action 'widget-toggle-action
2160 :match (lambda (_widget _value) t)
2161 :on "on"
2162 :off "off")
2164 (defun widget-toggle-value-create (widget)
2165 "Insert text representing the `on' and `off' states."
2166 (let* ((val (widget-value widget))
2167 (text (substitute-command-keys
2168 (widget-get widget (if val :on :off))))
2169 (img (widget-image-find
2170 (widget-get widget (if val :on-glyph :off-glyph)))))
2171 (widget-image-insert widget (or text "")
2172 (if img
2173 (append img '(:ascent center))))))
2175 (defun widget-toggle-action (widget &optional event)
2176 ;; Toggle value.
2177 (widget-value-set widget (not (widget-value widget)))
2178 (widget-apply widget :notify widget event)
2179 (run-hook-with-args 'widget-edit-functions widget))
2181 ;;; The `checkbox' Widget.
2183 (define-widget 'checkbox 'toggle
2184 "A checkbox toggle."
2185 :button-suffix ""
2186 :button-prefix ""
2187 :format "%[%v%]"
2188 :on "[X]"
2189 ;; We could probably do the same job as the images using single
2190 ;; space characters in a boxed face with a stretch specification to
2191 ;; make them square.
2192 :on-glyph "checked"
2193 :off "[ ]"
2194 :off-glyph "unchecked"
2195 :help-echo "Toggle this item."
2196 :action 'widget-checkbox-action)
2198 (defun widget-checkbox-action (widget &optional event)
2199 "Toggle checkbox, notify parent, and set active state of sibling."
2200 (widget-toggle-action widget event)
2201 (let ((sibling (widget-get-sibling widget)))
2202 (when sibling
2203 (if (widget-value widget)
2204 (widget-apply sibling :activate)
2205 (widget-apply sibling :deactivate))
2206 (widget-clear-undo))))
2208 ;;; The `checklist' Widget.
2210 (define-widget 'checklist 'default
2211 "A multiple choice widget."
2212 :convert-widget 'widget-types-convert-widget
2213 :copy 'widget-types-copy
2214 :format "%v"
2215 :offset 4
2216 :entry-format "%b %v"
2217 :greedy nil
2218 :value-create 'widget-checklist-value-create
2219 :value-get 'widget-checklist-value-get
2220 :validate 'widget-checklist-validate
2221 :match 'widget-checklist-match
2222 :match-inline 'widget-checklist-match-inline)
2224 (defun widget-checklist-value-create (widget)
2225 ;; Insert all values
2226 (let ((alist (widget-checklist-match-find widget))
2227 (args (widget-get widget :args)))
2228 (dolist (item args)
2229 (widget-checklist-add-item widget item (assq item alist)))
2230 (widget-put widget :children (nreverse (widget-get widget :children)))))
2232 (defun widget-checklist-add-item (widget type chosen)
2233 "Create checklist item in WIDGET of type TYPE.
2234 If the item is checked, CHOSEN is a cons whose cdr is the value."
2235 (and (eq (preceding-char) ?\n)
2236 (widget-get widget :indent)
2237 (insert-char ?\s (widget-get widget :indent)))
2238 (widget-specify-insert
2239 (let* ((children (widget-get widget :children))
2240 (buttons (widget-get widget :buttons))
2241 (button-args (or (widget-get type :sibling-args)
2242 (widget-get widget :button-args)))
2243 (from (point))
2244 child button)
2245 (insert (widget-get widget :entry-format))
2246 (goto-char from)
2247 ;; Parse % escapes in format.
2248 (while (re-search-forward "%\\([bv%]\\)" nil t)
2249 (let ((escape (char-after (match-beginning 1))))
2250 (delete-char -2)
2251 (cond ((eq escape ?%)
2252 (insert ?%))
2253 ((eq escape ?b)
2254 (setq button (apply 'widget-create-child-and-convert
2255 widget 'checkbox
2256 :value (not (null chosen))
2257 button-args)))
2258 ((eq escape ?v)
2259 (setq child
2260 (cond ((not chosen)
2261 (let ((child (widget-create-child widget type)))
2262 (widget-apply child :deactivate)
2263 child))
2264 ((widget-get type :inline)
2265 (widget-create-child-value
2266 widget type (cdr chosen)))
2268 (widget-create-child-value
2269 widget type (car (cdr chosen)))))))
2271 (error "Unknown escape `%c'" escape)))))
2272 ;; Update properties.
2273 (and button child (widget-put child :button button))
2274 (and button (widget-put widget :buttons (cons button buttons)))
2275 (and child (widget-put widget :children (cons child children))))))
2277 (defun widget-checklist-match (widget vals)
2278 ;; All values must match a type in the checklist.
2279 (and (listp vals)
2280 (null (cdr (widget-checklist-match-inline widget vals)))))
2282 (defun widget-checklist-match-inline (widget vals)
2283 ;; Find the values which match a type in the checklist.
2284 (let ((greedy (widget-get widget :greedy))
2285 (args (copy-sequence (widget-get widget :args)))
2286 found rest)
2287 (while vals
2288 (let ((answer (widget-checklist-match-up args vals)))
2289 (cond (answer
2290 (let ((vals2 (widget-match-inline answer vals)))
2291 (setq found (append found (car vals2))
2292 vals (cdr vals2)
2293 args (delq answer args))))
2294 (greedy
2295 (setq rest (append rest (list (car vals)))
2296 vals (cdr vals)))
2298 (setq rest (append rest vals)
2299 vals nil)))))
2300 (cons found rest)))
2302 (defun widget-checklist-match-find (widget &optional vals)
2303 "Find the vals which match a type in the checklist.
2304 Return an alist of (TYPE MATCH)."
2305 (or vals (setq vals (widget-get widget :value)))
2306 (let ((greedy (widget-get widget :greedy))
2307 (args (copy-sequence (widget-get widget :args)))
2308 found)
2309 (while vals
2310 (let ((answer (widget-checklist-match-up args vals)))
2311 (cond (answer
2312 (let ((match (widget-match-inline answer vals)))
2313 (setq found (cons (cons answer (car match)) found)
2314 vals (cdr match)
2315 args (delq answer args))))
2316 (greedy
2317 (setq vals (cdr vals)))
2319 (setq vals nil)))))
2320 found))
2322 (defun widget-checklist-match-up (args vals)
2323 "Return the first type from ARGS that matches VALS."
2324 (let (current found)
2325 (while (and args (null found))
2326 (setq current (car args)
2327 args (cdr args)
2328 found (widget-match-inline current vals)))
2329 (if found
2330 current)))
2332 (defun widget-checklist-value-get (widget)
2333 ;; The values of all selected items.
2334 (let ((children (widget-get widget :children))
2335 child result)
2336 (while children
2337 (setq child (car children)
2338 children (cdr children))
2339 (if (widget-value (widget-get child :button))
2340 (setq result (append result (widget-apply child :value-inline)))))
2341 result))
2343 (defun widget-checklist-validate (widget)
2344 ;; Ticked children must be valid.
2345 (let ((children (widget-get widget :children))
2346 child button found)
2347 (while (and children (not found))
2348 (setq child (car children)
2349 children (cdr children)
2350 button (widget-get child :button)
2351 found (and (widget-value button)
2352 (widget-apply child :validate))))
2353 found))
2355 ;;; The `option' Widget
2357 (define-widget 'option 'checklist
2358 "An widget with an optional item."
2359 :inline t)
2361 ;;; The `choice-item' Widget.
2363 (define-widget 'choice-item 'item
2364 "Button items that delegate action events to their parents."
2365 :action 'widget-parent-action
2366 :format "%[%t%] \n")
2368 ;;; The `radio-button' Widget.
2370 (define-widget 'radio-button 'toggle
2371 "A radio button for use in the `radio' widget."
2372 :notify 'widget-radio-button-notify
2373 :format "%[%v%]"
2374 :button-suffix ""
2375 :button-prefix ""
2376 :on "(*)"
2377 :on-glyph "radio1"
2378 :off "( )"
2379 :off-glyph "radio0")
2381 (defun widget-radio-button-notify (widget _child &optional event)
2382 ;; Tell daddy.
2383 (widget-apply (widget-get widget :parent) :action widget event))
2385 ;;; The `radio-button-choice' Widget.
2387 (define-widget 'radio-button-choice 'default
2388 "Select one of multiple options."
2389 :convert-widget 'widget-types-convert-widget
2390 :copy 'widget-types-copy
2391 :offset 4
2392 :format "%v"
2393 :entry-format "%b %v"
2394 :value-create 'widget-radio-value-create
2395 :value-get 'widget-radio-value-get
2396 :value-inline 'widget-radio-value-inline
2397 :value-set 'widget-radio-value-set
2398 :error "You must push one of the buttons"
2399 :validate 'widget-radio-validate
2400 :match 'widget-choice-match
2401 :match-inline 'widget-choice-match-inline
2402 :action 'widget-radio-action)
2404 (defun widget-radio-value-create (widget)
2405 ;; Insert all values
2406 (let ((args (widget-get widget :args))
2407 arg)
2408 (while args
2409 (setq arg (car args)
2410 args (cdr args))
2411 (widget-radio-add-item widget arg))))
2413 (defun widget-radio-add-item (widget type)
2414 "Add to radio widget WIDGET a new radio button item of type TYPE."
2415 ;; (setq type (widget-convert type))
2416 (and (eq (preceding-char) ?\n)
2417 (widget-get widget :indent)
2418 (insert-char ?\s (widget-get widget :indent)))
2419 (widget-specify-insert
2420 (let* ((value (widget-get widget :value))
2421 (children (widget-get widget :children))
2422 (buttons (widget-get widget :buttons))
2423 (button-args (or (widget-get type :sibling-args)
2424 (widget-get widget :button-args)))
2425 (from (point))
2426 (chosen (and (null (widget-get widget :choice))
2427 (widget-apply type :match value)))
2428 child button)
2429 (insert (widget-get widget :entry-format))
2430 (goto-char from)
2431 ;; Parse % escapes in format.
2432 (while (re-search-forward "%\\([bv%]\\)" nil t)
2433 (let ((escape (char-after (match-beginning 1))))
2434 (delete-char -2)
2435 (cond ((eq escape ?%)
2436 (insert ?%))
2437 ((eq escape ?b)
2438 (setq button (apply 'widget-create-child-and-convert
2439 widget 'radio-button
2440 :value (not (null chosen))
2441 button-args)))
2442 ((eq escape ?v)
2443 (setq child (if chosen
2444 (widget-create-child-value
2445 widget type value)
2446 (widget-create-child widget type)))
2447 (unless chosen
2448 (widget-apply child :deactivate)))
2450 (error "Unknown escape `%c'" escape)))))
2451 ;; Update properties.
2452 (when chosen
2453 (widget-put widget :choice type))
2454 (when button
2455 (widget-put child :button button)
2456 (widget-put widget :buttons (nconc buttons (list button))))
2457 (when child
2458 (widget-put widget :children (nconc children (list child))))
2459 child)))
2461 (defun widget-radio-value-get (widget)
2462 ;; Get value of the child widget.
2463 (let ((chosen (widget-radio-chosen widget)))
2464 (and chosen (widget-value chosen))))
2466 (defun widget-radio-chosen (widget)
2467 "Return the widget representing the chosen radio button."
2468 (let ((children (widget-get widget :children))
2469 current found)
2470 (while children
2471 (setq current (car children)
2472 children (cdr children))
2473 (when (widget-apply (widget-get current :button) :value-get)
2474 (setq found current
2475 children nil)))
2476 found))
2478 (defun widget-radio-value-inline (widget)
2479 ;; Get value of the child widget.
2480 (let ((children (widget-get widget :children))
2481 current found)
2482 (while children
2483 (setq current (car children)
2484 children (cdr children))
2485 (when (widget-apply (widget-get current :button) :value-get)
2486 (setq found (widget-apply current :value-inline)
2487 children nil)))
2488 found))
2490 (defun widget-radio-value-set (widget value)
2491 ;; We can't just delete and recreate a radio widget, since children
2492 ;; can be added after the original creation and won't be recreated
2493 ;; by `:create'.
2494 (let ((children (widget-get widget :children))
2495 current found)
2496 (while children
2497 (setq current (car children)
2498 children (cdr children))
2499 (let* ((button (widget-get current :button))
2500 (match (and (not found)
2501 (widget-apply current :match value))))
2502 (widget-value-set button match)
2503 (if match
2504 (progn
2505 (widget-value-set current value)
2506 (widget-apply current :activate))
2507 (widget-apply current :deactivate))
2508 (setq found (or found match))))))
2510 (defun widget-radio-validate (widget)
2511 ;; Valid if we have made a valid choice.
2512 (let ((children (widget-get widget :children))
2513 current found button)
2514 (while (and children (not found))
2515 (setq current (car children)
2516 children (cdr children)
2517 button (widget-get current :button)
2518 found (widget-apply button :value-get)))
2519 (if found
2520 (widget-apply current :validate)
2521 widget)))
2523 (defun widget-radio-action (widget child event)
2524 ;; Check if a radio button was pressed.
2525 (let ((children (widget-get widget :children))
2526 (buttons (widget-get widget :buttons))
2527 current)
2528 (when (memq child buttons)
2529 (while children
2530 (setq current (car children)
2531 children (cdr children))
2532 (let* ((button (widget-get current :button)))
2533 (cond ((eq child button)
2534 (widget-value-set button t)
2535 (widget-apply current :activate))
2536 ((widget-value button)
2537 (widget-value-set button nil)
2538 (widget-apply current :deactivate)))))))
2539 ;; Pass notification to parent.
2540 (widget-apply widget :notify child event))
2542 ;;; The `insert-button' Widget.
2544 (define-widget 'insert-button 'push-button
2545 "An insert button for the `editable-list' widget."
2546 :tag "INS"
2547 :help-echo "Insert a new item into the list at this position."
2548 :action 'widget-insert-button-action)
2550 (defun widget-insert-button-action (widget &optional _event)
2551 ;; Ask the parent to insert a new item.
2552 (widget-apply (widget-get widget :parent)
2553 :insert-before (widget-get widget :widget)))
2555 ;;; The `delete-button' Widget.
2557 (define-widget 'delete-button 'push-button
2558 "A delete button for the `editable-list' widget."
2559 :tag "DEL"
2560 :help-echo "Delete this item from the list."
2561 :action 'widget-delete-button-action)
2563 (defun widget-delete-button-action (widget &optional _event)
2564 ;; Ask the parent to insert a new item.
2565 (widget-apply (widget-get widget :parent)
2566 :delete-at (widget-get widget :widget)))
2568 ;;; The `editable-list' Widget.
2570 ;; (defcustom widget-editable-list-gui nil
2571 ;; "If non-nil, use GUI push-buttons in editable list when available."
2572 ;; :type 'boolean
2573 ;; :group 'widgets)
2575 (define-widget 'editable-list 'default
2576 "A variable list of widgets of the same type."
2577 :convert-widget 'widget-types-convert-widget
2578 :copy 'widget-types-copy
2579 :offset 12
2580 :format "%v%i\n"
2581 :format-handler 'widget-editable-list-format-handler
2582 :entry-format "%i %d %v"
2583 :value-create 'widget-editable-list-value-create
2584 :value-get 'widget-editable-list-value-get
2585 :validate 'widget-children-validate
2586 :match 'widget-editable-list-match
2587 :match-inline 'widget-editable-list-match-inline
2588 :insert-before 'widget-editable-list-insert-before
2589 :delete-at 'widget-editable-list-delete-at)
2591 (defun widget-editable-list-format-handler (widget escape)
2592 ;; We recognize the insert button.
2593 ;; (let ((widget-push-button-gui widget-editable-list-gui))
2594 (cond ((eq escape ?i)
2595 (and (widget-get widget :indent)
2596 (insert-char ?\s (widget-get widget :indent)))
2597 (apply 'widget-create-child-and-convert
2598 widget 'insert-button
2599 (widget-get widget :append-button-args)))
2601 (widget-default-format-handler widget escape)))
2602 ;; )
2605 (defun widget-editable-list-value-create (widget)
2606 ;; Insert all values
2607 (let* ((value (widget-get widget :value))
2608 (type (nth 0 (widget-get widget :args)))
2609 children)
2610 (widget-put widget :value-pos (point-marker))
2611 (set-marker-insertion-type (widget-get widget :value-pos) t)
2612 (while value
2613 (let ((answer (widget-match-inline type value)))
2614 (if answer
2615 (setq children (cons (widget-editable-list-entry-create
2616 widget
2617 (if (widget-get type :inline)
2618 (car answer)
2619 (car (car answer)))
2621 children)
2622 value (cdr answer))
2623 (setq value nil))))
2624 (widget-put widget :children (nreverse children))))
2626 (defun widget-editable-list-value-get (widget)
2627 ;; Get value of the child widget.
2628 (apply 'append (mapcar (lambda (child) (widget-apply child :value-inline))
2629 (widget-get widget :children))))
2631 (defun widget-editable-list-match (widget value)
2632 ;; Value must be a list and all the members must match the type.
2633 (and (listp value)
2634 (null (cdr (widget-editable-list-match-inline widget value)))))
2636 (defun widget-editable-list-match-inline (widget value)
2637 (let ((type (nth 0 (widget-get widget :args)))
2638 (ok t)
2639 found)
2640 (while (and value ok)
2641 (let ((answer (widget-match-inline type value)))
2642 (if answer
2643 (setq found (append found (car answer))
2644 value (cdr answer))
2645 (setq ok nil))))
2646 (cons found value)))
2648 (defun widget-editable-list-insert-before (widget before)
2649 ;; Insert a new child in the list of children.
2650 (save-excursion
2651 (let ((children (widget-get widget :children))
2652 (inhibit-read-only t)
2653 (inhibit-modification-hooks t))
2654 (cond (before
2655 (goto-char (widget-get before :entry-from)))
2657 (goto-char (widget-get widget :value-pos))))
2658 (let ((child (widget-editable-list-entry-create
2659 widget nil nil)))
2660 (when (< (widget-get child :entry-from) (widget-get widget :from))
2661 (set-marker (widget-get widget :from)
2662 (widget-get child :entry-from)))
2663 (if (eq (car children) before)
2664 (widget-put widget :children (cons child children))
2665 (while (not (eq (car (cdr children)) before))
2666 (setq children (cdr children)))
2667 (setcdr children (cons child (cdr children)))))))
2668 (widget-setup)
2669 (widget-apply widget :notify widget))
2671 (defun widget-editable-list-delete-at (widget child)
2672 ;; Delete child from list of children.
2673 (save-excursion
2674 (let ((buttons (copy-sequence (widget-get widget :buttons)))
2675 button
2676 (inhibit-read-only t)
2677 (inhibit-modification-hooks t))
2678 (while buttons
2679 (setq button (car buttons)
2680 buttons (cdr buttons))
2681 (when (eq (widget-get button :widget) child)
2682 (widget-put widget
2683 :buttons (delq button (widget-get widget :buttons)))
2684 (widget-delete button))))
2685 (let ((entry-from (widget-get child :entry-from))
2686 (entry-to (widget-get child :entry-to))
2687 (inhibit-read-only t)
2688 (inhibit-modification-hooks t))
2689 (widget-delete child)
2690 (delete-region entry-from entry-to)
2691 (set-marker entry-from nil)
2692 (set-marker entry-to nil))
2693 (widget-put widget :children (delq child (widget-get widget :children))))
2694 (widget-setup)
2695 (widget-apply widget :notify widget))
2697 (defun widget-editable-list-entry-create (widget value conv)
2698 ;; Create a new entry to the list.
2699 (let ((type (nth 0 (widget-get widget :args)))
2700 ;; (widget-push-button-gui widget-editable-list-gui)
2701 child delete insert)
2702 (widget-specify-insert
2703 (save-excursion
2704 (and (widget-get widget :indent)
2705 (insert-char ?\s (widget-get widget :indent)))
2706 (insert (widget-get widget :entry-format)))
2707 ;; Parse % escapes in format.
2708 (while (re-search-forward "%\\(.\\)" nil t)
2709 (let ((escape (char-after (match-beginning 1))))
2710 (delete-char -2)
2711 (cond ((eq escape ?%)
2712 (insert ?%))
2713 ((eq escape ?i)
2714 (setq insert (apply 'widget-create-child-and-convert
2715 widget 'insert-button
2716 (widget-get widget :insert-button-args))))
2717 ((eq escape ?d)
2718 (setq delete (apply 'widget-create-child-and-convert
2719 widget 'delete-button
2720 (widget-get widget :delete-button-args))))
2721 ((eq escape ?v)
2722 (if conv
2723 (setq child (widget-create-child-value
2724 widget type value))
2725 (setq child (widget-create-child-value
2726 widget type (widget-default-get type)))))
2728 (error "Unknown escape `%c'" escape)))))
2729 (let ((buttons (widget-get widget :buttons)))
2730 (if insert (push insert buttons))
2731 (if delete (push delete buttons))
2732 (widget-put widget :buttons buttons))
2733 (let ((entry-from (point-min-marker))
2734 (entry-to (point-max-marker)))
2735 (set-marker-insertion-type entry-from t)
2736 (set-marker-insertion-type entry-to nil)
2737 (widget-put child :entry-from entry-from)
2738 (widget-put child :entry-to entry-to)))
2739 (if insert (widget-put insert :widget child))
2740 (if delete (widget-put delete :widget child))
2741 child))
2743 ;;; The `group' Widget.
2745 (define-widget 'group 'default
2746 "A widget which groups other widgets inside."
2747 :convert-widget 'widget-types-convert-widget
2748 :copy 'widget-types-copy
2749 :format "%v"
2750 :value-create 'widget-group-value-create
2751 :value-get 'widget-editable-list-value-get
2752 :default-get 'widget-group-default-get
2753 :validate 'widget-children-validate
2754 :match 'widget-group-match
2755 :match-inline 'widget-group-match-inline)
2757 (defun widget-group-value-create (widget)
2758 ;; Create each component.
2759 (let ((args (widget-get widget :args))
2760 (value (widget-get widget :value))
2761 arg answer children)
2762 (while args
2763 (setq arg (car args)
2764 args (cdr args)
2765 answer (widget-match-inline arg value)
2766 value (cdr answer))
2767 (and (eq (preceding-char) ?\n)
2768 (widget-get widget :indent)
2769 (insert-char ?\s (widget-get widget :indent)))
2770 (push (cond ((null answer)
2771 (widget-create-child widget arg))
2772 ((widget-get arg :inline)
2773 (widget-create-child-value widget arg (car answer)))
2775 (widget-create-child-value widget arg (car (car answer)))))
2776 children))
2777 (widget-put widget :children (nreverse children))))
2779 (defun widget-group-default-get (widget)
2780 ;; Get the default of the components.
2781 (mapcar 'widget-default-get (widget-get widget :args)))
2783 (defun widget-group-match (widget vals)
2784 ;; Match if the components match.
2785 (and (listp vals)
2786 (let ((match (widget-group-match-inline widget vals)))
2787 (and match (null (cdr match))))))
2789 (defun widget-group-match-inline (widget vals)
2790 ;; Match if the components match.
2791 (let ((args (widget-get widget :args))
2792 argument answer found)
2793 (while args
2794 (setq argument (car args)
2795 args (cdr args))
2796 (if (setq answer (widget-match-inline argument vals))
2797 (setq found (append found (car answer))
2798 vals (cdr answer))
2799 (setq vals nil
2800 args nil)))
2801 (if answer
2802 (cons found vals))))
2804 ;;; The `visibility' Widget.
2806 (define-widget 'visibility 'item
2807 "An indicator and manipulator for hidden items.
2809 The following properties have special meanings for this widget:
2810 :on-glyph Image filename or spec to display when the item is visible.
2811 :on Text shown if the \"on\" image is nil or cannot be displayed.
2812 :off-glyph Image filename or spec to display when the item is hidden.
2813 :off Text shown if the \"off\" image is nil cannot be displayed."
2814 :format "%[%v%]"
2815 :button-prefix ""
2816 :button-suffix ""
2817 :on-glyph "down"
2818 :on "Hide"
2819 :off-glyph "right"
2820 :off "Show"
2821 :value-create 'widget-visibility-value-create
2822 :action 'widget-toggle-action
2823 :match (lambda (_widget _value) t))
2825 (defalias 'widget-visibility-value-create 'widget-toggle-value-create)
2827 ;;; The `documentation-link' Widget.
2829 ;; This is a helper widget for `documentation-string'.
2831 (define-widget 'documentation-link 'link
2832 "Link type used in documentation strings."
2833 :tab-order -1
2834 :help-echo "Describe this symbol"
2835 :action 'widget-documentation-link-action)
2837 (defun widget-documentation-link-action (widget &optional _event)
2838 "Display documentation for WIDGET's value. Ignore optional argument EVENT."
2839 (let* ((string (widget-get widget :value))
2840 (symbol (intern string)))
2841 (if (and (fboundp symbol) (boundp symbol))
2842 ;; If there are two doc strings, give the user a way to pick one.
2843 (apropos (concat "\\`" (regexp-quote string) "\\'"))
2844 (cond
2845 ((fboundp symbol)
2846 (describe-function symbol))
2847 ((facep symbol)
2848 (describe-face symbol))
2849 ((featurep symbol)
2850 (describe-package symbol))
2851 ((or (boundp symbol) (get symbol 'variable-documentation))
2852 (describe-variable symbol))
2854 (message "No documentation available for %s" symbol))))))
2856 (defcustom widget-documentation-links t
2857 "Add hyperlinks to documentation strings when non-nil."
2858 :type 'boolean
2859 :group 'widget-documentation)
2861 (defcustom widget-documentation-link-regexp "['`‘]\\([^\n `'‘’]+\\)['’]"
2862 "Regexp for matching potential links in documentation strings.
2863 The first group should be the link itself."
2864 :type 'regexp
2865 :group 'widget-documentation)
2867 (defcustom widget-documentation-link-p 'intern-soft
2868 "Predicate used to test if a string is useful as a link.
2869 The value should be a function. The function will be called with one
2870 argument, a string, and should return non-nil if there should be a
2871 link for that string."
2872 :type 'function
2873 :options '(widget-documentation-link-p)
2874 :group 'widget-documentation)
2876 (defcustom widget-documentation-link-type 'documentation-link
2877 "Widget type used for links in documentation strings."
2878 :type 'symbol
2879 :group 'widget-documentation)
2881 (defun widget-documentation-link-add (widget from to)
2882 (widget-specify-doc widget from to)
2883 (when widget-documentation-links
2884 (let ((regexp widget-documentation-link-regexp)
2885 (buttons (widget-get widget :buttons))
2886 (widget-mouse-face (default-value 'widget-mouse-face))
2887 (widget-button-face widget-documentation-face)
2888 (widget-button-pressed-face widget-documentation-face))
2889 (save-excursion
2890 (goto-char from)
2891 (while (re-search-forward regexp to t)
2892 (let ((name (match-string 1))
2893 (begin (match-beginning 1))
2894 (end (match-end 1)))
2895 (when (funcall widget-documentation-link-p name)
2896 (push (widget-convert-button widget-documentation-link-type
2897 begin end :value name)
2898 buttons)))))
2899 (widget-put widget :buttons buttons))))
2901 ;;; The `documentation-string' Widget.
2903 (define-widget 'documentation-string 'item
2904 "A documentation string."
2905 :format "%v"
2906 :action 'widget-documentation-string-action
2907 :value-create 'widget-documentation-string-value-create
2908 :visibility-widget 'visibility)
2910 (defun widget-documentation-string-value-create (widget)
2911 ;; Insert documentation string.
2912 (let ((doc (substitute-command-keys (widget-value widget)))
2913 (indent (widget-get widget :indent))
2914 (shown (widget-get (widget-get widget :parent) :documentation-shown))
2915 (start (point)))
2916 (if (string-match "\n" doc)
2917 (let ((before (substring doc 0 (match-beginning 0)))
2918 (after (substring doc (match-end 0)))
2919 button end)
2920 (widget-documentation-string-indent-to indent)
2921 (insert before ?\s)
2922 (widget-documentation-link-add widget start (point))
2923 (setq button
2924 (widget-create-child-and-convert
2925 widget (widget-get widget :visibility-widget)
2926 :help-echo "Show or hide rest of the documentation."
2927 :on "Hide"
2928 :off "More"
2929 :always-active t
2930 :action 'widget-parent-action
2931 shown))
2932 (when shown
2933 (insert ?\n)
2934 (setq start (point))
2935 (when (and indent (not (zerop indent)))
2936 (insert-char ?\s indent))
2937 (insert after)
2938 (setq end (point))
2939 (widget-documentation-link-add widget start end)
2940 ;; Indent the subsequent lines.
2941 (when (and indent (> indent 0))
2942 (save-excursion
2943 (save-restriction
2944 (narrow-to-region start end)
2945 (goto-char (point-min))
2946 (while (search-forward "\n" nil t)
2947 (widget-documentation-string-indent-to indent))))))
2948 (widget-put widget :buttons (list button)))
2949 (widget-documentation-string-indent-to indent)
2950 (insert doc)
2951 (widget-documentation-link-add widget start (point))))
2952 (insert ?\n))
2954 (defun widget-documentation-string-indent-to (col)
2955 (when (and (numberp col)
2956 (> col 0))
2957 (let ((opoint (point)))
2958 (indent-to col)
2959 (put-text-property opoint (point)
2960 'display `(space :align-to ,col)))))
2962 (defun widget-documentation-string-action (widget &rest _ignore)
2963 ;; Toggle documentation.
2964 (let ((parent (widget-get widget :parent)))
2965 (widget-put parent :documentation-shown
2966 (not (widget-get parent :documentation-shown))))
2967 ;; Redraw.
2968 (widget-value-set widget (widget-value widget)))
2970 (defun widget-add-documentation-string-button (widget &rest args)
2971 "Insert a new `documentation-string' widget based on WIDGET.
2972 The new widget becomes a child of WIDGET, and is also added to
2973 its `:buttons' list. The documentation string is found from
2974 WIDGET using the function `widget-docstring'.
2975 Optional ARGS specifies additional keyword arguments for the
2976 `documentation-string' widget."
2977 (let ((doc (widget-docstring widget))
2978 (indent (widget-get widget :indent))
2979 (doc-indent (widget-get widget :documentation-indent)))
2980 (when doc
2981 (and (eq (preceding-char) ?\n)
2982 indent
2983 (insert-char ?\s indent))
2984 (unless (or (numberp doc-indent) (null doc-indent))
2985 (setq doc-indent 0))
2986 (widget-put widget :buttons
2987 (cons (apply 'widget-create-child-and-convert
2988 widget 'documentation-string
2989 :indent doc-indent
2990 (nconc args (list doc)))
2991 (widget-get widget :buttons))))))
2993 ;;; The Sexp Widgets.
2995 (define-widget 'const 'item
2996 "An immutable sexp."
2997 :prompt-value 'widget-const-prompt-value
2998 :format "%t\n%d")
3000 (defun widget-const-prompt-value (widget _prompt _value _unbound)
3001 ;; Return the value of the const.
3002 (widget-value widget))
3004 (define-widget 'function-item 'const
3005 "An immutable function name."
3006 :format "%v\n%h"
3007 :documentation-property (lambda (symbol)
3008 (condition-case nil
3009 (documentation symbol t)
3010 (error nil))))
3012 (define-widget 'variable-item 'const
3013 "An immutable variable name."
3014 :format "%v\n%h"
3015 :documentation-property 'variable-documentation)
3017 (define-widget 'other 'sexp
3018 "Matches any value, but doesn't let the user edit the value.
3019 This is useful as last item in a `choice' widget.
3020 You should use this widget type with a default value,
3021 as in (other DEFAULT) or (other :tag \"NAME\" DEFAULT).
3022 If the user selects this alternative, that specifies DEFAULT
3023 as the value."
3024 :tag "Other"
3025 :format "%t%n"
3026 :value 'other)
3028 (defvar widget-string-prompt-value-history nil
3029 "History of input to `widget-string-prompt-value'.")
3031 (define-widget 'string 'editable-field
3032 "A string"
3033 :tag "String"
3034 :format "%{%t%}: %v"
3035 :complete-function 'ispell-complete-word
3036 :prompt-history 'widget-string-prompt-value-history)
3038 (define-widget 'regexp 'string
3039 "A regular expression."
3040 :match 'widget-regexp-match
3041 :validate 'widget-regexp-validate
3042 ;; Doesn't work well with terminating newline.
3043 ;; :value-face 'widget-single-line-field
3044 :tag "Regexp")
3046 (defun widget-regexp-match (_widget value)
3047 ;; Match valid regexps.
3048 (and (stringp value)
3049 (condition-case nil
3050 (prog1 t
3051 (string-match value ""))
3052 (error nil))))
3054 (defun widget-regexp-validate (widget)
3055 "Check that the value of WIDGET is a valid regexp."
3056 (condition-case data
3057 (prog1 nil
3058 (string-match (widget-value widget) ""))
3059 (error (widget-put widget :error (error-message-string data))
3060 widget)))
3062 (define-widget 'file 'string
3063 "A file widget.
3064 It reads a file name from an editable text field."
3065 :completions #'completion-file-name-table
3066 :prompt-value 'widget-file-prompt-value
3067 :format "%{%t%}: %v"
3068 ;; Doesn't work well with terminating newline.
3069 ;; :value-face 'widget-single-line-field
3070 :tag "File")
3072 (defun widget-file-prompt-value (widget prompt value unbound)
3073 ;; Read file from minibuffer.
3074 (abbreviate-file-name
3075 (if unbound
3076 (read-file-name prompt)
3077 (let ((prompt2 (format "%s (default %s): " prompt value))
3078 (dir (file-name-directory value))
3079 (file (file-name-nondirectory value))
3080 (must-match (widget-get widget :must-match)))
3081 (read-file-name prompt2 dir nil must-match file)))))
3083 ;;;(defun widget-file-action (widget &optional event)
3084 ;;; ;; Read a file name from the minibuffer.
3085 ;;; (let* ((value (widget-value widget))
3086 ;;; (dir (file-name-directory value))
3087 ;;; (file (file-name-nondirectory value))
3088 ;;; (menu-tag (widget-apply widget :menu-tag-get))
3089 ;;; (must-match (widget-get widget :must-match))
3090 ;;; (answer (read-file-name (concat menu-tag " (default " value "): ")
3091 ;;; dir nil must-match file)))
3092 ;;; (widget-value-set widget (abbreviate-file-name answer))
3093 ;;; (widget-setup)
3094 ;;; (widget-apply widget :notify widget event)))
3096 ;; Fixme: use file-name-as-directory.
3097 (define-widget 'directory 'file
3098 "A directory widget.
3099 It reads a directory name from an editable text field."
3100 :tag "Directory")
3102 (defvar widget-symbol-prompt-value-history nil
3103 "History of input to `widget-symbol-prompt-value'.")
3105 (define-widget 'symbol 'editable-field
3106 "A Lisp symbol."
3107 :value nil
3108 :tag "Symbol"
3109 :format "%{%t%}: %v"
3110 :match (lambda (_widget value) (symbolp value))
3111 :completions obarray
3112 :prompt-internal 'widget-symbol-prompt-internal
3113 :prompt-match 'symbolp
3114 :prompt-history 'widget-symbol-prompt-value-history
3115 :value-to-internal (lambda (_widget value)
3116 (if (symbolp value)
3117 (symbol-name value)
3118 value))
3119 :value-to-external (lambda (_widget value)
3120 (if (stringp value)
3121 (intern value)
3122 value)))
3124 (defun widget-symbol-prompt-internal (widget prompt initial history)
3125 ;; Read file from minibuffer.
3126 (let ((answer (completing-read prompt obarray
3127 (widget-get widget :prompt-match)
3128 nil initial history)))
3129 (if (and (stringp answer)
3130 (not (zerop (length answer))))
3131 answer
3132 (error "No value"))))
3134 (defvar widget-function-prompt-value-history nil
3135 "History of input to `widget-function-prompt-value'.")
3137 (define-widget 'function 'restricted-sexp
3138 "A Lisp function."
3139 :completions (apply-partially #'completion-table-with-predicate
3140 obarray #'fboundp 'strict)
3141 :prompt-value 'widget-field-prompt-value
3142 :prompt-internal 'widget-symbol-prompt-internal
3143 :prompt-match 'fboundp
3144 :prompt-history 'widget-function-prompt-value-history
3145 :action 'widget-field-action
3146 :match-alternatives '(functionp)
3147 :validate (lambda (widget)
3148 (unless (functionp (widget-value widget))
3149 (widget-put widget :error (format "Invalid function: %S"
3150 (widget-value widget)))
3151 widget))
3152 :value 'ignore
3153 :tag "Function")
3155 (defvar widget-variable-prompt-value-history nil
3156 "History of input to `widget-variable-prompt-value'.")
3158 (define-widget 'variable 'symbol
3159 "A Lisp variable."
3160 :prompt-match 'boundp
3161 :prompt-history 'widget-variable-prompt-value-history
3162 :completions (apply-partially #'completion-table-with-predicate
3163 obarray #'boundp 'strict)
3164 :tag "Variable")
3166 (define-widget 'coding-system 'symbol
3167 "A MULE coding-system."
3168 :format "%{%t%}: %v"
3169 :tag "Coding system"
3170 :base-only nil
3171 :prompt-history 'coding-system-value-history
3172 :prompt-value 'widget-coding-system-prompt-value
3173 :action 'widget-coding-system-action
3174 :completions (apply-partially #'completion-table-with-predicate
3175 obarray #'coding-system-p 'strict)
3176 :validate (lambda (widget)
3177 (unless (coding-system-p (widget-value widget))
3178 (widget-put widget :error (format "Invalid coding system: %S"
3179 (widget-value widget)))
3180 widget))
3181 :value 'undecided
3182 :prompt-match 'coding-system-p)
3184 (defun widget-coding-system-prompt-value (widget prompt value _unbound)
3185 "Read coding-system from minibuffer."
3186 (if (widget-get widget :base-only)
3187 (intern
3188 (completing-read (format "%s (default %s): " prompt value)
3189 (mapcar #'list (coding-system-list t)) nil nil nil
3190 coding-system-history))
3191 (read-coding-system (format "%s (default %s): " prompt value) value)))
3193 (defun widget-coding-system-action (widget &optional event)
3194 (let ((answer
3195 (widget-coding-system-prompt-value
3196 widget
3197 (widget-apply widget :menu-tag-get)
3198 (widget-value widget)
3199 t)))
3200 (widget-value-set widget answer)
3201 (widget-apply widget :notify widget event)
3202 (widget-setup)))
3204 ;;; I'm not sure about what this is good for? KFS.
3205 (defvar widget-key-sequence-prompt-value-history nil
3206 "History of input to `widget-key-sequence-prompt-value'.")
3208 (defvar widget-key-sequence-default-value [ignore]
3209 "Default value for an empty key sequence.")
3211 (defvar widget-key-sequence-map
3212 (let ((map (make-sparse-keymap)))
3213 (set-keymap-parent map widget-field-keymap)
3214 (define-key map [(control ?q)] 'widget-key-sequence-read-event)
3215 map))
3217 (define-widget 'key-sequence 'restricted-sexp
3218 "A key sequence."
3219 :prompt-value 'widget-field-prompt-value
3220 :prompt-internal 'widget-symbol-prompt-internal
3221 ; :prompt-match 'fboundp ;; What was this good for? KFS
3222 :prompt-history 'widget-key-sequence-prompt-value-history
3223 :action 'widget-field-action
3224 :match-alternatives '(stringp vectorp)
3225 :format "%{%t%}: %v"
3226 :validate 'widget-key-sequence-validate
3227 :value-to-internal 'widget-key-sequence-value-to-internal
3228 :value-to-external 'widget-key-sequence-value-to-external
3229 :value widget-key-sequence-default-value
3230 :keymap widget-key-sequence-map
3231 :help-echo "C-q: insert KEY, EVENT, or CODE; RET: enter value"
3232 :tag "Key sequence")
3234 (defun widget-key-sequence-read-event (ev)
3235 (interactive (list
3236 (let ((inhibit-quit t) quit-flag)
3237 (read-event "Insert KEY, EVENT, or CODE: "))))
3238 (let ((ev2 (and (memq 'down (event-modifiers ev))
3239 (read-event)))
3240 (tr (and (keymapp function-key-map)
3241 (lookup-key function-key-map (vector ev)))))
3242 (when (and (integerp ev)
3243 (or (and (<= ?0 ev) (< ev (+ ?0 (min 10 read-quoted-char-radix))))
3244 (and (<= ?a (downcase ev))
3245 (< (downcase ev) (+ ?a -10 (min 36 read-quoted-char-radix))))))
3246 (setq unread-command-events (cons ev unread-command-events)
3247 ev (read-quoted-char (format "Enter code (radix %d)" read-quoted-char-radix))
3248 tr nil)
3249 (if (and (integerp ev) (not (characterp ev)))
3250 (insert (char-to-string ev)))) ;; throw invalid char error
3251 (setq ev (key-description (list ev)))
3252 (when (arrayp tr)
3253 (setq tr (key-description (list (aref tr 0))))
3254 (if (y-or-n-p (format "Key %s is translated to %s -- use %s? " ev tr tr))
3255 (setq ev tr ev2 nil)))
3256 (insert (if (= (char-before) ?\s) "" " ") ev " ")
3257 (if ev2
3258 (insert (key-description (list ev2)) " "))))
3260 (defun widget-key-sequence-validate (widget)
3261 (unless (or (stringp (widget-value widget))
3262 (vectorp (widget-value widget)))
3263 (widget-put widget :error (format "Invalid key sequence: %S"
3264 (widget-value widget)))
3265 widget))
3267 (defun widget-key-sequence-value-to-internal (widget value)
3268 (if (widget-apply widget :match value)
3269 (if (equal value widget-key-sequence-default-value)
3271 (key-description value))
3272 value))
3274 (defun widget-key-sequence-value-to-external (_widget value)
3275 (if (stringp value)
3276 (if (string-match "\\`[[:space:]]*\\'" value)
3277 widget-key-sequence-default-value
3278 (read-kbd-macro value))
3279 value))
3282 (define-widget 'sexp 'editable-field
3283 "An arbitrary Lisp expression."
3284 :tag "Lisp expression"
3285 :format "%{%t%}: %v"
3286 :value nil
3287 :validate 'widget-sexp-validate
3288 :match (lambda (_widget _value) t)
3289 :value-to-internal 'widget-sexp-value-to-internal
3290 :value-to-external (lambda (_widget value) (read value))
3291 :prompt-history 'widget-sexp-prompt-value-history
3292 :prompt-value 'widget-sexp-prompt-value)
3294 (defun widget-sexp-value-to-internal (_widget value)
3295 ;; Use pp for printer representation.
3296 (let ((pp (if (symbolp value)
3297 (prin1-to-string value)
3298 (pp-to-string value))))
3299 (while (string-match "\n\\'" pp)
3300 (setq pp (substring pp 0 -1)))
3301 (if (or (string-match "\n\\'" pp)
3302 (> (length pp) 40))
3303 (concat "\n" pp)
3304 pp)))
3306 (defun widget-sexp-validate (widget)
3307 ;; Valid if we can read the string and there is no junk left after it.
3308 (with-temp-buffer
3309 (insert (widget-apply widget :value-get))
3310 (goto-char (point-min))
3311 (let (err)
3312 (condition-case data ;Note: We get a spurious byte-compile warning here.
3313 (progn
3314 ;; Avoid a confusing end-of-file error.
3315 (skip-syntax-forward "\\s-")
3316 (if (eobp)
3317 (setq err "Empty sexp -- use nil?")
3318 (unless (widget-apply widget :match (read (current-buffer)))
3319 (setq err (widget-get widget :type-error))))
3320 ;; Allow whitespace after expression.
3321 (skip-syntax-forward "\\s-")
3322 (if (and (not (eobp))
3323 (not err))
3324 (setq err (format "Junk at end of expression: %s"
3325 (buffer-substring (point)
3326 (point-max))))))
3327 (end-of-file ; Avoid confusing error message.
3328 (setq err "Unbalanced sexp"))
3329 (error (setq err (error-message-string data))))
3330 (if (not err)
3332 (widget-put widget :error err)
3333 widget))))
3335 (defvar widget-sexp-prompt-value-history nil
3336 "History of input to `widget-sexp-prompt-value'.")
3338 (defun widget-sexp-prompt-value (widget prompt value unbound)
3339 ;; Read an arbitrary sexp.
3340 (let ((found (read-string prompt
3341 (if unbound nil (cons (prin1-to-string value) 0))
3342 (widget-get widget :prompt-history))))
3343 (let ((answer (read-from-string found)))
3344 (unless (= (cdr answer) (length found))
3345 (error "Junk at end of expression: %s"
3346 (substring found (cdr answer))))
3347 (car answer))))
3349 (define-widget 'restricted-sexp 'sexp
3350 "A Lisp expression restricted to values that match.
3351 To use this type, you must define :match or :match-alternatives."
3352 :type-error "The specified value is not valid"
3353 :match 'widget-restricted-sexp-match
3354 :value-to-internal (lambda (widget value)
3355 (if (widget-apply widget :match value)
3356 (prin1-to-string value)
3357 value)))
3359 (defun widget-restricted-sexp-match (widget value)
3360 (let ((alternatives (widget-get widget :match-alternatives))
3361 matched)
3362 (while (and alternatives (not matched))
3363 (if (cond ((functionp (car alternatives))
3364 (funcall (car alternatives) value))
3365 ((and (consp (car alternatives))
3366 (eq (car (car alternatives)) 'quote))
3367 (eq value (nth 1 (car alternatives)))))
3368 (setq matched t))
3369 (setq alternatives (cdr alternatives)))
3370 matched))
3372 (define-widget 'integer 'restricted-sexp
3373 "An integer."
3374 :tag "Integer"
3375 :value 0
3376 :type-error "This field should contain an integer"
3377 :match-alternatives '(integerp))
3379 (define-widget 'number 'restricted-sexp
3380 "A number (floating point or integer)."
3381 :tag "Number"
3382 :value 0.0
3383 :type-error "This field should contain a number (floating point or integer)"
3384 :match-alternatives '(numberp))
3386 (define-widget 'float 'restricted-sexp
3387 "A floating point number."
3388 :tag "Floating point number"
3389 :value 0.0
3390 :type-error "This field should contain a floating point number"
3391 :match-alternatives '(floatp))
3393 (define-widget 'character 'editable-field
3394 "A character."
3395 :tag "Character"
3396 :value 0
3397 :size 1
3398 :format "%{%t%}: %v\n"
3399 :valid-regexp "\\`.\\'"
3400 :error "This field should contain a single character"
3401 :value-get (lambda (w) (widget-field-value-get w t))
3402 :value-to-internal (lambda (_widget value)
3403 (if (stringp value)
3404 value
3405 (char-to-string value)))
3406 :value-to-external (lambda (_widget value)
3407 (if (stringp value)
3408 (aref value 0)
3409 value))
3410 :match (lambda (_widget value)
3411 (characterp value)))
3413 (define-widget 'list 'group
3414 "A Lisp list."
3415 :tag "List"
3416 :format "%{%t%}:\n%v")
3418 (define-widget 'vector 'group
3419 "A Lisp vector."
3420 :tag "Vector"
3421 :format "%{%t%}:\n%v"
3422 :match 'widget-vector-match
3423 :value-to-internal (lambda (_widget value) (append value nil))
3424 :value-to-external (lambda (_widget value) (apply 'vector value)))
3426 (defun widget-vector-match (widget value)
3427 (and (vectorp value)
3428 (widget-group-match widget
3429 (widget-apply widget :value-to-internal value))))
3431 (define-widget 'cons 'group
3432 "A cons-cell."
3433 :tag "Cons-cell"
3434 :format "%{%t%}:\n%v"
3435 :match 'widget-cons-match
3436 :value-to-internal (lambda (_widget value)
3437 (list (car value) (cdr value)))
3438 :value-to-external (lambda (_widget value)
3439 (apply 'cons value)))
3441 (defun widget-cons-match (widget value)
3442 (and (consp value)
3443 (widget-group-match widget
3444 (widget-apply widget :value-to-internal value))))
3446 ;;; The `lazy' Widget.
3448 ;; Recursive datatypes.
3450 (define-widget 'lazy 'default
3451 "Base widget for recursive data structures.
3453 The `lazy' widget will, when instantiated, contain a single inferior
3454 widget, of the widget type specified by the :type parameter. The
3455 value of the `lazy' widget is the same as the value of the inferior
3456 widget. When deriving a new widget from the 'lazy' widget, the :type
3457 parameter is allowed to refer to the widget currently being defined,
3458 thus allowing recursive data structures to be described.
3460 The :type parameter takes the same arguments as the defcustom
3461 parameter with the same name.
3463 Most composite widgets, i.e. widgets containing other widgets, does
3464 not allow recursion. That is, when you define a new widget type, none
3465 of the inferior widgets may be of the same type you are currently
3466 defining.
3468 In Lisp, however, it is custom to define data structures in terms of
3469 themselves. A list, for example, is defined as either nil, or a cons
3470 cell whose cdr itself is a list. The obvious way to translate this
3471 into a widget type would be
3473 (define-widget \\='my-list \\='choice
3474 \"A list of sexps.\"
3475 :tag \"Sexp list\"
3476 :args \\='((const nil) (cons :value (nil) sexp my-list)))
3478 Here we attempt to define my-list as a choice of either the constant
3479 nil, or a cons-cell containing a sexp and my-lisp. This will not work
3480 because the `choice' widget does not allow recursion.
3482 Using the `lazy' widget you can overcome this problem, as in this
3483 example:
3485 (define-widget \\='sexp-list \\='lazy
3486 \"A list of sexps.\"
3487 :tag \"Sexp list\"
3488 :type \\='(choice (const nil) (cons :value (nil) sexp sexp-list)))"
3489 :format "%{%t%}: %v"
3490 ;; We don't convert :type because we want to allow recursive
3491 ;; data structures. This is slow, so we should not create speed
3492 ;; critical widgets by deriving from this.
3493 :convert-widget 'widget-value-convert-widget
3494 :value-create 'widget-type-value-create
3495 :value-get 'widget-child-value-get
3496 :value-inline 'widget-child-value-inline
3497 :default-get 'widget-type-default-get
3498 :match 'widget-type-match
3499 :validate 'widget-child-validate)
3502 ;;; The `plist' Widget.
3504 ;; Property lists.
3506 (define-widget 'plist 'list
3507 "A property list."
3508 :key-type '(symbol :tag "Key")
3509 :value-type '(sexp :tag "Value")
3510 :convert-widget 'widget-plist-convert-widget
3511 :tag "Plist")
3513 (defvar widget-plist-value-type) ;Dynamic variable
3515 (defun widget-plist-convert-widget (widget)
3516 ;; Handle `:options'.
3517 (let* ((options (widget-get widget :options))
3518 (widget-plist-value-type (widget-get widget :value-type))
3519 (other `(editable-list :inline t
3520 (group :inline t
3521 ,(widget-get widget :key-type)
3522 ,widget-plist-value-type)))
3523 (args (if options
3524 (list `(checklist :inline t
3525 :greedy t
3526 ,@(mapcar 'widget-plist-convert-option
3527 options))
3528 other)
3529 (list other))))
3530 (widget-put widget :args args)
3531 widget))
3533 (defun widget-plist-convert-option (option)
3534 ;; Convert a single plist option.
3535 (let (key-type value-type)
3536 (if (listp option)
3537 (let ((key (nth 0 option)))
3538 (setq value-type (nth 1 option))
3539 (if (listp key)
3540 (setq key-type key)
3541 (setq key-type `(const ,key))))
3542 (setq key-type `(const ,option)
3543 value-type widget-plist-value-type))
3544 `(group :format "Key: %v" :inline t ,key-type ,value-type)))
3547 ;;; The `alist' Widget.
3549 ;; Association lists.
3551 (define-widget 'alist 'list
3552 "An association list."
3553 :key-type '(sexp :tag "Key")
3554 :value-type '(sexp :tag "Value")
3555 :convert-widget 'widget-alist-convert-widget
3556 :tag "Alist")
3558 (defvar widget-alist-value-type) ;Dynamic variable
3560 (defun widget-alist-convert-widget (widget)
3561 ;; Handle `:options'.
3562 (let* ((options (widget-get widget :options))
3563 (widget-alist-value-type (widget-get widget :value-type))
3564 (other `(editable-list :inline t
3565 (cons :format "%v"
3566 ,(widget-get widget :key-type)
3567 ,widget-alist-value-type)))
3568 (args (if options
3569 (list `(checklist :inline t
3570 :greedy t
3571 ,@(mapcar 'widget-alist-convert-option
3572 options))
3573 other)
3574 (list other))))
3575 (widget-put widget :args args)
3576 widget))
3578 (defun widget-alist-convert-option (option)
3579 ;; Convert a single alist option.
3580 (let (key-type value-type)
3581 (if (listp option)
3582 (let ((key (nth 0 option)))
3583 (setq value-type (nth 1 option))
3584 (if (listp key)
3585 (setq key-type key)
3586 (setq key-type `(const ,key))))
3587 (setq key-type `(const ,option)
3588 value-type widget-alist-value-type))
3589 `(cons :format "Key: %v" ,key-type ,value-type)))
3591 (define-widget 'choice 'menu-choice
3592 "A union of several sexp types."
3593 :tag "Choice"
3594 :format "%{%t%}: %[Value Menu%] %v"
3595 :button-prefix 'widget-push-button-prefix
3596 :button-suffix 'widget-push-button-suffix
3597 :prompt-value 'widget-choice-prompt-value)
3599 (defun widget-choice-prompt-value (widget prompt value _unbound)
3600 "Make a choice."
3601 (let ((args (widget-get widget :args))
3602 (completion-ignore-case (widget-get widget :case-fold))
3603 current choices old)
3604 ;; Find the first arg that matches VALUE.
3605 (let ((look args))
3606 (while look
3607 (if (widget-apply (car look) :match value)
3608 (setq old (car look)
3609 look nil)
3610 (setq look (cdr look)))))
3611 ;; Find new choice.
3612 (setq current
3613 (cond ((= (length args) 0)
3614 nil)
3615 ((= (length args) 1)
3616 (nth 0 args))
3617 ((and (= (length args) 2)
3618 (memq old args))
3619 (if (eq old (nth 0 args))
3620 (nth 1 args)
3621 (nth 0 args)))
3623 (while args
3624 (setq current (car args)
3625 args (cdr args))
3626 (setq choices
3627 (cons (cons (widget-apply current :menu-tag-get)
3628 current)
3629 choices)))
3630 (let ((val (completing-read prompt choices nil t)))
3631 (if (stringp val)
3632 (let ((try (try-completion val choices)))
3633 (when (stringp try)
3634 (setq val try))
3635 (cdr (assoc val choices)))
3636 nil)))))
3637 (if current
3638 (widget-prompt-value current prompt nil t)
3639 value)))
3641 (define-widget 'radio 'radio-button-choice
3642 "A union of several sexp types."
3643 :tag "Choice"
3644 :format "%{%t%}:\n%v"
3645 :prompt-value 'widget-choice-prompt-value)
3647 (define-widget 'repeat 'editable-list
3648 "A variable length homogeneous list."
3649 :tag "Repeat"
3650 :format "%{%t%}:\n%v%i\n")
3652 (define-widget 'set 'checklist
3653 "A list of members from a fixed set."
3654 :tag "Set"
3655 :format "%{%t%}:\n%v")
3657 (define-widget 'boolean 'toggle
3658 "To be nil or non-nil, that is the question."
3659 :tag "Boolean"
3660 :prompt-value 'widget-boolean-prompt-value
3661 :button-prefix 'widget-push-button-prefix
3662 :button-suffix 'widget-push-button-suffix
3663 :format "%{%t%}: %[Toggle%] %v\n"
3664 :on "on (non-nil)"
3665 :off "off (nil)")
3667 (defun widget-boolean-prompt-value (_widget prompt _value _unbound)
3668 ;; Toggle a boolean.
3669 (y-or-n-p prompt))
3671 ;;; The `color' Widget.
3673 ;; Fixme: match
3674 (define-widget 'color 'editable-field
3675 "Choose a color name (with sample)."
3676 :format "%{%t%}: %v (%{sample%})\n"
3677 :value-create 'widget-color-value-create
3678 :size 10
3679 :tag "Color"
3680 :value "black"
3681 :completions (or facemenu-color-alist (defined-colors))
3682 :sample-face-get 'widget-color-sample-face-get
3683 :notify 'widget-color-notify
3684 :action 'widget-color-action)
3686 (defun widget-color-value-create (widget)
3687 (widget-field-value-create widget)
3688 (widget-insert " ")
3689 (widget-create-child-and-convert
3690 widget 'push-button
3691 :tag " Choose " :action 'widget-color--choose-action)
3692 (widget-insert " "))
3694 (defun widget-color--choose-action (widget &optional _event)
3695 (list-colors-display
3696 nil nil
3697 `(lambda (color)
3698 (when (buffer-live-p ,(current-buffer))
3699 (widget-value-set ',(widget-get widget :parent) color)
3700 (let* ((buf (get-buffer "*Colors*"))
3701 (win (get-buffer-window buf 0)))
3702 (if win
3703 (quit-window nil win)
3704 (bury-buffer buf)))
3705 (pop-to-buffer ,(current-buffer))))))
3707 (defun widget-color-sample-face-get (widget)
3708 (let* ((value (condition-case nil
3709 (widget-value widget)
3710 (error (widget-get widget :value)))))
3711 (if (color-defined-p value)
3712 (list (cons 'foreground-color value))
3713 'default)))
3715 (defun widget-color-action (widget &optional event)
3716 "Prompt for a color."
3717 (let* ((tag (widget-apply widget :menu-tag-get))
3718 (prompt (concat tag ": "))
3719 (answer (facemenu-read-color prompt)))
3720 (unless (zerop (length answer))
3721 (widget-value-set widget answer)
3722 (widget-setup)
3723 (widget-apply widget :notify widget event))))
3725 (defun widget-color-notify (widget child &optional event)
3726 "Update the sample, and notify the parent."
3727 (overlay-put (widget-get widget :sample-overlay)
3728 'face (widget-apply widget :sample-face-get))
3729 (widget-default-notify widget child event))
3731 ;;; The Help Echo
3733 (defun widget-echo-help (pos)
3734 "Display help-echo text for widget at POS."
3735 (let* ((widget (widget-at pos))
3736 (help-echo (and widget (widget-get widget :help-echo))))
3737 (if (functionp help-echo)
3738 (setq help-echo (funcall help-echo widget)))
3739 (if help-echo (message "%s" (eval help-echo)))))
3741 ;;; The End:
3743 (provide 'wid-edit)
3745 ;;; wid-edit.el ends here