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