* cus-edit.el (Custom-move-and-invoke): Delete function.
[emacs.git] / lisp / wid-edit.el
blobce186d1b68f75705a1550d3afaa6e6804e8f81c6
1 ;;; wid-edit.el --- Functions for creating and using widgets -*-byte-compile-dynamic: t;-*-
2 ;;
3 ;; Copyright (C) 1996, 1997, 1999, 2000, 2001, 2002, 2003,
4 ;; 2004, 2005 Free Software Foundation, Inc.
5 ;;
6 ;; Author: Per Abrahamsen <abraham@dina.kvl.dk>
7 ;; Maintainer: FSF
8 ;; Keywords: extensions
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 2, or (at your option)
15 ;; 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; see the file COPYING. If not, write to the
24 ;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
25 ;; Boston, MA 02110-1301, USA.
27 ;;; Wishlist items (from widget.texi):
29 ;; * The `menu-choice' tag should be prettier, something like the
30 ;; abbreviated menus in Open Look.
32 ;; * Finish `:tab-order'.
34 ;; * Make indentation work with glyphs and proportional fonts.
36 ;; * Add commands to show overview of object and class hierarchies to
37 ;; the browser.
39 ;; * Find a way to disable mouse highlight for inactive widgets.
41 ;; * Find a way to make glyphs look inactive.
43 ;; * Add `key-binding' widget.
45 ;; * Add `widget' widget for editing widget specifications.
47 ;; * Find clean way to implement variable length list. See
48 ;; `TeX-printer-list' for an explanation.
50 ;; * `C-h' in `widget-prompt-value' should give type specific help.
52 ;; * A mailto widget. [This should work OK as a url-link if with
53 ;; browse-url-browser-function' set up appropriately.]
55 ;;; Commentary:
57 ;; See `widget.el'.
59 ;;; Code:
61 (defvar widget)
63 ;;; Compatibility.
65 (defun widget-event-point (event)
66 "Character position of the end of event if that exists, or nil."
67 (posn-point (event-end event)))
69 (defun widget-button-release-event-p (event)
70 "Non-nil if EVENT is a mouse-button-release event object."
71 (and (eventp event)
72 (memq (event-basic-type event) '(mouse-1 mouse-2 mouse-3))
73 (or (memq 'click (event-modifiers event))
74 (memq 'drag (event-modifiers event)))))
76 ;;; Customization.
78 (defgroup widgets nil
79 "Customization support for the Widget Library."
80 :link '(custom-manual "(widget)Top")
81 :link '(emacs-library-link :tag "Lisp File" "widget.el")
82 :prefix "widget-"
83 :group 'extensions
84 :group 'hypermedia)
86 (defgroup widget-documentation nil
87 "Options controling the display of documentation strings."
88 :group 'widgets)
90 (defgroup widget-faces nil
91 "Faces used by the widget library."
92 :group 'widgets
93 :group 'faces)
95 (defvar widget-documentation-face 'widget-documentation
96 "Face used for documentation strings in widgets.
97 This exists as a variable so it can be set locally in certain buffers.")
99 (defface widget-documentation '((((class color)
100 (background dark))
101 (:foreground "lime green"))
102 (((class color)
103 (background light))
104 (:foreground "dark green"))
105 (t nil))
106 "Face used for documentation text."
107 :group 'widget-documentation
108 :group 'widget-faces)
109 ;; backward compatibility alias
110 (put 'widget-documentation-face 'face-alias 'widget-documentation)
112 (defvar widget-button-face 'widget-button
113 "Face used for buttons in widgets.
114 This exists as a variable so it can be set locally in certain buffers.")
116 (defface widget-button '((t (:weight bold)))
117 "Face used for widget buttons."
118 :group 'widget-faces)
119 ;; backward compatibility alias
120 (put 'widget-button-face 'face-alias 'widget-button)
122 (defcustom widget-mouse-face 'highlight
123 "Face used for widget buttons when the mouse is above them."
124 :type 'face
125 :group 'widget-faces)
127 ;; TTY gets special definitions here and in the next defface, because
128 ;; the gray colors defined for other displays cause black text on a black
129 ;; background, at least on light-background TTYs.
130 (defface widget-field '((((type tty))
131 :background "yellow3"
132 :foreground "black")
133 (((class grayscale color)
134 (background light))
135 :background "gray85")
136 (((class grayscale color)
137 (background dark))
138 :background "dim gray")
140 :slant italic))
141 "Face used for editable fields."
142 :group 'widget-faces)
143 ;; backward-compatibility alias
144 (put 'widget-field-face 'face-alias 'widget-field)
146 (defface widget-single-line-field '((((type tty))
147 :background "green3"
148 :foreground "black")
149 (((class grayscale color)
150 (background light))
151 :background "gray85")
152 (((class grayscale color)
153 (background dark))
154 :background "dim gray")
156 :slant italic))
157 "Face used for editable fields spanning only a single line."
158 :group 'widget-faces)
159 ;; backward-compatibility alias
160 (put 'widget-single-line-field-face 'face-alias 'widget-single-line-field)
162 ;;; This causes display-table to be loaded, and not usefully.
163 ;;;(defvar widget-single-line-display-table
164 ;;; (let ((table (make-display-table)))
165 ;;; (aset table 9 "^I")
166 ;;; (aset table 10 "^J")
167 ;;; table)
168 ;;; "Display table used for single-line editable fields.")
170 ;;;(when (fboundp 'set-face-display-table)
171 ;;; (set-face-display-table 'widget-single-line-field-face
172 ;;; widget-single-line-display-table))
174 ;;; Utility functions.
176 ;; These are not really widget specific.
178 (defun widget-princ-to-string (object)
179 "Return string representation of OBJECT, any Lisp object.
180 No quoting characters are used; no delimiters are printed around
181 the contents of strings."
182 (with-output-to-string
183 (princ object)))
185 (defun widget-clear-undo ()
186 "Clear all undo information."
187 (buffer-disable-undo (current-buffer))
188 (buffer-enable-undo))
190 (defcustom widget-menu-max-size 40
191 "Largest number of items allowed in a popup-menu.
192 Larger menus are read through the minibuffer."
193 :group 'widgets
194 :type 'integer)
196 (defcustom widget-menu-max-shortcuts 40
197 "Largest number of items for which it works to choose one with a character.
198 For a larger number of items, the minibuffer is used."
199 :group 'widgets
200 :type 'integer)
202 (defcustom widget-menu-minibuffer-flag nil
203 "*Control how to ask for a choice from the keyboard.
204 Non-nil means use the minibuffer;
205 nil means read a single character."
206 :group 'widgets
207 :type 'boolean)
209 (defun widget-choose (title items &optional event)
210 "Choose an item from a list.
212 First argument TITLE is the name of the list.
213 Second argument ITEMS is a list whose members are either
214 (NAME . VALUE), to indicate selectable items, or just strings to
215 indicate unselectable items.
216 Optional third argument EVENT is an input event.
218 The user is asked to choose between each NAME from the items alist,
219 and the VALUE of the chosen element will be returned. If EVENT is a
220 mouse event, and the number of elements in items is less than
221 `widget-menu-max-size', a popup menu will be used, otherwise the
222 minibuffer."
223 (cond ((and (< (length items) widget-menu-max-size)
224 event (display-popup-menus-p))
225 ;; Mouse click.
226 (x-popup-menu event
227 (list title (cons "" items))))
228 ((or widget-menu-minibuffer-flag
229 (> (length items) widget-menu-max-shortcuts))
230 ;; Read the choice of name from the minibuffer.
231 (setq items (widget-remove-if 'stringp items))
232 (let ((val (completing-read (concat title ": ") items nil t)))
233 (if (stringp val)
234 (let ((try (try-completion val items)))
235 (when (stringp try)
236 (setq val try))
237 (cdr (assoc val items))))))
239 ;; Construct a menu of the choices
240 ;; and then use it for prompting for a single character.
241 (let* ((overriding-terminal-local-map (make-sparse-keymap))
242 (next-digit ?0)
243 map choice some-choice-enabled value)
244 ;; Define SPC as a prefix char to get to this menu.
245 (define-key overriding-terminal-local-map " "
246 (setq map (make-sparse-keymap title)))
247 (with-current-buffer (get-buffer-create " widget-choose")
248 (erase-buffer)
249 (insert "Available choices:\n\n")
250 (while items
251 (setq choice (car items) items (cdr items))
252 (if (consp choice)
253 (let* ((name (car choice))
254 (function (cdr choice)))
255 (insert (format "%c = %s\n" next-digit name))
256 (define-key map (vector next-digit) function)
257 (setq some-choice-enabled t)))
258 ;; Allocate digits to disabled alternatives
259 ;; so that the digit of a given alternative never varies.
260 (setq next-digit (1+ next-digit)))
261 (insert "\nC-g = Quit"))
262 (or some-choice-enabled
263 (error "None of the choices is currently meaningful"))
264 (define-key map [?\C-g] 'keyboard-quit)
265 (define-key map [t] 'keyboard-quit)
266 (define-key map [?\M-\C-v] 'scroll-other-window)
267 (define-key map [?\M--] 'negative-argument)
268 (setcdr map (nreverse (cdr map)))
269 ;; Read a char with the menu, and return the result
270 ;; that corresponds to it.
271 (save-window-excursion
272 (let ((buf (get-buffer " widget-choose")))
273 (fit-window-to-buffer (display-buffer buf))
274 (let ((cursor-in-echo-area t)
275 keys
276 (char 0)
277 (arg 1))
278 (while (not (or (and (>= char ?0) (< char next-digit))
279 (eq value 'keyboard-quit)))
280 ;; Unread a SPC to lead to our new menu.
281 (setq unread-command-events (cons ?\s unread-command-events))
282 (setq keys (read-key-sequence title))
283 (setq value
284 (lookup-key overriding-terminal-local-map keys t)
285 char (string-to-char (substring keys 1)))
286 (cond ((eq value 'scroll-other-window)
287 (let ((minibuffer-scroll-window
288 (get-buffer-window buf)))
289 (if (> 0 arg)
290 (scroll-other-window-down
291 (window-height minibuffer-scroll-window))
292 (scroll-other-window))
293 (setq arg 1)))
294 ((eq value 'negative-argument)
295 (setq arg -1))
297 (setq arg 1)))))))
298 (when (eq value 'keyboard-quit)
299 (error "Canceled"))
300 value))))
302 (defun widget-remove-if (predictate list)
303 (let (result (tail list))
304 (while tail
305 (or (funcall predictate (car tail))
306 (setq result (cons (car tail) result)))
307 (setq tail (cdr tail)))
308 (nreverse result)))
310 ;;; Widget text specifications.
312 ;; These functions are for specifying text properties.
314 ;; We can set it to nil now that get_local_map uses get_pos_property.
315 (defconst widget-field-add-space nil
316 "Non-nil means add extra space at the end of editable text fields.
317 If you don't add the space, it will become impossible to edit a zero
318 size field.")
320 (defvar widget-field-use-before-change t
321 "Non-nil means use `before-change-functions' to track editable fields.
322 This enables the use of undo, but doesn't work on Emacs 19.34 and earlier.
323 Using before hooks also means that the :notify function can't know the
324 new value.")
326 (defun widget-specify-field (widget from to)
327 "Specify editable button for WIDGET between FROM and TO."
328 ;; Terminating space is not part of the field, but necessary in
329 ;; order for local-map to work. Remove next sexp if local-map works
330 ;; at the end of the overlay.
331 (save-excursion
332 (goto-char to)
333 (cond ((null (widget-get widget :size))
334 (forward-char 1))
335 (widget-field-add-space
336 (insert-and-inherit " ")))
337 (setq to (point)))
338 (let ((keymap (widget-get widget :keymap))
339 (face (or (widget-get widget :value-face) 'widget-field))
340 (help-echo (widget-get widget :help-echo))
341 (follow-link (widget-get widget :follow-link))
342 (rear-sticky
343 (or (not widget-field-add-space) (widget-get widget :size))))
344 (if (functionp help-echo)
345 (setq help-echo 'widget-mouse-help))
346 (when (= (char-before to) ?\n)
347 ;; When the last character in the field is a newline, we want to
348 ;; give it a `field' char-property of `boundary', which helps the
349 ;; C-n/C-p act more naturally when entering/leaving the field. We
350 ;; do this by making a small secondary overlay to contain just that
351 ;; one character.
352 (let ((overlay (make-overlay (1- to) to nil t nil)))
353 (overlay-put overlay 'field 'boundary)
354 ;; We need the real field for tabbing.
355 (overlay-put overlay 'real-field widget)
356 ;; Use `local-map' here, not `keymap', so that normal editing
357 ;; works in the field when, say, Custom uses `suppress-keymap'.
358 (overlay-put overlay 'local-map keymap)
359 (overlay-put overlay 'face face)
360 (overlay-put overlay 'follow-link follow-link)
361 (overlay-put overlay 'help-echo help-echo))
362 (setq to (1- to))
363 (setq rear-sticky t))
364 (let ((overlay (make-overlay from to nil nil rear-sticky)))
365 (widget-put widget :field-overlay overlay)
366 ;;(overlay-put overlay 'detachable nil)
367 (overlay-put overlay 'field widget)
368 (overlay-put overlay 'local-map keymap)
369 (overlay-put overlay 'face face)
370 (overlay-put overlay 'follow-link follow-link)
371 (overlay-put overlay 'help-echo help-echo)))
372 (widget-specify-secret widget))
374 (defun widget-specify-secret (field)
375 "Replace text in FIELD with value of `:secret', if non-nil."
376 (let ((secret (widget-get field :secret))
377 (size (widget-get field :size)))
378 (when secret
379 (let ((begin (widget-field-start field))
380 (end (widget-field-end field)))
381 (when size
382 (while (and (> end begin)
383 (eq (char-after (1- end)) ?\s))
384 (setq end (1- end))))
385 (while (< begin end)
386 (let ((old (char-after begin)))
387 (unless (eq old secret)
388 (subst-char-in-region begin (1+ begin) old secret)
389 (put-text-property begin (1+ begin) 'secret old))
390 (setq begin (1+ begin))))))))
392 (defun widget-specify-button (widget from to)
393 "Specify button for WIDGET between FROM and TO."
394 (let ((overlay (make-overlay from to nil t nil))
395 (follow-link (widget-get widget :follow-link))
396 (help-echo (widget-get widget :help-echo)))
397 (widget-put widget :button-overlay overlay)
398 (if (functionp help-echo)
399 (setq help-echo 'widget-mouse-help))
400 (overlay-put overlay 'button widget)
401 (overlay-put overlay 'keymap (widget-get widget :keymap))
402 (overlay-put overlay 'evaporate t)
403 ;; We want to avoid the face with image buttons.
404 (unless (widget-get widget :suppress-face)
405 (overlay-put overlay 'face (widget-apply widget :button-face-get))
406 ; Text terminals cannot change mouse pointer shape, so use mouse
407 ; face instead.
408 (or (display-graphic-p)
409 (overlay-put overlay 'mouse-face widget-mouse-face)))
410 (overlay-put overlay 'pointer 'hand)
411 (overlay-put overlay 'follow-link follow-link)
412 (overlay-put overlay 'help-echo help-echo)))
414 (defun widget-mouse-help (window overlay point)
415 "Help-echo callback for widgets whose :help-echo is a function."
416 (with-current-buffer (overlay-buffer overlay)
417 (let* ((widget (widget-at (overlay-start overlay)))
418 (help-echo (if widget (widget-get widget :help-echo))))
419 (if (functionp help-echo)
420 (funcall help-echo widget)
421 help-echo))))
423 (defun widget-specify-sample (widget from to)
424 "Specify sample for WIDGET between FROM and TO."
425 (let ((overlay (make-overlay from to nil t nil)))
426 (overlay-put overlay 'face (widget-apply widget :sample-face-get))
427 (overlay-put overlay 'evaporate t)
428 (widget-put widget :sample-overlay overlay)))
430 (defun widget-specify-doc (widget from to)
431 "Specify documentation for WIDGET between FROM and TO."
432 (let ((overlay (make-overlay from to nil t nil)))
433 (overlay-put overlay 'widget-doc widget)
434 (overlay-put overlay 'face widget-documentation-face)
435 (overlay-put overlay 'evaporate t)
436 (widget-put widget :doc-overlay overlay)))
438 (defmacro widget-specify-insert (&rest form)
439 "Execute FORM without inheriting any text properties."
440 `(save-restriction
441 (let ((inhibit-read-only t)
442 (inhibit-modification-hooks t))
443 (narrow-to-region (point) (point))
444 (prog1 (progn ,@form)
445 (goto-char (point-max))))))
447 (defface widget-inactive
448 '((t :inherit shadow))
449 "Face used for inactive widgets."
450 :group 'widget-faces)
451 ;; backward-compatibility alias
452 (put 'widget-inactive-face 'face-alias 'widget-inactive)
454 (defun widget-specify-inactive (widget from to)
455 "Make WIDGET inactive for user modifications."
456 (unless (widget-get widget :inactive)
457 (let ((overlay (make-overlay from to nil t nil)))
458 (overlay-put overlay 'face 'widget-inactive)
459 ;; This is disabled, as it makes the mouse cursor change shape.
460 ;; (overlay-put overlay 'mouse-face 'widget-inactive)
461 (overlay-put overlay 'evaporate t)
462 (overlay-put overlay 'priority 100)
463 (overlay-put overlay 'modification-hooks '(widget-overlay-inactive))
464 (widget-put widget :inactive overlay))))
466 (defun widget-overlay-inactive (&rest junk)
467 "Ignoring the arguments, signal an error."
468 (unless inhibit-read-only
469 (error "The widget here is not active")))
472 (defun widget-specify-active (widget)
473 "Make WIDGET active for user modifications."
474 (let ((inactive (widget-get widget :inactive)))
475 (when inactive
476 (delete-overlay inactive)
477 (widget-put widget :inactive nil))))
479 ;;; Widget Properties.
481 (defsubst widget-type (widget)
482 "Return the type of WIDGET, a symbol."
483 (car widget))
485 ;;;###autoload
486 (defun widgetp (widget)
487 "Return non-nil iff WIDGET is a widget."
488 (if (symbolp widget)
489 (get widget 'widget-type)
490 (and (consp widget)
491 (symbolp (car widget))
492 (get (car widget) 'widget-type))))
494 (defun widget-get-indirect (widget property)
495 "In WIDGET, get the value of PROPERTY.
496 If the value is a symbol, return its binding.
497 Otherwise, just return the value."
498 (let ((value (widget-get widget property)))
499 (if (symbolp value)
500 (symbol-value value)
501 value)))
503 (defun widget-member (widget property)
504 "Non-nil iff there is a definition in WIDGET for PROPERTY."
505 (cond ((plist-member (cdr widget) property)
507 ((car widget)
508 (widget-member (get (car widget) 'widget-type) property))
509 (t nil)))
511 (defun widget-value (widget)
512 "Extract the current value of WIDGET."
513 (widget-apply widget
514 :value-to-external (widget-apply widget :value-get)))
516 (defun widget-value-set (widget value)
517 "Set the current value of WIDGET to VALUE."
518 (widget-apply widget
519 :value-set (widget-apply widget
520 :value-to-internal value)))
522 (defun widget-default-get (widget)
523 "Extract the default external value of WIDGET."
524 (widget-apply widget :value-to-external
525 (or (widget-get widget :value)
526 (widget-apply widget :default-get))))
528 (defun widget-match-inline (widget vals)
529 "In WIDGET, match the start of VALS."
530 (cond ((widget-get widget :inline)
531 (widget-apply widget :match-inline vals))
532 ((and (listp vals)
533 (widget-apply widget :match (car vals)))
534 (cons (list (car vals)) (cdr vals)))
535 (t nil)))
537 (defun widget-apply-action (widget &optional event)
538 "Apply :action in WIDGET in response to EVENT."
539 (if (widget-apply widget :active)
540 (widget-apply widget :action event)
541 (error "Attempt to perform action on inactive widget")))
543 ;;; Helper functions.
545 ;; These are widget specific.
547 ;;;###autoload
548 (defun widget-prompt-value (widget prompt &optional value unbound)
549 "Prompt for a value matching WIDGET, using PROMPT.
550 The current value is assumed to be VALUE, unless UNBOUND is non-nil."
551 (unless (listp widget)
552 (setq widget (list widget)))
553 (setq prompt (format "[%s] %s" (widget-type widget) prompt))
554 (setq widget (widget-convert widget))
555 (let ((answer (widget-apply widget :prompt-value prompt value unbound)))
556 (unless (widget-apply widget :match answer)
557 (error "Value does not match %S type" (car widget)))
558 answer))
560 (defun widget-get-sibling (widget)
561 "Get the item WIDGET is assumed to toggle.
562 This is only meaningful for radio buttons or checkboxes in a list."
563 (let* ((children (widget-get (widget-get widget :parent) :children))
564 child)
565 (catch 'child
566 (while children
567 (setq child (car children)
568 children (cdr children))
569 (when (eq (widget-get child :button) widget)
570 (throw 'child child)))
571 nil)))
573 (defun widget-map-buttons (function &optional buffer maparg)
574 "Map FUNCTION over the buttons in BUFFER.
575 FUNCTION is called with the arguments WIDGET and MAPARG.
577 If FUNCTION returns non-nil, the walk is cancelled.
579 The arguments MAPARG, and BUFFER default to nil and (current-buffer),
580 respectively."
581 (let ((cur (point-min))
582 (widget nil)
583 (overlays (if buffer
584 (with-current-buffer buffer (overlay-lists))
585 (overlay-lists))))
586 (setq overlays (append (car overlays) (cdr overlays)))
587 (while (setq cur (pop overlays))
588 (setq widget (overlay-get cur 'button))
589 (if (and widget (funcall function widget maparg))
590 (setq overlays nil)))))
592 ;;; Images.
594 (defcustom widget-image-directory (file-name-as-directory
595 (expand-file-name "custom" data-directory))
596 "Where widget button images are located.
597 If this variable is nil, widget will try to locate the directory
598 automatically."
599 :group 'widgets
600 :type 'directory)
602 (defcustom widget-image-enable t
603 "If non nil, use image buttons in widgets when available."
604 :version "21.1"
605 :group 'widgets
606 :type 'boolean)
608 (defcustom widget-image-conversion
609 '((xpm ".xpm") (gif ".gif") (png ".png") (jpeg ".jpg" ".jpeg")
610 (xbm ".xbm"))
611 "Conversion alist from image formats to file name suffixes."
612 :group 'widgets
613 :type '(repeat (cons :format "%v"
614 (symbol :tag "Image Format" unknown)
615 (repeat :tag "Suffixes"
616 (string :format "%v")))))
618 (defun widget-image-find (image)
619 "Create a graphical button from IMAGE.
620 IMAGE should either already be an image, or be a file name sans
621 extension (xpm, xbm, gif, jpg, or png) located in
622 `widget-image-directory' or otherwise where `find-image' will find it."
623 (cond ((not (and image widget-image-enable (display-graphic-p)))
624 ;; We don't want or can't use images.
625 nil)
626 ((and (consp image)
627 (eq 'image (car image)))
628 ;; Already an image spec. Use it.
629 image)
630 ((stringp image)
631 ;; A string. Look it up in relevant directories.
632 (let* ((load-path (cons widget-image-directory load-path))
633 specs)
634 (dolist (elt widget-image-conversion)
635 (dolist (ext (cdr elt))
636 (push (list :type (car elt) :file (concat image ext)) specs)))
637 (setq specs (nreverse specs))
638 (find-image specs)))
640 ;; Oh well.
641 nil)))
643 (defvar widget-button-pressed-face 'widget-button-pressed
644 "Face used for pressed buttons in widgets.
645 This exists as a variable so it can be set locally in certain
646 buffers.")
648 (defun widget-image-insert (widget tag image &optional down inactive)
649 "In WIDGET, insert the text TAG or, if supported, IMAGE.
650 IMAGE should either be an image or an image file name sans extension
651 \(xpm, xbm, gif, jpg, or png) located in `widget-image-directory'.
653 Optional arguments DOWN and INACTIVE are used instead of IMAGE when the
654 button is pressed or inactive, respectively. These are currently ignored."
655 (if (and (display-graphic-p)
656 (setq image (widget-image-find image)))
657 (progn (widget-put widget :suppress-face t)
658 (insert-image image
659 (propertize
660 tag 'mouse-face widget-button-pressed-face)))
661 (insert tag)))
663 ;;; Buttons.
665 (defgroup widget-button nil
666 "The look of various kinds of buttons."
667 :group 'widgets)
669 (defcustom widget-button-prefix ""
670 "String used as prefix for buttons."
671 :type 'string
672 :group 'widget-button)
674 (defcustom widget-button-suffix ""
675 "String used as suffix for buttons."
676 :type 'string
677 :group 'widget-button)
679 ;;; Creating Widgets.
681 ;;;###autoload
682 (defun widget-create (type &rest args)
683 "Create widget of TYPE.
684 The optional ARGS are additional keyword arguments."
685 (let ((widget (apply 'widget-convert type args)))
686 (widget-apply widget :create)
687 widget))
689 (defun widget-create-child-and-convert (parent type &rest args)
690 "As part of the widget PARENT, create a child widget TYPE.
691 The child is converted, using the keyword arguments ARGS."
692 (let ((widget (apply 'widget-convert type args)))
693 (widget-put widget :parent parent)
694 (unless (widget-get widget :indent)
695 (widget-put widget :indent (+ (or (widget-get parent :indent) 0)
696 (or (widget-get widget :extra-offset) 0)
697 (widget-get parent :offset))))
698 (widget-apply widget :create)
699 widget))
701 (defun widget-create-child (parent type)
702 "Create widget of TYPE."
703 (let ((widget (widget-copy type)))
704 (widget-put widget :parent parent)
705 (unless (widget-get widget :indent)
706 (widget-put widget :indent (+ (or (widget-get parent :indent) 0)
707 (or (widget-get widget :extra-offset) 0)
708 (widget-get parent :offset))))
709 (widget-apply widget :create)
710 widget))
712 (defun widget-create-child-value (parent type value)
713 "Create widget of TYPE with value VALUE."
714 (let ((widget (widget-copy type)))
715 (widget-put widget :value (widget-apply widget :value-to-internal value))
716 (widget-put widget :parent parent)
717 (unless (widget-get widget :indent)
718 (widget-put widget :indent (+ (or (widget-get parent :indent) 0)
719 (or (widget-get widget :extra-offset) 0)
720 (widget-get parent :offset))))
721 (widget-apply widget :create)
722 widget))
724 ;;;###autoload
725 (defun widget-delete (widget)
726 "Delete WIDGET."
727 (widget-apply widget :delete))
729 (defun widget-copy (widget)
730 "Make a deep copy of WIDGET."
731 (widget-apply (copy-sequence widget) :copy))
733 (defun widget-convert (type &rest args)
734 "Convert TYPE to a widget without inserting it in the buffer.
735 The optional ARGS are additional keyword arguments."
736 ;; Don't touch the type.
737 (let* ((widget (if (symbolp type)
738 (list type)
739 (copy-sequence type)))
740 (current widget)
741 done
742 (keys args))
743 ;; First set the :args keyword.
744 (while (cdr current) ;Look in the type.
745 (if (and (keywordp (cadr current))
746 ;; If the last element is a keyword,
747 ;; it is still the :args element,
748 ;; even though it is a keyword.
749 (cddr current))
750 (if (eq (cadr current) :args)
751 ;; If :args is explicitly specified, obey it.
752 (setq current nil)
753 ;; Some other irrelevant keyword.
754 (setq current (cdr (cdr current))))
755 (setcdr current (list :args (cdr current)))
756 (setq current nil)))
757 (while (and args (not done)) ;Look in ARGS.
758 (cond ((eq (car args) :args)
759 ;; Handle explicit specification of :args.
760 (setq args (cadr args)
761 done t))
762 ((keywordp (car args))
763 (setq args (cddr args)))
764 (t (setq done t))))
765 (when done
766 (widget-put widget :args args))
767 ;; Then Convert the widget.
768 (setq type widget)
769 (while type
770 (let ((convert-widget (plist-get (cdr type) :convert-widget)))
771 (if convert-widget
772 (setq widget (funcall convert-widget widget))))
773 (setq type (get (car type) 'widget-type)))
774 ;; Finally set the keyword args.
775 (while keys
776 (let ((next (nth 0 keys)))
777 (if (keywordp next)
778 (progn
779 (widget-put widget next (nth 1 keys))
780 (setq keys (nthcdr 2 keys)))
781 (setq keys nil))))
782 ;; Convert the :value to internal format.
783 (if (widget-member widget :value)
784 (widget-put widget
785 :value (widget-apply widget
786 :value-to-internal
787 (widget-get widget :value))))
788 ;; Return the newly create widget.
789 widget))
791 ;;;###autoload
792 (defun widget-insert (&rest args)
793 "Call `insert' with ARGS even if surrounding text is read only."
794 (let ((inhibit-read-only t)
795 (inhibit-modification-hooks t))
796 (apply 'insert args)))
798 (defun widget-convert-text (type from to
799 &optional button-from button-to
800 &rest args)
801 "Return a widget of type TYPE with endpoint FROM TO.
802 No text will be inserted to the buffer, instead the text between FROM
803 and TO will be used as the widgets end points. If optional arguments
804 BUTTON-FROM and BUTTON-TO are given, these will be used as the widgets
805 button end points.
806 Optional ARGS are extra keyword arguments for TYPE."
807 (let ((widget (apply 'widget-convert type :delete 'widget-leave-text args))
808 (from (copy-marker from))
809 (to (copy-marker to)))
810 (set-marker-insertion-type from t)
811 (set-marker-insertion-type to nil)
812 (widget-put widget :from from)
813 (widget-put widget :to to)
814 (when button-from
815 (widget-specify-button widget button-from button-to))
816 widget))
818 (defun widget-convert-button (type from to &rest args)
819 "Return a widget of type TYPE with endpoint FROM TO.
820 Optional ARGS are extra keyword arguments for TYPE.
821 No text will be inserted to the buffer, instead the text between FROM
822 and TO will be used as the widgets end points, as well as the widgets
823 button end points."
824 (apply 'widget-convert-text type from to from to args))
826 (defun widget-leave-text (widget)
827 "Remove markers and overlays from WIDGET and its children."
828 (let ((button (widget-get widget :button-overlay))
829 (sample (widget-get widget :sample-overlay))
830 (doc (widget-get widget :doc-overlay))
831 (field (widget-get widget :field-overlay)))
832 (set-marker (widget-get widget :from) nil)
833 (set-marker (widget-get widget :to) nil)
834 (when button
835 (delete-overlay button))
836 (when sample
837 (delete-overlay sample))
838 (when doc
839 (delete-overlay doc))
840 (when field
841 (delete-overlay field))
842 (mapc 'widget-leave-text (widget-get widget :children))))
844 ;;; Keymap and Commands.
846 ;;;###autoload
847 (defvar widget-keymap
848 (let ((map (make-sparse-keymap)))
849 (define-key map "\t" 'widget-forward)
850 (define-key map [(shift tab)] 'widget-backward)
851 (define-key map [backtab] 'widget-backward)
852 (define-key map [mouse-1] 'widget-move-and-invoke)
853 (define-key map [down-mouse-2] 'widget-button-click)
854 (define-key map "\C-m" 'widget-button-press)
855 map)
856 "Keymap containing useful binding for buffers containing widgets.
857 Recommended as a parent keymap for modes using widgets.")
859 (defvar widget-global-map global-map
860 "Keymap used for events a widget does not handle itself.")
861 (make-variable-buffer-local 'widget-global-map)
863 (defvar widget-field-keymap
864 (let ((map (copy-keymap widget-keymap)))
865 (define-key map "\C-k" 'widget-kill-line)
866 (define-key map "\M-\t" 'widget-complete)
867 (define-key map "\C-m" 'widget-field-activate)
868 ;; Since the widget code uses a `field' property to identify fields,
869 ;; ordinary beginning-of-line does the right thing.
870 ;; (define-key map "\C-a" 'widget-beginning-of-line)
871 (define-key map "\C-e" 'widget-end-of-line)
872 map)
873 "Keymap used inside an editable field.")
875 (defvar widget-text-keymap
876 (let ((map (copy-keymap widget-keymap)))
877 ;; Since the widget code uses a `field' property to identify fields,
878 ;; ordinary beginning-of-line does the right thing.
879 ;; (define-key map "\C-a" 'widget-beginning-of-line)
880 (define-key map "\C-e" 'widget-end-of-line)
881 map)
882 "Keymap used inside a text field.")
884 (defun widget-field-activate (pos &optional event)
885 "Invoke the editable field at point."
886 (interactive "@d")
887 (let ((field (widget-field-at pos)))
888 (if field
889 (widget-apply-action field event)
890 (call-interactively
891 (lookup-key widget-global-map (this-command-keys))))))
893 (defface widget-button-pressed
894 '((((min-colors 88) (class color))
895 (:foreground "red1"))
896 (((class color))
897 (:foreground "red"))
899 (:weight bold :underline t)))
900 "Face used for pressed buttons."
901 :group 'widget-faces)
902 ;; backward-compatibility alias
903 (put 'widget-button-pressed-face 'face-alias 'widget-button-pressed)
905 (defun widget-move-and-invoke (event)
906 "Move to where you click, and if it is an active field, invoke it."
907 (interactive "e")
908 (mouse-set-point event)
909 (let ((pos (widget-event-point event)))
910 (if (and pos (get-char-property pos 'button))
911 (widget-button-click event))))
913 (defun widget-button-click (event)
914 "Invoke the button that the mouse is pointing at."
915 (interactive "e")
916 (if (widget-event-point event)
917 (let* ((pos (widget-event-point event))
918 (start (event-start event))
919 (button (get-char-property
920 pos 'button (and (windowp (posn-window start))
921 (window-buffer (posn-window start))))))
922 (if button
923 ;; Mouse click on a widget button. Do the following
924 ;; in a save-excursion so that the click on the button
925 ;; doesn't change point.
926 (save-selected-window
927 (select-window (posn-window (event-start event)))
928 (save-excursion
929 (goto-char (posn-point (event-start event)))
930 (let* ((overlay (widget-get button :button-overlay))
931 (face (overlay-get overlay 'face))
932 (mouse-face (overlay-get overlay 'mouse-face)))
933 (unwind-protect
934 ;; Read events, including mouse-movement events
935 ;; until we receive a release event. Highlight/
936 ;; unhighlight the button the mouse was initially
937 ;; on when we move over it.
938 (save-excursion
939 (when face ; avoid changing around image
940 (overlay-put overlay
941 'face widget-button-pressed-face)
942 (overlay-put overlay
943 'mouse-face widget-button-pressed-face))
944 (unless (widget-apply button :mouse-down-action event)
945 (let ((track-mouse t))
946 (while (not (widget-button-release-event-p event))
947 (setq event (read-event)
948 pos (widget-event-point event))
949 (if (and pos
950 (eq (get-char-property pos 'button)
951 button))
952 (when face
953 (overlay-put overlay
954 'face
955 widget-button-pressed-face)
956 (overlay-put overlay
957 'mouse-face
958 widget-button-pressed-face))
959 (overlay-put overlay 'face face)
960 (overlay-put overlay 'mouse-face mouse-face)))))
962 ;; When mouse is released over the button, run
963 ;; its action function.
964 (when (and pos
965 (eq (get-char-property pos 'button) button))
966 (widget-apply-action button event)))
967 (overlay-put overlay 'face face)
968 (overlay-put overlay 'mouse-face mouse-face))))
970 (unless (pos-visible-in-window-p (widget-event-point event))
971 (mouse-set-point event)
972 (beginning-of-line)
973 (recenter))
976 (let ((up t) command)
977 ;; Mouse click not on a widget button. Find the global
978 ;; command to run, and check whether it is bound to an
979 ;; up event.
980 (mouse-set-point event)
981 (if (memq (event-basic-type event) '(mouse-1 down-mouse-1))
982 (cond ((setq command ;down event
983 (lookup-key widget-global-map [down-mouse-1]))
984 (setq up nil))
985 ((setq command ;up event
986 (lookup-key widget-global-map [mouse-1]))))
987 (cond ((setq command ;down event
988 (lookup-key widget-global-map [down-mouse-2]))
989 (setq up nil))
990 ((setq command ;up event
991 (lookup-key widget-global-map [mouse-2])))))
992 (when up
993 ;; Don't execute up events twice.
994 (while (not (widget-button-release-event-p event))
995 (setq event (read-event))))
996 (when command
997 (call-interactively command)))))
998 (message "You clicked somewhere weird.")))
1000 (defun widget-button-press (pos &optional event)
1001 "Invoke button at POS."
1002 (interactive "@d")
1003 (let ((button (get-char-property pos 'button)))
1004 (if button
1005 (widget-apply-action button event)
1006 (let ((command (lookup-key widget-global-map (this-command-keys))))
1007 (when (commandp command)
1008 (call-interactively command))))))
1010 (defun widget-tabable-at (&optional pos)
1011 "Return the tabable widget at POS, or nil.
1012 POS defaults to the value of (point)."
1013 (let ((widget (widget-at pos)))
1014 (if widget
1015 (let ((order (widget-get widget :tab-order)))
1016 (if order
1017 (if (>= order 0)
1018 widget)
1019 widget)))))
1021 (defvar widget-use-overlay-change t
1022 "If non-nil, use overlay change functions to tab around in the buffer.
1023 This is much faster, but doesn't work reliably on Emacs 19.34.")
1025 (defun widget-move (arg)
1026 "Move point to the ARG next field or button.
1027 ARG may be negative to move backward."
1028 (or (bobp) (> arg 0) (backward-char))
1029 (let ((wrapped 0)
1030 (number arg)
1031 (old (widget-tabable-at)))
1032 ;; Forward.
1033 (while (> arg 0)
1034 (cond ((eobp)
1035 (goto-char (point-min))
1036 (setq wrapped (1+ wrapped)))
1037 (widget-use-overlay-change
1038 (goto-char (next-overlay-change (point))))
1040 (forward-char 1)))
1041 (and (= wrapped 2)
1042 (eq arg number)
1043 (error "No buttons or fields found"))
1044 (let ((new (widget-tabable-at)))
1045 (when new
1046 (unless (eq new old)
1047 (setq arg (1- arg))
1048 (setq old new)))))
1049 ;; Backward.
1050 (while (< arg 0)
1051 (cond ((bobp)
1052 (goto-char (point-max))
1053 (setq wrapped (1+ wrapped)))
1054 (widget-use-overlay-change
1055 (goto-char (previous-overlay-change (point))))
1057 (backward-char 1)))
1058 (and (= wrapped 2)
1059 (eq arg number)
1060 (error "No buttons or fields found"))
1061 (let ((new (widget-tabable-at)))
1062 (when new
1063 (unless (eq new old)
1064 (setq arg (1+ arg))))))
1065 (let ((new (widget-tabable-at)))
1066 (while (eq (widget-tabable-at) new)
1067 (backward-char)))
1068 (forward-char))
1069 (widget-echo-help (point))
1070 (run-hooks 'widget-move-hook))
1072 (defun widget-forward (arg)
1073 "Move point to the next field or button.
1074 With optional ARG, move across that many fields."
1075 (interactive "p")
1076 (run-hooks 'widget-forward-hook)
1077 (widget-move arg))
1079 (defun widget-backward (arg)
1080 "Move point to the previous field or button.
1081 With optional ARG, move across that many fields."
1082 (interactive "p")
1083 (run-hooks 'widget-backward-hook)
1084 (widget-move (- arg)))
1086 ;; Since the widget code uses a `field' property to identify fields,
1087 ;; ordinary beginning-of-line does the right thing.
1088 (defalias 'widget-beginning-of-line 'beginning-of-line)
1090 (defun widget-end-of-line ()
1091 "Go to end of field or end of line, whichever is first.
1092 Trailing spaces at the end of padded fields are not considered part of
1093 the field."
1094 (interactive)
1095 ;; Ordinary end-of-line does the right thing, because we're inside
1096 ;; text with a `field' property.
1097 (end-of-line)
1098 (unless (eolp)
1099 ;; ... except that we want to ignore trailing spaces in fields that
1100 ;; aren't terminated by a newline, because they are used as padding,
1101 ;; and ignored when extracting the entered value of the field.
1102 (skip-chars-backward " " (field-beginning (1- (point))))))
1104 (defun widget-kill-line ()
1105 "Kill to end of field or end of line, whichever is first."
1106 (interactive)
1107 (let* ((field (widget-field-find (point)))
1108 (end (and field (widget-field-end field))))
1109 (if (and field (> (line-beginning-position 2) end))
1110 (kill-region (point) end)
1111 (call-interactively 'kill-line))))
1113 (defcustom widget-complete-field (lookup-key global-map "\M-\t")
1114 "Default function to call for completion inside fields."
1115 :options '(ispell-complete-word complete-tag lisp-complete-symbol)
1116 :type 'function
1117 :group 'widgets)
1119 (defun widget-narrow-to-field ()
1120 "Narrow to field."
1121 (interactive)
1122 (let ((field (widget-field-find (point))))
1123 (if field
1124 (narrow-to-region (line-beginning-position) (line-end-position)))))
1126 (defun widget-complete ()
1127 "Complete content of editable field from point.
1128 When not inside a field, move to the previous button or field."
1129 (interactive)
1130 (let ((field (widget-field-find (point))))
1131 (if field
1132 (save-restriction
1133 (widget-narrow-to-field)
1134 (widget-apply field :complete))
1135 (error "Not in an editable field"))))
1137 ;;; Setting up the buffer.
1139 (defvar widget-field-new nil
1140 "List of all newly created editable fields in the buffer.")
1141 (make-variable-buffer-local 'widget-field-new)
1143 (defvar widget-field-list nil
1144 "List of all editable fields in the buffer.")
1145 (make-variable-buffer-local 'widget-field-list)
1147 (defun widget-at (&optional pos)
1148 "The button or field at POS (default, point)."
1149 (or (get-char-property (or pos (point)) 'button)
1150 (widget-field-at pos)))
1152 ;;;###autoload
1153 (defun widget-setup ()
1154 "Setup current buffer so editing string widgets works."
1155 (let ((inhibit-read-only t)
1156 (inhibit-modification-hooks t)
1157 field)
1158 (while widget-field-new
1159 (setq field (car widget-field-new)
1160 widget-field-new (cdr widget-field-new)
1161 widget-field-list (cons field widget-field-list))
1162 (let ((from (car (widget-get field :field-overlay)))
1163 (to (cdr (widget-get field :field-overlay))))
1164 (widget-specify-field field
1165 (marker-position from) (marker-position to))
1166 (set-marker from nil)
1167 (set-marker to nil))))
1168 (widget-clear-undo)
1169 (widget-add-change))
1171 (defvar widget-field-last nil)
1172 ;; Last field containing point.
1173 (make-variable-buffer-local 'widget-field-last)
1175 (defvar widget-field-was nil)
1176 ;; The widget data before the change.
1177 (make-variable-buffer-local 'widget-field-was)
1179 (defun widget-field-at (pos)
1180 "Return the widget field at POS, or nil if none."
1181 (let ((field (get-char-property (or pos (point)) 'field)))
1182 (if (eq field 'boundary)
1183 (get-char-property (or pos (point)) 'real-field)
1184 field)))
1186 (defun widget-field-buffer (widget)
1187 "Return the buffer of WIDGET's editing field."
1188 (let ((overlay (widget-get widget :field-overlay)))
1189 (cond ((overlayp overlay)
1190 (overlay-buffer overlay))
1191 ((consp overlay)
1192 (marker-buffer (car overlay))))))
1194 (defun widget-field-start (widget)
1195 "Return the start of WIDGET's editing field."
1196 (let ((overlay (widget-get widget :field-overlay)))
1197 (if (overlayp overlay)
1198 (overlay-start overlay)
1199 (car overlay))))
1201 (defun widget-field-end (widget)
1202 "Return the end of WIDGET's editing field."
1203 (let ((overlay (widget-get widget :field-overlay)))
1204 ;; Don't subtract one if local-map works at the end of the overlay,
1205 ;; or if a special `boundary' field has been added after the widget
1206 ;; field.
1207 (if (overlayp overlay)
1208 (if (and (not (eq (with-current-buffer
1209 (widget-field-buffer widget)
1210 (save-restriction
1211 ;; `widget-narrow-to-field' can be
1212 ;; active when this function is called
1213 ;; from an change-functions hook. So
1214 ;; temporarily remove field narrowing
1215 ;; before to call `get-char-property'.
1216 (widen)
1217 (get-char-property (overlay-end overlay)
1218 'field)))
1219 'boundary))
1220 (or widget-field-add-space
1221 (null (widget-get widget :size))))
1222 (1- (overlay-end overlay))
1223 (overlay-end overlay))
1224 (cdr overlay))))
1226 (defun widget-field-find (pos)
1227 "Return the field at POS.
1228 Unlike (get-char-property POS 'field), this works with empty fields too."
1229 (let ((fields widget-field-list)
1230 field found)
1231 (while fields
1232 (setq field (car fields)
1233 fields (cdr fields))
1234 (when (and (<= (widget-field-start field) pos)
1235 (<= pos (widget-field-end field)))
1236 (when found
1237 (error "Overlapping fields"))
1238 (setq found field)))
1239 found))
1241 (defun widget-before-change (from to)
1242 ;; This is how, for example, a variable changes its state to `modified'.
1243 ;; when it is being edited.
1244 (unless inhibit-read-only
1245 (let ((from-field (widget-field-find from))
1246 (to-field (widget-field-find to)))
1247 (cond ((not (eq from-field to-field))
1248 (add-hook 'post-command-hook 'widget-add-change nil t)
1249 (signal 'text-read-only
1250 '("Change should be restricted to a single field")))
1251 ((null from-field)
1252 (add-hook 'post-command-hook 'widget-add-change nil t)
1253 (signal 'text-read-only
1254 '("Attempt to change text outside editable field")))
1255 (widget-field-use-before-change
1256 (widget-apply from-field :notify from-field))))))
1258 (defun widget-add-change ()
1259 (remove-hook 'post-command-hook 'widget-add-change t)
1260 (add-hook 'before-change-functions 'widget-before-change nil t)
1261 (add-hook 'after-change-functions 'widget-after-change nil t))
1263 (defun widget-after-change (from to old)
1264 "Adjust field size and text properties."
1265 (let ((field (widget-field-find from))
1266 (other (widget-field-find to)))
1267 (when field
1268 (unless (eq field other)
1269 (error "Change in different fields"))
1270 (let ((size (widget-get field :size)))
1271 (when size
1272 (let ((begin (widget-field-start field))
1273 (end (widget-field-end field)))
1274 (cond ((< (- end begin) size)
1275 ;; Field too small.
1276 (save-excursion
1277 (goto-char end)
1278 (insert-char ?\s (- (+ begin size) end))))
1279 ((> (- end begin) size)
1280 ;; Field too large and
1281 (if (or (< (point) (+ begin size))
1282 (> (point) end))
1283 ;; Point is outside extra space.
1284 (setq begin (+ begin size))
1285 ;; Point is within the extra space.
1286 (setq begin (point)))
1287 (save-excursion
1288 (goto-char end)
1289 (while (and (eq (preceding-char) ?\s)
1290 (> (point) begin))
1291 (delete-backward-char 1)))))))
1292 (widget-specify-secret field))
1293 (widget-apply field :notify field))))
1295 ;;; Widget Functions
1297 ;; These functions are used in the definition of multiple widgets.
1299 (defun widget-parent-action (widget &optional event)
1300 "Tell :parent of WIDGET to handle the :action.
1301 Optional EVENT is the event that triggered the action."
1302 (widget-apply (widget-get widget :parent) :action event))
1304 (defun widget-children-value-delete (widget)
1305 "Delete all :children and :buttons in WIDGET."
1306 (mapc 'widget-delete (widget-get widget :children))
1307 (widget-put widget :children nil)
1308 (mapc 'widget-delete (widget-get widget :buttons))
1309 (widget-put widget :buttons nil))
1311 (defun widget-children-validate (widget)
1312 "All the :children must be valid."
1313 (let ((children (widget-get widget :children))
1314 child found)
1315 (while (and children (not found))
1316 (setq child (car children)
1317 children (cdr children)
1318 found (widget-apply child :validate)))
1319 found))
1321 (defun widget-child-value-get (widget)
1322 "Get the value of the first member of :children in WIDGET."
1323 (widget-value (car (widget-get widget :children))))
1325 (defun widget-child-value-inline (widget)
1326 "Get the inline value of the first member of :children in WIDGET."
1327 (widget-apply (car (widget-get widget :children)) :value-inline))
1329 (defun widget-child-validate (widget)
1330 "The result of validating the first member of :children in WIDGET."
1331 (widget-apply (car (widget-get widget :children)) :validate))
1333 (defun widget-type-value-create (widget)
1334 "Convert and instantiate the value of the :type attribute of WIDGET.
1335 Store the newly created widget in the :children attribute.
1337 The value of the :type attribute should be an unconverted widget type."
1338 (let ((value (widget-get widget :value))
1339 (type (widget-get widget :type)))
1340 (widget-put widget :children
1341 (list (widget-create-child-value widget
1342 (widget-convert type)
1343 value)))))
1345 (defun widget-type-default-get (widget)
1346 "Get default value from the :type attribute of WIDGET.
1348 The value of the :type attribute should be an unconverted widget type."
1349 (widget-default-get (widget-convert (widget-get widget :type))))
1351 (defun widget-type-match (widget value)
1352 "Non-nil if the :type value of WIDGET matches VALUE.
1354 The value of the :type attribute should be an unconverted widget type."
1355 (widget-apply (widget-convert (widget-get widget :type)) :match value))
1357 (defun widget-types-copy (widget)
1358 "Copy :args as widget types in WIDGET."
1359 (widget-put widget :args (mapcar 'widget-copy (widget-get widget :args)))
1360 widget)
1362 ;; Made defsubst to speed up face editor creation.
1363 (defsubst widget-types-convert-widget (widget)
1364 "Convert :args as widget types in WIDGET."
1365 (widget-put widget :args (mapcar 'widget-convert (widget-get widget :args)))
1366 widget)
1368 (defun widget-value-convert-widget (widget)
1369 "Initialize :value from :args in WIDGET."
1370 (let ((args (widget-get widget :args)))
1371 (when args
1372 (widget-put widget :value (car args))
1373 ;; Don't convert :value here, as this is done in `widget-convert'.
1374 ;; (widget-put widget :value (widget-apply widget
1375 ;; :value-to-internal (car args)))
1376 (widget-put widget :args nil)))
1377 widget)
1379 (defun widget-value-value-get (widget)
1380 "Return the :value property of WIDGET."
1381 (widget-get widget :value))
1383 ;;; The `default' Widget.
1385 (define-widget 'default nil
1386 "Basic widget other widgets are derived from."
1387 :value-to-internal (lambda (widget value) value)
1388 :value-to-external (lambda (widget value) value)
1389 :button-prefix 'widget-button-prefix
1390 :button-suffix 'widget-button-suffix
1391 :complete 'widget-default-complete
1392 :create 'widget-default-create
1393 :indent nil
1394 :offset 0
1395 :format-handler 'widget-default-format-handler
1396 :button-face-get 'widget-default-button-face-get
1397 :sample-face-get 'widget-default-sample-face-get
1398 :delete 'widget-default-delete
1399 :copy 'identity
1400 :value-set 'widget-default-value-set
1401 :value-inline 'widget-default-value-inline
1402 :value-delete 'ignore
1403 :default-get 'widget-default-default-get
1404 :menu-tag-get 'widget-default-menu-tag-get
1405 :validate #'ignore
1406 :active 'widget-default-active
1407 :activate 'widget-specify-active
1408 :deactivate 'widget-default-deactivate
1409 :mouse-down-action #'ignore
1410 :action 'widget-default-action
1411 :notify 'widget-default-notify
1412 :prompt-value 'widget-default-prompt-value)
1414 (defun widget-default-complete (widget)
1415 "Call the value of the :complete-function property of WIDGET.
1416 If that does not exists, call the value of `widget-complete-field'."
1417 (call-interactively (or (widget-get widget :complete-function)
1418 widget-complete-field)))
1420 (defun widget-default-create (widget)
1421 "Create WIDGET at point in the current buffer."
1422 (widget-specify-insert
1423 (let ((from (point))
1424 button-begin button-end
1425 sample-begin sample-end
1426 doc-begin doc-end
1427 value-pos)
1428 (insert (widget-get widget :format))
1429 (goto-char from)
1430 ;; Parse escapes in format.
1431 (while (re-search-forward "%\\(.\\)" nil t)
1432 (let ((escape (char-after (match-beginning 1))))
1433 (delete-backward-char 2)
1434 (cond ((eq escape ?%)
1435 (insert ?%))
1436 ((eq escape ?\[)
1437 (setq button-begin (point))
1438 (insert (widget-get-indirect widget :button-prefix)))
1439 ((eq escape ?\])
1440 (insert (widget-get-indirect widget :button-suffix))
1441 (setq button-end (point)))
1442 ((eq escape ?\{)
1443 (setq sample-begin (point)))
1444 ((eq escape ?\})
1445 (setq sample-end (point)))
1446 ((eq escape ?n)
1447 (when (widget-get widget :indent)
1448 (insert ?\n)
1449 (insert-char ?\s (widget-get widget :indent))))
1450 ((eq escape ?t)
1451 (let ((image (widget-get widget :tag-glyph))
1452 (tag (widget-get widget :tag)))
1453 (cond (image
1454 (widget-image-insert widget (or tag "image") image))
1455 (tag
1456 (insert tag))
1458 (princ (widget-get widget :value)
1459 (current-buffer))))))
1460 ((eq escape ?d)
1461 (let ((doc (widget-get widget :doc)))
1462 (when doc
1463 (setq doc-begin (point))
1464 (insert doc)
1465 (while (eq (preceding-char) ?\n)
1466 (delete-backward-char 1))
1467 (insert ?\n)
1468 (setq doc-end (point)))))
1469 ((eq escape ?v)
1470 (if (and button-begin (not button-end))
1471 (widget-apply widget :value-create)
1472 (setq value-pos (point))))
1474 (widget-apply widget :format-handler escape)))))
1475 ;; Specify button, sample, and doc, and insert value.
1476 (and button-begin button-end
1477 (widget-specify-button widget button-begin button-end))
1478 (and sample-begin sample-end
1479 (widget-specify-sample widget sample-begin sample-end))
1480 (and doc-begin doc-end
1481 (widget-specify-doc widget doc-begin doc-end))
1482 (when value-pos
1483 (goto-char value-pos)
1484 (widget-apply widget :value-create)))
1485 (let ((from (point-min-marker))
1486 (to (point-max-marker)))
1487 (set-marker-insertion-type from t)
1488 (set-marker-insertion-type to nil)
1489 (widget-put widget :from from)
1490 (widget-put widget :to to)))
1491 (widget-clear-undo))
1493 (defun widget-default-format-handler (widget escape)
1494 ;; We recognize the %h escape by default.
1495 (let* ((buttons (widget-get widget :buttons)))
1496 (cond ((eq escape ?h)
1497 (let* ((doc-property (widget-get widget :documentation-property))
1498 (doc-try (cond ((widget-get widget :doc))
1499 ((functionp doc-property)
1500 (funcall doc-property
1501 (widget-get widget :value)))
1502 ((symbolp doc-property)
1503 (documentation-property
1504 (widget-get widget :value)
1505 doc-property))))
1506 (doc-text (and (stringp doc-try)
1507 (> (length doc-try) 1)
1508 doc-try))
1509 (doc-indent (widget-get widget :documentation-indent)))
1510 (when doc-text
1511 (and (eq (preceding-char) ?\n)
1512 (widget-get widget :indent)
1513 (insert-char ?\s (widget-get widget :indent)))
1514 ;; The `*' in the beginning is redundant.
1515 (when (eq (aref doc-text 0) ?*)
1516 (setq doc-text (substring doc-text 1)))
1517 ;; Get rid of trailing newlines.
1518 (when (string-match "\n+\\'" doc-text)
1519 (setq doc-text (substring doc-text 0 (match-beginning 0))))
1520 (push (widget-create-child-and-convert
1521 widget 'documentation-string
1522 :indent (cond ((numberp doc-indent )
1523 doc-indent)
1524 ((null doc-indent)
1525 nil)
1526 (t 0))
1527 doc-text)
1528 buttons))))
1530 (error "Unknown escape `%c'" escape)))
1531 (widget-put widget :buttons buttons)))
1533 (defun widget-default-button-face-get (widget)
1534 ;; Use :button-face or widget-button-face
1535 (or (widget-get widget :button-face)
1536 (let ((parent (widget-get widget :parent)))
1537 (if parent
1538 (widget-apply parent :button-face-get)
1539 widget-button-face))))
1541 (defun widget-default-sample-face-get (widget)
1542 ;; Use :sample-face.
1543 (widget-get widget :sample-face))
1545 (defun widget-default-delete (widget)
1546 "Remove widget from the buffer."
1547 (let ((from (widget-get widget :from))
1548 (to (widget-get widget :to))
1549 (inactive-overlay (widget-get widget :inactive))
1550 (button-overlay (widget-get widget :button-overlay))
1551 (sample-overlay (widget-get widget :sample-overlay))
1552 (doc-overlay (widget-get widget :doc-overlay))
1553 (inhibit-modification-hooks t)
1554 (inhibit-read-only t))
1555 (widget-apply widget :value-delete)
1556 (widget-children-value-delete widget)
1557 (when inactive-overlay
1558 (delete-overlay inactive-overlay))
1559 (when button-overlay
1560 (delete-overlay button-overlay))
1561 (when sample-overlay
1562 (delete-overlay sample-overlay))
1563 (when doc-overlay
1564 (delete-overlay doc-overlay))
1565 (when (< from to)
1566 ;; Kludge: this doesn't need to be true for empty formats.
1567 (delete-region from to))
1568 (set-marker from nil)
1569 (set-marker to nil))
1570 (widget-clear-undo))
1572 (defun widget-default-value-set (widget value)
1573 "Recreate widget with new value."
1574 (let* ((old-pos (point))
1575 (from (copy-marker (widget-get widget :from)))
1576 (to (copy-marker (widget-get widget :to)))
1577 (offset (if (and (<= from old-pos) (<= old-pos to))
1578 (if (>= old-pos (1- to))
1579 (- old-pos to 1)
1580 (- old-pos from)))))
1581 ;;??? Bug: this ought to insert the new value before deleting the old one,
1582 ;; so that markers on either side of the value automatically
1583 ;; stay on the same side. -- rms.
1584 (save-excursion
1585 (goto-char (widget-get widget :from))
1586 (widget-apply widget :delete)
1587 (widget-put widget :value value)
1588 (widget-apply widget :create))
1589 (if offset
1590 (if (< offset 0)
1591 (goto-char (+ (widget-get widget :to) offset 1))
1592 (goto-char (min (+ from offset) (1- (widget-get widget :to))))))))
1594 (defun widget-default-value-inline (widget)
1595 "Wrap value in a list unless it is inline."
1596 (if (widget-get widget :inline)
1597 (widget-value widget)
1598 (list (widget-value widget))))
1600 (defun widget-default-default-get (widget)
1601 "Get `:value'."
1602 (widget-get widget :value))
1604 (defun widget-default-menu-tag-get (widget)
1605 "Use tag or value for menus."
1606 (or (widget-get widget :menu-tag)
1607 (widget-get widget :tag)
1608 (widget-princ-to-string (widget-get widget :value))))
1610 (defun widget-default-active (widget)
1611 "Return t iff this widget active (user modifiable)."
1612 (or (widget-get widget :always-active)
1613 (and (not (widget-get widget :inactive))
1614 (let ((parent (widget-get widget :parent)))
1615 (or (null parent)
1616 (widget-apply parent :active))))))
1618 (defun widget-default-deactivate (widget)
1619 "Make WIDGET inactive for user modifications."
1620 (widget-specify-inactive widget
1621 (widget-get widget :from)
1622 (widget-get widget :to)))
1624 (defun widget-default-action (widget &optional event)
1625 "Notify the parent when a widget changes."
1626 (let ((parent (widget-get widget :parent)))
1627 (when parent
1628 (widget-apply parent :notify widget event))))
1630 (defun widget-default-notify (widget child &optional event)
1631 "Pass notification to parent."
1632 (widget-default-action widget event))
1634 (defun widget-default-prompt-value (widget prompt value unbound)
1635 "Read an arbitrary value. Stolen from `set-variable'."
1636 ;; (let ((initial (if unbound
1637 ;; nil
1638 ;; It would be nice if we could do a `(cons val 1)' here.
1639 ;; (prin1-to-string (custom-quote value))))))
1640 (eval-minibuffer prompt))
1642 ;;; The `item' Widget.
1644 (define-widget 'item 'default
1645 "Constant items for inclusion in other widgets."
1646 :convert-widget 'widget-value-convert-widget
1647 :value-create 'widget-item-value-create
1648 :value-delete 'ignore
1649 :value-get 'widget-value-value-get
1650 :match 'widget-item-match
1651 :match-inline 'widget-item-match-inline
1652 :action 'widget-item-action
1653 :format "%t\n")
1655 (defun widget-item-value-create (widget)
1656 "Insert the printed representation of the value."
1657 (princ (widget-get widget :value) (current-buffer)))
1659 (defun widget-item-match (widget value)
1660 ;; Match if the value is the same.
1661 (equal (widget-get widget :value) value))
1663 (defun widget-item-match-inline (widget values)
1664 ;; Match if the value is the same.
1665 (let ((value (widget-get widget :value)))
1666 (and (listp value)
1667 (<= (length value) (length values))
1668 (let ((head (widget-sublist values 0 (length value))))
1669 (and (equal head value)
1670 (cons head (widget-sublist values (length value))))))))
1672 (defun widget-sublist (list start &optional end)
1673 "Return the sublist of LIST from START to END.
1674 If END is omitted, it defaults to the length of LIST."
1675 (if (> start 0) (setq list (nthcdr start list)))
1676 (if end
1677 (unless (<= end start)
1678 (setq list (copy-sequence list))
1679 (setcdr (nthcdr (- end start 1) list) nil)
1680 list)
1681 (copy-sequence list)))
1683 (defun widget-item-action (widget &optional event)
1684 ;; Just notify itself.
1685 (widget-apply widget :notify widget event))
1687 ;;; The `push-button' Widget.
1689 ;; (defcustom widget-push-button-gui t
1690 ;; "If non nil, use GUI push buttons when available."
1691 ;; :group 'widgets
1692 ;; :type 'boolean)
1694 ;; Cache already created GUI objects.
1695 ;; (defvar widget-push-button-cache nil)
1697 (defcustom widget-push-button-prefix "["
1698 "String used as prefix for buttons."
1699 :type 'string
1700 :group 'widget-button)
1702 (defcustom widget-push-button-suffix "]"
1703 "String used as suffix for buttons."
1704 :type 'string
1705 :group 'widget-button)
1707 (define-widget 'push-button 'item
1708 "A pushable button."
1709 :button-prefix ""
1710 :button-suffix ""
1711 :value-create 'widget-push-button-value-create
1712 :format "%[%v%]")
1714 (defun widget-push-button-value-create (widget)
1715 "Insert text representing the `on' and `off' states."
1716 (let* ((tag (or (widget-get widget :tag)
1717 (widget-get widget :value)))
1718 (tag-glyph (widget-get widget :tag-glyph))
1719 (text (concat widget-push-button-prefix
1720 tag widget-push-button-suffix)))
1721 (if tag-glyph
1722 (widget-image-insert widget text tag-glyph)
1723 (insert text))))
1725 ;; (defun widget-gui-action (widget)
1726 ;; "Apply :action for WIDGET."
1727 ;; (widget-apply-action widget (this-command-keys)))
1729 ;;; The `link' Widget.
1731 (defcustom widget-link-prefix "["
1732 "String used as prefix for links."
1733 :type 'string
1734 :group 'widget-button)
1736 (defcustom widget-link-suffix "]"
1737 "String used as suffix for links."
1738 :type 'string
1739 :group 'widget-button)
1741 (define-widget 'link 'item
1742 "An embedded link."
1743 :button-prefix 'widget-link-prefix
1744 :button-suffix 'widget-link-suffix
1745 :follow-link "\C-m"
1746 :help-echo "Follow the link."
1747 :format "%[%t%]")
1749 ;;; The `info-link' Widget.
1751 (define-widget 'info-link 'link
1752 "A link to an info file."
1753 :action 'widget-info-link-action)
1755 (defun widget-info-link-action (widget &optional event)
1756 "Open the info node specified by WIDGET."
1757 (info (widget-value widget)))
1759 ;;; The `url-link' Widget.
1761 (define-widget 'url-link 'link
1762 "A link to an www page."
1763 :action 'widget-url-link-action)
1765 (defun widget-url-link-action (widget &optional event)
1766 "Open the URL specified by WIDGET."
1767 (browse-url (widget-value widget)))
1769 ;;; The `function-link' Widget.
1771 (define-widget 'function-link 'link
1772 "A link to an Emacs function."
1773 :action 'widget-function-link-action)
1775 (defun widget-function-link-action (widget &optional event)
1776 "Show the function specified by WIDGET."
1777 (describe-function (widget-value widget)))
1779 ;;; The `variable-link' Widget.
1781 (define-widget 'variable-link 'link
1782 "A link to an Emacs variable."
1783 :action 'widget-variable-link-action)
1785 (defun widget-variable-link-action (widget &optional event)
1786 "Show the variable specified by WIDGET."
1787 (describe-variable (widget-value widget)))
1789 ;;; The `file-link' Widget.
1791 (define-widget 'file-link 'link
1792 "A link to a file."
1793 :action 'widget-file-link-action)
1795 (defun widget-file-link-action (widget &optional event)
1796 "Find the file specified by WIDGET."
1797 (find-file (widget-value widget)))
1799 ;;; The `emacs-library-link' Widget.
1801 (define-widget 'emacs-library-link 'link
1802 "A link to an Emacs Lisp library file."
1803 :action 'widget-emacs-library-link-action)
1805 (defun widget-emacs-library-link-action (widget &optional event)
1806 "Find the Emacs library file specified by WIDGET."
1807 (find-file (locate-library (widget-value widget))))
1809 ;;; The `emacs-commentary-link' Widget.
1811 (define-widget 'emacs-commentary-link 'link
1812 "A link to Commentary in an Emacs Lisp library file."
1813 :action 'widget-emacs-commentary-link-action)
1815 (defun widget-emacs-commentary-link-action (widget &optional event)
1816 "Find the Commentary section of the Emacs file specified by WIDGET."
1817 (finder-commentary (widget-value widget)))
1819 ;;; The `editable-field' Widget.
1821 (define-widget 'editable-field 'default
1822 "An editable text field."
1823 :convert-widget 'widget-value-convert-widget
1824 :keymap widget-field-keymap
1825 :format "%v"
1826 :help-echo "M-TAB: complete field; RET: enter value"
1827 :value ""
1828 :prompt-internal 'widget-field-prompt-internal
1829 :prompt-history 'widget-field-history
1830 :prompt-value 'widget-field-prompt-value
1831 :action 'widget-field-action
1832 :validate 'widget-field-validate
1833 :valid-regexp ""
1834 :error "Field's value doesn't match allowed forms"
1835 :value-create 'widget-field-value-create
1836 :value-delete 'widget-field-value-delete
1837 :value-get 'widget-field-value-get
1838 :match 'widget-field-match)
1840 (defvar widget-field-history nil
1841 "History of field minibuffer edits.")
1843 (defun widget-field-prompt-internal (widget prompt initial history)
1844 "Read string for WIDGET promptinhg with PROMPT.
1845 INITIAL is the initial input and HISTORY is a symbol containing
1846 the earlier input."
1847 (read-string prompt initial history))
1849 (defun widget-field-prompt-value (widget prompt value unbound)
1850 "Prompt for a string."
1851 (widget-apply widget
1852 :value-to-external
1853 (widget-apply widget
1854 :prompt-internal prompt
1855 (unless unbound
1856 (cons (widget-apply widget
1857 :value-to-internal value)
1859 (widget-get widget :prompt-history))))
1861 (defvar widget-edit-functions nil)
1863 (defun widget-field-action (widget &optional event)
1864 "Move to next field."
1865 (widget-forward 1)
1866 (run-hook-with-args 'widget-edit-functions widget))
1868 (defun widget-field-validate (widget)
1869 "Valid if the content matches `:valid-regexp'."
1870 (unless (string-match (widget-get widget :valid-regexp)
1871 (widget-apply widget :value-get))
1872 widget))
1874 (defun widget-field-value-create (widget)
1875 "Create an editable text field."
1876 (let ((size (widget-get widget :size))
1877 (value (widget-get widget :value))
1878 (from (point))
1879 ;; This is changed to a real overlay in `widget-setup'. We
1880 ;; need the end points to behave differently until
1881 ;; `widget-setup' is called.
1882 (overlay (cons (make-marker) (make-marker))))
1883 (widget-put widget :field-overlay overlay)
1884 (insert value)
1885 (and size
1886 (< (length value) size)
1887 (insert-char ?\s (- size (length value))))
1888 (unless (memq widget widget-field-list)
1889 (setq widget-field-new (cons widget widget-field-new)))
1890 (move-marker (cdr overlay) (point))
1891 (set-marker-insertion-type (cdr overlay) nil)
1892 (when (null size)
1893 (insert ?\n))
1894 (move-marker (car overlay) from)
1895 (set-marker-insertion-type (car overlay) t)))
1897 (defun widget-field-value-delete (widget)
1898 "Remove the widget from the list of active editing fields."
1899 (setq widget-field-list (delq widget widget-field-list))
1900 (setq widget-field-new (delq widget widget-field-new))
1901 ;; These are nil if the :format string doesn't contain `%v'.
1902 (let ((overlay (widget-get widget :field-overlay)))
1903 (when (overlayp overlay)
1904 (delete-overlay overlay))))
1906 (defun widget-field-value-get (widget)
1907 "Return current text in editing field."
1908 (let ((from (widget-field-start widget))
1909 (to (widget-field-end widget))
1910 (buffer (widget-field-buffer widget))
1911 (size (widget-get widget :size))
1912 (secret (widget-get widget :secret))
1913 (old (current-buffer)))
1914 (if (and from to)
1915 (progn
1916 (set-buffer buffer)
1917 (while (and size
1918 (not (zerop size))
1919 (> to from)
1920 (eq (char-after (1- to)) ?\s))
1921 (setq to (1- to)))
1922 (let ((result (buffer-substring-no-properties from to)))
1923 (when secret
1924 (let ((index 0))
1925 (while (< (+ from index) to)
1926 (aset result index
1927 (get-char-property (+ from index) 'secret))
1928 (setq index (1+ index)))))
1929 (set-buffer old)
1930 result))
1931 (widget-get widget :value))))
1933 (defun widget-field-match (widget value)
1934 ;; Match any string.
1935 (stringp value))
1937 ;;; The `text' Widget.
1939 (define-widget 'text 'editable-field
1940 "A multiline text area."
1941 :keymap widget-text-keymap)
1943 ;;; The `menu-choice' Widget.
1945 (define-widget 'menu-choice 'default
1946 "A menu of options."
1947 :convert-widget 'widget-types-convert-widget
1948 :copy 'widget-types-copy
1949 :format "%[%t%]: %v"
1950 :case-fold t
1951 :tag "choice"
1952 :void '(item :format "invalid (%t)\n")
1953 :value-create 'widget-choice-value-create
1954 :value-get 'widget-child-value-get
1955 :value-inline 'widget-child-value-inline
1956 :default-get 'widget-choice-default-get
1957 :mouse-down-action 'widget-choice-mouse-down-action
1958 :action 'widget-choice-action
1959 :error "Make a choice"
1960 :validate 'widget-choice-validate
1961 :match 'widget-choice-match
1962 :match-inline 'widget-choice-match-inline)
1964 (defun widget-choice-value-create (widget)
1965 "Insert the first choice that matches the value."
1966 (let ((value (widget-get widget :value))
1967 (args (widget-get widget :args))
1968 (explicit (widget-get widget :explicit-choice))
1969 current)
1970 (if explicit
1971 (progn
1972 ;; If the user specified the choice for this value,
1973 ;; respect that choice.
1974 (widget-put widget :children (list (widget-create-child-value
1975 widget explicit value)))
1976 (widget-put widget :choice explicit)
1977 (widget-put widget :explicit-choice nil))
1978 (while args
1979 (setq current (car args)
1980 args (cdr args))
1981 (when (widget-apply current :match value)
1982 (widget-put widget :children (list (widget-create-child-value
1983 widget current value)))
1984 (widget-put widget :choice current)
1985 (setq args nil
1986 current nil)))
1987 (when current
1988 (let ((void (widget-get widget :void)))
1989 (widget-put widget :children (list (widget-create-child-and-convert
1990 widget void :value value)))
1991 (widget-put widget :choice void))))))
1993 (defun widget-choice-default-get (widget)
1994 ;; Get default for the first choice.
1995 (widget-default-get (car (widget-get widget :args))))
1997 (defcustom widget-choice-toggle nil
1998 "If non-nil, a binary choice will just toggle between the values.
1999 Otherwise, the user will explicitly have to choose between the values
2000 when he invoked the menu."
2001 :type 'boolean
2002 :group 'widgets)
2004 (defun widget-choice-mouse-down-action (widget &optional event)
2005 ;; Return non-nil if we need a menu.
2006 (let ((args (widget-get widget :args))
2007 (old (widget-get widget :choice)))
2008 (cond ((not (display-popup-menus-p))
2009 ;; No place to pop up a menu.
2010 nil)
2011 ((< (length args) 2)
2012 ;; Empty or singleton list, just return the value.
2013 nil)
2014 ((> (length args) widget-menu-max-size)
2015 ;; Too long, prompt.
2016 nil)
2017 ((> (length args) 2)
2018 ;; Reasonable sized list, use menu.
2020 ((and widget-choice-toggle (memq old args))
2021 ;; We toggle.
2022 nil)
2024 ;; Ask which of the two.
2025 t))))
2027 (defun widget-choice-action (widget &optional event)
2028 ;; Make a choice.
2029 (let ((args (widget-get widget :args))
2030 (old (widget-get widget :choice))
2031 (tag (widget-apply widget :menu-tag-get))
2032 (completion-ignore-case (widget-get widget :case-fold))
2033 this-explicit
2034 current choices)
2035 ;; Remember old value.
2036 (if (and old (not (widget-apply widget :validate)))
2037 (let* ((external (widget-value widget))
2038 (internal (widget-apply old :value-to-internal external)))
2039 (widget-put old :value internal)))
2040 ;; Find new choice.
2041 (setq current
2042 (cond ((= (length args) 0)
2043 nil)
2044 ((= (length args) 1)
2045 (nth 0 args))
2046 ((and widget-choice-toggle
2047 (= (length args) 2)
2048 (memq old args))
2049 (if (eq old (nth 0 args))
2050 (nth 1 args)
2051 (nth 0 args)))
2053 (while args
2054 (setq current (car args)
2055 args (cdr args))
2056 (setq choices
2057 (cons (cons (widget-apply current :menu-tag-get)
2058 current)
2059 choices)))
2060 (setq this-explicit t)
2061 (widget-choose tag (reverse choices) event))))
2062 (when current
2063 ;; If this was an explicit user choice, record the choice,
2064 ;; so that widget-choice-value-create will respect it.
2065 (when this-explicit
2066 (widget-put widget :explicit-choice current))
2067 (widget-value-set widget (widget-default-get current))
2068 (widget-setup)
2069 (widget-apply widget :notify widget event)))
2070 (run-hook-with-args 'widget-edit-functions widget))
2072 (defun widget-choice-validate (widget)
2073 ;; Valid if we have made a valid choice.
2074 (if (eq (widget-get widget :void) (widget-get widget :choice))
2075 widget
2076 (widget-apply (car (widget-get widget :children)) :validate)))
2078 (defun widget-choice-match (widget value)
2079 ;; Matches if one of the choices matches.
2080 (let ((args (widget-get widget :args))
2081 current found)
2082 (while (and args (not found))
2083 (setq current (car args)
2084 args (cdr args)
2085 found (widget-apply current :match value)))
2086 found))
2088 (defun widget-choice-match-inline (widget values)
2089 ;; Matches if one of the choices matches.
2090 (let ((args (widget-get widget :args))
2091 current found)
2092 (while (and args (null found))
2093 (setq current (car args)
2094 args (cdr args)
2095 found (widget-match-inline current values)))
2096 found))
2098 ;;; The `toggle' Widget.
2100 (define-widget 'toggle 'item
2101 "Toggle between two states."
2102 :format "%[%v%]\n"
2103 :value-create 'widget-toggle-value-create
2104 :action 'widget-toggle-action
2105 :match (lambda (widget value) t)
2106 :on "on"
2107 :off "off")
2109 (defun widget-toggle-value-create (widget)
2110 "Insert text representing the `on' and `off' states."
2111 (if (widget-value widget)
2112 (let ((image (widget-get widget :on-glyph)))
2113 (and (display-graphic-p)
2114 (listp image)
2115 (not (eq (car image) 'image))
2116 (widget-put widget :on-glyph (setq image (eval image))))
2117 (widget-image-insert widget
2118 (widget-get widget :on)
2119 image))
2120 (let ((image (widget-get widget :off-glyph)))
2121 (and (display-graphic-p)
2122 (listp image)
2123 (not (eq (car image) 'image))
2124 (widget-put widget :off-glyph (setq image (eval image))))
2125 (widget-image-insert widget (widget-get widget :off) image))))
2127 (defun widget-toggle-action (widget &optional event)
2128 ;; Toggle value.
2129 (widget-value-set widget (not (widget-value widget)))
2130 (widget-apply widget :notify widget event)
2131 (run-hook-with-args 'widget-edit-functions widget))
2133 ;;; The `checkbox' Widget.
2135 (define-widget 'checkbox 'toggle
2136 "A checkbox toggle."
2137 :button-suffix ""
2138 :button-prefix ""
2139 :format "%[%v%]"
2140 :on "[X]"
2141 ;; We could probably do the same job as the images using single
2142 ;; space characters in a boxed face with a stretch specification to
2143 ;; make them square.
2144 :on-glyph '(create-image "\300\300\141\143\067\076\034\030"
2145 'xbm t :width 8 :height 8
2146 :background "grey75" ; like default mode line
2147 :foreground "black"
2148 :relief -2
2149 :ascent 'center)
2150 :off "[ ]"
2151 :off-glyph '(create-image (make-string 8 0)
2152 'xbm t :width 8 :height 8
2153 :background "grey75"
2154 :foreground "black"
2155 :relief -2
2156 :ascent 'center)
2157 :help-echo "Toggle this item."
2158 :action 'widget-checkbox-action)
2160 (defun widget-checkbox-action (widget &optional event)
2161 "Toggle checkbox, notify parent, and set active state of sibling."
2162 (widget-toggle-action widget event)
2163 (let ((sibling (widget-get-sibling widget)))
2164 (when sibling
2165 (if (widget-value widget)
2166 (widget-apply sibling :activate)
2167 (widget-apply sibling :deactivate)))))
2169 ;;; The `checklist' Widget.
2171 (define-widget 'checklist 'default
2172 "A multiple choice widget."
2173 :convert-widget 'widget-types-convert-widget
2174 :copy 'widget-types-copy
2175 :format "%v"
2176 :offset 4
2177 :entry-format "%b %v"
2178 :greedy nil
2179 :value-create 'widget-checklist-value-create
2180 :value-get 'widget-checklist-value-get
2181 :validate 'widget-checklist-validate
2182 :match 'widget-checklist-match
2183 :match-inline 'widget-checklist-match-inline)
2185 (defun widget-checklist-value-create (widget)
2186 ;; Insert all values
2187 (let ((alist (widget-checklist-match-find widget (widget-get widget :value)))
2188 (args (widget-get widget :args)))
2189 (while args
2190 (widget-checklist-add-item widget (car args) (assq (car args) alist))
2191 (setq args (cdr args)))
2192 (widget-put widget :children (nreverse (widget-get widget :children)))))
2194 (defun widget-checklist-add-item (widget type chosen)
2195 "Create checklist item in WIDGET of type TYPE.
2196 If the item is checked, CHOSEN is a cons whose cdr is the value."
2197 (and (eq (preceding-char) ?\n)
2198 (widget-get widget :indent)
2199 (insert-char ?\s (widget-get widget :indent)))
2200 (widget-specify-insert
2201 (let* ((children (widget-get widget :children))
2202 (buttons (widget-get widget :buttons))
2203 (button-args (or (widget-get type :sibling-args)
2204 (widget-get widget :button-args)))
2205 (from (point))
2206 child button)
2207 (insert (widget-get widget :entry-format))
2208 (goto-char from)
2209 ;; Parse % escapes in format.
2210 (while (re-search-forward "%\\([bv%]\\)" nil t)
2211 (let ((escape (char-after (match-beginning 1))))
2212 (delete-backward-char 2)
2213 (cond ((eq escape ?%)
2214 (insert ?%))
2215 ((eq escape ?b)
2216 (setq button (apply 'widget-create-child-and-convert
2217 widget 'checkbox
2218 :value (not (null chosen))
2219 button-args)))
2220 ((eq escape ?v)
2221 (setq child
2222 (cond ((not chosen)
2223 (let ((child (widget-create-child widget type)))
2224 (widget-apply child :deactivate)
2225 child))
2226 ((widget-get type :inline)
2227 (widget-create-child-value
2228 widget type (cdr chosen)))
2230 (widget-create-child-value
2231 widget type (car (cdr chosen)))))))
2233 (error "Unknown escape `%c'" escape)))))
2234 ;; Update properties.
2235 (and button child (widget-put child :button button))
2236 (and button (widget-put widget :buttons (cons button buttons)))
2237 (and child (widget-put widget :children (cons child children))))))
2239 (defun widget-checklist-match (widget values)
2240 ;; All values must match a type in the checklist.
2241 (and (listp values)
2242 (null (cdr (widget-checklist-match-inline widget values)))))
2244 (defun widget-checklist-match-inline (widget values)
2245 ;; Find the values which match a type in the checklist.
2246 (let ((greedy (widget-get widget :greedy))
2247 (args (copy-sequence (widget-get widget :args)))
2248 found rest)
2249 (while values
2250 (let ((answer (widget-checklist-match-up args values)))
2251 (cond (answer
2252 (let ((vals (widget-match-inline answer values)))
2253 (setq found (append found (car vals))
2254 values (cdr vals)
2255 args (delq answer args))))
2256 (greedy
2257 (setq rest (append rest (list (car values)))
2258 values (cdr values)))
2260 (setq rest (append rest values)
2261 values nil)))))
2262 (cons found rest)))
2264 (defun widget-checklist-match-find (widget vals)
2265 "Find the vals which match a type in the checklist.
2266 Return an alist of (TYPE MATCH)."
2267 (let ((greedy (widget-get widget :greedy))
2268 (args (copy-sequence (widget-get widget :args)))
2269 found)
2270 (while vals
2271 (let ((answer (widget-checklist-match-up args vals)))
2272 (cond (answer
2273 (let ((match (widget-match-inline answer vals)))
2274 (setq found (cons (cons answer (car match)) found)
2275 vals (cdr match)
2276 args (delq answer args))))
2277 (greedy
2278 (setq vals (cdr vals)))
2280 (setq vals nil)))))
2281 found))
2283 (defun widget-checklist-match-up (args vals)
2284 "Return the first type from ARGS that matches VALS."
2285 (let (current found)
2286 (while (and args (null found))
2287 (setq current (car args)
2288 args (cdr args)
2289 found (widget-match-inline current vals)))
2290 (if found
2291 current)))
2293 (defun widget-checklist-value-get (widget)
2294 ;; The values of all selected items.
2295 (let ((children (widget-get widget :children))
2296 child result)
2297 (while children
2298 (setq child (car children)
2299 children (cdr children))
2300 (if (widget-value (widget-get child :button))
2301 (setq result (append result (widget-apply child :value-inline)))))
2302 result))
2304 (defun widget-checklist-validate (widget)
2305 ;; Ticked chilren must be valid.
2306 (let ((children (widget-get widget :children))
2307 child button found)
2308 (while (and children (not found))
2309 (setq child (car children)
2310 children (cdr children)
2311 button (widget-get child :button)
2312 found (and (widget-value button)
2313 (widget-apply child :validate))))
2314 found))
2316 ;;; The `option' Widget
2318 (define-widget 'option 'checklist
2319 "An widget with an optional item."
2320 :inline t)
2322 ;;; The `choice-item' Widget.
2324 (define-widget 'choice-item 'item
2325 "Button items that delegate action events to their parents."
2326 :action 'widget-parent-action
2327 :format "%[%t%] \n")
2329 ;;; The `radio-button' Widget.
2331 (define-widget 'radio-button 'toggle
2332 "A radio button for use in the `radio' widget."
2333 :notify 'widget-radio-button-notify
2334 :format "%[%v%]"
2335 :button-suffix ""
2336 :button-prefix ""
2337 :on "(*)"
2338 :on-glyph "radio1"
2339 :off "( )"
2340 :off-glyph "radio0")
2342 (defun widget-radio-button-notify (widget child &optional event)
2343 ;; Tell daddy.
2344 (widget-apply (widget-get widget :parent) :action widget event))
2346 ;;; The `radio-button-choice' Widget.
2348 (define-widget 'radio-button-choice 'default
2349 "Select one of multiple options."
2350 :convert-widget 'widget-types-convert-widget
2351 :copy 'widget-types-copy
2352 :offset 4
2353 :format "%v"
2354 :entry-format "%b %v"
2355 :value-create 'widget-radio-value-create
2356 :value-get 'widget-radio-value-get
2357 :value-inline 'widget-radio-value-inline
2358 :value-set 'widget-radio-value-set
2359 :error "You must push one of the buttons"
2360 :validate 'widget-radio-validate
2361 :match 'widget-choice-match
2362 :match-inline 'widget-choice-match-inline
2363 :action 'widget-radio-action)
2365 (defun widget-radio-value-create (widget)
2366 ;; Insert all values
2367 (let ((args (widget-get widget :args))
2368 arg)
2369 (while args
2370 (setq arg (car args)
2371 args (cdr args))
2372 (widget-radio-add-item widget arg))))
2374 (defun widget-radio-add-item (widget type)
2375 "Add to radio widget WIDGET a new radio button item of type TYPE."
2376 ;; (setq type (widget-convert type))
2377 (and (eq (preceding-char) ?\n)
2378 (widget-get widget :indent)
2379 (insert-char ?\s (widget-get widget :indent)))
2380 (widget-specify-insert
2381 (let* ((value (widget-get widget :value))
2382 (children (widget-get widget :children))
2383 (buttons (widget-get widget :buttons))
2384 (button-args (or (widget-get type :sibling-args)
2385 (widget-get widget :button-args)))
2386 (from (point))
2387 (chosen (and (null (widget-get widget :choice))
2388 (widget-apply type :match value)))
2389 child button)
2390 (insert (widget-get widget :entry-format))
2391 (goto-char from)
2392 ;; Parse % escapes in format.
2393 (while (re-search-forward "%\\([bv%]\\)" nil t)
2394 (let ((escape (char-after (match-beginning 1))))
2395 (delete-backward-char 2)
2396 (cond ((eq escape ?%)
2397 (insert ?%))
2398 ((eq escape ?b)
2399 (setq button (apply 'widget-create-child-and-convert
2400 widget 'radio-button
2401 :value (not (null chosen))
2402 button-args)))
2403 ((eq escape ?v)
2404 (setq child (if chosen
2405 (widget-create-child-value
2406 widget type value)
2407 (widget-create-child widget type)))
2408 (unless chosen
2409 (widget-apply child :deactivate)))
2411 (error "Unknown escape `%c'" escape)))))
2412 ;; Update properties.
2413 (when chosen
2414 (widget-put widget :choice type))
2415 (when button
2416 (widget-put child :button button)
2417 (widget-put widget :buttons (nconc buttons (list button))))
2418 (when child
2419 (widget-put widget :children (nconc children (list child))))
2420 child)))
2422 (defun widget-radio-value-get (widget)
2423 ;; Get value of the child widget.
2424 (let ((chosen (widget-radio-chosen widget)))
2425 (and chosen (widget-value chosen))))
2427 (defun widget-radio-chosen (widget)
2428 "Return the widget representing the chosen radio button."
2429 (let ((children (widget-get widget :children))
2430 current found)
2431 (while children
2432 (setq current (car children)
2433 children (cdr children))
2434 (when (widget-apply (widget-get current :button) :value-get)
2435 (setq found current
2436 children nil)))
2437 found))
2439 (defun widget-radio-value-inline (widget)
2440 ;; Get value of the child widget.
2441 (let ((children (widget-get widget :children))
2442 current found)
2443 (while children
2444 (setq current (car children)
2445 children (cdr children))
2446 (when (widget-apply (widget-get current :button) :value-get)
2447 (setq found (widget-apply current :value-inline)
2448 children nil)))
2449 found))
2451 (defun widget-radio-value-set (widget value)
2452 ;; We can't just delete and recreate a radio widget, since children
2453 ;; can be added after the original creation and won't be recreated
2454 ;; by `:create'.
2455 (let ((children (widget-get widget :children))
2456 current found)
2457 (while children
2458 (setq current (car children)
2459 children (cdr children))
2460 (let* ((button (widget-get current :button))
2461 (match (and (not found)
2462 (widget-apply current :match value))))
2463 (widget-value-set button match)
2464 (if match
2465 (progn
2466 (widget-value-set current value)
2467 (widget-apply current :activate))
2468 (widget-apply current :deactivate))
2469 (setq found (or found match))))))
2471 (defun widget-radio-validate (widget)
2472 ;; Valid if we have made a valid choice.
2473 (let ((children (widget-get widget :children))
2474 current found button)
2475 (while (and children (not found))
2476 (setq current (car children)
2477 children (cdr children)
2478 button (widget-get current :button)
2479 found (widget-apply button :value-get)))
2480 (if found
2481 (widget-apply current :validate)
2482 widget)))
2484 (defun widget-radio-action (widget child event)
2485 ;; Check if a radio button was pressed.
2486 (let ((children (widget-get widget :children))
2487 (buttons (widget-get widget :buttons))
2488 current)
2489 (when (memq child buttons)
2490 (while children
2491 (setq current (car children)
2492 children (cdr children))
2493 (let* ((button (widget-get current :button)))
2494 (cond ((eq child button)
2495 (widget-value-set button t)
2496 (widget-apply current :activate))
2497 ((widget-value button)
2498 (widget-value-set button nil)
2499 (widget-apply current :deactivate)))))))
2500 ;; Pass notification to parent.
2501 (widget-apply widget :notify child event))
2503 ;;; The `insert-button' Widget.
2505 (define-widget 'insert-button 'push-button
2506 "An insert button for the `editable-list' widget."
2507 :tag "INS"
2508 :help-echo "Insert a new item into the list at this position."
2509 :action 'widget-insert-button-action)
2511 (defun widget-insert-button-action (widget &optional event)
2512 ;; Ask the parent to insert a new item.
2513 (widget-apply (widget-get widget :parent)
2514 :insert-before (widget-get widget :widget)))
2516 ;;; The `delete-button' Widget.
2518 (define-widget 'delete-button 'push-button
2519 "A delete button for the `editable-list' widget."
2520 :tag "DEL"
2521 :help-echo "Delete this item from the list."
2522 :action 'widget-delete-button-action)
2524 (defun widget-delete-button-action (widget &optional event)
2525 ;; Ask the parent to insert a new item.
2526 (widget-apply (widget-get widget :parent)
2527 :delete-at (widget-get widget :widget)))
2529 ;;; The `editable-list' Widget.
2531 ;; (defcustom widget-editable-list-gui nil
2532 ;; "If non nil, use GUI push-buttons in editable list when available."
2533 ;; :type 'boolean
2534 ;; :group 'widgets)
2536 (define-widget 'editable-list 'default
2537 "A variable list of widgets of the same type."
2538 :convert-widget 'widget-types-convert-widget
2539 :copy 'widget-types-copy
2540 :offset 12
2541 :format "%v%i\n"
2542 :format-handler 'widget-editable-list-format-handler
2543 :entry-format "%i %d %v"
2544 :value-create 'widget-editable-list-value-create
2545 :value-get 'widget-editable-list-value-get
2546 :validate 'widget-children-validate
2547 :match 'widget-editable-list-match
2548 :match-inline 'widget-editable-list-match-inline
2549 :insert-before 'widget-editable-list-insert-before
2550 :delete-at 'widget-editable-list-delete-at)
2552 (defun widget-editable-list-format-handler (widget escape)
2553 ;; We recognize the insert button.
2554 ;; (let ((widget-push-button-gui widget-editable-list-gui))
2555 (cond ((eq escape ?i)
2556 (and (widget-get widget :indent)
2557 (insert-char ?\s (widget-get widget :indent)))
2558 (apply 'widget-create-child-and-convert
2559 widget 'insert-button
2560 (widget-get widget :append-button-args)))
2562 (widget-default-format-handler widget escape)))
2563 ;; )
2566 (defun widget-editable-list-value-create (widget)
2567 ;; Insert all values
2568 (let* ((value (widget-get widget :value))
2569 (type (nth 0 (widget-get widget :args)))
2570 children)
2571 (widget-put widget :value-pos (copy-marker (point)))
2572 (set-marker-insertion-type (widget-get widget :value-pos) t)
2573 (while value
2574 (let ((answer (widget-match-inline type value)))
2575 (if answer
2576 (setq children (cons (widget-editable-list-entry-create
2577 widget
2578 (if (widget-get type :inline)
2579 (car answer)
2580 (car (car answer)))
2582 children)
2583 value (cdr answer))
2584 (setq value nil))))
2585 (widget-put widget :children (nreverse children))))
2587 (defun widget-editable-list-value-get (widget)
2588 ;; Get value of the child widget.
2589 (apply 'append (mapcar (lambda (child) (widget-apply child :value-inline))
2590 (widget-get widget :children))))
2592 (defun widget-editable-list-match (widget value)
2593 ;; Value must be a list and all the members must match the type.
2594 (and (listp value)
2595 (null (cdr (widget-editable-list-match-inline widget value)))))
2597 (defun widget-editable-list-match-inline (widget value)
2598 (let ((type (nth 0 (widget-get widget :args)))
2599 (ok t)
2600 found)
2601 (while (and value ok)
2602 (let ((answer (widget-match-inline type value)))
2603 (if answer
2604 (setq found (append found (car answer))
2605 value (cdr answer))
2606 (setq ok nil))))
2607 (cons found value)))
2609 (defun widget-editable-list-insert-before (widget before)
2610 ;; Insert a new child in the list of children.
2611 (save-excursion
2612 (let ((children (widget-get widget :children))
2613 (inhibit-read-only t)
2614 before-change-functions
2615 after-change-functions)
2616 (cond (before
2617 (goto-char (widget-get before :entry-from)))
2619 (goto-char (widget-get widget :value-pos))))
2620 (let ((child (widget-editable-list-entry-create
2621 widget nil nil)))
2622 (when (< (widget-get child :entry-from) (widget-get widget :from))
2623 (set-marker (widget-get widget :from)
2624 (widget-get child :entry-from)))
2625 (if (eq (car children) before)
2626 (widget-put widget :children (cons child children))
2627 (while (not (eq (car (cdr children)) before))
2628 (setq children (cdr children)))
2629 (setcdr children (cons child (cdr children)))))))
2630 (widget-setup)
2631 (widget-apply widget :notify widget))
2633 (defun widget-editable-list-delete-at (widget child)
2634 ;; Delete child from list of children.
2635 (save-excursion
2636 (let ((buttons (copy-sequence (widget-get widget :buttons)))
2637 button
2638 (inhibit-read-only t)
2639 before-change-functions
2640 after-change-functions)
2641 (while buttons
2642 (setq button (car buttons)
2643 buttons (cdr buttons))
2644 (when (eq (widget-get button :widget) child)
2645 (widget-put widget
2646 :buttons (delq button (widget-get widget :buttons)))
2647 (widget-delete button))))
2648 (let ((entry-from (widget-get child :entry-from))
2649 (entry-to (widget-get child :entry-to))
2650 (inhibit-read-only t)
2651 before-change-functions
2652 after-change-functions)
2653 (widget-delete child)
2654 (delete-region entry-from entry-to)
2655 (set-marker entry-from nil)
2656 (set-marker entry-to nil))
2657 (widget-put widget :children (delq child (widget-get widget :children))))
2658 (widget-setup)
2659 (widget-apply widget :notify widget))
2661 (defun widget-editable-list-entry-create (widget value conv)
2662 ;; Create a new entry to the list.
2663 (let ((type (nth 0 (widget-get widget :args)))
2664 ;; (widget-push-button-gui widget-editable-list-gui)
2665 child delete insert)
2666 (widget-specify-insert
2667 (save-excursion
2668 (and (widget-get widget :indent)
2669 (insert-char ?\s (widget-get widget :indent)))
2670 (insert (widget-get widget :entry-format)))
2671 ;; Parse % escapes in format.
2672 (while (re-search-forward "%\\(.\\)" nil t)
2673 (let ((escape (char-after (match-beginning 1))))
2674 (delete-backward-char 2)
2675 (cond ((eq escape ?%)
2676 (insert ?%))
2677 ((eq escape ?i)
2678 (setq insert (apply 'widget-create-child-and-convert
2679 widget 'insert-button
2680 (widget-get widget :insert-button-args))))
2681 ((eq escape ?d)
2682 (setq delete (apply 'widget-create-child-and-convert
2683 widget 'delete-button
2684 (widget-get widget :delete-button-args))))
2685 ((eq escape ?v)
2686 (if conv
2687 (setq child (widget-create-child-value
2688 widget type value))
2689 (setq child (widget-create-child-value
2690 widget type (widget-default-get type)))))
2692 (error "Unknown escape `%c'" escape)))))
2693 (let ((buttons (widget-get widget :buttons)))
2694 (if insert (push insert buttons))
2695 (if delete (push delete buttons))
2696 (widget-put widget :buttons buttons))
2697 (let ((entry-from (point-min-marker))
2698 (entry-to (point-max-marker)))
2699 (set-marker-insertion-type entry-from t)
2700 (set-marker-insertion-type entry-to nil)
2701 (widget-put child :entry-from entry-from)
2702 (widget-put child :entry-to entry-to)))
2703 (if insert (widget-put insert :widget child))
2704 (if delete (widget-put delete :widget child))
2705 child))
2707 ;;; The `group' Widget.
2709 (define-widget 'group 'default
2710 "A widget which groups other widgets inside."
2711 :convert-widget 'widget-types-convert-widget
2712 :copy 'widget-types-copy
2713 :format "%v"
2714 :value-create 'widget-group-value-create
2715 :value-get 'widget-editable-list-value-get
2716 :default-get 'widget-group-default-get
2717 :validate 'widget-children-validate
2718 :match 'widget-group-match
2719 :match-inline 'widget-group-match-inline)
2721 (defun widget-group-value-create (widget)
2722 ;; Create each component.
2723 (let ((args (widget-get widget :args))
2724 (value (widget-get widget :value))
2725 arg answer children)
2726 (while args
2727 (setq arg (car args)
2728 args (cdr args)
2729 answer (widget-match-inline arg value)
2730 value (cdr answer))
2731 (and (eq (preceding-char) ?\n)
2732 (widget-get widget :indent)
2733 (insert-char ?\s (widget-get widget :indent)))
2734 (push (cond ((null answer)
2735 (widget-create-child widget arg))
2736 ((widget-get arg :inline)
2737 (widget-create-child-value widget arg (car answer)))
2739 (widget-create-child-value widget arg (car (car answer)))))
2740 children))
2741 (widget-put widget :children (nreverse children))))
2743 (defun widget-group-default-get (widget)
2744 ;; Get the default of the components.
2745 (mapcar 'widget-default-get (widget-get widget :args)))
2747 (defun widget-group-match (widget values)
2748 ;; Match if the components match.
2749 (and (listp values)
2750 (let ((match (widget-group-match-inline widget values)))
2751 (and match (null (cdr match))))))
2753 (defun widget-group-match-inline (widget vals)
2754 ;; Match if the components match.
2755 (let ((args (widget-get widget :args))
2756 argument answer found)
2757 (while args
2758 (setq argument (car args)
2759 args (cdr args)
2760 answer (widget-match-inline argument vals))
2761 (if answer
2762 (setq vals (cdr answer)
2763 found (append found (car answer)))
2764 (setq vals nil
2765 args nil)))
2766 (if answer
2767 (cons found vals))))
2769 ;;; The `visibility' Widget.
2771 (define-widget 'visibility 'item
2772 "An indicator and manipulator for hidden items."
2773 :format "%[%v%]"
2774 :button-prefix ""
2775 :button-suffix ""
2776 :on "Hide"
2777 :off "Show"
2778 :value-create 'widget-visibility-value-create
2779 :action 'widget-toggle-action
2780 :match (lambda (widget value) t))
2782 (defun widget-visibility-value-create (widget)
2783 ;; Insert text representing the `on' and `off' states.
2784 (let ((on (widget-get widget :on))
2785 (off (widget-get widget :off)))
2786 (if on
2787 (setq on (concat widget-push-button-prefix
2789 widget-push-button-suffix))
2790 (setq on ""))
2791 (if off
2792 (setq off (concat widget-push-button-prefix
2794 widget-push-button-suffix))
2795 (setq off ""))
2796 (if (widget-value widget)
2797 (widget-image-insert widget on "down" "down-pushed")
2798 (widget-image-insert widget off "right" "right-pushed"))))
2800 ;;; The `documentation-link' Widget.
2802 ;; This is a helper widget for `documentation-string'.
2804 (define-widget 'documentation-link 'link
2805 "Link type used in documentation strings."
2806 :tab-order -1
2807 :help-echo "Describe this symbol"
2808 :action 'widget-documentation-link-action)
2810 (defun widget-documentation-link-action (widget &optional event)
2811 "Display documentation for WIDGET's value. Ignore optional argument EVENT."
2812 (let* ((string (widget-get widget :value))
2813 (symbol (intern string)))
2814 (if (and (fboundp symbol) (boundp symbol))
2815 ;; If there are two doc strings, give the user a way to pick one.
2816 (apropos (concat "\\`" (regexp-quote string) "\\'"))
2817 (if (fboundp symbol)
2818 (describe-function symbol)
2819 (describe-variable symbol)))))
2821 (defcustom widget-documentation-links t
2822 "Add hyperlinks to documentation strings when non-nil."
2823 :type 'boolean
2824 :group 'widget-documentation)
2826 (defcustom widget-documentation-link-regexp "`\\([^\n`' ]+\\)'"
2827 "Regexp for matching potential links in documentation strings.
2828 The first group should be the link itself."
2829 :type 'regexp
2830 :group 'widget-documentation)
2832 (defcustom widget-documentation-link-p 'intern-soft
2833 "Predicate used to test if a string is useful as a link.
2834 The value should be a function. The function will be called one
2835 argument, a string, and should return non-nil if there should be a
2836 link for that string."
2837 :type 'function
2838 :options '(widget-documentation-link-p)
2839 :group 'widget-documentation)
2841 (defcustom widget-documentation-link-type 'documentation-link
2842 "Widget type used for links in documentation strings."
2843 :type 'symbol
2844 :group 'widget-documentation)
2846 (defun widget-documentation-link-add (widget from to)
2847 (widget-specify-doc widget from to)
2848 (when widget-documentation-links
2849 (let ((regexp widget-documentation-link-regexp)
2850 (buttons (widget-get widget :buttons))
2851 (widget-mouse-face (default-value 'widget-mouse-face))
2852 (widget-button-face widget-documentation-face)
2853 (widget-button-pressed-face widget-documentation-face))
2854 (save-excursion
2855 (goto-char from)
2856 (while (re-search-forward regexp to t)
2857 (let ((name (match-string 1))
2858 (begin (match-beginning 1))
2859 (end (match-end 1)))
2860 (when (funcall widget-documentation-link-p name)
2861 (push (widget-convert-button widget-documentation-link-type
2862 begin end :value name)
2863 buttons)))))
2864 (widget-put widget :buttons buttons)))
2865 (let ((indent (widget-get widget :indent)))
2866 (when (and indent (not (zerop indent)))
2867 (save-excursion
2868 (save-restriction
2869 (narrow-to-region from to)
2870 (goto-char (point-min))
2871 (while (search-forward "\n" nil t)
2872 (insert-char ?\s indent)))))))
2874 ;;; The `documentation-string' Widget.
2876 (define-widget 'documentation-string 'item
2877 "A documentation string."
2878 :format "%v"
2879 :action 'widget-documentation-string-action
2880 :value-create 'widget-documentation-string-value-create)
2882 (defun widget-documentation-string-value-create (widget)
2883 ;; Insert documentation string.
2884 (let ((doc (widget-value widget))
2885 (indent (widget-get widget :indent))
2886 (shown (widget-get (widget-get widget :parent) :documentation-shown))
2887 (start (point)))
2888 (if (string-match "\n" doc)
2889 (let ((before (substring doc 0 (match-beginning 0)))
2890 (after (substring doc (match-beginning 0)))
2891 button)
2892 (insert before ?\s)
2893 (widget-documentation-link-add widget start (point))
2894 (setq button
2895 (widget-create-child-and-convert
2896 widget 'visibility
2897 :help-echo "Show or hide rest of the documentation."
2898 :on "Hide Rest"
2899 :off "More"
2900 :always-active t
2901 :action 'widget-parent-action
2902 shown))
2903 (when shown
2904 (setq start (point))
2905 (when (and indent (not (zerop indent)))
2906 (insert-char ?\s indent))
2907 (insert after)
2908 (widget-documentation-link-add widget start (point)))
2909 (widget-put widget :buttons (list button)))
2910 (insert doc)
2911 (widget-documentation-link-add widget start (point))))
2912 (insert ?\n))
2914 (defun widget-documentation-string-action (widget &rest ignore)
2915 ;; Toggle documentation.
2916 (let ((parent (widget-get widget :parent)))
2917 (widget-put parent :documentation-shown
2918 (not (widget-get parent :documentation-shown))))
2919 ;; Redraw.
2920 (widget-value-set widget (widget-value widget)))
2922 ;;; The Sexp Widgets.
2924 (define-widget 'const 'item
2925 "An immutable sexp."
2926 :prompt-value 'widget-const-prompt-value
2927 :format "%t\n%d")
2929 (defun widget-const-prompt-value (widget prompt value unbound)
2930 ;; Return the value of the const.
2931 (widget-value widget))
2933 (define-widget 'function-item 'const
2934 "An immutable function name."
2935 :format "%v\n%h"
2936 :documentation-property (lambda (symbol)
2937 (condition-case nil
2938 (documentation symbol t)
2939 (error nil))))
2941 (define-widget 'variable-item 'const
2942 "An immutable variable name."
2943 :format "%v\n%h"
2944 :documentation-property 'variable-documentation)
2946 (define-widget 'other 'sexp
2947 "Matches any value, but doesn't let the user edit the value.
2948 This is useful as last item in a `choice' widget.
2949 You should use this widget type with a default value,
2950 as in (other DEFAULT) or (other :tag \"NAME\" DEFAULT).
2951 If the user selects this alternative, that specifies DEFAULT
2952 as the value."
2953 :tag "Other"
2954 :format "%t%n"
2955 :value 'other)
2957 (defvar widget-string-prompt-value-history nil
2958 "History of input to `widget-string-prompt-value'.")
2960 (define-widget 'string 'editable-field
2961 "A string"
2962 :tag "String"
2963 :format "%{%t%}: %v"
2964 :complete-function 'ispell-complete-word
2965 :prompt-history 'widget-string-prompt-value-history)
2967 (define-widget 'regexp 'string
2968 "A regular expression."
2969 :match 'widget-regexp-match
2970 :validate 'widget-regexp-validate
2971 ;; Doesn't work well with terminating newline.
2972 ;; :value-face 'widget-single-line-field
2973 :tag "Regexp")
2975 (defun widget-regexp-match (widget value)
2976 ;; Match valid regexps.
2977 (and (stringp value)
2978 (condition-case nil
2979 (prog1 t
2980 (string-match value ""))
2981 (error nil))))
2983 (defun widget-regexp-validate (widget)
2984 "Check that the value of WIDGET is a valid regexp."
2985 (condition-case data
2986 (prog1 nil
2987 (string-match (widget-value widget) ""))
2988 (error (widget-put widget :error (error-message-string data))
2989 widget)))
2991 (define-widget 'file 'string
2992 "A file widget.
2993 It will read a file name from the minibuffer when invoked."
2994 :complete-function 'widget-file-complete
2995 :prompt-value 'widget-file-prompt-value
2996 :format "%{%t%}: %v"
2997 ;; Doesn't work well with terminating newline.
2998 ;; :value-face 'widget-single-line-field
2999 :tag "File")
3001 (defun widget-file-complete ()
3002 "Perform completion on file name preceding point."
3003 (interactive)
3004 (let* ((end (point))
3005 (beg (widget-field-start widget))
3006 (pattern (buffer-substring beg end))
3007 (name-part (file-name-nondirectory pattern))
3008 ;; I think defaulting to root is right
3009 ;; because these really should be absolute file names.
3010 (directory (or (file-name-directory pattern) "/"))
3011 (completion (file-name-completion name-part directory)))
3012 (cond ((eq completion t))
3013 ((null completion)
3014 (message "Can't find completion for \"%s\"" pattern)
3015 (ding))
3016 ((not (string= name-part completion))
3017 (delete-region beg end)
3018 (insert (expand-file-name completion directory)))
3020 (message "Making completion list...")
3021 (with-output-to-temp-buffer "*Completions*"
3022 (display-completion-list
3023 (sort (file-name-all-completions name-part directory)
3024 'string<)
3025 name-part))
3026 (message "Making completion list...%s" "done")))))
3028 (defun widget-file-prompt-value (widget prompt value unbound)
3029 ;; Read file from minibuffer.
3030 (abbreviate-file-name
3031 (if unbound
3032 (read-file-name prompt)
3033 (let ((prompt2 (format "%s (default %s): " prompt value))
3034 (dir (file-name-directory value))
3035 (file (file-name-nondirectory value))
3036 (must-match (widget-get widget :must-match)))
3037 (read-file-name prompt2 dir nil must-match file)))))
3039 ;;;(defun widget-file-action (widget &optional event)
3040 ;;; ;; Read a file name from the minibuffer.
3041 ;;; (let* ((value (widget-value widget))
3042 ;;; (dir (file-name-directory value))
3043 ;;; (file (file-name-nondirectory value))
3044 ;;; (menu-tag (widget-apply widget :menu-tag-get))
3045 ;;; (must-match (widget-get widget :must-match))
3046 ;;; (answer (read-file-name (concat menu-tag " (default " value "): ")
3047 ;;; dir nil must-match file)))
3048 ;;; (widget-value-set widget (abbreviate-file-name answer))
3049 ;;; (widget-setup)
3050 ;;; (widget-apply widget :notify widget event)))
3052 ;; Fixme: use file-name-as-directory.
3053 (define-widget 'directory 'file
3054 "A directory widget.
3055 It will read a directory name from the minibuffer when invoked."
3056 :tag "Directory")
3058 (defvar widget-symbol-prompt-value-history nil
3059 "History of input to `widget-symbol-prompt-value'.")
3061 (define-widget 'symbol 'editable-field
3062 "A Lisp symbol."
3063 :value nil
3064 :tag "Symbol"
3065 :format "%{%t%}: %v"
3066 :match (lambda (widget value) (symbolp value))
3067 :complete-function 'lisp-complete-symbol
3068 :prompt-internal 'widget-symbol-prompt-internal
3069 :prompt-match 'symbolp
3070 :prompt-history 'widget-symbol-prompt-value-history
3071 :value-to-internal (lambda (widget value)
3072 (if (symbolp value)
3073 (symbol-name value)
3074 value))
3075 :value-to-external (lambda (widget value)
3076 (if (stringp value)
3077 (intern value)
3078 value)))
3080 (defun widget-symbol-prompt-internal (widget prompt initial history)
3081 ;; Read file from minibuffer.
3082 (let ((answer (completing-read prompt obarray
3083 (widget-get widget :prompt-match)
3084 nil initial history)))
3085 (if (and (stringp answer)
3086 (not (zerop (length answer))))
3087 answer
3088 (error "No value"))))
3090 (defvar widget-function-prompt-value-history nil
3091 "History of input to `widget-function-prompt-value'.")
3093 (define-widget 'function 'restricted-sexp
3094 "A Lisp function."
3095 :complete-function (lambda ()
3096 (interactive)
3097 (lisp-complete-symbol 'fboundp))
3098 :prompt-value 'widget-field-prompt-value
3099 :prompt-internal 'widget-symbol-prompt-internal
3100 :prompt-match 'fboundp
3101 :prompt-history 'widget-function-prompt-value-history
3102 :action 'widget-field-action
3103 :match-alternatives '(functionp)
3104 :validate (lambda (widget)
3105 (unless (functionp (widget-value widget))
3106 (widget-put widget :error (format "Invalid function: %S"
3107 (widget-value widget)))
3108 widget))
3109 :value 'ignore
3110 :tag "Function")
3112 (defvar widget-variable-prompt-value-history nil
3113 "History of input to `widget-variable-prompt-value'.")
3115 (define-widget 'variable 'symbol
3116 "A Lisp variable."
3117 :prompt-match 'boundp
3118 :prompt-history 'widget-variable-prompt-value-history
3119 :complete-function (lambda ()
3120 (interactive)
3121 (lisp-complete-symbol 'boundp))
3122 :tag "Variable")
3124 (defvar widget-coding-system-prompt-value-history nil
3125 "History of input to `widget-coding-system-prompt-value'.")
3127 (define-widget 'coding-system 'symbol
3128 "A MULE coding-system."
3129 :format "%{%t%}: %v"
3130 :tag "Coding system"
3131 :base-only nil
3132 :prompt-history 'widget-coding-system-prompt-value-history
3133 :prompt-value 'widget-coding-system-prompt-value
3134 :action 'widget-coding-system-action
3135 :complete-function (lambda ()
3136 (interactive)
3137 (lisp-complete-symbol 'coding-system-p))
3138 :validate (lambda (widget)
3139 (unless (coding-system-p (widget-value widget))
3140 (widget-put widget :error (format "Invalid coding system: %S"
3141 (widget-value widget)))
3142 widget))
3143 :value 'undecided
3144 :prompt-match 'coding-system-p)
3146 (defun widget-coding-system-prompt-value (widget prompt value unbound)
3147 "Read coding-system from minibuffer."
3148 (if (widget-get widget :base-only)
3149 (intern
3150 (completing-read (format "%s (default %s): " prompt value)
3151 (mapcar #'list (coding-system-list t)) nil nil nil
3152 coding-system-history))
3153 (read-coding-system (format "%s (default %s): " prompt value) value)))
3155 (defun widget-coding-system-action (widget &optional event)
3156 (let ((answer
3157 (widget-coding-system-prompt-value
3158 widget
3159 (widget-apply widget :menu-tag-get)
3160 (widget-value widget)
3161 t)))
3162 (widget-value-set widget answer)
3163 (widget-apply widget :notify widget event)
3164 (widget-setup)))
3166 (define-widget 'sexp 'editable-field
3167 "An arbitrary Lisp expression."
3168 :tag "Lisp expression"
3169 :format "%{%t%}: %v"
3170 :value nil
3171 :validate 'widget-sexp-validate
3172 :match (lambda (widget value) t)
3173 :value-to-internal 'widget-sexp-value-to-internal
3174 :value-to-external (lambda (widget value) (read value))
3175 :prompt-history 'widget-sexp-prompt-value-history
3176 :prompt-value 'widget-sexp-prompt-value)
3178 (defun widget-sexp-value-to-internal (widget value)
3179 ;; Use pp for printer representation.
3180 (let ((pp (if (symbolp value)
3181 (prin1-to-string value)
3182 (pp-to-string value))))
3183 (while (string-match "\n\\'" pp)
3184 (setq pp (substring pp 0 -1)))
3185 (if (or (string-match "\n\\'" pp)
3186 (> (length pp) 40))
3187 (concat "\n" pp)
3188 pp)))
3190 (defun widget-sexp-validate (widget)
3191 ;; Valid if we can read the string and there is no junk left after it.
3192 (with-temp-buffer
3193 (insert (widget-apply widget :value-get))
3194 (goto-char (point-min))
3195 (let (err)
3196 (condition-case data
3197 (progn
3198 ;; Avoid a confusing end-of-file error.
3199 (skip-syntax-forward "\\s-")
3200 (if (eobp)
3201 (setq err "Empty sexp -- use `nil'?")
3202 (unless (widget-apply widget :match (read (current-buffer)))
3203 (setq err (widget-get widget :type-error))))
3204 ;; Allow whitespace after expression.
3205 (skip-syntax-forward "\\s-")
3206 (if (and (not (eobp))
3207 (not err))
3208 (setq err (format "Junk at end of expression: %s"
3209 (buffer-substring (point)
3210 (point-max))))))
3211 (end-of-file ; Avoid confusing error message.
3212 (setq err "Unbalanced sexp"))
3213 (error (setq err (error-message-string data))))
3214 (if (not err)
3216 (widget-put widget :error err)
3217 widget))))
3219 (defvar widget-sexp-prompt-value-history nil
3220 "History of input to `widget-sexp-prompt-value'.")
3222 (defun widget-sexp-prompt-value (widget prompt value unbound)
3223 ;; Read an arbitrary sexp.
3224 (let ((found (read-string prompt
3225 (if unbound nil (cons (prin1-to-string value) 0))
3226 (widget-get widget :prompt-history))))
3227 (let ((answer (read-from-string found)))
3228 (unless (= (cdr answer) (length found))
3229 (error "Junk at end of expression: %s"
3230 (substring found (cdr answer))))
3231 (car answer))))
3233 (define-widget 'restricted-sexp 'sexp
3234 "A Lisp expression restricted to values that match.
3235 To use this type, you must define :match or :match-alternatives."
3236 :type-error "The specified value is not valid"
3237 :match 'widget-restricted-sexp-match
3238 :value-to-internal (lambda (widget value)
3239 (if (widget-apply widget :match value)
3240 (prin1-to-string value)
3241 value)))
3243 (defun widget-restricted-sexp-match (widget value)
3244 (let ((alternatives (widget-get widget :match-alternatives))
3245 matched)
3246 (while (and alternatives (not matched))
3247 (if (cond ((functionp (car alternatives))
3248 (funcall (car alternatives) value))
3249 ((and (consp (car alternatives))
3250 (eq (car (car alternatives)) 'quote))
3251 (eq value (nth 1 (car alternatives)))))
3252 (setq matched t))
3253 (setq alternatives (cdr alternatives)))
3254 matched))
3256 (define-widget 'integer 'restricted-sexp
3257 "An integer."
3258 :tag "Integer"
3259 :value 0
3260 :type-error "This field should contain an integer"
3261 :match-alternatives '(integerp))
3263 (define-widget 'number 'restricted-sexp
3264 "A number (floating point or integer)."
3265 :tag "Number"
3266 :value 0.0
3267 :type-error "This field should contain a number (floating point or integer)"
3268 :match-alternatives '(numberp))
3270 (define-widget 'float 'restricted-sexp
3271 "A floating point number."
3272 :tag "Floating point number"
3273 :value 0.0
3274 :type-error "This field should contain a floating point number"
3275 :match-alternatives '(floatp))
3277 (define-widget 'character 'editable-field
3278 "A character."
3279 :tag "Character"
3280 :value 0
3281 :size 1
3282 :format "%{%t%}: %v\n"
3283 :valid-regexp "\\`.\\'"
3284 :error "This field should contain a single character"
3285 :value-to-internal (lambda (widget value)
3286 (if (stringp value)
3287 value
3288 (char-to-string value)))
3289 :value-to-external (lambda (widget value)
3290 (if (stringp value)
3291 (aref value 0)
3292 value))
3293 :match (lambda (widget value)
3294 (char-valid-p value)))
3296 (define-widget 'list 'group
3297 "A Lisp list."
3298 :tag "List"
3299 :format "%{%t%}:\n%v")
3301 (define-widget 'vector 'group
3302 "A Lisp vector."
3303 :tag "Vector"
3304 :format "%{%t%}:\n%v"
3305 :match 'widget-vector-match
3306 :value-to-internal (lambda (widget value) (append value nil))
3307 :value-to-external (lambda (widget value) (apply 'vector value)))
3309 (defun widget-vector-match (widget value)
3310 (and (vectorp value)
3311 (widget-group-match widget
3312 (widget-apply widget :value-to-internal value))))
3314 (define-widget 'cons 'group
3315 "A cons-cell."
3316 :tag "Cons-cell"
3317 :format "%{%t%}:\n%v"
3318 :match 'widget-cons-match
3319 :value-to-internal (lambda (widget value)
3320 (list (car value) (cdr value)))
3321 :value-to-external (lambda (widget value)
3322 (apply 'cons value)))
3324 (defun widget-cons-match (widget value)
3325 (and (consp value)
3326 (widget-group-match widget
3327 (widget-apply widget :value-to-internal value))))
3329 ;;; The `lazy' Widget.
3331 ;; Recursive datatypes.
3333 (define-widget 'lazy 'default
3334 "Base widget for recursive datastructures.
3336 The `lazy' widget will, when instantiated, contain a single inferior
3337 widget, of the widget type specified by the :type parameter. The
3338 value of the `lazy' widget is the same as the value of the inferior
3339 widget. When deriving a new widget from the 'lazy' widget, the :type
3340 parameter is allowed to refer to the widget currently being defined,
3341 thus allowing recursive datastructures to be described.
3343 The :type parameter takes the same arguments as the defcustom
3344 parameter with the same name.
3346 Most composite widgets, i.e. widgets containing other widgets, does
3347 not allow recursion. That is, when you define a new widget type, none
3348 of the inferior widgets may be of the same type you are currently
3349 defining.
3351 In Lisp, however, it is custom to define datastructures in terms of
3352 themselves. A list, for example, is defined as either nil, or a cons
3353 cell whose cdr itself is a list. The obvious way to translate this
3354 into a widget type would be
3356 (define-widget 'my-list 'choice
3357 \"A list of sexps.\"
3358 :tag \"Sexp list\"
3359 :args '((const nil) (cons :value (nil) sexp my-list)))
3361 Here we attempt to define my-list as a choice of either the constant
3362 nil, or a cons-cell containing a sexp and my-lisp. This will not work
3363 because the `choice' widget does not allow recursion.
3365 Using the `lazy' widget you can overcome this problem, as in this
3366 example:
3368 (define-widget 'sexp-list 'lazy
3369 \"A list of sexps.\"
3370 :tag \"Sexp list\"
3371 :type '(choice (const nil) (cons :value (nil) sexp sexp-list)))"
3372 :format "%{%t%}: %v"
3373 ;; We don't convert :type because we want to allow recursive
3374 ;; datastructures. This is slow, so we should not create speed
3375 ;; critical widgets by deriving from this.
3376 :convert-widget 'widget-value-convert-widget
3377 :value-create 'widget-type-value-create
3378 :value-get 'widget-child-value-get
3379 :value-inline 'widget-child-value-inline
3380 :default-get 'widget-type-default-get
3381 :match 'widget-type-match
3382 :validate 'widget-child-validate)
3385 ;;; The `plist' Widget.
3387 ;; Property lists.
3389 (define-widget 'plist 'list
3390 "A property list."
3391 :key-type '(symbol :tag "Key")
3392 :value-type '(sexp :tag "Value")
3393 :convert-widget 'widget-plist-convert-widget
3394 :tag "Plist")
3396 (defvar widget-plist-value-type) ;Dynamic variable
3398 (defun widget-plist-convert-widget (widget)
3399 ;; Handle `:options'.
3400 (let* ((options (widget-get widget :options))
3401 (widget-plist-value-type (widget-get widget :value-type))
3402 (other `(editable-list :inline t
3403 (group :inline t
3404 ,(widget-get widget :key-type)
3405 ,widget-plist-value-type)))
3406 (args (if options
3407 (list `(checklist :inline t
3408 :greedy t
3409 ,@(mapcar 'widget-plist-convert-option
3410 options))
3411 other)
3412 (list other))))
3413 (widget-put widget :args args)
3414 widget))
3416 (defun widget-plist-convert-option (option)
3417 ;; Convert a single plist option.
3418 (let (key-type value-type)
3419 (if (listp option)
3420 (let ((key (nth 0 option)))
3421 (setq value-type (nth 1 option))
3422 (if (listp key)
3423 (setq key-type key)
3424 (setq key-type `(const ,key))))
3425 (setq key-type `(const ,option)
3426 value-type widget-plist-value-type))
3427 `(group :format "Key: %v" :inline t ,key-type ,value-type)))
3430 ;;; The `alist' Widget.
3432 ;; Association lists.
3434 (define-widget 'alist 'list
3435 "An association list."
3436 :key-type '(sexp :tag "Key")
3437 :value-type '(sexp :tag "Value")
3438 :convert-widget 'widget-alist-convert-widget
3439 :tag "Alist")
3441 (defvar widget-alist-value-type) ;Dynamic variable
3443 (defun widget-alist-convert-widget (widget)
3444 ;; Handle `:options'.
3445 (let* ((options (widget-get widget :options))
3446 (widget-alist-value-type (widget-get widget :value-type))
3447 (other `(editable-list :inline t
3448 (cons :format "%v"
3449 ,(widget-get widget :key-type)
3450 ,widget-alist-value-type)))
3451 (args (if options
3452 (list `(checklist :inline t
3453 :greedy t
3454 ,@(mapcar 'widget-alist-convert-option
3455 options))
3456 other)
3457 (list other))))
3458 (widget-put widget :args args)
3459 widget))
3461 (defun widget-alist-convert-option (option)
3462 ;; Convert a single alist option.
3463 (let (key-type value-type)
3464 (if (listp option)
3465 (let ((key (nth 0 option)))
3466 (setq value-type (nth 1 option))
3467 (if (listp key)
3468 (setq key-type key)
3469 (setq key-type `(const ,key))))
3470 (setq key-type `(const ,option)
3471 value-type widget-alist-value-type))
3472 `(cons :format "Key: %v" ,key-type ,value-type)))
3474 (define-widget 'choice 'menu-choice
3475 "A union of several sexp types."
3476 :tag "Choice"
3477 :format "%{%t%}: %[Value Menu%] %v"
3478 :button-prefix 'widget-push-button-prefix
3479 :button-suffix 'widget-push-button-suffix
3480 :prompt-value 'widget-choice-prompt-value)
3482 (defun widget-choice-prompt-value (widget prompt value unbound)
3483 "Make a choice."
3484 (let ((args (widget-get widget :args))
3485 (completion-ignore-case (widget-get widget :case-fold))
3486 current choices old)
3487 ;; Find the first arg that matches VALUE.
3488 (let ((look args))
3489 (while look
3490 (if (widget-apply (car look) :match value)
3491 (setq old (car look)
3492 look nil)
3493 (setq look (cdr look)))))
3494 ;; Find new choice.
3495 (setq current
3496 (cond ((= (length args) 0)
3497 nil)
3498 ((= (length args) 1)
3499 (nth 0 args))
3500 ((and (= (length args) 2)
3501 (memq old args))
3502 (if (eq old (nth 0 args))
3503 (nth 1 args)
3504 (nth 0 args)))
3506 (while args
3507 (setq current (car args)
3508 args (cdr args))
3509 (setq choices
3510 (cons (cons (widget-apply current :menu-tag-get)
3511 current)
3512 choices)))
3513 (let ((val (completing-read prompt choices nil t)))
3514 (if (stringp val)
3515 (let ((try (try-completion val choices)))
3516 (when (stringp try)
3517 (setq val try))
3518 (cdr (assoc val choices)))
3519 nil)))))
3520 (if current
3521 (widget-prompt-value current prompt nil t)
3522 value)))
3524 (define-widget 'radio 'radio-button-choice
3525 "A union of several sexp types."
3526 :tag "Choice"
3527 :format "%{%t%}:\n%v"
3528 :prompt-value 'widget-choice-prompt-value)
3530 (define-widget 'repeat 'editable-list
3531 "A variable length homogeneous list."
3532 :tag "Repeat"
3533 :format "%{%t%}:\n%v%i\n")
3535 (define-widget 'set 'checklist
3536 "A list of members from a fixed set."
3537 :tag "Set"
3538 :format "%{%t%}:\n%v")
3540 (define-widget 'boolean 'toggle
3541 "To be nil or non-nil, that is the question."
3542 :tag "Boolean"
3543 :prompt-value 'widget-boolean-prompt-value
3544 :button-prefix 'widget-push-button-prefix
3545 :button-suffix 'widget-push-button-suffix
3546 :format "%{%t%}: %[Toggle%] %v\n"
3547 :on "on (non-nil)"
3548 :off "off (nil)")
3550 (defun widget-boolean-prompt-value (widget prompt value unbound)
3551 ;; Toggle a boolean.
3552 (y-or-n-p prompt))
3554 ;;; The `color' Widget.
3556 ;; Fixme: match
3557 (define-widget 'color 'editable-field
3558 "Choose a color name (with sample)."
3559 :format "%t: %v (%{sample%})\n"
3560 :size 10
3561 :tag "Color"
3562 :value "black"
3563 :complete 'widget-color-complete
3564 :sample-face-get 'widget-color-sample-face-get
3565 :notify 'widget-color-notify
3566 :action 'widget-color-action)
3568 (defun widget-color-complete (widget)
3569 "Complete the color in WIDGET."
3570 (require 'facemenu) ; for facemenu-color-alist
3571 (let* ((prefix (buffer-substring-no-properties (widget-field-start widget)
3572 (point)))
3573 (list (or facemenu-color-alist (defined-colors)))
3574 (completion (try-completion prefix list)))
3575 (cond ((eq completion t)
3576 (message "Exact match."))
3577 ((null completion)
3578 (error "Can't find completion for \"%s\"" prefix))
3579 ((not (string-equal prefix completion))
3580 (insert-and-inherit (substring completion (length prefix))))
3582 (message "Making completion list...")
3583 (with-output-to-temp-buffer "*Completions*"
3584 (display-completion-list (all-completions prefix list nil)
3585 prefix))
3586 (message "Making completion list...done")))))
3588 (defun widget-color-sample-face-get (widget)
3589 (let* ((value (condition-case nil
3590 (widget-value widget)
3591 (error (widget-get widget :value)))))
3592 (if (color-defined-p value)
3593 (list (cons 'foreground-color value))
3594 'default)))
3596 (defun widget-color-action (widget &optional event)
3597 "Prompt for a color."
3598 (let* ((tag (widget-apply widget :menu-tag-get))
3599 (prompt (concat tag ": "))
3600 (value (widget-value widget))
3601 (start (widget-field-start widget))
3602 (answer (facemenu-read-color prompt)))
3603 (unless (zerop (length answer))
3604 (widget-value-set widget answer)
3605 (widget-setup)
3606 (widget-apply widget :notify widget event))))
3608 (defun widget-color-notify (widget child &optional event)
3609 "Update the sample, and notify the parent."
3610 (overlay-put (widget-get widget :sample-overlay)
3611 'face (widget-apply widget :sample-face-get))
3612 (widget-default-notify widget child event))
3614 ;;; The Help Echo
3616 (defun widget-echo-help (pos)
3617 "Display help-echo text for widget at POS."
3618 (let* ((widget (widget-at pos))
3619 (help-echo (and widget (widget-get widget :help-echo))))
3620 (if (functionp help-echo)
3621 (setq help-echo (funcall help-echo widget)))
3622 (if help-echo (message "%s" (eval help-echo)))))
3624 ;;; The End:
3626 (provide 'wid-edit)
3628 ;;; arch-tag: a076e75e-18a1-4b46-8be5-3f317bcbc707
3629 ;;; wid-edit.el ends here