(tramp-completion-mode): defvar moved up.
[emacs.git] / lisp / wid-edit.el
blob064725c8bfe2c5d7e112c87f111917b6ea3224e5
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 (overlay-put overlay 'mouse-face widget-mouse-face))
407 (overlay-put overlay 'pointer 'hand)
408 (overlay-put overlay 'follow-link follow-link)
409 (overlay-put overlay 'help-echo help-echo)))
411 (defun widget-mouse-help (window overlay point)
412 "Help-echo callback for widgets whose :help-echo is a function."
413 (with-current-buffer (overlay-buffer overlay)
414 (let* ((widget (widget-at (overlay-start overlay)))
415 (help-echo (if widget (widget-get widget :help-echo))))
416 (if (functionp help-echo)
417 (funcall help-echo widget)
418 help-echo))))
420 (defun widget-specify-sample (widget from to)
421 "Specify sample for WIDGET between FROM and TO."
422 (let ((overlay (make-overlay from to nil t nil)))
423 (overlay-put overlay 'face (widget-apply widget :sample-face-get))
424 (overlay-put overlay 'evaporate t)
425 (widget-put widget :sample-overlay overlay)))
427 (defun widget-specify-doc (widget from to)
428 "Specify documentation for WIDGET between FROM and TO."
429 (let ((overlay (make-overlay from to nil t nil)))
430 (overlay-put overlay 'widget-doc widget)
431 (overlay-put overlay 'face widget-documentation-face)
432 (overlay-put overlay 'evaporate t)
433 (widget-put widget :doc-overlay overlay)))
435 (defmacro widget-specify-insert (&rest form)
436 "Execute FORM without inheriting any text properties."
437 `(save-restriction
438 (let ((inhibit-read-only t)
439 (inhibit-modification-hooks t))
440 (narrow-to-region (point) (point))
441 (prog1 (progn ,@form)
442 (goto-char (point-max))))))
444 (defface widget-inactive
445 '((t :inherit shadow))
446 "Face used for inactive widgets."
447 :group 'widget-faces)
448 ;; backward-compatibility alias
449 (put 'widget-inactive-face 'face-alias 'widget-inactive)
451 (defun widget-specify-inactive (widget from to)
452 "Make WIDGET inactive for user modifications."
453 (unless (widget-get widget :inactive)
454 (let ((overlay (make-overlay from to nil t nil)))
455 (overlay-put overlay 'face 'widget-inactive)
456 ;; This is disabled, as it makes the mouse cursor change shape.
457 ;; (overlay-put overlay 'mouse-face 'widget-inactive)
458 (overlay-put overlay 'evaporate t)
459 (overlay-put overlay 'priority 100)
460 (overlay-put overlay 'modification-hooks '(widget-overlay-inactive))
461 (widget-put widget :inactive overlay))))
463 (defun widget-overlay-inactive (&rest junk)
464 "Ignoring the arguments, signal an error."
465 (unless inhibit-read-only
466 (error "The widget here is not active")))
469 (defun widget-specify-active (widget)
470 "Make WIDGET active for user modifications."
471 (let ((inactive (widget-get widget :inactive)))
472 (when inactive
473 (delete-overlay inactive)
474 (widget-put widget :inactive nil))))
476 ;;; Widget Properties.
478 (defsubst widget-type (widget)
479 "Return the type of WIDGET, a symbol."
480 (car widget))
482 ;;;###autoload
483 (defun widgetp (widget)
484 "Return non-nil iff WIDGET is a widget."
485 (if (symbolp widget)
486 (get widget 'widget-type)
487 (and (consp widget)
488 (symbolp (car widget))
489 (get (car widget) 'widget-type))))
491 (defun widget-get-indirect (widget property)
492 "In WIDGET, get the value of PROPERTY.
493 If the value is a symbol, return its binding.
494 Otherwise, just return the value."
495 (let ((value (widget-get widget property)))
496 (if (symbolp value)
497 (symbol-value value)
498 value)))
500 (defun widget-member (widget property)
501 "Non-nil iff there is a definition in WIDGET for PROPERTY."
502 (cond ((plist-member (cdr widget) property)
504 ((car widget)
505 (widget-member (get (car widget) 'widget-type) property))
506 (t nil)))
508 (defun widget-value (widget)
509 "Extract the current value of WIDGET."
510 (widget-apply widget
511 :value-to-external (widget-apply widget :value-get)))
513 (defun widget-value-set (widget value)
514 "Set the current value of WIDGET to VALUE."
515 (widget-apply widget
516 :value-set (widget-apply widget
517 :value-to-internal value)))
519 (defun widget-default-get (widget)
520 "Extract the default external value of WIDGET."
521 (widget-apply widget :value-to-external
522 (or (widget-get widget :value)
523 (widget-apply widget :default-get))))
525 (defun widget-match-inline (widget vals)
526 "In WIDGET, match the start of VALS."
527 (cond ((widget-get widget :inline)
528 (widget-apply widget :match-inline vals))
529 ((and (listp vals)
530 (widget-apply widget :match (car vals)))
531 (cons (list (car vals)) (cdr vals)))
532 (t nil)))
534 (defun widget-apply-action (widget &optional event)
535 "Apply :action in WIDGET in response to EVENT."
536 (if (widget-apply widget :active)
537 (widget-apply widget :action event)
538 (error "Attempt to perform action on inactive widget")))
540 ;;; Helper functions.
542 ;; These are widget specific.
544 ;;;###autoload
545 (defun widget-prompt-value (widget prompt &optional value unbound)
546 "Prompt for a value matching WIDGET, using PROMPT.
547 The current value is assumed to be VALUE, unless UNBOUND is non-nil."
548 (unless (listp widget)
549 (setq widget (list widget)))
550 (setq prompt (format "[%s] %s" (widget-type widget) prompt))
551 (setq widget (widget-convert widget))
552 (let ((answer (widget-apply widget :prompt-value prompt value unbound)))
553 (unless (widget-apply widget :match answer)
554 (error "Value does not match %S type" (car widget)))
555 answer))
557 (defun widget-get-sibling (widget)
558 "Get the item WIDGET is assumed to toggle.
559 This is only meaningful for radio buttons or checkboxes in a list."
560 (let* ((children (widget-get (widget-get widget :parent) :children))
561 child)
562 (catch 'child
563 (while children
564 (setq child (car children)
565 children (cdr children))
566 (when (eq (widget-get child :button) widget)
567 (throw 'child child)))
568 nil)))
570 (defun widget-map-buttons (function &optional buffer maparg)
571 "Map FUNCTION over the buttons in BUFFER.
572 FUNCTION is called with the arguments WIDGET and MAPARG.
574 If FUNCTION returns non-nil, the walk is cancelled.
576 The arguments MAPARG, and BUFFER default to nil and (current-buffer),
577 respectively."
578 (let ((cur (point-min))
579 (widget nil)
580 (overlays (if buffer
581 (with-current-buffer buffer (overlay-lists))
582 (overlay-lists))))
583 (setq overlays (append (car overlays) (cdr overlays)))
584 (while (setq cur (pop overlays))
585 (setq widget (overlay-get cur 'button))
586 (if (and widget (funcall function widget maparg))
587 (setq overlays nil)))))
589 ;;; Images.
591 (defcustom widget-image-directory (file-name-as-directory
592 (expand-file-name "custom" data-directory))
593 "Where widget button images are located.
594 If this variable is nil, widget will try to locate the directory
595 automatically."
596 :group 'widgets
597 :type 'directory)
599 (defcustom widget-image-enable t
600 "If non nil, use image buttons in widgets when available."
601 :version "21.1"
602 :group 'widgets
603 :type 'boolean)
605 (defcustom widget-image-conversion
606 '((xpm ".xpm") (gif ".gif") (png ".png") (jpeg ".jpg" ".jpeg")
607 (xbm ".xbm"))
608 "Conversion alist from image formats to file name suffixes."
609 :group 'widgets
610 :type '(repeat (cons :format "%v"
611 (symbol :tag "Image Format" unknown)
612 (repeat :tag "Suffixes"
613 (string :format "%v")))))
615 (defun widget-image-find (image)
616 "Create a graphical button from IMAGE.
617 IMAGE should either already be an image, or be a file name sans
618 extension (xpm, xbm, gif, jpg, or png) located in
619 `widget-image-directory' or otherwise where `find-image' will find it."
620 (cond ((not (and image widget-image-enable (display-graphic-p)))
621 ;; We don't want or can't use images.
622 nil)
623 ((and (consp image)
624 (eq 'image (car image)))
625 ;; Already an image spec. Use it.
626 image)
627 ((stringp image)
628 ;; A string. Look it up in relevant directories.
629 (let* ((load-path (cons widget-image-directory load-path))
630 specs)
631 (dolist (elt widget-image-conversion)
632 (dolist (ext (cdr elt))
633 (push (list :type (car elt) :file (concat image ext)) specs)))
634 (setq specs (nreverse specs))
635 (find-image specs)))
637 ;; Oh well.
638 nil)))
640 (defvar widget-button-pressed-face 'widget-button-pressed
641 "Face used for pressed buttons in widgets.
642 This exists as a variable so it can be set locally in certain
643 buffers.")
645 (defun widget-image-insert (widget tag image &optional down inactive)
646 "In WIDGET, insert the text TAG or, if supported, IMAGE.
647 IMAGE should either be an image or an image file name sans extension
648 \(xpm, xbm, gif, jpg, or png) located in `widget-image-directory'.
650 Optional arguments DOWN and INACTIVE are used instead of IMAGE when the
651 button is pressed or inactive, respectively. These are currently ignored."
652 (if (and (display-graphic-p)
653 (setq image (widget-image-find image)))
654 (progn (widget-put widget :suppress-face t)
655 (insert-image image
656 (propertize
657 tag 'mouse-face widget-button-pressed-face)))
658 (insert tag)))
660 (defun widget-move-and-invoke (event)
661 "Move to where you click, and if it is an active field, invoke it."
662 (interactive "e")
663 (mouse-set-point event)
664 (let ((pos (widget-event-point event)))
665 (if (and pos (get-char-property pos 'button))
666 (widget-button-click event))))
668 ;;; Buttons.
670 (defgroup widget-button nil
671 "The look of various kinds of buttons."
672 :group 'widgets)
674 (defcustom widget-button-prefix ""
675 "String used as prefix for buttons."
676 :type 'string
677 :group 'widget-button)
679 (defcustom widget-button-suffix ""
680 "String used as suffix for buttons."
681 :type 'string
682 :group 'widget-button)
684 ;;; Creating Widgets.
686 ;;;###autoload
687 (defun widget-create (type &rest args)
688 "Create widget of TYPE.
689 The optional ARGS are additional keyword arguments."
690 (let ((widget (apply 'widget-convert type args)))
691 (widget-apply widget :create)
692 widget))
694 (defun widget-create-child-and-convert (parent type &rest args)
695 "As part of the widget PARENT, create a child widget TYPE.
696 The child is converted, using the keyword arguments ARGS."
697 (let ((widget (apply 'widget-convert type args)))
698 (widget-put widget :parent parent)
699 (unless (widget-get widget :indent)
700 (widget-put widget :indent (+ (or (widget-get parent :indent) 0)
701 (or (widget-get widget :extra-offset) 0)
702 (widget-get parent :offset))))
703 (widget-apply widget :create)
704 widget))
706 (defun widget-create-child (parent type)
707 "Create widget of TYPE."
708 (let ((widget (widget-copy type)))
709 (widget-put widget :parent parent)
710 (unless (widget-get widget :indent)
711 (widget-put widget :indent (+ (or (widget-get parent :indent) 0)
712 (or (widget-get widget :extra-offset) 0)
713 (widget-get parent :offset))))
714 (widget-apply widget :create)
715 widget))
717 (defun widget-create-child-value (parent type value)
718 "Create widget of TYPE with value VALUE."
719 (let ((widget (widget-copy type)))
720 (widget-put widget :value (widget-apply widget :value-to-internal value))
721 (widget-put widget :parent parent)
722 (unless (widget-get widget :indent)
723 (widget-put widget :indent (+ (or (widget-get parent :indent) 0)
724 (or (widget-get widget :extra-offset) 0)
725 (widget-get parent :offset))))
726 (widget-apply widget :create)
727 widget))
729 ;;;###autoload
730 (defun widget-delete (widget)
731 "Delete WIDGET."
732 (widget-apply widget :delete))
734 (defun widget-copy (widget)
735 "Make a deep copy of WIDGET."
736 (widget-apply (copy-sequence widget) :copy))
738 (defun widget-convert (type &rest args)
739 "Convert TYPE to a widget without inserting it in the buffer.
740 The optional ARGS are additional keyword arguments."
741 ;; Don't touch the type.
742 (let* ((widget (if (symbolp type)
743 (list type)
744 (copy-sequence type)))
745 (current widget)
746 done
747 (keys args))
748 ;; First set the :args keyword.
749 (while (cdr current) ;Look in the type.
750 (if (and (keywordp (cadr current))
751 ;; If the last element is a keyword,
752 ;; it is still the :args element,
753 ;; even though it is a keyword.
754 (cddr current))
755 (if (eq (cadr current) :args)
756 ;; If :args is explicitly specified, obey it.
757 (setq current nil)
758 ;; Some other irrelevant keyword.
759 (setq current (cdr (cdr current))))
760 (setcdr current (list :args (cdr current)))
761 (setq current nil)))
762 (while (and args (not done)) ;Look in ARGS.
763 (cond ((eq (car args) :args)
764 ;; Handle explicit specification of :args.
765 (setq args (cadr args)
766 done t))
767 ((keywordp (car args))
768 (setq args (cddr args)))
769 (t (setq done t))))
770 (when done
771 (widget-put widget :args args))
772 ;; Then Convert the widget.
773 (setq type widget)
774 (while type
775 (let ((convert-widget (plist-get (cdr type) :convert-widget)))
776 (if convert-widget
777 (setq widget (funcall convert-widget widget))))
778 (setq type (get (car type) 'widget-type)))
779 ;; Finally set the keyword args.
780 (while keys
781 (let ((next (nth 0 keys)))
782 (if (keywordp next)
783 (progn
784 (widget-put widget next (nth 1 keys))
785 (setq keys (nthcdr 2 keys)))
786 (setq keys nil))))
787 ;; Convert the :value to internal format.
788 (if (widget-member widget :value)
789 (widget-put widget
790 :value (widget-apply widget
791 :value-to-internal
792 (widget-get widget :value))))
793 ;; Return the newly create widget.
794 widget))
796 ;;;###autoload
797 (defun widget-insert (&rest args)
798 "Call `insert' with ARGS even if surrounding text is read only."
799 (let ((inhibit-read-only t)
800 (inhibit-modification-hooks t))
801 (apply 'insert args)))
803 (defun widget-convert-text (type from to
804 &optional button-from button-to
805 &rest args)
806 "Return a widget of type TYPE with endpoint FROM TO.
807 No text will be inserted to the buffer, instead the text between FROM
808 and TO will be used as the widgets end points. If optional arguments
809 BUTTON-FROM and BUTTON-TO are given, these will be used as the widgets
810 button end points.
811 Optional ARGS are extra keyword arguments for TYPE."
812 (let ((widget (apply 'widget-convert type :delete 'widget-leave-text args))
813 (from (copy-marker from))
814 (to (copy-marker to)))
815 (set-marker-insertion-type from t)
816 (set-marker-insertion-type to nil)
817 (widget-put widget :from from)
818 (widget-put widget :to to)
819 (when button-from
820 (widget-specify-button widget button-from button-to))
821 widget))
823 (defun widget-convert-button (type from to &rest args)
824 "Return a widget of type TYPE with endpoint FROM TO.
825 Optional ARGS are extra keyword arguments for TYPE.
826 No text will be inserted to the buffer, instead the text between FROM
827 and TO will be used as the widgets end points, as well as the widgets
828 button end points."
829 (apply 'widget-convert-text type from to from to args))
831 (defun widget-leave-text (widget)
832 "Remove markers and overlays from WIDGET and its children."
833 (let ((button (widget-get widget :button-overlay))
834 (sample (widget-get widget :sample-overlay))
835 (doc (widget-get widget :doc-overlay))
836 (field (widget-get widget :field-overlay)))
837 (set-marker (widget-get widget :from) nil)
838 (set-marker (widget-get widget :to) nil)
839 (when button
840 (delete-overlay button))
841 (when sample
842 (delete-overlay sample))
843 (when doc
844 (delete-overlay doc))
845 (when field
846 (delete-overlay field))
847 (mapc 'widget-leave-text (widget-get widget :children))))
849 ;;; Keymap and Commands.
851 ;;;###autoload
852 (defvar widget-keymap
853 (let ((map (make-sparse-keymap)))
854 (define-key map "\t" 'widget-forward)
855 (define-key map [(shift tab)] 'widget-backward)
856 (define-key map [backtab] 'widget-backward)
857 (define-key map [down-mouse-2] 'widget-button-click)
858 (define-key map "\C-m" 'widget-button-press)
859 map)
860 "Keymap containing useful binding for buffers containing widgets.
861 Recommended as a parent keymap for modes using widgets.")
863 (defvar widget-global-map global-map
864 "Keymap used for events a widget does not handle itself.")
865 (make-variable-buffer-local 'widget-global-map)
867 (defvar widget-field-keymap
868 (let ((map (copy-keymap widget-keymap)))
869 (define-key map "\C-k" 'widget-kill-line)
870 (define-key map "\M-\t" 'widget-complete)
871 (define-key map "\C-m" 'widget-field-activate)
872 ;; Since the widget code uses a `field' property to identify fields,
873 ;; ordinary beginning-of-line does the right thing.
874 ;; (define-key map "\C-a" 'widget-beginning-of-line)
875 (define-key map "\C-e" 'widget-end-of-line)
876 map)
877 "Keymap used inside an editable field.")
879 (defvar widget-text-keymap
880 (let ((map (copy-keymap widget-keymap)))
881 ;; Since the widget code uses a `field' property to identify fields,
882 ;; ordinary beginning-of-line does the right thing.
883 ;; (define-key map "\C-a" 'widget-beginning-of-line)
884 (define-key map "\C-e" 'widget-end-of-line)
885 map)
886 "Keymap used inside a text field.")
888 (defun widget-field-activate (pos &optional event)
889 "Invoke the editable field at point."
890 (interactive "@d")
891 (let ((field (widget-field-at pos)))
892 (if field
893 (widget-apply-action field event)
894 (call-interactively
895 (lookup-key widget-global-map (this-command-keys))))))
897 (defface widget-button-pressed
898 '((((min-colors 88) (class color))
899 (:foreground "red1"))
900 (((class color))
901 (:foreground "red"))
903 (:weight bold :underline t)))
904 "Face used for pressed buttons."
905 :group 'widget-faces)
906 ;; backward-compatibility alias
907 (put 'widget-button-pressed-face 'face-alias 'widget-button-pressed)
909 (defun widget-button-click (event)
910 "Invoke the button that the mouse is pointing at."
911 (interactive "e")
912 (if (widget-event-point event)
913 (let* ((pos (widget-event-point event))
914 (start (event-start event))
915 (button (get-char-property
916 pos 'button (and (windowp (posn-window start))
917 (window-buffer (posn-window start))))))
918 (if button
919 ;; Mouse click on a widget button. Do the following
920 ;; in a save-excursion so that the click on the button
921 ;; doesn't change point.
922 (save-selected-window
923 (select-window (posn-window (event-start event)))
924 (save-excursion
925 (goto-char (posn-point (event-start event)))
926 (let* ((overlay (widget-get button :button-overlay))
927 (face (overlay-get overlay 'face))
928 (mouse-face (overlay-get overlay 'mouse-face)))
929 (unwind-protect
930 ;; Read events, including mouse-movement events
931 ;; until we receive a release event. Highlight/
932 ;; unhighlight the button the mouse was initially
933 ;; on when we move over it.
934 (save-excursion
935 (when face ; avoid changing around image
936 (overlay-put overlay
937 'face widget-button-pressed-face)
938 (overlay-put overlay
939 'mouse-face widget-button-pressed-face))
940 (unless (widget-apply button :mouse-down-action event)
941 (let ((track-mouse t))
942 (while (not (widget-button-release-event-p event))
943 (setq event (read-event)
944 pos (widget-event-point event))
945 (if (and pos
946 (eq (get-char-property pos 'button)
947 button))
948 (when face
949 (overlay-put overlay
950 'face
951 widget-button-pressed-face)
952 (overlay-put overlay
953 'mouse-face
954 widget-button-pressed-face))
955 (overlay-put overlay 'face face)
956 (overlay-put overlay 'mouse-face mouse-face)))))
958 ;; When mouse is released over the button, run
959 ;; its action function.
960 (when (and pos
961 (eq (get-char-property pos 'button) button))
962 (widget-apply-action button event)))
963 (overlay-put overlay 'face face)
964 (overlay-put overlay 'mouse-face mouse-face))))
966 (unless (pos-visible-in-window-p (widget-event-point event))
967 (mouse-set-point event)
968 (beginning-of-line)
969 (recenter))
972 (let ((up t) command)
973 ;; Mouse click not on a widget button. Find the global
974 ;; command to run, and check whether it is bound to an
975 ;; up event.
976 (mouse-set-point event)
977 (if (memq (event-basic-type event) '(mouse-1 down-mouse-1))
978 (cond ((setq command ;down event
979 (lookup-key widget-global-map [down-mouse-1]))
980 (setq up nil))
981 ((setq command ;up event
982 (lookup-key widget-global-map [mouse-1]))))
983 (cond ((setq command ;down event
984 (lookup-key widget-global-map [down-mouse-2]))
985 (setq up nil))
986 ((setq command ;up event
987 (lookup-key widget-global-map [mouse-2])))))
988 (when up
989 ;; Don't execute up events twice.
990 (while (not (widget-button-release-event-p event))
991 (setq event (read-event))))
992 (when command
993 (call-interactively command)))))
994 (message "You clicked somewhere weird.")))
996 (defun widget-button-press (pos &optional event)
997 "Invoke button at POS."
998 (interactive "@d")
999 (let ((button (get-char-property pos 'button)))
1000 (if button
1001 (widget-apply-action button event)
1002 (let ((command (lookup-key widget-global-map (this-command-keys))))
1003 (when (commandp command)
1004 (call-interactively command))))))
1006 (defun widget-tabable-at (&optional pos)
1007 "Return the tabable widget at POS, or nil.
1008 POS defaults to the value of (point)."
1009 (let ((widget (widget-at pos)))
1010 (if widget
1011 (let ((order (widget-get widget :tab-order)))
1012 (if order
1013 (if (>= order 0)
1014 widget)
1015 widget)))))
1017 (defvar widget-use-overlay-change t
1018 "If non-nil, use overlay change functions to tab around in the buffer.
1019 This is much faster, but doesn't work reliably on Emacs 19.34.")
1021 (defun widget-move (arg)
1022 "Move point to the ARG next field or button.
1023 ARG may be negative to move backward."
1024 (or (bobp) (> arg 0) (backward-char))
1025 (let ((wrapped 0)
1026 (number arg)
1027 (old (widget-tabable-at)))
1028 ;; Forward.
1029 (while (> arg 0)
1030 (cond ((eobp)
1031 (goto-char (point-min))
1032 (setq wrapped (1+ wrapped)))
1033 (widget-use-overlay-change
1034 (goto-char (next-overlay-change (point))))
1036 (forward-char 1)))
1037 (and (= wrapped 2)
1038 (eq arg number)
1039 (error "No buttons or fields found"))
1040 (let ((new (widget-tabable-at)))
1041 (when new
1042 (unless (eq new old)
1043 (setq arg (1- arg))
1044 (setq old new)))))
1045 ;; Backward.
1046 (while (< arg 0)
1047 (cond ((bobp)
1048 (goto-char (point-max))
1049 (setq wrapped (1+ wrapped)))
1050 (widget-use-overlay-change
1051 (goto-char (previous-overlay-change (point))))
1053 (backward-char 1)))
1054 (and (= wrapped 2)
1055 (eq arg number)
1056 (error "No buttons or fields found"))
1057 (let ((new (widget-tabable-at)))
1058 (when new
1059 (unless (eq new old)
1060 (setq arg (1+ arg))))))
1061 (let ((new (widget-tabable-at)))
1062 (while (eq (widget-tabable-at) new)
1063 (backward-char)))
1064 (forward-char))
1065 (widget-echo-help (point))
1066 (run-hooks 'widget-move-hook))
1068 (defun widget-forward (arg)
1069 "Move point to the next field or button.
1070 With optional ARG, move across that many fields."
1071 (interactive "p")
1072 (run-hooks 'widget-forward-hook)
1073 (widget-move arg))
1075 (defun widget-backward (arg)
1076 "Move point to the previous field or button.
1077 With optional ARG, move across that many fields."
1078 (interactive "p")
1079 (run-hooks 'widget-backward-hook)
1080 (widget-move (- arg)))
1082 ;; Since the widget code uses a `field' property to identify fields,
1083 ;; ordinary beginning-of-line does the right thing.
1084 (defalias 'widget-beginning-of-line 'beginning-of-line)
1086 (defun widget-end-of-line ()
1087 "Go to end of field or end of line, whichever is first.
1088 Trailing spaces at the end of padded fields are not considered part of
1089 the field."
1090 (interactive)
1091 ;; Ordinary end-of-line does the right thing, because we're inside
1092 ;; text with a `field' property.
1093 (end-of-line)
1094 (unless (eolp)
1095 ;; ... except that we want to ignore trailing spaces in fields that
1096 ;; aren't terminated by a newline, because they are used as padding,
1097 ;; and ignored when extracting the entered value of the field.
1098 (skip-chars-backward " " (field-beginning (1- (point))))))
1100 (defun widget-kill-line ()
1101 "Kill to end of field or end of line, whichever is first."
1102 (interactive)
1103 (let* ((field (widget-field-find (point)))
1104 (end (and field (widget-field-end field))))
1105 (if (and field (> (line-beginning-position 2) end))
1106 (kill-region (point) end)
1107 (call-interactively 'kill-line))))
1109 (defcustom widget-complete-field (lookup-key global-map "\M-\t")
1110 "Default function to call for completion inside fields."
1111 :options '(ispell-complete-word complete-tag lisp-complete-symbol)
1112 :type 'function
1113 :group 'widgets)
1115 (defun widget-narrow-to-field ()
1116 "Narrow to field."
1117 (interactive)
1118 (let ((field (widget-field-find (point))))
1119 (if field
1120 (narrow-to-region (line-beginning-position) (line-end-position)))))
1122 (defun widget-complete ()
1123 "Complete content of editable field from point.
1124 When not inside a field, move to the previous button or field."
1125 (interactive)
1126 (let ((field (widget-field-find (point))))
1127 (if field
1128 (save-restriction
1129 (widget-narrow-to-field)
1130 (widget-apply field :complete))
1131 (error "Not in an editable field"))))
1133 ;;; Setting up the buffer.
1135 (defvar widget-field-new nil
1136 "List of all newly created editable fields in the buffer.")
1137 (make-variable-buffer-local 'widget-field-new)
1139 (defvar widget-field-list nil
1140 "List of all editable fields in the buffer.")
1141 (make-variable-buffer-local 'widget-field-list)
1143 (defun widget-at (&optional pos)
1144 "The button or field at POS (default, point)."
1145 (or (get-char-property (or pos (point)) 'button)
1146 (widget-field-at pos)))
1148 ;;;###autoload
1149 (defun widget-setup ()
1150 "Setup current buffer so editing string widgets works."
1151 (let ((inhibit-read-only t)
1152 (inhibit-modification-hooks t)
1153 field)
1154 (while widget-field-new
1155 (setq field (car widget-field-new)
1156 widget-field-new (cdr widget-field-new)
1157 widget-field-list (cons field widget-field-list))
1158 (let ((from (car (widget-get field :field-overlay)))
1159 (to (cdr (widget-get field :field-overlay))))
1160 (widget-specify-field field
1161 (marker-position from) (marker-position to))
1162 (set-marker from nil)
1163 (set-marker to nil))))
1164 (widget-clear-undo)
1165 (widget-add-change))
1167 (defvar widget-field-last nil)
1168 ;; Last field containing point.
1169 (make-variable-buffer-local 'widget-field-last)
1171 (defvar widget-field-was nil)
1172 ;; The widget data before the change.
1173 (make-variable-buffer-local 'widget-field-was)
1175 (defun widget-field-at (pos)
1176 "Return the widget field at POS, or nil if none."
1177 (let ((field (get-char-property (or pos (point)) 'field)))
1178 (if (eq field 'boundary)
1179 (get-char-property (or pos (point)) 'real-field)
1180 field)))
1182 (defun widget-field-buffer (widget)
1183 "Return the buffer of WIDGET's editing field."
1184 (let ((overlay (widget-get widget :field-overlay)))
1185 (cond ((overlayp overlay)
1186 (overlay-buffer overlay))
1187 ((consp overlay)
1188 (marker-buffer (car overlay))))))
1190 (defun widget-field-start (widget)
1191 "Return the start of WIDGET's editing field."
1192 (let ((overlay (widget-get widget :field-overlay)))
1193 (if (overlayp overlay)
1194 (overlay-start overlay)
1195 (car overlay))))
1197 (defun widget-field-end (widget)
1198 "Return the end of WIDGET's editing field."
1199 (let ((overlay (widget-get widget :field-overlay)))
1200 ;; Don't subtract one if local-map works at the end of the overlay,
1201 ;; or if a special `boundary' field has been added after the widget
1202 ;; field.
1203 (if (overlayp overlay)
1204 (if (and (not (eq (with-current-buffer
1205 (widget-field-buffer widget)
1206 (save-restriction
1207 ;; `widget-narrow-to-field' can be
1208 ;; active when this function is called
1209 ;; from an change-functions hook. So
1210 ;; temporarily remove field narrowing
1211 ;; before to call `get-char-property'.
1212 (widen)
1213 (get-char-property (overlay-end overlay)
1214 'field)))
1215 'boundary))
1216 (or widget-field-add-space
1217 (null (widget-get widget :size))))
1218 (1- (overlay-end overlay))
1219 (overlay-end overlay))
1220 (cdr overlay))))
1222 (defun widget-field-find (pos)
1223 "Return the field at POS.
1224 Unlike (get-char-property POS 'field), this works with empty fields too."
1225 (let ((fields widget-field-list)
1226 field found)
1227 (while fields
1228 (setq field (car fields)
1229 fields (cdr fields))
1230 (when (and (<= (widget-field-start field) pos)
1231 (<= pos (widget-field-end field)))
1232 (when found
1233 (error "Overlapping fields"))
1234 (setq found field)))
1235 found))
1237 (defun widget-before-change (from to)
1238 ;; This is how, for example, a variable changes its state to `modified'.
1239 ;; when it is being edited.
1240 (unless inhibit-read-only
1241 (let ((from-field (widget-field-find from))
1242 (to-field (widget-field-find to)))
1243 (cond ((not (eq from-field to-field))
1244 (add-hook 'post-command-hook 'widget-add-change nil t)
1245 (signal 'text-read-only
1246 '("Change should be restricted to a single field")))
1247 ((null from-field)
1248 (add-hook 'post-command-hook 'widget-add-change nil t)
1249 (signal 'text-read-only
1250 '("Attempt to change text outside editable field")))
1251 (widget-field-use-before-change
1252 (widget-apply from-field :notify from-field))))))
1254 (defun widget-add-change ()
1255 (remove-hook 'post-command-hook 'widget-add-change t)
1256 (add-hook 'before-change-functions 'widget-before-change nil t)
1257 (add-hook 'after-change-functions 'widget-after-change nil t))
1259 (defun widget-after-change (from to old)
1260 "Adjust field size and text properties."
1261 (let ((field (widget-field-find from))
1262 (other (widget-field-find to)))
1263 (when field
1264 (unless (eq field other)
1265 (error "Change in different fields"))
1266 (let ((size (widget-get field :size)))
1267 (when size
1268 (let ((begin (widget-field-start field))
1269 (end (widget-field-end field)))
1270 (cond ((< (- end begin) size)
1271 ;; Field too small.
1272 (save-excursion
1273 (goto-char end)
1274 (insert-char ?\s (- (+ begin size) end))))
1275 ((> (- end begin) size)
1276 ;; Field too large and
1277 (if (or (< (point) (+ begin size))
1278 (> (point) end))
1279 ;; Point is outside extra space.
1280 (setq begin (+ begin size))
1281 ;; Point is within the extra space.
1282 (setq begin (point)))
1283 (save-excursion
1284 (goto-char end)
1285 (while (and (eq (preceding-char) ?\s)
1286 (> (point) begin))
1287 (delete-backward-char 1)))))))
1288 (widget-specify-secret field))
1289 (widget-apply field :notify field))))
1291 ;;; Widget Functions
1293 ;; These functions are used in the definition of multiple widgets.
1295 (defun widget-parent-action (widget &optional event)
1296 "Tell :parent of WIDGET to handle the :action.
1297 Optional EVENT is the event that triggered the action."
1298 (widget-apply (widget-get widget :parent) :action event))
1300 (defun widget-children-value-delete (widget)
1301 "Delete all :children and :buttons in WIDGET."
1302 (mapc 'widget-delete (widget-get widget :children))
1303 (widget-put widget :children nil)
1304 (mapc 'widget-delete (widget-get widget :buttons))
1305 (widget-put widget :buttons nil))
1307 (defun widget-children-validate (widget)
1308 "All the :children must be valid."
1309 (let ((children (widget-get widget :children))
1310 child found)
1311 (while (and children (not found))
1312 (setq child (car children)
1313 children (cdr children)
1314 found (widget-apply child :validate)))
1315 found))
1317 (defun widget-child-value-get (widget)
1318 "Get the value of the first member of :children in WIDGET."
1319 (widget-value (car (widget-get widget :children))))
1321 (defun widget-child-value-inline (widget)
1322 "Get the inline value of the first member of :children in WIDGET."
1323 (widget-apply (car (widget-get widget :children)) :value-inline))
1325 (defun widget-child-validate (widget)
1326 "The result of validating the first member of :children in WIDGET."
1327 (widget-apply (car (widget-get widget :children)) :validate))
1329 (defun widget-type-value-create (widget)
1330 "Convert and instantiate the value of the :type attribute of WIDGET.
1331 Store the newly created widget in the :children attribute.
1333 The value of the :type attribute should be an unconverted widget type."
1334 (let ((value (widget-get widget :value))
1335 (type (widget-get widget :type)))
1336 (widget-put widget :children
1337 (list (widget-create-child-value widget
1338 (widget-convert type)
1339 value)))))
1341 (defun widget-type-default-get (widget)
1342 "Get default value from the :type attribute of WIDGET.
1344 The value of the :type attribute should be an unconverted widget type."
1345 (widget-default-get (widget-convert (widget-get widget :type))))
1347 (defun widget-type-match (widget value)
1348 "Non-nil if the :type value of WIDGET matches VALUE.
1350 The value of the :type attribute should be an unconverted widget type."
1351 (widget-apply (widget-convert (widget-get widget :type)) :match value))
1353 (defun widget-types-copy (widget)
1354 "Copy :args as widget types in WIDGET."
1355 (widget-put widget :args (mapcar 'widget-copy (widget-get widget :args)))
1356 widget)
1358 ;; Made defsubst to speed up face editor creation.
1359 (defsubst widget-types-convert-widget (widget)
1360 "Convert :args as widget types in WIDGET."
1361 (widget-put widget :args (mapcar 'widget-convert (widget-get widget :args)))
1362 widget)
1364 (defun widget-value-convert-widget (widget)
1365 "Initialize :value from :args in WIDGET."
1366 (let ((args (widget-get widget :args)))
1367 (when args
1368 (widget-put widget :value (car args))
1369 ;; Don't convert :value here, as this is done in `widget-convert'.
1370 ;; (widget-put widget :value (widget-apply widget
1371 ;; :value-to-internal (car args)))
1372 (widget-put widget :args nil)))
1373 widget)
1375 (defun widget-value-value-get (widget)
1376 "Return the :value property of WIDGET."
1377 (widget-get widget :value))
1379 ;;; The `default' Widget.
1381 (define-widget 'default nil
1382 "Basic widget other widgets are derived from."
1383 :value-to-internal (lambda (widget value) value)
1384 :value-to-external (lambda (widget value) value)
1385 :button-prefix 'widget-button-prefix
1386 :button-suffix 'widget-button-suffix
1387 :complete 'widget-default-complete
1388 :create 'widget-default-create
1389 :indent nil
1390 :offset 0
1391 :format-handler 'widget-default-format-handler
1392 :button-face-get 'widget-default-button-face-get
1393 :sample-face-get 'widget-default-sample-face-get
1394 :delete 'widget-default-delete
1395 :copy 'identity
1396 :value-set 'widget-default-value-set
1397 :value-inline 'widget-default-value-inline
1398 :value-delete 'ignore
1399 :default-get 'widget-default-default-get
1400 :menu-tag-get 'widget-default-menu-tag-get
1401 :validate #'ignore
1402 :active 'widget-default-active
1403 :activate 'widget-specify-active
1404 :deactivate 'widget-default-deactivate
1405 :mouse-down-action #'ignore
1406 :action 'widget-default-action
1407 :notify 'widget-default-notify
1408 :prompt-value 'widget-default-prompt-value)
1410 (defun widget-default-complete (widget)
1411 "Call the value of the :complete-function property of WIDGET.
1412 If that does not exists, call the value of `widget-complete-field'."
1413 (call-interactively (or (widget-get widget :complete-function)
1414 widget-complete-field)))
1416 (defun widget-default-create (widget)
1417 "Create WIDGET at point in the current buffer."
1418 (widget-specify-insert
1419 (let ((from (point))
1420 button-begin button-end
1421 sample-begin sample-end
1422 doc-begin doc-end
1423 value-pos)
1424 (insert (widget-get widget :format))
1425 (goto-char from)
1426 ;; Parse escapes in format.
1427 (while (re-search-forward "%\\(.\\)" nil t)
1428 (let ((escape (char-after (match-beginning 1))))
1429 (delete-backward-char 2)
1430 (cond ((eq escape ?%)
1431 (insert ?%))
1432 ((eq escape ?\[)
1433 (setq button-begin (point))
1434 (insert (widget-get-indirect widget :button-prefix)))
1435 ((eq escape ?\])
1436 (insert (widget-get-indirect widget :button-suffix))
1437 (setq button-end (point)))
1438 ((eq escape ?\{)
1439 (setq sample-begin (point)))
1440 ((eq escape ?\})
1441 (setq sample-end (point)))
1442 ((eq escape ?n)
1443 (when (widget-get widget :indent)
1444 (insert ?\n)
1445 (insert-char ?\s (widget-get widget :indent))))
1446 ((eq escape ?t)
1447 (let ((image (widget-get widget :tag-glyph))
1448 (tag (widget-get widget :tag)))
1449 (cond (image
1450 (widget-image-insert widget (or tag "image") image))
1451 (tag
1452 (insert tag))
1454 (princ (widget-get widget :value)
1455 (current-buffer))))))
1456 ((eq escape ?d)
1457 (let ((doc (widget-get widget :doc)))
1458 (when doc
1459 (setq doc-begin (point))
1460 (insert doc)
1461 (while (eq (preceding-char) ?\n)
1462 (delete-backward-char 1))
1463 (insert ?\n)
1464 (setq doc-end (point)))))
1465 ((eq escape ?v)
1466 (if (and button-begin (not button-end))
1467 (widget-apply widget :value-create)
1468 (setq value-pos (point))))
1470 (widget-apply widget :format-handler escape)))))
1471 ;; Specify button, sample, and doc, and insert value.
1472 (and button-begin button-end
1473 (widget-specify-button widget button-begin button-end))
1474 (and sample-begin sample-end
1475 (widget-specify-sample widget sample-begin sample-end))
1476 (and doc-begin doc-end
1477 (widget-specify-doc widget doc-begin doc-end))
1478 (when value-pos
1479 (goto-char value-pos)
1480 (widget-apply widget :value-create)))
1481 (let ((from (point-min-marker))
1482 (to (point-max-marker)))
1483 (set-marker-insertion-type from t)
1484 (set-marker-insertion-type to nil)
1485 (widget-put widget :from from)
1486 (widget-put widget :to to)))
1487 (widget-clear-undo))
1489 (defun widget-default-format-handler (widget escape)
1490 ;; We recognize the %h escape by default.
1491 (let* ((buttons (widget-get widget :buttons)))
1492 (cond ((eq escape ?h)
1493 (let* ((doc-property (widget-get widget :documentation-property))
1494 (doc-try (cond ((widget-get widget :doc))
1495 ((functionp doc-property)
1496 (funcall doc-property
1497 (widget-get widget :value)))
1498 ((symbolp doc-property)
1499 (documentation-property
1500 (widget-get widget :value)
1501 doc-property))))
1502 (doc-text (and (stringp doc-try)
1503 (> (length doc-try) 1)
1504 doc-try))
1505 (doc-indent (widget-get widget :documentation-indent)))
1506 (when doc-text
1507 (and (eq (preceding-char) ?\n)
1508 (widget-get widget :indent)
1509 (insert-char ?\s (widget-get widget :indent)))
1510 ;; The `*' in the beginning is redundant.
1511 (when (eq (aref doc-text 0) ?*)
1512 (setq doc-text (substring doc-text 1)))
1513 ;; Get rid of trailing newlines.
1514 (when (string-match "\n+\\'" doc-text)
1515 (setq doc-text (substring doc-text 0 (match-beginning 0))))
1516 (push (widget-create-child-and-convert
1517 widget 'documentation-string
1518 :indent (cond ((numberp doc-indent )
1519 doc-indent)
1520 ((null doc-indent)
1521 nil)
1522 (t 0))
1523 doc-text)
1524 buttons))))
1526 (error "Unknown escape `%c'" escape)))
1527 (widget-put widget :buttons buttons)))
1529 (defun widget-default-button-face-get (widget)
1530 ;; Use :button-face or widget-button-face
1531 (or (widget-get widget :button-face)
1532 (let ((parent (widget-get widget :parent)))
1533 (if parent
1534 (widget-apply parent :button-face-get)
1535 widget-button-face))))
1537 (defun widget-default-sample-face-get (widget)
1538 ;; Use :sample-face.
1539 (widget-get widget :sample-face))
1541 (defun widget-default-delete (widget)
1542 "Remove widget from the buffer."
1543 (let ((from (widget-get widget :from))
1544 (to (widget-get widget :to))
1545 (inactive-overlay (widget-get widget :inactive))
1546 (button-overlay (widget-get widget :button-overlay))
1547 (sample-overlay (widget-get widget :sample-overlay))
1548 (doc-overlay (widget-get widget :doc-overlay))
1549 (inhibit-modification-hooks t)
1550 (inhibit-read-only t))
1551 (widget-apply widget :value-delete)
1552 (widget-children-value-delete widget)
1553 (when inactive-overlay
1554 (delete-overlay inactive-overlay))
1555 (when button-overlay
1556 (delete-overlay button-overlay))
1557 (when sample-overlay
1558 (delete-overlay sample-overlay))
1559 (when doc-overlay
1560 (delete-overlay doc-overlay))
1561 (when (< from to)
1562 ;; Kludge: this doesn't need to be true for empty formats.
1563 (delete-region from to))
1564 (set-marker from nil)
1565 (set-marker to nil))
1566 (widget-clear-undo))
1568 (defun widget-default-value-set (widget value)
1569 "Recreate widget with new value."
1570 (let* ((old-pos (point))
1571 (from (copy-marker (widget-get widget :from)))
1572 (to (copy-marker (widget-get widget :to)))
1573 (offset (if (and (<= from old-pos) (<= old-pos to))
1574 (if (>= old-pos (1- to))
1575 (- old-pos to 1)
1576 (- old-pos from)))))
1577 ;;??? Bug: this ought to insert the new value before deleting the old one,
1578 ;; so that markers on either side of the value automatically
1579 ;; stay on the same side. -- rms.
1580 (save-excursion
1581 (goto-char (widget-get widget :from))
1582 (widget-apply widget :delete)
1583 (widget-put widget :value value)
1584 (widget-apply widget :create))
1585 (if offset
1586 (if (< offset 0)
1587 (goto-char (+ (widget-get widget :to) offset 1))
1588 (goto-char (min (+ from offset) (1- (widget-get widget :to))))))))
1590 (defun widget-default-value-inline (widget)
1591 "Wrap value in a list unless it is inline."
1592 (if (widget-get widget :inline)
1593 (widget-value widget)
1594 (list (widget-value widget))))
1596 (defun widget-default-default-get (widget)
1597 "Get `:value'."
1598 (widget-get widget :value))
1600 (defun widget-default-menu-tag-get (widget)
1601 "Use tag or value for menus."
1602 (or (widget-get widget :menu-tag)
1603 (widget-get widget :tag)
1604 (widget-princ-to-string (widget-get widget :value))))
1606 (defun widget-default-active (widget)
1607 "Return t iff this widget active (user modifiable)."
1608 (or (widget-get widget :always-active)
1609 (and (not (widget-get widget :inactive))
1610 (let ((parent (widget-get widget :parent)))
1611 (or (null parent)
1612 (widget-apply parent :active))))))
1614 (defun widget-default-deactivate (widget)
1615 "Make WIDGET inactive for user modifications."
1616 (widget-specify-inactive widget
1617 (widget-get widget :from)
1618 (widget-get widget :to)))
1620 (defun widget-default-action (widget &optional event)
1621 "Notify the parent when a widget changes."
1622 (let ((parent (widget-get widget :parent)))
1623 (when parent
1624 (widget-apply parent :notify widget event))))
1626 (defun widget-default-notify (widget child &optional event)
1627 "Pass notification to parent."
1628 (widget-default-action widget event))
1630 (defun widget-default-prompt-value (widget prompt value unbound)
1631 "Read an arbitrary value. Stolen from `set-variable'."
1632 ;; (let ((initial (if unbound
1633 ;; nil
1634 ;; It would be nice if we could do a `(cons val 1)' here.
1635 ;; (prin1-to-string (custom-quote value))))))
1636 (eval-minibuffer prompt))
1638 ;;; The `item' Widget.
1640 (define-widget 'item 'default
1641 "Constant items for inclusion in other widgets."
1642 :convert-widget 'widget-value-convert-widget
1643 :value-create 'widget-item-value-create
1644 :value-delete 'ignore
1645 :value-get 'widget-value-value-get
1646 :match 'widget-item-match
1647 :match-inline 'widget-item-match-inline
1648 :action 'widget-item-action
1649 :format "%t\n")
1651 (defun widget-item-value-create (widget)
1652 "Insert the printed representation of the value."
1653 (princ (widget-get widget :value) (current-buffer)))
1655 (defun widget-item-match (widget value)
1656 ;; Match if the value is the same.
1657 (equal (widget-get widget :value) value))
1659 (defun widget-item-match-inline (widget values)
1660 ;; Match if the value is the same.
1661 (let ((value (widget-get widget :value)))
1662 (and (listp value)
1663 (<= (length value) (length values))
1664 (let ((head (widget-sublist values 0 (length value))))
1665 (and (equal head value)
1666 (cons head (widget-sublist values (length value))))))))
1668 (defun widget-sublist (list start &optional end)
1669 "Return the sublist of LIST from START to END.
1670 If END is omitted, it defaults to the length of LIST."
1671 (if (> start 0) (setq list (nthcdr start list)))
1672 (if end
1673 (unless (<= end start)
1674 (setq list (copy-sequence list))
1675 (setcdr (nthcdr (- end start 1) list) nil)
1676 list)
1677 (copy-sequence list)))
1679 (defun widget-item-action (widget &optional event)
1680 ;; Just notify itself.
1681 (widget-apply widget :notify widget event))
1683 ;;; The `push-button' Widget.
1685 ;; (defcustom widget-push-button-gui t
1686 ;; "If non nil, use GUI push buttons when available."
1687 ;; :group 'widgets
1688 ;; :type 'boolean)
1690 ;; Cache already created GUI objects.
1691 ;; (defvar widget-push-button-cache nil)
1693 (defcustom widget-push-button-prefix "["
1694 "String used as prefix for buttons."
1695 :type 'string
1696 :group 'widget-button)
1698 (defcustom widget-push-button-suffix "]"
1699 "String used as suffix for buttons."
1700 :type 'string
1701 :group 'widget-button)
1703 (define-widget 'push-button 'item
1704 "A pushable button."
1705 :button-prefix ""
1706 :button-suffix ""
1707 :value-create 'widget-push-button-value-create
1708 :format "%[%v%]")
1710 (defun widget-push-button-value-create (widget)
1711 "Insert text representing the `on' and `off' states."
1712 (let* ((tag (or (widget-get widget :tag)
1713 (widget-get widget :value)))
1714 (tag-glyph (widget-get widget :tag-glyph))
1715 (text (concat widget-push-button-prefix
1716 tag widget-push-button-suffix)))
1717 (if tag-glyph
1718 (widget-image-insert widget text tag-glyph)
1719 (insert text))))
1721 ;; (defun widget-gui-action (widget)
1722 ;; "Apply :action for WIDGET."
1723 ;; (widget-apply-action widget (this-command-keys)))
1725 ;;; The `link' Widget.
1727 (defcustom widget-link-prefix "["
1728 "String used as prefix for links."
1729 :type 'string
1730 :group 'widget-button)
1732 (defcustom widget-link-suffix "]"
1733 "String used as suffix for links."
1734 :type 'string
1735 :group 'widget-button)
1737 (define-widget 'link 'item
1738 "An embedded link."
1739 :button-prefix 'widget-link-prefix
1740 :button-suffix 'widget-link-suffix
1741 :follow-link "\C-m"
1742 :help-echo "Follow the link."
1743 :format "%[%t%]")
1745 ;;; The `info-link' Widget.
1747 (define-widget 'info-link 'link
1748 "A link to an info file."
1749 :action 'widget-info-link-action)
1751 (defun widget-info-link-action (widget &optional event)
1752 "Open the info node specified by WIDGET."
1753 (info (widget-value widget)))
1755 ;;; The `url-link' Widget.
1757 (define-widget 'url-link 'link
1758 "A link to an www page."
1759 :action 'widget-url-link-action)
1761 (defun widget-url-link-action (widget &optional event)
1762 "Open the URL specified by WIDGET."
1763 (browse-url (widget-value widget)))
1765 ;;; The `function-link' Widget.
1767 (define-widget 'function-link 'link
1768 "A link to an Emacs function."
1769 :action 'widget-function-link-action)
1771 (defun widget-function-link-action (widget &optional event)
1772 "Show the function specified by WIDGET."
1773 (describe-function (widget-value widget)))
1775 ;;; The `variable-link' Widget.
1777 (define-widget 'variable-link 'link
1778 "A link to an Emacs variable."
1779 :action 'widget-variable-link-action)
1781 (defun widget-variable-link-action (widget &optional event)
1782 "Show the variable specified by WIDGET."
1783 (describe-variable (widget-value widget)))
1785 ;;; The `file-link' Widget.
1787 (define-widget 'file-link 'link
1788 "A link to a file."
1789 :action 'widget-file-link-action)
1791 (defun widget-file-link-action (widget &optional event)
1792 "Find the file specified by WIDGET."
1793 (find-file (widget-value widget)))
1795 ;;; The `emacs-library-link' Widget.
1797 (define-widget 'emacs-library-link 'link
1798 "A link to an Emacs Lisp library file."
1799 :action 'widget-emacs-library-link-action)
1801 (defun widget-emacs-library-link-action (widget &optional event)
1802 "Find the Emacs library file specified by WIDGET."
1803 (find-file (locate-library (widget-value widget))))
1805 ;;; The `emacs-commentary-link' Widget.
1807 (define-widget 'emacs-commentary-link 'link
1808 "A link to Commentary in an Emacs Lisp library file."
1809 :action 'widget-emacs-commentary-link-action)
1811 (defun widget-emacs-commentary-link-action (widget &optional event)
1812 "Find the Commentary section of the Emacs file specified by WIDGET."
1813 (finder-commentary (widget-value widget)))
1815 ;;; The `editable-field' Widget.
1817 (define-widget 'editable-field 'default
1818 "An editable text field."
1819 :convert-widget 'widget-value-convert-widget
1820 :keymap widget-field-keymap
1821 :format "%v"
1822 :help-echo "M-TAB: complete field; RET: enter value"
1823 :value ""
1824 :prompt-internal 'widget-field-prompt-internal
1825 :prompt-history 'widget-field-history
1826 :prompt-value 'widget-field-prompt-value
1827 :action 'widget-field-action
1828 :validate 'widget-field-validate
1829 :valid-regexp ""
1830 :error "Field's value doesn't match allowed forms"
1831 :value-create 'widget-field-value-create
1832 :value-delete 'widget-field-value-delete
1833 :value-get 'widget-field-value-get
1834 :match 'widget-field-match)
1836 (defvar widget-field-history nil
1837 "History of field minibuffer edits.")
1839 (defun widget-field-prompt-internal (widget prompt initial history)
1840 "Read string for WIDGET promptinhg with PROMPT.
1841 INITIAL is the initial input and HISTORY is a symbol containing
1842 the earlier input."
1843 (read-string prompt initial history))
1845 (defun widget-field-prompt-value (widget prompt value unbound)
1846 "Prompt for a string."
1847 (widget-apply widget
1848 :value-to-external
1849 (widget-apply widget
1850 :prompt-internal prompt
1851 (unless unbound
1852 (cons (widget-apply widget
1853 :value-to-internal value)
1855 (widget-get widget :prompt-history))))
1857 (defvar widget-edit-functions nil)
1859 (defun widget-field-action (widget &optional event)
1860 "Move to next field."
1861 (widget-forward 1)
1862 (run-hook-with-args 'widget-edit-functions widget))
1864 (defun widget-field-validate (widget)
1865 "Valid if the content matches `:valid-regexp'."
1866 (unless (string-match (widget-get widget :valid-regexp)
1867 (widget-apply widget :value-get))
1868 widget))
1870 (defun widget-field-value-create (widget)
1871 "Create an editable text field."
1872 (let ((size (widget-get widget :size))
1873 (value (widget-get widget :value))
1874 (from (point))
1875 ;; This is changed to a real overlay in `widget-setup'. We
1876 ;; need the end points to behave differently until
1877 ;; `widget-setup' is called.
1878 (overlay (cons (make-marker) (make-marker))))
1879 (widget-put widget :field-overlay overlay)
1880 (insert value)
1881 (and size
1882 (< (length value) size)
1883 (insert-char ?\s (- size (length value))))
1884 (unless (memq widget widget-field-list)
1885 (setq widget-field-new (cons widget widget-field-new)))
1886 (move-marker (cdr overlay) (point))
1887 (set-marker-insertion-type (cdr overlay) nil)
1888 (when (null size)
1889 (insert ?\n))
1890 (move-marker (car overlay) from)
1891 (set-marker-insertion-type (car overlay) t)))
1893 (defun widget-field-value-delete (widget)
1894 "Remove the widget from the list of active editing fields."
1895 (setq widget-field-list (delq widget widget-field-list))
1896 (setq widget-field-new (delq widget widget-field-new))
1897 ;; These are nil if the :format string doesn't contain `%v'.
1898 (let ((overlay (widget-get widget :field-overlay)))
1899 (when (overlayp overlay)
1900 (delete-overlay overlay))))
1902 (defun widget-field-value-get (widget)
1903 "Return current text in editing field."
1904 (let ((from (widget-field-start widget))
1905 (to (widget-field-end widget))
1906 (buffer (widget-field-buffer widget))
1907 (size (widget-get widget :size))
1908 (secret (widget-get widget :secret))
1909 (old (current-buffer)))
1910 (if (and from to)
1911 (progn
1912 (set-buffer buffer)
1913 (while (and size
1914 (not (zerop size))
1915 (> to from)
1916 (eq (char-after (1- to)) ?\s))
1917 (setq to (1- to)))
1918 (let ((result (buffer-substring-no-properties from to)))
1919 (when secret
1920 (let ((index 0))
1921 (while (< (+ from index) to)
1922 (aset result index
1923 (get-char-property (+ from index) 'secret))
1924 (setq index (1+ index)))))
1925 (set-buffer old)
1926 result))
1927 (widget-get widget :value))))
1929 (defun widget-field-match (widget value)
1930 ;; Match any string.
1931 (stringp value))
1933 ;;; The `text' Widget.
1935 (define-widget 'text 'editable-field
1936 "A multiline text area."
1937 :keymap widget-text-keymap)
1939 ;;; The `menu-choice' Widget.
1941 (define-widget 'menu-choice 'default
1942 "A menu of options."
1943 :convert-widget 'widget-types-convert-widget
1944 :copy 'widget-types-copy
1945 :format "%[%t%]: %v"
1946 :case-fold t
1947 :tag "choice"
1948 :void '(item :format "invalid (%t)\n")
1949 :value-create 'widget-choice-value-create
1950 :value-get 'widget-child-value-get
1951 :value-inline 'widget-child-value-inline
1952 :default-get 'widget-choice-default-get
1953 :mouse-down-action 'widget-choice-mouse-down-action
1954 :action 'widget-choice-action
1955 :error "Make a choice"
1956 :validate 'widget-choice-validate
1957 :match 'widget-choice-match
1958 :match-inline 'widget-choice-match-inline)
1960 (defun widget-choice-value-create (widget)
1961 "Insert the first choice that matches the value."
1962 (let ((value (widget-get widget :value))
1963 (args (widget-get widget :args))
1964 (explicit (widget-get widget :explicit-choice))
1965 current)
1966 (if explicit
1967 (progn
1968 ;; If the user specified the choice for this value,
1969 ;; respect that choice.
1970 (widget-put widget :children (list (widget-create-child-value
1971 widget explicit value)))
1972 (widget-put widget :choice explicit)
1973 (widget-put widget :explicit-choice nil))
1974 (while args
1975 (setq current (car args)
1976 args (cdr args))
1977 (when (widget-apply current :match value)
1978 (widget-put widget :children (list (widget-create-child-value
1979 widget current value)))
1980 (widget-put widget :choice current)
1981 (setq args nil
1982 current nil)))
1983 (when current
1984 (let ((void (widget-get widget :void)))
1985 (widget-put widget :children (list (widget-create-child-and-convert
1986 widget void :value value)))
1987 (widget-put widget :choice void))))))
1989 (defun widget-choice-default-get (widget)
1990 ;; Get default for the first choice.
1991 (widget-default-get (car (widget-get widget :args))))
1993 (defcustom widget-choice-toggle nil
1994 "If non-nil, a binary choice will just toggle between the values.
1995 Otherwise, the user will explicitly have to choose between the values
1996 when he invoked the menu."
1997 :type 'boolean
1998 :group 'widgets)
2000 (defun widget-choice-mouse-down-action (widget &optional event)
2001 ;; Return non-nil if we need a menu.
2002 (let ((args (widget-get widget :args))
2003 (old (widget-get widget :choice)))
2004 (cond ((not (display-popup-menus-p))
2005 ;; No place to pop up a menu.
2006 nil)
2007 ((< (length args) 2)
2008 ;; Empty or singleton list, just return the value.
2009 nil)
2010 ((> (length args) widget-menu-max-size)
2011 ;; Too long, prompt.
2012 nil)
2013 ((> (length args) 2)
2014 ;; Reasonable sized list, use menu.
2016 ((and widget-choice-toggle (memq old args))
2017 ;; We toggle.
2018 nil)
2020 ;; Ask which of the two.
2021 t))))
2023 (defun widget-choice-action (widget &optional event)
2024 ;; Make a choice.
2025 (let ((args (widget-get widget :args))
2026 (old (widget-get widget :choice))
2027 (tag (widget-apply widget :menu-tag-get))
2028 (completion-ignore-case (widget-get widget :case-fold))
2029 this-explicit
2030 current choices)
2031 ;; Remember old value.
2032 (if (and old (not (widget-apply widget :validate)))
2033 (let* ((external (widget-value widget))
2034 (internal (widget-apply old :value-to-internal external)))
2035 (widget-put old :value internal)))
2036 ;; Find new choice.
2037 (setq current
2038 (cond ((= (length args) 0)
2039 nil)
2040 ((= (length args) 1)
2041 (nth 0 args))
2042 ((and widget-choice-toggle
2043 (= (length args) 2)
2044 (memq old args))
2045 (if (eq old (nth 0 args))
2046 (nth 1 args)
2047 (nth 0 args)))
2049 (while args
2050 (setq current (car args)
2051 args (cdr args))
2052 (setq choices
2053 (cons (cons (widget-apply current :menu-tag-get)
2054 current)
2055 choices)))
2056 (setq this-explicit t)
2057 (widget-choose tag (reverse choices) event))))
2058 (when current
2059 ;; If this was an explicit user choice, record the choice,
2060 ;; so that widget-choice-value-create will respect it.
2061 (when this-explicit
2062 (widget-put widget :explicit-choice current))
2063 (widget-value-set widget (widget-default-get current))
2064 (widget-setup)
2065 (widget-apply widget :notify widget event)))
2066 (run-hook-with-args 'widget-edit-functions widget))
2068 (defun widget-choice-validate (widget)
2069 ;; Valid if we have made a valid choice.
2070 (if (eq (widget-get widget :void) (widget-get widget :choice))
2071 widget
2072 (widget-apply (car (widget-get widget :children)) :validate)))
2074 (defun widget-choice-match (widget value)
2075 ;; Matches if one of the choices matches.
2076 (let ((args (widget-get widget :args))
2077 current found)
2078 (while (and args (not found))
2079 (setq current (car args)
2080 args (cdr args)
2081 found (widget-apply current :match value)))
2082 found))
2084 (defun widget-choice-match-inline (widget values)
2085 ;; Matches if one of the choices matches.
2086 (let ((args (widget-get widget :args))
2087 current found)
2088 (while (and args (null found))
2089 (setq current (car args)
2090 args (cdr args)
2091 found (widget-match-inline current values)))
2092 found))
2094 ;;; The `toggle' Widget.
2096 (define-widget 'toggle 'item
2097 "Toggle between two states."
2098 :format "%[%v%]\n"
2099 :value-create 'widget-toggle-value-create
2100 :action 'widget-toggle-action
2101 :match (lambda (widget value) t)
2102 :on "on"
2103 :off "off")
2105 (defun widget-toggle-value-create (widget)
2106 "Insert text representing the `on' and `off' states."
2107 (if (widget-value widget)
2108 (let ((image (widget-get widget :on-glyph)))
2109 (and (display-graphic-p)
2110 (listp image)
2111 (not (eq (car image) 'image))
2112 (widget-put widget :on-glyph (setq image (eval image))))
2113 (widget-image-insert widget
2114 (widget-get widget :on)
2115 image))
2116 (let ((image (widget-get widget :off-glyph)))
2117 (and (display-graphic-p)
2118 (listp image)
2119 (not (eq (car image) 'image))
2120 (widget-put widget :off-glyph (setq image (eval image))))
2121 (widget-image-insert widget (widget-get widget :off) image))))
2123 (defun widget-toggle-action (widget &optional event)
2124 ;; Toggle value.
2125 (widget-value-set widget (not (widget-value widget)))
2126 (widget-apply widget :notify widget event)
2127 (run-hook-with-args 'widget-edit-functions widget))
2129 ;;; The `checkbox' Widget.
2131 (define-widget 'checkbox 'toggle
2132 "A checkbox toggle."
2133 :button-suffix ""
2134 :button-prefix ""
2135 :format "%[%v%]"
2136 :on "[X]"
2137 ;; We could probably do the same job as the images using single
2138 ;; space characters in a boxed face with a stretch specification to
2139 ;; make them square.
2140 :on-glyph '(create-image "\300\300\141\143\067\076\034\030"
2141 'xbm t :width 8 :height 8
2142 :background "grey75" ; like default mode line
2143 :foreground "black"
2144 :relief -2
2145 :ascent 'center)
2146 :off "[ ]"
2147 :off-glyph '(create-image (make-string 8 0)
2148 'xbm t :width 8 :height 8
2149 :background "grey75"
2150 :foreground "black"
2151 :relief -2
2152 :ascent 'center)
2153 :help-echo "Toggle this item."
2154 :action 'widget-checkbox-action)
2156 (defun widget-checkbox-action (widget &optional event)
2157 "Toggle checkbox, notify parent, and set active state of sibling."
2158 (widget-toggle-action widget event)
2159 (let ((sibling (widget-get-sibling widget)))
2160 (when sibling
2161 (if (widget-value widget)
2162 (widget-apply sibling :activate)
2163 (widget-apply sibling :deactivate)))))
2165 ;;; The `checklist' Widget.
2167 (define-widget 'checklist 'default
2168 "A multiple choice widget."
2169 :convert-widget 'widget-types-convert-widget
2170 :copy 'widget-types-copy
2171 :format "%v"
2172 :offset 4
2173 :entry-format "%b %v"
2174 :greedy nil
2175 :value-create 'widget-checklist-value-create
2176 :value-get 'widget-checklist-value-get
2177 :validate 'widget-checklist-validate
2178 :match 'widget-checklist-match
2179 :match-inline 'widget-checklist-match-inline)
2181 (defun widget-checklist-value-create (widget)
2182 ;; Insert all values
2183 (let ((alist (widget-checklist-match-find widget (widget-get widget :value)))
2184 (args (widget-get widget :args)))
2185 (while args
2186 (widget-checklist-add-item widget (car args) (assq (car args) alist))
2187 (setq args (cdr args)))
2188 (widget-put widget :children (nreverse (widget-get widget :children)))))
2190 (defun widget-checklist-add-item (widget type chosen)
2191 "Create checklist item in WIDGET of type TYPE.
2192 If the item is checked, CHOSEN is a cons whose cdr is the value."
2193 (and (eq (preceding-char) ?\n)
2194 (widget-get widget :indent)
2195 (insert-char ?\s (widget-get widget :indent)))
2196 (widget-specify-insert
2197 (let* ((children (widget-get widget :children))
2198 (buttons (widget-get widget :buttons))
2199 (button-args (or (widget-get type :sibling-args)
2200 (widget-get widget :button-args)))
2201 (from (point))
2202 child button)
2203 (insert (widget-get widget :entry-format))
2204 (goto-char from)
2205 ;; Parse % escapes in format.
2206 (while (re-search-forward "%\\([bv%]\\)" nil t)
2207 (let ((escape (char-after (match-beginning 1))))
2208 (delete-backward-char 2)
2209 (cond ((eq escape ?%)
2210 (insert ?%))
2211 ((eq escape ?b)
2212 (setq button (apply 'widget-create-child-and-convert
2213 widget 'checkbox
2214 :value (not (null chosen))
2215 button-args)))
2216 ((eq escape ?v)
2217 (setq child
2218 (cond ((not chosen)
2219 (let ((child (widget-create-child widget type)))
2220 (widget-apply child :deactivate)
2221 child))
2222 ((widget-get type :inline)
2223 (widget-create-child-value
2224 widget type (cdr chosen)))
2226 (widget-create-child-value
2227 widget type (car (cdr chosen)))))))
2229 (error "Unknown escape `%c'" escape)))))
2230 ;; Update properties.
2231 (and button child (widget-put child :button button))
2232 (and button (widget-put widget :buttons (cons button buttons)))
2233 (and child (widget-put widget :children (cons child children))))))
2235 (defun widget-checklist-match (widget values)
2236 ;; All values must match a type in the checklist.
2237 (and (listp values)
2238 (null (cdr (widget-checklist-match-inline widget values)))))
2240 (defun widget-checklist-match-inline (widget values)
2241 ;; Find the values which match a type in the checklist.
2242 (let ((greedy (widget-get widget :greedy))
2243 (args (copy-sequence (widget-get widget :args)))
2244 found rest)
2245 (while values
2246 (let ((answer (widget-checklist-match-up args values)))
2247 (cond (answer
2248 (let ((vals (widget-match-inline answer values)))
2249 (setq found (append found (car vals))
2250 values (cdr vals)
2251 args (delq answer args))))
2252 (greedy
2253 (setq rest (append rest (list (car values)))
2254 values (cdr values)))
2256 (setq rest (append rest values)
2257 values nil)))))
2258 (cons found rest)))
2260 (defun widget-checklist-match-find (widget vals)
2261 "Find the vals which match a type in the checklist.
2262 Return an alist of (TYPE MATCH)."
2263 (let ((greedy (widget-get widget :greedy))
2264 (args (copy-sequence (widget-get widget :args)))
2265 found)
2266 (while vals
2267 (let ((answer (widget-checklist-match-up args vals)))
2268 (cond (answer
2269 (let ((match (widget-match-inline answer vals)))
2270 (setq found (cons (cons answer (car match)) found)
2271 vals (cdr match)
2272 args (delq answer args))))
2273 (greedy
2274 (setq vals (cdr vals)))
2276 (setq vals nil)))))
2277 found))
2279 (defun widget-checklist-match-up (args vals)
2280 "Return the first type from ARGS that matches VALS."
2281 (let (current found)
2282 (while (and args (null found))
2283 (setq current (car args)
2284 args (cdr args)
2285 found (widget-match-inline current vals)))
2286 (if found
2287 current)))
2289 (defun widget-checklist-value-get (widget)
2290 ;; The values of all selected items.
2291 (let ((children (widget-get widget :children))
2292 child result)
2293 (while children
2294 (setq child (car children)
2295 children (cdr children))
2296 (if (widget-value (widget-get child :button))
2297 (setq result (append result (widget-apply child :value-inline)))))
2298 result))
2300 (defun widget-checklist-validate (widget)
2301 ;; Ticked chilren must be valid.
2302 (let ((children (widget-get widget :children))
2303 child button found)
2304 (while (and children (not found))
2305 (setq child (car children)
2306 children (cdr children)
2307 button (widget-get child :button)
2308 found (and (widget-value button)
2309 (widget-apply child :validate))))
2310 found))
2312 ;;; The `option' Widget
2314 (define-widget 'option 'checklist
2315 "An widget with an optional item."
2316 :inline t)
2318 ;;; The `choice-item' Widget.
2320 (define-widget 'choice-item 'item
2321 "Button items that delegate action events to their parents."
2322 :action 'widget-parent-action
2323 :format "%[%t%] \n")
2325 ;;; The `radio-button' Widget.
2327 (define-widget 'radio-button 'toggle
2328 "A radio button for use in the `radio' widget."
2329 :notify 'widget-radio-button-notify
2330 :format "%[%v%]"
2331 :button-suffix ""
2332 :button-prefix ""
2333 :on "(*)"
2334 :on-glyph "radio1"
2335 :off "( )"
2336 :off-glyph "radio0")
2338 (defun widget-radio-button-notify (widget child &optional event)
2339 ;; Tell daddy.
2340 (widget-apply (widget-get widget :parent) :action widget event))
2342 ;;; The `radio-button-choice' Widget.
2344 (define-widget 'radio-button-choice 'default
2345 "Select one of multiple options."
2346 :convert-widget 'widget-types-convert-widget
2347 :copy 'widget-types-copy
2348 :offset 4
2349 :format "%v"
2350 :entry-format "%b %v"
2351 :value-create 'widget-radio-value-create
2352 :value-get 'widget-radio-value-get
2353 :value-inline 'widget-radio-value-inline
2354 :value-set 'widget-radio-value-set
2355 :error "You must push one of the buttons"
2356 :validate 'widget-radio-validate
2357 :match 'widget-choice-match
2358 :match-inline 'widget-choice-match-inline
2359 :action 'widget-radio-action)
2361 (defun widget-radio-value-create (widget)
2362 ;; Insert all values
2363 (let ((args (widget-get widget :args))
2364 arg)
2365 (while args
2366 (setq arg (car args)
2367 args (cdr args))
2368 (widget-radio-add-item widget arg))))
2370 (defun widget-radio-add-item (widget type)
2371 "Add to radio widget WIDGET a new radio button item of type TYPE."
2372 ;; (setq type (widget-convert type))
2373 (and (eq (preceding-char) ?\n)
2374 (widget-get widget :indent)
2375 (insert-char ?\s (widget-get widget :indent)))
2376 (widget-specify-insert
2377 (let* ((value (widget-get widget :value))
2378 (children (widget-get widget :children))
2379 (buttons (widget-get widget :buttons))
2380 (button-args (or (widget-get type :sibling-args)
2381 (widget-get widget :button-args)))
2382 (from (point))
2383 (chosen (and (null (widget-get widget :choice))
2384 (widget-apply type :match value)))
2385 child button)
2386 (insert (widget-get widget :entry-format))
2387 (goto-char from)
2388 ;; Parse % escapes in format.
2389 (while (re-search-forward "%\\([bv%]\\)" nil t)
2390 (let ((escape (char-after (match-beginning 1))))
2391 (delete-backward-char 2)
2392 (cond ((eq escape ?%)
2393 (insert ?%))
2394 ((eq escape ?b)
2395 (setq button (apply 'widget-create-child-and-convert
2396 widget 'radio-button
2397 :value (not (null chosen))
2398 button-args)))
2399 ((eq escape ?v)
2400 (setq child (if chosen
2401 (widget-create-child-value
2402 widget type value)
2403 (widget-create-child widget type)))
2404 (unless chosen
2405 (widget-apply child :deactivate)))
2407 (error "Unknown escape `%c'" escape)))))
2408 ;; Update properties.
2409 (when chosen
2410 (widget-put widget :choice type))
2411 (when button
2412 (widget-put child :button button)
2413 (widget-put widget :buttons (nconc buttons (list button))))
2414 (when child
2415 (widget-put widget :children (nconc children (list child))))
2416 child)))
2418 (defun widget-radio-value-get (widget)
2419 ;; Get value of the child widget.
2420 (let ((chosen (widget-radio-chosen widget)))
2421 (and chosen (widget-value chosen))))
2423 (defun widget-radio-chosen (widget)
2424 "Return the widget representing the chosen radio button."
2425 (let ((children (widget-get widget :children))
2426 current found)
2427 (while children
2428 (setq current (car children)
2429 children (cdr children))
2430 (when (widget-apply (widget-get current :button) :value-get)
2431 (setq found current
2432 children nil)))
2433 found))
2435 (defun widget-radio-value-inline (widget)
2436 ;; Get value of the child widget.
2437 (let ((children (widget-get widget :children))
2438 current found)
2439 (while children
2440 (setq current (car children)
2441 children (cdr children))
2442 (when (widget-apply (widget-get current :button) :value-get)
2443 (setq found (widget-apply current :value-inline)
2444 children nil)))
2445 found))
2447 (defun widget-radio-value-set (widget value)
2448 ;; We can't just delete and recreate a radio widget, since children
2449 ;; can be added after the original creation and won't be recreated
2450 ;; by `:create'.
2451 (let ((children (widget-get widget :children))
2452 current found)
2453 (while children
2454 (setq current (car children)
2455 children (cdr children))
2456 (let* ((button (widget-get current :button))
2457 (match (and (not found)
2458 (widget-apply current :match value))))
2459 (widget-value-set button match)
2460 (if match
2461 (progn
2462 (widget-value-set current value)
2463 (widget-apply current :activate))
2464 (widget-apply current :deactivate))
2465 (setq found (or found match))))))
2467 (defun widget-radio-validate (widget)
2468 ;; Valid if we have made a valid choice.
2469 (let ((children (widget-get widget :children))
2470 current found button)
2471 (while (and children (not found))
2472 (setq current (car children)
2473 children (cdr children)
2474 button (widget-get current :button)
2475 found (widget-apply button :value-get)))
2476 (if found
2477 (widget-apply current :validate)
2478 widget)))
2480 (defun widget-radio-action (widget child event)
2481 ;; Check if a radio button was pressed.
2482 (let ((children (widget-get widget :children))
2483 (buttons (widget-get widget :buttons))
2484 current)
2485 (when (memq child buttons)
2486 (while children
2487 (setq current (car children)
2488 children (cdr children))
2489 (let* ((button (widget-get current :button)))
2490 (cond ((eq child button)
2491 (widget-value-set button t)
2492 (widget-apply current :activate))
2493 ((widget-value button)
2494 (widget-value-set button nil)
2495 (widget-apply current :deactivate)))))))
2496 ;; Pass notification to parent.
2497 (widget-apply widget :notify child event))
2499 ;;; The `insert-button' Widget.
2501 (define-widget 'insert-button 'push-button
2502 "An insert button for the `editable-list' widget."
2503 :tag "INS"
2504 :help-echo "Insert a new item into the list at this position."
2505 :action 'widget-insert-button-action)
2507 (defun widget-insert-button-action (widget &optional event)
2508 ;; Ask the parent to insert a new item.
2509 (widget-apply (widget-get widget :parent)
2510 :insert-before (widget-get widget :widget)))
2512 ;;; The `delete-button' Widget.
2514 (define-widget 'delete-button 'push-button
2515 "A delete button for the `editable-list' widget."
2516 :tag "DEL"
2517 :help-echo "Delete this item from the list."
2518 :action 'widget-delete-button-action)
2520 (defun widget-delete-button-action (widget &optional event)
2521 ;; Ask the parent to insert a new item.
2522 (widget-apply (widget-get widget :parent)
2523 :delete-at (widget-get widget :widget)))
2525 ;;; The `editable-list' Widget.
2527 ;; (defcustom widget-editable-list-gui nil
2528 ;; "If non nil, use GUI push-buttons in editable list when available."
2529 ;; :type 'boolean
2530 ;; :group 'widgets)
2532 (define-widget 'editable-list 'default
2533 "A variable list of widgets of the same type."
2534 :convert-widget 'widget-types-convert-widget
2535 :copy 'widget-types-copy
2536 :offset 12
2537 :format "%v%i\n"
2538 :format-handler 'widget-editable-list-format-handler
2539 :entry-format "%i %d %v"
2540 :value-create 'widget-editable-list-value-create
2541 :value-get 'widget-editable-list-value-get
2542 :validate 'widget-children-validate
2543 :match 'widget-editable-list-match
2544 :match-inline 'widget-editable-list-match-inline
2545 :insert-before 'widget-editable-list-insert-before
2546 :delete-at 'widget-editable-list-delete-at)
2548 (defun widget-editable-list-format-handler (widget escape)
2549 ;; We recognize the insert button.
2550 ;; (let ((widget-push-button-gui widget-editable-list-gui))
2551 (cond ((eq escape ?i)
2552 (and (widget-get widget :indent)
2553 (insert-char ?\s (widget-get widget :indent)))
2554 (apply 'widget-create-child-and-convert
2555 widget 'insert-button
2556 (widget-get widget :append-button-args)))
2558 (widget-default-format-handler widget escape)))
2559 ;; )
2562 (defun widget-editable-list-value-create (widget)
2563 ;; Insert all values
2564 (let* ((value (widget-get widget :value))
2565 (type (nth 0 (widget-get widget :args)))
2566 children)
2567 (widget-put widget :value-pos (copy-marker (point)))
2568 (set-marker-insertion-type (widget-get widget :value-pos) t)
2569 (while value
2570 (let ((answer (widget-match-inline type value)))
2571 (if answer
2572 (setq children (cons (widget-editable-list-entry-create
2573 widget
2574 (if (widget-get type :inline)
2575 (car answer)
2576 (car (car answer)))
2578 children)
2579 value (cdr answer))
2580 (setq value nil))))
2581 (widget-put widget :children (nreverse children))))
2583 (defun widget-editable-list-value-get (widget)
2584 ;; Get value of the child widget.
2585 (apply 'append (mapcar (lambda (child) (widget-apply child :value-inline))
2586 (widget-get widget :children))))
2588 (defun widget-editable-list-match (widget value)
2589 ;; Value must be a list and all the members must match the type.
2590 (and (listp value)
2591 (null (cdr (widget-editable-list-match-inline widget value)))))
2593 (defun widget-editable-list-match-inline (widget value)
2594 (let ((type (nth 0 (widget-get widget :args)))
2595 (ok t)
2596 found)
2597 (while (and value ok)
2598 (let ((answer (widget-match-inline type value)))
2599 (if answer
2600 (setq found (append found (car answer))
2601 value (cdr answer))
2602 (setq ok nil))))
2603 (cons found value)))
2605 (defun widget-editable-list-insert-before (widget before)
2606 ;; Insert a new child in the list of children.
2607 (save-excursion
2608 (let ((children (widget-get widget :children))
2609 (inhibit-read-only t)
2610 before-change-functions
2611 after-change-functions)
2612 (cond (before
2613 (goto-char (widget-get before :entry-from)))
2615 (goto-char (widget-get widget :value-pos))))
2616 (let ((child (widget-editable-list-entry-create
2617 widget nil nil)))
2618 (when (< (widget-get child :entry-from) (widget-get widget :from))
2619 (set-marker (widget-get widget :from)
2620 (widget-get child :entry-from)))
2621 (if (eq (car children) before)
2622 (widget-put widget :children (cons child children))
2623 (while (not (eq (car (cdr children)) before))
2624 (setq children (cdr children)))
2625 (setcdr children (cons child (cdr children)))))))
2626 (widget-setup)
2627 (widget-apply widget :notify widget))
2629 (defun widget-editable-list-delete-at (widget child)
2630 ;; Delete child from list of children.
2631 (save-excursion
2632 (let ((buttons (copy-sequence (widget-get widget :buttons)))
2633 button
2634 (inhibit-read-only t)
2635 before-change-functions
2636 after-change-functions)
2637 (while buttons
2638 (setq button (car buttons)
2639 buttons (cdr buttons))
2640 (when (eq (widget-get button :widget) child)
2641 (widget-put widget
2642 :buttons (delq button (widget-get widget :buttons)))
2643 (widget-delete button))))
2644 (let ((entry-from (widget-get child :entry-from))
2645 (entry-to (widget-get child :entry-to))
2646 (inhibit-read-only t)
2647 before-change-functions
2648 after-change-functions)
2649 (widget-delete child)
2650 (delete-region entry-from entry-to)
2651 (set-marker entry-from nil)
2652 (set-marker entry-to nil))
2653 (widget-put widget :children (delq child (widget-get widget :children))))
2654 (widget-setup)
2655 (widget-apply widget :notify widget))
2657 (defun widget-editable-list-entry-create (widget value conv)
2658 ;; Create a new entry to the list.
2659 (let ((type (nth 0 (widget-get widget :args)))
2660 ;; (widget-push-button-gui widget-editable-list-gui)
2661 child delete insert)
2662 (widget-specify-insert
2663 (save-excursion
2664 (and (widget-get widget :indent)
2665 (insert-char ?\s (widget-get widget :indent)))
2666 (insert (widget-get widget :entry-format)))
2667 ;; Parse % escapes in format.
2668 (while (re-search-forward "%\\(.\\)" nil t)
2669 (let ((escape (char-after (match-beginning 1))))
2670 (delete-backward-char 2)
2671 (cond ((eq escape ?%)
2672 (insert ?%))
2673 ((eq escape ?i)
2674 (setq insert (apply 'widget-create-child-and-convert
2675 widget 'insert-button
2676 (widget-get widget :insert-button-args))))
2677 ((eq escape ?d)
2678 (setq delete (apply 'widget-create-child-and-convert
2679 widget 'delete-button
2680 (widget-get widget :delete-button-args))))
2681 ((eq escape ?v)
2682 (if conv
2683 (setq child (widget-create-child-value
2684 widget type value))
2685 (setq child (widget-create-child-value
2686 widget type (widget-default-get type)))))
2688 (error "Unknown escape `%c'" escape)))))
2689 (let ((buttons (widget-get widget :buttons)))
2690 (if insert (push insert buttons))
2691 (if delete (push delete buttons))
2692 (widget-put widget :buttons buttons))
2693 (let ((entry-from (point-min-marker))
2694 (entry-to (point-max-marker)))
2695 (set-marker-insertion-type entry-from t)
2696 (set-marker-insertion-type entry-to nil)
2697 (widget-put child :entry-from entry-from)
2698 (widget-put child :entry-to entry-to)))
2699 (if insert (widget-put insert :widget child))
2700 (if delete (widget-put delete :widget child))
2701 child))
2703 ;;; The `group' Widget.
2705 (define-widget 'group 'default
2706 "A widget which groups other widgets inside."
2707 :convert-widget 'widget-types-convert-widget
2708 :copy 'widget-types-copy
2709 :format "%v"
2710 :value-create 'widget-group-value-create
2711 :value-get 'widget-editable-list-value-get
2712 :default-get 'widget-group-default-get
2713 :validate 'widget-children-validate
2714 :match 'widget-group-match
2715 :match-inline 'widget-group-match-inline)
2717 (defun widget-group-value-create (widget)
2718 ;; Create each component.
2719 (let ((args (widget-get widget :args))
2720 (value (widget-get widget :value))
2721 arg answer children)
2722 (while args
2723 (setq arg (car args)
2724 args (cdr args)
2725 answer (widget-match-inline arg value)
2726 value (cdr answer))
2727 (and (eq (preceding-char) ?\n)
2728 (widget-get widget :indent)
2729 (insert-char ?\s (widget-get widget :indent)))
2730 (push (cond ((null answer)
2731 (widget-create-child widget arg))
2732 ((widget-get arg :inline)
2733 (widget-create-child-value widget arg (car answer)))
2735 (widget-create-child-value widget arg (car (car answer)))))
2736 children))
2737 (widget-put widget :children (nreverse children))))
2739 (defun widget-group-default-get (widget)
2740 ;; Get the default of the components.
2741 (mapcar 'widget-default-get (widget-get widget :args)))
2743 (defun widget-group-match (widget values)
2744 ;; Match if the components match.
2745 (and (listp values)
2746 (let ((match (widget-group-match-inline widget values)))
2747 (and match (null (cdr match))))))
2749 (defun widget-group-match-inline (widget vals)
2750 ;; Match if the components match.
2751 (let ((args (widget-get widget :args))
2752 argument answer found)
2753 (while args
2754 (setq argument (car args)
2755 args (cdr args)
2756 answer (widget-match-inline argument vals))
2757 (if answer
2758 (setq vals (cdr answer)
2759 found (append found (car answer)))
2760 (setq vals nil
2761 args nil)))
2762 (if answer
2763 (cons found vals))))
2765 ;;; The `visibility' Widget.
2767 (define-widget 'visibility 'item
2768 "An indicator and manipulator for hidden items."
2769 :format "%[%v%]"
2770 :button-prefix ""
2771 :button-suffix ""
2772 :on "Hide"
2773 :off "Show"
2774 :value-create 'widget-visibility-value-create
2775 :action 'widget-toggle-action
2776 :match (lambda (widget value) t))
2778 (defun widget-visibility-value-create (widget)
2779 ;; Insert text representing the `on' and `off' states.
2780 (let ((on (widget-get widget :on))
2781 (off (widget-get widget :off)))
2782 (if on
2783 (setq on (concat widget-push-button-prefix
2785 widget-push-button-suffix))
2786 (setq on ""))
2787 (if off
2788 (setq off (concat widget-push-button-prefix
2790 widget-push-button-suffix))
2791 (setq off ""))
2792 (if (widget-value widget)
2793 (widget-image-insert widget on "down" "down-pushed")
2794 (widget-image-insert widget off "right" "right-pushed"))))
2796 ;;; The `documentation-link' Widget.
2798 ;; This is a helper widget for `documentation-string'.
2800 (define-widget 'documentation-link 'link
2801 "Link type used in documentation strings."
2802 :tab-order -1
2803 :help-echo "Describe this symbol"
2804 :action 'widget-documentation-link-action)
2806 (defun widget-documentation-link-action (widget &optional event)
2807 "Display documentation for WIDGET's value. Ignore optional argument EVENT."
2808 (let* ((string (widget-get widget :value))
2809 (symbol (intern string)))
2810 (if (and (fboundp symbol) (boundp symbol))
2811 ;; If there are two doc strings, give the user a way to pick one.
2812 (apropos (concat "\\`" (regexp-quote string) "\\'"))
2813 (if (fboundp symbol)
2814 (describe-function symbol)
2815 (describe-variable symbol)))))
2817 (defcustom widget-documentation-links t
2818 "Add hyperlinks to documentation strings when non-nil."
2819 :type 'boolean
2820 :group 'widget-documentation)
2822 (defcustom widget-documentation-link-regexp "`\\([^\n`' ]+\\)'"
2823 "Regexp for matching potential links in documentation strings.
2824 The first group should be the link itself."
2825 :type 'regexp
2826 :group 'widget-documentation)
2828 (defcustom widget-documentation-link-p 'intern-soft
2829 "Predicate used to test if a string is useful as a link.
2830 The value should be a function. The function will be called one
2831 argument, a string, and should return non-nil if there should be a
2832 link for that string."
2833 :type 'function
2834 :options '(widget-documentation-link-p)
2835 :group 'widget-documentation)
2837 (defcustom widget-documentation-link-type 'documentation-link
2838 "Widget type used for links in documentation strings."
2839 :type 'symbol
2840 :group 'widget-documentation)
2842 (defun widget-documentation-link-add (widget from to)
2843 (widget-specify-doc widget from to)
2844 (when widget-documentation-links
2845 (let ((regexp widget-documentation-link-regexp)
2846 (buttons (widget-get widget :buttons))
2847 (widget-mouse-face (default-value 'widget-mouse-face))
2848 (widget-button-face widget-documentation-face)
2849 (widget-button-pressed-face widget-documentation-face))
2850 (save-excursion
2851 (goto-char from)
2852 (while (re-search-forward regexp to t)
2853 (let ((name (match-string 1))
2854 (begin (match-beginning 1))
2855 (end (match-end 1)))
2856 (when (funcall widget-documentation-link-p name)
2857 (push (widget-convert-button widget-documentation-link-type
2858 begin end :value name)
2859 buttons)))))
2860 (widget-put widget :buttons buttons)))
2861 (let ((indent (widget-get widget :indent)))
2862 (when (and indent (not (zerop indent)))
2863 (save-excursion
2864 (save-restriction
2865 (narrow-to-region from to)
2866 (goto-char (point-min))
2867 (while (search-forward "\n" nil t)
2868 (insert-char ?\s indent)))))))
2870 ;;; The `documentation-string' Widget.
2872 (define-widget 'documentation-string 'item
2873 "A documentation string."
2874 :format "%v"
2875 :action 'widget-documentation-string-action
2876 :value-create 'widget-documentation-string-value-create)
2878 (defun widget-documentation-string-value-create (widget)
2879 ;; Insert documentation string.
2880 (let ((doc (widget-value widget))
2881 (indent (widget-get widget :indent))
2882 (shown (widget-get (widget-get widget :parent) :documentation-shown))
2883 (start (point)))
2884 (if (string-match "\n" doc)
2885 (let ((before (substring doc 0 (match-beginning 0)))
2886 (after (substring doc (match-beginning 0)))
2887 button)
2888 (insert before ?\s)
2889 (widget-documentation-link-add widget start (point))
2890 (setq button
2891 (widget-create-child-and-convert
2892 widget 'visibility
2893 :help-echo "Show or hide rest of the documentation."
2894 :on "Hide Rest"
2895 :off "More"
2896 :always-active t
2897 :action 'widget-parent-action
2898 shown))
2899 (when shown
2900 (setq start (point))
2901 (when (and indent (not (zerop indent)))
2902 (insert-char ?\s indent))
2903 (insert after)
2904 (widget-documentation-link-add widget start (point)))
2905 (widget-put widget :buttons (list button)))
2906 (insert doc)
2907 (widget-documentation-link-add widget start (point))))
2908 (insert ?\n))
2910 (defun widget-documentation-string-action (widget &rest ignore)
2911 ;; Toggle documentation.
2912 (let ((parent (widget-get widget :parent)))
2913 (widget-put parent :documentation-shown
2914 (not (widget-get parent :documentation-shown))))
2915 ;; Redraw.
2916 (widget-value-set widget (widget-value widget)))
2918 ;;; The Sexp Widgets.
2920 (define-widget 'const 'item
2921 "An immutable sexp."
2922 :prompt-value 'widget-const-prompt-value
2923 :format "%t\n%d")
2925 (defun widget-const-prompt-value (widget prompt value unbound)
2926 ;; Return the value of the const.
2927 (widget-value widget))
2929 (define-widget 'function-item 'const
2930 "An immutable function name."
2931 :format "%v\n%h"
2932 :documentation-property (lambda (symbol)
2933 (condition-case nil
2934 (documentation symbol t)
2935 (error nil))))
2937 (define-widget 'variable-item 'const
2938 "An immutable variable name."
2939 :format "%v\n%h"
2940 :documentation-property 'variable-documentation)
2942 (define-widget 'other 'sexp
2943 "Matches any value, but doesn't let the user edit the value.
2944 This is useful as last item in a `choice' widget.
2945 You should use this widget type with a default value,
2946 as in (other DEFAULT) or (other :tag \"NAME\" DEFAULT).
2947 If the user selects this alternative, that specifies DEFAULT
2948 as the value."
2949 :tag "Other"
2950 :format "%t%n"
2951 :value 'other)
2953 (defvar widget-string-prompt-value-history nil
2954 "History of input to `widget-string-prompt-value'.")
2956 (define-widget 'string 'editable-field
2957 "A string"
2958 :tag "String"
2959 :format "%{%t%}: %v"
2960 :complete-function 'ispell-complete-word
2961 :prompt-history 'widget-string-prompt-value-history)
2963 (define-widget 'regexp 'string
2964 "A regular expression."
2965 :match 'widget-regexp-match
2966 :validate 'widget-regexp-validate
2967 ;; Doesn't work well with terminating newline.
2968 ;; :value-face 'widget-single-line-field
2969 :tag "Regexp")
2971 (defun widget-regexp-match (widget value)
2972 ;; Match valid regexps.
2973 (and (stringp value)
2974 (condition-case nil
2975 (prog1 t
2976 (string-match value ""))
2977 (error nil))))
2979 (defun widget-regexp-validate (widget)
2980 "Check that the value of WIDGET is a valid regexp."
2981 (condition-case data
2982 (prog1 nil
2983 (string-match (widget-value widget) ""))
2984 (error (widget-put widget :error (error-message-string data))
2985 widget)))
2987 (define-widget 'file 'string
2988 "A file widget.
2989 It will read a file name from the minibuffer when invoked."
2990 :complete-function 'widget-file-complete
2991 :prompt-value 'widget-file-prompt-value
2992 :format "%{%t%}: %v"
2993 ;; Doesn't work well with terminating newline.
2994 ;; :value-face 'widget-single-line-field
2995 :tag "File")
2997 (defun widget-file-complete ()
2998 "Perform completion on file name preceding point."
2999 (interactive)
3000 (let* ((end (point))
3001 (beg (widget-field-start widget))
3002 (pattern (buffer-substring beg end))
3003 (name-part (file-name-nondirectory pattern))
3004 ;; I think defaulting to root is right
3005 ;; because these really should be absolute file names.
3006 (directory (or (file-name-directory pattern) "/"))
3007 (completion (file-name-completion name-part directory)))
3008 (cond ((eq completion t))
3009 ((null completion)
3010 (message "Can't find completion for \"%s\"" pattern)
3011 (ding))
3012 ((not (string= name-part completion))
3013 (delete-region beg end)
3014 (insert (expand-file-name completion directory)))
3016 (message "Making completion list...")
3017 (with-output-to-temp-buffer "*Completions*"
3018 (display-completion-list
3019 (sort (file-name-all-completions name-part directory)
3020 'string<)
3021 name-part))
3022 (message "Making completion list...%s" "done")))))
3024 (defun widget-file-prompt-value (widget prompt value unbound)
3025 ;; Read file from minibuffer.
3026 (abbreviate-file-name
3027 (if unbound
3028 (read-file-name prompt)
3029 (let ((prompt2 (format "%s (default %s): " prompt value))
3030 (dir (file-name-directory value))
3031 (file (file-name-nondirectory value))
3032 (must-match (widget-get widget :must-match)))
3033 (read-file-name prompt2 dir nil must-match file)))))
3035 ;;;(defun widget-file-action (widget &optional event)
3036 ;;; ;; Read a file name from the minibuffer.
3037 ;;; (let* ((value (widget-value widget))
3038 ;;; (dir (file-name-directory value))
3039 ;;; (file (file-name-nondirectory value))
3040 ;;; (menu-tag (widget-apply widget :menu-tag-get))
3041 ;;; (must-match (widget-get widget :must-match))
3042 ;;; (answer (read-file-name (concat menu-tag " (default " value "): ")
3043 ;;; dir nil must-match file)))
3044 ;;; (widget-value-set widget (abbreviate-file-name answer))
3045 ;;; (widget-setup)
3046 ;;; (widget-apply widget :notify widget event)))
3048 ;; Fixme: use file-name-as-directory.
3049 (define-widget 'directory 'file
3050 "A directory widget.
3051 It will read a directory name from the minibuffer when invoked."
3052 :tag "Directory")
3054 (defvar widget-symbol-prompt-value-history nil
3055 "History of input to `widget-symbol-prompt-value'.")
3057 (define-widget 'symbol 'editable-field
3058 "A Lisp symbol."
3059 :value nil
3060 :tag "Symbol"
3061 :format "%{%t%}: %v"
3062 :match (lambda (widget value) (symbolp value))
3063 :complete-function 'lisp-complete-symbol
3064 :prompt-internal 'widget-symbol-prompt-internal
3065 :prompt-match 'symbolp
3066 :prompt-history 'widget-symbol-prompt-value-history
3067 :value-to-internal (lambda (widget value)
3068 (if (symbolp value)
3069 (symbol-name value)
3070 value))
3071 :value-to-external (lambda (widget value)
3072 (if (stringp value)
3073 (intern value)
3074 value)))
3076 (defun widget-symbol-prompt-internal (widget prompt initial history)
3077 ;; Read file from minibuffer.
3078 (let ((answer (completing-read prompt obarray
3079 (widget-get widget :prompt-match)
3080 nil initial history)))
3081 (if (and (stringp answer)
3082 (not (zerop (length answer))))
3083 answer
3084 (error "No value"))))
3086 (defvar widget-function-prompt-value-history nil
3087 "History of input to `widget-function-prompt-value'.")
3089 (define-widget 'function 'restricted-sexp
3090 "A Lisp function."
3091 :complete-function (lambda ()
3092 (interactive)
3093 (lisp-complete-symbol 'fboundp))
3094 :prompt-value 'widget-field-prompt-value
3095 :prompt-internal 'widget-symbol-prompt-internal
3096 :prompt-match 'fboundp
3097 :prompt-history 'widget-function-prompt-value-history
3098 :action 'widget-field-action
3099 :match-alternatives '(functionp)
3100 :validate (lambda (widget)
3101 (unless (functionp (widget-value widget))
3102 (widget-put widget :error (format "Invalid function: %S"
3103 (widget-value widget)))
3104 widget))
3105 :value 'ignore
3106 :tag "Function")
3108 (defvar widget-variable-prompt-value-history nil
3109 "History of input to `widget-variable-prompt-value'.")
3111 (define-widget 'variable 'symbol
3112 "A Lisp variable."
3113 :prompt-match 'boundp
3114 :prompt-history 'widget-variable-prompt-value-history
3115 :complete-function (lambda ()
3116 (interactive)
3117 (lisp-complete-symbol 'boundp))
3118 :tag "Variable")
3120 (defvar widget-coding-system-prompt-value-history nil
3121 "History of input to `widget-coding-system-prompt-value'.")
3123 (define-widget 'coding-system 'symbol
3124 "A MULE coding-system."
3125 :format "%{%t%}: %v"
3126 :tag "Coding system"
3127 :base-only nil
3128 :prompt-history 'widget-coding-system-prompt-value-history
3129 :prompt-value 'widget-coding-system-prompt-value
3130 :action 'widget-coding-system-action
3131 :complete-function (lambda ()
3132 (interactive)
3133 (lisp-complete-symbol 'coding-system-p))
3134 :validate (lambda (widget)
3135 (unless (coding-system-p (widget-value widget))
3136 (widget-put widget :error (format "Invalid coding system: %S"
3137 (widget-value widget)))
3138 widget))
3139 :value 'undecided
3140 :prompt-match 'coding-system-p)
3142 (defun widget-coding-system-prompt-value (widget prompt value unbound)
3143 "Read coding-system from minibuffer."
3144 (if (widget-get widget :base-only)
3145 (intern
3146 (completing-read (format "%s (default %s): " prompt value)
3147 (mapcar #'list (coding-system-list t)) nil nil nil
3148 coding-system-history))
3149 (read-coding-system (format "%s (default %s): " prompt value) value)))
3151 (defun widget-coding-system-action (widget &optional event)
3152 (let ((answer
3153 (widget-coding-system-prompt-value
3154 widget
3155 (widget-apply widget :menu-tag-get)
3156 (widget-value widget)
3157 t)))
3158 (widget-value-set widget answer)
3159 (widget-apply widget :notify widget event)
3160 (widget-setup)))
3162 (define-widget 'sexp 'editable-field
3163 "An arbitrary Lisp expression."
3164 :tag "Lisp expression"
3165 :format "%{%t%}: %v"
3166 :value nil
3167 :validate 'widget-sexp-validate
3168 :match (lambda (widget value) t)
3169 :value-to-internal 'widget-sexp-value-to-internal
3170 :value-to-external (lambda (widget value) (read value))
3171 :prompt-history 'widget-sexp-prompt-value-history
3172 :prompt-value 'widget-sexp-prompt-value)
3174 (defun widget-sexp-value-to-internal (widget value)
3175 ;; Use pp for printer representation.
3176 (let ((pp (if (symbolp value)
3177 (prin1-to-string value)
3178 (pp-to-string value))))
3179 (while (string-match "\n\\'" pp)
3180 (setq pp (substring pp 0 -1)))
3181 (if (or (string-match "\n\\'" pp)
3182 (> (length pp) 40))
3183 (concat "\n" pp)
3184 pp)))
3186 (defun widget-sexp-validate (widget)
3187 ;; Valid if we can read the string and there is no junk left after it.
3188 (with-temp-buffer
3189 (insert (widget-apply widget :value-get))
3190 (goto-char (point-min))
3191 (let (err)
3192 (condition-case data
3193 (progn
3194 ;; Avoid a confusing end-of-file error.
3195 (skip-syntax-forward "\\s-")
3196 (if (eobp)
3197 (setq err "Empty sexp -- use `nil'?")
3198 (unless (widget-apply widget :match (read (current-buffer)))
3199 (setq err (widget-get widget :type-error))))
3200 ;; Allow whitespace after expression.
3201 (skip-syntax-forward "\\s-")
3202 (if (and (not (eobp))
3203 (not err))
3204 (setq err (format "Junk at end of expression: %s"
3205 (buffer-substring (point)
3206 (point-max))))))
3207 (end-of-file ; Avoid confusing error message.
3208 (setq err "Unbalanced sexp"))
3209 (error (setq err (error-message-string data))))
3210 (if (not err)
3212 (widget-put widget :error err)
3213 widget))))
3215 (defvar widget-sexp-prompt-value-history nil
3216 "History of input to `widget-sexp-prompt-value'.")
3218 (defun widget-sexp-prompt-value (widget prompt value unbound)
3219 ;; Read an arbitrary sexp.
3220 (let ((found (read-string prompt
3221 (if unbound nil (cons (prin1-to-string value) 0))
3222 (widget-get widget :prompt-history))))
3223 (let ((answer (read-from-string found)))
3224 (unless (= (cdr answer) (length found))
3225 (error "Junk at end of expression: %s"
3226 (substring found (cdr answer))))
3227 (car answer))))
3229 (define-widget 'restricted-sexp 'sexp
3230 "A Lisp expression restricted to values that match.
3231 To use this type, you must define :match or :match-alternatives."
3232 :type-error "The specified value is not valid"
3233 :match 'widget-restricted-sexp-match
3234 :value-to-internal (lambda (widget value)
3235 (if (widget-apply widget :match value)
3236 (prin1-to-string value)
3237 value)))
3239 (defun widget-restricted-sexp-match (widget value)
3240 (let ((alternatives (widget-get widget :match-alternatives))
3241 matched)
3242 (while (and alternatives (not matched))
3243 (if (cond ((functionp (car alternatives))
3244 (funcall (car alternatives) value))
3245 ((and (consp (car alternatives))
3246 (eq (car (car alternatives)) 'quote))
3247 (eq value (nth 1 (car alternatives)))))
3248 (setq matched t))
3249 (setq alternatives (cdr alternatives)))
3250 matched))
3252 (define-widget 'integer 'restricted-sexp
3253 "An integer."
3254 :tag "Integer"
3255 :value 0
3256 :type-error "This field should contain an integer"
3257 :match-alternatives '(integerp))
3259 (define-widget 'number 'restricted-sexp
3260 "A number (floating point or integer)."
3261 :tag "Number"
3262 :value 0.0
3263 :type-error "This field should contain a number (floating point or integer)"
3264 :match-alternatives '(numberp))
3266 (define-widget 'float 'restricted-sexp
3267 "A floating point number."
3268 :tag "Floating point number"
3269 :value 0.0
3270 :type-error "This field should contain a floating point number"
3271 :match-alternatives '(floatp))
3273 (define-widget 'character 'editable-field
3274 "A character."
3275 :tag "Character"
3276 :value 0
3277 :size 1
3278 :format "%{%t%}: %v\n"
3279 :valid-regexp "\\`.\\'"
3280 :error "This field should contain a single character"
3281 :value-to-internal (lambda (widget value)
3282 (if (stringp value)
3283 value
3284 (char-to-string value)))
3285 :value-to-external (lambda (widget value)
3286 (if (stringp value)
3287 (aref value 0)
3288 value))
3289 :match (lambda (widget value)
3290 (char-valid-p value)))
3292 (define-widget 'list 'group
3293 "A Lisp list."
3294 :tag "List"
3295 :format "%{%t%}:\n%v")
3297 (define-widget 'vector 'group
3298 "A Lisp vector."
3299 :tag "Vector"
3300 :format "%{%t%}:\n%v"
3301 :match 'widget-vector-match
3302 :value-to-internal (lambda (widget value) (append value nil))
3303 :value-to-external (lambda (widget value) (apply 'vector value)))
3305 (defun widget-vector-match (widget value)
3306 (and (vectorp value)
3307 (widget-group-match widget
3308 (widget-apply widget :value-to-internal value))))
3310 (define-widget 'cons 'group
3311 "A cons-cell."
3312 :tag "Cons-cell"
3313 :format "%{%t%}:\n%v"
3314 :match 'widget-cons-match
3315 :value-to-internal (lambda (widget value)
3316 (list (car value) (cdr value)))
3317 :value-to-external (lambda (widget value)
3318 (apply 'cons value)))
3320 (defun widget-cons-match (widget value)
3321 (and (consp value)
3322 (widget-group-match widget
3323 (widget-apply widget :value-to-internal value))))
3325 ;;; The `lazy' Widget.
3327 ;; Recursive datatypes.
3329 (define-widget 'lazy 'default
3330 "Base widget for recursive datastructures.
3332 The `lazy' widget will, when instantiated, contain a single inferior
3333 widget, of the widget type specified by the :type parameter. The
3334 value of the `lazy' widget is the same as the value of the inferior
3335 widget. When deriving a new widget from the 'lazy' widget, the :type
3336 parameter is allowed to refer to the widget currently being defined,
3337 thus allowing recursive datastructures to be described.
3339 The :type parameter takes the same arguments as the defcustom
3340 parameter with the same name.
3342 Most composite widgets, i.e. widgets containing other widgets, does
3343 not allow recursion. That is, when you define a new widget type, none
3344 of the inferior widgets may be of the same type you are currently
3345 defining.
3347 In Lisp, however, it is custom to define datastructures in terms of
3348 themselves. A list, for example, is defined as either nil, or a cons
3349 cell whose cdr itself is a list. The obvious way to translate this
3350 into a widget type would be
3352 (define-widget 'my-list 'choice
3353 \"A list of sexps.\"
3354 :tag \"Sexp list\"
3355 :args '((const nil) (cons :value (nil) sexp my-list)))
3357 Here we attempt to define my-list as a choice of either the constant
3358 nil, or a cons-cell containing a sexp and my-lisp. This will not work
3359 because the `choice' widget does not allow recursion.
3361 Using the `lazy' widget you can overcome this problem, as in this
3362 example:
3364 (define-widget 'sexp-list 'lazy
3365 \"A list of sexps.\"
3366 :tag \"Sexp list\"
3367 :type '(choice (const nil) (cons :value (nil) sexp sexp-list)))"
3368 :format "%{%t%}: %v"
3369 ;; We don't convert :type because we want to allow recursive
3370 ;; datastructures. This is slow, so we should not create speed
3371 ;; critical widgets by deriving from this.
3372 :convert-widget 'widget-value-convert-widget
3373 :value-create 'widget-type-value-create
3374 :value-get 'widget-child-value-get
3375 :value-inline 'widget-child-value-inline
3376 :default-get 'widget-type-default-get
3377 :match 'widget-type-match
3378 :validate 'widget-child-validate)
3381 ;;; The `plist' Widget.
3383 ;; Property lists.
3385 (define-widget 'plist 'list
3386 "A property list."
3387 :key-type '(symbol :tag "Key")
3388 :value-type '(sexp :tag "Value")
3389 :convert-widget 'widget-plist-convert-widget
3390 :tag "Plist")
3392 (defvar widget-plist-value-type) ;Dynamic variable
3394 (defun widget-plist-convert-widget (widget)
3395 ;; Handle `:options'.
3396 (let* ((options (widget-get widget :options))
3397 (widget-plist-value-type (widget-get widget :value-type))
3398 (other `(editable-list :inline t
3399 (group :inline t
3400 ,(widget-get widget :key-type)
3401 ,widget-plist-value-type)))
3402 (args (if options
3403 (list `(checklist :inline t
3404 :greedy t
3405 ,@(mapcar 'widget-plist-convert-option
3406 options))
3407 other)
3408 (list other))))
3409 (widget-put widget :args args)
3410 widget))
3412 (defun widget-plist-convert-option (option)
3413 ;; Convert a single plist option.
3414 (let (key-type value-type)
3415 (if (listp option)
3416 (let ((key (nth 0 option)))
3417 (setq value-type (nth 1 option))
3418 (if (listp key)
3419 (setq key-type key)
3420 (setq key-type `(const ,key))))
3421 (setq key-type `(const ,option)
3422 value-type widget-plist-value-type))
3423 `(group :format "Key: %v" :inline t ,key-type ,value-type)))
3426 ;;; The `alist' Widget.
3428 ;; Association lists.
3430 (define-widget 'alist 'list
3431 "An association list."
3432 :key-type '(sexp :tag "Key")
3433 :value-type '(sexp :tag "Value")
3434 :convert-widget 'widget-alist-convert-widget
3435 :tag "Alist")
3437 (defvar widget-alist-value-type) ;Dynamic variable
3439 (defun widget-alist-convert-widget (widget)
3440 ;; Handle `:options'.
3441 (let* ((options (widget-get widget :options))
3442 (widget-alist-value-type (widget-get widget :value-type))
3443 (other `(editable-list :inline t
3444 (cons :format "%v"
3445 ,(widget-get widget :key-type)
3446 ,widget-alist-value-type)))
3447 (args (if options
3448 (list `(checklist :inline t
3449 :greedy t
3450 ,@(mapcar 'widget-alist-convert-option
3451 options))
3452 other)
3453 (list other))))
3454 (widget-put widget :args args)
3455 widget))
3457 (defun widget-alist-convert-option (option)
3458 ;; Convert a single alist option.
3459 (let (key-type value-type)
3460 (if (listp option)
3461 (let ((key (nth 0 option)))
3462 (setq value-type (nth 1 option))
3463 (if (listp key)
3464 (setq key-type key)
3465 (setq key-type `(const ,key))))
3466 (setq key-type `(const ,option)
3467 value-type widget-alist-value-type))
3468 `(cons :format "Key: %v" ,key-type ,value-type)))
3470 (define-widget 'choice 'menu-choice
3471 "A union of several sexp types."
3472 :tag "Choice"
3473 :format "%{%t%}: %[Value Menu%] %v"
3474 :button-prefix 'widget-push-button-prefix
3475 :button-suffix 'widget-push-button-suffix
3476 :prompt-value 'widget-choice-prompt-value)
3478 (defun widget-choice-prompt-value (widget prompt value unbound)
3479 "Make a choice."
3480 (let ((args (widget-get widget :args))
3481 (completion-ignore-case (widget-get widget :case-fold))
3482 current choices old)
3483 ;; Find the first arg that matches VALUE.
3484 (let ((look args))
3485 (while look
3486 (if (widget-apply (car look) :match value)
3487 (setq old (car look)
3488 look nil)
3489 (setq look (cdr look)))))
3490 ;; Find new choice.
3491 (setq current
3492 (cond ((= (length args) 0)
3493 nil)
3494 ((= (length args) 1)
3495 (nth 0 args))
3496 ((and (= (length args) 2)
3497 (memq old args))
3498 (if (eq old (nth 0 args))
3499 (nth 1 args)
3500 (nth 0 args)))
3502 (while args
3503 (setq current (car args)
3504 args (cdr args))
3505 (setq choices
3506 (cons (cons (widget-apply current :menu-tag-get)
3507 current)
3508 choices)))
3509 (let ((val (completing-read prompt choices nil t)))
3510 (if (stringp val)
3511 (let ((try (try-completion val choices)))
3512 (when (stringp try)
3513 (setq val try))
3514 (cdr (assoc val choices)))
3515 nil)))))
3516 (if current
3517 (widget-prompt-value current prompt nil t)
3518 value)))
3520 (define-widget 'radio 'radio-button-choice
3521 "A union of several sexp types."
3522 :tag "Choice"
3523 :format "%{%t%}:\n%v"
3524 :prompt-value 'widget-choice-prompt-value)
3526 (define-widget 'repeat 'editable-list
3527 "A variable length homogeneous list."
3528 :tag "Repeat"
3529 :format "%{%t%}:\n%v%i\n")
3531 (define-widget 'set 'checklist
3532 "A list of members from a fixed set."
3533 :tag "Set"
3534 :format "%{%t%}:\n%v")
3536 (define-widget 'boolean 'toggle
3537 "To be nil or non-nil, that is the question."
3538 :tag "Boolean"
3539 :prompt-value 'widget-boolean-prompt-value
3540 :button-prefix 'widget-push-button-prefix
3541 :button-suffix 'widget-push-button-suffix
3542 :format "%{%t%}: %[Toggle%] %v\n"
3543 :on "on (non-nil)"
3544 :off "off (nil)")
3546 (defun widget-boolean-prompt-value (widget prompt value unbound)
3547 ;; Toggle a boolean.
3548 (y-or-n-p prompt))
3550 ;;; The `color' Widget.
3552 ;; Fixme: match
3553 (define-widget 'color 'editable-field
3554 "Choose a color name (with sample)."
3555 :format "%t: %v (%{sample%})\n"
3556 :size 10
3557 :tag "Color"
3558 :value "black"
3559 :complete 'widget-color-complete
3560 :sample-face-get 'widget-color-sample-face-get
3561 :notify 'widget-color-notify
3562 :action 'widget-color-action)
3564 (defun widget-color-complete (widget)
3565 "Complete the color in WIDGET."
3566 (require 'facemenu) ; for facemenu-color-alist
3567 (let* ((prefix (buffer-substring-no-properties (widget-field-start widget)
3568 (point)))
3569 (list (or facemenu-color-alist (defined-colors)))
3570 (completion (try-completion prefix list)))
3571 (cond ((eq completion t)
3572 (message "Exact match."))
3573 ((null completion)
3574 (error "Can't find completion for \"%s\"" prefix))
3575 ((not (string-equal prefix completion))
3576 (insert-and-inherit (substring completion (length prefix))))
3578 (message "Making completion list...")
3579 (with-output-to-temp-buffer "*Completions*"
3580 (display-completion-list (all-completions prefix list nil)
3581 prefix))
3582 (message "Making completion list...done")))))
3584 (defun widget-color-sample-face-get (widget)
3585 (let* ((value (condition-case nil
3586 (widget-value widget)
3587 (error (widget-get widget :value)))))
3588 (if (color-defined-p value)
3589 (list (cons 'foreground-color value))
3590 'default)))
3592 (defun widget-color-action (widget &optional event)
3593 "Prompt for a color."
3594 (let* ((tag (widget-apply widget :menu-tag-get))
3595 (prompt (concat tag ": "))
3596 (value (widget-value widget))
3597 (start (widget-field-start widget))
3598 (answer (facemenu-read-color prompt)))
3599 (unless (zerop (length answer))
3600 (widget-value-set widget answer)
3601 (widget-setup)
3602 (widget-apply widget :notify widget event))))
3604 (defun widget-color-notify (widget child &optional event)
3605 "Update the sample, and notify the parent."
3606 (overlay-put (widget-get widget :sample-overlay)
3607 'face (widget-apply widget :sample-face-get))
3608 (widget-default-notify widget child event))
3610 ;;; The Help Echo
3612 (defun widget-echo-help (pos)
3613 "Display help-echo text for widget at POS."
3614 (let* ((widget (widget-at pos))
3615 (help-echo (and widget (widget-get widget :help-echo))))
3616 (if (functionp help-echo)
3617 (setq help-echo (funcall help-echo widget)))
3618 (if help-echo (message "%s" (eval help-echo)))))
3620 ;;; The End:
3622 (provide 'wid-edit)
3624 ;;; arch-tag: a076e75e-18a1-4b46-8be5-3f317bcbc707
3625 ;;; wid-edit.el ends here