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