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