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