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