Check for some overflows in vertical-motion
[emacs.git] / lisp / wid-edit.el
blob04a900f23c095f2dbfb7311f12def2ddc5ba4a06
1 ;;; wid-edit.el --- Functions for creating and using widgets -*-byte-compile-dynamic: t; lexical-binding:t -*-
2 ;;
3 ;; Copyright (C) 1996-1997, 1999-2015 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 (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 (widget-get widget :tag)))
1507 (cond (image
1508 (widget-image-insert widget (or tag "image") image))
1509 (tag
1510 (insert tag))
1512 (princ (widget-get widget :value)
1513 (current-buffer))))))
1514 ((eq escape ?d)
1515 (let ((doc (widget-get widget :doc)))
1516 (when doc
1517 (setq doc-begin (point))
1518 (insert doc)
1519 (while (eq (preceding-char) ?\n)
1520 (delete-char -1))
1521 (insert ?\n)
1522 (setq doc-end (point)))))
1523 ((eq escape ?h)
1524 (widget-add-documentation-string-button widget))
1525 ((eq escape ?v)
1526 (if (and button-begin (not button-end))
1527 (widget-apply widget :value-create)
1528 (setq value-pos (point))))
1530 (widget-apply widget :format-handler escape)))))
1531 ;; Specify button, sample, and doc, and insert value.
1532 (and button-begin button-end
1533 (widget-specify-button widget button-begin button-end))
1534 (and sample-begin sample-end
1535 (widget-specify-sample widget sample-begin sample-end))
1536 (and doc-begin doc-end
1537 (widget-specify-doc widget doc-begin doc-end))
1538 (when value-pos
1539 (goto-char value-pos)
1540 (widget-apply widget :value-create)))
1541 (let ((from (point-min-marker))
1542 (to (point-max-marker)))
1543 (set-marker-insertion-type from t)
1544 (set-marker-insertion-type to nil)
1545 (widget-put widget :from from)
1546 (widget-put widget :to to)))
1547 (widget-clear-undo))
1549 (defun widget-default-format-handler (_widget escape)
1550 (error "Unknown escape `%c'" escape))
1552 (defun widget-default-button-face-get (widget)
1553 ;; Use :button-face or widget-button-face
1554 (or (widget-get widget :button-face)
1555 (let ((parent (widget-get widget :parent)))
1556 (if parent
1557 (widget-apply parent :button-face-get)
1558 widget-button-face))))
1560 (defun widget-default-mouse-face-get (widget)
1561 ;; Use :mouse-face or widget-mouse-face
1562 (or (widget-get widget :mouse-face)
1563 (let ((parent (widget-get widget :parent)))
1564 (if parent
1565 (widget-apply parent :mouse-face-get)
1566 widget-mouse-face))))
1568 (defun widget-default-sample-face-get (widget)
1569 ;; Use :sample-face.
1570 (widget-get widget :sample-face))
1572 (defun widget-default-delete (widget)
1573 "Remove widget from the buffer."
1574 (let ((from (widget-get widget :from))
1575 (to (widget-get widget :to))
1576 (inactive-overlay (widget-get widget :inactive))
1577 (button-overlay (widget-get widget :button-overlay))
1578 (sample-overlay (widget-get widget :sample-overlay))
1579 (doc-overlay (widget-get widget :doc-overlay))
1580 (inhibit-modification-hooks t)
1581 (inhibit-read-only t))
1582 (widget-apply widget :value-delete)
1583 (widget-children-value-delete widget)
1584 (when inactive-overlay
1585 (delete-overlay inactive-overlay))
1586 (when button-overlay
1587 (delete-overlay button-overlay))
1588 (when sample-overlay
1589 (delete-overlay sample-overlay))
1590 (when doc-overlay
1591 (delete-overlay doc-overlay))
1592 (when (< from to)
1593 ;; Kludge: this doesn't need to be true for empty formats.
1594 (delete-region from to))
1595 (set-marker from nil)
1596 (set-marker to nil))
1597 (widget-clear-undo))
1599 (defun widget-default-value-set (widget value)
1600 "Recreate widget with new value."
1601 (let* ((old-pos (point))
1602 (from (copy-marker (widget-get widget :from)))
1603 (to (copy-marker (widget-get widget :to)))
1604 (offset (if (and (<= from old-pos) (<= old-pos to))
1605 (if (>= old-pos (1- to))
1606 (- old-pos to 1)
1607 (- old-pos from)))))
1608 ;;??? Bug: this ought to insert the new value before deleting the old one,
1609 ;; so that markers on either side of the value automatically
1610 ;; stay on the same side. -- rms.
1611 (save-excursion
1612 (goto-char (widget-get widget :from))
1613 (widget-apply widget :delete)
1614 (widget-put widget :value value)
1615 (widget-apply widget :create))
1616 (if offset
1617 (if (< offset 0)
1618 (goto-char (+ (widget-get widget :to) offset 1))
1619 (goto-char (min (+ from offset) (1- (widget-get widget :to))))))))
1621 (defun widget-default-value-inline (widget)
1622 "Wrap value in a list unless it is inline."
1623 (if (widget-get widget :inline)
1624 (widget-value widget)
1625 (list (widget-value widget))))
1627 (defun widget-default-default-get (widget)
1628 "Get `:value'."
1629 (widget-get widget :value))
1631 (defun widget-default-menu-tag-get (widget)
1632 "Use tag or value for menus."
1633 (or (widget-get widget :menu-tag)
1634 (widget-get widget :tag)
1635 (widget-princ-to-string (widget-get widget :value))))
1637 (defun widget-default-active (widget)
1638 "Return t if this widget is active (user modifiable)."
1639 (or (widget-get widget :always-active)
1640 (and (not (widget-get widget :inactive))
1641 (let ((parent (widget-get widget :parent)))
1642 (or (null parent)
1643 (widget-apply parent :active))))))
1645 (defun widget-default-deactivate (widget)
1646 "Make WIDGET inactive for user modifications."
1647 (widget-specify-inactive widget
1648 (widget-get widget :from)
1649 (widget-get widget :to)))
1651 (defun widget-default-action (widget &optional event)
1652 "Notify the parent when a widget changes."
1653 (let ((parent (widget-get widget :parent)))
1654 (when parent
1655 (widget-apply parent :notify widget event))))
1657 (defun widget-default-notify (widget _child &optional event)
1658 "Pass notification to parent."
1659 (widget-default-action widget event))
1661 (defun widget-default-prompt-value (_widget prompt _value _unbound)
1662 "Read an arbitrary value."
1663 (eval-minibuffer prompt))
1665 (defun widget-docstring (widget)
1666 "Return the documentation string specified by WIDGET, or nil if none.
1667 If WIDGET has a `:doc' property, that specifies the documentation string.
1668 Otherwise, try the `:documentation-property' property. If this
1669 is a function, call it with the widget's value as an argument; if
1670 it is a symbol, use this symbol together with the widget's value
1671 as the argument to `documentation-property'."
1672 (let ((doc (or (widget-get widget :doc)
1673 (let ((doc-prop (widget-get widget :documentation-property))
1674 (value (widget-get widget :value)))
1675 (cond ((functionp doc-prop)
1676 (funcall doc-prop value))
1677 ((symbolp doc-prop)
1678 (documentation-property value doc-prop)))))))
1679 (when (and (stringp doc) (> (length doc) 0))
1680 ;; Remove any redundant `*' in the beginning.
1681 (when (eq (aref doc 0) ?*)
1682 (setq doc (substring doc 1)))
1683 ;; Remove trailing newlines.
1684 (when (string-match "\n+\\'" doc)
1685 (setq doc (substring doc 0 (match-beginning 0))))
1686 doc)))
1688 ;;; The `item' Widget.
1690 (define-widget 'item 'default
1691 "Constant items for inclusion in other widgets."
1692 :convert-widget 'widget-value-convert-widget
1693 :value-create 'widget-item-value-create
1694 :value-delete 'ignore
1695 :value-get 'widget-value-value-get
1696 :match 'widget-item-match
1697 :match-inline 'widget-item-match-inline
1698 :action 'widget-item-action
1699 :format "%t\n")
1701 (defun widget-item-value-create (widget)
1702 "Insert the printed representation of the value."
1703 (princ (widget-get widget :value) (current-buffer)))
1705 (defun widget-item-match (widget value)
1706 ;; Match if the value is the same.
1707 (equal (widget-get widget :value) value))
1709 (defun widget-item-match-inline (widget vals)
1710 ;; Match if the value is the same.
1711 (let ((value (widget-get widget :value)))
1712 (and (listp value)
1713 (<= (length value) (length vals))
1714 (let ((head (widget-sublist vals 0 (length value))))
1715 (and (equal head value)
1716 (cons head (widget-sublist vals (length value))))))))
1718 (defun widget-sublist (list start &optional end)
1719 "Return the sublist of LIST from START to END.
1720 If END is omitted, it defaults to the length of LIST."
1721 (if (> start 0) (setq list (nthcdr start list)))
1722 (if end
1723 (unless (<= end start)
1724 (setq list (copy-sequence list))
1725 (setcdr (nthcdr (- end start 1) list) nil)
1726 list)
1727 (copy-sequence list)))
1729 (defun widget-item-action (widget &optional event)
1730 ;; Just notify itself.
1731 (widget-apply widget :notify widget event))
1733 ;;; The `push-button' Widget.
1735 ;; (defcustom widget-push-button-gui t
1736 ;; "If non-nil, use GUI push buttons when available."
1737 ;; :group 'widgets
1738 ;; :type 'boolean)
1740 ;; Cache already created GUI objects.
1741 ;; (defvar widget-push-button-cache nil)
1743 (defcustom widget-push-button-prefix "["
1744 "String used as prefix for buttons."
1745 :type 'string
1746 :group 'widget-button)
1748 (defcustom widget-push-button-suffix "]"
1749 "String used as suffix for buttons."
1750 :type 'string
1751 :group 'widget-button)
1753 (define-widget 'push-button 'item
1754 "A pushable button."
1755 :button-prefix ""
1756 :button-suffix ""
1757 :value-create 'widget-push-button-value-create
1758 :format "%[%v%]")
1760 (defun widget-push-button-value-create (widget)
1761 "Insert text representing the `on' and `off' states."
1762 (let* ((tag (or (widget-get widget :tag)
1763 (widget-get widget :value)))
1764 (tag-glyph (widget-get widget :tag-glyph))
1765 (text (concat widget-push-button-prefix
1766 tag widget-push-button-suffix)))
1767 (if tag-glyph
1768 (widget-image-insert widget text tag-glyph)
1769 (insert text))))
1771 ;; (defun widget-gui-action (widget)
1772 ;; "Apply :action for WIDGET."
1773 ;; (widget-apply-action widget (this-command-keys)))
1775 ;;; The `link' Widget.
1777 (defcustom widget-link-prefix "["
1778 "String used as prefix for links."
1779 :type 'string
1780 :group 'widget-button)
1782 (defcustom widget-link-suffix "]"
1783 "String used as suffix for links."
1784 :type 'string
1785 :group 'widget-button)
1787 (define-widget 'link 'item
1788 "An embedded link."
1789 :button-prefix 'widget-link-prefix
1790 :button-suffix 'widget-link-suffix
1791 :follow-link 'mouse-face
1792 :help-echo "Follow the link."
1793 :format "%[%t%]")
1795 ;;; The `info-link' Widget.
1797 (define-widget 'info-link 'link
1798 "A link to an info file."
1799 :action 'widget-info-link-action)
1801 (defun widget-info-link-action (widget &optional _event)
1802 "Open the info node specified by WIDGET."
1803 (info (widget-value widget)))
1805 ;;; The `url-link' Widget.
1807 (define-widget 'url-link 'link
1808 "A link to an www page."
1809 :action 'widget-url-link-action)
1811 (defun widget-url-link-action (widget &optional _event)
1812 "Open the URL specified by WIDGET."
1813 (browse-url (widget-value widget)))
1815 ;;; The `function-link' Widget.
1817 (define-widget 'function-link 'link
1818 "A link to an Emacs function."
1819 :action 'widget-function-link-action)
1821 (defun widget-function-link-action (widget &optional _event)
1822 "Show the function specified by WIDGET."
1823 (describe-function (widget-value widget)))
1825 ;;; The `variable-link' Widget.
1827 (define-widget 'variable-link 'link
1828 "A link to an Emacs variable."
1829 :action 'widget-variable-link-action)
1831 (defun widget-variable-link-action (widget &optional _event)
1832 "Show the variable specified by WIDGET."
1833 (describe-variable (widget-value widget)))
1835 ;;; The `file-link' Widget.
1837 (define-widget 'file-link 'link
1838 "A link to a file."
1839 :action 'widget-file-link-action)
1841 (defun widget-file-link-action (widget &optional _event)
1842 "Find the file specified by WIDGET."
1843 (find-file (widget-value widget)))
1845 ;;; The `emacs-library-link' Widget.
1847 (define-widget 'emacs-library-link 'link
1848 "A link to an Emacs Lisp library file."
1849 :action 'widget-emacs-library-link-action)
1851 (defun widget-emacs-library-link-action (widget &optional _event)
1852 "Find the Emacs library file specified by WIDGET."
1853 (find-file (locate-library (widget-value widget))))
1855 ;;; The `emacs-commentary-link' Widget.
1857 (define-widget 'emacs-commentary-link 'link
1858 "A link to Commentary in an Emacs Lisp library file."
1859 :action 'widget-emacs-commentary-link-action)
1861 (defun widget-emacs-commentary-link-action (widget &optional _event)
1862 "Find the Commentary section of the Emacs file specified by WIDGET."
1863 (finder-commentary (widget-value widget)))
1865 ;;; The `editable-field' Widget.
1867 (define-widget 'editable-field 'default
1868 "An editable text field.
1869 Note: In an `editable-field' widget, the `%v' escape must be preceded
1870 by some other text in the `:format' string (if specified)."
1871 :convert-widget 'widget-value-convert-widget
1872 :keymap widget-field-keymap
1873 :format "%v"
1874 :help-echo "M-TAB: complete field; RET: enter value"
1875 :value ""
1876 :prompt-internal 'widget-field-prompt-internal
1877 :prompt-history 'widget-field-history
1878 :prompt-value 'widget-field-prompt-value
1879 :action 'widget-field-action
1880 :validate 'widget-field-validate
1881 :valid-regexp ""
1882 :error "Field's value doesn't match allowed forms"
1883 :value-create 'widget-field-value-create
1884 :value-set 'widget-field-value-set
1885 :value-delete 'widget-field-value-delete
1886 :value-get 'widget-field-value-get
1887 :match 'widget-field-match)
1889 (defvar widget-field-history nil
1890 "History of field minibuffer edits.")
1892 (defun widget-field-prompt-internal (_widget prompt initial history)
1893 "Read string for WIDGET prompting with PROMPT.
1894 INITIAL is the initial input and HISTORY is a symbol containing
1895 the earlier input."
1896 (read-string prompt initial history))
1898 (defun widget-field-prompt-value (widget prompt value unbound)
1899 "Prompt for a string."
1900 (widget-apply widget
1901 :value-to-external
1902 (widget-apply widget
1903 :prompt-internal prompt
1904 (unless unbound
1905 (cons (widget-apply widget
1906 :value-to-internal value)
1908 (widget-get widget :prompt-history))))
1910 (defvar widget-edit-functions nil)
1912 (defun widget-field-action (widget &optional _event)
1913 "Move to next field."
1914 (widget-forward 1)
1915 (run-hook-with-args 'widget-edit-functions widget))
1917 (defun widget-field-validate (widget)
1918 "Valid if the content matches `:valid-regexp'."
1919 (unless (string-match (widget-get widget :valid-regexp)
1920 (widget-apply widget :value-get))
1921 widget))
1923 (defun widget-field-value-set (widget value)
1924 "Set an editable text field WIDGET to VALUE"
1925 (let ((from (widget-field-start widget))
1926 (to (widget-field-text-end widget))
1927 (buffer (widget-field-buffer widget)))
1928 (when (and from to (buffer-live-p buffer))
1929 (with-current-buffer buffer
1930 (goto-char from)
1931 (delete-char (- to from))
1932 (insert value)))))
1934 (defun widget-field-value-create (widget)
1935 "Create an editable text field."
1936 (let ((size (widget-get widget :size))
1937 (value (widget-get widget :value))
1938 (from (point))
1939 ;; This is changed to a real overlay in `widget-setup'. We
1940 ;; need the end points to behave differently until
1941 ;; `widget-setup' is called.
1942 (overlay (cons (make-marker) (make-marker))))
1943 (widget-put widget :field-overlay overlay)
1944 (insert value)
1945 (and size
1946 (< (length value) size)
1947 (insert-char ?\s (- size (length value))))
1948 (unless (memq widget widget-field-list)
1949 (setq widget-field-new (cons widget widget-field-new)))
1950 (move-marker (cdr overlay) (point))
1951 (set-marker-insertion-type (cdr overlay) nil)
1952 (when (null size)
1953 (insert ?\n))
1954 (move-marker (car overlay) from)
1955 (set-marker-insertion-type (car overlay) t)))
1957 (defun widget-field-value-delete (widget)
1958 "Remove the widget from the list of active editing fields."
1959 (setq widget-field-list (delq widget widget-field-list))
1960 (setq widget-field-new (delq widget widget-field-new))
1961 ;; These are nil if the :format string doesn't contain `%v'.
1962 (let ((overlay (widget-get widget :field-overlay)))
1963 (when (overlayp overlay)
1964 (delete-overlay overlay))))
1966 (defun widget-field-value-get (widget &optional no-truncate)
1967 "Return current text in editing field.
1968 Normally, trailing spaces within the editing field are truncated.
1969 But if NO-TRUNCATE is non-nil, include them."
1970 (let ((from (widget-field-start widget))
1971 (to (if no-truncate
1972 (widget-field-end widget)
1973 (widget-field-text-end widget)))
1974 (buffer (widget-field-buffer widget))
1975 (secret (widget-get widget :secret))
1976 (old (current-buffer)))
1977 (if (and from to)
1978 (progn
1979 (set-buffer buffer)
1980 (let ((result (buffer-substring-no-properties from to)))
1981 (when secret
1982 (let ((index 0))
1983 (while (< (+ from index) to)
1984 (aset result index
1985 (get-char-property (+ from index) 'secret))
1986 (setq index (1+ index)))))
1987 (set-buffer old)
1988 result))
1989 (widget-get widget :value))))
1991 (defun widget-field-match (_widget value)
1992 ;; Match any string.
1993 (stringp value))
1995 ;;; The `text' Widget.
1997 (define-widget 'text 'editable-field
1998 "A multiline text area."
1999 :keymap widget-text-keymap)
2001 ;;; The `menu-choice' Widget.
2003 (define-widget 'menu-choice 'default
2004 "A menu of options."
2005 :convert-widget 'widget-types-convert-widget
2006 :copy 'widget-types-copy
2007 :format "%[%t%]: %v"
2008 :case-fold t
2009 :tag "choice"
2010 :void '(item :format "invalid (%t)\n")
2011 :value-create 'widget-choice-value-create
2012 :value-get 'widget-child-value-get
2013 :value-inline 'widget-child-value-inline
2014 :default-get 'widget-choice-default-get
2015 :mouse-down-action 'widget-choice-mouse-down-action
2016 :action 'widget-choice-action
2017 :error "Make a choice"
2018 :validate 'widget-choice-validate
2019 :match 'widget-choice-match
2020 :match-inline 'widget-choice-match-inline)
2022 (defun widget-choice-value-create (widget)
2023 "Insert the first choice that matches the value."
2024 (let ((value (widget-get widget :value))
2025 (args (widget-get widget :args))
2026 (explicit (widget-get widget :explicit-choice))
2027 current)
2028 (if explicit
2029 (progn
2030 ;; If the user specified the choice for this value,
2031 ;; respect that choice.
2032 (widget-put widget :children (list (widget-create-child-value
2033 widget explicit value)))
2034 (widget-put widget :choice explicit)
2035 (widget-put widget :explicit-choice nil))
2036 (while args
2037 (setq current (car args)
2038 args (cdr args))
2039 (when (widget-apply current :match value)
2040 (widget-put widget :children (list (widget-create-child-value
2041 widget current value)))
2042 (widget-put widget :choice current)
2043 (setq args nil
2044 current nil)))
2045 (when current
2046 (let ((void (widget-get widget :void)))
2047 (widget-put widget :children (list (widget-create-child-and-convert
2048 widget void :value value)))
2049 (widget-put widget :choice void))))))
2051 (defun widget-choice-default-get (widget)
2052 ;; Get default for the first choice.
2053 (widget-default-get (car (widget-get widget :args))))
2055 (defcustom widget-choice-toggle nil
2056 "If non-nil, a binary choice will just toggle between the values.
2057 Otherwise, the user will explicitly have to choose between the values
2058 when he invoked the menu."
2059 :type 'boolean
2060 :group 'widgets)
2062 (defun widget-choice-mouse-down-action (widget &optional _event)
2063 ;; Return non-nil if we need a menu.
2064 (let ((args (widget-get widget :args))
2065 (old (widget-get widget :choice)))
2066 (cond ((not (display-popup-menus-p))
2067 ;; No place to pop up a menu.
2068 nil)
2069 ((< (length args) 2)
2070 ;; Empty or singleton list, just return the value.
2071 nil)
2072 ((> (length args) widget-menu-max-size)
2073 ;; Too long, prompt.
2074 nil)
2075 ((> (length args) 2)
2076 ;; Reasonable sized list, use menu.
2078 ((and widget-choice-toggle (memq old args))
2079 ;; We toggle.
2080 nil)
2082 ;; Ask which of the two.
2083 t))))
2085 (defun widget-choice-action (widget &optional event)
2086 ;; Make a choice.
2087 (let ((args (widget-get widget :args))
2088 (old (widget-get widget :choice))
2089 (tag (widget-apply widget :menu-tag-get))
2090 (completion-ignore-case (widget-get widget :case-fold))
2091 this-explicit
2092 current choices)
2093 ;; Remember old value.
2094 (if (and old (not (widget-apply widget :validate)))
2095 (let* ((external (widget-value widget))
2096 (internal (widget-apply old :value-to-internal external)))
2097 (widget-put old :value internal)))
2098 ;; Find new choice.
2099 (setq current
2100 (cond ((= (length args) 0)
2101 nil)
2102 ((= (length args) 1)
2103 (nth 0 args))
2104 ((and widget-choice-toggle
2105 (= (length args) 2)
2106 (memq old args))
2107 (if (eq old (nth 0 args))
2108 (nth 1 args)
2109 (nth 0 args)))
2111 (while args
2112 (setq current (car args)
2113 args (cdr args))
2114 (setq choices
2115 (cons (cons (widget-apply current :menu-tag-get)
2116 current)
2117 choices)))
2118 (setq this-explicit t)
2119 (widget-choose tag (reverse choices) event))))
2120 (when current
2121 ;; If this was an explicit user choice, record the choice,
2122 ;; so that widget-choice-value-create will respect it.
2123 (when this-explicit
2124 (widget-put widget :explicit-choice current))
2125 (widget-value-set widget (widget-default-get current))
2126 (widget-setup)
2127 (widget-apply widget :notify widget event)))
2128 (run-hook-with-args 'widget-edit-functions widget))
2130 (defun widget-choice-validate (widget)
2131 ;; Valid if we have made a valid choice.
2132 (if (eq (widget-get widget :void) (widget-get widget :choice))
2133 widget
2134 (widget-apply (car (widget-get widget :children)) :validate)))
2136 (defun widget-choice-match (widget value)
2137 ;; Matches if one of the choices matches.
2138 (let ((args (widget-get widget :args))
2139 current found)
2140 (while (and args (not found))
2141 (setq current (car args)
2142 args (cdr args)
2143 found (widget-apply current :match value)))
2144 found))
2146 (defun widget-choice-match-inline (widget vals)
2147 ;; Matches if one of the choices matches.
2148 (let ((args (widget-get widget :args))
2149 current found)
2150 (while (and args (null found))
2151 (setq current (car args)
2152 args (cdr args)
2153 found (widget-match-inline current vals)))
2154 found))
2156 ;;; The `toggle' Widget.
2158 (define-widget 'toggle 'item
2159 "Toggle between two states."
2160 :format "%[%v%]\n"
2161 :value-create 'widget-toggle-value-create
2162 :action 'widget-toggle-action
2163 :match (lambda (_widget _value) t)
2164 :on "on"
2165 :off "off")
2167 (defun widget-toggle-value-create (widget)
2168 "Insert text representing the `on' and `off' states."
2169 (let* ((val (widget-value widget))
2170 (text (widget-get widget (if val :on :off)))
2171 (img (widget-image-find
2172 (widget-get widget (if val :on-glyph :off-glyph)))))
2173 (widget-image-insert widget (or text "")
2174 (if img
2175 (append img '(:ascent center))))))
2177 (defun widget-toggle-action (widget &optional event)
2178 ;; Toggle value.
2179 (widget-value-set widget (not (widget-value widget)))
2180 (widget-apply widget :notify widget event)
2181 (run-hook-with-args 'widget-edit-functions widget))
2183 ;;; The `checkbox' Widget.
2185 (define-widget 'checkbox 'toggle
2186 "A checkbox toggle."
2187 :button-suffix ""
2188 :button-prefix ""
2189 :format "%[%v%]"
2190 :on "[X]"
2191 ;; We could probably do the same job as the images using single
2192 ;; space characters in a boxed face with a stretch specification to
2193 ;; make them square.
2194 :on-glyph "checked"
2195 :off "[ ]"
2196 :off-glyph "unchecked"
2197 :help-echo "Toggle this item."
2198 :action 'widget-checkbox-action)
2200 (defun widget-checkbox-action (widget &optional event)
2201 "Toggle checkbox, notify parent, and set active state of sibling."
2202 (widget-toggle-action widget event)
2203 (let ((sibling (widget-get-sibling widget)))
2204 (when sibling
2205 (if (widget-value widget)
2206 (widget-apply sibling :activate)
2207 (widget-apply sibling :deactivate))
2208 (widget-clear-undo))))
2210 ;;; The `checklist' Widget.
2212 (define-widget 'checklist 'default
2213 "A multiple choice widget."
2214 :convert-widget 'widget-types-convert-widget
2215 :copy 'widget-types-copy
2216 :format "%v"
2217 :offset 4
2218 :entry-format "%b %v"
2219 :greedy nil
2220 :value-create 'widget-checklist-value-create
2221 :value-get 'widget-checklist-value-get
2222 :validate 'widget-checklist-validate
2223 :match 'widget-checklist-match
2224 :match-inline 'widget-checklist-match-inline)
2226 (defun widget-checklist-value-create (widget)
2227 ;; Insert all values
2228 (let ((alist (widget-checklist-match-find widget))
2229 (args (widget-get widget :args)))
2230 (dolist (item args)
2231 (widget-checklist-add-item widget item (assq item alist)))
2232 (widget-put widget :children (nreverse (widget-get widget :children)))))
2234 (defun widget-checklist-add-item (widget type chosen)
2235 "Create checklist item in WIDGET of type TYPE.
2236 If the item is checked, CHOSEN is a cons whose cdr is the value."
2237 (and (eq (preceding-char) ?\n)
2238 (widget-get widget :indent)
2239 (insert-char ?\s (widget-get widget :indent)))
2240 (widget-specify-insert
2241 (let* ((children (widget-get widget :children))
2242 (buttons (widget-get widget :buttons))
2243 (button-args (or (widget-get type :sibling-args)
2244 (widget-get widget :button-args)))
2245 (from (point))
2246 child button)
2247 (insert (widget-get widget :entry-format))
2248 (goto-char from)
2249 ;; Parse % escapes in format.
2250 (while (re-search-forward "%\\([bv%]\\)" nil t)
2251 (let ((escape (char-after (match-beginning 1))))
2252 (delete-char -2)
2253 (cond ((eq escape ?%)
2254 (insert ?%))
2255 ((eq escape ?b)
2256 (setq button (apply 'widget-create-child-and-convert
2257 widget 'checkbox
2258 :value (not (null chosen))
2259 button-args)))
2260 ((eq escape ?v)
2261 (setq child
2262 (cond ((not chosen)
2263 (let ((child (widget-create-child widget type)))
2264 (widget-apply child :deactivate)
2265 child))
2266 ((widget-get type :inline)
2267 (widget-create-child-value
2268 widget type (cdr chosen)))
2270 (widget-create-child-value
2271 widget type (car (cdr chosen)))))))
2273 (error "Unknown escape `%c'" escape)))))
2274 ;; Update properties.
2275 (and button child (widget-put child :button button))
2276 (and button (widget-put widget :buttons (cons button buttons)))
2277 (and child (widget-put widget :children (cons child children))))))
2279 (defun widget-checklist-match (widget vals)
2280 ;; All values must match a type in the checklist.
2281 (and (listp vals)
2282 (null (cdr (widget-checklist-match-inline widget vals)))))
2284 (defun widget-checklist-match-inline (widget vals)
2285 ;; Find the values which match a type in the checklist.
2286 (let ((greedy (widget-get widget :greedy))
2287 (args (copy-sequence (widget-get widget :args)))
2288 found rest)
2289 (while vals
2290 (let ((answer (widget-checklist-match-up args vals)))
2291 (cond (answer
2292 (let ((vals2 (widget-match-inline answer vals)))
2293 (setq found (append found (car vals2))
2294 vals (cdr vals2)
2295 args (delq answer args))))
2296 (greedy
2297 (setq rest (append rest (list (car vals)))
2298 vals (cdr vals)))
2300 (setq rest (append rest vals)
2301 vals nil)))))
2302 (cons found rest)))
2304 (defun widget-checklist-match-find (widget &optional vals)
2305 "Find the vals which match a type in the checklist.
2306 Return an alist of (TYPE MATCH)."
2307 (or vals (setq vals (widget-get widget :value)))
2308 (let ((greedy (widget-get widget :greedy))
2309 (args (copy-sequence (widget-get widget :args)))
2310 found)
2311 (while vals
2312 (let ((answer (widget-checklist-match-up args vals)))
2313 (cond (answer
2314 (let ((match (widget-match-inline answer vals)))
2315 (setq found (cons (cons answer (car match)) found)
2316 vals (cdr match)
2317 args (delq answer args))))
2318 (greedy
2319 (setq vals (cdr vals)))
2321 (setq vals nil)))))
2322 found))
2324 (defun widget-checklist-match-up (args vals)
2325 "Return the first type from ARGS that matches VALS."
2326 (let (current found)
2327 (while (and args (null found))
2328 (setq current (car args)
2329 args (cdr args)
2330 found (widget-match-inline current vals)))
2331 (if found
2332 current)))
2334 (defun widget-checklist-value-get (widget)
2335 ;; The values of all selected items.
2336 (let ((children (widget-get widget :children))
2337 child result)
2338 (while children
2339 (setq child (car children)
2340 children (cdr children))
2341 (if (widget-value (widget-get child :button))
2342 (setq result (append result (widget-apply child :value-inline)))))
2343 result))
2345 (defun widget-checklist-validate (widget)
2346 ;; Ticked children must be valid.
2347 (let ((children (widget-get widget :children))
2348 child button found)
2349 (while (and children (not found))
2350 (setq child (car children)
2351 children (cdr children)
2352 button (widget-get child :button)
2353 found (and (widget-value button)
2354 (widget-apply child :validate))))
2355 found))
2357 ;;; The `option' Widget
2359 (define-widget 'option 'checklist
2360 "An widget with an optional item."
2361 :inline t)
2363 ;;; The `choice-item' Widget.
2365 (define-widget 'choice-item 'item
2366 "Button items that delegate action events to their parents."
2367 :action 'widget-parent-action
2368 :format "%[%t%] \n")
2370 ;;; The `radio-button' Widget.
2372 (define-widget 'radio-button 'toggle
2373 "A radio button for use in the `radio' widget."
2374 :notify 'widget-radio-button-notify
2375 :format "%[%v%]"
2376 :button-suffix ""
2377 :button-prefix ""
2378 :on "(*)"
2379 :on-glyph "radio1"
2380 :off "( )"
2381 :off-glyph "radio0")
2383 (defun widget-radio-button-notify (widget _child &optional event)
2384 ;; Tell daddy.
2385 (widget-apply (widget-get widget :parent) :action widget event))
2387 ;;; The `radio-button-choice' Widget.
2389 (define-widget 'radio-button-choice 'default
2390 "Select one of multiple options."
2391 :convert-widget 'widget-types-convert-widget
2392 :copy 'widget-types-copy
2393 :offset 4
2394 :format "%v"
2395 :entry-format "%b %v"
2396 :value-create 'widget-radio-value-create
2397 :value-get 'widget-radio-value-get
2398 :value-inline 'widget-radio-value-inline
2399 :value-set 'widget-radio-value-set
2400 :error "You must push one of the buttons"
2401 :validate 'widget-radio-validate
2402 :match 'widget-choice-match
2403 :match-inline 'widget-choice-match-inline
2404 :action 'widget-radio-action)
2406 (defun widget-radio-value-create (widget)
2407 ;; Insert all values
2408 (let ((args (widget-get widget :args))
2409 arg)
2410 (while args
2411 (setq arg (car args)
2412 args (cdr args))
2413 (widget-radio-add-item widget arg))))
2415 (defun widget-radio-add-item (widget type)
2416 "Add to radio widget WIDGET a new radio button item of type TYPE."
2417 ;; (setq type (widget-convert type))
2418 (and (eq (preceding-char) ?\n)
2419 (widget-get widget :indent)
2420 (insert-char ?\s (widget-get widget :indent)))
2421 (widget-specify-insert
2422 (let* ((value (widget-get widget :value))
2423 (children (widget-get widget :children))
2424 (buttons (widget-get widget :buttons))
2425 (button-args (or (widget-get type :sibling-args)
2426 (widget-get widget :button-args)))
2427 (from (point))
2428 (chosen (and (null (widget-get widget :choice))
2429 (widget-apply type :match value)))
2430 child button)
2431 (insert (widget-get widget :entry-format))
2432 (goto-char from)
2433 ;; Parse % escapes in format.
2434 (while (re-search-forward "%\\([bv%]\\)" nil t)
2435 (let ((escape (char-after (match-beginning 1))))
2436 (delete-char -2)
2437 (cond ((eq escape ?%)
2438 (insert ?%))
2439 ((eq escape ?b)
2440 (setq button (apply 'widget-create-child-and-convert
2441 widget 'radio-button
2442 :value (not (null chosen))
2443 button-args)))
2444 ((eq escape ?v)
2445 (setq child (if chosen
2446 (widget-create-child-value
2447 widget type value)
2448 (widget-create-child widget type)))
2449 (unless chosen
2450 (widget-apply child :deactivate)))
2452 (error "Unknown escape `%c'" escape)))))
2453 ;; Update properties.
2454 (when chosen
2455 (widget-put widget :choice type))
2456 (when button
2457 (widget-put child :button button)
2458 (widget-put widget :buttons (nconc buttons (list button))))
2459 (when child
2460 (widget-put widget :children (nconc children (list child))))
2461 child)))
2463 (defun widget-radio-value-get (widget)
2464 ;; Get value of the child widget.
2465 (let ((chosen (widget-radio-chosen widget)))
2466 (and chosen (widget-value chosen))))
2468 (defun widget-radio-chosen (widget)
2469 "Return the widget representing the chosen radio button."
2470 (let ((children (widget-get widget :children))
2471 current found)
2472 (while children
2473 (setq current (car children)
2474 children (cdr children))
2475 (when (widget-apply (widget-get current :button) :value-get)
2476 (setq found current
2477 children nil)))
2478 found))
2480 (defun widget-radio-value-inline (widget)
2481 ;; Get value of the child widget.
2482 (let ((children (widget-get widget :children))
2483 current found)
2484 (while children
2485 (setq current (car children)
2486 children (cdr children))
2487 (when (widget-apply (widget-get current :button) :value-get)
2488 (setq found (widget-apply current :value-inline)
2489 children nil)))
2490 found))
2492 (defun widget-radio-value-set (widget value)
2493 ;; We can't just delete and recreate a radio widget, since children
2494 ;; can be added after the original creation and won't be recreated
2495 ;; by `:create'.
2496 (let ((children (widget-get widget :children))
2497 current found)
2498 (while children
2499 (setq current (car children)
2500 children (cdr children))
2501 (let* ((button (widget-get current :button))
2502 (match (and (not found)
2503 (widget-apply current :match value))))
2504 (widget-value-set button match)
2505 (if match
2506 (progn
2507 (widget-value-set current value)
2508 (widget-apply current :activate))
2509 (widget-apply current :deactivate))
2510 (setq found (or found match))))))
2512 (defun widget-radio-validate (widget)
2513 ;; Valid if we have made a valid choice.
2514 (let ((children (widget-get widget :children))
2515 current found button)
2516 (while (and children (not found))
2517 (setq current (car children)
2518 children (cdr children)
2519 button (widget-get current :button)
2520 found (widget-apply button :value-get)))
2521 (if found
2522 (widget-apply current :validate)
2523 widget)))
2525 (defun widget-radio-action (widget child event)
2526 ;; Check if a radio button was pressed.
2527 (let ((children (widget-get widget :children))
2528 (buttons (widget-get widget :buttons))
2529 current)
2530 (when (memq child buttons)
2531 (while children
2532 (setq current (car children)
2533 children (cdr children))
2534 (let* ((button (widget-get current :button)))
2535 (cond ((eq child button)
2536 (widget-value-set button t)
2537 (widget-apply current :activate))
2538 ((widget-value button)
2539 (widget-value-set button nil)
2540 (widget-apply current :deactivate)))))))
2541 ;; Pass notification to parent.
2542 (widget-apply widget :notify child event))
2544 ;;; The `insert-button' Widget.
2546 (define-widget 'insert-button 'push-button
2547 "An insert button for the `editable-list' widget."
2548 :tag "INS"
2549 :help-echo "Insert a new item into the list at this position."
2550 :action 'widget-insert-button-action)
2552 (defun widget-insert-button-action (widget &optional _event)
2553 ;; Ask the parent to insert a new item.
2554 (widget-apply (widget-get widget :parent)
2555 :insert-before (widget-get widget :widget)))
2557 ;;; The `delete-button' Widget.
2559 (define-widget 'delete-button 'push-button
2560 "A delete button for the `editable-list' widget."
2561 :tag "DEL"
2562 :help-echo "Delete this item from the list."
2563 :action 'widget-delete-button-action)
2565 (defun widget-delete-button-action (widget &optional _event)
2566 ;; Ask the parent to insert a new item.
2567 (widget-apply (widget-get widget :parent)
2568 :delete-at (widget-get widget :widget)))
2570 ;;; The `editable-list' Widget.
2572 ;; (defcustom widget-editable-list-gui nil
2573 ;; "If non-nil, use GUI push-buttons in editable list when available."
2574 ;; :type 'boolean
2575 ;; :group 'widgets)
2577 (define-widget 'editable-list 'default
2578 "A variable list of widgets of the same type."
2579 :convert-widget 'widget-types-convert-widget
2580 :copy 'widget-types-copy
2581 :offset 12
2582 :format "%v%i\n"
2583 :format-handler 'widget-editable-list-format-handler
2584 :entry-format "%i %d %v"
2585 :value-create 'widget-editable-list-value-create
2586 :value-get 'widget-editable-list-value-get
2587 :validate 'widget-children-validate
2588 :match 'widget-editable-list-match
2589 :match-inline 'widget-editable-list-match-inline
2590 :insert-before 'widget-editable-list-insert-before
2591 :delete-at 'widget-editable-list-delete-at)
2593 (defun widget-editable-list-format-handler (widget escape)
2594 ;; We recognize the insert button.
2595 ;; (let ((widget-push-button-gui widget-editable-list-gui))
2596 (cond ((eq escape ?i)
2597 (and (widget-get widget :indent)
2598 (insert-char ?\s (widget-get widget :indent)))
2599 (apply 'widget-create-child-and-convert
2600 widget 'insert-button
2601 (widget-get widget :append-button-args)))
2603 (widget-default-format-handler widget escape)))
2604 ;; )
2607 (defun widget-editable-list-value-create (widget)
2608 ;; Insert all values
2609 (let* ((value (widget-get widget :value))
2610 (type (nth 0 (widget-get widget :args)))
2611 children)
2612 (widget-put widget :value-pos (point-marker))
2613 (set-marker-insertion-type (widget-get widget :value-pos) t)
2614 (while value
2615 (let ((answer (widget-match-inline type value)))
2616 (if answer
2617 (setq children (cons (widget-editable-list-entry-create
2618 widget
2619 (if (widget-get type :inline)
2620 (car answer)
2621 (car (car answer)))
2623 children)
2624 value (cdr answer))
2625 (setq value nil))))
2626 (widget-put widget :children (nreverse children))))
2628 (defun widget-editable-list-value-get (widget)
2629 ;; Get value of the child widget.
2630 (apply 'append (mapcar (lambda (child) (widget-apply child :value-inline))
2631 (widget-get widget :children))))
2633 (defun widget-editable-list-match (widget value)
2634 ;; Value must be a list and all the members must match the type.
2635 (and (listp value)
2636 (null (cdr (widget-editable-list-match-inline widget value)))))
2638 (defun widget-editable-list-match-inline (widget value)
2639 (let ((type (nth 0 (widget-get widget :args)))
2640 (ok t)
2641 found)
2642 (while (and value ok)
2643 (let ((answer (widget-match-inline type value)))
2644 (if answer
2645 (setq found (append found (car answer))
2646 value (cdr answer))
2647 (setq ok nil))))
2648 (cons found value)))
2650 (defun widget-editable-list-insert-before (widget before)
2651 ;; Insert a new child in the list of children.
2652 (save-excursion
2653 (let ((children (widget-get widget :children))
2654 (inhibit-read-only t)
2655 before-change-functions
2656 after-change-functions)
2657 (cond (before
2658 (goto-char (widget-get before :entry-from)))
2660 (goto-char (widget-get widget :value-pos))))
2661 (let ((child (widget-editable-list-entry-create
2662 widget nil nil)))
2663 (when (< (widget-get child :entry-from) (widget-get widget :from))
2664 (set-marker (widget-get widget :from)
2665 (widget-get child :entry-from)))
2666 (if (eq (car children) before)
2667 (widget-put widget :children (cons child children))
2668 (while (not (eq (car (cdr children)) before))
2669 (setq children (cdr children)))
2670 (setcdr children (cons child (cdr children)))))))
2671 (widget-setup)
2672 (widget-apply widget :notify widget))
2674 (defun widget-editable-list-delete-at (widget child)
2675 ;; Delete child from list of children.
2676 (save-excursion
2677 (let ((buttons (copy-sequence (widget-get widget :buttons)))
2678 button
2679 (inhibit-read-only t)
2680 before-change-functions
2681 after-change-functions)
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 before-change-functions
2693 after-change-functions)
2694 (widget-delete child)
2695 (delete-region entry-from entry-to)
2696 (set-marker entry-from nil)
2697 (set-marker entry-to nil))
2698 (widget-put widget :children (delq child (widget-get widget :children))))
2699 (widget-setup)
2700 (widget-apply widget :notify widget))
2702 (defun widget-editable-list-entry-create (widget value conv)
2703 ;; Create a new entry to the list.
2704 (let ((type (nth 0 (widget-get widget :args)))
2705 ;; (widget-push-button-gui widget-editable-list-gui)
2706 child delete insert)
2707 (widget-specify-insert
2708 (save-excursion
2709 (and (widget-get widget :indent)
2710 (insert-char ?\s (widget-get widget :indent)))
2711 (insert (widget-get widget :entry-format)))
2712 ;; Parse % escapes in format.
2713 (while (re-search-forward "%\\(.\\)" nil t)
2714 (let ((escape (char-after (match-beginning 1))))
2715 (delete-char -2)
2716 (cond ((eq escape ?%)
2717 (insert ?%))
2718 ((eq escape ?i)
2719 (setq insert (apply 'widget-create-child-and-convert
2720 widget 'insert-button
2721 (widget-get widget :insert-button-args))))
2722 ((eq escape ?d)
2723 (setq delete (apply 'widget-create-child-and-convert
2724 widget 'delete-button
2725 (widget-get widget :delete-button-args))))
2726 ((eq escape ?v)
2727 (if conv
2728 (setq child (widget-create-child-value
2729 widget type value))
2730 (setq child (widget-create-child-value
2731 widget type (widget-default-get type)))))
2733 (error "Unknown escape `%c'" escape)))))
2734 (let ((buttons (widget-get widget :buttons)))
2735 (if insert (push insert buttons))
2736 (if delete (push delete buttons))
2737 (widget-put widget :buttons buttons))
2738 (let ((entry-from (point-min-marker))
2739 (entry-to (point-max-marker)))
2740 (set-marker-insertion-type entry-from t)
2741 (set-marker-insertion-type entry-to nil)
2742 (widget-put child :entry-from entry-from)
2743 (widget-put child :entry-to entry-to)))
2744 (if insert (widget-put insert :widget child))
2745 (if delete (widget-put delete :widget child))
2746 child))
2748 ;;; The `group' Widget.
2750 (define-widget 'group 'default
2751 "A widget which groups other widgets inside."
2752 :convert-widget 'widget-types-convert-widget
2753 :copy 'widget-types-copy
2754 :format "%v"
2755 :value-create 'widget-group-value-create
2756 :value-get 'widget-editable-list-value-get
2757 :default-get 'widget-group-default-get
2758 :validate 'widget-children-validate
2759 :match 'widget-group-match
2760 :match-inline 'widget-group-match-inline)
2762 (defun widget-group-value-create (widget)
2763 ;; Create each component.
2764 (let ((args (widget-get widget :args))
2765 (value (widget-get widget :value))
2766 arg answer children)
2767 (while args
2768 (setq arg (car args)
2769 args (cdr args)
2770 answer (widget-match-inline arg value)
2771 value (cdr answer))
2772 (and (eq (preceding-char) ?\n)
2773 (widget-get widget :indent)
2774 (insert-char ?\s (widget-get widget :indent)))
2775 (push (cond ((null answer)
2776 (widget-create-child widget arg))
2777 ((widget-get arg :inline)
2778 (widget-create-child-value widget arg (car answer)))
2780 (widget-create-child-value widget arg (car (car answer)))))
2781 children))
2782 (widget-put widget :children (nreverse children))))
2784 (defun widget-group-default-get (widget)
2785 ;; Get the default of the components.
2786 (mapcar 'widget-default-get (widget-get widget :args)))
2788 (defun widget-group-match (widget vals)
2789 ;; Match if the components match.
2790 (and (listp vals)
2791 (let ((match (widget-group-match-inline widget vals)))
2792 (and match (null (cdr match))))))
2794 (defun widget-group-match-inline (widget vals)
2795 ;; Match if the components match.
2796 (let ((args (widget-get widget :args))
2797 argument answer found)
2798 (while args
2799 (setq argument (car args)
2800 args (cdr args))
2801 (if (setq answer (widget-match-inline argument vals))
2802 (setq found (append found (car answer))
2803 vals (cdr answer))
2804 (setq vals nil
2805 args nil)))
2806 (if answer
2807 (cons found vals))))
2809 ;;; The `visibility' Widget.
2811 (define-widget 'visibility 'item
2812 "An indicator and manipulator for hidden items.
2814 The following properties have special meanings for this widget:
2815 :on-glyph Image filename or spec to display when the item is visible.
2816 :on Text shown if the \"on\" image is nil or cannot be displayed.
2817 :off-glyph Image filename or spec to display when the item is hidden.
2818 :off Text shown if the \"off\" image is nil cannot be displayed."
2819 :format "%[%v%]"
2820 :button-prefix ""
2821 :button-suffix ""
2822 :on-glyph "down"
2823 :on "Hide"
2824 :off-glyph "right"
2825 :off "Show"
2826 :value-create 'widget-visibility-value-create
2827 :action 'widget-toggle-action
2828 :match (lambda (_widget _value) t))
2830 (defalias 'widget-visibility-value-create 'widget-toggle-value-create)
2832 ;;; The `documentation-link' Widget.
2834 ;; This is a helper widget for `documentation-string'.
2836 (define-widget 'documentation-link 'link
2837 "Link type used in documentation strings."
2838 :tab-order -1
2839 :help-echo "Describe this symbol"
2840 :action 'widget-documentation-link-action)
2842 (defun widget-documentation-link-action (widget &optional _event)
2843 "Display documentation for WIDGET's value. Ignore optional argument EVENT."
2844 (let* ((string (widget-get widget :value))
2845 (symbol (intern string)))
2846 (if (and (fboundp symbol) (boundp symbol))
2847 ;; If there are two doc strings, give the user a way to pick one.
2848 (apropos (concat "\\`" (regexp-quote string) "\\'"))
2849 (if (fboundp symbol)
2850 (describe-function symbol)
2851 (describe-variable symbol)))))
2853 (defcustom widget-documentation-links t
2854 "Add hyperlinks to documentation strings when non-nil."
2855 :type 'boolean
2856 :group 'widget-documentation)
2858 (defcustom widget-documentation-link-regexp "`\\([^\n`' ]+\\)'"
2859 "Regexp for matching potential links in documentation strings.
2860 The first group should be the link itself."
2861 :type 'regexp
2862 :group 'widget-documentation)
2864 (defcustom widget-documentation-link-p 'intern-soft
2865 "Predicate used to test if a string is useful as a link.
2866 The value should be a function. The function will be called with one
2867 argument, a string, and should return non-nil if there should be a
2868 link for that string."
2869 :type 'function
2870 :options '(widget-documentation-link-p)
2871 :group 'widget-documentation)
2873 (defcustom widget-documentation-link-type 'documentation-link
2874 "Widget type used for links in documentation strings."
2875 :type 'symbol
2876 :group 'widget-documentation)
2878 (defun widget-documentation-link-add (widget from to)
2879 (widget-specify-doc widget from to)
2880 (when widget-documentation-links
2881 (let ((regexp widget-documentation-link-regexp)
2882 (buttons (widget-get widget :buttons))
2883 (widget-mouse-face (default-value 'widget-mouse-face))
2884 (widget-button-face widget-documentation-face)
2885 (widget-button-pressed-face widget-documentation-face))
2886 (save-excursion
2887 (goto-char from)
2888 (while (re-search-forward regexp to t)
2889 (let ((name (match-string 1))
2890 (begin (match-beginning 1))
2891 (end (match-end 1)))
2892 (when (funcall widget-documentation-link-p name)
2893 (push (widget-convert-button widget-documentation-link-type
2894 begin end :value name)
2895 buttons)))))
2896 (widget-put widget :buttons buttons))))
2898 ;;; The `documentation-string' Widget.
2900 (define-widget 'documentation-string 'item
2901 "A documentation string."
2902 :format "%v"
2903 :action 'widget-documentation-string-action
2904 :value-create 'widget-documentation-string-value-create
2905 :visibility-widget 'visibility)
2907 (defun widget-documentation-string-value-create (widget)
2908 ;; Insert documentation string.
2909 (let ((doc (widget-value widget))
2910 (indent (widget-get widget :indent))
2911 (shown (widget-get (widget-get widget :parent) :documentation-shown))
2912 (start (point)))
2913 (if (string-match "\n" doc)
2914 (let ((before (substring doc 0 (match-beginning 0)))
2915 (after (substring doc (match-end 0)))
2916 button end)
2917 (widget-documentation-string-indent-to indent)
2918 (insert before ?\s)
2919 (widget-documentation-link-add widget start (point))
2920 (setq button
2921 (widget-create-child-and-convert
2922 widget (widget-get widget :visibility-widget)
2923 :help-echo "Show or hide rest of the documentation."
2924 :on "Hide"
2925 :off "More"
2926 :always-active t
2927 :action 'widget-parent-action
2928 shown))
2929 (when shown
2930 (insert ?\n)
2931 (setq start (point))
2932 (when (and indent (not (zerop indent)))
2933 (insert-char ?\s indent))
2934 (insert after)
2935 (setq end (point))
2936 (widget-documentation-link-add widget start end)
2937 ;; Indent the subsequent lines.
2938 (when (and indent (> indent 0))
2939 (save-excursion
2940 (save-restriction
2941 (narrow-to-region start end)
2942 (goto-char (point-min))
2943 (while (search-forward "\n" nil t)
2944 (widget-documentation-string-indent-to indent))))))
2945 (widget-put widget :buttons (list button)))
2946 (widget-documentation-string-indent-to indent)
2947 (insert doc)
2948 (widget-documentation-link-add widget start (point))))
2949 (insert ?\n))
2951 (defun widget-documentation-string-indent-to (col)
2952 (when (and (numberp col)
2953 (> col 0))
2954 (let ((opoint (point)))
2955 (indent-to col)
2956 (put-text-property opoint (point)
2957 'display `(space :align-to ,col)))))
2959 (defun widget-documentation-string-action (widget &rest _ignore)
2960 ;; Toggle documentation.
2961 (let ((parent (widget-get widget :parent)))
2962 (widget-put parent :documentation-shown
2963 (not (widget-get parent :documentation-shown))))
2964 ;; Redraw.
2965 (widget-value-set widget (widget-value widget)))
2967 (defun widget-add-documentation-string-button (widget &rest args)
2968 "Insert a new `documentation-string' widget based on WIDGET.
2969 The new widget becomes a child of WIDGET, and is also added to
2970 its `:buttons' list. The documentation string is found from
2971 WIDGET using the function `widget-docstring'.
2972 Optional ARGS specifies additional keyword arguments for the
2973 `documentation-string' widget."
2974 (let ((doc (widget-docstring widget))
2975 (indent (widget-get widget :indent))
2976 (doc-indent (widget-get widget :documentation-indent)))
2977 (when doc
2978 (and (eq (preceding-char) ?\n)
2979 indent
2980 (insert-char ?\s indent))
2981 (unless (or (numberp doc-indent) (null doc-indent))
2982 (setq doc-indent 0))
2983 (widget-put widget :buttons
2984 (cons (apply 'widget-create-child-and-convert
2985 widget 'documentation-string
2986 :indent doc-indent
2987 (nconc args (list doc)))
2988 (widget-get widget :buttons))))))
2990 ;;; The Sexp Widgets.
2992 (define-widget 'const 'item
2993 "An immutable sexp."
2994 :prompt-value 'widget-const-prompt-value
2995 :format "%t\n%d")
2997 (defun widget-const-prompt-value (widget _prompt _value _unbound)
2998 ;; Return the value of the const.
2999 (widget-value widget))
3001 (define-widget 'function-item 'const
3002 "An immutable function name."
3003 :format "%v\n%h"
3004 :documentation-property (lambda (symbol)
3005 (condition-case nil
3006 (documentation symbol t)
3007 (error nil))))
3009 (define-widget 'variable-item 'const
3010 "An immutable variable name."
3011 :format "%v\n%h"
3012 :documentation-property 'variable-documentation)
3014 (define-widget 'other 'sexp
3015 "Matches any value, but doesn't let the user edit the value.
3016 This is useful as last item in a `choice' widget.
3017 You should use this widget type with a default value,
3018 as in (other DEFAULT) or (other :tag \"NAME\" DEFAULT).
3019 If the user selects this alternative, that specifies DEFAULT
3020 as the value."
3021 :tag "Other"
3022 :format "%t%n"
3023 :value 'other)
3025 (defvar widget-string-prompt-value-history nil
3026 "History of input to `widget-string-prompt-value'.")
3028 (define-widget 'string 'editable-field
3029 "A string"
3030 :tag "String"
3031 :format "%{%t%}: %v"
3032 :complete-function 'ispell-complete-word
3033 :prompt-history 'widget-string-prompt-value-history)
3035 (define-widget 'regexp 'string
3036 "A regular expression."
3037 :match 'widget-regexp-match
3038 :validate 'widget-regexp-validate
3039 ;; Doesn't work well with terminating newline.
3040 ;; :value-face 'widget-single-line-field
3041 :tag "Regexp")
3043 (defun widget-regexp-match (_widget value)
3044 ;; Match valid regexps.
3045 (and (stringp value)
3046 (condition-case nil
3047 (prog1 t
3048 (string-match value ""))
3049 (error nil))))
3051 (defun widget-regexp-validate (widget)
3052 "Check that the value of WIDGET is a valid regexp."
3053 (condition-case data
3054 (prog1 nil
3055 (string-match (widget-value widget) ""))
3056 (error (widget-put widget :error (error-message-string data))
3057 widget)))
3059 (define-widget 'file 'string
3060 "A file widget.
3061 It reads a file name from an editable text field."
3062 :completions #'completion-file-name-table
3063 :prompt-value 'widget-file-prompt-value
3064 :format "%{%t%}: %v"
3065 ;; Doesn't work well with terminating newline.
3066 ;; :value-face 'widget-single-line-field
3067 :tag "File")
3069 (defun widget-file-prompt-value (widget prompt value unbound)
3070 ;; Read file from minibuffer.
3071 (abbreviate-file-name
3072 (if unbound
3073 (read-file-name prompt)
3074 (let ((prompt2 (format "%s (default %s): " prompt value))
3075 (dir (file-name-directory value))
3076 (file (file-name-nondirectory value))
3077 (must-match (widget-get widget :must-match)))
3078 (read-file-name prompt2 dir nil must-match file)))))
3080 ;;;(defun widget-file-action (widget &optional event)
3081 ;;; ;; Read a file name from the minibuffer.
3082 ;;; (let* ((value (widget-value widget))
3083 ;;; (dir (file-name-directory value))
3084 ;;; (file (file-name-nondirectory value))
3085 ;;; (menu-tag (widget-apply widget :menu-tag-get))
3086 ;;; (must-match (widget-get widget :must-match))
3087 ;;; (answer (read-file-name (concat menu-tag " (default " value "): ")
3088 ;;; dir nil must-match file)))
3089 ;;; (widget-value-set widget (abbreviate-file-name answer))
3090 ;;; (widget-setup)
3091 ;;; (widget-apply widget :notify widget event)))
3093 ;; Fixme: use file-name-as-directory.
3094 (define-widget 'directory 'file
3095 "A directory widget.
3096 It reads a directory name from an editable text field."
3097 :tag "Directory")
3099 (defvar widget-symbol-prompt-value-history nil
3100 "History of input to `widget-symbol-prompt-value'.")
3102 (define-widget 'symbol 'editable-field
3103 "A Lisp symbol."
3104 :value nil
3105 :tag "Symbol"
3106 :format "%{%t%}: %v"
3107 :match (lambda (_widget value) (symbolp value))
3108 :completions obarray
3109 :prompt-internal 'widget-symbol-prompt-internal
3110 :prompt-match 'symbolp
3111 :prompt-history 'widget-symbol-prompt-value-history
3112 :value-to-internal (lambda (_widget value)
3113 (if (symbolp value)
3114 (symbol-name value)
3115 value))
3116 :value-to-external (lambda (_widget value)
3117 (if (stringp value)
3118 (intern value)
3119 value)))
3121 (defun widget-symbol-prompt-internal (widget prompt initial history)
3122 ;; Read file from minibuffer.
3123 (let ((answer (completing-read prompt obarray
3124 (widget-get widget :prompt-match)
3125 nil initial history)))
3126 (if (and (stringp answer)
3127 (not (zerop (length answer))))
3128 answer
3129 (error "No value"))))
3131 (defvar widget-function-prompt-value-history nil
3132 "History of input to `widget-function-prompt-value'.")
3134 (define-widget 'function 'restricted-sexp
3135 "A Lisp function."
3136 :completions (apply-partially #'completion-table-with-predicate
3137 obarray #'fboundp 'strict)
3138 :prompt-value 'widget-field-prompt-value
3139 :prompt-internal 'widget-symbol-prompt-internal
3140 :prompt-match 'fboundp
3141 :prompt-history 'widget-function-prompt-value-history
3142 :action 'widget-field-action
3143 :match-alternatives '(functionp)
3144 :validate (lambda (widget)
3145 (unless (functionp (widget-value widget))
3146 (widget-put widget :error (format "Invalid function: %S"
3147 (widget-value widget)))
3148 widget))
3149 :value 'ignore
3150 :tag "Function")
3152 (defvar widget-variable-prompt-value-history nil
3153 "History of input to `widget-variable-prompt-value'.")
3155 (define-widget 'variable 'symbol
3156 "A Lisp variable."
3157 :prompt-match 'boundp
3158 :prompt-history 'widget-variable-prompt-value-history
3159 :completions (apply-partially #'completion-table-with-predicate
3160 obarray #'boundp 'strict)
3161 :tag "Variable")
3163 (define-widget 'coding-system 'symbol
3164 "A MULE coding-system."
3165 :format "%{%t%}: %v"
3166 :tag "Coding system"
3167 :base-only nil
3168 :prompt-history 'coding-system-value-history
3169 :prompt-value 'widget-coding-system-prompt-value
3170 :action 'widget-coding-system-action
3171 :completions (apply-partially #'completion-table-with-predicate
3172 obarray #'coding-system-p 'strict)
3173 :validate (lambda (widget)
3174 (unless (coding-system-p (widget-value widget))
3175 (widget-put widget :error (format "Invalid coding system: %S"
3176 (widget-value widget)))
3177 widget))
3178 :value 'undecided
3179 :prompt-match 'coding-system-p)
3181 (defun widget-coding-system-prompt-value (widget prompt value _unbound)
3182 "Read coding-system from minibuffer."
3183 (if (widget-get widget :base-only)
3184 (intern
3185 (completing-read (format "%s (default %s): " prompt value)
3186 (mapcar #'list (coding-system-list t)) nil nil nil
3187 coding-system-history))
3188 (read-coding-system (format "%s (default %s): " prompt value) value)))
3190 (defun widget-coding-system-action (widget &optional event)
3191 (let ((answer
3192 (widget-coding-system-prompt-value
3193 widget
3194 (widget-apply widget :menu-tag-get)
3195 (widget-value widget)
3196 t)))
3197 (widget-value-set widget answer)
3198 (widget-apply widget :notify widget event)
3199 (widget-setup)))
3201 ;;; I'm not sure about what this is good for? KFS.
3202 (defvar widget-key-sequence-prompt-value-history nil
3203 "History of input to `widget-key-sequence-prompt-value'.")
3205 (defvar widget-key-sequence-default-value [ignore]
3206 "Default value for an empty key sequence.")
3208 (defvar widget-key-sequence-map
3209 (let ((map (make-sparse-keymap)))
3210 (set-keymap-parent map widget-field-keymap)
3211 (define-key map [(control ?q)] 'widget-key-sequence-read-event)
3212 map))
3214 (define-widget 'key-sequence 'restricted-sexp
3215 "A key sequence."
3216 :prompt-value 'widget-field-prompt-value
3217 :prompt-internal 'widget-symbol-prompt-internal
3218 ; :prompt-match 'fboundp ;; What was this good for? KFS
3219 :prompt-history 'widget-key-sequence-prompt-value-history
3220 :action 'widget-field-action
3221 :match-alternatives '(stringp vectorp)
3222 :format "%{%t%}: %v"
3223 :validate 'widget-key-sequence-validate
3224 :value-to-internal 'widget-key-sequence-value-to-internal
3225 :value-to-external 'widget-key-sequence-value-to-external
3226 :value widget-key-sequence-default-value
3227 :keymap widget-key-sequence-map
3228 :help-echo "C-q: insert KEY, EVENT, or CODE; RET: enter value"
3229 :tag "Key sequence")
3231 (defun widget-key-sequence-read-event (ev)
3232 (interactive (list
3233 (let ((inhibit-quit t) quit-flag)
3234 (read-event "Insert KEY, EVENT, or CODE: "))))
3235 (let ((ev2 (and (memq 'down (event-modifiers ev))
3236 (read-event)))
3237 (tr (and (keymapp function-key-map)
3238 (lookup-key function-key-map (vector ev)))))
3239 (when (and (integerp ev)
3240 (or (and (<= ?0 ev) (< ev (+ ?0 (min 10 read-quoted-char-radix))))
3241 (and (<= ?a (downcase ev))
3242 (< (downcase ev) (+ ?a -10 (min 36 read-quoted-char-radix))))))
3243 (setq unread-command-events (cons ev unread-command-events)
3244 ev (read-quoted-char (format "Enter code (radix %d)" read-quoted-char-radix))
3245 tr nil)
3246 (if (and (integerp ev) (not (characterp ev)))
3247 (insert (char-to-string ev)))) ;; throw invalid char error
3248 (setq ev (key-description (list ev)))
3249 (when (arrayp tr)
3250 (setq tr (key-description (list (aref tr 0))))
3251 (if (y-or-n-p (format "Key %s is translated to %s -- use %s? " ev tr tr))
3252 (setq ev tr ev2 nil)))
3253 (insert (if (= (char-before) ?\s) "" " ") ev " ")
3254 (if ev2
3255 (insert (key-description (list ev2)) " "))))
3257 (defun widget-key-sequence-validate (widget)
3258 (unless (or (stringp (widget-value widget))
3259 (vectorp (widget-value widget)))
3260 (widget-put widget :error (format "Invalid key sequence: %S"
3261 (widget-value widget)))
3262 widget))
3264 (defun widget-key-sequence-value-to-internal (widget value)
3265 (if (widget-apply widget :match value)
3266 (if (equal value widget-key-sequence-default-value)
3268 (key-description value))
3269 value))
3271 (defun widget-key-sequence-value-to-external (_widget value)
3272 (if (stringp value)
3273 (if (string-match "\\`[[:space:]]*\\'" value)
3274 widget-key-sequence-default-value
3275 (read-kbd-macro value))
3276 value))
3279 (define-widget 'sexp 'editable-field
3280 "An arbitrary Lisp expression."
3281 :tag "Lisp expression"
3282 :format "%{%t%}: %v"
3283 :value nil
3284 :validate 'widget-sexp-validate
3285 :match (lambda (_widget _value) t)
3286 :value-to-internal 'widget-sexp-value-to-internal
3287 :value-to-external (lambda (_widget value) (read value))
3288 :prompt-history 'widget-sexp-prompt-value-history
3289 :prompt-value 'widget-sexp-prompt-value)
3291 (defun widget-sexp-value-to-internal (_widget value)
3292 ;; Use pp for printer representation.
3293 (let ((pp (if (symbolp value)
3294 (prin1-to-string value)
3295 (pp-to-string value))))
3296 (while (string-match "\n\\'" pp)
3297 (setq pp (substring pp 0 -1)))
3298 (if (or (string-match "\n\\'" pp)
3299 (> (length pp) 40))
3300 (concat "\n" pp)
3301 pp)))
3303 (defun widget-sexp-validate (widget)
3304 ;; Valid if we can read the string and there is no junk left after it.
3305 (with-temp-buffer
3306 (insert (widget-apply widget :value-get))
3307 (goto-char (point-min))
3308 (let (err)
3309 (condition-case data ;Note: We get a spurious byte-compile warning here.
3310 (progn
3311 ;; Avoid a confusing end-of-file error.
3312 (skip-syntax-forward "\\s-")
3313 (if (eobp)
3314 (setq err "Empty sexp -- use `nil'?")
3315 (unless (widget-apply widget :match (read (current-buffer)))
3316 (setq err (widget-get widget :type-error))))
3317 ;; Allow whitespace after expression.
3318 (skip-syntax-forward "\\s-")
3319 (if (and (not (eobp))
3320 (not err))
3321 (setq err (format "Junk at end of expression: %s"
3322 (buffer-substring (point)
3323 (point-max))))))
3324 (end-of-file ; Avoid confusing error message.
3325 (setq err "Unbalanced sexp"))
3326 (error (setq err (error-message-string data))))
3327 (if (not err)
3329 (widget-put widget :error err)
3330 widget))))
3332 (defvar widget-sexp-prompt-value-history nil
3333 "History of input to `widget-sexp-prompt-value'.")
3335 (defun widget-sexp-prompt-value (widget prompt value unbound)
3336 ;; Read an arbitrary sexp.
3337 (let ((found (read-string prompt
3338 (if unbound nil (cons (prin1-to-string value) 0))
3339 (widget-get widget :prompt-history))))
3340 (let ((answer (read-from-string found)))
3341 (unless (= (cdr answer) (length found))
3342 (error "Junk at end of expression: %s"
3343 (substring found (cdr answer))))
3344 (car answer))))
3346 (define-widget 'restricted-sexp 'sexp
3347 "A Lisp expression restricted to values that match.
3348 To use this type, you must define :match or :match-alternatives."
3349 :type-error "The specified value is not valid"
3350 :match 'widget-restricted-sexp-match
3351 :value-to-internal (lambda (widget value)
3352 (if (widget-apply widget :match value)
3353 (prin1-to-string value)
3354 value)))
3356 (defun widget-restricted-sexp-match (widget value)
3357 (let ((alternatives (widget-get widget :match-alternatives))
3358 matched)
3359 (while (and alternatives (not matched))
3360 (if (cond ((functionp (car alternatives))
3361 (funcall (car alternatives) value))
3362 ((and (consp (car alternatives))
3363 (eq (car (car alternatives)) 'quote))
3364 (eq value (nth 1 (car alternatives)))))
3365 (setq matched t))
3366 (setq alternatives (cdr alternatives)))
3367 matched))
3369 (define-widget 'integer 'restricted-sexp
3370 "An integer."
3371 :tag "Integer"
3372 :value 0
3373 :type-error "This field should contain an integer"
3374 :match-alternatives '(integerp))
3376 (define-widget 'number 'restricted-sexp
3377 "A number (floating point or integer)."
3378 :tag "Number"
3379 :value 0.0
3380 :type-error "This field should contain a number (floating point or integer)"
3381 :match-alternatives '(numberp))
3383 (define-widget 'float 'restricted-sexp
3384 "A floating point number."
3385 :tag "Floating point number"
3386 :value 0.0
3387 :type-error "This field should contain a floating point number"
3388 :match-alternatives '(floatp))
3390 (define-widget 'character 'editable-field
3391 "A character."
3392 :tag "Character"
3393 :value 0
3394 :size 1
3395 :format "%{%t%}: %v\n"
3396 :valid-regexp "\\`.\\'"
3397 :error "This field should contain a single character"
3398 :value-get (lambda (w) (widget-field-value-get w t))
3399 :value-to-internal (lambda (_widget value)
3400 (if (stringp value)
3401 value
3402 (char-to-string value)))
3403 :value-to-external (lambda (_widget value)
3404 (if (stringp value)
3405 (aref value 0)
3406 value))
3407 :match (lambda (_widget value)
3408 (characterp value)))
3410 (define-widget 'list 'group
3411 "A Lisp list."
3412 :tag "List"
3413 :format "%{%t%}:\n%v")
3415 (define-widget 'vector 'group
3416 "A Lisp vector."
3417 :tag "Vector"
3418 :format "%{%t%}:\n%v"
3419 :match 'widget-vector-match
3420 :value-to-internal (lambda (_widget value) (append value nil))
3421 :value-to-external (lambda (_widget value) (apply 'vector value)))
3423 (defun widget-vector-match (widget value)
3424 (and (vectorp value)
3425 (widget-group-match widget
3426 (widget-apply widget :value-to-internal value))))
3428 (define-widget 'cons 'group
3429 "A cons-cell."
3430 :tag "Cons-cell"
3431 :format "%{%t%}:\n%v"
3432 :match 'widget-cons-match
3433 :value-to-internal (lambda (_widget value)
3434 (list (car value) (cdr value)))
3435 :value-to-external (lambda (_widget value)
3436 (apply 'cons value)))
3438 (defun widget-cons-match (widget value)
3439 (and (consp value)
3440 (widget-group-match widget
3441 (widget-apply widget :value-to-internal value))))
3443 ;;; The `lazy' Widget.
3445 ;; Recursive datatypes.
3447 (define-widget 'lazy 'default
3448 "Base widget for recursive data structures.
3450 The `lazy' widget will, when instantiated, contain a single inferior
3451 widget, of the widget type specified by the :type parameter. The
3452 value of the `lazy' widget is the same as the value of the inferior
3453 widget. When deriving a new widget from the 'lazy' widget, the :type
3454 parameter is allowed to refer to the widget currently being defined,
3455 thus allowing recursive data structures to be described.
3457 The :type parameter takes the same arguments as the defcustom
3458 parameter with the same name.
3460 Most composite widgets, i.e. widgets containing other widgets, does
3461 not allow recursion. That is, when you define a new widget type, none
3462 of the inferior widgets may be of the same type you are currently
3463 defining.
3465 In Lisp, however, it is custom to define data structures in terms of
3466 themselves. A list, for example, is defined as either nil, or a cons
3467 cell whose cdr itself is a list. The obvious way to translate this
3468 into a widget type would be
3470 (define-widget 'my-list 'choice
3471 \"A list of sexps.\"
3472 :tag \"Sexp list\"
3473 :args '((const nil) (cons :value (nil) sexp my-list)))
3475 Here we attempt to define my-list as a choice of either the constant
3476 nil, or a cons-cell containing a sexp and my-lisp. This will not work
3477 because the `choice' widget does not allow recursion.
3479 Using the `lazy' widget you can overcome this problem, as in this
3480 example:
3482 (define-widget 'sexp-list 'lazy
3483 \"A list of sexps.\"
3484 :tag \"Sexp list\"
3485 :type '(choice (const nil) (cons :value (nil) sexp sexp-list)))"
3486 :format "%{%t%}: %v"
3487 ;; We don't convert :type because we want to allow recursive
3488 ;; data structures. This is slow, so we should not create speed
3489 ;; critical widgets by deriving from this.
3490 :convert-widget 'widget-value-convert-widget
3491 :value-create 'widget-type-value-create
3492 :value-get 'widget-child-value-get
3493 :value-inline 'widget-child-value-inline
3494 :default-get 'widget-type-default-get
3495 :match 'widget-type-match
3496 :validate 'widget-child-validate)
3499 ;;; The `plist' Widget.
3501 ;; Property lists.
3503 (define-widget 'plist 'list
3504 "A property list."
3505 :key-type '(symbol :tag "Key")
3506 :value-type '(sexp :tag "Value")
3507 :convert-widget 'widget-plist-convert-widget
3508 :tag "Plist")
3510 (defvar widget-plist-value-type) ;Dynamic variable
3512 (defun widget-plist-convert-widget (widget)
3513 ;; Handle `:options'.
3514 (let* ((options (widget-get widget :options))
3515 (widget-plist-value-type (widget-get widget :value-type))
3516 (other `(editable-list :inline t
3517 (group :inline t
3518 ,(widget-get widget :key-type)
3519 ,widget-plist-value-type)))
3520 (args (if options
3521 (list `(checklist :inline t
3522 :greedy t
3523 ,@(mapcar 'widget-plist-convert-option
3524 options))
3525 other)
3526 (list other))))
3527 (widget-put widget :args args)
3528 widget))
3530 (defun widget-plist-convert-option (option)
3531 ;; Convert a single plist option.
3532 (let (key-type value-type)
3533 (if (listp option)
3534 (let ((key (nth 0 option)))
3535 (setq value-type (nth 1 option))
3536 (if (listp key)
3537 (setq key-type key)
3538 (setq key-type `(const ,key))))
3539 (setq key-type `(const ,option)
3540 value-type widget-plist-value-type))
3541 `(group :format "Key: %v" :inline t ,key-type ,value-type)))
3544 ;;; The `alist' Widget.
3546 ;; Association lists.
3548 (define-widget 'alist 'list
3549 "An association list."
3550 :key-type '(sexp :tag "Key")
3551 :value-type '(sexp :tag "Value")
3552 :convert-widget 'widget-alist-convert-widget
3553 :tag "Alist")
3555 (defvar widget-alist-value-type) ;Dynamic variable
3557 (defun widget-alist-convert-widget (widget)
3558 ;; Handle `:options'.
3559 (let* ((options (widget-get widget :options))
3560 (widget-alist-value-type (widget-get widget :value-type))
3561 (other `(editable-list :inline t
3562 (cons :format "%v"
3563 ,(widget-get widget :key-type)
3564 ,widget-alist-value-type)))
3565 (args (if options
3566 (list `(checklist :inline t
3567 :greedy t
3568 ,@(mapcar 'widget-alist-convert-option
3569 options))
3570 other)
3571 (list other))))
3572 (widget-put widget :args args)
3573 widget))
3575 (defun widget-alist-convert-option (option)
3576 ;; Convert a single alist option.
3577 (let (key-type value-type)
3578 (if (listp option)
3579 (let ((key (nth 0 option)))
3580 (setq value-type (nth 1 option))
3581 (if (listp key)
3582 (setq key-type key)
3583 (setq key-type `(const ,key))))
3584 (setq key-type `(const ,option)
3585 value-type widget-alist-value-type))
3586 `(cons :format "Key: %v" ,key-type ,value-type)))
3588 (define-widget 'choice 'menu-choice
3589 "A union of several sexp types."
3590 :tag "Choice"
3591 :format "%{%t%}: %[Value Menu%] %v"
3592 :button-prefix 'widget-push-button-prefix
3593 :button-suffix 'widget-push-button-suffix
3594 :prompt-value 'widget-choice-prompt-value)
3596 (defun widget-choice-prompt-value (widget prompt value _unbound)
3597 "Make a choice."
3598 (let ((args (widget-get widget :args))
3599 (completion-ignore-case (widget-get widget :case-fold))
3600 current choices old)
3601 ;; Find the first arg that matches VALUE.
3602 (let ((look args))
3603 (while look
3604 (if (widget-apply (car look) :match value)
3605 (setq old (car look)
3606 look nil)
3607 (setq look (cdr look)))))
3608 ;; Find new choice.
3609 (setq current
3610 (cond ((= (length args) 0)
3611 nil)
3612 ((= (length args) 1)
3613 (nth 0 args))
3614 ((and (= (length args) 2)
3615 (memq old args))
3616 (if (eq old (nth 0 args))
3617 (nth 1 args)
3618 (nth 0 args)))
3620 (while args
3621 (setq current (car args)
3622 args (cdr args))
3623 (setq choices
3624 (cons (cons (widget-apply current :menu-tag-get)
3625 current)
3626 choices)))
3627 (let ((val (completing-read prompt choices nil t)))
3628 (if (stringp val)
3629 (let ((try (try-completion val choices)))
3630 (when (stringp try)
3631 (setq val try))
3632 (cdr (assoc val choices)))
3633 nil)))))
3634 (if current
3635 (widget-prompt-value current prompt nil t)
3636 value)))
3638 (define-widget 'radio 'radio-button-choice
3639 "A union of several sexp types."
3640 :tag "Choice"
3641 :format "%{%t%}:\n%v"
3642 :prompt-value 'widget-choice-prompt-value)
3644 (define-widget 'repeat 'editable-list
3645 "A variable length homogeneous list."
3646 :tag "Repeat"
3647 :format "%{%t%}:\n%v%i\n")
3649 (define-widget 'set 'checklist
3650 "A list of members from a fixed set."
3651 :tag "Set"
3652 :format "%{%t%}:\n%v")
3654 (define-widget 'boolean 'toggle
3655 "To be nil or non-nil, that is the question."
3656 :tag "Boolean"
3657 :prompt-value 'widget-boolean-prompt-value
3658 :button-prefix 'widget-push-button-prefix
3659 :button-suffix 'widget-push-button-suffix
3660 :format "%{%t%}: %[Toggle%] %v\n"
3661 :on "on (non-nil)"
3662 :off "off (nil)")
3664 (defun widget-boolean-prompt-value (_widget prompt _value _unbound)
3665 ;; Toggle a boolean.
3666 (y-or-n-p prompt))
3668 ;;; The `color' Widget.
3670 ;; Fixme: match
3671 (define-widget 'color 'editable-field
3672 "Choose a color name (with sample)."
3673 :format "%{%t%}: %v (%{sample%})\n"
3674 :value-create 'widget-color-value-create
3675 :size 10
3676 :tag "Color"
3677 :value "black"
3678 :completions (or facemenu-color-alist (defined-colors))
3679 :sample-face-get 'widget-color-sample-face-get
3680 :notify 'widget-color-notify
3681 :action 'widget-color-action)
3683 (defun widget-color-value-create (widget)
3684 (widget-field-value-create widget)
3685 (widget-insert " ")
3686 (widget-create-child-and-convert
3687 widget 'push-button
3688 :tag " Choose " :action 'widget-color--choose-action)
3689 (widget-insert " "))
3691 (defun widget-color--choose-action (widget &optional _event)
3692 (list-colors-display
3693 nil nil
3694 `(lambda (color)
3695 (when (buffer-live-p ,(current-buffer))
3696 (widget-value-set ',(widget-get widget :parent) color)
3697 (let* ((buf (get-buffer "*Colors*"))
3698 (win (get-buffer-window buf 0)))
3699 (bury-buffer buf)
3700 (and win (> (length (window-list)) 1)
3701 (delete-window win)))
3702 (pop-to-buffer ,(current-buffer))))))
3704 (defun widget-color-sample-face-get (widget)
3705 (let* ((value (condition-case nil
3706 (widget-value widget)
3707 (error (widget-get widget :value)))))
3708 (if (color-defined-p value)
3709 (list (cons 'foreground-color value))
3710 'default)))
3712 (defun widget-color-action (widget &optional event)
3713 "Prompt for a color."
3714 (let* ((tag (widget-apply widget :menu-tag-get))
3715 (prompt (concat tag ": "))
3716 (answer (facemenu-read-color prompt)))
3717 (unless (zerop (length answer))
3718 (widget-value-set widget answer)
3719 (widget-setup)
3720 (widget-apply widget :notify widget event))))
3722 (defun widget-color-notify (widget child &optional event)
3723 "Update the sample, and notify the parent."
3724 (overlay-put (widget-get widget :sample-overlay)
3725 'face (widget-apply widget :sample-face-get))
3726 (widget-default-notify widget child event))
3728 ;;; The Help Echo
3730 (defun widget-echo-help (pos)
3731 "Display help-echo text for widget at POS."
3732 (let* ((widget (widget-at pos))
3733 (help-echo (and widget (widget-get widget :help-echo))))
3734 (if (functionp help-echo)
3735 (setq help-echo (funcall help-echo widget)))
3736 (if help-echo (message "%s" (eval help-echo)))))
3738 ;;; The End:
3740 (provide 'wid-edit)
3742 ;;; wid-edit.el ends here