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