(define-minor-mode): Remove :toggle arg.
[emacs.git] / lisp / wid-edit.el
blobdeed46ddcda0fec7d2ebfa828005dde7139b9364
1 ;;; wid-edit.el --- Functions for creating and using widgets -*-byte-compile-dynamic: t;-*-
2 ;;
3 ;; Copyright (C) 1996, 1997, 1999, 2000 Free Software Foundation, Inc.
4 ;;
5 ;; Author: Per Abrahamsen <abraham@dina.kvl.dk>
6 ;; Maintainer: FSF
7 ;; Keywords: extensions
9 ;; This file is part of GNU Emacs.
11 ;; GNU Emacs is free software; you can redistribute it and/or modify
12 ;; it under the terms of the GNU General Public License as published by
13 ;; the Free Software Foundation; either version 2, or (at your option)
14 ;; any later version.
16 ;; GNU Emacs is distributed in the hope that it will be useful,
17 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
18 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 ;; GNU General Public License for more details.
21 ;; You should have received a copy of the GNU General Public License
22 ;; along with GNU Emacs; see the file COPYING. If not, write to the
23 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
24 ;; Boston, MA 02111-1307, USA.
26 ;;; Wishlist items (from widget.texi):
28 ;; * The `menu-choice' tag should be prettier, something like the
29 ;; abbreviated menus in Open Look.
31 ;; * Finish `:tab-order'.
33 ;; * Make indentation work with glyphs and proportional fonts.
35 ;; * Add commands to show overview of object and class hierarchies to
36 ;; the browser.
38 ;; * Find a way to disable mouse highlight for inactive widgets.
40 ;; * Find a way to make glyphs look inactive.
42 ;; * Add `key-binding' widget.
44 ;; * Add `widget' widget for editing widget specifications.
46 ;; * Find clean way to implement variable length list. See
47 ;; `TeX-printer-list' for an explanation.
49 ;; * `C-h' in `widget-prompt-value' should give type specific help.
51 ;; * A mailto widget. [This should work OK as a url-link if with
52 ;; browse-url-browser-function' set up appropriately.]
54 ;;; Commentary:
56 ;; See `widget.el'.
58 ;;; Code:
60 ;;; Compatibility.
62 (defun widget-event-point (event)
63 "Character position of the end of event if that exists, or nil."
64 (posn-point (event-end event)))
66 (autoload 'pp-to-string "pp")
67 (autoload 'Info-goto-node "info")
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-face
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-face '((((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)
110 (defvar widget-button-face 'widget-button-face
111 "Face used for buttons in widgets.
112 This exists as a variable so it can be set locally in certain buffers.")
114 (defface widget-button-face '((t (:bold t)))
115 "Face used for widget buttons."
116 :group 'widget-faces)
118 (defcustom widget-mouse-face 'highlight
119 "Face used for widget buttons when the mouse is above them."
120 :type 'face
121 :group 'widget-faces)
123 (defface widget-field-face '((((class grayscale color)
124 (background light))
125 (:background "gray85"))
126 (((class grayscale color)
127 (background dark))
128 (:background "dim gray"))
130 (:italic t)))
131 "Face used for editable fields."
132 :group 'widget-faces)
134 (defface widget-single-line-field-face '((((class grayscale color)
135 (background light))
136 (:background "gray85"))
137 (((class grayscale color)
138 (background dark))
139 (:background "dim gray"))
141 (:italic t)))
142 "Face used for editable fields spanning only a single line."
143 :group 'widget-faces)
145 ;;; This causes display-table to be loaded, and not usefully.
146 ;;;(defvar widget-single-line-display-table
147 ;;; (let ((table (make-display-table)))
148 ;;; (aset table 9 "^I")
149 ;;; (aset table 10 "^J")
150 ;;; table)
151 ;;; "Display table used for single-line editable fields.")
153 ;;;(when (fboundp 'set-face-display-table)
154 ;;; (set-face-display-table 'widget-single-line-field-face
155 ;;; widget-single-line-display-table))
157 ;;; Utility functions.
159 ;; These are not really widget specific.
161 (defun widget-princ-to-string (object)
162 "Return string representation of OBJECT, any Lisp object.
163 No quoting characters are used; no delimiters are printed around
164 the contents of strings."
165 (with-output-to-string
166 (princ object)))
168 (defun widget-clear-undo ()
169 "Clear all undo information."
170 (buffer-disable-undo (current-buffer))
171 (buffer-enable-undo))
173 (defcustom widget-menu-max-size 40
174 "Largest number of items allowed in a popup-menu.
175 Larger menus are read through the minibuffer."
176 :group 'widgets
177 :type 'integer)
179 (defcustom widget-menu-max-shortcuts 40
180 "Largest number of items for which it works to choose one with a character.
181 For a larger number of items, the minibuffer is used."
182 :group 'widgets
183 :type 'integer)
185 (defcustom widget-menu-minibuffer-flag nil
186 "*Control how to ask for a choice from the keyboard.
187 Non-nil means use the minibuffer;
188 nil means read a single character."
189 :group 'widgets
190 :type 'boolean)
192 (defun widget-choose (title items &optional event)
193 "Choose an item from a list.
195 First argument TITLE is the name of the list.
196 Second argument ITEMS is an list whose members are either
197 (NAME . VALUE), to indicate selectable items, or just strings to
198 indicate unselectable items.
199 Optional third argument EVENT is an input event.
201 The user is asked to choose between each NAME from the items alist,
202 and the VALUE of the chosen element will be returned. If EVENT is a
203 mouse event, and the number of elements in items is less than
204 `widget-menu-max-size', a popup menu will be used, otherwise the
205 minibuffer."
206 (cond ((and (< (length items) widget-menu-max-size)
207 event (display-mouse-p))
208 ;; Mouse click.
209 (x-popup-menu event
210 (list title (cons "" items))))
211 ((or widget-menu-minibuffer-flag
212 (> (length items) widget-menu-max-shortcuts))
213 ;; Read the choice of name from the minibuffer.
214 (setq items (widget-remove-if 'stringp items))
215 (let ((val (completing-read (concat title ": ") items nil t)))
216 (if (stringp val)
217 (let ((try (try-completion val items)))
218 (when (stringp try)
219 (setq val try))
220 (cdr (assoc val items))))))
222 ;; Construct a menu of the choices
223 ;; and then use it for prompting for a single character.
224 (let* ((overriding-terminal-local-map (make-sparse-keymap))
225 (next-digit ?0)
226 map choice some-choice-enabled value)
227 ;; Define SPC as a prefix char to get to this menu.
228 (define-key overriding-terminal-local-map " "
229 (setq map (make-sparse-keymap title)))
230 (save-excursion
231 (set-buffer (get-buffer-create " widget-choose"))
232 (erase-buffer)
233 (insert "Available choices:\n\n")
234 (while items
235 (setq choice (car items) items (cdr items))
236 (if (consp choice)
237 (let* ((name (car choice))
238 (function (cdr choice)))
239 (insert (format "%c = %s\n" next-digit name))
240 (define-key map (vector next-digit) function)
241 (setq some-choice-enabled t)))
242 ;; Allocate digits to disabled alternatives
243 ;; so that the digit of a given alternative never varies.
244 (setq next-digit (1+ next-digit)))
245 (insert "\nC-g = Quit"))
246 (or some-choice-enabled
247 (error "None of the choices is currently meaningful"))
248 (define-key map [?\C-g] 'keyboard-quit)
249 (define-key map [t] 'keyboard-quit)
250 (define-key map [?\M-\C-v] 'scroll-other-window)
251 (define-key map [?\M--] 'negative-argument)
252 (setcdr map (nreverse (cdr map)))
253 ;; Read a char with the menu, and return the result
254 ;; that corresponds to it.
255 (save-window-excursion
256 (let ((buf (get-buffer " widget-choose")))
257 (display-buffer buf)
258 (let ((cursor-in-echo-area t)
259 keys
260 (char 0)
261 (arg 1))
262 (while (not (or (and (>= char ?0) (< char next-digit))
263 (eq value 'keyboard-quit)))
264 ;; Unread a SPC to lead to our new menu.
265 (setq unread-command-events (cons ?\ unread-command-events))
266 (setq keys (read-key-sequence title))
267 (setq value
268 (lookup-key overriding-terminal-local-map keys t)
269 char (string-to-char (substring keys 1)))
270 (cond ((eq value 'scroll-other-window)
271 (let ((minibuffer-scroll-window
272 (get-buffer-window buf)))
273 (if (> 0 arg)
274 (scroll-other-window-down
275 (window-height minibuffer-scroll-window))
276 (scroll-other-window))
277 (setq arg 1)))
278 ((eq value 'negative-argument)
279 (setq arg -1))
281 (setq arg 1)))))))
282 (when (eq value 'keyboard-quit)
283 (error "Canceled"))
284 value))))
286 (defun widget-remove-if (predictate list)
287 (let (result (tail list))
288 (while tail
289 (or (funcall predictate (car tail))
290 (setq result (cons (car tail) result)))
291 (setq tail (cdr tail)))
292 (nreverse result)))
294 ;;; Widget text specifications.
296 ;; These functions are for specifying text properties.
298 (defvar widget-field-add-space t
299 "Non-nil means add extra space at the end of editable text fields.
300 If you don't add the space, it will become impossible to edit a zero
301 size field.")
303 (defvar widget-field-use-before-change t
304 "Non-nil means use `before-change-functions' to track editable fields.
305 This enables the use of undo, but doesn't work on Emacs 19.34 and earlier.
306 Using before hooks also means that the :notify function can't know the
307 new value.")
309 (defun widget-specify-field (widget from to)
310 "Specify editable button for WIDGET between FROM and TO."
311 ;; Terminating space is not part of the field, but necessary in
312 ;; order for local-map to work. Remove next sexp if local-map works
313 ;; at the end of the overlay.
314 (save-excursion
315 (goto-char to)
316 (cond ((null (widget-get widget :size))
317 (forward-char 1))
318 (widget-field-add-space
319 (insert-and-inherit " ")))
320 (setq to (point)))
321 (let ((keymap (widget-get widget :keymap))
322 (face (or (widget-get widget :value-face) 'widget-field-face))
323 (help-echo (widget-get widget :help-echo))
324 (rear-sticky
325 (or (not widget-field-add-space) (widget-get widget :size))))
326 (when (= (char-before to) ?\n)
327 ;; When the last character in the field is a newline, we want to
328 ;; give it a `field' char-property of `boundary', which helps the
329 ;; C-n/C-p act more naturally when entering/leaving the field. We
330 ;; do this by making a small secondary overlay to contain just that
331 ;; one character.
332 (let ((overlay (make-overlay (1- to) to nil t nil)))
333 (overlay-put overlay 'field 'boundary)
334 ;; Use `local-map' here, not `keymap', so that normal editing
335 ;; works in the field when, say, Custom uses `suppress-keymap'.
336 (overlay-put overlay 'local-map keymap)
337 (overlay-put overlay 'face face)
338 (overlay-put overlay 'help-echo help-echo))
339 (setq to (1- to))
340 (setq rear-sticky t))
341 (let ((overlay (make-overlay from to nil nil rear-sticky)))
342 (widget-put widget :field-overlay overlay)
343 ;;(overlay-put overlay 'detachable nil)
344 (overlay-put overlay 'field widget)
345 (overlay-put overlay 'local-map keymap)
346 (overlay-put overlay 'face face)
347 (overlay-put overlay 'help-echo help-echo)))
348 (widget-specify-secret widget))
350 (defun widget-specify-secret (field)
351 "Replace text in FIELD with value of `:secret', if non-nil."
352 (let ((secret (widget-get field :secret))
353 (size (widget-get field :size)))
354 (when secret
355 (let ((begin (widget-field-start field))
356 (end (widget-field-end field)))
357 (when size
358 (while (and (> end begin)
359 (eq (char-after (1- end)) ?\ ))
360 (setq end (1- end))))
361 (while (< begin end)
362 (let ((old (char-after begin)))
363 (unless (eq old secret)
364 (subst-char-in-region begin (1+ begin) old secret)
365 (put-text-property begin (1+ begin) 'secret old))
366 (setq begin (1+ begin))))))))
368 (defun widget-specify-button (widget from to)
369 "Specify button for WIDGET between FROM and TO."
370 (let ((overlay (make-overlay from to nil t nil)))
371 (widget-put widget :button-overlay overlay)
372 (overlay-put overlay 'button widget)
373 (overlay-put overlay 'keymap (widget-get widget :keymap))
374 ;; We want to avoid the face with image buttons.
375 (unless (widget-get widget :suppress-face)
376 (overlay-put overlay 'face (widget-apply widget :button-face-get))
377 (overlay-put overlay 'mouse-face widget-mouse-face))
378 (overlay-put overlay 'help-echo (widget-get widget :help-echo))))
380 (defun widget-specify-sample (widget from to)
381 "Specify sample for WIDGET between FROM and TO."
382 (let ((overlay (make-overlay from to nil t nil)))
383 (overlay-put overlay 'face (widget-apply widget :sample-face-get))
384 (widget-put widget :sample-overlay overlay)))
386 (defun widget-specify-doc (widget from to)
387 "Specify documentation for WIDGET between FROM and TO."
388 (let ((overlay (make-overlay from to nil t nil)))
389 (overlay-put overlay 'widget-doc widget)
390 (overlay-put overlay 'face widget-documentation-face)
391 (widget-put widget :doc-overlay overlay)))
393 (defmacro widget-specify-insert (&rest form)
394 "Execute FORM without inheriting any text properties."
395 `(save-restriction
396 (let ((inhibit-read-only t)
397 (inhibit-modification-hooks t)
398 result)
399 (insert "<>")
400 (narrow-to-region (- (point) 2) (point))
401 (goto-char (1+ (point-min)))
402 (setq result (progn ,@form))
403 (delete-region (point-min) (1+ (point-min)))
404 (delete-region (1- (point-max)) (point-max))
405 (goto-char (point-max))
406 result)))
408 (defface widget-inactive-face '((((class grayscale color)
409 (background dark))
410 (:foreground "light gray"))
411 (((class grayscale color)
412 (background light))
413 (:foreground "dim gray"))
415 (:italic t)))
416 "Face used for inactive widgets."
417 :group 'widget-faces)
419 (defun widget-specify-inactive (widget from to)
420 "Make WIDGET inactive for user modifications."
421 (unless (widget-get widget :inactive)
422 (let ((overlay (make-overlay from to nil t nil)))
423 (overlay-put overlay 'face 'widget-inactive-face)
424 ;; This is disabled, as it makes the mouse cursor change shape.
425 ;; (overlay-put overlay 'mouse-face 'widget-inactive-face)
426 (overlay-put overlay 'evaporate t)
427 (overlay-put overlay 'priority 100)
428 (overlay-put overlay 'modification-hooks '(widget-overlay-inactive))
429 (widget-put widget :inactive overlay))))
431 (defun widget-overlay-inactive (&rest junk)
432 "Ignoring the arguments, signal an error."
433 (unless inhibit-read-only
434 (error "The widget here is not active")))
437 (defun widget-specify-active (widget)
438 "Make WIDGET active for user modifications."
439 (let ((inactive (widget-get widget :inactive)))
440 (when inactive
441 (delete-overlay inactive)
442 (widget-put widget :inactive nil))))
444 ;;; Widget Properties.
446 (defsubst widget-type (widget)
447 "Return the type of WIDGET, a symbol."
448 (car widget))
450 (defun widget-get-indirect (widget property)
451 "In WIDGET, get the value of PROPERTY.
452 If the value is a symbol, return its binding.
453 Otherwise, just return the value."
454 (let ((value (widget-get widget property)))
455 (if (symbolp value)
456 (symbol-value value)
457 value)))
459 (defun widget-member (widget property)
460 "Non-nil iff there is a definition in WIDGET for PROPERTY."
461 (cond ((plist-member (cdr widget) property)
463 ((car widget)
464 (widget-member (get (car widget) 'widget-type) property))
465 (t nil)))
467 (defun widget-value (widget)
468 "Extract the current value of WIDGET."
469 (widget-apply widget
470 :value-to-external (widget-apply widget :value-get)))
472 (defun widget-value-set (widget value)
473 "Set the current value of WIDGET to VALUE."
474 (widget-apply widget
475 :value-set (widget-apply widget
476 :value-to-internal value)))
478 (defun widget-default-get (widget)
479 "Extract the default value of WIDGET."
480 (or (widget-get widget :value)
481 (widget-apply widget :default-get)))
483 (defun widget-match-inline (widget vals)
484 "In WIDGET, match the start of VALS."
485 (cond ((widget-get widget :inline)
486 (widget-apply widget :match-inline vals))
487 ((and (listp vals)
488 (widget-apply widget :match (car vals)))
489 (cons (list (car vals)) (cdr vals)))
490 (t nil)))
492 (defun widget-apply-action (widget &optional event)
493 "Apply :action in WIDGET in response to EVENT."
494 (if (widget-apply widget :active)
495 (widget-apply widget :action event)
496 (error "Attempt to perform action on inactive widget")))
498 ;;; Helper functions.
500 ;; These are widget specific.
502 ;;;###autoload
503 (defun widget-prompt-value (widget prompt &optional value unbound)
504 "Prompt for a value matching WIDGET, using PROMPT.
505 The current value is assumed to be VALUE, unless UNBOUND is non-nil."
506 (unless (listp widget)
507 (setq widget (list widget)))
508 (setq prompt (format "[%s] %s" (widget-type widget) prompt))
509 (setq widget (widget-convert widget))
510 (let ((answer (widget-apply widget :prompt-value prompt value unbound)))
511 (unless (widget-apply widget :match answer)
512 (error "Value does not match %S type" (car widget)))
513 answer))
515 (defun widget-get-sibling (widget)
516 "Get the item WIDGET is assumed to toggle.
517 This is only meaningful for radio buttons or checkboxes in a list."
518 (let* ((children (widget-get (widget-get widget :parent) :children))
519 child)
520 (catch 'child
521 (while children
522 (setq child (car children)
523 children (cdr children))
524 (when (eq (widget-get child :button) widget)
525 (throw 'child child)))
526 nil)))
528 (defun widget-map-buttons (function &optional buffer maparg)
529 "Map FUNCTION over the buttons in BUFFER.
530 FUNCTION is called with the arguments WIDGET and MAPARG.
532 If FUNCTION returns non-nil, the walk is cancelled.
534 The arguments MAPARG, and BUFFER default to nil and (current-buffer),
535 respectively."
536 (let ((cur (point-min))
537 (widget nil)
538 (parent nil)
539 (overlays (if buffer
540 (save-excursion (set-buffer buffer) (overlay-lists))
541 (overlay-lists))))
542 (setq overlays (append (car overlays) (cdr overlays)))
543 (while (setq cur (pop overlays))
544 (setq widget (overlay-get cur 'button))
545 (if (and widget (funcall function widget maparg))
546 (setq overlays nil)))))
548 ;;; Images.
550 (defcustom widget-image-directory (file-name-as-directory
551 (expand-file-name "custom" data-directory))
552 "Where widget button images are located.
553 If this variable is nil, widget will try to locate the directory
554 automatically."
555 :group 'widgets
556 :type 'directory)
558 (defcustom widget-image-enable t
559 "If non nil, use image buttons in widgets when available."
560 :version "21.1"
561 :group 'widgets
562 :type 'boolean)
564 (defcustom widget-image-conversion
565 '((xpm ".xpm") (gif ".gif") (png ".png") (jpeg ".jpg" ".jpeg")
566 (xbm ".xbm"))
567 "Conversion alist from image formats to file name suffixes."
568 :group 'widgets
569 :type '(repeat (cons :format "%v"
570 (symbol :tag "Image Format" unknown)
571 (repeat :tag "Suffixes"
572 (string :format "%v")))))
574 (defun widget-image-find (image)
575 "Create a graphical button from IMAGE.
576 IMAGE should either already be an image, or be a file name sans
577 extension (xpm, xbm, gif, jpg, or png) located in
578 `widget-image-directory' or otherwise where `find-image' will find it."
579 (cond ((not (and image widget-image-enable (display-graphic-p)))
580 ;; We don't want or can't use images.
581 nil)
582 ((and (consp image)
583 (eq 'image (car image)))
584 ;; Already an image spec. Use it.
585 image)
586 ((stringp image)
587 ;; A string. Look it up in relevant directories.
588 (let* ((load-path (cons widget-image-directory load-path))
589 specs)
590 (dolist (elt widget-image-conversion)
591 (dolist (ext (cdr elt))
592 (push (list :type (car elt) :file (concat image ext)) specs)))
593 (setq specs (nreverse specs))
594 (find-image specs)))
596 ;; Oh well.
597 nil)))
599 (defvar widget-button-pressed-face 'widget-button-pressed-face
600 "Face used for pressed buttons in widgets.
601 This exists as a variable so it can be set locally in certain
602 buffers.")
604 (defun widget-image-insert (widget tag image &optional down inactive)
605 "In WIDGET, insert the text TAG or, if supported, IMAGE.
606 IMAGE should either be an image or an image file name sans extension
607 \(xpm, xbm, gif, jpg, or png) located in `widget-image-directory'.
609 Optional arguments DOWN and INACTIVE are used instead of IMAGE when the
610 button is pressed or inactive, respectively. These are currently ignored."
611 (if (and (display-graphic-p)
612 (setq image (widget-image-find image)))
613 (progn (widget-put widget :suppress-face t)
614 (insert-image image
615 (propertize
616 tag 'mouse-face widget-button-pressed-face)))
617 (insert tag)))
619 ;;; Buttons.
621 (defgroup widget-button nil
622 "The look of various kinds of buttons."
623 :group 'widgets)
625 (defcustom widget-button-prefix ""
626 "String used as prefix for buttons."
627 :type 'string
628 :group 'widget-button)
630 (defcustom widget-button-suffix ""
631 "String used as suffix for buttons."
632 :type 'string
633 :group 'widget-button)
635 ;;; Creating Widgets.
637 ;;;###autoload
638 (defun widget-create (type &rest args)
639 "Create widget of TYPE.
640 The optional ARGS are additional keyword arguments."
641 (let ((widget (apply 'widget-convert type args)))
642 (widget-apply widget :create)
643 widget))
645 (defun widget-create-child-and-convert (parent type &rest args)
646 "As part of the widget PARENT, create a child widget TYPE.
647 The child is converted, using the keyword arguments ARGS."
648 (let ((widget (apply 'widget-convert type args)))
649 (widget-put widget :parent parent)
650 (unless (widget-get widget :indent)
651 (widget-put widget :indent (+ (or (widget-get parent :indent) 0)
652 (or (widget-get widget :extra-offset) 0)
653 (widget-get parent :offset))))
654 (widget-apply widget :create)
655 widget))
657 (defun widget-create-child (parent type)
658 "Create widget of TYPE."
659 (let ((widget (copy-sequence type)))
660 (widget-put widget :parent parent)
661 (unless (widget-get widget :indent)
662 (widget-put widget :indent (+ (or (widget-get parent :indent) 0)
663 (or (widget-get widget :extra-offset) 0)
664 (widget-get parent :offset))))
665 (widget-apply widget :create)
666 widget))
668 (defun widget-create-child-value (parent type value)
669 "Create widget of TYPE with value VALUE."
670 (let ((widget (copy-sequence type)))
671 (widget-put widget :value (widget-apply widget :value-to-internal value))
672 (widget-put widget :parent parent)
673 (unless (widget-get widget :indent)
674 (widget-put widget :indent (+ (or (widget-get parent :indent) 0)
675 (or (widget-get widget :extra-offset) 0)
676 (widget-get parent :offset))))
677 (widget-apply widget :create)
678 widget))
680 ;;;###autoload
681 (defun widget-delete (widget)
682 "Delete WIDGET."
683 (widget-apply widget :delete))
685 (defun widget-convert (type &rest args)
686 "Convert TYPE to a widget without inserting it in the buffer.
687 The optional ARGS are additional keyword arguments."
688 ;; Don't touch the type.
689 (let* ((widget (if (symbolp type)
690 (list type)
691 (copy-sequence type)))
692 (current widget)
693 (keys args))
694 ;; First set the :args keyword.
695 (while (cdr current) ;Look in the type.
696 (if (keywordp (car (cdr current)))
697 (setq current (cdr (cdr current)))
698 (setcdr current (list :args (cdr current)))
699 (setq current nil)))
700 (while args ;Look in the args.
701 (if (keywordp (nth 0 args))
702 (setq args (nthcdr 2 args))
703 (widget-put widget :args args)
704 (setq args nil)))
705 ;; Then Convert the widget.
706 (setq type widget)
707 (while type
708 (let ((convert-widget (plist-get (cdr type) :convert-widget)))
709 (if convert-widget
710 (setq widget (funcall convert-widget widget))))
711 (setq type (get (car type) 'widget-type)))
712 ;; Finally set the keyword args.
713 (while keys
714 (let ((next (nth 0 keys)))
715 (if (keywordp next)
716 (progn
717 (widget-put widget next (nth 1 keys))
718 (setq keys (nthcdr 2 keys)))
719 (setq keys nil))))
720 ;; Convert the :value to internal format.
721 (if (widget-member widget :value)
722 (widget-put widget
723 :value (widget-apply widget
724 :value-to-internal
725 (widget-get widget :value))))
726 ;; Return the newly create widget.
727 widget))
729 (defun widget-insert (&rest args)
730 "Call `insert' with ARGS even if surrounding text is read only."
731 (let ((inhibit-read-only t)
732 (inhibit-modification-hooks t))
733 (apply 'insert args)))
735 (defun widget-convert-text (type from to
736 &optional button-from button-to
737 &rest args)
738 "Return a widget of type TYPE with endpoint FROM TO.
739 Optional ARGS are extra keyword arguments for TYPE.
740 and TO will be used as the widgets end points. If optional arguments
741 BUTTON-FROM and BUTTON-TO are given, these will be used as the widgets
742 button end points.
743 Optional ARGS are extra keyword arguments for TYPE."
744 (let ((widget (apply 'widget-convert type :delete 'widget-leave-text args))
745 (from (copy-marker from))
746 (to (copy-marker to)))
747 (set-marker-insertion-type from t)
748 (set-marker-insertion-type to nil)
749 (widget-put widget :from from)
750 (widget-put widget :to to)
751 (when button-from
752 (widget-specify-button widget button-from button-to))
753 widget))
755 (defun widget-convert-button (type from to &rest args)
756 "Return a widget of type TYPE with endpoint FROM TO.
757 Optional ARGS are extra keyword arguments for TYPE.
758 No text will be inserted to the buffer, instead the text between FROM
759 and TO will be used as the widgets end points, as well as the widgets
760 button end points."
761 (apply 'widget-convert-text type from to from to args))
763 (defun widget-leave-text (widget)
764 "Remove markers and overlays from WIDGET and its children."
765 (let ((button (widget-get widget :button-overlay))
766 (sample (widget-get widget :sample-overlay))
767 (doc (widget-get widget :doc-overlay))
768 (field (widget-get widget :field-overlay)))
769 (set-marker (widget-get widget :from) nil)
770 (set-marker (widget-get widget :to) nil)
771 (when button
772 (delete-overlay button))
773 (when sample
774 (delete-overlay sample))
775 (when doc
776 (delete-overlay doc))
777 (when field
778 (delete-overlay field))
779 (mapc 'widget-leave-text (widget-get widget :children))))
781 ;;; Keymap and Commands.
783 (defvar widget-keymap
784 (let ((map (make-sparse-keymap)))
785 (define-key map "\t" 'widget-forward)
786 (define-key map [(shift tab)] 'widget-backward)
787 (define-key map [backtab] 'widget-backward)
788 (define-key map [down-mouse-2] 'widget-button-click)
789 (define-key map "\C-m" 'widget-button-press)
790 map)
791 "Keymap containing useful binding for buffers containing widgets.
792 Recommended as a parent keymap for modes using widgets.")
794 (defvar widget-global-map global-map
795 "Keymap used for events a widget does not handle itself.")
796 (make-variable-buffer-local 'widget-global-map)
798 (defvar widget-field-keymap
799 (let ((map (copy-keymap widget-keymap)))
800 (define-key map "\C-k" 'widget-kill-line)
801 (define-key map "\M-\t" 'widget-complete)
802 (define-key map "\C-m" 'widget-field-activate)
803 ;; Since the widget code uses a `field' property to identify fields,
804 ;; ordinary beginning-of-line/end-of-line do the right thing.
805 ;; (define-key map "\C-a" 'widget-beginning-of-line)
806 ;; (define-key map "\C-e" 'widget-end-of-line)
807 map)
808 "Keymap used inside an editable field.")
810 (defvar widget-text-keymap
811 (let ((map (copy-keymap widget-keymap)))
812 ;; Since the widget code uses a `field' property to identify fields,
813 ;; ordinary beginning-of-line/end-of-line do the right thing.
814 ;; (define-key map "\C-a" 'widget-beginning-of-line)
815 ;; (define-key map "\C-e" 'widget-end-of-line)
816 map)
817 "Keymap used inside a text field.")
819 (defun widget-field-activate (pos &optional event)
820 "Invoke the ediable field at point."
821 (interactive "@d")
822 (let ((field (widget-field-at pos)))
823 (if field
824 (widget-apply-action field event)
825 (call-interactively
826 (lookup-key widget-global-map (this-command-keys))))))
828 (defface widget-button-pressed-face
829 '((((class color))
830 (:foreground "red"))
832 (:bold t :underline t)))
833 "Face used for pressed buttons."
834 :group 'widget-faces)
836 (defun widget-button-click (event)
837 "Invoke the button that the mouse is pointing at."
838 (interactive "@e")
839 (if (widget-event-point event)
840 (save-excursion
841 (mouse-set-point event)
842 (let* ((pos (widget-event-point event))
843 (button (get-char-property pos 'button)))
844 (if button
845 (let* ((overlay (widget-get button :button-overlay))
846 (face (overlay-get overlay 'face))
847 (mouse-face (overlay-get overlay 'mouse-face)))
848 (unwind-protect
849 (let ((track-mouse t))
850 (save-excursion
851 (when face ; avoid changing around image
852 (overlay-put overlay
853 'face widget-button-pressed-face)
854 (overlay-put overlay
855 'mouse-face widget-button-pressed-face))
856 (unless (widget-apply button :mouse-down-action event)
857 (while (not (widget-button-release-event-p event))
858 (setq event (read-event)
859 pos (widget-event-point event))
860 (if (and pos
861 (eq (get-char-property pos 'button)
862 button))
863 (when face
864 (overlay-put overlay
865 'face
866 widget-button-pressed-face)
867 (overlay-put overlay
868 'mouse-face
869 widget-button-pressed-face))
870 (overlay-put overlay 'face face)
871 (overlay-put overlay 'mouse-face mouse-face))))
872 (when (and pos
873 (eq (get-char-property pos 'button) button))
874 (widget-apply-action button event))))
875 (overlay-put overlay 'face face)
876 (overlay-put overlay 'mouse-face mouse-face)))
877 (let ((up t)
878 command)
879 ;; Find the global command to run, and check whether it
880 ;; is bound to an up event.
881 (if (memq (event-basic-type event) '(mouse-1 down-mouse-1))
882 (cond ((setq command ;down event
883 (lookup-key widget-global-map [down-mouse-1]))
884 (setq up nil))
885 ((setq command ;up event
886 (lookup-key widget-global-map [mouse-1]))))
887 (cond ((setq command ;down event
888 (lookup-key widget-global-map [down-mouse-2]))
889 (setq up nil))
890 ((setq command ;up event
891 (lookup-key widget-global-map [mouse-2])))))
892 (when up
893 ;; Don't execute up events twice.
894 (while (not (widget-button-release-event-p event))
895 (setq event (read-event))))
896 (when command
897 (call-interactively command)))))
898 (unless (pos-visible-in-window-p (widget-event-point event))
899 (mouse-set-point event)
900 (beginning-of-line)
901 (recenter)))
902 (message "You clicked somewhere weird.")))
904 (defun widget-button-press (pos &optional event)
905 "Invoke button at POS."
906 (interactive "@d")
907 (let ((button (get-char-property pos 'button)))
908 (if button
909 (widget-apply-action button event)
910 (let ((command (lookup-key widget-global-map (this-command-keys))))
911 (when (commandp command)
912 (call-interactively command))))))
914 (defun widget-tabable-at (&optional pos)
915 "Return the tabable widget at POS, or nil.
916 POS defaults to the value of (point)."
917 (let ((widget (widget-at pos)))
918 (if widget
919 (let ((order (widget-get widget :tab-order)))
920 (if order
921 (if (>= order 0)
922 widget)
923 widget)))))
925 (defvar widget-use-overlay-change t
926 "If non-nil, use overlay change functions to tab around in the buffer.
927 This is much faster, but doesn't work reliably on Emacs 19.34.")
929 (defun widget-move (arg)
930 "Move point to the ARG next field or button.
931 ARG may be negative to move backward."
932 (or (bobp) (> arg 0) (backward-char))
933 (let ((pos (point))
934 (number arg)
935 (old (widget-tabable-at))
936 new)
937 ;; Forward.
938 (while (> arg 0)
939 (cond ((eobp)
940 (goto-char (point-min)))
941 (widget-use-overlay-change
942 (goto-char (next-overlay-change (point))))
944 (forward-char 1)))
945 (and (eq pos (point))
946 (eq arg number)
947 (error "No buttons or fields found"))
948 (let ((new (widget-tabable-at)))
949 (when new
950 (unless (eq new old)
951 (setq arg (1- arg))
952 (setq old new)))))
953 ;; Backward.
954 (while (< arg 0)
955 (cond ((bobp)
956 (goto-char (point-max)))
957 (widget-use-overlay-change
958 (goto-char (previous-overlay-change (point))))
960 (backward-char 1)))
961 (and (eq pos (point))
962 (eq arg number)
963 (error "No buttons or fields found"))
964 (let ((new (widget-tabable-at)))
965 (when new
966 (unless (eq new old)
967 (setq arg (1+ arg))))))
968 (let ((new (widget-tabable-at)))
969 (while (eq (widget-tabable-at) new)
970 (backward-char)))
971 (forward-char))
972 (widget-echo-help (point))
973 (run-hooks 'widget-move-hook))
975 (defun widget-forward (arg)
976 "Move point to the next field or button.
977 With optional ARG, move across that many fields."
978 (interactive "p")
979 (run-hooks 'widget-forward-hook)
980 (widget-move arg))
982 (defun widget-backward (arg)
983 "Move point to the previous field or button.
984 With optional ARG, move across that many fields."
985 (interactive "p")
986 (run-hooks 'widget-backward-hook)
987 (widget-move (- arg)))
989 ;; Since the widget code uses a `field' property to identify fields,
990 ;; ordinary beginning-of-line/end-of-line do the right thing.
991 (defalias 'widget-beginning-of-line 'beginning-of-line)
992 (defalias 'widget-end-of-line 'end-of-line)
994 (defun widget-kill-line ()
995 "Kill to end of field or end of line, whichever is first."
996 (interactive)
997 (let* ((field (widget-field-find (point)))
998 (end (and field (widget-field-end field))))
999 (if (and field (> (line-beginning-position 2) end))
1000 (kill-region (point) end)
1001 (call-interactively 'kill-line))))
1003 (defcustom widget-complete-field (lookup-key global-map "\M-\t")
1004 "Default function to call for completion inside fields."
1005 :options '(ispell-complete-word complete-tag lisp-complete-symbol)
1006 :type 'function
1007 :group 'widgets)
1009 (defun widget-complete ()
1010 "Complete content of editable field from point.
1011 When not inside a field, move to the previous button or field."
1012 (interactive)
1013 (let ((field (widget-field-find (point))))
1014 (if field
1015 (widget-apply field :complete)
1016 (error "Not in an editable field"))))
1018 ;;; Setting up the buffer.
1020 (defvar widget-field-new nil)
1021 ;; List of all newly created editable fields in the buffer.
1022 (make-variable-buffer-local 'widget-field-new)
1024 (defvar widget-field-list nil)
1025 ;; List of all editable fields in the buffer.
1026 (make-variable-buffer-local 'widget-field-list)
1028 (defun widget-at (&optional pos)
1029 "The button or field at POS (default, point)."
1030 (or (get-char-property (or pos (point)) 'button)
1031 (widget-field-at pos)))
1033 (defun widget-setup ()
1034 "Setup current buffer so editing string widgets works."
1035 (let ((inhibit-read-only t)
1036 (inhibit-modification-hooks t)
1037 field)
1038 (while widget-field-new
1039 (setq field (car widget-field-new)
1040 widget-field-new (cdr widget-field-new)
1041 widget-field-list (cons field widget-field-list))
1042 (let ((from (car (widget-get field :field-overlay)))
1043 (to (cdr (widget-get field :field-overlay))))
1044 (widget-specify-field field
1045 (marker-position from) (marker-position to))
1046 (set-marker from nil)
1047 (set-marker to nil))))
1048 (widget-clear-undo)
1049 (widget-add-change))
1051 (defvar widget-field-last nil)
1052 ;; Last field containing point.
1053 (make-variable-buffer-local 'widget-field-last)
1055 (defvar widget-field-was nil)
1056 ;; The widget data before the change.
1057 (make-variable-buffer-local 'widget-field-was)
1059 (defun widget-field-at (pos)
1060 "Return the widget field at POS, or nil if none."
1061 (let ((field (get-char-property (or pos (point)) 'field)))
1062 (if (eq field 'boundary)
1064 field)))
1066 (defun widget-field-buffer (widget)
1067 "Return the start of WIDGET's editing field."
1068 (let ((overlay (widget-get widget :field-overlay)))
1069 (and overlay (overlay-buffer overlay))))
1071 (defun widget-field-start (widget)
1072 "Return the start of WIDGET's editing field."
1073 (let ((overlay (widget-get widget :field-overlay)))
1074 (and overlay (overlay-start overlay))))
1076 (defun widget-field-end (widget)
1077 "Return the end of WIDGET's editing field."
1078 (let ((overlay (widget-get widget :field-overlay)))
1079 ;; Don't subtract one if local-map works at the end of the overlay,
1080 ;; or if a special `boundary' field has been added after the widget
1081 ;; field.
1082 (and overlay
1083 (if (and (not (eq (get-char-property (overlay-end overlay)
1084 'field
1085 (widget-field-buffer widget))
1086 'boundary))
1087 (or widget-field-add-space
1088 (null (widget-get widget :size))))
1089 (1- (overlay-end overlay))
1090 (overlay-end overlay)))))
1092 (defun widget-field-find (pos)
1093 "Return the field at POS.
1094 Unlike (get-char-property POS 'field) this, works with empty fields too."
1095 (let ((fields widget-field-list)
1096 field found)
1097 (while fields
1098 (setq field (car fields)
1099 fields (cdr fields))
1100 (when (and (<= (widget-field-start field) pos)
1101 (<= pos (widget-field-end field)))
1102 (when found
1103 (error "Overlapping fields"))
1104 (setq found field)))
1105 found))
1107 (defun widget-before-change (from to)
1108 ;; This is how, for example, a variable changes its state to `modified'.
1109 ;; when it is being edited.
1110 (unless inhibit-read-only
1111 (let ((from-field (widget-field-find from))
1112 (to-field (widget-field-find to)))
1113 (cond ((not (eq from-field to-field))
1114 (add-hook 'post-command-hook 'widget-add-change nil t)
1115 (signal 'text-read-only
1116 '("Change should be restricted to a single field")))
1117 ((null from-field)
1118 (add-hook 'post-command-hook 'widget-add-change nil t)
1119 (signal 'text-read-only
1120 '("Attempt to change text outside editable field")))
1121 (widget-field-use-before-change
1122 (widget-apply from-field :notify from-field))))))
1124 (defun widget-add-change ()
1125 (remove-hook 'post-command-hook 'widget-add-change t)
1126 (add-hook 'before-change-functions 'widget-before-change nil t)
1127 (add-hook 'after-change-functions 'widget-after-change nil t))
1129 (defun widget-after-change (from to old)
1130 "Adjust field size and text properties."
1131 (let ((field (widget-field-find from))
1132 (other (widget-field-find to)))
1133 (when field
1134 (unless (eq field other)
1135 (error "Change in different fields"))
1136 (let ((size (widget-get field :size)))
1137 (when size
1138 (let ((begin (widget-field-start field))
1139 (end (widget-field-end field)))
1140 (cond ((< (- end begin) size)
1141 ;; Field too small.
1142 (save-excursion
1143 (goto-char end)
1144 (insert-char ?\ (- (+ begin size) end))))
1145 ((> (- end begin) size)
1146 ;; Field too large and
1147 (if (or (< (point) (+ begin size))
1148 (> (point) end))
1149 ;; Point is outside extra space.
1150 (setq begin (+ begin size))
1151 ;; Point is within the extra space.
1152 (setq begin (point)))
1153 (save-excursion
1154 (goto-char end)
1155 (while (and (eq (preceding-char) ?\ )
1156 (> (point) begin))
1157 (delete-backward-char 1)))))))
1158 (widget-specify-secret field))
1159 (widget-apply field :notify field))))
1161 ;;; Widget Functions
1163 ;; These functions are used in the definition of multiple widgets.
1165 (defun widget-parent-action (widget &optional event)
1166 "Tell :parent of WIDGET to handle the :action.
1167 Optional EVENT is the event that triggered the action."
1168 (widget-apply (widget-get widget :parent) :action event))
1170 (defun widget-children-value-delete (widget)
1171 "Delete all :children and :buttons in WIDGET."
1172 (mapc 'widget-delete (widget-get widget :children))
1173 (widget-put widget :children nil)
1174 (mapc 'widget-delete (widget-get widget :buttons))
1175 (widget-put widget :buttons nil))
1177 (defun widget-children-validate (widget)
1178 "All the :children must be valid."
1179 (let ((children (widget-get widget :children))
1180 child found)
1181 (while (and children (not found))
1182 (setq child (car children)
1183 children (cdr children)
1184 found (widget-apply child :validate)))
1185 found))
1187 ;; Made defsubst to speed up face editor creation.
1188 (defsubst widget-types-convert-widget (widget)
1189 "Convert :args as widget types in WIDGET."
1190 (widget-put widget :args (mapcar 'widget-convert (widget-get widget :args)))
1191 widget)
1193 (defun widget-value-convert-widget (widget)
1194 "Initialize :value from :args in WIDGET."
1195 (let ((args (widget-get widget :args)))
1196 (when args
1197 (widget-put widget :value (car args))
1198 ;; Don't convert :value here, as this is done in `widget-convert'.
1199 ;; (widget-put widget :value (widget-apply widget
1200 ;; :value-to-internal (car args)))
1201 (widget-put widget :args nil)))
1202 widget)
1204 (defun widget-value-value-get (widget)
1205 "Return the :value property of WIDGET."
1206 (widget-get widget :value))
1208 ;;; The `default' Widget.
1210 (define-widget 'default nil
1211 "Basic widget other widgets are derived from."
1212 :value-to-internal (lambda (widget value) value)
1213 :value-to-external (lambda (widget value) value)
1214 :button-prefix 'widget-button-prefix
1215 :button-suffix 'widget-button-suffix
1216 :complete 'widget-default-complete
1217 :create 'widget-default-create
1218 :indent nil
1219 :offset 0
1220 :format-handler 'widget-default-format-handler
1221 :button-face-get 'widget-default-button-face-get
1222 :sample-face-get 'widget-default-sample-face-get
1223 :delete 'widget-default-delete
1224 :value-set 'widget-default-value-set
1225 :value-inline 'widget-default-value-inline
1226 :default-get 'widget-default-default-get
1227 :menu-tag-get 'widget-default-menu-tag-get
1228 :validate #'ignore
1229 :active 'widget-default-active
1230 :activate 'widget-specify-active
1231 :deactivate 'widget-default-deactivate
1232 :mouse-down-action #'ignore
1233 :action 'widget-default-action
1234 :notify 'widget-default-notify
1235 :prompt-value 'widget-default-prompt-value)
1237 (defun widget-default-complete (widget)
1238 "Call the value of the :complete-function property of WIDGET.
1239 If that does not exists, call the value of `widget-complete-field'."
1240 (call-interactively (or (widget-get widget :complete-function)
1241 widget-complete-field)))
1243 (defun widget-default-create (widget)
1244 "Create WIDGET at point in the current buffer."
1245 (widget-specify-insert
1246 (let ((from (point))
1247 button-begin button-end
1248 sample-begin sample-end
1249 doc-begin doc-end
1250 value-pos)
1251 (insert (widget-get widget :format))
1252 (goto-char from)
1253 ;; Parse escapes in format.
1254 (while (re-search-forward "%\\(.\\)" nil t)
1255 (let ((escape (char-after (match-beginning 1))))
1256 (delete-backward-char 2)
1257 (cond ((eq escape ?%)
1258 (insert ?%))
1259 ((eq escape ?\[)
1260 (setq button-begin (point))
1261 (insert (widget-get-indirect widget :button-prefix)))
1262 ((eq escape ?\])
1263 (insert (widget-get-indirect widget :button-suffix))
1264 (setq button-end (point)))
1265 ((eq escape ?\{)
1266 (setq sample-begin (point)))
1267 ((eq escape ?\})
1268 (setq sample-end (point)))
1269 ((eq escape ?n)
1270 (when (widget-get widget :indent)
1271 (insert ?\n)
1272 (insert-char ? (widget-get widget :indent))))
1273 ((eq escape ?t)
1274 (let ((image (widget-get widget :tag-glyph))
1275 (tag (widget-get widget :tag)))
1276 (cond (image
1277 (widget-image-insert widget (or tag "image") image))
1278 (tag
1279 (insert tag))
1281 (princ (widget-get widget :value)
1282 (current-buffer))))))
1283 ((eq escape ?d)
1284 (let ((doc (widget-get widget :doc)))
1285 (when doc
1286 (setq doc-begin (point))
1287 (insert doc)
1288 (while (eq (preceding-char) ?\n)
1289 (delete-backward-char 1))
1290 (insert ?\n)
1291 (setq doc-end (point)))))
1292 ((eq escape ?v)
1293 (if (and button-begin (not button-end))
1294 (widget-apply widget :value-create)
1295 (setq value-pos (point))))
1297 (widget-apply widget :format-handler escape)))))
1298 ;; Specify button, sample, and doc, and insert value.
1299 (and button-begin button-end
1300 (widget-specify-button widget button-begin button-end))
1301 (and sample-begin sample-end
1302 (widget-specify-sample widget sample-begin sample-end))
1303 (and doc-begin doc-end
1304 (widget-specify-doc widget doc-begin doc-end))
1305 (when value-pos
1306 (goto-char value-pos)
1307 (widget-apply widget :value-create)))
1308 (let ((from (point-min-marker))
1309 (to (point-max-marker)))
1310 (set-marker-insertion-type from t)
1311 (set-marker-insertion-type to nil)
1312 (widget-put widget :from from)
1313 (widget-put widget :to to)))
1314 (widget-clear-undo))
1316 (defun widget-default-format-handler (widget escape)
1317 ;; We recognize the %h escape by default.
1318 (let* ((buttons (widget-get widget :buttons)))
1319 (cond ((eq escape ?h)
1320 (let* ((doc-property (widget-get widget :documentation-property))
1321 (doc-try (cond ((widget-get widget :doc))
1322 ((functionp doc-property)
1323 (funcall doc-property
1324 (widget-get widget :value)))
1325 ((symbolp doc-property)
1326 (documentation-property
1327 (widget-get widget :value)
1328 doc-property))))
1329 (doc-text (and (stringp doc-try)
1330 (> (length doc-try) 1)
1331 doc-try))
1332 (doc-indent (widget-get widget :documentation-indent)))
1333 (when doc-text
1334 (and (eq (preceding-char) ?\n)
1335 (widget-get widget :indent)
1336 (insert-char ? (widget-get widget :indent)))
1337 ;; The `*' in the beginning is redundant.
1338 (when (eq (aref doc-text 0) ?*)
1339 (setq doc-text (substring doc-text 1)))
1340 ;; Get rid of trailing newlines.
1341 (when (string-match "\n+\\'" doc-text)
1342 (setq doc-text (substring doc-text 0 (match-beginning 0))))
1343 (push (widget-create-child-and-convert
1344 widget 'documentation-string
1345 :indent (cond ((numberp doc-indent )
1346 doc-indent)
1347 ((null doc-indent)
1348 nil)
1349 (t 0))
1350 doc-text)
1351 buttons))))
1353 (error "Unknown escape `%c'" escape)))
1354 (widget-put widget :buttons buttons)))
1356 (defun widget-default-button-face-get (widget)
1357 ;; Use :button-face or widget-button-face
1358 (or (widget-get widget :button-face)
1359 (let ((parent (widget-get widget :parent)))
1360 (if parent
1361 (widget-apply parent :button-face-get)
1362 widget-button-face))))
1364 (defun widget-default-sample-face-get (widget)
1365 ;; Use :sample-face.
1366 (widget-get widget :sample-face))
1368 (defun widget-default-delete (widget)
1369 "Remove widget from the buffer."
1370 (let ((from (widget-get widget :from))
1371 (to (widget-get widget :to))
1372 (inactive-overlay (widget-get widget :inactive))
1373 (button-overlay (widget-get widget :button-overlay))
1374 (sample-overlay (widget-get widget :sample-overlay))
1375 (doc-overlay (widget-get widget :doc-overlay))
1376 (inhibit-modification-hooks t)
1377 (inhibit-read-only t))
1378 (widget-apply widget :value-delete)
1379 (when inactive-overlay
1380 (delete-overlay inactive-overlay))
1381 (when button-overlay
1382 (delete-overlay button-overlay))
1383 (when sample-overlay
1384 (delete-overlay sample-overlay))
1385 (when doc-overlay
1386 (delete-overlay doc-overlay))
1387 (when (< from to)
1388 ;; Kludge: this doesn't need to be true for empty formats.
1389 (delete-region from to))
1390 (set-marker from nil)
1391 (set-marker to nil))
1392 (widget-clear-undo))
1394 (defun widget-default-value-set (widget value)
1395 "Recreate widget with new value."
1396 (let* ((old-pos (point))
1397 (from (copy-marker (widget-get widget :from)))
1398 (to (copy-marker (widget-get widget :to)))
1399 (offset (if (and (<= from old-pos) (<= old-pos to))
1400 (if (>= old-pos (1- to))
1401 (- old-pos to 1)
1402 (- old-pos from)))))
1403 ;;??? Bug: this ought to insert the new value before deleting the old one,
1404 ;; so that markers on either side of the value automatically
1405 ;; stay on the same side. -- rms.
1406 (save-excursion
1407 (goto-char (widget-get widget :from))
1408 (widget-apply widget :delete)
1409 (widget-put widget :value value)
1410 (widget-apply widget :create))
1411 (if offset
1412 (if (< offset 0)
1413 (goto-char (+ (widget-get widget :to) offset 1))
1414 (goto-char (min (+ from offset) (1- (widget-get widget :to))))))))
1416 (defun widget-default-value-inline (widget)
1417 "Wrap value in a list unless it is inline."
1418 (if (widget-get widget :inline)
1419 (widget-value widget)
1420 (list (widget-value widget))))
1422 (defun widget-default-default-get (widget)
1423 "Get `:value'."
1424 (widget-get widget :value))
1426 (defun widget-default-menu-tag-get (widget)
1427 "Use tag or value for menus."
1428 (or (widget-get widget :menu-tag)
1429 (widget-get widget :tag)
1430 (widget-princ-to-string (widget-get widget :value))))
1432 (defun widget-default-active (widget)
1433 "Return t iff this widget active (user modifiable)."
1434 (or (widget-get widget :always-active)
1435 (and (not (widget-get widget :inactive))
1436 (let ((parent (widget-get widget :parent)))
1437 (or (null parent)
1438 (widget-apply parent :active))))))
1440 (defun widget-default-deactivate (widget)
1441 "Make WIDGET inactive for user modifications."
1442 (widget-specify-inactive widget
1443 (widget-get widget :from)
1444 (widget-get widget :to)))
1446 (defun widget-default-action (widget &optional event)
1447 "Notify the parent when a widget changes."
1448 (let ((parent (widget-get widget :parent)))
1449 (when parent
1450 (widget-apply parent :notify widget event))))
1452 (defun widget-default-notify (widget child &optional event)
1453 "Pass notification to parent."
1454 (widget-default-action widget event))
1456 (defun widget-default-prompt-value (widget prompt value unbound)
1457 "Read an arbitrary value. Stolen from `set-variable'."
1458 ;; (let ((initial (if unbound
1459 ;; nil
1460 ;; It would be nice if we could do a `(cons val 1)' here.
1461 ;; (prin1-to-string (custom-quote value))))))
1462 (eval-minibuffer prompt))
1464 ;;; The `item' Widget.
1466 (define-widget 'item 'default
1467 "Constant items for inclusion in other widgets."
1468 :convert-widget 'widget-value-convert-widget
1469 :value-create 'widget-item-value-create
1470 :value-delete 'ignore
1471 :value-get 'widget-value-value-get
1472 :match 'widget-item-match
1473 :match-inline 'widget-item-match-inline
1474 :action 'widget-item-action
1475 :format "%t\n")
1477 (defun widget-item-value-create (widget)
1478 "Insert the printed representation of the value."
1479 (princ (widget-get widget :value) (current-buffer)))
1481 (defun widget-item-match (widget value)
1482 ;; Match if the value is the same.
1483 (equal (widget-get widget :value) value))
1485 (defun widget-item-match-inline (widget values)
1486 ;; Match if the value is the same.
1487 (let ((value (widget-get widget :value)))
1488 (and (listp value)
1489 (<= (length value) (length values))
1490 (let ((head (widget-sublist values 0 (length value))))
1491 (and (equal head value)
1492 (cons head (widget-sublist values (length value))))))))
1494 (defun widget-sublist (list start &optional end)
1495 "Return the sublist of LIST from START to END.
1496 If END is omitted, it defaults to the length of LIST."
1497 (if (> start 0) (setq list (nthcdr start list)))
1498 (if end
1499 (unless (<= end start)
1500 (setq list (copy-sequence list))
1501 (setcdr (nthcdr (- end start 1) list) nil)
1502 list)
1503 (copy-sequence list)))
1505 (defun widget-item-action (widget &optional event)
1506 ;; Just notify itself.
1507 (widget-apply widget :notify widget event))
1509 ;;; The `push-button' Widget.
1511 ;; (defcustom widget-push-button-gui t
1512 ;; "If non nil, use GUI push buttons when available."
1513 ;; :group 'widgets
1514 ;; :type 'boolean)
1516 ;; Cache already created GUI objects.
1517 ;; (defvar widget-push-button-cache nil)
1519 (defcustom widget-push-button-prefix "["
1520 "String used as prefix for buttons."
1521 :type 'string
1522 :group 'widget-button)
1524 (defcustom widget-push-button-suffix "]"
1525 "String used as suffix for buttons."
1526 :type 'string
1527 :group 'widget-button)
1529 (define-widget 'push-button 'item
1530 "A pushable button."
1531 :button-prefix ""
1532 :button-suffix ""
1533 :value-create 'widget-push-button-value-create
1534 :format "%[%v%]")
1536 (defun widget-push-button-value-create (widget)
1537 "Insert text representing the `on' and `off' states."
1538 (let* ((tag (or (widget-get widget :tag)
1539 (widget-get widget :value)))
1540 (tag-glyph (widget-get widget :tag-glyph))
1541 (text (concat widget-push-button-prefix
1542 tag widget-push-button-suffix)))
1543 (if tag-glyph
1544 (widget-image-insert widget text tag-glyph)
1545 (insert text))))
1547 ;; (defun widget-gui-action (widget)
1548 ;; "Apply :action for WIDGET."
1549 ;; (widget-apply-action widget (this-command-keys)))
1551 ;;; The `link' Widget.
1553 (defcustom widget-link-prefix "["
1554 "String used as prefix for links."
1555 :type 'string
1556 :group 'widget-button)
1558 (defcustom widget-link-suffix "]"
1559 "String used as suffix for links."
1560 :type 'string
1561 :group 'widget-button)
1563 (define-widget 'link 'item
1564 "An embedded link."
1565 :button-prefix 'widget-link-prefix
1566 :button-suffix 'widget-link-suffix
1567 :help-echo "Follow the link."
1568 :format "%[%t%]")
1570 ;;; The `info-link' Widget.
1572 (define-widget 'info-link 'link
1573 "A link to an info file."
1574 :action 'widget-info-link-action)
1576 (defun widget-info-link-action (widget &optional event)
1577 "Open the info node specified by WIDGET."
1578 (Info-goto-node (widget-value widget)))
1580 ;;; The `url-link' Widget.
1582 (define-widget 'url-link 'link
1583 "A link to an www page."
1584 :action 'widget-url-link-action)
1586 (defun widget-url-link-action (widget &optional event)
1587 "Open the url specified by WIDGET."
1588 (browse-url (widget-value widget)))
1590 ;;; The `function-link' Widget.
1592 (define-widget 'function-link 'link
1593 "A link to an Emacs function."
1594 :action 'widget-function-link-action)
1596 (defun widget-function-link-action (widget &optional event)
1597 "Show the function specified by WIDGET."
1598 (describe-function (widget-value widget)))
1600 ;;; The `variable-link' Widget.
1602 (define-widget 'variable-link 'link
1603 "A link to an Emacs variable."
1604 :action 'widget-variable-link-action)
1606 (defun widget-variable-link-action (widget &optional event)
1607 "Show the variable specified by WIDGET."
1608 (describe-variable (widget-value widget)))
1610 ;;; The `file-link' Widget.
1612 (define-widget 'file-link 'link
1613 "A link to a file."
1614 :action 'widget-file-link-action)
1616 (defun widget-file-link-action (widget &optional event)
1617 "Find the file specified by WIDGET."
1618 (find-file (widget-value widget)))
1620 ;;; The `emacs-library-link' Widget.
1622 (define-widget 'emacs-library-link 'link
1623 "A link to an Emacs Lisp library file."
1624 :action 'widget-emacs-library-link-action)
1626 (defun widget-emacs-library-link-action (widget &optional event)
1627 "Find the Emacs Library file specified by WIDGET."
1628 (find-file (locate-library (widget-value widget))))
1630 ;;; The `emacs-commentary-link' Widget.
1632 (define-widget 'emacs-commentary-link 'link
1633 "A link to Commentary in an Emacs Lisp library file."
1634 :action 'widget-emacs-commentary-link-action)
1636 (defun widget-emacs-commentary-link-action (widget &optional event)
1637 "Find the Commentary section of the Emacs file specified by WIDGET."
1638 (finder-commentary (widget-value widget)))
1640 ;;; The `editable-field' Widget.
1642 (define-widget 'editable-field 'default
1643 "An editable text field."
1644 :convert-widget 'widget-value-convert-widget
1645 :keymap widget-field-keymap
1646 :format "%v"
1647 :help-echo "M-TAB: complete field; RET: enter value"
1648 :value ""
1649 :prompt-internal 'widget-field-prompt-internal
1650 :prompt-history 'widget-field-history
1651 :prompt-value 'widget-field-prompt-value
1652 :action 'widget-field-action
1653 :validate 'widget-field-validate
1654 :valid-regexp ""
1655 :error "Field's value doesn't match allowed forms"
1656 :value-create 'widget-field-value-create
1657 :value-delete 'widget-field-value-delete
1658 :value-get 'widget-field-value-get
1659 :match 'widget-field-match)
1661 (defvar widget-field-history nil
1662 "History of field minibuffer edits.")
1664 (defun widget-field-prompt-internal (widget prompt initial history)
1665 "Read string for WIDGET promptinhg with PROMPT.
1666 INITIAL is the initial input and HISTORY is a symbol containing
1667 the earlier input."
1668 (read-string prompt initial history))
1670 (defun widget-field-prompt-value (widget prompt value unbound)
1671 "Prompt for a string."
1672 (widget-apply widget
1673 :value-to-external
1674 (widget-apply widget
1675 :prompt-internal prompt
1676 (unless unbound
1677 (cons (widget-apply widget
1678 :value-to-internal value)
1680 (widget-get widget :prompt-history))))
1682 (defvar widget-edit-functions nil)
1684 (defun widget-field-action (widget &optional event)
1685 "Move to next field."
1686 (widget-forward 1)
1687 (run-hook-with-args 'widget-edit-functions widget))
1689 (defun widget-field-validate (widget)
1690 "Valid if the content matches `:valid-regexp'."
1691 (unless (string-match (widget-get widget :valid-regexp)
1692 (widget-apply widget :value-get))
1693 widget))
1695 (defun widget-field-value-create (widget)
1696 "Create an editable text field."
1697 (let ((size (widget-get widget :size))
1698 (value (widget-get widget :value))
1699 (from (point))
1700 ;; This is changed to a real overlay in `widget-setup'. We
1701 ;; need the end points to behave differently until
1702 ;; `widget-setup' is called.
1703 (overlay (cons (make-marker) (make-marker))))
1704 (widget-put widget :field-overlay overlay)
1705 (insert value)
1706 (and size
1707 (< (length value) size)
1708 (insert-char ?\ (- size (length value))))
1709 (unless (memq widget widget-field-list)
1710 (setq widget-field-new (cons widget widget-field-new)))
1711 (move-marker (cdr overlay) (point))
1712 (set-marker-insertion-type (cdr overlay) nil)
1713 (when (null size)
1714 (insert ?\n))
1715 (move-marker (car overlay) from)
1716 (set-marker-insertion-type (car overlay) t)))
1718 (defun widget-field-value-delete (widget)
1719 "Remove the widget from the list of active editing fields."
1720 (setq widget-field-list (delq widget widget-field-list))
1721 ;; These are nil if the :format string doesn't contain `%v'.
1722 (let ((overlay (widget-get widget :field-overlay)))
1723 (when overlay
1724 (delete-overlay overlay))))
1726 (defun widget-field-value-get (widget)
1727 "Return current text in editing field."
1728 (let ((from (widget-field-start widget))
1729 (to (widget-field-end widget))
1730 (buffer (widget-field-buffer widget))
1731 (size (widget-get widget :size))
1732 (secret (widget-get widget :secret))
1733 (old (current-buffer)))
1734 (if (and from to)
1735 (progn
1736 (set-buffer buffer)
1737 (while (and size
1738 (not (zerop size))
1739 (> to from)
1740 (eq (char-after (1- to)) ?\ ))
1741 (setq to (1- to)))
1742 (let ((result (buffer-substring-no-properties from to)))
1743 (when secret
1744 (let ((index 0))
1745 (while (< (+ from index) to)
1746 (aset result index
1747 (get-char-property (+ from index) 'secret))
1748 (setq index (1+ index)))))
1749 (set-buffer old)
1750 result))
1751 (widget-get widget :value))))
1753 (defun widget-field-match (widget value)
1754 ;; Match any string.
1755 (stringp value))
1757 ;;; The `text' Widget.
1759 (define-widget 'text 'editable-field
1760 :keymap widget-text-keymap
1761 "A multiline text area.")
1763 ;;; The `menu-choice' Widget.
1765 (define-widget 'menu-choice 'default
1766 "A menu of options."
1767 :convert-widget 'widget-types-convert-widget
1768 :format "%[%t%]: %v"
1769 :case-fold t
1770 :tag "choice"
1771 :void '(item :format "invalid (%t)\n")
1772 :value-create 'widget-choice-value-create
1773 :value-delete 'widget-children-value-delete
1774 :value-get 'widget-choice-value-get
1775 :value-inline 'widget-choice-value-inline
1776 :default-get 'widget-choice-default-get
1777 :mouse-down-action 'widget-choice-mouse-down-action
1778 :action 'widget-choice-action
1779 :error "Make a choice"
1780 :validate 'widget-choice-validate
1781 :match 'widget-choice-match
1782 :match-inline 'widget-choice-match-inline)
1784 (defun widget-choice-value-create (widget)
1785 "Insert the first choice that matches the value."
1786 (let ((value (widget-get widget :value))
1787 (args (widget-get widget :args))
1788 (explicit (widget-get widget :explicit-choice))
1789 current)
1790 (if (and explicit (equal value (widget-get widget :explicit-choice-value)))
1791 (progn
1792 ;; If the user specified the choice for this value,
1793 ;; respect that choice as long as the value is the same.
1794 (widget-put widget :children (list (widget-create-child-value
1795 widget explicit value)))
1796 (widget-put widget :choice explicit))
1797 (while args
1798 (setq current (car args)
1799 args (cdr args))
1800 (when (widget-apply current :match value)
1801 (widget-put widget :children (list (widget-create-child-value
1802 widget current value)))
1803 (widget-put widget :choice current)
1804 (setq args nil
1805 current nil)))
1806 (when current
1807 (let ((void (widget-get widget :void)))
1808 (widget-put widget :children (list (widget-create-child-and-convert
1809 widget void :value value)))
1810 (widget-put widget :choice void))))))
1812 (defun widget-choice-value-get (widget)
1813 ;; Get value of the child widget.
1814 (widget-value (car (widget-get widget :children))))
1816 (defun widget-choice-value-inline (widget)
1817 ;; Get value of the child widget.
1818 (widget-apply (car (widget-get widget :children)) :value-inline))
1820 (defun widget-choice-default-get (widget)
1821 ;; Get default for the first choice.
1822 (widget-default-get (car (widget-get widget :args))))
1824 (defcustom widget-choice-toggle nil
1825 "If non-nil, a binary choice will just toggle between the values.
1826 Otherwise, the user will explicitly have to choose between the values
1827 when he invoked the menu."
1828 :type 'boolean
1829 :group 'widgets)
1831 (defun widget-choice-mouse-down-action (widget &optional event)
1832 ;; Return non-nil if we need a menu.
1833 (let ((args (widget-get widget :args))
1834 (old (widget-get widget :choice)))
1835 (cond ((not (display-popup-menus-p))
1836 ;; No place to pop up a menu.
1837 nil)
1838 ((< (length args) 2)
1839 ;; Empty or singleton list, just return the value.
1840 nil)
1841 ((> (length args) widget-menu-max-size)
1842 ;; Too long, prompt.
1843 nil)
1844 ((> (length args) 2)
1845 ;; Reasonable sized list, use menu.
1847 ((and widget-choice-toggle (memq old args))
1848 ;; We toggle.
1849 nil)
1851 ;; Ask which of the two.
1852 t))))
1854 (defun widget-choice-action (widget &optional event)
1855 ;; Make a choice.
1856 (let ((args (widget-get widget :args))
1857 (old (widget-get widget :choice))
1858 (tag (widget-apply widget :menu-tag-get))
1859 (completion-ignore-case (widget-get widget :case-fold))
1860 this-explicit
1861 current choices)
1862 ;; Remember old value.
1863 (if (and old (not (widget-apply widget :validate)))
1864 (let* ((external (widget-value widget))
1865 (internal (widget-apply old :value-to-internal external)))
1866 (widget-put old :value internal)))
1867 ;; Find new choice.
1868 (setq current
1869 (cond ((= (length args) 0)
1870 nil)
1871 ((= (length args) 1)
1872 (nth 0 args))
1873 ((and widget-choice-toggle
1874 (= (length args) 2)
1875 (memq old args))
1876 (if (eq old (nth 0 args))
1877 (nth 1 args)
1878 (nth 0 args)))
1880 (while args
1881 (setq current (car args)
1882 args (cdr args))
1883 (setq choices
1884 (cons (cons (widget-apply current :menu-tag-get)
1885 current)
1886 choices)))
1887 (setq this-explicit t)
1888 (widget-choose tag (reverse choices) event))))
1889 (when current
1890 ;; If this was an explicit user choice,
1891 ;; record the choice, and the record the value it was made for.
1892 ;; widget-choice-value-create will respect this choice,
1893 ;; as long as the value is the same.
1894 (when this-explicit
1895 (widget-put widget :explicit-choice current)
1896 (widget-put widget :explicit-choice-value (widget-get widget :value)))
1897 (widget-value-set
1898 widget (widget-apply current
1899 :value-to-external (widget-default-get current)))
1900 (widget-setup)
1901 (widget-apply widget :notify widget event)))
1902 (run-hook-with-args 'widget-edit-functions widget))
1904 (defun widget-choice-validate (widget)
1905 ;; Valid if we have made a valid choice.
1906 (if (eq (widget-get widget :void) (widget-get widget :choice))
1907 widget
1908 (widget-apply (car (widget-get widget :children)) :validate)))
1910 (defun widget-choice-match (widget value)
1911 ;; Matches if one of the choices matches.
1912 (let ((args (widget-get widget :args))
1913 current found)
1914 (while (and args (not found))
1915 (setq current (car args)
1916 args (cdr args)
1917 found (widget-apply current :match value)))
1918 found))
1920 (defun widget-choice-match-inline (widget values)
1921 ;; Matches if one of the choices matches.
1922 (let ((args (widget-get widget :args))
1923 current found)
1924 (while (and args (null found))
1925 (setq current (car args)
1926 args (cdr args)
1927 found (widget-match-inline current values)))
1928 found))
1930 ;;; The `toggle' Widget.
1932 (define-widget 'toggle 'item
1933 "Toggle between two states."
1934 :format "%[%v%]\n"
1935 :value-create 'widget-toggle-value-create
1936 :action 'widget-toggle-action
1937 :match (lambda (widget value) t)
1938 :on "on"
1939 :off "off")
1941 (defun widget-toggle-value-create (widget)
1942 "Insert text representing the `on' and `off' states."
1943 (if (widget-value widget)
1944 (widget-image-insert widget
1945 (widget-get widget :on)
1946 (widget-get widget :on-glyph))
1947 (widget-image-insert widget
1948 (widget-get widget :off)
1949 (widget-get widget :off-glyph))))
1951 (defun widget-toggle-action (widget &optional event)
1952 ;; Toggle value.
1953 (widget-value-set widget (not (widget-value widget)))
1954 (widget-apply widget :notify widget event)
1955 (run-hook-with-args 'widget-edit-functions widget))
1957 ;;; The `checkbox' Widget.
1959 (define-widget 'checkbox 'toggle
1960 "A checkbox toggle."
1961 :button-suffix ""
1962 :button-prefix ""
1963 :format "%[%v%]"
1964 :on "[X]"
1965 ;; We could probably do the same job as the images using single
1966 ;; space characters in a boxed face with a stretch specification to
1967 ;; make them square.
1968 :on-glyph (create-image (make-bool-vector 49 1)
1969 'xbm t :width 7 :height 7
1970 :foreground "grey75" ; like default mode line
1971 :relief -3 :ascent 'center)
1972 :off "[ ]"
1973 :off-glyph (create-image (make-bool-vector 49 1)
1974 'xbm t :width 7 :height 7
1975 :foreground "grey75"
1976 :relief 3 :ascent 'center)
1977 :help-echo "Toggle this item."
1978 :action 'widget-checkbox-action)
1980 (defun widget-checkbox-action (widget &optional event)
1981 "Toggle checkbox, notify parent, and set active state of sibling."
1982 (widget-toggle-action widget event)
1983 (let ((sibling (widget-get-sibling widget)))
1984 (when sibling
1985 (if (widget-value widget)
1986 (widget-apply sibling :activate)
1987 (widget-apply sibling :deactivate)))))
1989 ;;; The `checklist' Widget.
1991 (define-widget 'checklist 'default
1992 "A multiple choice widget."
1993 :convert-widget 'widget-types-convert-widget
1994 :format "%v"
1995 :offset 4
1996 :entry-format "%b %v"
1997 :menu-tag "checklist"
1998 :greedy nil
1999 :value-create 'widget-checklist-value-create
2000 :value-delete 'widget-children-value-delete
2001 :value-get 'widget-checklist-value-get
2002 :validate 'widget-checklist-validate
2003 :match 'widget-checklist-match
2004 :match-inline 'widget-checklist-match-inline)
2006 (defun widget-checklist-value-create (widget)
2007 ;; Insert all values
2008 (let ((alist (widget-checklist-match-find widget (widget-get widget :value)))
2009 (args (widget-get widget :args)))
2010 (while args
2011 (widget-checklist-add-item widget (car args) (assq (car args) alist))
2012 (setq args (cdr args)))
2013 (widget-put widget :children (nreverse (widget-get widget :children)))))
2015 (defun widget-checklist-add-item (widget type chosen)
2016 "Create checklist item in WIDGET of type TYPE.
2017 If the item is checked, CHOSEN is a cons whose cdr is the value."
2018 (and (eq (preceding-char) ?\n)
2019 (widget-get widget :indent)
2020 (insert-char ? (widget-get widget :indent)))
2021 (widget-specify-insert
2022 (let* ((children (widget-get widget :children))
2023 (buttons (widget-get widget :buttons))
2024 (button-args (or (widget-get type :sibling-args)
2025 (widget-get widget :button-args)))
2026 (from (point))
2027 child button)
2028 (insert (widget-get widget :entry-format))
2029 (goto-char from)
2030 ;; Parse % escapes in format.
2031 (while (re-search-forward "%\\([bv%]\\)" nil t)
2032 (let ((escape (char-after (match-beginning 1))))
2033 (delete-backward-char 2)
2034 (cond ((eq escape ?%)
2035 (insert ?%))
2036 ((eq escape ?b)
2037 (setq button (apply 'widget-create-child-and-convert
2038 widget 'checkbox
2039 :value (not (null chosen))
2040 button-args)))
2041 ((eq escape ?v)
2042 (setq child
2043 (cond ((not chosen)
2044 (let ((child (widget-create-child widget type)))
2045 (widget-apply child :deactivate)
2046 child))
2047 ((widget-get type :inline)
2048 (widget-create-child-value
2049 widget type (cdr chosen)))
2051 (widget-create-child-value
2052 widget type (car (cdr chosen)))))))
2054 (error "Unknown escape `%c'" escape)))))
2055 ;; Update properties.
2056 (and button child (widget-put child :button button))
2057 (and button (widget-put widget :buttons (cons button buttons)))
2058 (and child (widget-put widget :children (cons child children))))))
2060 (defun widget-checklist-match (widget values)
2061 ;; All values must match a type in the checklist.
2062 (and (listp values)
2063 (null (cdr (widget-checklist-match-inline widget values)))))
2065 (defun widget-checklist-match-inline (widget values)
2066 ;; Find the values which match a type in the checklist.
2067 (let ((greedy (widget-get widget :greedy))
2068 (args (copy-sequence (widget-get widget :args)))
2069 found rest)
2070 (while values
2071 (let ((answer (widget-checklist-match-up args values)))
2072 (cond (answer
2073 (let ((vals (widget-match-inline answer values)))
2074 (setq found (append found (car vals))
2075 values (cdr vals)
2076 args (delq answer args))))
2077 (greedy
2078 (setq rest (append rest (list (car values)))
2079 values (cdr values)))
2081 (setq rest (append rest values)
2082 values nil)))))
2083 (cons found rest)))
2085 (defun widget-checklist-match-find (widget vals)
2086 "Find the vals which match a type in the checklist.
2087 Return an alist of (TYPE MATCH)."
2088 (let ((greedy (widget-get widget :greedy))
2089 (args (copy-sequence (widget-get widget :args)))
2090 found)
2091 (while vals
2092 (let ((answer (widget-checklist-match-up args vals)))
2093 (cond (answer
2094 (let ((match (widget-match-inline answer vals)))
2095 (setq found (cons (cons answer (car match)) found)
2096 vals (cdr match)
2097 args (delq answer args))))
2098 (greedy
2099 (setq vals (cdr vals)))
2101 (setq vals nil)))))
2102 found))
2104 (defun widget-checklist-match-up (args vals)
2105 "Return the first type from ARGS that matches VALS."
2106 (let (current found)
2107 (while (and args (null found))
2108 (setq current (car args)
2109 args (cdr args)
2110 found (widget-match-inline current vals)))
2111 (if found
2112 current)))
2114 (defun widget-checklist-value-get (widget)
2115 ;; The values of all selected items.
2116 (let ((children (widget-get widget :children))
2117 child result)
2118 (while children
2119 (setq child (car children)
2120 children (cdr children))
2121 (if (widget-value (widget-get child :button))
2122 (setq result (append result (widget-apply child :value-inline)))))
2123 result))
2125 (defun widget-checklist-validate (widget)
2126 ;; Ticked chilren must be valid.
2127 (let ((children (widget-get widget :children))
2128 child button found)
2129 (while (and children (not found))
2130 (setq child (car children)
2131 children (cdr children)
2132 button (widget-get child :button)
2133 found (and (widget-value button)
2134 (widget-apply child :validate))))
2135 found))
2137 ;;; The `option' Widget
2139 (define-widget 'option 'checklist
2140 "An widget with an optional item."
2141 :inline t)
2143 ;;; The `choice-item' Widget.
2145 (define-widget 'choice-item 'item
2146 "Button items that delegate action events to their parents."
2147 :action 'widget-parent-action
2148 :format "%[%t%] \n")
2150 ;;; The `radio-button' Widget.
2152 (define-widget 'radio-button 'toggle
2153 "A radio button for use in the `radio' widget."
2154 :notify 'widget-radio-button-notify
2155 :format "%[%v%]"
2156 :button-suffix ""
2157 :button-prefix ""
2158 :on "(*)"
2159 :on-glyph "radio1"
2160 :off "( )"
2161 :off-glyph "radio0")
2163 (defun widget-radio-button-notify (widget child &optional event)
2164 ;; Tell daddy.
2165 (widget-apply (widget-get widget :parent) :action widget event))
2167 ;;; The `radio-button-choice' Widget.
2169 (define-widget 'radio-button-choice 'default
2170 "Select one of multiple options."
2171 :convert-widget 'widget-types-convert-widget
2172 :offset 4
2173 :format "%v"
2174 :entry-format "%b %v"
2175 :menu-tag "radio"
2176 :value-create 'widget-radio-value-create
2177 :value-delete 'widget-children-value-delete
2178 :value-get 'widget-radio-value-get
2179 :value-inline 'widget-radio-value-inline
2180 :value-set 'widget-radio-value-set
2181 :error "You must push one of the buttons"
2182 :validate 'widget-radio-validate
2183 :match 'widget-choice-match
2184 :match-inline 'widget-choice-match-inline
2185 :action 'widget-radio-action)
2187 (defun widget-radio-value-create (widget)
2188 ;; Insert all values
2189 (let ((args (widget-get widget :args))
2190 arg)
2191 (while args
2192 (setq arg (car args)
2193 args (cdr args))
2194 (widget-radio-add-item widget arg))))
2196 (defun widget-radio-add-item (widget type)
2197 "Add to radio widget WIDGET a new radio button item of type TYPE."
2198 ;; (setq type (widget-convert type))
2199 (and (eq (preceding-char) ?\n)
2200 (widget-get widget :indent)
2201 (insert-char ? (widget-get widget :indent)))
2202 (widget-specify-insert
2203 (let* ((value (widget-get widget :value))
2204 (children (widget-get widget :children))
2205 (buttons (widget-get widget :buttons))
2206 (button-args (or (widget-get type :sibling-args)
2207 (widget-get widget :button-args)))
2208 (from (point))
2209 (chosen (and (null (widget-get widget :choice))
2210 (widget-apply type :match value)))
2211 child button)
2212 (insert (widget-get widget :entry-format))
2213 (goto-char from)
2214 ;; Parse % escapes in format.
2215 (while (re-search-forward "%\\([bv%]\\)" nil t)
2216 (let ((escape (char-after (match-beginning 1))))
2217 (delete-backward-char 2)
2218 (cond ((eq escape ?%)
2219 (insert ?%))
2220 ((eq escape ?b)
2221 (setq button (apply 'widget-create-child-and-convert
2222 widget 'radio-button
2223 :value (not (null chosen))
2224 button-args)))
2225 ((eq escape ?v)
2226 (setq child (if chosen
2227 (widget-create-child-value
2228 widget type value)
2229 (widget-create-child widget type)))
2230 (unless chosen
2231 (widget-apply child :deactivate)))
2233 (error "Unknown escape `%c'" escape)))))
2234 ;; Update properties.
2235 (when chosen
2236 (widget-put widget :choice type))
2237 (when button
2238 (widget-put child :button button)
2239 (widget-put widget :buttons (nconc buttons (list button))))
2240 (when child
2241 (widget-put widget :children (nconc children (list child))))
2242 child)))
2244 (defun widget-radio-value-get (widget)
2245 ;; Get value of the child widget.
2246 (let ((chosen (widget-radio-chosen widget)))
2247 (and chosen (widget-value chosen))))
2249 (defun widget-radio-chosen (widget)
2250 "Return the widget representing the chosen radio button."
2251 (let ((children (widget-get widget :children))
2252 current found)
2253 (while children
2254 (setq current (car children)
2255 children (cdr children))
2256 (when (widget-apply (widget-get current :button) :value-get)
2257 (setq found current
2258 children nil)))
2259 found))
2261 (defun widget-radio-value-inline (widget)
2262 ;; Get value of the child widget.
2263 (let ((children (widget-get widget :children))
2264 current found)
2265 (while children
2266 (setq current (car children)
2267 children (cdr children))
2268 (when (widget-apply (widget-get current :button) :value-get)
2269 (setq found (widget-apply current :value-inline)
2270 children nil)))
2271 found))
2273 (defun widget-radio-value-set (widget value)
2274 ;; We can't just delete and recreate a radio widget, since children
2275 ;; can be added after the original creation and won't be recreated
2276 ;; by `:create'.
2277 (let ((children (widget-get widget :children))
2278 current found)
2279 (while children
2280 (setq current (car children)
2281 children (cdr children))
2282 (let* ((button (widget-get current :button))
2283 (match (and (not found)
2284 (widget-apply current :match value))))
2285 (widget-value-set button match)
2286 (if match
2287 (progn
2288 (widget-value-set current value)
2289 (widget-apply current :activate))
2290 (widget-apply current :deactivate))
2291 (setq found (or found match))))))
2293 (defun widget-radio-validate (widget)
2294 ;; Valid if we have made a valid choice.
2295 (let ((children (widget-get widget :children))
2296 current found button)
2297 (while (and children (not found))
2298 (setq current (car children)
2299 children (cdr children)
2300 button (widget-get current :button)
2301 found (widget-apply button :value-get)))
2302 (if found
2303 (widget-apply current :validate)
2304 widget)))
2306 (defun widget-radio-action (widget child event)
2307 ;; Check if a radio button was pressed.
2308 (let ((children (widget-get widget :children))
2309 (buttons (widget-get widget :buttons))
2310 current)
2311 (when (memq child buttons)
2312 (while children
2313 (setq current (car children)
2314 children (cdr children))
2315 (let* ((button (widget-get current :button)))
2316 (cond ((eq child button)
2317 (widget-value-set button t)
2318 (widget-apply current :activate))
2319 ((widget-value button)
2320 (widget-value-set button nil)
2321 (widget-apply current :deactivate)))))))
2322 ;; Pass notification to parent.
2323 (widget-apply widget :notify child event))
2325 ;;; The `insert-button' Widget.
2327 (define-widget 'insert-button 'push-button
2328 "An insert button for the `editable-list' widget."
2329 :tag "INS"
2330 :help-echo "Insert a new item into the list at this position."
2331 :action 'widget-insert-button-action)
2333 (defun widget-insert-button-action (widget &optional event)
2334 ;; Ask the parent to insert a new item.
2335 (widget-apply (widget-get widget :parent)
2336 :insert-before (widget-get widget :widget)))
2338 ;;; The `delete-button' Widget.
2340 (define-widget 'delete-button 'push-button
2341 "A delete button for the `editable-list' widget."
2342 :tag "DEL"
2343 :help-echo "Delete this item from the list."
2344 :action 'widget-delete-button-action)
2346 (defun widget-delete-button-action (widget &optional event)
2347 ;; Ask the parent to insert a new item.
2348 (widget-apply (widget-get widget :parent)
2349 :delete-at (widget-get widget :widget)))
2351 ;;; The `editable-list' Widget.
2353 ;; (defcustom widget-editable-list-gui nil
2354 ;; "If non nil, use GUI push-buttons in editable list when available."
2355 ;; :type 'boolean
2356 ;; :group 'widgets)
2358 (define-widget 'editable-list 'default
2359 "A variable list of widgets of the same type."
2360 :convert-widget 'widget-types-convert-widget
2361 :offset 12
2362 :format "%v%i\n"
2363 :format-handler 'widget-editable-list-format-handler
2364 :entry-format "%i %d %v"
2365 :menu-tag "editable-list"
2366 :value-create 'widget-editable-list-value-create
2367 :value-delete 'widget-children-value-delete
2368 :value-get 'widget-editable-list-value-get
2369 :validate 'widget-children-validate
2370 :match 'widget-editable-list-match
2371 :match-inline 'widget-editable-list-match-inline
2372 :insert-before 'widget-editable-list-insert-before
2373 :delete-at 'widget-editable-list-delete-at)
2375 (defun widget-editable-list-format-handler (widget escape)
2376 ;; We recognize the insert button.
2377 ;;; (let ((widget-push-button-gui widget-editable-list-gui))
2378 (cond ((eq escape ?i)
2379 (and (widget-get widget :indent)
2380 (insert-char ?\ (widget-get widget :indent)))
2381 (apply 'widget-create-child-and-convert
2382 widget 'insert-button
2383 (widget-get widget :append-button-args)))
2385 (widget-default-format-handler widget escape)))
2386 ;;; )
2389 (defun widget-editable-list-value-create (widget)
2390 ;; Insert all values
2391 (let* ((value (widget-get widget :value))
2392 (type (nth 0 (widget-get widget :args)))
2393 children)
2394 (widget-put widget :value-pos (copy-marker (point)))
2395 (set-marker-insertion-type (widget-get widget :value-pos) t)
2396 (while value
2397 (let ((answer (widget-match-inline type value)))
2398 (if answer
2399 (setq children (cons (widget-editable-list-entry-create
2400 widget
2401 (if (widget-get type :inline)
2402 (car answer)
2403 (car (car answer)))
2405 children)
2406 value (cdr answer))
2407 (setq value nil))))
2408 (widget-put widget :children (nreverse children))))
2410 (defun widget-editable-list-value-get (widget)
2411 ;; Get value of the child widget.
2412 (apply 'append (mapcar (lambda (child) (widget-apply child :value-inline))
2413 (widget-get widget :children))))
2415 (defun widget-editable-list-match (widget value)
2416 ;; Value must be a list and all the members must match the type.
2417 (and (listp value)
2418 (null (cdr (widget-editable-list-match-inline widget value)))))
2420 (defun widget-editable-list-match-inline (widget value)
2421 (let ((type (nth 0 (widget-get widget :args)))
2422 (ok t)
2423 found)
2424 (while (and value ok)
2425 (let ((answer (widget-match-inline type value)))
2426 (if answer
2427 (setq found (append found (car answer))
2428 value (cdr answer))
2429 (setq ok nil))))
2430 (cons found value)))
2432 (defun widget-editable-list-insert-before (widget before)
2433 ;; Insert a new child in the list of children.
2434 (save-excursion
2435 (let ((children (widget-get widget :children))
2436 (inhibit-read-only t)
2437 before-change-functions
2438 after-change-functions)
2439 (cond (before
2440 (goto-char (widget-get before :entry-from)))
2442 (goto-char (widget-get widget :value-pos))))
2443 (let ((child (widget-editable-list-entry-create
2444 widget nil nil)))
2445 (when (< (widget-get child :entry-from) (widget-get widget :from))
2446 (set-marker (widget-get widget :from)
2447 (widget-get child :entry-from)))
2448 (if (eq (car children) before)
2449 (widget-put widget :children (cons child children))
2450 (while (not (eq (car (cdr children)) before))
2451 (setq children (cdr children)))
2452 (setcdr children (cons child (cdr children)))))))
2453 (widget-setup)
2454 (widget-apply widget :notify widget))
2456 (defun widget-editable-list-delete-at (widget child)
2457 ;; Delete child from list of children.
2458 (save-excursion
2459 (let ((buttons (copy-sequence (widget-get widget :buttons)))
2460 button
2461 (inhibit-read-only t)
2462 before-change-functions
2463 after-change-functions)
2464 (while buttons
2465 (setq button (car buttons)
2466 buttons (cdr buttons))
2467 (when (eq (widget-get button :widget) child)
2468 (widget-put widget
2469 :buttons (delq button (widget-get widget :buttons)))
2470 (widget-delete button))))
2471 (let ((entry-from (widget-get child :entry-from))
2472 (entry-to (widget-get child :entry-to))
2473 (inhibit-read-only t)
2474 before-change-functions
2475 after-change-functions)
2476 (widget-delete child)
2477 (delete-region entry-from entry-to)
2478 (set-marker entry-from nil)
2479 (set-marker entry-to nil))
2480 (widget-put widget :children (delq child (widget-get widget :children))))
2481 (widget-setup)
2482 (widget-apply widget :notify widget))
2484 (defun widget-editable-list-entry-create (widget value conv)
2485 ;; Create a new entry to the list.
2486 (let ((type (nth 0 (widget-get widget :args)))
2487 ;;; (widget-push-button-gui widget-editable-list-gui)
2488 child delete insert)
2489 (widget-specify-insert
2490 (save-excursion
2491 (and (widget-get widget :indent)
2492 (insert-char ?\ (widget-get widget :indent)))
2493 (insert (widget-get widget :entry-format)))
2494 ;; Parse % escapes in format.
2495 (while (re-search-forward "%\\(.\\)" nil t)
2496 (let ((escape (char-after (match-beginning 1))))
2497 (delete-backward-char 2)
2498 (cond ((eq escape ?%)
2499 (insert ?%))
2500 ((eq escape ?i)
2501 (setq insert (apply 'widget-create-child-and-convert
2502 widget 'insert-button
2503 (widget-get widget :insert-button-args))))
2504 ((eq escape ?d)
2505 (setq delete (apply 'widget-create-child-and-convert
2506 widget 'delete-button
2507 (widget-get widget :delete-button-args))))
2508 ((eq escape ?v)
2509 (if conv
2510 (setq child (widget-create-child-value
2511 widget type value))
2512 (setq child (widget-create-child-value
2513 widget type
2514 (widget-apply type :value-to-external
2515 (widget-default-get type))))))
2517 (error "Unknown escape `%c'" escape)))))
2518 (widget-put widget
2519 :buttons (cons delete
2520 (cons insert
2521 (widget-get widget :buttons))))
2522 (let ((entry-from (point-min-marker))
2523 (entry-to (point-max-marker)))
2524 (set-marker-insertion-type entry-from t)
2525 (set-marker-insertion-type entry-to nil)
2526 (widget-put child :entry-from entry-from)
2527 (widget-put child :entry-to entry-to)))
2528 (widget-put insert :widget child)
2529 (widget-put delete :widget child)
2530 child))
2532 ;;; The `group' Widget.
2534 (define-widget 'group 'default
2535 "A widget which groups other widgets inside."
2536 :convert-widget 'widget-types-convert-widget
2537 :format "%v"
2538 :value-create 'widget-group-value-create
2539 :value-delete 'widget-children-value-delete
2540 :value-get 'widget-editable-list-value-get
2541 :default-get 'widget-group-default-get
2542 :validate 'widget-children-validate
2543 :match 'widget-group-match
2544 :match-inline 'widget-group-match-inline)
2546 (defun widget-group-value-create (widget)
2547 ;; Create each component.
2548 (let ((args (widget-get widget :args))
2549 (value (widget-get widget :value))
2550 arg answer children)
2551 (while args
2552 (setq arg (car args)
2553 args (cdr args)
2554 answer (widget-match-inline arg value)
2555 value (cdr answer))
2556 (and (eq (preceding-char) ?\n)
2557 (widget-get widget :indent)
2558 (insert-char ?\ (widget-get widget :indent)))
2559 (push (cond ((null answer)
2560 (widget-create-child widget arg))
2561 ((widget-get arg :inline)
2562 (widget-create-child-value widget arg (car answer)))
2564 (widget-create-child-value widget arg (car (car answer)))))
2565 children))
2566 (widget-put widget :children (nreverse children))))
2568 (defun widget-group-default-get (widget)
2569 ;; Get the default of the components.
2570 (mapcar 'widget-default-get (widget-get widget :args)))
2572 (defun widget-group-match (widget values)
2573 ;; Match if the components match.
2574 (and (listp values)
2575 (let ((match (widget-group-match-inline widget values)))
2576 (and match (null (cdr match))))))
2578 (defun widget-group-match-inline (widget vals)
2579 ;; Match if the components match.
2580 (let ((args (widget-get widget :args))
2581 argument answer found)
2582 (while args
2583 (setq argument (car args)
2584 args (cdr args)
2585 answer (widget-match-inline argument vals))
2586 (if answer
2587 (setq vals (cdr answer)
2588 found (append found (car answer)))
2589 (setq vals nil
2590 args nil)))
2591 (if answer
2592 (cons found vals))))
2594 ;;; The `visibility' Widget.
2596 (define-widget 'visibility 'item
2597 "An indicator and manipulator for hidden items."
2598 :format "%[%v%]"
2599 :button-prefix ""
2600 :button-suffix ""
2601 :on "Hide"
2602 :off "Show"
2603 :value-create 'widget-visibility-value-create
2604 :action 'widget-toggle-action
2605 :match (lambda (widget value) t))
2607 (defun widget-visibility-value-create (widget)
2608 ;; Insert text representing the `on' and `off' states.
2609 (let ((on (widget-get widget :on))
2610 (off (widget-get widget :off)))
2611 (if on
2612 (setq on (concat widget-push-button-prefix
2614 widget-push-button-suffix))
2615 (setq on ""))
2616 (if off
2617 (setq off (concat widget-push-button-prefix
2619 widget-push-button-suffix))
2620 (setq off ""))
2621 (if (widget-value widget)
2622 (widget-image-insert widget on "down" "down-pushed")
2623 (widget-image-insert widget off "right" "right-pushed"))))
2625 ;;; The `documentation-link' Widget.
2627 ;; This is a helper widget for `documentation-string'.
2629 (define-widget 'documentation-link 'link
2630 "Link type used in documentation strings."
2631 :tab-order -1
2632 :help-echo "Describe this symbol"
2633 :action 'widget-documentation-link-action)
2635 (defun widget-documentation-link-action (widget &optional event)
2636 "Display documentation for WIDGET's value. Ignore optional argument EVENT."
2637 (let* ((string (widget-get widget :value))
2638 (symbol (intern string)))
2639 (if (and (fboundp symbol) (boundp symbol))
2640 ;; If there are two doc strings, give the user a way to pick one.
2641 (apropos (concat "\\`" (regexp-quote string) "\\'"))
2642 (if (fboundp symbol)
2643 (describe-function symbol)
2644 (describe-variable symbol)))))
2646 (defcustom widget-documentation-links t
2647 "Add hyperlinks to documentation strings when non-nil."
2648 :type 'boolean
2649 :group 'widget-documentation)
2651 (defcustom widget-documentation-link-regexp "`\\([^\n`' ]+\\)'"
2652 "Regexp for matching potential links in documentation strings.
2653 The first group should be the link itself."
2654 :type 'regexp
2655 :group 'widget-documentation)
2657 (defcustom widget-documentation-link-p 'intern-soft
2658 "Predicate used to test if a string is useful as a link.
2659 The value should be a function. The function will be called one
2660 argument, a string, and should return non-nil if there should be a
2661 link for that string."
2662 :type 'function
2663 :options '(widget-documentation-link-p)
2664 :group 'widget-documentation)
2666 (defcustom widget-documentation-link-type 'documentation-link
2667 "Widget type used for links in documentation strings."
2668 :type 'symbol
2669 :group 'widget-documentation)
2671 (defun widget-documentation-link-add (widget from to)
2672 (widget-specify-doc widget from to)
2673 (when widget-documentation-links
2674 (let ((regexp widget-documentation-link-regexp)
2675 (buttons (widget-get widget :buttons))
2676 (widget-mouse-face (default-value 'widget-mouse-face))
2677 (widget-button-face widget-documentation-face)
2678 (widget-button-pressed-face widget-documentation-face))
2679 (save-excursion
2680 (goto-char from)
2681 (while (re-search-forward regexp to t)
2682 (let ((name (match-string 1))
2683 (begin (match-beginning 1))
2684 (end (match-end 1)))
2685 (when (funcall widget-documentation-link-p name)
2686 (push (widget-convert-button widget-documentation-link-type
2687 begin end :value name)
2688 buttons)))))
2689 (widget-put widget :buttons buttons)))
2690 (let ((indent (widget-get widget :indent)))
2691 (when (and indent (not (zerop indent)))
2692 (save-excursion
2693 (save-restriction
2694 (narrow-to-region from to)
2695 (goto-char (point-min))
2696 (while (search-forward "\n" nil t)
2697 (insert-char ?\ indent)))))))
2699 ;;; The `documentation-string' Widget.
2701 (define-widget 'documentation-string 'item
2702 "A documentation string."
2703 :format "%v"
2704 :action 'widget-documentation-string-action
2705 :value-delete 'widget-children-value-delete
2706 :value-create 'widget-documentation-string-value-create)
2708 (defun widget-documentation-string-value-create (widget)
2709 ;; Insert documentation string.
2710 (let ((doc (widget-value widget))
2711 (indent (widget-get widget :indent))
2712 (shown (widget-get (widget-get widget :parent) :documentation-shown))
2713 (start (point)))
2714 (if (string-match "\n" doc)
2715 (let ((before (substring doc 0 (match-beginning 0)))
2716 (after (substring doc (match-beginning 0)))
2717 button)
2718 (insert before ?\ )
2719 (widget-documentation-link-add widget start (point))
2720 (setq button
2721 (widget-create-child-and-convert
2722 widget 'visibility
2723 :help-echo "Show or hide rest of the documentation."
2724 :off "More"
2725 :always-active t
2726 :action 'widget-parent-action
2727 shown))
2728 (when shown
2729 (setq start (point))
2730 (when (and indent (not (zerop indent)))
2731 (insert-char ?\ indent))
2732 (insert after)
2733 (widget-documentation-link-add widget start (point)))
2734 (widget-put widget :buttons (list button)))
2735 (insert doc)
2736 (widget-documentation-link-add widget start (point))))
2737 (insert ?\n))
2739 (defun widget-documentation-string-action (widget &rest ignore)
2740 ;; Toggle documentation.
2741 (let ((parent (widget-get widget :parent)))
2742 (widget-put parent :documentation-shown
2743 (not (widget-get parent :documentation-shown))))
2744 ;; Redraw.
2745 (widget-value-set widget (widget-value widget)))
2747 ;;; The Sexp Widgets.
2749 (define-widget 'const 'item
2750 "An immutable sexp."
2751 :prompt-value 'widget-const-prompt-value
2752 :format "%t\n%d")
2754 (defun widget-const-prompt-value (widget prompt value unbound)
2755 ;; Return the value of the const.
2756 (widget-value widget))
2758 (define-widget 'function-item 'const
2759 "An immutable function name."
2760 :format "%v\n%h"
2761 :documentation-property (lambda (symbol)
2762 (condition-case nil
2763 (documentation symbol t)
2764 (error nil))))
2766 (define-widget 'variable-item 'const
2767 "An immutable variable name."
2768 :format "%v\n%h"
2769 :documentation-property 'variable-documentation)
2771 (define-widget 'other 'sexp
2772 "Matches any value, but doesn't let the user edit the value.
2773 This is useful as last item in a `choice' widget.
2774 You should use this widget type with a default value,
2775 as in (other DEFAULT) or (other :tag \"NAME\" DEFAULT).
2776 If the user selects this alternative, that specifies DEFAULT
2777 as the value."
2778 :tag "Other"
2779 :format "%t%n"
2780 :value 'other)
2782 (defvar widget-string-prompt-value-history nil
2783 "History of input to `widget-string-prompt-value'.")
2785 (define-widget 'string 'editable-field
2786 "A string"
2787 :tag "String"
2788 :format "%{%t%}: %v"
2789 :complete-function 'ispell-complete-word
2790 :prompt-history 'widget-string-prompt-value-history)
2792 (define-widget 'regexp 'string
2793 "A regular expression."
2794 :match 'widget-regexp-match
2795 :validate 'widget-regexp-validate
2796 ;; Doesn't work well with terminating newline.
2797 ;; :value-face 'widget-single-line-field-face
2798 :tag "Regexp")
2800 (defun widget-regexp-match (widget value)
2801 ;; Match valid regexps.
2802 (and (stringp value)
2803 (condition-case nil
2804 (prog1 t
2805 (string-match value ""))
2806 (error nil))))
2808 (defun widget-regexp-validate (widget)
2809 "Check that the value of WIDGET is a valid regexp."
2810 (condition-case data
2811 (prog1 nil
2812 (string-match (widget-value widget) ""))
2813 (error (widget-put widget :error (error-message-string data))
2814 widget)))
2816 (define-widget 'file 'string
2817 "A file widget.
2818 It will read a file name from the minibuffer when invoked."
2819 :complete-function 'widget-file-complete
2820 :prompt-value 'widget-file-prompt-value
2821 :format "%{%t%}: %v"
2822 ;; Doesn't work well with terminating newline.
2823 ;; :value-face 'widget-single-line-field-face
2824 :tag "File")
2826 (defun widget-file-complete ()
2827 "Perform completion on file name preceding point."
2828 (interactive)
2829 (let* ((end (point))
2830 (beg (save-excursion
2831 (skip-chars-backward "^ ")
2832 (point)))
2833 (pattern (buffer-substring beg end))
2834 (name-part (file-name-nondirectory pattern))
2835 (directory (file-name-directory pattern))
2836 (completion (file-name-completion name-part directory)))
2837 (cond ((eq completion t))
2838 ((null completion)
2839 (message "Can't find completion for \"%s\"" pattern)
2840 (ding))
2841 ((not (string= name-part completion))
2842 (delete-region beg end)
2843 (insert (expand-file-name completion directory)))
2845 (message "Making completion list...")
2846 (with-output-to-temp-buffer "*Completions*"
2847 (display-completion-list
2848 (sort (file-name-all-completions name-part directory)
2849 'string<)))
2850 (message "Making completion list...%s" "done")))))
2852 (defun widget-file-prompt-value (widget prompt value unbound)
2853 ;; Read file from minibuffer.
2854 (abbreviate-file-name
2855 (if unbound
2856 (read-file-name prompt)
2857 (let ((prompt2 (format "%s (default %s) " prompt value))
2858 (dir (file-name-directory value))
2859 (file (file-name-nondirectory value))
2860 (must-match (widget-get widget :must-match)))
2861 (read-file-name prompt2 dir nil must-match file)))))
2863 ;;;(defun widget-file-action (widget &optional event)
2864 ;;; ;; Read a file name from the minibuffer.
2865 ;;; (let* ((value (widget-value widget))
2866 ;;; (dir (file-name-directory value))
2867 ;;; (file (file-name-nondirectory value))
2868 ;;; (menu-tag (widget-apply widget :menu-tag-get))
2869 ;;; (must-match (widget-get widget :must-match))
2870 ;;; (answer (read-file-name (concat menu-tag ": (default `" value "') ")
2871 ;;; dir nil must-match file)))
2872 ;;; (widget-value-set widget (abbreviate-file-name answer))
2873 ;;; (widget-setup)
2874 ;;; (widget-apply widget :notify widget event)))
2876 (define-widget 'directory 'file
2877 "A directory widget.
2878 It will read a directory name from the minibuffer when invoked."
2879 :tag "Directory")
2881 (defvar widget-symbol-prompt-value-history nil
2882 "History of input to `widget-symbol-prompt-value'.")
2884 (define-widget 'symbol 'editable-field
2885 "A Lisp symbol."
2886 :value nil
2887 :tag "Symbol"
2888 :format "%{%t%}: %v"
2889 :match (lambda (widget value) (symbolp value))
2890 :complete-function 'lisp-complete-symbol
2891 :prompt-internal 'widget-symbol-prompt-internal
2892 :prompt-match 'symbolp
2893 :prompt-history 'widget-symbol-prompt-value-history
2894 :value-to-internal (lambda (widget value)
2895 (if (symbolp value)
2896 (symbol-name value)
2897 value))
2898 :value-to-external (lambda (widget value)
2899 (if (stringp value)
2900 (intern value)
2901 value)))
2903 (defun widget-symbol-prompt-internal (widget prompt initial history)
2904 ;; Read file from minibuffer.
2905 (let ((answer (completing-read prompt obarray
2906 (widget-get widget :prompt-match)
2907 nil initial history)))
2908 (if (and (stringp answer)
2909 (not (zerop (length answer))))
2910 answer
2911 (error "No value"))))
2913 (defvar widget-function-prompt-value-history nil
2914 "History of input to `widget-function-prompt-value'.")
2916 (define-widget 'function 'sexp
2917 "A Lisp function."
2918 :complete-function (lambda ()
2919 (interactive)
2920 (lisp-complete-symbol 'fboundp))
2921 :prompt-value 'widget-field-prompt-value
2922 :prompt-internal 'widget-symbol-prompt-internal
2923 :prompt-match 'fboundp
2924 :prompt-history 'widget-function-prompt-value-history
2925 :action 'widget-field-action
2926 :validate (lambda (widget)
2927 (unless (functionp (widget-value widget))
2928 (widget-put widget :error (format "Invalid function: %S"
2929 (widget-value widget)))
2930 widget))
2931 :value 'ignore
2932 :tag "Function")
2934 (defvar widget-variable-prompt-value-history nil
2935 "History of input to `widget-variable-prompt-value'.")
2937 (define-widget 'variable 'symbol
2938 ;; Should complete on variables.
2939 "A Lisp variable."
2940 :prompt-match 'boundp
2941 :prompt-history 'widget-variable-prompt-value-history
2942 :complete-function (lambda ()
2943 (interactive)
2944 (lisp-complete-symbol 'boundp))
2945 :tag "Variable")
2947 (defvar widget-coding-system-prompt-value-history nil
2948 "History of input to `widget-coding-system-prompt-value'.")
2950 (define-widget 'coding-system 'symbol
2951 "A MULE coding-system."
2952 :format "%{%t%}: %v"
2953 :tag "Coding system"
2954 :base-only nil
2955 :prompt-history 'widget-coding-system-prompt-value-history
2956 :prompt-value 'widget-coding-system-prompt-value
2957 :action 'widget-coding-system-action
2958 :complete-function (lambda ()
2959 (interactive)
2960 (lisp-complete-symbol 'coding-system-p))
2961 :validate (lambda (widget)
2962 (unless (coding-system-p (widget-value widget))
2963 (widget-put widget :error (format "Invalid coding system: %S"
2964 (widget-value widget)))
2965 widget))
2966 :value 'undecided
2967 :prompt-match 'coding-system-p)
2969 (defun widget-coding-system-prompt-value (widget prompt value unbound)
2970 "Read coding-system from minibuffer."
2971 (if (widget-get widget :base-only)
2972 (intern
2973 (completing-read (format "%s (default %s) " prompt value)
2974 (mapcar #'list (coding-system-list t)) nil nil nil
2975 coding-system-history))
2976 (read-coding-system (format "%s (default %s) " prompt value) value)))
2978 (defun widget-coding-system-action (widget &optional event)
2979 (let ((answer
2980 (widget-coding-system-prompt-value
2981 widget
2982 (widget-apply widget :menu-tag-get)
2983 (widget-value widget)
2984 t)))
2985 (widget-value-set widget answer)
2986 (widget-apply widget :notify widget event)
2987 (widget-setup)))
2989 (define-widget 'sexp 'editable-field
2990 "An arbitrary Lisp expression."
2991 :tag "Lisp expression"
2992 :format "%{%t%}: %v"
2993 :value nil
2994 :validate 'widget-sexp-validate
2995 :match (lambda (widget value) t)
2996 :value-to-internal 'widget-sexp-value-to-internal
2997 :value-to-external (lambda (widget value) (read value))
2998 :prompt-history 'widget-sexp-prompt-value-history
2999 :prompt-value 'widget-sexp-prompt-value)
3001 (defun widget-sexp-value-to-internal (widget value)
3002 ;; Use pp for printer representation.
3003 (let ((pp (if (symbolp value)
3004 (prin1-to-string value)
3005 (pp-to-string value))))
3006 (while (string-match "\n\\'" pp)
3007 (setq pp (substring pp 0 -1)))
3008 (if (or (string-match "\n\\'" pp)
3009 (> (length pp) 40))
3010 (concat "\n" pp)
3011 pp)))
3013 (defun widget-sexp-validate (widget)
3014 ;; Valid if we can read the string and there is no junk left after it.
3015 (with-temp-buffer
3016 (insert (widget-apply widget :value-get))
3017 (goto-char (point-min))
3018 (let (err)
3019 (condition-case data
3020 (progn
3021 ;; Avoid a confusing end-of-file error.
3022 (skip-syntax-forward "\\s-")
3023 (if (eobp)
3024 (setq err "Empty sexp -- use `nil'?")
3025 (unless (widget-apply widget :match (read (current-buffer)))
3026 (setq err (widget-get widget :type-error))))
3027 (if (and (not (eobp))
3028 (not err))
3029 (setq err (format "Junk at end of expression: %s"
3030 (buffer-substring (point)
3031 (point-max))))))
3032 (end-of-file ; Avoid confusing error message.
3033 (setq err "Unbalanced sexp"))
3034 (error (setq err (error-message-string data))))
3035 (if (not err)
3037 (widget-put widget :error err)
3038 widget))))
3040 (defvar widget-sexp-prompt-value-history nil
3041 "History of input to `widget-sexp-prompt-value'.")
3043 (defun widget-sexp-prompt-value (widget prompt value unbound)
3044 ;; Read an arbitrary sexp.
3045 (let ((found (read-string prompt
3046 (if unbound nil (cons (prin1-to-string value) 0))
3047 (widget-get widget :prompt-history))))
3048 (let ((answer (read-from-string found)))
3049 (unless (= (cdr answer) (length found))
3050 (error "Junk at end of expression: %s"
3051 (substring found (cdr answer))))
3052 (car answer))))
3054 (define-widget 'restricted-sexp 'sexp
3055 "A Lisp expression restricted to values that match.
3056 To use this type, you must define :match or :match-alternatives."
3057 :type-error "The specified value is not valid"
3058 :match 'widget-restricted-sexp-match
3059 :value-to-internal (lambda (widget value)
3060 (if (widget-apply widget :match value)
3061 (prin1-to-string value)
3062 value)))
3064 (defun widget-restricted-sexp-match (widget value)
3065 (let ((alternatives (widget-get widget :match-alternatives))
3066 matched)
3067 (while (and alternatives (not matched))
3068 (if (cond ((functionp (car alternatives))
3069 (funcall (car alternatives) value))
3070 ((and (consp (car alternatives))
3071 (eq (car (car alternatives)) 'quote))
3072 (eq value (nth 1 (car alternatives)))))
3073 (setq matched t))
3074 (setq alternatives (cdr alternatives)))
3075 matched))
3077 (define-widget 'integer 'restricted-sexp
3078 "An integer."
3079 :tag "Integer"
3080 :value 0
3081 :type-error "This field should contain an integer"
3082 :match-alternatives '(integerp))
3084 (define-widget 'number 'restricted-sexp
3085 "A floating point number."
3086 :tag "Number"
3087 :value 0.0
3088 :type-error "This field should contain a number"
3089 :match-alternatives '(numberp))
3091 (define-widget 'character 'editable-field
3092 "A character."
3093 :tag "Character"
3094 :value 0
3095 :size 1
3096 :format "%{%t%}: %v\n"
3097 :valid-regexp "\\`.\\'"
3098 :error "This field should contain a single character"
3099 :value-to-internal (lambda (widget value)
3100 (if (stringp value)
3101 value
3102 (char-to-string value)))
3103 :value-to-external (lambda (widget value)
3104 (if (stringp value)
3105 (aref value 0)
3106 value))
3107 :match (lambda (widget value)
3108 (char-valid-p value)))
3110 (define-widget 'list 'group
3111 "A Lisp list."
3112 :tag "List"
3113 :format "%{%t%}:\n%v")
3115 (define-widget 'vector 'group
3116 "A Lisp vector."
3117 :tag "Vector"
3118 :format "%{%t%}:\n%v"
3119 :match 'widget-vector-match
3120 :value-to-internal (lambda (widget value) (append value nil))
3121 :value-to-external (lambda (widget value) (apply 'vector value)))
3123 (defun widget-vector-match (widget value)
3124 (and (vectorp value)
3125 (widget-group-match widget
3126 (widget-apply widget :value-to-internal value))))
3128 (define-widget 'cons 'group
3129 "A cons-cell."
3130 :tag "Cons-cell"
3131 :format "%{%t%}:\n%v"
3132 :match 'widget-cons-match
3133 :value-to-internal (lambda (widget value)
3134 (list (car value) (cdr value)))
3135 :value-to-external (lambda (widget value)
3136 (cons (nth 0 value) (nth 1 value))))
3138 (defun widget-cons-match (widget value)
3139 (and (consp value)
3140 (widget-group-match widget
3141 (widget-apply widget :value-to-internal value))))
3143 ;;; The `plist' Widget.
3145 ;; Property lists.
3147 (define-widget 'plist 'list
3148 "A property list."
3149 :key-type '(symbol :tag "Key")
3150 :value-type '(sexp :tag "Value")
3151 :convert-widget 'widget-plist-convert-widget
3152 :tag "Plist")
3154 (defvar widget-plist-value-type) ;Dynamic variable
3156 (defun widget-plist-convert-widget (widget)
3157 ;; Handle `:options'.
3158 (let* ((options (widget-get widget :options))
3159 (other `(editable-list :inline t
3160 (group :inline t
3161 ,(widget-get widget :key-type)
3162 ,(widget-get widget :value-type))))
3163 (args (if options
3164 (list `(checklist :inline t
3165 :greedy t
3166 ,@(mapcar 'widget-plist-convert-option
3167 options))
3168 other)
3169 (list other))))
3170 (widget-put widget :args args)
3171 widget))
3173 (defun widget-plist-convert-option (option)
3174 ;; Convert a single plist option.
3175 (let (key-type value-type)
3176 (if (listp option)
3177 (let ((key (nth 0 option)))
3178 (setq value-type (nth 1 option))
3179 (if (listp key)
3180 (setq key-type key)
3181 (setq key-type `(const ,key))))
3182 (setq key-type `(const ,option)
3183 value-type widget-plist-value-type))
3184 `(group :format "Key: %v" :inline t ,key-type ,value-type)))
3187 ;;; The `alist' Widget.
3189 ;; Association lists.
3191 (define-widget 'alist 'list
3192 "An association list."
3193 :key-type '(sexp :tag "Key")
3194 :value-type '(sexp :tag "Value")
3195 :convert-widget 'widget-alist-convert-widget
3196 :tag "Alist")
3198 (defvar widget-alist-value-type) ;Dynamic variable
3200 (defun widget-alist-convert-widget (widget)
3201 ;; Handle `:options'.
3202 (let* ((options (widget-get widget :options))
3203 (other `(editable-list :inline t
3204 (cons :format "%v"
3205 ,(widget-get widget :key-type)
3206 ,(widget-get widget :value-type))))
3207 (args (if options
3208 (list `(checklist :inline t
3209 :greedy t
3210 ,@(mapcar 'widget-alist-convert-option
3211 options))
3212 other)
3213 (list other))))
3214 (widget-put widget :args args)
3215 widget))
3217 (defun widget-alist-convert-option (option)
3218 ;; Convert a single alist option.
3219 (let (key-type value-type)
3220 (if (listp option)
3221 (let ((key (nth 0 option)))
3222 (setq value-type (nth 1 option))
3223 (if (listp key)
3224 (setq key-type key)
3225 (setq key-type `(const ,key))))
3226 (setq key-type `(const ,option)
3227 value-type widget-alist-value-type))
3228 `(cons :format "Key: %v" ,key-type ,value-type)))
3230 (define-widget 'choice 'menu-choice
3231 "A union of several sexp types."
3232 :tag "Choice"
3233 :format "%{%t%}: %[Value Menu%] %v"
3234 :button-prefix 'widget-push-button-prefix
3235 :button-suffix 'widget-push-button-suffix
3236 :prompt-value 'widget-choice-prompt-value)
3238 (defun widget-choice-prompt-value (widget prompt value unbound)
3239 "Make a choice."
3240 (let ((args (widget-get widget :args))
3241 (completion-ignore-case (widget-get widget :case-fold))
3242 current choices old)
3243 ;; Find the first arg that matches VALUE.
3244 (let ((look args))
3245 (while look
3246 (if (widget-apply (car look) :match value)
3247 (setq old (car look)
3248 look nil)
3249 (setq look (cdr look)))))
3250 ;; Find new choice.
3251 (setq current
3252 (cond ((= (length args) 0)
3253 nil)
3254 ((= (length args) 1)
3255 (nth 0 args))
3256 ((and (= (length args) 2)
3257 (memq old args))
3258 (if (eq old (nth 0 args))
3259 (nth 1 args)
3260 (nth 0 args)))
3262 (while args
3263 (setq current (car args)
3264 args (cdr args))
3265 (setq choices
3266 (cons (cons (widget-apply current :menu-tag-get)
3267 current)
3268 choices)))
3269 (let ((val (completing-read prompt choices nil t)))
3270 (if (stringp val)
3271 (let ((try (try-completion val choices)))
3272 (when (stringp try)
3273 (setq val try))
3274 (cdr (assoc val choices)))
3275 nil)))))
3276 (if current
3277 (widget-prompt-value current prompt nil t)
3278 value)))
3280 (define-widget 'radio 'radio-button-choice
3281 "A union of several sexp types."
3282 :tag "Choice"
3283 :format "%{%t%}:\n%v"
3284 :prompt-value 'widget-choice-prompt-value)
3286 (define-widget 'repeat 'editable-list
3287 "A variable length homogeneous list."
3288 :tag "Repeat"
3289 :format "%{%t%}:\n%v%i\n")
3291 (define-widget 'set 'checklist
3292 "A list of members from a fixed set."
3293 :tag "Set"
3294 :format "%{%t%}:\n%v")
3296 (define-widget 'boolean 'toggle
3297 "To be nil or non-nil, that is the question."
3298 :tag "Boolean"
3299 :prompt-value 'widget-boolean-prompt-value
3300 :button-prefix 'widget-push-button-prefix
3301 :button-suffix 'widget-push-button-suffix
3302 :format "%{%t%}: %[Toggle%] %v\n"
3303 :on "on (non-nil)"
3304 :off "off (nil)")
3306 (defun widget-boolean-prompt-value (widget prompt value unbound)
3307 ;; Toggle a boolean.
3308 (y-or-n-p prompt))
3310 ;;; The `color' Widget.
3312 (define-widget 'color 'editable-field
3313 "Choose a color name (with sample)."
3314 :format "%t: %v (%{sample%})\n"
3315 :size 10
3316 :tag "Color"
3317 :value "black"
3318 :complete 'widget-color-complete
3319 :sample-face-get 'widget-color-sample-face-get
3320 :notify 'widget-color-notify
3321 :action 'widget-color-action)
3323 (defun widget-color-complete (widget)
3324 "Complete the color in WIDGET."
3325 (require 'facemenu) ; for facemenu-color-alist
3326 (let* ((prefix (buffer-substring-no-properties (widget-field-start widget)
3327 (point)))
3328 (list (or facemenu-color-alist
3329 (mapcar 'list (defined-colors))))
3330 (completion (try-completion prefix list)))
3331 (cond ((eq completion t)
3332 (message "Exact match."))
3333 ((null completion)
3334 (error "Can't find completion for \"%s\"" prefix))
3335 ((not (string-equal prefix completion))
3336 (insert-and-inherit (substring completion (length prefix))))
3338 (message "Making completion list...")
3339 (with-output-to-temp-buffer "*Completions*"
3340 (display-completion-list (all-completions prefix list nil)))
3341 (message "Making completion list...done")))))
3343 (defun widget-color-sample-face-get (widget)
3344 (let* ((value (condition-case nil
3345 (widget-value widget)
3346 (error (widget-get widget :value))))
3347 (symbol (intern (concat "fg:" value))))
3348 (condition-case nil
3349 (facemenu-get-face symbol)
3350 (error 'default))))
3352 (defun widget-color-action (widget &optional event)
3353 ;; Prompt for a color.
3354 (let* ((tag (widget-apply widget :menu-tag-get))
3355 (prompt (concat tag ": "))
3356 (value (widget-value widget))
3357 (start (widget-field-start widget))
3358 (pos (cond ((< (point) start)
3360 ((> (point) (+ start (length value)))
3361 (length value))
3363 (- (point) start))))
3364 (answer (facemenu-read-color prompt)))
3365 (unless (zerop (length answer))
3366 (widget-value-set widget answer)
3367 (widget-setup)
3368 (widget-apply widget :notify widget event))))
3370 (defun widget-color-notify (widget child &optional event)
3371 "Update the sample, and notofy the parent."
3372 (overlay-put (widget-get widget :sample-overlay)
3373 'face (widget-apply widget :sample-face-get))
3374 (widget-default-notify widget child event))
3376 ;;; The Help Echo
3378 (defun widget-echo-help (pos)
3379 "Display the help echo for widget at POS."
3380 (let* ((widget (widget-at pos))
3381 (help-echo (and widget (widget-get widget :help-echo))))
3382 (if (or (stringp help-echo)
3383 (and (functionp help-echo)
3384 ;; Kluge: help-echo originally could be a function of
3385 ;; one arg -- the widget. It is more useful in Emacs
3386 ;; 21 to have it as a function usable also as a
3387 ;; help-echo property, when it can sort out its own
3388 ;; widget if necessary. Try both calling sequences
3389 ;; (rather than messing around to get the function's
3390 ;; arity).
3391 (stringp
3392 (setq help-echo
3393 (condition-case nil
3394 (funcall help-echo
3395 (selected-window) (current-buffer)
3396 (point))
3397 (error (funcall help-echo widget))))))
3398 (stringp (eval help-echo)))
3399 (message "%s" help-echo))))
3401 ;;; The End:
3403 (provide 'wid-edit)
3405 ;;; wid-edit.el ends here