Remove CVS merge cookie left in.
[emacs.git] / lisp / wid-edit.el
bloba17d6b83e5b9d10c747c4a41164f783877c4a0d1
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 ((overlay (make-overlay from to nil
322 nil (or (not widget-field-add-space)
323 (widget-get widget :size)))))
324 (widget-put widget :field-overlay overlay)
325 ;;(overlay-put overlay 'detachable nil)
326 (overlay-put overlay 'field widget)
327 (overlay-put overlay 'keymap (widget-get widget :keymap))
328 (overlay-put overlay 'face (or (widget-get widget :value-face)
329 'widget-field-face))
330 (overlay-put overlay 'help-echo (widget-get widget :help-echo)))
331 (widget-specify-secret widget))
333 (defun widget-specify-secret (field)
334 "Replace text in FIELD with value of `:secret', if non-nil."
335 (let ((secret (widget-get field :secret))
336 (size (widget-get field :size)))
337 (when secret
338 (let ((begin (widget-field-start field))
339 (end (widget-field-end field)))
340 (when size
341 (while (and (> end begin)
342 (eq (char-after (1- end)) ?\ ))
343 (setq end (1- end))))
344 (while (< begin end)
345 (let ((old (char-after begin)))
346 (unless (eq old secret)
347 (subst-char-in-region begin (1+ begin) old secret)
348 (put-text-property begin (1+ begin) 'secret old))
349 (setq begin (1+ begin))))))))
351 (defun widget-specify-button (widget from to)
352 "Specify button for WIDGET between FROM and TO."
353 (let ((overlay (make-overlay from to nil t nil)))
354 (widget-put widget :button-overlay overlay)
355 (overlay-put overlay 'button widget)
356 (overlay-put overlay 'keymap (widget-get widget :keymap))
357 ;; We want to avoid the face with image buttons.
358 (unless (widget-get widget :suppress-face)
359 (overlay-put overlay 'face (widget-apply widget :button-face-get))
360 (overlay-put overlay 'mouse-face widget-mouse-face))
361 (overlay-put overlay 'help-echo (widget-get widget :help-echo))))
363 (defun widget-specify-sample (widget from to)
364 "Specify sample for WIDGET between FROM and TO."
365 (let ((overlay (make-overlay from to nil t nil)))
366 (overlay-put overlay 'face (widget-apply widget :sample-face-get))
367 (widget-put widget :sample-overlay overlay)))
369 (defun widget-specify-doc (widget from to)
370 "Specify documentation for WIDGET between FROM and TO."
371 (let ((overlay (make-overlay from to nil t nil)))
372 (overlay-put overlay 'widget-doc widget)
373 (overlay-put overlay 'face widget-documentation-face)
374 (widget-put widget :doc-overlay overlay)))
376 (defmacro widget-specify-insert (&rest form)
377 "Execute FORM without inheriting any text properties."
378 `(save-restriction
379 (let ((inhibit-read-only t)
380 (inhibit-modification-hooks t)
381 result)
382 (insert "<>")
383 (narrow-to-region (- (point) 2) (point))
384 (goto-char (1+ (point-min)))
385 (setq result (progn ,@form))
386 (delete-region (point-min) (1+ (point-min)))
387 (delete-region (1- (point-max)) (point-max))
388 (goto-char (point-max))
389 result)))
391 (defface widget-inactive-face '((((class grayscale color)
392 (background dark))
393 (:foreground "light gray"))
394 (((class grayscale color)
395 (background light))
396 (:foreground "dim gray"))
398 (:italic t)))
399 "Face used for inactive widgets."
400 :group 'widget-faces)
402 (defun widget-specify-inactive (widget from to)
403 "Make WIDGET inactive for user modifications."
404 (unless (widget-get widget :inactive)
405 (let ((overlay (make-overlay from to nil t nil)))
406 (overlay-put overlay 'face 'widget-inactive-face)
407 ;; This is disabled, as it makes the mouse cursor change shape.
408 ;; (overlay-put overlay 'mouse-face 'widget-inactive-face)
409 (overlay-put overlay 'evaporate t)
410 (overlay-put overlay 'priority 100)
411 (overlay-put overlay 'modification-hooks '(widget-overlay-inactive))
412 (widget-put widget :inactive overlay))))
414 (defun widget-overlay-inactive (&rest junk)
415 "Ignoring the arguments, signal an error."
416 (unless inhibit-read-only
417 (error "The widget here is not active")))
420 (defun widget-specify-active (widget)
421 "Make WIDGET active for user modifications."
422 (let ((inactive (widget-get widget :inactive)))
423 (when inactive
424 (delete-overlay inactive)
425 (widget-put widget :inactive nil))))
427 ;;; Widget Properties.
429 (defsubst widget-type (widget)
430 "Return the type of WIDGET, a symbol."
431 (car widget))
433 (defun widget-get-indirect (widget property)
434 "In WIDGET, get the value of PROPERTY.
435 If the value is a symbol, return its binding.
436 Otherwise, just return the value."
437 (let ((value (widget-get widget property)))
438 (if (symbolp value)
439 (symbol-value value)
440 value)))
442 (defun widget-member (widget property)
443 "Non-nil iff there is a definition in WIDGET for PROPERTY."
444 (cond ((plist-member (cdr widget) property)
446 ((car widget)
447 (widget-member (get (car widget) 'widget-type) property))
448 (t nil)))
450 (defun widget-value (widget)
451 "Extract the current value of WIDGET."
452 (widget-apply widget
453 :value-to-external (widget-apply widget :value-get)))
455 (defun widget-value-set (widget value)
456 "Set the current value of WIDGET to VALUE."
457 (widget-apply widget
458 :value-set (widget-apply widget
459 :value-to-internal value)))
461 (defun widget-default-get (widget)
462 "Extract the default value of WIDGET."
463 (or (widget-get widget :value)
464 (widget-apply widget :default-get)))
466 (defun widget-match-inline (widget vals)
467 "In WIDGET, match the start of VALS."
468 (cond ((widget-get widget :inline)
469 (widget-apply widget :match-inline vals))
470 ((and (listp vals)
471 (widget-apply widget :match (car vals)))
472 (cons (list (car vals)) (cdr vals)))
473 (t nil)))
475 (defun widget-apply-action (widget &optional event)
476 "Apply :action in WIDGET in response to EVENT."
477 (if (widget-apply widget :active)
478 (widget-apply widget :action event)
479 (error "Attempt to perform action on inactive widget")))
481 ;;; Helper functions.
483 ;; These are widget specific.
485 ;;;###autoload
486 (defun widget-prompt-value (widget prompt &optional value unbound)
487 "Prompt for a value matching WIDGET, using PROMPT.
488 The current value is assumed to be VALUE, unless UNBOUND is non-nil."
489 (unless (listp widget)
490 (setq widget (list widget)))
491 (setq prompt (format "[%s] %s" (widget-type widget) prompt))
492 (setq widget (widget-convert widget))
493 (let ((answer (widget-apply widget :prompt-value prompt value unbound)))
494 (unless (widget-apply widget :match answer)
495 (error "Value does not match %S type" (car widget)))
496 answer))
498 (defun widget-get-sibling (widget)
499 "Get the item WIDGET is assumed to toggle.
500 This is only meaningful for radio buttons or checkboxes in a list."
501 (let* ((children (widget-get (widget-get widget :parent) :children))
502 child)
503 (catch 'child
504 (while children
505 (setq child (car children)
506 children (cdr children))
507 (when (eq (widget-get child :button) widget)
508 (throw 'child child)))
509 nil)))
511 (defun widget-map-buttons (function &optional buffer maparg)
512 "Map FUNCTION over the buttons in BUFFER.
513 FUNCTION is called with the arguments WIDGET and MAPARG.
515 If FUNCTION returns non-nil, the walk is cancelled.
517 The arguments MAPARG, and BUFFER default to nil and (current-buffer),
518 respectively."
519 (let ((cur (point-min))
520 (widget nil)
521 (parent nil)
522 (overlays (if buffer
523 (save-excursion (set-buffer buffer) (overlay-lists))
524 (overlay-lists))))
525 (setq overlays (append (car overlays) (cdr overlays)))
526 (while (setq cur (pop overlays))
527 (setq widget (overlay-get cur 'button))
528 (if (and widget (funcall function widget maparg))
529 (setq overlays nil)))))
531 ;;; Images.
533 (defcustom widget-image-directory (file-name-as-directory
534 (expand-file-name "custom" data-directory))
535 "Where widget button images are located.
536 If this variable is nil, widget will try to locate the directory
537 automatically."
538 :group 'widgets
539 :type 'directory)
541 (defcustom widget-image-enable t
542 "If non nil, use image buttons in widgets when available."
543 :version "21.1"
544 :group 'widgets
545 :type 'boolean)
547 (defcustom widget-image-conversion
548 '((xpm ".xpm") (gif ".gif") (png ".png") (jpeg ".jpg" ".jpeg")
549 (xbm ".xbm"))
550 "Conversion alist from image formats to file name suffixes."
551 :group 'widgets
552 :type '(repeat (cons :format "%v"
553 (symbol :tag "Image Format" unknown)
554 (repeat :tag "Suffixes"
555 (string :format "%v")))))
557 (defun widget-image-find (image)
558 "Create a graphical button from IMAGE.
559 IMAGE should either already be an image, or be a file name sans
560 extension (xpm, xbm, gif, jpg, or png) located in
561 `widget-image-directory' or otherwise where `find-image' will find it."
562 (cond ((not (and image widget-image-enable (display-graphic-p)))
563 ;; We don't want or can't use images.
564 nil)
565 ((and (consp image)
566 (eq 'image (car image)))
567 ;; Already an image spec. Use it.
568 image)
569 ((stringp image)
570 ;; A string. Look it up in relevant directories.
571 (let* ((load-path (cons widget-image-directory load-path))
572 specs)
573 (dolist (elt widget-image-conversion)
574 (dolist (ext (cdr elt))
575 (push (list :type (car elt) :file (concat image ext)) specs)))
576 (setq specs (nreverse specs))
577 (find-image specs)))
579 ;; Oh well.
580 nil)))
582 (defvar widget-button-pressed-face 'widget-button-pressed-face
583 "Face used for pressed buttons in widgets.
584 This exists as a variable so it can be set locally in certain
585 buffers.")
587 (defun widget-image-insert (widget tag image &optional down inactive)
588 "In WIDGET, insert the text TAG or, if supported, IMAGE.
589 IMAGE should either be an image or an image file name sans extension
590 \(xpm, xbm, gif, jpg, or png) located in `widget-image-directory'.
592 Optional arguments DOWN and INACTIVE are used instead of IMAGE when the
593 button is pressed or inactive, respectively. These are currently ignored."
594 (if (and (display-graphic-p)
595 (setq image (widget-image-find image)))
596 (progn (widget-put widget :suppress-face t)
597 (insert-image image
598 (propertize
599 tag 'mouse-face widget-button-pressed-face)))
600 (insert tag)))
602 ;;; Buttons.
604 (defgroup widget-button nil
605 "The look of various kinds of buttons."
606 :group 'widgets)
608 (defcustom widget-button-prefix ""
609 "String used as prefix for buttons."
610 :type 'string
611 :group 'widget-button)
613 (defcustom widget-button-suffix ""
614 "String used as suffix for buttons."
615 :type 'string
616 :group 'widget-button)
618 ;;; Creating Widgets.
620 ;;;###autoload
621 (defun widget-create (type &rest args)
622 "Create widget of TYPE.
623 The optional ARGS are additional keyword arguments."
624 (let ((widget (apply 'widget-convert type args)))
625 (widget-apply widget :create)
626 widget))
628 (defun widget-create-child-and-convert (parent type &rest args)
629 "As part of the widget PARENT, create a child widget TYPE.
630 The child is converted, using the keyword arguments ARGS."
631 (let ((widget (apply 'widget-convert type args)))
632 (widget-put widget :parent parent)
633 (unless (widget-get widget :indent)
634 (widget-put widget :indent (+ (or (widget-get parent :indent) 0)
635 (or (widget-get widget :extra-offset) 0)
636 (widget-get parent :offset))))
637 (widget-apply widget :create)
638 widget))
640 (defun widget-create-child (parent type)
641 "Create widget of TYPE."
642 (let ((widget (copy-sequence type)))
643 (widget-put widget :parent parent)
644 (unless (widget-get widget :indent)
645 (widget-put widget :indent (+ (or (widget-get parent :indent) 0)
646 (or (widget-get widget :extra-offset) 0)
647 (widget-get parent :offset))))
648 (widget-apply widget :create)
649 widget))
651 (defun widget-create-child-value (parent type value)
652 "Create widget of TYPE with value VALUE."
653 (let ((widget (copy-sequence type)))
654 (widget-put widget :value (widget-apply widget :value-to-internal value))
655 (widget-put widget :parent parent)
656 (unless (widget-get widget :indent)
657 (widget-put widget :indent (+ (or (widget-get parent :indent) 0)
658 (or (widget-get widget :extra-offset) 0)
659 (widget-get parent :offset))))
660 (widget-apply widget :create)
661 widget))
663 ;;;###autoload
664 (defun widget-delete (widget)
665 "Delete WIDGET."
666 (widget-apply widget :delete))
668 (defun widget-convert (type &rest args)
669 "Convert TYPE to a widget without inserting it in the buffer.
670 The optional ARGS are additional keyword arguments."
671 ;; Don't touch the type.
672 (let* ((widget (if (symbolp type)
673 (list type)
674 (copy-sequence type)))
675 (current widget)
676 (keys args))
677 ;; First set the :args keyword.
678 (while (cdr current) ;Look in the type.
679 (if (keywordp (car (cdr current)))
680 (setq current (cdr (cdr current)))
681 (setcdr current (list :args (cdr current)))
682 (setq current nil)))
683 (while args ;Look in the args.
684 (if (keywordp (nth 0 args))
685 (setq args (nthcdr 2 args))
686 (widget-put widget :args args)
687 (setq args nil)))
688 ;; Then Convert the widget.
689 (setq type widget)
690 (while type
691 (let ((convert-widget (plist-get (cdr type) :convert-widget)))
692 (if convert-widget
693 (setq widget (funcall convert-widget widget))))
694 (setq type (get (car type) 'widget-type)))
695 ;; Finally set the keyword args.
696 (while keys
697 (let ((next (nth 0 keys)))
698 (if (keywordp next)
699 (progn
700 (widget-put widget next (nth 1 keys))
701 (setq keys (nthcdr 2 keys)))
702 (setq keys nil))))
703 ;; Convert the :value to internal format.
704 (if (widget-member widget :value)
705 (widget-put widget
706 :value (widget-apply widget
707 :value-to-internal
708 (widget-get widget :value))))
709 ;; Return the newly create widget.
710 widget))
712 (defun widget-insert (&rest args)
713 "Call `insert' with ARGS even if surrounding text is read only."
714 (let ((inhibit-read-only t)
715 (inhibit-modification-hooks t))
716 (apply 'insert args)))
718 (defun widget-convert-text (type from to
719 &optional button-from button-to
720 &rest args)
721 "Return a widget of type TYPE with endpoint FROM TO.
722 Optional ARGS are extra keyword arguments for TYPE.
723 and TO will be used as the widgets end points. If optional arguments
724 BUTTON-FROM and BUTTON-TO are given, these will be used as the widgets
725 button end points.
726 Optional ARGS are extra keyword arguments for TYPE."
727 (let ((widget (apply 'widget-convert type :delete 'widget-leave-text args))
728 (from (copy-marker from))
729 (to (copy-marker to)))
730 (set-marker-insertion-type from t)
731 (set-marker-insertion-type to nil)
732 (widget-put widget :from from)
733 (widget-put widget :to to)
734 (when button-from
735 (widget-specify-button widget button-from button-to))
736 widget))
738 (defun widget-convert-button (type from to &rest args)
739 "Return a widget of type TYPE with endpoint FROM TO.
740 Optional ARGS are extra keyword arguments for TYPE.
741 No text will be inserted to the buffer, instead the text between FROM
742 and TO will be used as the widgets end points, as well as the widgets
743 button end points."
744 (apply 'widget-convert-text type from to from to args))
746 (defun widget-leave-text (widget)
747 "Remove markers and overlays from WIDGET and its children."
748 (let ((button (widget-get widget :button-overlay))
749 (sample (widget-get widget :sample-overlay))
750 (doc (widget-get widget :doc-overlay))
751 (field (widget-get widget :field-overlay)))
752 (set-marker (widget-get widget :from) nil)
753 (set-marker (widget-get widget :to) nil)
754 (when button
755 (delete-overlay button))
756 (when sample
757 (delete-overlay sample))
758 (when doc
759 (delete-overlay doc))
760 (when field
761 (delete-overlay field))
762 (mapc 'widget-leave-text (widget-get widget :children))))
764 ;;; Keymap and Commands.
766 (defvar widget-keymap
767 (let ((map (make-sparse-keymap)))
768 (define-key map "\t" 'widget-forward)
769 (define-key map [(shift tab)] 'widget-backward)
770 (define-key map [backtab] 'widget-backward)
771 (define-key map [down-mouse-2] 'widget-button-click)
772 (define-key map "\C-m" 'widget-button-press)
773 map)
774 "Keymap containing useful binding for buffers containing widgets.
775 Recommended as a parent keymap for modes using widgets.")
777 (defvar widget-global-map global-map
778 "Keymap used for events the widget does not handle themselves.")
779 (make-variable-buffer-local 'widget-global-map)
781 (defvar widget-field-keymap
782 (let ((map (copy-keymap widget-keymap)))
783 (define-key map [menu-bar] nil)
784 (define-key map "\C-k" 'widget-kill-line)
785 (define-key map "\M-\t" 'widget-complete)
786 (define-key map "\C-m" 'widget-field-activate)
787 (define-key map "\C-a" 'widget-beginning-of-line)
788 (define-key map "\C-e" 'widget-end-of-line)
789 (set-keymap-parent map global-map)
790 map)
791 "Keymap used inside an editable field.")
793 (defvar widget-text-keymap
794 (let ((map (copy-keymap widget-keymap)))
795 (define-key map [menu-bar] 'nil)
796 (define-key map "\C-a" 'widget-beginning-of-line)
797 (define-key map "\C-e" 'widget-end-of-line)
798 (set-keymap-parent map global-map)
799 map)
800 "Keymap used inside a text field.")
802 (defun widget-field-activate (pos &optional event)
803 "Invoke the ediable field at point."
804 (interactive "@d")
805 (let ((field (get-char-property pos 'field)))
806 (if field
807 (widget-apply-action field event)
808 (call-interactively
809 (lookup-key widget-global-map (this-command-keys))))))
811 (defface widget-button-pressed-face
812 '((((class color))
813 (:foreground "red"))
815 (:bold t :underline t)))
816 "Face used for pressed buttons."
817 :group 'widget-faces)
819 (defun widget-button-click (event)
820 "Invoke the button that the mouse is pointing at."
821 (interactive "@e")
822 (if (widget-event-point event)
823 (save-excursion
824 (mouse-set-point event)
825 (let* ((pos (widget-event-point event))
826 (button (get-char-property pos 'button)))
827 (if button
828 (let* ((overlay (widget-get button :button-overlay))
829 (face (overlay-get overlay 'face))
830 (mouse-face (overlay-get overlay 'mouse-face)))
831 (unwind-protect
832 (let ((track-mouse t))
833 (save-excursion
834 (when face ; avoid changing around image
835 (overlay-put overlay
836 'face widget-button-pressed-face)
837 (overlay-put overlay
838 'mouse-face widget-button-pressed-face))
839 (unless (widget-apply button :mouse-down-action event)
840 (while (not (widget-button-release-event-p event))
841 (setq event (read-event)
842 pos (widget-event-point event))
843 (if (and pos
844 (eq (get-char-property pos 'button)
845 button))
846 (when face
847 (overlay-put overlay
848 'face
849 widget-button-pressed-face)
850 (overlay-put overlay
851 'mouse-face
852 widget-button-pressed-face))
853 (overlay-put overlay 'face face)
854 (overlay-put overlay 'mouse-face mouse-face))))
855 (when (and pos
856 (eq (get-char-property pos 'button) button))
857 (widget-apply-action button event))))
858 (overlay-put overlay 'face face)
859 (overlay-put overlay 'mouse-face mouse-face)))
860 (let ((up t)
861 command)
862 ;; Find the global command to run, and check whether it
863 ;; is bound to an up event.
864 (if (memq (event-basic-type event) '(mouse-1 down-mouse-1))
865 (cond ((setq command ;down event
866 (lookup-key widget-global-map [down-mouse-1]))
867 (setq up nil))
868 ((setq command ;up event
869 (lookup-key widget-global-map [mouse-1]))))
870 (cond ((setq command ;down event
871 (lookup-key widget-global-map [down-mouse-2]))
872 (setq up nil))
873 ((setq command ;up event
874 (lookup-key widget-global-map [mouse-2])))))
875 (when up
876 ;; Don't execute up events twice.
877 (while (not (widget-button-release-event-p event))
878 (setq event (read-event))))
879 (when command
880 (call-interactively command)))))
881 (unless (pos-visible-in-window-p (widget-event-point event))
882 (mouse-set-point event)
883 (beginning-of-line)
884 (recenter)))
885 (message "You clicked somewhere weird.")))
887 (defun widget-button-press (pos &optional event)
888 "Invoke button at POS."
889 (interactive "@d")
890 (let ((button (get-char-property pos 'button)))
891 (if button
892 (widget-apply-action button event)
893 (let ((command (lookup-key widget-global-map (this-command-keys))))
894 (when (commandp command)
895 (call-interactively command))))))
897 (defun widget-tabable-at (&optional pos)
898 "Return the tabable widget at POS, or nil.
899 POS defaults to the value of (point)."
900 (unless pos
901 (setq pos (point)))
902 (let ((widget (or (get-char-property pos 'button)
903 (get-char-property pos 'field))))
904 (if widget
905 (let ((order (widget-get widget :tab-order)))
906 (if order
907 (if (>= order 0)
908 widget)
909 widget)))))
911 (defvar widget-use-overlay-change t
912 "If non-nil, use overlay change functions to tab around in the buffer.
913 This is much faster, but doesn't work reliably on Emacs 19.34.")
915 (defun widget-move (arg)
916 "Move point to the ARG next field or button.
917 ARG may be negative to move backward."
918 (or (bobp) (> arg 0) (backward-char))
919 (let ((pos (point))
920 (number arg)
921 (old (widget-tabable-at))
922 new)
923 ;; Forward.
924 (while (> arg 0)
925 (cond ((eobp)
926 (goto-char (point-min)))
927 (widget-use-overlay-change
928 (goto-char (next-overlay-change (point))))
930 (forward-char 1)))
931 (and (eq pos (point))
932 (eq arg number)
933 (error "No buttons or fields found"))
934 (let ((new (widget-tabable-at)))
935 (when new
936 (unless (eq new old)
937 (setq arg (1- arg))
938 (setq old new)))))
939 ;; Backward.
940 (while (< arg 0)
941 (cond ((bobp)
942 (goto-char (point-max)))
943 (widget-use-overlay-change
944 (goto-char (previous-overlay-change (point))))
946 (backward-char 1)))
947 (and (eq pos (point))
948 (eq arg number)
949 (error "No buttons or fields found"))
950 (let ((new (widget-tabable-at)))
951 (when new
952 (unless (eq new old)
953 (setq arg (1+ arg))))))
954 (let ((new (widget-tabable-at)))
955 (while (eq (widget-tabable-at) new)
956 (backward-char)))
957 (forward-char))
958 (widget-echo-help (point))
959 (run-hooks 'widget-move-hook))
961 (defun widget-forward (arg)
962 "Move point to the next field or button.
963 With optional ARG, move across that many fields."
964 (interactive "p")
965 (run-hooks 'widget-forward-hook)
966 (widget-move arg))
968 (defun widget-backward (arg)
969 "Move point to the previous field or button.
970 With optional ARG, move across that many fields."
971 (interactive "p")
972 (run-hooks 'widget-backward-hook)
973 (widget-move (- arg)))
975 (defun widget-beginning-of-line ()
976 "Go to beginning of field or beginning of line, whichever is first."
977 (interactive)
978 (let* ((field (widget-field-find (point)))
979 (start (and field (widget-field-start field))))
980 (goto-char (if start
981 (max start (line-beginning-position))
982 (line-beginning-position)))))
984 (defun widget-end-of-line ()
985 "Go to end of field or end of line, whichever is first."
986 (interactive)
987 (let* ((field (widget-field-find (point)))
988 (end (and field (widget-field-end field))))
989 (goto-char (if end
990 (min end (line-end-position))
991 (line-end-position)))))
993 (defun widget-kill-line ()
994 "Kill to end of field or end of line, whichever is first."
995 (interactive)
996 (let* ((field (widget-field-find (point)))
997 (end (and field (widget-field-end field))))
998 (if (and field (> (line-beginning-position 2) end))
999 (kill-region (point) end)
1000 (call-interactively 'kill-line))))
1002 (defcustom widget-complete-field (lookup-key global-map "\M-\t")
1003 "Default function to call for completion inside fields."
1004 :options '(ispell-complete-word complete-tag lisp-complete-symbol)
1005 :type 'function
1006 :group 'widgets)
1008 (defun widget-complete ()
1009 "Complete content of editable field from point.
1010 When not inside a field, move to the previous button or field."
1011 (interactive)
1012 (let ((field (widget-field-find (point))))
1013 (if field
1014 (widget-apply field :complete)
1015 (error "Not in an editable field"))))
1017 ;;; Setting up the buffer.
1019 (defvar widget-field-new nil)
1020 ;; List of all newly created editable fields in the buffer.
1021 (make-variable-buffer-local 'widget-field-new)
1023 (defvar widget-field-list nil)
1024 ;; List of all editable fields in the buffer.
1025 (make-variable-buffer-local 'widget-field-list)
1027 (defun widget-setup ()
1028 "Setup current buffer so editing string widgets works."
1029 (let ((inhibit-read-only t)
1030 (inhibit-modification-hooks t)
1031 field)
1032 (while widget-field-new
1033 (setq field (car widget-field-new)
1034 widget-field-new (cdr widget-field-new)
1035 widget-field-list (cons field widget-field-list))
1036 (let ((from (car (widget-get field :field-overlay)))
1037 (to (cdr (widget-get field :field-overlay))))
1038 (widget-specify-field field
1039 (marker-position from) (marker-position to))
1040 (set-marker from nil)
1041 (set-marker to nil))))
1042 (widget-clear-undo)
1043 (widget-add-change))
1045 (defvar widget-field-last nil)
1046 ;; Last field containing point.
1047 (make-variable-buffer-local 'widget-field-last)
1049 (defvar widget-field-was nil)
1050 ;; The widget data before the change.
1051 (make-variable-buffer-local 'widget-field-was)
1053 (defun widget-field-buffer (widget)
1054 "Return the start of WIDGET's editing field."
1055 (let ((overlay (widget-get widget :field-overlay)))
1056 (and overlay (overlay-buffer overlay))))
1058 (defun widget-field-start (widget)
1059 "Return the start of WIDGET's editing field."
1060 (let ((overlay (widget-get widget :field-overlay)))
1061 (and overlay (overlay-start overlay))))
1063 (defun widget-field-end (widget)
1064 "Return the end of WIDGET's editing field."
1065 (let ((overlay (widget-get widget :field-overlay)))
1066 ;; Don't subtract one if local-map works at the end of the overlay.
1067 (and overlay (if (or widget-field-add-space
1068 (null (widget-get widget :size)))
1069 (1- (overlay-end overlay))
1070 (overlay-end overlay)))))
1072 (defun widget-field-find (pos)
1073 "Return the field at POS.
1074 Unlike (get-char-property POS 'field) this, works with empty fields too."
1075 (let ((fields widget-field-list)
1076 field found)
1077 (while fields
1078 (setq field (car fields)
1079 fields (cdr fields))
1080 (when (and (<= (widget-field-start field) pos)
1081 (<= pos (widget-field-end field)))
1082 (when found
1083 (error "Overlapping fields"))
1084 (setq found field)))
1085 found))
1087 (defun widget-before-change (from to)
1088 ;; This is how, for example, a variable changes its state to `modified'.
1089 ;; when it is being edited.
1090 (unless inhibit-read-only
1091 (let ((from-field (widget-field-find from))
1092 (to-field (widget-field-find to)))
1093 (cond ((not (eq from-field to-field))
1094 (add-hook 'post-command-hook 'widget-add-change nil t)
1095 (signal 'text-read-only
1096 '("Change should be restricted to a single field")))
1097 ((null from-field)
1098 (add-hook 'post-command-hook 'widget-add-change nil t)
1099 (signal 'text-read-only
1100 '("Attempt to change text outside editable field")))
1101 (widget-field-use-before-change
1102 (widget-apply from-field :notify from-field))))))
1104 (defun widget-add-change ()
1105 (make-local-hook 'post-command-hook)
1106 (remove-hook 'post-command-hook 'widget-add-change t)
1107 (make-local-hook 'before-change-functions)
1108 (add-hook 'before-change-functions 'widget-before-change nil t)
1109 (make-local-hook 'after-change-functions)
1110 (add-hook 'after-change-functions 'widget-after-change nil t))
1112 (defun widget-after-change (from to old)
1113 "Adjust field size and text properties."
1114 (let ((field (widget-field-find from))
1115 (other (widget-field-find to)))
1116 (when field
1117 (unless (eq field other)
1118 (error "Change in different fields"))
1119 (let ((size (widget-get field :size)))
1120 (when size
1121 (let ((begin (widget-field-start field))
1122 (end (widget-field-end field)))
1123 (cond ((< (- end begin) size)
1124 ;; Field too small.
1125 (save-excursion
1126 (goto-char end)
1127 (insert-char ?\ (- (+ begin size) end))))
1128 ((> (- end begin) size)
1129 ;; Field too large and
1130 (if (or (< (point) (+ begin size))
1131 (> (point) end))
1132 ;; Point is outside extra space.
1133 (setq begin (+ begin size))
1134 ;; Point is within the extra space.
1135 (setq begin (point)))
1136 (save-excursion
1137 (goto-char end)
1138 (while (and (eq (preceding-char) ?\ )
1139 (> (point) begin))
1140 (delete-backward-char 1)))))))
1141 (widget-specify-secret field))
1142 (widget-apply field :notify field))))
1144 ;;; Widget Functions
1146 ;; These functions are used in the definition of multiple widgets.
1148 (defun widget-parent-action (widget &optional event)
1149 "Tell :parent of WIDGET to handle the :action.
1150 Optional EVENT is the event that triggered the action."
1151 (widget-apply (widget-get widget :parent) :action event))
1153 (defun widget-children-value-delete (widget)
1154 "Delete all :children and :buttons in WIDGET."
1155 (mapc 'widget-delete (widget-get widget :children))
1156 (widget-put widget :children nil)
1157 (mapc 'widget-delete (widget-get widget :buttons))
1158 (widget-put widget :buttons nil))
1160 (defun widget-children-validate (widget)
1161 "All the :children must be valid."
1162 (let ((children (widget-get widget :children))
1163 child found)
1164 (while (and children (not found))
1165 (setq child (car children)
1166 children (cdr children)
1167 found (widget-apply child :validate)))
1168 found))
1170 ;; Made defsubst to speed up face editor creation.
1171 (defsubst widget-types-convert-widget (widget)
1172 "Convert :args as widget types in WIDGET."
1173 (widget-put widget :args (mapcar 'widget-convert (widget-get widget :args)))
1174 widget)
1176 (defun widget-value-convert-widget (widget)
1177 "Initialize :value from :args in WIDGET."
1178 (let ((args (widget-get widget :args)))
1179 (when args
1180 (widget-put widget :value (car args))
1181 ;; Don't convert :value here, as this is done in `widget-convert'.
1182 ;; (widget-put widget :value (widget-apply widget
1183 ;; :value-to-internal (car args)))
1184 (widget-put widget :args nil)))
1185 widget)
1187 (defun widget-value-value-get (widget)
1188 "Return the :value property of WIDGET."
1189 (widget-get widget :value))
1191 ;;; The `default' Widget.
1193 (define-widget 'default nil
1194 "Basic widget other widgets are derived from."
1195 :value-to-internal (lambda (widget value) value)
1196 :value-to-external (lambda (widget value) value)
1197 :button-prefix 'widget-button-prefix
1198 :button-suffix 'widget-button-suffix
1199 :complete 'widget-default-complete
1200 :create 'widget-default-create
1201 :indent nil
1202 :offset 0
1203 :format-handler 'widget-default-format-handler
1204 :button-face-get 'widget-default-button-face-get
1205 :sample-face-get 'widget-default-sample-face-get
1206 :delete 'widget-default-delete
1207 :value-set 'widget-default-value-set
1208 :value-inline 'widget-default-value-inline
1209 :default-get 'widget-default-default-get
1210 :menu-tag-get 'widget-default-menu-tag-get
1211 :validate #'ignore
1212 :active 'widget-default-active
1213 :activate 'widget-specify-active
1214 :deactivate 'widget-default-deactivate
1215 :mouse-down-action #'ignore
1216 :action 'widget-default-action
1217 :notify 'widget-default-notify
1218 :prompt-value 'widget-default-prompt-value)
1220 (defun widget-default-complete (widget)
1221 "Call the value of the :complete-function property of WIDGET.
1222 If that does not exists, call the value of `widget-complete-field'."
1223 (call-interactively (or (widget-get widget :complete-function)
1224 widget-complete-field)))
1226 (defun widget-default-create (widget)
1227 "Create WIDGET at point in the current buffer."
1228 (widget-specify-insert
1229 (let ((from (point))
1230 button-begin button-end
1231 sample-begin sample-end
1232 doc-begin doc-end
1233 value-pos)
1234 (insert (widget-get widget :format))
1235 (goto-char from)
1236 ;; Parse escapes in format.
1237 (while (re-search-forward "%\\(.\\)" nil t)
1238 (let ((escape (char-after (match-beginning 1))))
1239 (delete-backward-char 2)
1240 (cond ((eq escape ?%)
1241 (insert ?%))
1242 ((eq escape ?\[)
1243 (setq button-begin (point))
1244 (insert (widget-get-indirect widget :button-prefix)))
1245 ((eq escape ?\])
1246 (insert (widget-get-indirect widget :button-suffix))
1247 (setq button-end (point)))
1248 ((eq escape ?\{)
1249 (setq sample-begin (point)))
1250 ((eq escape ?\})
1251 (setq sample-end (point)))
1252 ((eq escape ?n)
1253 (when (widget-get widget :indent)
1254 (insert ?\n)
1255 (insert-char ? (widget-get widget :indent))))
1256 ((eq escape ?t)
1257 (let ((image (widget-get widget :tag-glyph))
1258 (tag (widget-get widget :tag)))
1259 (cond (image
1260 (widget-image-insert widget (or tag "image") image))
1261 (tag
1262 (insert tag))
1264 (princ (widget-get widget :value)
1265 (current-buffer))))))
1266 ((eq escape ?d)
1267 (let ((doc (widget-get widget :doc)))
1268 (when doc
1269 (setq doc-begin (point))
1270 (insert doc)
1271 (while (eq (preceding-char) ?\n)
1272 (delete-backward-char 1))
1273 (insert ?\n)
1274 (setq doc-end (point)))))
1275 ((eq escape ?v)
1276 (if (and button-begin (not button-end))
1277 (widget-apply widget :value-create)
1278 (setq value-pos (point))))
1280 (widget-apply widget :format-handler escape)))))
1281 ;; Specify button, sample, and doc, and insert value.
1282 (and button-begin button-end
1283 (widget-specify-button widget button-begin button-end))
1284 (and sample-begin sample-end
1285 (widget-specify-sample widget sample-begin sample-end))
1286 (and doc-begin doc-end
1287 (widget-specify-doc widget doc-begin doc-end))
1288 (when value-pos
1289 (goto-char value-pos)
1290 (widget-apply widget :value-create)))
1291 (let ((from (point-min-marker))
1292 (to (point-max-marker)))
1293 (set-marker-insertion-type from t)
1294 (set-marker-insertion-type to nil)
1295 (widget-put widget :from from)
1296 (widget-put widget :to to)))
1297 (widget-clear-undo))
1299 (defun widget-default-format-handler (widget escape)
1300 ;; We recognize the %h escape by default.
1301 (let* ((buttons (widget-get widget :buttons)))
1302 (cond ((eq escape ?h)
1303 (let* ((doc-property (widget-get widget :documentation-property))
1304 (doc-try (cond ((widget-get widget :doc))
1305 ((functionp doc-property)
1306 (funcall doc-property
1307 (widget-get widget :value)))
1308 ((symbolp doc-property)
1309 (documentation-property
1310 (widget-get widget :value)
1311 doc-property))))
1312 (doc-text (and (stringp doc-try)
1313 (> (length doc-try) 1)
1314 doc-try))
1315 (doc-indent (widget-get widget :documentation-indent)))
1316 (when doc-text
1317 (and (eq (preceding-char) ?\n)
1318 (widget-get widget :indent)
1319 (insert-char ? (widget-get widget :indent)))
1320 ;; The `*' in the beginning is redundant.
1321 (when (eq (aref doc-text 0) ?*)
1322 (setq doc-text (substring doc-text 1)))
1323 ;; Get rid of trailing newlines.
1324 (when (string-match "\n+\\'" doc-text)
1325 (setq doc-text (substring doc-text 0 (match-beginning 0))))
1326 (push (widget-create-child-and-convert
1327 widget 'documentation-string
1328 :indent (cond ((numberp doc-indent )
1329 doc-indent)
1330 ((null doc-indent)
1331 nil)
1332 (t 0))
1333 doc-text)
1334 buttons))))
1336 (error "Unknown escape `%c'" escape)))
1337 (widget-put widget :buttons buttons)))
1339 (defun widget-default-button-face-get (widget)
1340 ;; Use :button-face or widget-button-face
1341 (or (widget-get widget :button-face)
1342 (let ((parent (widget-get widget :parent)))
1343 (if parent
1344 (widget-apply parent :button-face-get)
1345 widget-button-face))))
1347 (defun widget-default-sample-face-get (widget)
1348 ;; Use :sample-face.
1349 (widget-get widget :sample-face))
1351 (defun widget-default-delete (widget)
1352 "Remove widget from the buffer."
1353 (let ((from (widget-get widget :from))
1354 (to (widget-get widget :to))
1355 (inactive-overlay (widget-get widget :inactive))
1356 (button-overlay (widget-get widget :button-overlay))
1357 (sample-overlay (widget-get widget :sample-overlay))
1358 (doc-overlay (widget-get widget :doc-overlay))
1359 (inhibit-modification-hooks t)
1360 (inhibit-read-only t))
1361 (widget-apply widget :value-delete)
1362 (when inactive-overlay
1363 (delete-overlay inactive-overlay))
1364 (when button-overlay
1365 (delete-overlay button-overlay))
1366 (when sample-overlay
1367 (delete-overlay sample-overlay))
1368 (when doc-overlay
1369 (delete-overlay doc-overlay))
1370 (when (< from to)
1371 ;; Kludge: this doesn't need to be true for empty formats.
1372 (delete-region from to))
1373 (set-marker from nil)
1374 (set-marker to nil))
1375 (widget-clear-undo))
1377 (defun widget-default-value-set (widget value)
1378 "Recreate widget with new value."
1379 (let* ((old-pos (point))
1380 (from (copy-marker (widget-get widget :from)))
1381 (to (copy-marker (widget-get widget :to)))
1382 (offset (if (and (<= from old-pos) (<= old-pos to))
1383 (if (>= old-pos (1- to))
1384 (- old-pos to 1)
1385 (- old-pos from)))))
1386 ;;??? Bug: this ought to insert the new value before deleting the old one,
1387 ;; so that markers on either side of the value automatically
1388 ;; stay on the same side. -- rms.
1389 (save-excursion
1390 (goto-char (widget-get widget :from))
1391 (widget-apply widget :delete)
1392 (widget-put widget :value value)
1393 (widget-apply widget :create))
1394 (if offset
1395 (if (< offset 0)
1396 (goto-char (+ (widget-get widget :to) offset 1))
1397 (goto-char (min (+ from offset) (1- (widget-get widget :to))))))))
1399 (defun widget-default-value-inline (widget)
1400 "Wrap value in a list unless it is inline."
1401 (if (widget-get widget :inline)
1402 (widget-value widget)
1403 (list (widget-value widget))))
1405 (defun widget-default-default-get (widget)
1406 "Get `:value'."
1407 (widget-get widget :value))
1409 (defun widget-default-menu-tag-get (widget)
1410 "Use tag or value for menus."
1411 (or (widget-get widget :menu-tag)
1412 (widget-get widget :tag)
1413 (widget-princ-to-string (widget-get widget :value))))
1415 (defun widget-default-active (widget)
1416 "Return t iff this widget active (user modifiable)."
1417 (or (widget-get widget :always-active)
1418 (and (not (widget-get widget :inactive))
1419 (let ((parent (widget-get widget :parent)))
1420 (or (null parent)
1421 (widget-apply parent :active))))))
1423 (defun widget-default-deactivate (widget)
1424 "Make WIDGET inactive for user modifications."
1425 (widget-specify-inactive widget
1426 (widget-get widget :from)
1427 (widget-get widget :to)))
1429 (defun widget-default-action (widget &optional event)
1430 "Notify the parent when a widget changes."
1431 (let ((parent (widget-get widget :parent)))
1432 (when parent
1433 (widget-apply parent :notify widget event))))
1435 (defun widget-default-notify (widget child &optional event)
1436 "Pass notification to parent."
1437 (widget-default-action widget event))
1439 (defun widget-default-prompt-value (widget prompt value unbound)
1440 "Read an arbitrary value. Stolen from `set-variable'."
1441 ;; (let ((initial (if unbound
1442 ;; nil
1443 ;; It would be nice if we could do a `(cons val 1)' here.
1444 ;; (prin1-to-string (custom-quote value))))))
1445 (eval-minibuffer prompt))
1447 ;;; The `item' Widget.
1449 (define-widget 'item 'default
1450 "Constant items for inclusion in other widgets."
1451 :convert-widget 'widget-value-convert-widget
1452 :value-create 'widget-item-value-create
1453 :value-delete 'ignore
1454 :value-get 'widget-value-value-get
1455 :match 'widget-item-match
1456 :match-inline 'widget-item-match-inline
1457 :action 'widget-item-action
1458 :format "%t\n")
1460 (defun widget-item-value-create (widget)
1461 "Insert the printed representation of the value."
1462 (princ (widget-get widget :value) (current-buffer)))
1464 (defun widget-item-match (widget value)
1465 ;; Match if the value is the same.
1466 (equal (widget-get widget :value) value))
1468 (defun widget-item-match-inline (widget values)
1469 ;; Match if the value is the same.
1470 (let ((value (widget-get widget :value)))
1471 (and (listp value)
1472 (<= (length value) (length values))
1473 (let ((head (widget-sublist values 0 (length value))))
1474 (and (equal head value)
1475 (cons head (widget-sublist values (length value))))))))
1477 (defun widget-sublist (list start &optional end)
1478 "Return the sublist of LIST from START to END.
1479 If END is omitted, it defaults to the length of LIST."
1480 (if (> start 0) (setq list (nthcdr start list)))
1481 (if end
1482 (unless (<= end start)
1483 (setq list (copy-sequence list))
1484 (setcdr (nthcdr (- end start 1) list) nil)
1485 list)
1486 (copy-sequence list)))
1488 (defun widget-item-action (widget &optional event)
1489 ;; Just notify itself.
1490 (widget-apply widget :notify widget event))
1492 ;;; The `push-button' Widget.
1494 ;; (defcustom widget-push-button-gui t
1495 ;; "If non nil, use GUI push buttons when available."
1496 ;; :group 'widgets
1497 ;; :type 'boolean)
1499 ;; Cache already created GUI objects.
1500 ;; (defvar widget-push-button-cache nil)
1502 (defcustom widget-push-button-prefix "["
1503 "String used as prefix for buttons."
1504 :type 'string
1505 :group 'widget-button)
1507 (defcustom widget-push-button-suffix "]"
1508 "String used as suffix for buttons."
1509 :type 'string
1510 :group 'widget-button)
1512 (define-widget 'push-button 'item
1513 "A pushable button."
1514 :button-prefix ""
1515 :button-suffix ""
1516 :value-create 'widget-push-button-value-create
1517 :format "%[%v%]")
1519 (defun widget-push-button-value-create (widget)
1520 "Insert text representing the `on' and `off' states."
1521 (let* ((tag (or (widget-get widget :tag)
1522 (widget-get widget :value)))
1523 (tag-glyph (widget-get widget :tag-glyph))
1524 (text (concat widget-push-button-prefix
1525 tag widget-push-button-suffix)))
1526 (if tag-glyph
1527 (widget-image-insert widget text tag-glyph)
1528 (insert text))))
1530 ;; (defun widget-gui-action (widget)
1531 ;; "Apply :action for WIDGET."
1532 ;; (widget-apply-action widget (this-command-keys)))
1534 ;;; The `link' Widget.
1536 (defcustom widget-link-prefix "["
1537 "String used as prefix for links."
1538 :type 'string
1539 :group 'widget-button)
1541 (defcustom widget-link-suffix "]"
1542 "String used as suffix for links."
1543 :type 'string
1544 :group 'widget-button)
1546 (define-widget 'link 'item
1547 "An embedded link."
1548 :button-prefix 'widget-link-prefix
1549 :button-suffix 'widget-link-suffix
1550 :help-echo "Follow the link."
1551 :format "%[%t%]")
1553 ;;; The `info-link' Widget.
1555 (define-widget 'info-link 'link
1556 "A link to an info file."
1557 :action 'widget-info-link-action)
1559 (defun widget-info-link-action (widget &optional event)
1560 "Open the info node specified by WIDGET."
1561 (Info-goto-node (widget-value widget)))
1563 ;;; The `url-link' Widget.
1565 (define-widget 'url-link 'link
1566 "A link to an www page."
1567 :action 'widget-url-link-action)
1569 (defun widget-url-link-action (widget &optional event)
1570 "Open the url specified by WIDGET."
1571 (browse-url (widget-value widget)))
1573 ;;; The `function-link' Widget.
1575 (define-widget 'function-link 'link
1576 "A link to an Emacs function."
1577 :action 'widget-function-link-action)
1579 (defun widget-function-link-action (widget &optional event)
1580 "Show the function specified by WIDGET."
1581 (describe-function (widget-value widget)))
1583 ;;; The `variable-link' Widget.
1585 (define-widget 'variable-link 'link
1586 "A link to an Emacs variable."
1587 :action 'widget-variable-link-action)
1589 (defun widget-variable-link-action (widget &optional event)
1590 "Show the variable specified by WIDGET."
1591 (describe-variable (widget-value widget)))
1593 ;;; The `file-link' Widget.
1595 (define-widget 'file-link 'link
1596 "A link to a file."
1597 :action 'widget-file-link-action)
1599 (defun widget-file-link-action (widget &optional event)
1600 "Find the file specified by WIDGET."
1601 (find-file (widget-value widget)))
1603 ;;; The `emacs-library-link' Widget.
1605 (define-widget 'emacs-library-link 'link
1606 "A link to an Emacs Lisp library file."
1607 :action 'widget-emacs-library-link-action)
1609 (defun widget-emacs-library-link-action (widget &optional event)
1610 "Find the Emacs Library file specified by WIDGET."
1611 (find-file (locate-library (widget-value widget))))
1613 ;;; The `emacs-commentary-link' Widget.
1615 (define-widget 'emacs-commentary-link 'link
1616 "A link to Commentary in an Emacs Lisp library file."
1617 :action 'widget-emacs-commentary-link-action)
1619 (defun widget-emacs-commentary-link-action (widget &optional event)
1620 "Find the Commentary section of the Emacs file specified by WIDGET."
1621 (finder-commentary (widget-value widget)))
1623 ;;; The `editable-field' Widget.
1625 (define-widget 'editable-field 'default
1626 "An editable text field."
1627 :convert-widget 'widget-value-convert-widget
1628 :keymap widget-field-keymap
1629 :format "%v"
1630 :help-echo "M-TAB: complete field; RET: enter value"
1631 :value ""
1632 :prompt-internal 'widget-field-prompt-internal
1633 :prompt-history 'widget-field-history
1634 :prompt-value 'widget-field-prompt-value
1635 :action 'widget-field-action
1636 :validate 'widget-field-validate
1637 :valid-regexp ""
1638 :error "Field's value doesn't match allowed forms"
1639 :value-create 'widget-field-value-create
1640 :value-delete 'widget-field-value-delete
1641 :value-get 'widget-field-value-get
1642 :match 'widget-field-match)
1644 (defvar widget-field-history nil
1645 "History of field minibuffer edits.")
1647 (defun widget-field-prompt-internal (widget prompt initial history)
1648 "Read string for WIDGET promptinhg with PROMPT.
1649 INITIAL is the initial input and HISTORY is a symbol containing
1650 the earlier input."
1651 (read-string prompt initial history))
1653 (defun widget-field-prompt-value (widget prompt value unbound)
1654 "Prompt for a string."
1655 (widget-apply widget
1656 :value-to-external
1657 (widget-apply widget
1658 :prompt-internal prompt
1659 (unless unbound
1660 (cons (widget-apply widget
1661 :value-to-internal value)
1663 (widget-get widget :prompt-history))))
1665 (defvar widget-edit-functions nil)
1667 (defun widget-field-action (widget &optional event)
1668 "Move to next field."
1669 (widget-forward 1)
1670 (run-hook-with-args 'widget-edit-functions widget))
1672 (defun widget-field-validate (widget)
1673 "Valid if the content matches `:valid-regexp'."
1674 (unless (string-match (widget-get widget :valid-regexp)
1675 (widget-apply widget :value-get))
1676 widget))
1678 (defun widget-field-value-create (widget)
1679 "Create an editable text field."
1680 (let ((size (widget-get widget :size))
1681 (value (widget-get widget :value))
1682 (from (point))
1683 ;; This is changed to a real overlay in `widget-setup'. We
1684 ;; need the end points to behave differently until
1685 ;; `widget-setup' is called.
1686 (overlay (cons (make-marker) (make-marker))))
1687 (widget-put widget :field-overlay overlay)
1688 (insert value)
1689 (and size
1690 (< (length value) size)
1691 (insert-char ?\ (- size (length value))))
1692 (unless (memq widget widget-field-list)
1693 (setq widget-field-new (cons widget widget-field-new)))
1694 (move-marker (cdr overlay) (point))
1695 (set-marker-insertion-type (cdr overlay) nil)
1696 (when (null size)
1697 (insert ?\n))
1698 (move-marker (car overlay) from)
1699 (set-marker-insertion-type (car overlay) t)))
1701 (defun widget-field-value-delete (widget)
1702 "Remove the widget from the list of active editing fields."
1703 (setq widget-field-list (delq widget widget-field-list))
1704 ;; These are nil if the :format string doesn't contain `%v'.
1705 (let ((overlay (widget-get widget :field-overlay)))
1706 (when overlay
1707 (delete-overlay overlay))))
1709 (defun widget-field-value-get (widget)
1710 "Return current text in editing field."
1711 (let ((from (widget-field-start widget))
1712 (to (widget-field-end widget))
1713 (buffer (widget-field-buffer widget))
1714 (size (widget-get widget :size))
1715 (secret (widget-get widget :secret))
1716 (old (current-buffer)))
1717 (if (and from to)
1718 (progn
1719 (set-buffer buffer)
1720 (while (and size
1721 (not (zerop size))
1722 (> to from)
1723 (eq (char-after (1- to)) ?\ ))
1724 (setq to (1- to)))
1725 (let ((result (buffer-substring-no-properties from to)))
1726 (when secret
1727 (let ((index 0))
1728 (while (< (+ from index) to)
1729 (aset result index
1730 (get-char-property (+ from index) 'secret))
1731 (setq index (1+ index)))))
1732 (set-buffer old)
1733 result))
1734 (widget-get widget :value))))
1736 (defun widget-field-match (widget value)
1737 ;; Match any string.
1738 (stringp value))
1740 ;;; The `text' Widget.
1742 (define-widget 'text 'editable-field
1743 :keymap widget-text-keymap
1744 "A multiline text area.")
1746 ;;; The `menu-choice' Widget.
1748 (define-widget 'menu-choice 'default
1749 "A menu of options."
1750 :convert-widget 'widget-types-convert-widget
1751 :format "%[%t%]: %v"
1752 :case-fold t
1753 :tag "choice"
1754 :void '(item :format "invalid (%t)\n")
1755 :value-create 'widget-choice-value-create
1756 :value-delete 'widget-children-value-delete
1757 :value-get 'widget-choice-value-get
1758 :value-inline 'widget-choice-value-inline
1759 :default-get 'widget-choice-default-get
1760 :mouse-down-action 'widget-choice-mouse-down-action
1761 :action 'widget-choice-action
1762 :error "Make a choice"
1763 :validate 'widget-choice-validate
1764 :match 'widget-choice-match
1765 :match-inline 'widget-choice-match-inline)
1767 (defun widget-choice-value-create (widget)
1768 "Insert the first choice that matches the value."
1769 (let ((value (widget-get widget :value))
1770 (args (widget-get widget :args))
1771 (explicit (widget-get widget :explicit-choice))
1772 current)
1773 (if (and explicit (equal value (widget-get widget :explicit-choice-value)))
1774 (progn
1775 ;; If the user specified the choice for this value,
1776 ;; respect that choice as long as the value is the same.
1777 (widget-put widget :children (list (widget-create-child-value
1778 widget explicit value)))
1779 (widget-put widget :choice explicit))
1780 (while args
1781 (setq current (car args)
1782 args (cdr args))
1783 (when (widget-apply current :match value)
1784 (widget-put widget :children (list (widget-create-child-value
1785 widget current value)))
1786 (widget-put widget :choice current)
1787 (setq args nil
1788 current nil)))
1789 (when current
1790 (let ((void (widget-get widget :void)))
1791 (widget-put widget :children (list (widget-create-child-and-convert
1792 widget void :value value)))
1793 (widget-put widget :choice void))))))
1795 (defun widget-choice-value-get (widget)
1796 ;; Get value of the child widget.
1797 (widget-value (car (widget-get widget :children))))
1799 (defun widget-choice-value-inline (widget)
1800 ;; Get value of the child widget.
1801 (widget-apply (car (widget-get widget :children)) :value-inline))
1803 (defun widget-choice-default-get (widget)
1804 ;; Get default for the first choice.
1805 (widget-default-get (car (widget-get widget :args))))
1807 (defcustom widget-choice-toggle nil
1808 "If non-nil, a binary choice will just toggle between the values.
1809 Otherwise, the user will explicitly have to choose between the values
1810 when he invoked the menu."
1811 :type 'boolean
1812 :group 'widgets)
1814 (defun widget-choice-mouse-down-action (widget &optional event)
1815 ;; Return non-nil if we need a menu.
1816 (let ((args (widget-get widget :args))
1817 (old (widget-get widget :choice)))
1818 (cond ((not (display-popup-menus-p))
1819 ;; No place to pop up a menu.
1820 nil)
1821 ((< (length args) 2)
1822 ;; Empty or singleton list, just return the value.
1823 nil)
1824 ((> (length args) widget-menu-max-size)
1825 ;; Too long, prompt.
1826 nil)
1827 ((> (length args) 2)
1828 ;; Reasonable sized list, use menu.
1830 ((and widget-choice-toggle (memq old args))
1831 ;; We toggle.
1832 nil)
1834 ;; Ask which of the two.
1835 t))))
1837 (defun widget-choice-action (widget &optional event)
1838 ;; Make a choice.
1839 (let ((args (widget-get widget :args))
1840 (old (widget-get widget :choice))
1841 (tag (widget-apply widget :menu-tag-get))
1842 (completion-ignore-case (widget-get widget :case-fold))
1843 this-explicit
1844 current choices)
1845 ;; Remember old value.
1846 (if (and old (not (widget-apply widget :validate)))
1847 (let* ((external (widget-value widget))
1848 (internal (widget-apply old :value-to-internal external)))
1849 (widget-put old :value internal)))
1850 ;; Find new choice.
1851 (setq current
1852 (cond ((= (length args) 0)
1853 nil)
1854 ((= (length args) 1)
1855 (nth 0 args))
1856 ((and widget-choice-toggle
1857 (= (length args) 2)
1858 (memq old args))
1859 (if (eq old (nth 0 args))
1860 (nth 1 args)
1861 (nth 0 args)))
1863 (while args
1864 (setq current (car args)
1865 args (cdr args))
1866 (setq choices
1867 (cons (cons (widget-apply current :menu-tag-get)
1868 current)
1869 choices)))
1870 (setq this-explicit t)
1871 (widget-choose tag (reverse choices) event))))
1872 (when current
1873 ;; If this was an explicit user choice,
1874 ;; record the choice, and the record the value it was made for.
1875 ;; widget-choice-value-create will respect this choice,
1876 ;; as long as the value is the same.
1877 (when this-explicit
1878 (widget-put widget :explicit-choice current)
1879 (widget-put widget :explicit-choice-value (widget-get widget :value)))
1880 (widget-value-set
1881 widget (widget-apply current
1882 :value-to-external (widget-default-get current)))
1883 (widget-setup)
1884 (widget-apply widget :notify widget event)))
1885 (run-hook-with-args 'widget-edit-functions widget))
1887 (defun widget-choice-validate (widget)
1888 ;; Valid if we have made a valid choice.
1889 (if (eq (widget-get widget :void) (widget-get widget :choice))
1890 widget
1891 (widget-apply (car (widget-get widget :children)) :validate)))
1893 (defun widget-choice-match (widget value)
1894 ;; Matches if one of the choices matches.
1895 (let ((args (widget-get widget :args))
1896 current found)
1897 (while (and args (not found))
1898 (setq current (car args)
1899 args (cdr args)
1900 found (widget-apply current :match value)))
1901 found))
1903 (defun widget-choice-match-inline (widget values)
1904 ;; Matches if one of the choices matches.
1905 (let ((args (widget-get widget :args))
1906 current found)
1907 (while (and args (null found))
1908 (setq current (car args)
1909 args (cdr args)
1910 found (widget-match-inline current values)))
1911 found))
1913 ;;; The `toggle' Widget.
1915 (define-widget 'toggle 'item
1916 "Toggle between two states."
1917 :format "%[%v%]\n"
1918 :value-create 'widget-toggle-value-create
1919 :action 'widget-toggle-action
1920 :match (lambda (widget value) t)
1921 :on "on"
1922 :off "off")
1924 (defun widget-toggle-value-create (widget)
1925 "Insert text representing the `on' and `off' states."
1926 (if (widget-value widget)
1927 (widget-image-insert widget
1928 (widget-get widget :on)
1929 (widget-get widget :on-glyph))
1930 (widget-image-insert widget
1931 (widget-get widget :off)
1932 (widget-get widget :off-glyph))))
1934 (defun widget-toggle-action (widget &optional event)
1935 ;; Toggle value.
1936 (widget-value-set widget (not (widget-value widget)))
1937 (widget-apply widget :notify widget event)
1938 (run-hook-with-args 'widget-edit-functions widget))
1940 ;;; The `checkbox' Widget.
1942 (define-widget 'checkbox 'toggle
1943 "A checkbox toggle."
1944 :button-suffix ""
1945 :button-prefix ""
1946 :format "%[%v%]"
1947 :on "[X]"
1948 ;; We could probably do the same job as the images using single
1949 ;; space characters in a boxed face with a stretch specification to
1950 ;; make them square.
1951 :on-glyph (create-image (make-bool-vector 49 1)
1952 'xbm t :width 7 :height 7
1953 :foreground "grey75" ; like default mode line
1954 :relief -3 :ascent 'center)
1955 :off "[ ]"
1956 :off-glyph (create-image (make-bool-vector 49 1)
1957 'xbm t :width 7 :height 7
1958 :foreground "grey75"
1959 :relief 3 :ascent 'center)
1960 :help-echo "Toggle this item."
1961 :action 'widget-checkbox-action)
1963 (defun widget-checkbox-action (widget &optional event)
1964 "Toggle checkbox, notify parent, and set active state of sibling."
1965 (widget-toggle-action widget event)
1966 (let ((sibling (widget-get-sibling widget)))
1967 (when sibling
1968 (if (widget-value widget)
1969 (widget-apply sibling :activate)
1970 (widget-apply sibling :deactivate)))))
1972 ;;; The `checklist' Widget.
1974 (define-widget 'checklist 'default
1975 "A multiple choice widget."
1976 :convert-widget 'widget-types-convert-widget
1977 :format "%v"
1978 :offset 4
1979 :entry-format "%b %v"
1980 :menu-tag "checklist"
1981 :greedy nil
1982 :value-create 'widget-checklist-value-create
1983 :value-delete 'widget-children-value-delete
1984 :value-get 'widget-checklist-value-get
1985 :validate 'widget-checklist-validate
1986 :match 'widget-checklist-match
1987 :match-inline 'widget-checklist-match-inline)
1989 (defun widget-checklist-value-create (widget)
1990 ;; Insert all values
1991 (let ((alist (widget-checklist-match-find widget (widget-get widget :value)))
1992 (args (widget-get widget :args)))
1993 (while args
1994 (widget-checklist-add-item widget (car args) (assq (car args) alist))
1995 (setq args (cdr args)))
1996 (widget-put widget :children (nreverse (widget-get widget :children)))))
1998 (defun widget-checklist-add-item (widget type chosen)
1999 "Create checklist item in WIDGET of type TYPE.
2000 If the item is checked, CHOSEN is a cons whose cdr is the value."
2001 (and (eq (preceding-char) ?\n)
2002 (widget-get widget :indent)
2003 (insert-char ? (widget-get widget :indent)))
2004 (widget-specify-insert
2005 (let* ((children (widget-get widget :children))
2006 (buttons (widget-get widget :buttons))
2007 (button-args (or (widget-get type :sibling-args)
2008 (widget-get widget :button-args)))
2009 (from (point))
2010 child button)
2011 (insert (widget-get widget :entry-format))
2012 (goto-char from)
2013 ;; Parse % escapes in format.
2014 (while (re-search-forward "%\\([bv%]\\)" nil t)
2015 (let ((escape (char-after (match-beginning 1))))
2016 (delete-backward-char 2)
2017 (cond ((eq escape ?%)
2018 (insert ?%))
2019 ((eq escape ?b)
2020 (setq button (apply 'widget-create-child-and-convert
2021 widget 'checkbox
2022 :value (not (null chosen))
2023 button-args)))
2024 ((eq escape ?v)
2025 (setq child
2026 (cond ((not chosen)
2027 (let ((child (widget-create-child widget type)))
2028 (widget-apply child :deactivate)
2029 child))
2030 ((widget-get type :inline)
2031 (widget-create-child-value
2032 widget type (cdr chosen)))
2034 (widget-create-child-value
2035 widget type (car (cdr chosen)))))))
2037 (error "Unknown escape `%c'" escape)))))
2038 ;; Update properties.
2039 (and button child (widget-put child :button button))
2040 (and button (widget-put widget :buttons (cons button buttons)))
2041 (and child (widget-put widget :children (cons child children))))))
2043 (defun widget-checklist-match (widget values)
2044 ;; All values must match a type in the checklist.
2045 (and (listp values)
2046 (null (cdr (widget-checklist-match-inline widget values)))))
2048 (defun widget-checklist-match-inline (widget values)
2049 ;; Find the values which match a type in the checklist.
2050 (let ((greedy (widget-get widget :greedy))
2051 (args (copy-sequence (widget-get widget :args)))
2052 found rest)
2053 (while values
2054 (let ((answer (widget-checklist-match-up args values)))
2055 (cond (answer
2056 (let ((vals (widget-match-inline answer values)))
2057 (setq found (append found (car vals))
2058 values (cdr vals)
2059 args (delq answer args))))
2060 (greedy
2061 (setq rest (append rest (list (car values)))
2062 values (cdr values)))
2064 (setq rest (append rest values)
2065 values nil)))))
2066 (cons found rest)))
2068 (defun widget-checklist-match-find (widget vals)
2069 "Find the vals which match a type in the checklist.
2070 Return an alist of (TYPE MATCH)."
2071 (let ((greedy (widget-get widget :greedy))
2072 (args (copy-sequence (widget-get widget :args)))
2073 found)
2074 (while vals
2075 (let ((answer (widget-checklist-match-up args vals)))
2076 (cond (answer
2077 (let ((match (widget-match-inline answer vals)))
2078 (setq found (cons (cons answer (car match)) found)
2079 vals (cdr match)
2080 args (delq answer args))))
2081 (greedy
2082 (setq vals (cdr vals)))
2084 (setq vals nil)))))
2085 found))
2087 (defun widget-checklist-match-up (args vals)
2088 "Return the first type from ARGS that matches VALS."
2089 (let (current found)
2090 (while (and args (null found))
2091 (setq current (car args)
2092 args (cdr args)
2093 found (widget-match-inline current vals)))
2094 (if found
2095 current)))
2097 (defun widget-checklist-value-get (widget)
2098 ;; The values of all selected items.
2099 (let ((children (widget-get widget :children))
2100 child result)
2101 (while children
2102 (setq child (car children)
2103 children (cdr children))
2104 (if (widget-value (widget-get child :button))
2105 (setq result (append result (widget-apply child :value-inline)))))
2106 result))
2108 (defun widget-checklist-validate (widget)
2109 ;; Ticked chilren must be valid.
2110 (let ((children (widget-get widget :children))
2111 child button found)
2112 (while (and children (not found))
2113 (setq child (car children)
2114 children (cdr children)
2115 button (widget-get child :button)
2116 found (and (widget-value button)
2117 (widget-apply child :validate))))
2118 found))
2120 ;;; The `option' Widget
2122 (define-widget 'option 'checklist
2123 "An widget with an optional item."
2124 :inline t)
2126 ;;; The `choice-item' Widget.
2128 (define-widget 'choice-item 'item
2129 "Button items that delegate action events to their parents."
2130 :action 'widget-parent-action
2131 :format "%[%t%] \n")
2133 ;;; The `radio-button' Widget.
2135 (define-widget 'radio-button 'toggle
2136 "A radio button for use in the `radio' widget."
2137 :notify 'widget-radio-button-notify
2138 :format "%[%v%]"
2139 :button-suffix ""
2140 :button-prefix ""
2141 :on "(*)"
2142 :on-glyph "radio1"
2143 :off "( )"
2144 :off-glyph "radio0")
2146 (defun widget-radio-button-notify (widget child &optional event)
2147 ;; Tell daddy.
2148 (widget-apply (widget-get widget :parent) :action widget event))
2150 ;;; The `radio-button-choice' Widget.
2152 (define-widget 'radio-button-choice 'default
2153 "Select one of multiple options."
2154 :convert-widget 'widget-types-convert-widget
2155 :offset 4
2156 :format "%v"
2157 :entry-format "%b %v"
2158 :menu-tag "radio"
2159 :value-create 'widget-radio-value-create
2160 :value-delete 'widget-children-value-delete
2161 :value-get 'widget-radio-value-get
2162 :value-inline 'widget-radio-value-inline
2163 :value-set 'widget-radio-value-set
2164 :error "You must push one of the buttons"
2165 :validate 'widget-radio-validate
2166 :match 'widget-choice-match
2167 :match-inline 'widget-choice-match-inline
2168 :action 'widget-radio-action)
2170 (defun widget-radio-value-create (widget)
2171 ;; Insert all values
2172 (let ((args (widget-get widget :args))
2173 arg)
2174 (while args
2175 (setq arg (car args)
2176 args (cdr args))
2177 (widget-radio-add-item widget arg))))
2179 (defun widget-radio-add-item (widget type)
2180 "Add to radio widget WIDGET a new radio button item of type TYPE."
2181 ;; (setq type (widget-convert type))
2182 (and (eq (preceding-char) ?\n)
2183 (widget-get widget :indent)
2184 (insert-char ? (widget-get widget :indent)))
2185 (widget-specify-insert
2186 (let* ((value (widget-get widget :value))
2187 (children (widget-get widget :children))
2188 (buttons (widget-get widget :buttons))
2189 (button-args (or (widget-get type :sibling-args)
2190 (widget-get widget :button-args)))
2191 (from (point))
2192 (chosen (and (null (widget-get widget :choice))
2193 (widget-apply type :match value)))
2194 child button)
2195 (insert (widget-get widget :entry-format))
2196 (goto-char from)
2197 ;; Parse % escapes in format.
2198 (while (re-search-forward "%\\([bv%]\\)" nil t)
2199 (let ((escape (char-after (match-beginning 1))))
2200 (delete-backward-char 2)
2201 (cond ((eq escape ?%)
2202 (insert ?%))
2203 ((eq escape ?b)
2204 (setq button (apply 'widget-create-child-and-convert
2205 widget 'radio-button
2206 :value (not (null chosen))
2207 button-args)))
2208 ((eq escape ?v)
2209 (setq child (if chosen
2210 (widget-create-child-value
2211 widget type value)
2212 (widget-create-child widget type)))
2213 (unless chosen
2214 (widget-apply child :deactivate)))
2216 (error "Unknown escape `%c'" escape)))))
2217 ;; Update properties.
2218 (when chosen
2219 (widget-put widget :choice type))
2220 (when button
2221 (widget-put child :button button)
2222 (widget-put widget :buttons (nconc buttons (list button))))
2223 (when child
2224 (widget-put widget :children (nconc children (list child))))
2225 child)))
2227 (defun widget-radio-value-get (widget)
2228 ;; Get value of the child widget.
2229 (let ((chosen (widget-radio-chosen widget)))
2230 (and chosen (widget-value chosen))))
2232 (defun widget-radio-chosen (widget)
2233 "Return the widget representing the chosen radio button."
2234 (let ((children (widget-get widget :children))
2235 current found)
2236 (while children
2237 (setq current (car children)
2238 children (cdr children))
2239 (when (widget-apply (widget-get current :button) :value-get)
2240 (setq found current
2241 children nil)))
2242 found))
2244 (defun widget-radio-value-inline (widget)
2245 ;; Get value of the child widget.
2246 (let ((children (widget-get widget :children))
2247 current found)
2248 (while children
2249 (setq current (car children)
2250 children (cdr children))
2251 (when (widget-apply (widget-get current :button) :value-get)
2252 (setq found (widget-apply current :value-inline)
2253 children nil)))
2254 found))
2256 (defun widget-radio-value-set (widget value)
2257 ;; We can't just delete and recreate a radio widget, since children
2258 ;; can be added after the original creation and won't be recreated
2259 ;; by `:create'.
2260 (let ((children (widget-get widget :children))
2261 current found)
2262 (while children
2263 (setq current (car children)
2264 children (cdr children))
2265 (let* ((button (widget-get current :button))
2266 (match (and (not found)
2267 (widget-apply current :match value))))
2268 (widget-value-set button match)
2269 (if match
2270 (progn
2271 (widget-value-set current value)
2272 (widget-apply current :activate))
2273 (widget-apply current :deactivate))
2274 (setq found (or found match))))))
2276 (defun widget-radio-validate (widget)
2277 ;; Valid if we have made a valid choice.
2278 (let ((children (widget-get widget :children))
2279 current found button)
2280 (while (and children (not found))
2281 (setq current (car children)
2282 children (cdr children)
2283 button (widget-get current :button)
2284 found (widget-apply button :value-get)))
2285 (if found
2286 (widget-apply current :validate)
2287 widget)))
2289 (defun widget-radio-action (widget child event)
2290 ;; Check if a radio button was pressed.
2291 (let ((children (widget-get widget :children))
2292 (buttons (widget-get widget :buttons))
2293 current)
2294 (when (memq child buttons)
2295 (while children
2296 (setq current (car children)
2297 children (cdr children))
2298 (let* ((button (widget-get current :button)))
2299 (cond ((eq child button)
2300 (widget-value-set button t)
2301 (widget-apply current :activate))
2302 ((widget-value button)
2303 (widget-value-set button nil)
2304 (widget-apply current :deactivate)))))))
2305 ;; Pass notification to parent.
2306 (widget-apply widget :notify child event))
2308 ;;; The `insert-button' Widget.
2310 (define-widget 'insert-button 'push-button
2311 "An insert button for the `editable-list' widget."
2312 :tag "INS"
2313 :help-echo "Insert a new item into the list at this position."
2314 :action 'widget-insert-button-action)
2316 (defun widget-insert-button-action (widget &optional event)
2317 ;; Ask the parent to insert a new item.
2318 (widget-apply (widget-get widget :parent)
2319 :insert-before (widget-get widget :widget)))
2321 ;;; The `delete-button' Widget.
2323 (define-widget 'delete-button 'push-button
2324 "A delete button for the `editable-list' widget."
2325 :tag "DEL"
2326 :help-echo "Delete this item from the list."
2327 :action 'widget-delete-button-action)
2329 (defun widget-delete-button-action (widget &optional event)
2330 ;; Ask the parent to insert a new item.
2331 (widget-apply (widget-get widget :parent)
2332 :delete-at (widget-get widget :widget)))
2334 ;;; The `editable-list' Widget.
2336 ;; (defcustom widget-editable-list-gui nil
2337 ;; "If non nil, use GUI push-buttons in editable list when available."
2338 ;; :type 'boolean
2339 ;; :group 'widgets)
2341 (define-widget 'editable-list 'default
2342 "A variable list of widgets of the same type."
2343 :convert-widget 'widget-types-convert-widget
2344 :offset 12
2345 :format "%v%i\n"
2346 :format-handler 'widget-editable-list-format-handler
2347 :entry-format "%i %d %v"
2348 :menu-tag "editable-list"
2349 :value-create 'widget-editable-list-value-create
2350 :value-delete 'widget-children-value-delete
2351 :value-get 'widget-editable-list-value-get
2352 :validate 'widget-children-validate
2353 :match 'widget-editable-list-match
2354 :match-inline 'widget-editable-list-match-inline
2355 :insert-before 'widget-editable-list-insert-before
2356 :delete-at 'widget-editable-list-delete-at)
2358 (defun widget-editable-list-format-handler (widget escape)
2359 ;; We recognize the insert button.
2360 ;;; (let ((widget-push-button-gui widget-editable-list-gui))
2361 (cond ((eq escape ?i)
2362 (and (widget-get widget :indent)
2363 (insert-char ?\ (widget-get widget :indent)))
2364 (apply 'widget-create-child-and-convert
2365 widget 'insert-button
2366 (widget-get widget :append-button-args)))
2368 (widget-default-format-handler widget escape)))
2369 ;;; )
2372 (defun widget-editable-list-value-create (widget)
2373 ;; Insert all values
2374 (let* ((value (widget-get widget :value))
2375 (type (nth 0 (widget-get widget :args)))
2376 children)
2377 (widget-put widget :value-pos (copy-marker (point)))
2378 (set-marker-insertion-type (widget-get widget :value-pos) t)
2379 (while value
2380 (let ((answer (widget-match-inline type value)))
2381 (if answer
2382 (setq children (cons (widget-editable-list-entry-create
2383 widget
2384 (if (widget-get type :inline)
2385 (car answer)
2386 (car (car answer)))
2388 children)
2389 value (cdr answer))
2390 (setq value nil))))
2391 (widget-put widget :children (nreverse children))))
2393 (defun widget-editable-list-value-get (widget)
2394 ;; Get value of the child widget.
2395 (apply 'append (mapcar (lambda (child) (widget-apply child :value-inline))
2396 (widget-get widget :children))))
2398 (defun widget-editable-list-match (widget value)
2399 ;; Value must be a list and all the members must match the type.
2400 (and (listp value)
2401 (null (cdr (widget-editable-list-match-inline widget value)))))
2403 (defun widget-editable-list-match-inline (widget value)
2404 (let ((type (nth 0 (widget-get widget :args)))
2405 (ok t)
2406 found)
2407 (while (and value ok)
2408 (let ((answer (widget-match-inline type value)))
2409 (if answer
2410 (setq found (append found (car answer))
2411 value (cdr answer))
2412 (setq ok nil))))
2413 (cons found value)))
2415 (defun widget-editable-list-insert-before (widget before)
2416 ;; Insert a new child in the list of children.
2417 (save-excursion
2418 (let ((children (widget-get widget :children))
2419 (inhibit-read-only t)
2420 before-change-functions
2421 after-change-functions)
2422 (cond (before
2423 (goto-char (widget-get before :entry-from)))
2425 (goto-char (widget-get widget :value-pos))))
2426 (let ((child (widget-editable-list-entry-create
2427 widget nil nil)))
2428 (when (< (widget-get child :entry-from) (widget-get widget :from))
2429 (set-marker (widget-get widget :from)
2430 (widget-get child :entry-from)))
2431 (if (eq (car children) before)
2432 (widget-put widget :children (cons child children))
2433 (while (not (eq (car (cdr children)) before))
2434 (setq children (cdr children)))
2435 (setcdr children (cons child (cdr children)))))))
2436 (widget-setup)
2437 (widget-apply widget :notify widget))
2439 (defun widget-editable-list-delete-at (widget child)
2440 ;; Delete child from list of children.
2441 (save-excursion
2442 (let ((buttons (copy-sequence (widget-get widget :buttons)))
2443 button
2444 (inhibit-read-only t)
2445 before-change-functions
2446 after-change-functions)
2447 (while buttons
2448 (setq button (car buttons)
2449 buttons (cdr buttons))
2450 (when (eq (widget-get button :widget) child)
2451 (widget-put widget
2452 :buttons (delq button (widget-get widget :buttons)))
2453 (widget-delete button))))
2454 (let ((entry-from (widget-get child :entry-from))
2455 (entry-to (widget-get child :entry-to))
2456 (inhibit-read-only t)
2457 before-change-functions
2458 after-change-functions)
2459 (widget-delete child)
2460 (delete-region entry-from entry-to)
2461 (set-marker entry-from nil)
2462 (set-marker entry-to nil))
2463 (widget-put widget :children (delq child (widget-get widget :children))))
2464 (widget-setup)
2465 (widget-apply widget :notify widget))
2467 (defun widget-editable-list-entry-create (widget value conv)
2468 ;; Create a new entry to the list.
2469 (let ((type (nth 0 (widget-get widget :args)))
2470 ;;; (widget-push-button-gui widget-editable-list-gui)
2471 child delete insert)
2472 (widget-specify-insert
2473 (save-excursion
2474 (and (widget-get widget :indent)
2475 (insert-char ?\ (widget-get widget :indent)))
2476 (insert (widget-get widget :entry-format)))
2477 ;; Parse % escapes in format.
2478 (while (re-search-forward "%\\(.\\)" nil t)
2479 (let ((escape (char-after (match-beginning 1))))
2480 (delete-backward-char 2)
2481 (cond ((eq escape ?%)
2482 (insert ?%))
2483 ((eq escape ?i)
2484 (setq insert (apply 'widget-create-child-and-convert
2485 widget 'insert-button
2486 (widget-get widget :insert-button-args))))
2487 ((eq escape ?d)
2488 (setq delete (apply 'widget-create-child-and-convert
2489 widget 'delete-button
2490 (widget-get widget :delete-button-args))))
2491 ((eq escape ?v)
2492 (if conv
2493 (setq child (widget-create-child-value
2494 widget type value))
2495 (setq child (widget-create-child-value
2496 widget type
2497 (widget-apply type :value-to-external
2498 (widget-default-get type))))))
2500 (error "Unknown escape `%c'" escape)))))
2501 (widget-put widget
2502 :buttons (cons delete
2503 (cons insert
2504 (widget-get widget :buttons))))
2505 (let ((entry-from (point-min-marker))
2506 (entry-to (point-max-marker)))
2507 (set-marker-insertion-type entry-from t)
2508 (set-marker-insertion-type entry-to nil)
2509 (widget-put child :entry-from entry-from)
2510 (widget-put child :entry-to entry-to)))
2511 (widget-put insert :widget child)
2512 (widget-put delete :widget child)
2513 child))
2515 ;;; The `group' Widget.
2517 (define-widget 'group 'default
2518 "A widget which groups other widgets inside."
2519 :convert-widget 'widget-types-convert-widget
2520 :format "%v"
2521 :value-create 'widget-group-value-create
2522 :value-delete 'widget-children-value-delete
2523 :value-get 'widget-editable-list-value-get
2524 :default-get 'widget-group-default-get
2525 :validate 'widget-children-validate
2526 :match 'widget-group-match
2527 :match-inline 'widget-group-match-inline)
2529 (defun widget-group-value-create (widget)
2530 ;; Create each component.
2531 (let ((args (widget-get widget :args))
2532 (value (widget-get widget :value))
2533 arg answer children)
2534 (while args
2535 (setq arg (car args)
2536 args (cdr args)
2537 answer (widget-match-inline arg value)
2538 value (cdr answer))
2539 (and (eq (preceding-char) ?\n)
2540 (widget-get widget :indent)
2541 (insert-char ?\ (widget-get widget :indent)))
2542 (push (cond ((null answer)
2543 (widget-create-child widget arg))
2544 ((widget-get arg :inline)
2545 (widget-create-child-value widget arg (car answer)))
2547 (widget-create-child-value widget arg (car (car answer)))))
2548 children))
2549 (widget-put widget :children (nreverse children))))
2551 (defun widget-group-default-get (widget)
2552 ;; Get the default of the components.
2553 (mapcar 'widget-default-get (widget-get widget :args)))
2555 (defun widget-group-match (widget values)
2556 ;; Match if the components match.
2557 (and (listp values)
2558 (let ((match (widget-group-match-inline widget values)))
2559 (and match (null (cdr match))))))
2561 (defun widget-group-match-inline (widget vals)
2562 ;; Match if the components match.
2563 (let ((args (widget-get widget :args))
2564 argument answer found)
2565 (while args
2566 (setq argument (car args)
2567 args (cdr args)
2568 answer (widget-match-inline argument vals))
2569 (if answer
2570 (setq vals (cdr answer)
2571 found (append found (car answer)))
2572 (setq vals nil
2573 args nil)))
2574 (if answer
2575 (cons found vals))))
2577 ;;; The `visibility' Widget.
2579 (define-widget 'visibility 'item
2580 "An indicator and manipulator for hidden items."
2581 :format "%[%v%]"
2582 :button-prefix ""
2583 :button-suffix ""
2584 :on "Hide"
2585 :off "Show"
2586 :value-create 'widget-visibility-value-create
2587 :action 'widget-toggle-action
2588 :match (lambda (widget value) t))
2590 (defun widget-visibility-value-create (widget)
2591 ;; Insert text representing the `on' and `off' states.
2592 (let ((on (widget-get widget :on))
2593 (off (widget-get widget :off)))
2594 (if on
2595 (setq on (concat widget-push-button-prefix
2597 widget-push-button-suffix))
2598 (setq on ""))
2599 (if off
2600 (setq off (concat widget-push-button-prefix
2602 widget-push-button-suffix))
2603 (setq off ""))
2604 (if (widget-value widget)
2605 (widget-image-insert widget on "down" "down-pushed")
2606 (widget-image-insert widget off "right" "right-pushed"))))
2608 ;;; The `documentation-link' Widget.
2610 ;; This is a helper widget for `documentation-string'.
2612 (define-widget 'documentation-link 'link
2613 "Link type used in documentation strings."
2614 :tab-order -1
2615 :help-echo "Describe this symbol"
2616 :action 'widget-documentation-link-action)
2618 (defun widget-documentation-link-action (widget &optional event)
2619 "Display documentation for WIDGET's value. Ignore optional argument EVENT."
2620 (let* ((string (widget-get widget :value))
2621 (symbol (intern string)))
2622 (if (and (fboundp symbol) (boundp symbol))
2623 ;; If there are two doc strings, give the user a way to pick one.
2624 (apropos (concat "\\`" (regexp-quote string) "\\'"))
2625 (if (fboundp symbol)
2626 (describe-function symbol)
2627 (describe-variable symbol)))))
2629 (defcustom widget-documentation-links t
2630 "Add hyperlinks to documentation strings when non-nil."
2631 :type 'boolean
2632 :group 'widget-documentation)
2634 (defcustom widget-documentation-link-regexp "`\\([^\n`' ]+\\)'"
2635 "Regexp for matching potential links in documentation strings.
2636 The first group should be the link itself."
2637 :type 'regexp
2638 :group 'widget-documentation)
2640 (defcustom widget-documentation-link-p 'intern-soft
2641 "Predicate used to test if a string is useful as a link.
2642 The value should be a function. The function will be called one
2643 argument, a string, and should return non-nil if there should be a
2644 link for that string."
2645 :type 'function
2646 :options '(widget-documentation-link-p)
2647 :group 'widget-documentation)
2649 (defcustom widget-documentation-link-type 'documentation-link
2650 "Widget type used for links in documentation strings."
2651 :type 'symbol
2652 :group 'widget-documentation)
2654 (defun widget-documentation-link-add (widget from to)
2655 (widget-specify-doc widget from to)
2656 (when widget-documentation-links
2657 (let ((regexp widget-documentation-link-regexp)
2658 (buttons (widget-get widget :buttons))
2659 (widget-mouse-face (default-value 'widget-mouse-face))
2660 (widget-button-face widget-documentation-face)
2661 (widget-button-pressed-face widget-documentation-face))
2662 (save-excursion
2663 (goto-char from)
2664 (while (re-search-forward regexp to t)
2665 (let ((name (match-string 1))
2666 (begin (match-beginning 1))
2667 (end (match-end 1)))
2668 (when (funcall widget-documentation-link-p name)
2669 (push (widget-convert-button widget-documentation-link-type
2670 begin end :value name)
2671 buttons)))))
2672 (widget-put widget :buttons buttons)))
2673 (let ((indent (widget-get widget :indent)))
2674 (when (and indent (not (zerop indent)))
2675 (save-excursion
2676 (save-restriction
2677 (narrow-to-region from to)
2678 (goto-char (point-min))
2679 (while (search-forward "\n" nil t)
2680 (insert-char ?\ indent)))))))
2682 ;;; The `documentation-string' Widget.
2684 (define-widget 'documentation-string 'item
2685 "A documentation string."
2686 :format "%v"
2687 :action 'widget-documentation-string-action
2688 :value-delete 'widget-children-value-delete
2689 :value-create 'widget-documentation-string-value-create)
2691 (defun widget-documentation-string-value-create (widget)
2692 ;; Insert documentation string.
2693 (let ((doc (widget-value widget))
2694 (indent (widget-get widget :indent))
2695 (shown (widget-get (widget-get widget :parent) :documentation-shown))
2696 (start (point)))
2697 (if (string-match "\n" doc)
2698 (let ((before (substring doc 0 (match-beginning 0)))
2699 (after (substring doc (match-beginning 0)))
2700 button)
2701 (insert before ?\ )
2702 (widget-documentation-link-add widget start (point))
2703 (setq button
2704 (widget-create-child-and-convert
2705 widget 'visibility
2706 :help-echo "Show or hide rest of the documentation."
2707 :off "More"
2708 :always-active t
2709 :action 'widget-parent-action
2710 shown))
2711 (when shown
2712 (setq start (point))
2713 (when (and indent (not (zerop indent)))
2714 (insert-char ?\ indent))
2715 (insert after)
2716 (widget-documentation-link-add widget start (point)))
2717 (widget-put widget :buttons (list button)))
2718 (insert doc)
2719 (widget-documentation-link-add widget start (point))))
2720 (insert ?\n))
2722 (defun widget-documentation-string-action (widget &rest ignore)
2723 ;; Toggle documentation.
2724 (let ((parent (widget-get widget :parent)))
2725 (widget-put parent :documentation-shown
2726 (not (widget-get parent :documentation-shown))))
2727 ;; Redraw.
2728 (widget-value-set widget (widget-value widget)))
2730 ;;; The Sexp Widgets.
2732 (define-widget 'const 'item
2733 "An immutable sexp."
2734 :prompt-value 'widget-const-prompt-value
2735 :format "%t\n%d")
2737 (defun widget-const-prompt-value (widget prompt value unbound)
2738 ;; Return the value of the const.
2739 (widget-value widget))
2741 (define-widget 'function-item 'const
2742 "An immutable function name."
2743 :format "%v\n%h"
2744 :documentation-property (lambda (symbol)
2745 (condition-case nil
2746 (documentation symbol t)
2747 (error nil))))
2749 (define-widget 'variable-item 'const
2750 "An immutable variable name."
2751 :format "%v\n%h"
2752 :documentation-property 'variable-documentation)
2754 (define-widget 'other 'sexp
2755 "Matches any value, but doesn't let the user edit the value.
2756 This is useful as last item in a `choice' widget.
2757 You should use this widget type with a default value,
2758 as in (other DEFAULT) or (other :tag \"NAME\" DEFAULT).
2759 If the user selects this alternative, that specifies DEFAULT
2760 as the value."
2761 :tag "Other"
2762 :format "%t%n"
2763 :value 'other)
2765 (defvar widget-string-prompt-value-history nil
2766 "History of input to `widget-string-prompt-value'.")
2768 (define-widget 'string 'editable-field
2769 "A string"
2770 :tag "String"
2771 :format "%{%t%}: %v"
2772 :complete-function 'ispell-complete-word
2773 :prompt-history 'widget-string-prompt-value-history)
2775 (define-widget 'regexp 'string
2776 "A regular expression."
2777 :match 'widget-regexp-match
2778 :validate 'widget-regexp-validate
2779 ;; Doesn't work well with terminating newline.
2780 ;; :value-face 'widget-single-line-field-face
2781 :tag "Regexp")
2783 (defun widget-regexp-match (widget value)
2784 ;; Match valid regexps.
2785 (and (stringp value)
2786 (condition-case nil
2787 (prog1 t
2788 (string-match value ""))
2789 (error nil))))
2791 (defun widget-regexp-validate (widget)
2792 "Check that the value of WIDGET is a valid regexp."
2793 (condition-case data
2794 (prog1 nil
2795 (string-match (widget-value widget) ""))
2796 (error (widget-put widget :error (error-message-string data))
2797 widget)))
2799 (define-widget 'file 'string
2800 "A file widget.
2801 It will read a file name from the minibuffer when invoked."
2802 :complete-function 'widget-file-complete
2803 :prompt-value 'widget-file-prompt-value
2804 :format "%{%t%}: %v"
2805 ;; Doesn't work well with terminating newline.
2806 ;; :value-face 'widget-single-line-field-face
2807 :tag "File")
2809 (defun widget-file-complete ()
2810 "Perform completion on file name preceding point."
2811 (interactive)
2812 (let* ((end (point))
2813 (beg (save-excursion
2814 (skip-chars-backward "^ ")
2815 (point)))
2816 (pattern (buffer-substring beg end))
2817 (name-part (file-name-nondirectory pattern))
2818 (directory (file-name-directory pattern))
2819 (completion (file-name-completion name-part directory)))
2820 (cond ((eq completion t))
2821 ((null completion)
2822 (message "Can't find completion for \"%s\"" pattern)
2823 (ding))
2824 ((not (string= name-part completion))
2825 (delete-region beg end)
2826 (insert (expand-file-name completion directory)))
2828 (message "Making completion list...")
2829 (with-output-to-temp-buffer "*Completions*"
2830 (display-completion-list
2831 (sort (file-name-all-completions name-part directory)
2832 'string<)))
2833 (message "Making completion list...%s" "done")))))
2835 (defun widget-file-prompt-value (widget prompt value unbound)
2836 ;; Read file from minibuffer.
2837 (abbreviate-file-name
2838 (if unbound
2839 (read-file-name prompt)
2840 (let ((prompt2 (format "%s (default %s) " prompt value))
2841 (dir (file-name-directory value))
2842 (file (file-name-nondirectory value))
2843 (must-match (widget-get widget :must-match)))
2844 (read-file-name prompt2 dir nil must-match file)))))
2846 ;;;(defun widget-file-action (widget &optional event)
2847 ;;; ;; Read a file name from the minibuffer.
2848 ;;; (let* ((value (widget-value widget))
2849 ;;; (dir (file-name-directory value))
2850 ;;; (file (file-name-nondirectory value))
2851 ;;; (menu-tag (widget-apply widget :menu-tag-get))
2852 ;;; (must-match (widget-get widget :must-match))
2853 ;;; (answer (read-file-name (concat menu-tag ": (default `" value "') ")
2854 ;;; dir nil must-match file)))
2855 ;;; (widget-value-set widget (abbreviate-file-name answer))
2856 ;;; (widget-setup)
2857 ;;; (widget-apply widget :notify widget event)))
2859 (define-widget 'directory 'file
2860 "A directory widget.
2861 It will read a directory name from the minibuffer when invoked."
2862 :tag "Directory")
2864 (defvar widget-symbol-prompt-value-history nil
2865 "History of input to `widget-symbol-prompt-value'.")
2867 (define-widget 'symbol 'editable-field
2868 "A Lisp symbol."
2869 :value nil
2870 :tag "Symbol"
2871 :format "%{%t%}: %v"
2872 :match (lambda (widget value) (symbolp value))
2873 :complete-function 'lisp-complete-symbol
2874 :prompt-internal 'widget-symbol-prompt-internal
2875 :prompt-match 'symbolp
2876 :prompt-history 'widget-symbol-prompt-value-history
2877 :value-to-internal (lambda (widget value)
2878 (if (symbolp value)
2879 (symbol-name value)
2880 value))
2881 :value-to-external (lambda (widget value)
2882 (if (stringp value)
2883 (intern value)
2884 value)))
2886 (defun widget-symbol-prompt-internal (widget prompt initial history)
2887 ;; Read file from minibuffer.
2888 (let ((answer (completing-read prompt obarray
2889 (widget-get widget :prompt-match)
2890 nil initial history)))
2891 (if (and (stringp answer)
2892 (not (zerop (length answer))))
2893 answer
2894 (error "No value"))))
2896 (defvar widget-function-prompt-value-history nil
2897 "History of input to `widget-function-prompt-value'.")
2899 (define-widget 'function 'sexp
2900 "A Lisp function."
2901 :complete-function (lambda ()
2902 (interactive)
2903 (lisp-complete-symbol 'fboundp))
2904 :prompt-value 'widget-field-prompt-value
2905 :prompt-internal 'widget-symbol-prompt-internal
2906 :prompt-match 'fboundp
2907 :prompt-history 'widget-function-prompt-value-history
2908 :action 'widget-field-action
2909 :validate (lambda (widget)
2910 (unless (functionp (widget-value widget))
2911 (widget-put widget :error (format "Invalid function: %S"
2912 (widget-value widget)))
2913 widget))
2914 :value 'ignore
2915 :tag "Function")
2917 (defvar widget-variable-prompt-value-history nil
2918 "History of input to `widget-variable-prompt-value'.")
2920 (define-widget 'variable 'symbol
2921 ;; Should complete on variables.
2922 "A Lisp variable."
2923 :prompt-match 'boundp
2924 :prompt-history 'widget-variable-prompt-value-history
2925 :complete-function (lambda ()
2926 (interactive)
2927 (lisp-complete-symbol 'boundp))
2928 :tag "Variable")
2930 (defvar widget-coding-system-prompt-value-history nil
2931 "History of input to `widget-coding-system-prompt-value'.")
2933 (define-widget 'coding-system 'symbol
2934 "A MULE coding-system."
2935 :format "%{%t%}: %v"
2936 :tag "Coding system"
2937 :base-only nil
2938 :prompt-history 'widget-coding-system-prompt-value-history
2939 :prompt-value 'widget-coding-system-prompt-value
2940 :action 'widget-coding-system-action
2941 :complete-function (lambda ()
2942 (interactive)
2943 (lisp-complete-symbol 'coding-system-p))
2944 :validate (lambda (widget)
2945 (unless (coding-system-p (widget-value widget))
2946 (widget-put widget :error (format "Invalid coding system: %S"
2947 (widget-value widget)))
2948 widget))
2949 :value 'undecided
2950 :prompt-match 'coding-system-p)
2952 (defun widget-coding-system-prompt-value (widget prompt value unbound)
2953 "Read coding-system from minibuffer."
2954 (if (widget-get widget :base-only)
2955 (intern
2956 (completing-read (format "%s (default %s) " prompt value)
2957 (mapcar #'list (coding-system-list t)) nil nil nil
2958 coding-system-history))
2959 (read-coding-system (format "%s (default %s) " prompt value) value)))
2961 (defun widget-coding-system-action (widget &optional event)
2962 (let ((answer
2963 (widget-coding-system-prompt-value
2964 widget
2965 (widget-apply widget :menu-tag-get)
2966 (widget-value widget)
2967 t)))
2968 (widget-value-set widget answer)
2969 (widget-apply widget :notify widget event)
2970 (widget-setup)))
2972 (define-widget 'sexp 'editable-field
2973 "An arbitrary Lisp expression."
2974 :tag "Lisp expression"
2975 :format "%{%t%}: %v"
2976 :value nil
2977 :validate 'widget-sexp-validate
2978 :match (lambda (widget value) t)
2979 :value-to-internal 'widget-sexp-value-to-internal
2980 :value-to-external (lambda (widget value) (read value))
2981 :prompt-history 'widget-sexp-prompt-value-history
2982 :prompt-value 'widget-sexp-prompt-value)
2984 (defun widget-sexp-value-to-internal (widget value)
2985 ;; Use pp for printer representation.
2986 (let ((pp (if (symbolp value)
2987 (prin1-to-string value)
2988 (pp-to-string value))))
2989 (while (string-match "\n\\'" pp)
2990 (setq pp (substring pp 0 -1)))
2991 (if (or (string-match "\n\\'" pp)
2992 (> (length pp) 40))
2993 (concat "\n" pp)
2994 pp)))
2996 (defun widget-sexp-validate (widget)
2997 ;; Valid if we can read the string and there is no junk left after it.
2998 (with-temp-buffer
2999 (insert (widget-apply widget :value-get))
3000 (goto-char (point-min))
3001 (let (err)
3002 (condition-case data
3003 (progn
3004 ;; Avoid a confusing end-of-file error.
3005 (skip-syntax-forward "\\s-")
3006 (if (eobp)
3007 (setq err "Empty sexp -- use `nil'?")
3008 (unless (widget-apply widget :match (read (current-buffer)))
3009 (setq err (widget-get widget :type-error))))
3010 (if (and (not (eobp))
3011 (not err))
3012 (setq err (format "Junk at end of expression: %s"
3013 (buffer-substring (point)
3014 (point-max))))))
3015 (end-of-file ; Avoid confusing error message.
3016 (setq err "Unbalanced sexp"))
3017 (error (setq err (error-message-string data))))
3018 (if (not err)
3020 (widget-put widget :error err)
3021 widget))))
3023 (defvar widget-sexp-prompt-value-history nil
3024 "History of input to `widget-sexp-prompt-value'.")
3026 (defun widget-sexp-prompt-value (widget prompt value unbound)
3027 ;; Read an arbitrary sexp.
3028 (let ((found (read-string prompt
3029 (if unbound nil (cons (prin1-to-string value) 0))
3030 (widget-get widget :prompt-history))))
3031 (let ((answer (read-from-string found)))
3032 (unless (= (cdr answer) (length found))
3033 (error "Junk at end of expression: %s"
3034 (substring found (cdr answer))))
3035 (car answer))))
3037 (define-widget 'restricted-sexp 'sexp
3038 "A Lisp expression restricted to values that match.
3039 To use this type, you must define :match or :match-alternatives."
3040 :type-error "The specified value is not valid"
3041 :match 'widget-restricted-sexp-match
3042 :value-to-internal (lambda (widget value)
3043 (if (widget-apply widget :match value)
3044 (prin1-to-string value)
3045 value)))
3047 (defun widget-restricted-sexp-match (widget value)
3048 (let ((alternatives (widget-get widget :match-alternatives))
3049 matched)
3050 (while (and alternatives (not matched))
3051 (if (cond ((functionp (car alternatives))
3052 (funcall (car alternatives) value))
3053 ((and (consp (car alternatives))
3054 (eq (car (car alternatives)) 'quote))
3055 (eq value (nth 1 (car alternatives)))))
3056 (setq matched t))
3057 (setq alternatives (cdr alternatives)))
3058 matched))
3060 (define-widget 'integer 'restricted-sexp
3061 "An integer."
3062 :tag "Integer"
3063 :value 0
3064 :type-error "This field should contain an integer"
3065 :match-alternatives '(integerp))
3067 (define-widget 'number 'restricted-sexp
3068 "A floating point number."
3069 :tag "Number"
3070 :value 0.0
3071 :type-error "This field should contain a number"
3072 :match-alternatives '(numberp))
3074 (define-widget 'character 'editable-field
3075 "A character."
3076 :tag "Character"
3077 :value 0
3078 :size 1
3079 :format "%{%t%}: %v\n"
3080 :valid-regexp "\\`.\\'"
3081 :error "This field should contain a single character"
3082 :value-to-internal (lambda (widget value)
3083 (if (stringp value)
3084 value
3085 (char-to-string value)))
3086 :value-to-external (lambda (widget value)
3087 (if (stringp value)
3088 (aref value 0)
3089 value))
3090 :match (lambda (widget value)
3091 (char-valid-p value)))
3093 (define-widget 'list 'group
3094 "A Lisp list."
3095 :tag "List"
3096 :format "%{%t%}:\n%v")
3098 (define-widget 'vector 'group
3099 "A Lisp vector."
3100 :tag "Vector"
3101 :format "%{%t%}:\n%v"
3102 :match 'widget-vector-match
3103 :value-to-internal (lambda (widget value) (append value nil))
3104 :value-to-external (lambda (widget value) (apply 'vector value)))
3106 (defun widget-vector-match (widget value)
3107 (and (vectorp value)
3108 (widget-group-match widget
3109 (widget-apply widget :value-to-internal value))))
3111 (define-widget 'cons 'group
3112 "A cons-cell."
3113 :tag "Cons-cell"
3114 :format "%{%t%}:\n%v"
3115 :match 'widget-cons-match
3116 :value-to-internal (lambda (widget value)
3117 (list (car value) (cdr value)))
3118 :value-to-external (lambda (widget value)
3119 (cons (nth 0 value) (nth 1 value))))
3121 (defun widget-cons-match (widget value)
3122 (and (consp value)
3123 (widget-group-match widget
3124 (widget-apply widget :value-to-internal value))))
3126 ;;; The `plist' Widget.
3128 ;; Property lists.
3130 (define-widget 'plist 'list
3131 "A property list."
3132 :key-type '(symbol :tag "Key")
3133 :value-type '(sexp :tag "Value")
3134 :convert-widget 'widget-plist-convert-widget
3135 :tag "Plist")
3137 (defvar widget-plist-value-type) ;Dynamic variable
3139 (defun widget-plist-convert-widget (widget)
3140 ;; Handle `:options'.
3141 (let* ((options (widget-get widget :options))
3142 (other `(editable-list :inline t
3143 (group :inline t
3144 ,(widget-get widget :key-type)
3145 ,(widget-get widget :value-type))))
3146 (args (if options
3147 (list `(checklist :inline t
3148 :greedy t
3149 ,@(mapcar 'widget-plist-convert-option
3150 options))
3151 other)
3152 (list other))))
3153 (widget-put widget :args args)
3154 widget))
3156 (defun widget-plist-convert-option (option)
3157 ;; Convert a single plist option.
3158 (let (key-type value-type)
3159 (if (listp option)
3160 (let ((key (nth 0 option)))
3161 (setq value-type (nth 1 option))
3162 (if (listp key)
3163 (setq key-type key)
3164 (setq key-type `(const ,key))))
3165 (setq key-type `(const ,option)
3166 value-type widget-plist-value-type))
3167 `(group :format "Key: %v" :inline t ,key-type ,value-type)))
3170 ;;; The `alist' Widget.
3172 ;; Association lists.
3174 (define-widget 'alist 'list
3175 "An association list."
3176 :key-type '(sexp :tag "Key")
3177 :value-type '(sexp :tag "Value")
3178 :convert-widget 'widget-alist-convert-widget
3179 :tag "Alist")
3181 (defvar widget-alist-value-type) ;Dynamic variable
3183 (defun widget-alist-convert-widget (widget)
3184 ;; Handle `:options'.
3185 (let* ((options (widget-get widget :options))
3186 (other `(editable-list :inline t
3187 (cons :format "%v"
3188 ,(widget-get widget :key-type)
3189 ,(widget-get widget :value-type))))
3190 (args (if options
3191 (list `(checklist :inline t
3192 :greedy t
3193 ,@(mapcar 'widget-alist-convert-option
3194 options))
3195 other)
3196 (list other))))
3197 (widget-put widget :args args)
3198 widget))
3200 (defun widget-alist-convert-option (option)
3201 ;; Convert a single alist option.
3202 (let (key-type value-type)
3203 (if (listp option)
3204 (let ((key (nth 0 option)))
3205 (setq value-type (nth 1 option))
3206 (if (listp key)
3207 (setq key-type key)
3208 (setq key-type `(const ,key))))
3209 (setq key-type `(const ,option)
3210 value-type widget-alist-value-type))
3211 `(cons :format "Key: %v" ,key-type ,value-type)))
3213 (define-widget 'choice 'menu-choice
3214 "A union of several sexp types."
3215 :tag "Choice"
3216 :format "%{%t%}: %[Value Menu%] %v"
3217 :button-prefix 'widget-push-button-prefix
3218 :button-suffix 'widget-push-button-suffix
3219 :prompt-value 'widget-choice-prompt-value)
3221 (defun widget-choice-prompt-value (widget prompt value unbound)
3222 "Make a choice."
3223 (let ((args (widget-get widget :args))
3224 (completion-ignore-case (widget-get widget :case-fold))
3225 current choices old)
3226 ;; Find the first arg that matches VALUE.
3227 (let ((look args))
3228 (while look
3229 (if (widget-apply (car look) :match value)
3230 (setq old (car look)
3231 look nil)
3232 (setq look (cdr look)))))
3233 ;; Find new choice.
3234 (setq current
3235 (cond ((= (length args) 0)
3236 nil)
3237 ((= (length args) 1)
3238 (nth 0 args))
3239 ((and (= (length args) 2)
3240 (memq old args))
3241 (if (eq old (nth 0 args))
3242 (nth 1 args)
3243 (nth 0 args)))
3245 (while args
3246 (setq current (car args)
3247 args (cdr args))
3248 (setq choices
3249 (cons (cons (widget-apply current :menu-tag-get)
3250 current)
3251 choices)))
3252 (let ((val (completing-read prompt choices nil t)))
3253 (if (stringp val)
3254 (let ((try (try-completion val choices)))
3255 (when (stringp try)
3256 (setq val try))
3257 (cdr (assoc val choices)))
3258 nil)))))
3259 (if current
3260 (widget-prompt-value current prompt nil t)
3261 value)))
3263 (define-widget 'radio 'radio-button-choice
3264 "A union of several sexp types."
3265 :tag "Choice"
3266 :format "%{%t%}:\n%v"
3267 :prompt-value 'widget-choice-prompt-value)
3269 (define-widget 'repeat 'editable-list
3270 "A variable length homogeneous list."
3271 :tag "Repeat"
3272 :format "%{%t%}:\n%v%i\n")
3274 (define-widget 'set 'checklist
3275 "A list of members from a fixed set."
3276 :tag "Set"
3277 :format "%{%t%}:\n%v")
3279 (define-widget 'boolean 'toggle
3280 "To be nil or non-nil, that is the question."
3281 :tag "Boolean"
3282 :prompt-value 'widget-boolean-prompt-value
3283 :button-prefix 'widget-push-button-prefix
3284 :button-suffix 'widget-push-button-suffix
3285 :format "%{%t%}: %[Toggle%] %v\n"
3286 :on "on (non-nil)"
3287 :off "off (nil)")
3289 (defun widget-boolean-prompt-value (widget prompt value unbound)
3290 ;; Toggle a boolean.
3291 (y-or-n-p prompt))
3293 ;;; The `color' Widget.
3295 (define-widget 'color 'editable-field
3296 "Choose a color name (with sample)."
3297 :format "%t: %v (%{sample%})\n"
3298 :size 10
3299 :tag "Color"
3300 :value "black"
3301 :complete 'widget-color-complete
3302 :sample-face-get 'widget-color-sample-face-get
3303 :notify 'widget-color-notify
3304 :action 'widget-color-action)
3306 (defun widget-color-complete (widget)
3307 "Complete the color in WIDGET."
3308 (require 'facemenu) ; for facemenu-color-alist
3309 (let* ((prefix (buffer-substring-no-properties (widget-field-start widget)
3310 (point)))
3311 (list (or facemenu-color-alist
3312 (mapcar 'list (defined-colors))))
3313 (completion (try-completion prefix list)))
3314 (cond ((eq completion t)
3315 (message "Exact match."))
3316 ((null completion)
3317 (error "Can't find completion for \"%s\"" prefix))
3318 ((not (string-equal prefix completion))
3319 (insert-and-inherit (substring completion (length prefix))))
3321 (message "Making completion list...")
3322 (with-output-to-temp-buffer "*Completions*"
3323 (display-completion-list (all-completions prefix list nil)))
3324 (message "Making completion list...done")))))
3326 (defun widget-color-sample-face-get (widget)
3327 (let* ((value (condition-case nil
3328 (widget-value widget)
3329 (error (widget-get widget :value))))
3330 (symbol (intern (concat "fg:" value))))
3331 (condition-case nil
3332 (facemenu-get-face symbol)
3333 (error 'default))))
3335 (defun widget-color-action (widget &optional event)
3336 ;; Prompt for a color.
3337 (let* ((tag (widget-apply widget :menu-tag-get))
3338 (prompt (concat tag ": "))
3339 (value (widget-value widget))
3340 (start (widget-field-start widget))
3341 (pos (cond ((< (point) start)
3343 ((> (point) (+ start (length value)))
3344 (length value))
3346 (- (point) start))))
3347 (answer (facemenu-read-color prompt)))
3348 (unless (zerop (length answer))
3349 (widget-value-set widget answer)
3350 (widget-setup)
3351 (widget-apply widget :notify widget event))))
3353 (defun widget-color-notify (widget child &optional event)
3354 "Update the sample, and notofy the parent."
3355 (overlay-put (widget-get widget :sample-overlay)
3356 'face (widget-apply widget :sample-face-get))
3357 (widget-default-notify widget child event))
3359 ;;; The Help Echo
3361 (defun widget-at (&optional pos)
3362 "The button or field at POS (default, point)."
3363 (unless pos
3364 (setq pos (point)))
3365 (or (get-char-property pos 'button)
3366 (get-char-property pos 'field)))
3368 (defun widget-echo-help (pos)
3369 "Display the help echo for widget at POS."
3370 (let* ((widget (widget-at pos))
3371 (help-echo (and widget (widget-get widget :help-echo))))
3372 (if (or (stringp help-echo)
3373 (and (functionp help-echo)
3374 ;; Kluge: help-echo originally could be a function of
3375 ;; one arg -- the widget. It is more useful in Emacs
3376 ;; 21 to have it as a function usable also as a
3377 ;; help-echo property, when it can sort out its own
3378 ;; widget if necessary. Try both calling sequences
3379 ;; (rather than messing around to get the function's
3380 ;; arity).
3381 (stringp
3382 (setq help-echo
3383 (condition-case nil
3384 (funcall help-echo
3385 (selected-window) (current-buffer)
3386 (point))
3387 (error (funcall help-echo widget))))))
3388 (stringp (eval help-echo)))
3389 (message "%s" help-echo))))
3391 ;;; The End:
3393 (provide 'wid-edit)
3395 ;;; wid-edit.el ends here