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