1 ;;; wid-edit.el --- Functions for creating and using widgets.
3 ;; Copyright (C) 2007 Didier Verna
4 ;; Copyright (C) 1996-1997, 1999-2002 Free Software Foundation, Inc.
6 ;; Author: Per Abrahamsen <abraham@dina.kvl.dk>
7 ;; Maintainer: Didier Verna <didier@xemacs.org>
8 ;; Keywords: extensions
10 ;; X-URL: http://www.dina.kvl.dk/~abraham/custom/
12 ;; This file is part of SXEmacs.
14 ;; SXEmacs is free software: you can redistribute it and/or modify
15 ;; it under the terms of the GNU General Public License as published by
16 ;; the Free Software Foundation, either version 3 of the License, or
17 ;; (at your option) any later version.
19 ;; SXEmacs is distributed in the hope that it will be useful,
20 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
21 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 ;; GNU General Public License for more details.
24 ;; You should have received a copy of the GNU General Public License
25 ;; along with this program. If not, see <http://www.gnu.org/licenses/>.
34 (globally-declare-fboundp 'debug))
38 ;; XEmacs: autoload of `finder-commentary' is redundant.
43 "Customization support for the Widget Library."
44 :link '(custom-manual "(widget)Top")
45 :link '(url-link :tag "Development Page"
46 "http://www.dina.kvl.dk/~abraham/custom/")
47 :link '(emacs-library-link :tag "Lisp File" "widget.el")
52 (defgroup widget-documentation nil
53 "Options controlling the display of documentation strings."
56 (defgroup widget-faces nil
57 "Faces used by the widget library."
61 (defvar widget-documentation-face 'widget-documentation-face
62 "Face used for documentation strings in widgets.
63 This exists as a variable so it can be set locally in certain buffers.")
65 (defface widget-documentation-face '((((class color)
67 (:foreground "lime green"))
70 (:foreground "dark green"))
72 "Face used for documentation text."
73 :group 'widget-documentation
76 (defvar widget-button-face 'widget-button-face
77 "Face used for buttons in widgets.
78 This exists as a variable so it can be set locally in certain buffers.")
80 (defface widget-button-face '((t (:bold t)))
81 "Face used for widget buttons."
84 (defcustom widget-mouse-face 'highlight
85 "Face used for widget buttons when the mouse is above them."
89 ;; #### comment from GNU Emacs 21.3.50, test the first spec.
90 ;; TTY gets special definitions here and in the next defface, because
91 ;; the gray colors defined for other displays cause black text on a black
92 ;; background, at least on light-background TTYs.
93 (defface widget-field-face '(
94 ;; #### sjt sez: XEmacs doesn't like this.
95 ;; The Custom face editor widget shows a Lisp
96 ;; form, not a face structure. Does it produce
97 ;; the right face on TTYs?
98 ;; One hypothesis is that the editor doesn't
99 ;; grok non-default display types in the value.
101 (:background "yellow3" :foreground "black"))
102 (((class grayscale color)
104 (:background "gray85"))
105 (((class grayscale color)
107 (:background "dim gray"))
110 "Face used for editable fields."
111 :group 'widget-faces)
114 ;(defface widget-single-line-field-face '((((class grayscale color)
115 ; (background light))
116 ; (:background "gray85"))
117 ; (((class grayscale color)
119 ; (:background "dim gray"))
122 ; "Face used for editable fields spanning only a single line."
123 ; :group 'widget-faces)
125 ;(defvar widget-single-line-display-table
126 ; (let ((table (make-display-table)))
127 ; (aset table 9 "^I")
128 ; (aset table 10 "^J")
130 ; "Display table used for single-line editable fields.")
132 ;(set-face-display-table 'widget-single-line-field-face
133 ; widget-single-line-display-table)
136 ;; Some functions from this file have been ported to C for speed.
137 ;; Setting this to t (*before* loading wid-edit.el) will make them
138 ;; shadow the subrs. It should be used only for debugging purposes.
139 (defvar widget-shadow-subrs nil)
142 ;;; Utility functions.
144 ;; These are not really widget specific.
146 (when (or (not (fboundp 'widget-plist-member))
148 ;; Recoded in C, for efficiency. It used to be a defsubst, but old
149 ;; compiled code won't fail -- it will just be slower.
150 (defun widget-plist-member (plist prop)
151 ;; Return non-nil if PLIST has the property PROP.
152 ;; PLIST is a property list, which is a list of the form
153 ;; (PROP1 VALUE1 PROP2 VALUE2 ...). PROP is a symbol.
154 ;; Unlike `plist-get', this allows you to distinguish between a missing
155 ;; property and a property with the value nil.
156 ;; The value is actually the tail of PLIST whose car is PROP.
157 (while (and plist (not (eq (car plist) prop)))
158 (setq plist (cddr plist)))
161 (defsubst widget-princ-to-string (object)
162 "Return string representation of OBJECT, any Lisp object.
164 No quoting characters or string delimiters are used."
165 ;(with-current-buffer (get-buffer-create " *widget-tmp*")
167 ; (princ object (current-buffer))
169 (prin1-to-string object t)
172 (defun widget-prettyprint-to-string (object)
173 "Use `cl-prettyprint' to generate a string representation of OBJECT.
175 Cleans up `cl-prettyprint''s gratuitous surrounding newlines."
177 (cl-prettyprint object)
178 ;; `cl-prettyprint' always surrounds the text with newlines.
179 (buffer-string (if (eq (char-after (point-min)) ?\n)
182 (if (eq (char-before (point-max)) ?\n)
186 (defun widget-clear-undo ()
187 "Clear all undo information."
188 (buffer-disable-undo)
189 (buffer-enable-undo))
191 (defun widget-sublist (list start &optional end)
192 "Return the sublist of LIST from START to END.
193 If END is omitted, it defaults to the length of LIST."
194 (if (> start 0) (setq list (nthcdr start list)))
198 (setq list (copy-sequence list))
199 (setcdr (nthcdr (- end start 1) list) nil)
201 (copy-sequence list)))
203 ;; Is unimplemented the right superclass?
204 (define-error 'missing-package "Package not installed" 'unimplemented)
206 (defcustom widget-menu-max-size 40
207 "Largest number of items allowed in a popup-menu.
208 Larger menus are read through the minibuffer."
212 (defcustom widget-menu-max-shortcuts 40
213 "Largest number of items for which it works to choose one with a character.
214 For a larger number of items, the minibuffer is used.
215 #### Not yet implemented in XEmacs."
219 (defcustom widget-menu-minibuffer-flag nil
220 "*Control how to ask for a choice from the keyboard.
221 Non-nil means use the minibuffer;
222 nil means read a single character."
226 (defun widget-choose (title items &optional event)
227 "Choose an item from a list.
229 First argument TITLE is the name of the list.
230 Second argument ITEMS is a list whose members are either
231 (NAME . VALUE), to indicate selectable items, or just strings to
232 indicate unselectable items.
233 Optional third argument EVENT is an input event.
235 The user is asked to choose a NAME from the items alist, and the VALUE of
236 the chosen element will be returned. If EVENT is a mouse event, and the
237 number of elements in items is less than `widget-menu-max-size', a popup
238 menu will be used, otherwise the minibuffer is used."
239 (cond ((and (< (length items) widget-menu-max-size)
241 (console-on-window-system-p))
242 ;; Pressed by the mouse.
243 (let ((val (get-popup-menu-response
250 (list (car x)) ; 'eval 'quote
254 (message "%s" menu-thingee)
258 (listp (event-object val))
259 (stringp (car-safe (event-object val)))
260 (car (event-object val))))
261 (cdr (assoc val items))))
262 ((and (not widget-menu-minibuffer-flag)
263 ;; Can't handle more than 10 items (as many digits)
264 (<= (length items) 10))
265 ;; Construct a menu of the choices
266 ;; and then use it for prompting for a single character.
267 (let* ((overriding-terminal-local-map (make-sparse-keymap))
268 (map (make-sparse-keymap title))
270 some-choice-enabled value)
271 ;; Define SPC as a prefix char to get to this menu.
272 (define-key overriding-terminal-local-map " " map)
273 (with-current-buffer (get-buffer-create " widget-choose")
275 (insert "Available choices:\n\n")
276 (dolist (choice items)
278 (let* ((name (car choice))
279 (function (cdr choice)))
280 (insert (format "%c = %s\n" next-digit name))
281 (define-key map (vector next-digit) function)
282 (setq some-choice-enabled t)))
283 ;; Allocate digits to disabled alternatives
284 ;; so that the digit of a given alternative never varies.
286 (insert "\nC-g = Quit"))
287 (or some-choice-enabled
288 (error "None of the choices is currently meaningful"))
289 (define-key map [?\C-g] 'keyboard-quit)
290 (define-key map [t] 'keyboard-quit)
291 ;(setcdr map (nreverse (cdr map)))
292 ;; Unread a SPC to lead to our new menu.
293 (push (character-to-event ?\ ) unread-command-events)
294 ;; Read a char with the menu, and return the result
295 ;; that corresponds to it.
296 (save-window-excursion
297 (display-buffer (get-buffer " widget-choose"))
298 (let ((cursor-in-echo-area t))
300 (lookup-key overriding-terminal-local-map
301 (read-key-sequence (concat title ": ") t)))))
303 (when (or (eq value 'keyboard-quit)
308 ;; Read the choice of name from the minibuffer.
309 (setq items (remove-if 'stringp items))
310 (let ((val (completing-read (concat title ": ") items nil t)))
312 (let ((try (try-completion val items)))
315 (cdr (assoc val items)))
318 ;; GNU Emacs 21.3.50 uses this in `widget-choose'
319 (defun widget-remove-if (predicate list)
320 (let (result (tail list))
322 (or (funcall predicate (car tail))
323 (setq result (cons (car tail) result)))
324 (setq tail (cdr tail)))
327 (defun widget-move-and-invoke (event)
328 "Move to where you click, and if it is an active field, invoke it."
330 (mouse-set-point event)
331 (let ((pos (event-point event)))
332 (if (and pos (get-char-property pos 'button))
333 (widget-button-click event))))
335 ;;; Widget text specifications.
337 ;; These functions are for specifying text properties.
339 ;; XEmacs: This probably should be unnecessary with end-closed extents.
340 ;; If it doesn't work, it should be made to work.
341 (defcustom widget-field-add-space t
342 ;; Setting this to nil might be available, once some problems are resolved.
343 "Non-nil means add extra space at the end of editable text fields.
345 Currently should be left set to `t', because without the space it becomes
346 impossible to edit a zero size field."
350 ;; #### Why aren't these used in XEmacs?
351 (defcustom widget-field-use-before-change
352 (and (or (> emacs-minor-version 34)
353 (> emacs-major-version 19))
354 (not (string-match "XEmacs" emacs-version)))
355 "Non-nil means use `before-change-functions' to track editable fields.
356 This enables the use of undo, but doesn't work on Emacs 19.34 and earlier.
357 Using before hooks also means that the :notify function can't know the
362 (defun widget-echo-this-extent (extent)
363 (let* ((widget (or (extent-property extent 'button)
364 (extent-property extent 'field)
365 (extent-property extent 'glyph-widget)))
366 (help-echo (and widget (widget-get widget :help-echo))))
367 (and (functionp help-echo)
368 (setq help-echo (funcall help-echo widget)))
369 (when (stringp help-echo)
370 (setq help-echo-owns-message t)
371 (display-message 'help-echo help-echo))))
373 (defsubst widget-handle-help-echo (extent help-echo)
374 (set-extent-property extent 'balloon-help help-echo)
375 (set-extent-property extent 'help-echo help-echo)
376 (when (functionp help-echo)
377 (set-extent-property extent 'balloon-help 'widget-echo-this-extent)
378 (set-extent-property extent 'help-echo 'widget-echo-this-extent)))
380 (defun widget-specify-field (widget from to)
381 "Specify editable button for WIDGET between FROM and TO."
384 (cond ((null (widget-get widget :size))
386 ;; XEmacs: This comment goes outside of the save-excursion in GNU.
387 ;; Terminating space is not part of the field, but necessary in
388 ;; order for local-map to work. Remove next sexp if local-map works
389 ;; at the end of the extent.
390 (widget-field-add-space
391 (insert-and-inherit " ")))
393 (let ((map (widget-get widget :keymap))
394 (face (or (widget-get widget :value-face) 'widget-field-face))
395 (help-echo (widget-get widget :help-echo))
396 (extent (make-extent from to)))
397 (unless (or (stringp help-echo) (null help-echo))
398 (setq help-echo 'widget-mouse-help))
399 (widget-put widget :field-extent extent)
400 (and (or (not widget-field-add-space)
401 (widget-get widget :size))
402 (set-extent-property extent 'end-closed nil))
403 (set-extent-property extent 'detachable nil)
404 (set-extent-property extent 'field widget)
405 (set-extent-property extent 'button-or-field t)
406 (set-extent-property extent 'keymap map)
407 (set-extent-property extent 'face face)
408 (widget-handle-help-echo extent help-echo))
409 (widget-specify-secret widget))
411 (defun widget-specify-secret (field)
412 "Replace text in FIELD with value of the `:secret' property, if non-nil.
414 The value of the `:secret' property, if non-nil, must be a character.
415 It is an error to use this function after creating the widget but before
416 invoking `widget-setup'."
417 (let ((secret (widget-get field :secret))
418 (size (widget-get field :size)))
420 (let ((begin (widget-field-start field))
421 (end (widget-field-end field)))
423 (while (and (> end begin)
424 (eq (char-after (1- end)) ?\ ))
425 (setq end (1- end))))
427 (let ((old (char-after begin)))
428 (unless (eq old secret)
429 (subst-char-in-region begin (1+ begin) old secret)
430 (put-text-property begin (1+ begin) 'secret old))
431 (setq begin (1+ begin))))))))
433 (defun widget-specify-button (widget from to)
434 "Specify button for WIDGET between FROM and TO."
435 (let ((face (widget-apply widget :button-face-get))
436 (help-echo (widget-get widget :help-echo))
437 (extent (make-extent from to))
438 (map (widget-get widget :button-keymap)))
439 (widget-put widget :button-extent extent)
440 (unless (or (null help-echo) (stringp help-echo))
441 (setq help-echo 'widget-mouse-help))
442 (set-extent-property extent 'start-open t)
443 (set-extent-property extent 'button widget)
444 (set-extent-property extent 'button-or-field t)
445 (set-extent-property extent 'mouse-face widget-mouse-face)
446 (widget-handle-help-echo extent help-echo)
447 (set-extent-property extent 'face face)
448 (set-extent-property extent 'keymap map)))
450 (defun widget-mouse-help (extent)
451 "Find mouse help string for button in extent."
452 (let* ((widget (widget-at (extent-start-position extent)))
453 (help-echo (and widget (widget-get widget :help-echo))))
454 (cond ((stringp help-echo)
456 ((and (functionp help-echo)
457 (stringp (setq help-echo (funcall help-echo widget))))
460 (format "(widget %S :help-echo %S)" widget help-echo)))))
462 (defun widget-specify-sample (widget from to)
463 "Specify sample for WIDGET between FROM and TO."
464 (let ((face (widget-apply widget :sample-face-get))
465 (extent (make-extent from to nil)))
466 (set-extent-property extent 'start-open t)
467 (set-extent-property extent 'face face)
468 (widget-put widget :sample-extent extent)))
470 (defun widget-specify-doc (widget from to)
471 "Specify documentation for WIDGET between FROM and TO."
472 (let ((extent (make-extent from to)))
473 (set-extent-property extent 'start-open t)
474 (set-extent-property extent 'widget-doc widget)
475 (set-extent-property extent 'face widget-documentation-face)
476 (widget-put widget :doc-extent extent)))
478 (defmacro widget-specify-insert (&rest form)
479 "Execute FORM without inheriting any text properties."
481 (let ((inhibit-read-only t)
482 before-change-functions
483 after-change-functions)
485 (narrow-to-region (- (point) 2) (point))
486 (goto-char (1+ (point-min)))
487 ;; XEmacs: use `prog1' instead of a `result' variable. The latter
488 ;; confuses the byte-compiler in some cases (a warning).
489 (prog1 (progn ,@form)
490 (delete-region (point-min) (1+ (point-min)))
491 (delete-region (1- (point-max)) (point-max))
492 (goto-char (point-max))))))
494 (put 'widget-specify-insert 'edebug-form-spec '(&rest form))
497 ;;; Inactive Widgets.
499 (defface widget-inactive-face '((((class grayscale color)
501 (:foreground "light gray"))
502 (((class grayscale color)
504 (:foreground "dim gray"))
507 "Face used for inactive widgets."
508 :group 'widget-faces)
510 ;; For inactiveness to work on complex structures, it is not
511 ;; sufficient to keep track of whether a button/field/glyph is
512 ;; inactive or not -- we must know how many time it was deactivated
513 ;; (inactiveness level). Successive deactivations of the same button
514 ;; increment its inactive-count, and activations decrement it. When
515 ;; inactive-count reaches 0, the button/field/glyph is reactivated.
517 (defun widget-activation-widget-mapper (extent action)
518 "Activate or deactivate EXTENT's widget (button or field).
519 Suitable for use with `map-extents'."
522 (decf (extent-property extent :inactive-count))
523 (when (zerop (extent-property extent :inactive-count))
524 (set-extent-properties
525 extent (extent-property extent :inactive-plist))
526 (set-extent-property extent :inactive-plist nil)))
528 (incf (extent-property extent :inactive-count 0))
529 ;; Store a plist of old properties, which will be fed to
530 ;; `set-extent-properties'.
531 (unless (extent-property extent :inactive-plist)
533 extent :inactive-plist
534 (list 'mouse-face (extent-property extent 'mouse-face)
535 'help-echo (extent-property extent 'help-echo)
536 'keymap (extent-property extent 'keymap)))
537 (set-extent-properties
538 extent '(mouse-face nil help-echo nil keymap nil)))))
541 (defun widget-activation-glyph-mapper (extent action)
542 (let ((activate-p (if (eq action :activate) t nil)))
544 (decf (extent-property extent :inactive-count))
545 (incf (extent-property extent :inactive-count 0)))
546 (when (or (and activate-p
547 (zerop (extent-property extent :inactive-count)))
548 (and (not activate-p)
549 (not (zerop (extent-property extent :inactive-count)))))
550 (let* ((glyph-widget (extent-property extent 'glyph-widget))
551 (up-glyph (widget-get glyph-widget :glyph-up))
552 (inactive-glyph (widget-get glyph-widget :glyph-inactive))
553 (instantiator (widget-get glyph-widget :glyph-instantiator))
554 (new-glyph (if activate-p up-glyph inactive-glyph)))
556 ;; Assume that an instantiator means a native widget.
559 (set-instantiator-property instantiator :active activate-p))
560 (widget-put glyph-widget :glyph-instantiator instantiator)
561 (set-glyph-image up-glyph instantiator))
562 ;; Check that the new glyph exists, and differs from the
564 ((and up-glyph inactive-glyph (not (eq up-glyph inactive-glyph))
565 ;; Check if the glyph is already installed.
566 (not (eq (extent-end-glyph extent) new-glyph)))
568 (set-extent-end-glyph extent new-glyph))))))
571 (defun widget-specify-inactive (widget from to)
572 "Make WIDGET inactive for user modifications."
573 (unless (widget-get widget :inactive)
574 (let ((extent (make-extent from to)))
575 ;; It is no longer necessary for the extent to be read-only, as
576 ;; the inactive editable fields now lose their keymaps.
577 (set-extent-properties
578 extent '(start-open t face widget-inactive-face
579 detachable t priority 2001 widget-inactive t))
580 (widget-put widget :inactive extent))
581 ;; Deactivate the buttons and fields within the range. In some
582 ;; cases, the fields are not yet setup at the time this function
583 ;; is called. Those fields are deactivated explicitly by
585 (map-extents 'widget-activation-widget-mapper
586 nil from to :deactivate nil 'button-or-field)
587 ;; Deactivate glyphs.
588 (map-extents 'widget-activation-glyph-mapper
589 nil from to :deactivate nil 'glyph-widget)))
591 (defun widget-specify-active (widget)
592 "Make WIDGET active for user modifications."
593 (let ((inactive (widget-get widget :inactive))
594 (from (widget-get widget :from))
595 (to (widget-get widget :to)))
596 (when (and inactive (not (extent-detached-p inactive)))
597 ;; Reactivate the buttons and fields covered by the extent.
598 (map-extents 'widget-activation-widget-mapper
599 nil from to :activate nil 'button-or-field)
600 ;; Reactivate the glyphs.
601 (map-extents 'widget-activation-glyph-mapper
602 nil from to :activate nil 'end-glyph)
603 (delete-extent inactive)
604 (widget-put widget :inactive nil))))
607 ;;; Widget Properties.
609 (defsubst widget-type (widget)
610 "Return the type of WIDGET, a symbol."
614 (defun widgetp (widget)
615 "Return non-nil iff WIDGET is a widget."
617 (get widget 'widget-type)
619 (symbolp (car widget))
620 (get (car widget) 'widget-type))))
622 (when (or (not (fboundp 'widget-put))
624 (defun widget-put (widget property value)
625 "In WIDGET set PROPERTY to VALUE.
626 The value can later be retrieved with `widget-get'."
627 (setcdr widget (plist-put (cdr widget) property value))))
629 ;; Recoded in C, for efficiency:
630 (when (or (not (fboundp 'widget-get))
632 (defun widget-get (widget property)
633 "In WIDGET, get the value of PROPERTY.
634 The value may have been specified when the widget was created, or
635 later with `widget-put'."
639 (cond ((setq tmp (widget-plist-member (cdr widget) property))
640 (setq value (car (cdr tmp))
642 ((setq tmp (car widget))
643 (setq widget (get tmp 'widget-type)))
645 (setq missing nil))))
648 (defun widget-get-indirect (widget property)
649 "In WIDGET, get the value of PROPERTY.
650 If the value is a symbol, return its binding.
651 Otherwise, just return the value."
652 (let ((value (widget-get widget property)))
657 (defun widget-member (widget property)
658 "Non-nil iff there is a definition in WIDGET for PROPERTY."
659 (cond ((widget-plist-member (cdr widget) property)
662 (widget-member (get (car widget) 'widget-type) property))
665 (when (or (not (fboundp 'widget-apply))
667 ;;This is in C, so don't ###utoload
668 (defun widget-apply (widget property &rest args)
669 "Apply the value of WIDGET's PROPERTY to the widget itself.
670 ARGS are passed as extra arguments to the function."
671 (apply (widget-get widget property) widget args)))
673 (defun widget-value (widget)
674 "Extract the current value of WIDGET."
676 :value-to-external (widget-apply widget :value-get)))
678 (defun widget-value-set (widget value)
679 "Set the current value of WIDGET to VALUE."
681 :value-set (widget-apply widget
682 :value-to-internal value)))
684 (defun widget-default-get (widget)
685 "Extract the default value of WIDGET."
686 (or (widget-get widget :value)
687 (widget-apply widget :default-get)))
689 (defun widget-match-inline (widget vals)
690 "In WIDGET, match the start of VALS."
691 (cond ((widget-get widget :inline)
692 (widget-apply widget :match-inline vals))
694 (widget-apply widget :match (car vals)))
695 (cons (list (car vals)) (cdr vals)))
698 (defun widget-apply-action (widget &optional event)
699 "Apply :action in WIDGET in response to EVENT."
700 (if (widget-apply widget :active)
701 (widget-apply widget :action event)
702 (error "Attempt to perform action on inactive widget")))
705 ;;; Helper functions.
707 ;; These are widget specific.
709 ;; #### Note: this should probably be a more general utility -- dvl
710 (defsubst widget-prompt-spaceify (prompt)
711 ;; Add a space at the end of PROMPT if needed
712 (if (or (string= prompt "") (eq ? (aref prompt (1- (length prompt)))))
714 (concat prompt " ")))
716 (defsubst widget-prompt (widget &optional prompt default-prompt)
717 ;; Construct a prompt for WIDGET.
718 ;; - If PROMPT is given, use it.
719 ;; - Otherwise, use the :tag property, if any.
720 ;; - Otherwise, use DEFAULT-PROMPT, if given.
721 ;; - Otherise, use "Value".
722 ;; - If the result is not the empty string, add a space for later addition
723 ;; of the widget type by `widget-prompt-value'.
725 (setq prompt (or (and (widget-get widget :tag)
726 (replace-in-string (widget-get widget :tag)
730 (widget-prompt-spaceify prompt))
733 (defun widget-prompt-value (widget &optional prompt value unbound)
734 "Prompt for a value matching WIDGET.
735 Prompt with PROMPT, or WIDGET's :tag otherwise.
736 The current value is assumed to be VALUE, unless UNBOUND is non-nil."
737 (unless (listp widget)
738 (setq widget (list widget)))
739 (setq widget (widget-convert widget))
740 (let ((answer (widget-apply widget
743 (widget-prompt widget prompt)
744 (widget-type widget))
746 (while (not (widget-apply widget :match answer))
747 (setq answer (signal 'error (list "Answer does not match type"
748 answer (widget-type widget)))))
751 (defun widget-get-sibling (widget)
752 "Get the item WIDGET is assumed to toggle.
753 This is only meaningful for radio buttons or checkboxes in a list."
754 (let* ((children (widget-get (widget-get widget :parent) :children))
758 (setq child (car children)
759 children (cdr children))
760 (when (eq (widget-get child :button) widget)
761 (throw 'child child)))
764 (defun widget-map-buttons (function &optional buffer maparg)
765 "Map FUNCTION over the buttons in BUFFER.
766 FUNCTION is called with the arguments WIDGET and MAPARG.
768 If FUNCTION returns non-nil, the walk is cancelled.
770 The arguments MAPARG, and BUFFER default to nil and (current-buffer),
772 (map-extents (lambda (extent ignore)
773 ;; If FUNCTION returns non-nil, we bail out
774 (funcall function (extent-property extent 'button) maparg))
781 (defcustom widget-glyph-directory (locate-data-directory "custom")
782 "Where widget button glyphs are located.
783 If this variable is nil, widget will try to locate the directory
788 (defcustom widget-glyph-enable t
789 "If non nil, use glyph buttons in widgets when available."
793 ;; #### What happens if you try to customize this?
794 (define-compatible-variable-alias 'widget-image-conversion
795 'widget-image-file-name-suffixes)
797 (defcustom widget-image-file-name-suffixes
798 '((xpm ".xpm") (gif ".gif") (png ".png") (jpeg ".jpg" ".jpeg")
800 "Conversion alist from image formats to file name suffixes."
802 :type '(repeat (cons :format "%v"
803 (symbol :tag "Image Format" unknown)
804 (repeat :tag "Suffixes"
805 (string :format "%v")))))
807 ;; Don't use this, because we cannot yet distinguish between widget
808 ;; glyphs associated with user action, and actionless ones.
809 ;(defvar widget-glyph-pointer-glyph
810 ; (make-pointer-glyph [cursor-font :data "hand2"])
811 ; "Glyph to be used as the mouse pointer shape over glyphs.
812 ;Use `set-glyph-image' to change this.")
814 (defvar widget-glyph-cache nil
815 "Cache of glyphs associated with strings (files).")
817 (defun widget-glyph-find (image tag)
818 "Create a glyph corresponding to IMAGE with string TAG as fallback.
819 IMAGE can already be a glyph, or a file name sans extension (xpm,
820 xbm, gif, jpg, or png) located in `widget-glyph-directory', or
821 in one of the data directories.
822 It can also be a valid image instantiator, in which case it will be
823 used to make the glyph, with an additional TAG string fallback."
824 (cond ((not (and image widget-glyph-enable))
825 ;; We don't want to use glyphs.
827 ((and (not (console-on-window-system-p))
828 ;; We don't use glyphs on TTY consoles, although we
829 ;; could. However, glyph faces aren't yet working
830 ;; properly, and movement through glyphs is unintuitive.
831 ;; As an exception, when TAG is nil, we assume that the
832 ;; caller knows what he is doing, and that the tag is
833 ;; encoded within the glyph.
834 (not (glyphp image)))
837 ;; Already a glyph. Use it.
840 ;; A string. Look it up in the cache first...
841 (or (lax-plist-get widget-glyph-cache image)
842 ;; ...and then in the relevant directories
843 (let* ((dirlist (cons (or widget-glyph-directory
844 (locate-data-directory "custom"))
845 data-directory-list))
850 (and (valid-image-instantiator-format-p (car el))
852 widget-image-file-name-suffixes)))
853 (file (locate-file image dirlist all-suffixes)))
855 (let* ((extension (concat "." (file-name-extension file)))
856 (format (car (rassoc* extension
857 widget-image-file-name-suffixes
859 ;; We create a glyph with the file as the default image
860 ;; instantiator, and the TAG fallback
861 (let ((glyph (make-glyph `([,format :file ,file]
862 [string :data ,tag]))))
864 (laxputf widget-glyph-cache image glyph)
867 ((valid-instantiator-p image 'image)
868 ;; A valid image instantiator (e.g. [gif :file "somefile"] etc.)
869 (make-glyph `(,image [string :data ,tag])))
874 (defun widget-glyph-insert (widget tag image &optional down inactive)
875 "In WIDGET, insert the text TAG or, if supported, IMAGE.
876 IMAGE should either be a glyph, an image instantiator, an image file
877 name sans extension (xpm, xbm, gif, jpg, or png) located in
878 `widget-glyph-directory', or anything else allowed by
881 If IMAGE is a list, it will be taken as a list of (UP DOWN INACTIVE)
882 glyphs. The down and inactive glyphs are shown when glyph is pressed
883 or inactive, respectively.
885 The optional DOWN and INACTIVE arguments are deprecated, and exist
886 only because of compatibility."
887 ;; Convert between IMAGE being a list, etc. Must use `psetq',
888 ;; because otherwise change to `image' screws up the rest.
889 (psetq image (or (and (consp image)
892 down (or (and (consp image)
895 inactive (or (and (consp image)
898 (let ((glyph (widget-glyph-find image tag)))
900 (widget-glyph-insert-glyph widget glyph
901 (widget-glyph-find down tag)
902 (widget-glyph-find inactive tag))
906 (defun widget-glyph-insert-glyph (widget glyph &optional down inactive
908 "In WIDGET, insert GLYPH.
909 If optional arguments DOWN and INACTIVE are given, they should be
910 glyphs used when the widget is pushed and inactive, respectively.
911 INSTANTIATOR is the vector used to create the glyph."
913 (let ((extent (make-extent (point) (1- (point))))
914 (help-echo (and widget (widget-get widget :help-echo)))
915 (map (and widget (widget-get widget :button-keymap))))
916 (set-extent-property extent 'glyph-widget widget)
917 ;; It would be fun if we could make this extent atomic, so it
918 ;; doesn't mess with cursor motion. But atomic-extents library is
919 ;; currently a mess, so I'd rather not use it.
920 (set-extent-property extent 'invisible t)
921 (set-extent-property extent 'start-open t)
922 (set-extent-property extent 'end-open t)
923 (set-extent-property extent 'keymap map)
924 ;;(set-extent-property extent 'pointer widget-glyph-pointer-glyph)
925 (set-extent-end-glyph extent glyph)
926 (unless (or (stringp help-echo) (null help-echo))
927 (setq help-echo 'widget-mouse-help))
929 (widget-handle-help-echo extent help-echo)))
931 (widget-put widget :glyph-up glyph)
932 (when down (widget-put widget :glyph-down down))
933 (when instantiator (widget-put widget :glyph-instantiator instantiator))
934 (when inactive (widget-put widget :glyph-inactive inactive))))
939 (defgroup widget-button nil
940 "The look of various kinds of buttons."
943 (defcustom widget-button-prefix ""
944 "String used as prefix for buttons."
946 :group 'widget-button)
948 (defcustom widget-button-suffix ""
949 "String used as suffix for buttons."
951 :group 'widget-button)
954 ;;; Creating Widgets.
957 (defun widget-create (type &rest args)
958 "Create a widget of type TYPE.
960 TYPE is copied, then converted to a widget using the keyword arguments ARGS."
961 (let ((widget (apply 'widget-convert type args)))
962 (widget-apply widget :create)
965 (defun widget-create-child-and-convert (parent type &rest args)
966 "As a child of widget PARENT, create a widget of type TYPE.
968 TYPE is copied, then converted to a widget using the keyword arguments ARGS."
969 (let ((widget (apply 'widget-convert type args)))
970 (widget-put widget :parent parent)
971 (unless (widget-get widget :indent)
972 (widget-put widget :indent (+ (or (widget-get parent :indent) 0)
973 (or (widget-get widget :extra-offset) 0)
974 (widget-get parent :offset))))
975 (widget-apply widget :create)
978 (defun widget-create-child (parent type)
979 "As a child of widget PARENT, create a widget of type TYPE.
981 TYPE is copied, then used as a widget as-is."
982 (let ((widget (copy-sequence type)))
983 (widget-put widget :parent parent)
984 (unless (widget-get widget :indent)
985 (widget-put widget :indent (+ (or (widget-get parent :indent) 0)
986 (or (widget-get widget :extra-offset) 0)
987 (widget-get parent :offset))))
988 (widget-apply widget :create)
991 (defun widget-create-child-value (parent type value)
992 "As a child of widget PARENT, create a widget with type TYPE and value VALUE.
994 TYPE is copied, then used as a widget as-is."
995 (let ((widget (copy-sequence type)))
996 (widget-put widget :value (widget-apply widget :value-to-internal value))
997 (widget-put widget :parent parent)
998 (unless (widget-get widget :indent)
999 (widget-put widget :indent (+ (or (widget-get parent :indent) 0)
1000 (or (widget-get widget :extra-offset) 0)
1001 (widget-get parent :offset))))
1002 (widget-apply widget :create)
1006 (defun widget-delete (widget)
1008 (widget-apply widget :delete))
1010 (defun widget-copy (widget)
1011 "Make a deep copy of WIDGET."
1012 (widget-apply (copy-sequence widget) :copy))
1015 (defun widget-convert (type &rest args)
1016 "Convert TYPE to a widget without inserting it in the buffer.
1017 The optional ARGS are additional keyword arguments.
1019 The widget's :args property is set from the longest tail of ARGS whose cdr
1020 is not a keyword, or from the longest tail of TYPE's :args property whose
1021 cdr is not a keyword. Keyword arguments from ARGS are set, and the :value
1022 property (if any) is converted from external to internal format."
1023 ;; Don't touch the type.
1024 (let* ((widget (if (symbolp type)
1026 (copy-sequence type)))
1029 ;; First set the :args.
1030 (while (cdr current) ; Use first non-keyword element of type.
1031 (let ((next (car (cdr current))))
1033 (setq current (cdr (cdr current)))
1034 (setcdr current (list :args (cdr current)))
1035 (setq current nil))))
1036 (while args ; Use first non-keyword element in ARGS.
1037 (let ((next (nth 0 args)))
1039 (setq args (nthcdr 2 args))
1040 (widget-put widget :args args)
1042 ;; Then convert the widget.
1045 (let ((convert-widget (plist-get (cdr type) :convert-widget)))
1047 (setq widget (funcall convert-widget widget))))
1048 (setq type (get (car type) 'widget-type)))
1049 ;; Finally set the keyword args.
1051 (let ((next (nth 0 keys)))
1054 (widget-put widget next (nth 1 keys))
1055 (setq keys (nthcdr 2 keys)))
1057 ;; Convert the :value to internal format.
1058 (if (widget-member widget :value)
1060 :value (widget-apply widget
1062 (widget-get widget :value))))
1063 ;; Return the newly created widget.
1067 (defun widget-insert (&rest args)
1068 "Call `insert' with ARGS even if surrounding text is read only."
1069 (let ((inhibit-read-only t)
1070 before-change-functions
1071 after-change-functions)
1072 (apply 'insert args)))
1074 (defun widget-convert-text (type from to
1075 &optional button-from button-to
1077 "Return a widget of type TYPE with endpoints FROM and TO.
1078 No text will be inserted in the buffer. Instead the positions FROM and TO
1079 will be used as the widget's end points. The widget is ``wrapped around''
1080 the text between them.
1081 If optional arguments BUTTON-FROM and BUTTON-TO are given, these will be
1082 used as the widget's button end points.
1083 Optional ARGS are extra keyword arguments for TYPE."
1084 (let ((widget (apply 'widget-convert type :delete 'widget-leave-text args))
1085 (from (copy-marker from))
1086 (to (copy-marker to)))
1087 (set-marker-insertion-type from t)
1088 (set-marker-insertion-type to nil)
1089 (widget-put widget :from from)
1090 (widget-put widget :to to)
1092 (widget-specify-button widget button-from button-to))
1095 (defun widget-convert-button (type from to &rest args)
1096 "Return a widget of type TYPE with endpoints FROM and TO.
1097 Optional ARGS are extra keyword arguments for TYPE.
1098 No text will be inserted in the buffer. Instead the positions FROM and TO
1099 will be used as the widget's end points, as well as the widget's button's
1100 end points. The widget is ``wrapped around'' the text between them."
1101 (apply 'widget-convert-text type from to from to args))
1103 (defun widget-leave-text (widget)
1104 "Remove markers and extents from WIDGET and its children."
1105 (let ((from (widget-get widget :from))
1106 (to (widget-get widget :to))
1107 (button (widget-get widget :button-extent))
1108 (sample (widget-get widget :sample-extent))
1109 (doc (widget-get widget :doc-extent))
1110 (field (widget-get widget :field-extent)))
1111 (set-marker from nil)
1113 ;; Maybe we should delete the extents here? As this code doesn't
1114 ;; remove them from widget structures, maybe it's safer to just
1115 ;; detach them. That's what GNU-compatible `delete-overlay' does.
1117 (detach-extent button))
1119 (detach-extent sample))
1121 (detach-extent doc))
1123 (detach-extent field))
1124 (mapc 'widget-leave-text (widget-get widget :children))))
1127 ;;; Keymap and Commands.
1129 (defvar widget-keymap nil
1130 "Keymap containing useful bindings for buffers containing widgets.
1131 Recommended as a parent keymap for modes using widgets.")
1133 (unless widget-keymap
1134 (setq widget-keymap (make-sparse-keymap))
1135 (define-key widget-keymap [tab] 'widget-forward)
1136 (define-key widget-keymap [(shift tab)] 'widget-backward)
1137 (define-key widget-keymap [(meta tab)] 'widget-backward)
1138 (define-key widget-keymap [backtab] 'widget-backward))
1140 (defvar widget-global-map global-map
1141 "Keymap used for events a widget does not handle itself.")
1142 (make-variable-buffer-local 'widget-global-map)
1144 (defvar widget-field-keymap nil
1145 "Keymap used inside an editable field.")
1147 (unless widget-field-keymap
1148 (setq widget-field-keymap (make-sparse-keymap))
1149 (set-keymap-parents widget-field-keymap global-map)
1150 (define-key widget-field-keymap "\C-k" 'widget-kill-line)
1151 (define-key widget-field-keymap [(meta tab)] 'widget-complete)
1152 (define-key widget-field-keymap [tab] 'widget-forward)
1153 (define-key widget-field-keymap [(shift tab)] 'widget-backward)
1154 (define-key widget-field-keymap "\C-m" 'widget-field-activate)
1155 (define-key widget-field-keymap "\C-a" 'widget-beginning-of-line)
1156 (define-key widget-field-keymap "\C-e" 'widget-end-of-line)
1157 (define-key widget-field-keymap "\C-t" 'widget-transpose-chars))
1159 (defvar widget-text-keymap nil
1160 "Keymap used inside a text field.")
1162 (unless widget-text-keymap
1163 (setq widget-text-keymap (make-sparse-keymap))
1164 (set-keymap-parents widget-field-keymap global-map)
1165 (define-key widget-text-keymap "\C-a" 'widget-beginning-of-line)
1166 (define-key widget-text-keymap "\C-e" 'widget-end-of-line)
1167 (define-key widget-text-keymap "\C-t" 'widget-transpose-chars))
1169 (defvar widget-button-keymap nil
1170 "Keymap used inside a button.")
1172 (unless widget-button-keymap
1173 (setq widget-button-keymap (make-sparse-keymap))
1174 (set-keymap-parents widget-button-keymap widget-keymap)
1175 (define-key widget-button-keymap "\C-m" 'widget-button-press)
1176 (define-key widget-button-keymap [button2] 'widget-button-click)
1177 ;; Ideally, button3 within a button should invoke a button-specific
1179 (define-key widget-button-keymap [button3] 'widget-button-click)
1181 (define-key widget-button-keymap [button1] 'widget-button1-click))
1184 (defun widget-field-activate (pos &optional event)
1185 "Invoke the editable field at point."
1187 (let ((field (widget-field-find pos)))
1189 (widget-apply-action field event)
1191 (lookup-key widget-global-map (this-command-keys))))))
1193 (defface widget-button-pressed-face
1195 (:foreground "red"))
1197 (:bold t :underline t)))
1198 "Face used for pressed buttons."
1199 :group 'widget-faces)
1201 (defun widget-event-point (event)
1202 "Character position of the mouse event, or nil."
1203 (and (mouse-event-p event)
1204 (event-point event)))
1206 (defun widget-button-click (event)
1207 "Invoke button under mouse pointer."
1209 (with-current-buffer (event-buffer event)
1210 (cond ((event-glyph event)
1211 (widget-glyph-click event))
1212 ((widget-event-point event)
1213 (let* ((pos (widget-event-point event))
1214 (button (get-char-property pos 'button)))
1216 (let* ((extent (widget-get button :button-extent))
1217 (face (extent-property extent 'face))
1218 (mouse-face (extent-property extent 'mouse-face))
1219 (help-echo (extent-property extent 'help-echo)))
1222 ;; Merge relevant faces, and make the result mouse-face.
1223 (let ((merge `(widget-button-pressed-face ,mouse-face)))
1224 (nconc merge (if (listp face)
1226 (setq merge (delete-if-not 'find-face merge))
1227 (set-extent-property extent 'mouse-face merge))
1228 (unless (widget-apply button :mouse-down-action event)
1229 ;; Wait for button release.
1230 (while (not (button-release-event-p
1231 (setq event (next-event))))
1232 (dispatch-event event)))
1233 ;; Disallow mouse-face and help-echo.
1234 (set-extent-property extent 'mouse-face nil)
1235 (set-extent-property extent 'help-echo nil)
1236 (setq pos (widget-event-point event))
1237 (unless (eq (current-buffer) (extent-object extent))
1238 ;; Barf if dispatch-event tripped us by
1240 (error "Buffer changed during mouse motion"))
1241 ;; Do the associated action.
1242 (when (and pos (extent-in-region-p extent pos pos))
1243 (widget-apply-action button event)))
1244 ;; Unwinding: fully release the button.
1245 (set-extent-property extent 'mouse-face mouse-face)
1246 (set-extent-property extent 'help-echo help-echo)))
1247 ;; This should not happen!
1248 (error "`widget-button-click' called outside button"))))
1250 (message "You clicked somewhere weird")))))
1252 (defun widget-button1-click (event)
1253 "Invoke glyph below mouse pointer."
1255 (if (event-glyph event)
1256 (widget-glyph-click event)
1257 ;; Should somehow avoid this.
1258 (let ((command (lookup-key widget-global-map (this-command-keys))))
1259 (and (commandp command)
1260 (call-interactively command)))))
1262 (defun widget-glyph-click (event)
1263 "Handle click on a glyph."
1264 (let* ((glyph (event-glyph event))
1265 (extent (event-glyph-extent event))
1266 (widget (extent-property extent 'glyph-widget))
1267 (down-glyph (or (and widget (widget-get widget :glyph-down)) glyph))
1268 (up-glyph (or (and widget (widget-get widget :glyph-up)) glyph))
1270 (unless (widget-apply widget :active)
1271 (error "This widget is inactive"))
1272 (let ((current-glyph 'down))
1273 ;; We always know what glyph is drawn currently, to avoid
1274 ;; unnecessary extent changes. Is this any noticeable gain?
1278 (set-extent-end-glyph extent down-glyph)
1279 ;; Redisplay (shouldn't be needed, but...)
1281 (unless (widget-apply widget :mouse-down-action event)
1282 ;; Wait for the release.
1283 (while (not (button-release-event-p last))
1284 (unless (button-press-event-p last)
1285 (dispatch-event last))
1286 (when (motion-event-p last)
1287 ;; Update glyphs on mouse motion.
1288 (if (eq extent (event-glyph-extent last))
1289 (unless (eq current-glyph 'down)
1290 (set-extent-end-glyph extent down-glyph)
1291 (setq current-glyph 'down))
1292 (unless (eq current-glyph 'up)
1293 (set-extent-end-glyph extent up-glyph)
1294 (setq current-glyph 'up))))
1295 (setq last (next-event event))))
1296 (unless (eq (current-buffer) (extent-object extent))
1297 ;; Barf if dispatch-event tripped us by changing buffer.
1298 (error "Buffer changed during mouse motion"))
1299 ;; Apply widget action.
1300 (when (eq extent (event-glyph-extent last))
1301 (let ((widget (extent-property (event-glyph-extent event)
1303 (cond ((null widget)
1304 (message "You clicked on a glyph"))
1305 ((not (widget-apply widget :active))
1306 (error "This glyph is inactive"))
1308 (widget-apply-action widget event))))))
1309 ;; Release the glyph.
1310 (and (eq current-glyph 'down)
1311 ;; The extent might have been detached or deleted
1312 (extent-live-p extent)
1313 (not (extent-detached-p extent))
1314 (set-extent-end-glyph extent up-glyph))))))
1316 (defun widget-button-press (pos &optional event)
1317 "Invoke button at POS."
1319 (let ((button (get-char-property pos 'button)))
1321 (widget-apply-action button event)
1322 (let ((command (lookup-key widget-global-map (this-command-keys))))
1323 (when (commandp command)
1324 (call-interactively command))))))
1326 (defun widget-tabable-at (&optional pos last-tab backwardp)
1327 "Return the tabable widget at POS, or nil.
1328 POS defaults to the value of (point)."
1331 (let ((widget (widget-at pos)))
1333 (let ((order (widget-get widget :tab-order)))
1335 (if last-tab (and (= order (if backwardp
1339 (and (> order 0) widget))
1343 ;; Return the button or field extent at point.
1344 (defun widget-button-or-field-extent (pos)
1345 (or (and (get-char-property pos 'button)
1346 (widget-get (get-char-property pos 'button)
1348 (and (get-char-property pos 'field)
1349 (widget-get (get-char-property pos 'field)
1352 (defun widget-next-button-or-field (pos)
1353 "Find the next button, or field, and return its start position, or nil.
1354 Internal function, don't use it outside `wid-edit'."
1355 (let* ((at-point (widget-button-or-field-extent pos))
1356 (extent (map-extents
1357 (lambda (ext ignore)
1359 nil (if at-point (extent-end-position at-point) pos)
1360 nil nil 'start-open 'button-or-field)))
1362 (extent-start-position extent))))
1364 ;; This is too slow in buffers with many buttons (W3).
1365 (defun widget-previous-button-or-field (pos)
1366 "Find the previous button, or field, and return its start position, or nil.
1367 Internal function, don't use it outside `wid-edit'."
1368 (let* ((at-point (widget-button-or-field-extent pos))
1371 (lambda (ext ignore)
1372 (if (eq ext at-point)
1373 ;; We reached the extent we were on originally
1374 (if (= pos (extent-start-position at-point))
1376 (setq previous-extent at-point))
1377 (setq previous-extent ext)
1379 nil nil pos nil 'start-open 'button-or-field)
1380 (and previous-extent
1381 (extent-start-position previous-extent))))
1383 (defun widget-move (arg)
1384 "Move point to the ARG next field or button.
1385 ARG may be negative to move backward."
1386 (let ((opoint (point)) (wrapped 0)
1387 (last-tab (widget-get (widget-at (point)) :tab-order))
1389 ;; Movement backward
1391 (setq nextpos (widget-previous-button-or-field (point)))
1395 (when (and (not (get-char-property nextpos 'widget-inactive))
1396 (widget-tabable-at nil last-tab t))
1399 last-tab (widget-get (widget-at (point))
1401 (if (and (not found) (> wrapped 1))
1404 (goto-char (point-max))
1408 (setq nextpos (widget-next-button-or-field (point)))
1412 (when (and (not (get-char-property nextpos 'widget-inactive))
1413 (widget-tabable-at nil last-tab))
1416 last-tab (widget-get (widget-at (point))
1418 (if (and (not found) (> wrapped 1))
1421 (goto-char (point-min))
1425 (widget-echo-help (point))
1426 (run-hooks 'widget-move-hook))))
1428 (defun widget-forward (arg)
1429 "Move point to the next field or button.
1430 With optional ARG, move across that many fields."
1432 (run-hooks 'widget-forward-hook)
1435 (defun widget-backward (arg)
1436 "Move point to the previous field or button.
1437 With optional ARG, move across that many fields."
1439 (run-hooks 'widget-backward-hook)
1440 (widget-move (- arg)))
1442 (defun widget-beginning-of-line ()
1443 "Go to beginning of field or beginning of line, whichever is first.
1445 It is an error to use this function after creating the widget but before
1446 invoking `widget-setup'."
1448 (let* ((field (widget-field-find (point)))
1449 (start (and field (widget-field-start field))))
1450 (if (and start (not (eq start (point))))
1452 (call-interactively 'beginning-of-line))))
1454 (defun widget-end-of-line ()
1455 "Go to end of field or end of line, whichever is first.
1457 It is an error to use this function after creating the widget but before
1458 invoking `widget-setup'."
1460 (let* ((field (widget-field-find (point)))
1461 (end (and field (widget-field-end field))))
1462 (if (and end (not (eq end (point))))
1464 (call-interactively 'end-of-line))))
1466 (defun widget-kill-line ()
1467 "Kill to end of field or end of line, whichever is first.
1469 It is an error to use this function after creating the widget but before
1470 invoking `widget-setup'."
1472 (let* ((field (widget-field-find (point)))
1473 (newline (save-excursion (forward-line 1) (point)))
1474 (end (and field (widget-field-end field))))
1475 (if (and field (> newline end))
1476 (kill-region (point) end)
1477 (call-interactively 'kill-line))))
1479 (defun widget-transpose-chars (arg)
1480 "Like `transpose-chars', but works correctly at end of widget."
1482 (let* ((field (widget-field-find (point)))
1483 (start (and field (widget-field-start field)))
1484 (end (and field (widget-field-end field)))
1485 (last-non-space (and start end
1488 (skip-chars-backward " \t\n" start)
1490 (cond ((and last-non-space
1491 (or (= last-non-space start)
1492 (= last-non-space (1+ start))))
1493 ;; empty or one-character field
1496 ;; at the beginning of the field -- we would get an error here.
1497 (error "Cannot transpose at beginning of field"))
1499 (when (and (null arg)
1500 (= last-non-space (point)))
1502 (transpose-chars arg)))))
1504 (defcustom widget-complete-field (lookup-key global-map "\M-\t")
1505 "Default function to call for completion inside fields."
1506 :options '(ispell-complete-word complete-tag lisp-complete-symbol)
1510 (defun widget-complete ()
1511 "Complete content of editable field from point.
1512 When not inside a field, move to the previous button or field."
1514 ;; Somehow, this should make pressing M-TAB twice scroll the
1515 ;; completions window.
1516 (let ((field (widget-field-find (point))))
1518 (widget-apply field :complete)
1519 (error "Not in an editable field"))))
1522 ;;; Setting up the buffer.
1524 (defvar widget-field-new nil
1525 "List of all newly created editable fields in the buffer.")
1526 (make-variable-buffer-local 'widget-field-new)
1528 (defvar widget-field-list nil
1529 "List of all editable fields in the buffer.")
1530 (make-variable-buffer-local 'widget-field-list)
1532 ;; Is this a misnomer?
1533 (defun widget-at (pos)
1534 "The button or field at POS."
1535 (or (get-char-property pos 'button)
1536 (get-char-property pos 'field)))
1539 (defun widget-setup ()
1540 "Setup current buffer so editing string widgets works."
1541 (let ((inhibit-read-only t)
1542 (after-change-functions nil)
1543 before-change-functions
1545 (while widget-field-new
1546 (setq field (car widget-field-new)
1547 widget-field-new (cdr widget-field-new)
1548 widget-field-list (cons field widget-field-list))
1549 (let ((from (car (widget-get field :field-extent)))
1550 (to (cdr (widget-get field :field-extent))))
1551 (widget-specify-field field
1552 (marker-position from) (marker-position to))
1553 (set-marker from nil)
1554 (set-marker to nil))
1555 ;; If the field is placed within the inactive zone, deactivate it.
1556 (let ((extent (widget-get field :field-extent)))
1557 (when (get-char-property (extent-start-position extent)
1559 (widget-activation-widget-mapper extent :deactivate)))))
1561 (widget-add-change))
1563 (defvar widget-field-last nil)
1564 ;; Last field containing point.
1565 (make-variable-buffer-local 'widget-field-last)
1567 (defvar widget-field-was nil)
1568 ;; The widget data before the change.
1569 (make-variable-buffer-local 'widget-field-was)
1571 (defun widget-field-at (pos)
1572 "Return the widget field at POS, or nil if none."
1573 (let ((field (get-char-property (or pos (point)) 'field)))
1574 (if (eq field 'boundary)
1578 (defun widget-field-buffer (widget)
1579 "Return the buffer containing WIDGET.
1581 It is an error to use this function after creating the widget but before
1582 invoking `widget-setup'."
1583 (let ((extent (widget-get widget :field-extent)))
1584 (and extent (extent-object extent))))
1586 (defun widget-field-start (widget)
1587 "Return the start of WIDGET's editing field.
1589 It is an error to use this function after creating the widget but before
1590 invoking `widget-setup'."
1591 (let ((extent (widget-get widget :field-extent)))
1592 (and extent (extent-start-position extent))))
1594 (defun widget-field-end (widget)
1595 "Return the end of WIDGET's editing field.
1597 It is an error to use this function after creating the widget but before
1598 invoking `widget-setup'."
1599 (let ((extent (widget-get widget :field-extent)))
1600 ;; Don't subtract one if local-map works at the end of the extent.
1601 (and extent (if (or widget-field-add-space
1602 (null (widget-get widget :size)))
1603 (1- (extent-end-position extent))
1604 (extent-end-position extent)))))
1606 (defun widget-field-find (pos)
1607 "Return the field at POS.
1608 Unlike (get-char-property POS 'field) this, works with empty fields too.
1610 Warning: using this function after creating the widget but before invoking
1611 `widget-setup' will always fail."
1612 ;; XEmacs: use `map-extents' instead of a while loop
1613 (let ((field-extent (map-extents (lambda (extent ignore)
1615 nil pos pos nil nil 'field)))
1617 (extent-property field-extent 'field))))
1619 ;; Warning: using this function after creating the widget but before
1620 ;; invoking `widget-setup' will always fail.
1621 (defun widget-before-change (from to)
1622 ;; Barf if the text changed is outside the editable fields.
1623 (unless inhibit-read-only
1624 (let ((from-field (widget-field-find from))
1625 (to-field (widget-field-find to)))
1626 (cond ((or (null from-field)
1628 ;; Either end of change is not within a field.
1629 (add-hook 'post-command-hook 'widget-add-change nil t)
1630 (error "Attempt to change text outside editable field"))
1631 ((not (eq from-field to-field))
1632 ;; The change begins in one fields, and ends in another one.
1633 (add-hook 'post-command-hook 'widget-add-change nil t)
1634 (error "Change should be restricted to a single field"))
1635 ((or (and from-field
1636 (get-char-property from 'widget-inactive))
1638 (get-char-property to 'widget-inactive)))
1639 ;; Trying to change an inactive editable field.
1640 (add-hook 'post-command-hook 'widget-add-change nil t)
1641 (error "Attempt to change an inactive field"))
1642 (widget-field-use-before-change
1643 ;; #### Bletch! This loses because XEmacs get confused
1644 ;; if before-change-functions change the contents of
1645 ;; buffer before from/to.
1647 (widget-apply from-field :notify from-field)
1648 (error (declare-fboundp (debug "Before Change")))))))))
1650 (defun widget-add-change ()
1651 (make-local-hook 'post-command-hook)
1652 (remove-hook 'post-command-hook 'widget-add-change t)
1653 (make-local-hook 'before-change-functions)
1654 (add-hook 'before-change-functions 'widget-before-change nil t)
1655 (make-local-hook 'after-change-functions)
1656 (add-hook 'after-change-functions 'widget-after-change nil t))
1658 (defun widget-after-change (from to old)
1659 "Adjust field size and text properties.
1661 Also, notify the widgets (so, for example, a variable changes its
1662 state to `modified'. when it is being edited)."
1664 (let ((field (widget-field-find from))
1665 (other (widget-field-find to)))
1667 (unless (eq field other)
1668 (declare-fboundp (debug "Change in different fields")))
1669 (let ((size (widget-get field :size)))
1671 (let ((begin (widget-field-start field))
1672 (end (widget-field-end field)))
1673 (cond ((< (- end begin) size)
1677 (insert-char ?\ (- (+ begin size) end))))
1678 ((> (- end begin) size)
1679 ;; Field too large and
1680 (if (or (< (point) (+ begin size))
1682 ;; Point is outside extra space.
1683 (setq begin (+ begin size))
1684 ;; Point is within the extra space.
1685 (setq begin (point)))
1688 (while (and (eq (preceding-char) ?\ )
1690 (delete-backward-char 1)))))))
1691 (widget-specify-secret field))
1692 (widget-apply field :notify field)))
1693 (error (declare-fboundp (debug "After Change")))))
1696 ;;; Widget Functions
1698 ;; These functions are used in the definition of multiple widgets.
1700 (defun widget-parent-action (widget &optional event)
1701 "Tell :parent of WIDGET to handle the :action.
1702 Optional EVENT is the event that triggered the action."
1703 (widget-apply (widget-get widget :parent) :action event))
1705 (defun widget-children-value-delete (widget)
1706 "Delete all :children and :buttons in WIDGET."
1707 (mapc 'widget-delete (widget-get widget :children))
1708 (widget-put widget :children nil)
1709 (mapc 'widget-delete (widget-get widget :buttons))
1710 (widget-put widget :buttons nil))
1712 (defun widget-children-validate (widget)
1713 "All the :children must be valid."
1714 (let ((children (widget-get widget :children))
1716 (while (and children (not found))
1717 (setq child (car children)
1718 children (cdr children)
1719 found (widget-apply child :validate)))
1722 (defun widget-types-copy (widget)
1723 "Copy :args as widget types in WIDGET."
1724 (widget-put widget :args (mapcar 'widget-copy (widget-get widget :args)))
1727 ;; Made defsubst to speed up face editor creation.
1728 (defsubst widget-types-convert-widget (widget)
1729 "Convert each member of :args in WIDGET from a widget type to a widget."
1730 (widget-put widget :args (mapcar 'widget-convert (widget-get widget :args)))
1733 (defun widget-value-convert-widget (widget)
1734 "Initialize :value from `(car :args)' in WIDGET, and reset :args."
1735 (let ((args (widget-get widget :args)))
1737 (widget-put widget :value (car args))
1738 ;; Don't convert :value here, as this is done in `widget-convert'.
1739 ;; (widget-put widget :value (widget-apply widget
1740 ;; :value-to-internal (car args)))
1741 (widget-put widget :args nil)))
1744 (defun widget-value-value-get (widget)
1745 "Return the :value property of WIDGET."
1746 (widget-get widget :value))
1748 ;;; The `default' Widget.
1750 (define-widget 'default nil
1751 "Basic widget other widgets are derived from."
1752 :value-to-internal (lambda (widget value) value)
1753 :value-to-external (lambda (widget value) value)
1754 :button-prefix 'widget-button-prefix
1755 :button-suffix 'widget-button-suffix
1756 :complete 'widget-default-complete
1757 :create 'widget-default-create
1760 :format-handler 'widget-default-format-handler
1761 :button-face-get 'widget-default-button-face-get
1762 :sample-face-get 'widget-default-sample-face-get
1763 :button-keymap widget-button-keymap
1764 :delete 'widget-default-delete
1765 :value-set 'widget-default-value-set
1766 :value-inline 'widget-default-value-inline
1767 :default-get 'widget-default-default-get
1768 :menu-tag-get 'widget-default-menu-tag-get
1770 :active 'widget-default-active
1771 :activate 'widget-specify-active
1772 :deactivate 'widget-default-deactivate
1773 :mouse-down-action #'ignore
1774 :action 'widget-default-action
1775 :notify 'widget-default-notify
1776 :prompt-value 'widget-default-prompt-value)
1778 (defun widget-default-complete (widget)
1779 "Call the value of the :complete-function property of WIDGET.
1780 If that does not exists, call the value of `widget-complete-field'."
1781 (call-interactively (or (widget-get widget :complete-function)
1782 widget-complete-field)))
1784 (defun widget-default-create (widget)
1785 "Create WIDGET at point in the current buffer."
1786 (widget-specify-insert
1787 (let ((from (point))
1788 button-begin button-end button-glyph
1789 sample-begin sample-end
1792 (insert (widget-get widget :format))
1794 ;; Parse escapes in format.
1795 ;; Coding this in C would speed up things *a lot*.
1797 ;; There are other things to try:
1798 ;; 1. Use skip-chars-forward.
1799 ;; 2. Use a LIMIT (or narrow buffer?) in the search/skip expression.
1800 ;; 3. Search/skip backward to allow LIMIT to be constant.
1801 (while (re-search-forward #r"%\(.\)" nil t)
1802 (let ((escape (aref (match-string 1) 0)))
1803 (replace-match "" t t)
1809 (setq button-begin (point-marker))
1810 (set-marker-insertion-type button-begin nil))
1812 (setq button-end (point-marker))
1813 (set-marker-insertion-type button-end nil))
1815 (setq sample-begin (point)))
1817 (setq sample-end (point)))
1819 (when (widget-get widget :indent)
1821 (insert-char ?\ (widget-get widget :indent))))
1823 (let* ((tag (widget-get widget :tag))
1824 (glyph (widget-get widget :tag-glyph)))
1827 (widget-glyph-insert
1828 widget (or tag "Image") glyph)))
1832 (princ (widget-get widget :value)
1833 (current-buffer))))))
1835 (let ((doc (widget-get widget :doc)))
1837 (setq doc-begin (point))
1839 (while (eq (preceding-char) ?\n)
1840 (delete-backward-char 1))
1842 (setq doc-end (point)))))
1844 (if (and button-begin (not button-end))
1845 (widget-apply widget :value-create)
1846 (setq value-pos (point-marker))))
1847 (lambda () ;otherwise
1848 (widget-apply widget :format-handler escape))]
1849 (string-match (format "[%c\010]" escape) ;^H can't be found in buff
1850 "%[]{}ntdv\010"))))) ;so it can be 'otherwise' cond
1851 ;; Specify button, sample, and doc, and insert value.
1852 (when (and button-begin button-end)
1853 (unless button-glyph
1854 (goto-char button-begin)
1855 (insert (widget-get-indirect widget :button-prefix))
1856 (goto-char button-end)
1857 (set-marker-insertion-type button-end t)
1858 (insert (widget-get-indirect widget :button-suffix)))
1859 (widget-specify-button widget button-begin button-end)
1860 ;; Is this necessary?
1861 (set-marker button-begin nil)
1862 (set-marker button-end nil))
1863 (and sample-begin sample-end
1864 (widget-specify-sample widget sample-begin sample-end))
1865 (and doc-begin doc-end
1866 (widget-specify-doc widget doc-begin doc-end))
1868 (goto-char value-pos)
1869 (widget-apply widget :value-create)))
1870 (let ((from (point-min-marker))
1871 (to (point-max-marker)))
1872 (set-marker-insertion-type from t)
1873 (set-marker-insertion-type to nil)
1874 (widget-put widget :from from)
1875 (widget-put widget :to to)))
1876 (widget-clear-undo))
1878 (defun widget-default-format-handler (widget escape)
1879 ;; We recognize the %h escape by default.
1880 (let* ((buttons (widget-get widget :buttons)))
1881 (cond ((eq escape ?h)
1882 (let* ((doc-property (widget-get widget :documentation-property))
1883 (doc-try (cond ((widget-get widget :doc))
1884 ((functionp doc-property)
1885 (funcall doc-property
1886 (widget-get widget :value)))
1887 ((symbolp doc-property)
1888 (documentation-property
1889 (widget-get widget :value)
1891 (doc-text (and (stringp doc-try)
1892 (> (length doc-try) 1)
1894 (doc-indent (widget-get widget :documentation-indent)))
1896 (and (eq (preceding-char) ?\n)
1897 (widget-get widget :indent)
1898 (insert-char ?\ (widget-get widget :indent)))
1899 ;; The `*' in the beginning is redundant.
1900 (when (eq (aref doc-text 0) ?*)
1901 (setq doc-text (substring doc-text 1)))
1902 ;; Get rid of trailing newlines.
1903 (when (string-match "\n+\\'" doc-text)
1904 (setq doc-text (substring doc-text 0 (match-beginning 0))))
1905 (push (widget-create-child-and-convert
1906 widget 'documentation-string
1907 :indent (cond ((numberp doc-indent)
1915 (signal 'error (list "Unknown escape" escape))))
1916 (widget-put widget :buttons buttons)))
1918 (defun widget-default-button-face-get (widget)
1919 ;; Use :button-face or widget-button-face
1920 (or (widget-get widget :button-face)
1921 (let ((parent (widget-get widget :parent)))
1923 (widget-apply parent :button-face-get)
1924 widget-button-face))))
1926 ;; Shouldn't this be like `widget-default-button-face-get', and recurse, and
1928 (defun widget-default-sample-face-get (widget)
1929 ;; Use :sample-face.
1930 (widget-get widget :sample-face))
1932 (defun widget-default-delete (widget)
1933 "Remove widget from the buffer."
1934 (let ((from (widget-get widget :from))
1935 (to (widget-get widget :to))
1936 (inactive-extent (widget-get widget :inactive))
1937 (button-extent (widget-get widget :button-extent))
1938 (sample-extent (widget-get widget :sample-extent))
1939 (doc-extent (widget-get widget :doc-extent))
1940 before-change-functions
1941 after-change-functions
1942 (inhibit-read-only t))
1943 (widget-apply widget :value-delete)
1944 ;; #### In current code, these are never reinserted, but recreated.
1945 ;; So they should either be destroyed, or we should think about how to
1947 (when inactive-extent
1948 (detach-extent inactive-extent))
1950 (detach-extent button-extent))
1952 (detach-extent sample-extent))
1954 (detach-extent doc-extent))
1956 ;; Kludge: this doesn't need to be true for empty formats.
1957 (delete-region from to))
1958 (set-marker from nil)
1959 (set-marker to nil))
1960 (widget-clear-undo))
1962 (defun widget-default-value-set (widget value)
1963 "Recreate widget with new value."
1964 (let* ((old-pos (point))
1965 (from (copy-marker (widget-get widget :from)))
1966 (to (copy-marker (widget-get widget :to)))
1967 (offset (if (and (<= from old-pos) (<= old-pos to))
1968 (if (>= old-pos (1- to))
1970 (- old-pos from)))))
1971 ;;??? Bug: this ought to insert the new value before deleting the old one,
1972 ;; so that markers on either side of the value automatically
1973 ;; stay on the same side. -- rms.
1975 (goto-char (widget-get widget :from))
1976 (widget-apply widget :delete)
1977 (widget-put widget :value value)
1978 (widget-apply widget :create))
1981 (goto-char (+ (widget-get widget :to) offset 1))
1982 (goto-char (min (+ from offset) (1- (widget-get widget :to))))))))
1984 (defun widget-default-value-inline (widget)
1985 "Wrap value in a list unless it is inline."
1986 (if (widget-get widget :inline)
1987 (widget-value widget)
1988 (list (widget-value widget))))
1990 (defun widget-default-default-get (widget)
1992 (widget-get widget :value))
1994 (defun widget-default-menu-tag-get (widget)
1995 "Use tag or value for menus."
1996 (or (widget-get widget :menu-tag)
1997 (widget-get widget :tag)
1998 (widget-princ-to-string (widget-get widget :value))))
2000 (defun widget-default-active (widget)
2001 "Return non-nil iff WIDGET is user-modifiable."
2002 (and (not (widget-get widget :inactive))
2003 (let ((parent (widget-get widget :parent)))
2005 (widget-apply parent :active)))))
2007 (defun widget-default-deactivate (widget)
2008 "Make WIDGET inactive for user modifications."
2009 (widget-specify-inactive widget
2010 (widget-get widget :from)
2011 (widget-get widget :to)))
2013 (defun widget-default-action (widget &optional event)
2014 "Notify the parent when a widget changes."
2015 (let ((parent (widget-get widget :parent)))
2017 (widget-apply parent :notify widget event))))
2019 (defun widget-default-notify (widget child &optional event)
2020 "Pass notification to parent."
2021 (widget-default-action widget event))
2023 (defun widget-default-prompt-value (widget prompt value unbound)
2024 "Read an arbitrary value."
2025 ;; #### XEmacs: What does this mean?
2026 ;; Stolen from `set-variable'.
2027 ;; (let ((initial (if unbound
2029 ;; It would be nice if we could do a `(cons val 1)' here.
2030 ;; (prin1-to-string (custom-quote value))))))
2031 ;; XEmacs: make this use default VALUE. Need to check callers.
2032 (eval-minibuffer (concat prompt ": ")))
2034 ;;; The `item' Widget.
2036 (define-widget 'item 'default
2037 "Constant items for inclusion in other widgets."
2038 :convert-widget 'widget-value-convert-widget
2039 :value-create 'widget-item-value-create
2040 :value-delete 'ignore
2041 :value-get 'widget-value-value-get
2042 :match 'widget-item-match
2043 :match-inline 'widget-item-match-inline
2044 :action 'widget-item-action
2047 (defun widget-item-value-create (widget)
2048 "Insert the printed representation of the value."
2049 (princ (widget-get widget :value) (current-buffer)))
2051 (defun widget-item-match (widget value)
2052 ;; Match if the value is the same.
2053 (equal (widget-get widget :value) value))
2055 (defun widget-item-match-inline (widget values)
2056 ;; Match if the value is the same.
2057 (let ((value (widget-get widget :value)))
2059 (<= (length value) (length values))
2060 (let ((head (widget-sublist values 0 (length value))))
2061 (and (equal head value)
2062 (cons head (widget-sublist values (length value))))))))
2064 (defun widget-item-action (widget &optional event)
2065 ;; Just notify itself.
2066 (widget-apply widget :notify widget event))
2068 ;;; The `push-button' Widget.
2070 ;; XEmacs: this seems to refer to button images. How about native widgets?
2071 (defcustom widget-push-button-gui widget-glyph-enable
2072 "If non nil, use GUI push buttons when available."
2076 (defcustom widget-push-button-prefix "["
2077 "String used as prefix for buttons."
2079 :group 'widget-button)
2081 (defcustom widget-push-button-suffix "]"
2082 "String used as suffix for buttons."
2084 :group 'widget-button)
2086 (define-widget 'push-button 'item
2087 "A button which invokes an action.
2089 Creators should usually specify `:action' and `:help-echo' members."
2092 :value-create 'widget-push-button-value-create
2095 (defun widget-push-button-value-create (widget)
2096 "Insert text representing the `on' and `off' states."
2097 (let* ((tag (or (widget-get widget :tag)
2098 (widget-get widget :value)))
2099 (tag-glyph (widget-get widget :tag-glyph))
2100 (text (concat widget-push-button-prefix
2101 tag widget-push-button-suffix))
2104 (widget-glyph-insert widget text tag-glyph))
2105 ;; We must check for console-on-window-system-p here,
2106 ;; because GUI will not work otherwise (it needs RGB
2107 ;; components for colors, and they are not known on TTYs).
2108 ((and widget-push-button-gui
2109 (console-on-window-system-p))
2110 (let* ((gui-button-shadow-thickness 1))
2111 (setq inst (make-gui-button tag 'widget-gui-action widget))
2112 (setq gui (make-glyph inst)))
2113 (widget-glyph-insert-glyph widget gui nil nil inst))
2117 (defun widget-gui-action (widget)
2118 "Apply :action for WIDGET."
2119 (widget-apply-action widget (this-command-keys)))
2121 ;;; The `link' Widget.
2123 (defcustom widget-link-prefix "["
2124 "String used as prefix for links."
2126 :group 'widget-button)
2128 (defcustom widget-link-suffix "]"
2129 "String used as suffix for links."
2131 :group 'widget-button)
2133 (define-widget 'link 'item
2136 This is an abstract widget. Subclasses should usually specify `:action'
2137 and `:help-echo' members."
2138 :button-prefix 'widget-link-prefix
2139 :button-suffix 'widget-link-suffix
2140 :help-echo "Follow the link."
2143 ;;; The `info-link' Widget.
2145 (define-widget 'info-link 'link
2146 "A link to an info file."
2147 :help-echo 'widget-info-link-help-echo
2148 :action 'widget-info-link-action)
2150 (defun widget-info-link-help-echo (widget)
2151 (concat "Read the manual entry `" (widget-value widget) "'"))
2153 (defun widget-info-link-action (widget &optional event)
2154 "Open the info node specified by WIDGET."
2155 (declare-fboundp (Info-goto-node (widget-value widget))))
2157 ;;; The `url-link' Widget.
2159 (define-widget 'url-link 'link
2160 "A link to an www page."
2161 :help-echo 'widget-url-link-help-echo
2162 :action 'widget-url-link-action)
2164 (defun widget-url-link-help-echo (widget)
2165 (concat "Visit <URL:" (widget-value widget) ">"))
2167 (defun widget-url-link-action (widget &optional event)
2168 "Open the url specified by WIDGET."
2169 (if-fboundp 'browse-url
2170 (browse-url (widget-value widget))
2171 (error 'missing-package "Cannot browse URLs in this SXEmacs" 'browse-url)))
2173 ;;; The `function-link' Widget.
2175 (define-widget 'function-link 'link
2176 "A link to an Emacs function."
2177 :action 'widget-function-link-action)
2179 (defun widget-function-link-action (widget &optional event)
2180 "Show the function specified by WIDGET."
2181 (describe-function (widget-value widget)))
2183 ;;; The `variable-link' Widget.
2185 (define-widget 'variable-link 'link
2186 "A link to an Emacs variable."
2187 :action 'widget-variable-link-action)
2189 (defun widget-variable-link-action (widget &optional event)
2190 "Show the variable specified by WIDGET."
2191 (describe-variable (widget-value widget)))
2193 ;;; The `file-link' Widget.
2195 (define-widget 'file-link 'link
2197 :action 'widget-file-link-action)
2199 (defun widget-file-link-action (widget &optional event)
2200 "Find the file specified by WIDGET."
2201 (find-file (widget-value widget)))
2203 ;;; The `emacs-library-link' Widget.
2205 (define-widget 'emacs-library-link 'link
2206 "A link to an Emacs Lisp library file."
2207 :help-echo 'widget-emacs-library-link-help-echo
2208 :action 'widget-emacs-library-link-action)
2210 (defun widget-emacs-library-link-help-echo (widget)
2211 (concat "Visit " (widget-value widget)))
2213 (defun widget-emacs-library-link-action (widget &optional event)
2214 "Find the Emacs Library file specified by WIDGET."
2215 (find-file (locate-library (widget-value widget))))
2217 ;;; The `emacs-commentary-link' Widget.
2219 (define-widget 'emacs-commentary-link 'link
2220 "A link to Commentary in an Emacs Lisp library file."
2221 :action 'widget-emacs-commentary-link-action)
2223 (defun widget-emacs-commentary-link-action (widget &optional event)
2224 "Find the Commentary section of the Emacs file specified by WIDGET."
2225 (declare-fboundp (finder-commentary (widget-value widget))))
2227 ;;; The `editable-field' Widget.
2229 (define-widget 'editable-field 'default
2230 "An editable text field."
2231 :convert-widget 'widget-value-convert-widget
2232 :keymap widget-field-keymap
2234 :help-echo "M-TAB: complete field; RET: enter value"
2236 :prompt-internal 'widget-field-prompt-internal
2237 :prompt-history 'widget-field-history
2238 :prompt-value 'widget-field-prompt-value
2239 :action 'widget-field-action
2240 :validate 'widget-field-validate
2242 :error "Field's value doesn't match allowed forms"
2243 :value-create 'widget-field-value-create
2244 :value-delete 'widget-field-value-delete
2245 :value-get 'widget-field-value-get
2246 :match 'widget-field-match)
2248 (defvar widget-field-history nil
2249 "History of field minibuffer edits.")
2251 (defun widget-field-prompt-internal (widget prompt initial history)
2252 "Read string for WIDGET prompting with PROMPT.
2253 INITIAL is the initial input and HISTORY is a symbol containing
2255 (read-string (concat prompt ": ") initial history))
2257 (defun widget-field-prompt-value (widget prompt value unbound)
2258 "Prompt for a string."
2259 (widget-apply widget
2261 (widget-apply widget
2262 :prompt-internal prompt
2264 (cons (widget-apply widget
2265 :value-to-internal value)
2267 (widget-get widget :prompt-history))))
2269 ;; #### Should be named `widget-action-hooks'.
2270 (defvar widget-edit-functions nil
2271 "Functions run on certain actions.
2273 Not a regular hook; each function should take a widget as an argument.
2274 The standard widget functions `widget-field-action', `widget-choice-action',
2275 and `widget-toggle-action' use `run-hook-with-args' to run these functions.")
2277 (defun widget-field-action (widget &optional event)
2278 ;; Edit the value in the minibuffer.
2279 (let* ((invalid (widget-apply widget :validate))
2280 (prompt (concat (widget-apply widget :menu-tag-get) ": "))
2281 (value (unless invalid
2282 (widget-value widget)))
2283 (answer (widget-apply widget :prompt-value prompt value invalid)))
2284 (unless (equal value answer)
2285 ;; This is a hack. We can't properly validate the widget
2286 ;; because validation requires the new value to be in the field.
2287 ;; However, widget-field-value-create will not function unless
2288 ;; the new value matches. So, we check whether the thing
2289 ;; matches, and if it does, use either the real or a dummy error
2291 (unless (widget-apply widget :match answer)
2292 (let ((error-message (or (widget-get widget :type-error)
2293 "Invalid field contents")))
2294 (widget-put widget :error error-message)
2295 (error error-message)))
2296 (widget-value-set widget answer)
2297 (widget-apply widget :notify widget event)
2299 (run-hook-with-args 'widget-edit-functions widget)))
2301 ;(defun widget-field-action (widget &optional event)
2302 ; ;; Move to next field.
2303 ; (widget-forward 1)
2304 ; (run-hook-with-args 'widget-edit-functions widget))
2306 (defun widget-field-validate (widget)
2307 "Valid if the content matches `:valid-regexp'."
2308 (save-excursion ; XEmacs
2309 (unless (string-match (widget-get widget :valid-regexp)
2310 (widget-apply widget :value-get))
2313 (defun widget-field-value-create (widget)
2314 "Create an editable text field."
2315 (let ((size (widget-get widget :size))
2316 (value (widget-get widget :value))
2318 ;; This is changed to a real extent in `widget-setup'. We
2319 ;; need the end points to behave differently until
2320 ;; `widget-setup' is called. Should probably be replaced with
2321 ;; a genuine extent, but some things break, then.
2322 (extent (cons (make-marker) (make-marker))))
2323 (widget-put widget :field-extent extent)
2326 (< (length value) size)
2327 (insert-char ?\ (- size (length value))))
2328 (unless (memq widget widget-field-list)
2329 (push widget widget-field-new))
2330 (move-marker (cdr extent) (point))
2331 (set-marker-insertion-type (cdr extent) nil)
2334 (move-marker (car extent) from)
2335 (set-marker-insertion-type (car extent) t)))
2337 (defun widget-field-value-delete (widget)
2338 "Remove the widget from the list of active editing fields."
2339 (setq widget-field-list (delq widget widget-field-list))
2340 ;; These are nil if the :format string doesn't contain `%v'.
2341 (let ((extent (widget-get widget :field-extent)))
2343 (detach-extent extent))))
2345 (defun widget-field-value-get (widget)
2346 "Return current text in editing field."
2347 (let ((from (widget-field-start widget))
2348 (to (widget-field-end widget))
2349 (buffer (widget-field-buffer widget))
2350 (size (widget-get widget :size))
2351 (secret (widget-get widget :secret))
2352 (old (current-buffer)))
2359 (eq (char-after (1- to)) ?\ ))
2361 (let ((result (buffer-substring-no-properties from to)))
2364 (while (< (+ from index) to)
2366 (get-char-property (+ from index) 'secret))
2371 (widget-get widget :value)))))
2373 (defun widget-field-match (widget value)
2374 ;; Match any string.
2377 ;;; The `text' Widget.
2379 (define-widget 'text 'editable-field
2380 "A multiline text area."
2381 :keymap widget-text-keymap)
2383 ;;; The `menu-choice' Widget.
2385 (define-widget 'menu-choice 'default
2386 "A menu of options."
2387 :convert-widget 'widget-types-convert-widget
2388 :format "%[%t%]: %v"
2391 :void '(item :format "invalid (%t)\n")
2392 :value-create 'widget-choice-value-create
2393 :value-delete 'widget-children-value-delete
2394 :value-get 'widget-choice-value-get
2395 :value-inline 'widget-choice-value-inline
2396 :default-get 'widget-choice-default-get
2397 :mouse-down-action 'widget-choice-mouse-down-action
2398 :action 'widget-choice-action
2399 :error "Make a choice"
2400 :validate 'widget-choice-validate
2401 :match 'widget-choice-match
2402 :match-inline 'widget-choice-match-inline)
2404 (defun widget-choice-value-create (widget)
2405 "Insert the first choice that matches the value."
2406 (let ((value (widget-get widget :value))
2407 (args (widget-get widget :args))
2408 (explicit (widget-get widget :explicit-choice))
2412 ;; If the user specified the choice for this value,
2413 ;; respect that choice as long as the value is the same.
2414 (widget-put widget :children (list (widget-create-child-value
2415 widget explicit value)))
2416 (widget-put widget :choice explicit))
2418 (setq current (car args)
2420 (when (widget-apply current :match value)
2421 (widget-put widget :children (list (widget-create-child-value
2422 widget current value)))
2423 (widget-put widget :choice current)
2427 (let ((void (widget-get widget :void)))
2428 (widget-put widget :children (list (widget-create-child-and-convert
2429 widget void :value value)))
2430 (widget-put widget :choice void))))))
2432 (defun widget-choice-value-get (widget)
2433 ;; Get value of the child widget.
2434 (widget-value (car (widget-get widget :children))))
2436 (defun widget-choice-value-inline (widget)
2437 ;; Get value of the child widget.
2438 (widget-apply (car (widget-get widget :children)) :value-inline))
2440 (defun widget-choice-default-get (widget)
2441 ;; Get default for the first choice.
2442 (widget-default-get (car (widget-get widget :args))))
2444 (defcustom widget-choice-toggle nil
2445 "If non-nil, a binary choice will just toggle between the values.
2446 Otherwise, the user will explicitly have to choose between the values
2447 when he invoked the menu."
2451 (defun widget-choice-mouse-down-action (widget &optional event)
2452 ;; Return non-nil if we need a menu.
2453 (let ((args (widget-get widget :args))
2454 (old (widget-get widget :choice)))
2455 (cond ((not (console-on-window-system-p))
2456 ;; No place to pop up a menu.
2458 ((< (length args) 2)
2459 ;; Empty or singleton list, just return the value.
2461 ((> (length args) widget-menu-max-size)
2462 ;; Too long, prompt.
2464 ((> (length args) 2)
2465 ;; Reasonable sized list, use menu.
2467 ((and widget-choice-toggle (memq old args))
2471 ;; Ask which of the two.
2474 (defun widget-choice-action (widget &optional event)
2476 (let ((args (widget-get widget :args))
2477 (old (widget-get widget :choice))
2478 (tag (widget-apply widget :menu-tag-get))
2479 (completion-ignore-case (widget-get widget :case-fold))
2481 ;; Remember old value.
2482 (if (and old (not (widget-apply widget :validate)))
2483 (let* ((external (widget-value widget))
2484 (internal (widget-apply old :value-to-internal external)))
2485 (widget-put old :value internal)))
2488 (cond ((= (length args) 0)
2490 ((= (length args) 1)
2492 ((and widget-choice-toggle
2495 (if (eq old (nth 0 args))
2500 (setq current (car args)
2503 (cons (cons (widget-apply current :menu-tag-get)
2507 (widget-choose tag (reverse choices) event)))
2508 (widget-put widget :explicit-choice choice)
2511 (let ((value (widget-default-get current)))
2512 (widget-value-set widget
2513 (widget-apply current :value-to-external value)))
2515 (widget-apply widget :notify widget event)))
2516 (run-hook-with-args 'widget-edit-functions widget))
2518 (defun widget-choice-validate (widget)
2519 ;; Valid if we have made a valid choice.
2520 (if (eq (widget-get widget :void) (widget-get widget :choice))
2522 (widget-apply (car (widget-get widget :children)) :validate)))
2524 (defun widget-choice-match (widget value)
2525 ;; Matches if one of the choices matches.
2526 (let ((args (widget-get widget :args))
2528 (while (and args (not found))
2529 (setq current (car args)
2531 found (widget-apply current :match value)))
2534 (defun widget-choice-match-inline (widget values)
2535 ;; Matches if one of the choices matches.
2536 (let ((args (widget-get widget :args))
2538 (while (and args (null found))
2539 (setq current (car args)
2541 found (widget-match-inline current values)))
2544 ;;; The `toggle' Widget.
2546 (define-widget 'toggle 'item
2547 "Toggle between two states."
2549 :value-create 'widget-toggle-value-create
2550 :action 'widget-toggle-action
2551 :match (lambda (widget value) t)
2555 (defun widget-toggle-value-create (widget)
2556 "Insert text representing the `on' and `off' states."
2557 (if (widget-value widget)
2558 (widget-glyph-insert widget
2559 (widget-get widget :on)
2560 (widget-get widget :on-glyph))
2561 (widget-glyph-insert widget
2562 (widget-get widget :off)
2563 (widget-get widget :off-glyph))))
2565 (defun widget-toggle-action (widget &optional event)
2567 (widget-value-set widget (not (widget-value widget)))
2568 (widget-apply widget :notify widget event)
2569 (run-hook-with-args 'widget-edit-functions widget))
2571 ;;; The `checkbox' Widget.
2573 (define-widget 'checkbox 'toggle
2574 "A checkbox toggle."
2582 :action 'widget-checkbox-action)
2584 (defun widget-checkbox-action (widget &optional event)
2585 "Toggle checkbox, notify parent, and set active state of sibling."
2586 (widget-toggle-action widget event)
2587 (let ((sibling (widget-get-sibling widget)))
2589 (if (widget-value widget)
2590 (widget-apply sibling :activate)
2591 (widget-apply sibling :deactivate)))))
2593 ;;; The `checklist' Widget.
2595 (define-widget 'checklist 'default
2596 "A set widget, selecting zero or more of many.
2598 The parent of several `checkbox' widgets, one for each option."
2599 :convert-widget 'widget-types-convert-widget
2602 :entry-format "%b %v"
2603 :menu-tag "checklist"
2605 :value-create 'widget-checklist-value-create
2606 :value-delete 'widget-children-value-delete
2607 :value-get 'widget-checklist-value-get
2608 :prompt-value 'widget-checklist-prompt-value
2609 :validate 'widget-checklist-validate
2610 :match 'widget-checklist-match
2611 :match-inline 'widget-checklist-match-inline)
2613 (defun widget-checklist-value-create (widget)
2614 ;; Insert all values
2615 (let ((alist (widget-checklist-match-find widget (widget-get widget :value)))
2616 (args (widget-get widget :args)))
2618 (widget-checklist-add-item widget (car args) (assq (car args) alist))
2619 (setq args (cdr args)))
2620 (widget-put widget :children (nreverse (widget-get widget :children)))))
2622 (defun widget-checklist-add-item (widget type chosen)
2623 "Create checklist item in WIDGET of type TYPE.
2624 If the item is checked, CHOSEN is a cons whose cdr is the value."
2625 (and (eq (preceding-char) ?\n)
2626 (widget-get widget :indent)
2627 (insert-char ?\ (widget-get widget :indent)))
2628 (widget-specify-insert
2629 (let* ((children (widget-get widget :children))
2630 (buttons (widget-get widget :buttons))
2631 (button-args (or (widget-get type :sibling-args)
2632 (widget-get widget :button-args)))
2635 (insert (widget-get widget :entry-format))
2637 ;; Parse % escapes in format.
2638 (while (re-search-forward #r"%\([bv%]\)" nil t)
2639 (let ((escape (aref (match-string 1) 0)))
2640 (replace-match "" t t)
2641 (cond ((eq escape ?%)
2644 (setq button (apply 'widget-create-child-and-convert
2646 :value (not (null chosen))
2651 (let ((child (widget-create-child widget type)))
2652 (widget-apply child :deactivate)
2654 ((widget-get type :inline)
2655 (widget-create-child-value
2656 widget type (cdr chosen)))
2658 (widget-create-child-value
2659 widget type (car (cdr chosen)))))))
2661 (signal 'error (list "Unknown escape" escape))))))
2662 ;; Update properties.
2663 (and button child (widget-put child :button button))
2664 (and button (widget-put widget :buttons (cons button buttons)))
2665 (and child (widget-put widget :children (cons child children))))))
2667 (defun widget-checklist-match (widget values)
2668 ;; All values must match a type in the checklist.
2670 (null (cdr (widget-checklist-match-inline widget values)))))
2672 (defun widget-checklist-match-inline (widget values)
2673 ;; Find the values which match a type in the checklist.
2674 (let ((greedy (widget-get widget :greedy))
2675 (args (copy-sequence (widget-get widget :args)))
2678 (let ((answer (widget-checklist-match-up args values)))
2680 (let ((vals (widget-match-inline answer values)))
2681 (setq found (append found (car vals))
2683 args (delq answer args))))
2685 (setq rest (append rest (list (car values)))
2686 values (cdr values)))
2688 (setq rest (append rest values)
2692 (defun widget-checklist-match-find (widget vals)
2693 "Find the vals which match a type in the checklist.
2694 Return an alist of (TYPE MATCH)."
2695 (let ((greedy (widget-get widget :greedy))
2696 (args (copy-sequence (widget-get widget :args)))
2699 (let ((answer (widget-checklist-match-up args vals)))
2701 (let ((match (widget-match-inline answer vals)))
2702 (setq found (cons (cons answer (car match)) found)
2704 args (delq answer args))))
2706 (setq vals (cdr vals)))
2711 (defun widget-checklist-match-up (args vals)
2712 "Return the first type from ARGS that matches VALS."
2713 (let (current found)
2714 (while (and args (null found))
2715 (setq current (car args)
2717 found (widget-match-inline current vals)))
2722 (defun widget-checklist-value-get (widget)
2723 ;; The values of all selected items.
2724 (let ((children (widget-get widget :children))
2727 (setq child (car children)
2728 children (cdr children))
2729 (if (widget-value (widget-get child :button))
2730 (setq result (append result (widget-apply child :value-inline)))))
2733 ;; #### FIXME: should handle default value some day -- dvl
2734 (defun widget-checklist-prompt-value (widget prompt value unbound)
2735 ;; Prompt for items to be selected, and the prompt for their value
2736 (let* ((args (widget-get widget :args))
2737 (choices (mapcar (lambda (elt)
2738 (cons (widget-get elt :tag) elt))
2743 (setq continue (completing-read
2744 (concat (widget-prompt-spaceify prompt)
2745 "select [ret. when done]: ")
2747 (if (string= continue "")
2749 (push (widget-prompt-value (cdr (assoc continue choices))
2754 (defun widget-checklist-validate (widget)
2755 ;; Ticked children must be valid.
2756 (let ((children (widget-get widget :children))
2758 (while (and children (not found))
2759 (setq child (car children)
2760 children (cdr children)
2761 button (widget-get child :button)
2762 found (and (widget-value button)
2763 (widget-apply child :validate))))
2766 ;;; The `option' Widget
2768 (define-widget 'option 'checklist
2769 "A widget presenting optional items for inline inclusion in a parent widget."
2772 ;;; The `choice-item' Widget.
2774 (define-widget 'choice-item 'item
2775 "Button items that delegate action events to their parents."
2776 :action 'widget-parent-action
2777 :format "%[%t%] \n")
2779 ;;; The `radio-button' Widget.
2781 (define-widget 'radio-button 'toggle
2782 "A radio button for use in the `radio' widget."
2783 :notify 'widget-radio-button-notify
2788 :on-glyph '("radio1" nil "radio0")
2790 :off-glyph "radio0")
2792 (defun widget-radio-button-notify (widget child &optional event)
2794 (widget-apply (widget-get widget :parent) :action widget event))
2796 ;;; The `radio-button-choice' Widget.
2798 (define-widget 'radio-button-choice 'default
2799 "A set widget, selecting exactly one of many options.
2801 The parent of several `radio-button' widgets, one for each option."
2802 :convert-widget 'widget-types-convert-widget
2805 :entry-format "%b %v"
2807 :value-create 'widget-radio-value-create
2808 :value-delete 'widget-children-value-delete
2809 :value-get 'widget-radio-value-get
2810 :value-inline 'widget-radio-value-inline
2811 :value-set 'widget-radio-value-set
2812 :error "You must push one of the buttons"
2813 :validate 'widget-radio-validate
2814 :match 'widget-choice-match
2815 :match-inline 'widget-choice-match-inline
2816 :action 'widget-radio-action)
2818 (defun widget-radio-value-create (widget)
2819 ;; Insert all values
2820 (let ((args (widget-get widget :args))
2823 (setq arg (car args)
2825 (widget-radio-add-item widget arg))))
2827 (defun widget-radio-add-item (widget type)
2828 "Add to radio widget WIDGET a new radio button item of type TYPE."
2829 ;; (setq type (widget-convert type))
2830 (and (eq (preceding-char) ?\n)
2831 (widget-get widget :indent)
2832 (insert-char ?\ (widget-get widget :indent)))
2833 (widget-specify-insert
2834 (let* ((value (widget-get widget :value))
2835 (children (widget-get widget :children))
2836 (buttons (widget-get widget :buttons))
2837 (button-args (or (widget-get type :sibling-args)
2838 (widget-get widget :button-args)))
2840 (chosen (and (null (widget-get widget :choice))
2841 (widget-apply type :match value)))
2843 (insert (widget-get widget :entry-format))
2845 ;; Parse % escapes in format.
2846 (while (re-search-forward #r"%\([bv%]\)" nil t)
2847 (let ((escape (aref (match-string 1) 0)))
2848 (replace-match "" t t)
2849 (cond ((eq escape ?%)
2852 (setq button (apply 'widget-create-child-and-convert
2853 widget 'radio-button
2854 :value (not (null chosen))
2857 (setq child (if chosen
2858 (widget-create-child-value
2860 (widget-create-child widget type)))
2862 (widget-apply child :deactivate)))
2864 (signal 'error (list "Unknown escape" escape))))))
2865 ;; Update properties.
2867 (widget-put widget :choice type))
2869 (widget-put child :button button)
2870 (widget-put widget :buttons (nconc buttons (list button))))
2872 (widget-put widget :children (nconc children (list child))))
2875 (defun widget-radio-value-get (widget)
2876 ;; Get value of the child widget.
2877 (let ((chosen (widget-radio-chosen widget)))
2878 (and chosen (widget-value chosen))))
2880 (defun widget-radio-chosen (widget)
2881 "Return the widget representing the chosen radio button."
2882 (let ((children (widget-get widget :children))
2885 (setq current (car children)
2886 children (cdr children))
2887 (when (widget-apply (widget-get current :button) :value-get)
2892 (defun widget-radio-value-inline (widget)
2893 ;; Get value of the child widget.
2894 (let ((children (widget-get widget :children))
2897 (setq current (car children)
2898 children (cdr children))
2899 (when (widget-apply (widget-get current :button) :value-get)
2900 (setq found (widget-apply current :value-inline)
2904 (defun widget-radio-value-set (widget value)
2905 ;; We can't just delete and recreate a radio widget, since children
2906 ;; can be added after the original creation and won't be recreated
2908 (let ((children (widget-get widget :children))
2911 (setq current (car children)
2912 children (cdr children))
2913 (let* ((button (widget-get current :button))
2914 (match (and (not found)
2915 (widget-apply current :match value))))
2916 (widget-value-set button match)
2919 (widget-value-set current value)
2920 (widget-apply current :activate))
2921 (widget-apply current :deactivate))
2922 (setq found (or found match))))))
2924 (defun widget-radio-validate (widget)
2925 ;; Valid if we have made a valid choice.
2926 (let ((children (widget-get widget :children))
2927 current found button)
2928 (while (and children (not found))
2929 (setq current (car children)
2930 children (cdr children)
2931 button (widget-get current :button)
2932 found (widget-apply button :value-get)))
2934 (widget-apply current :validate)
2937 (defun widget-radio-action (widget child event)
2938 ;; Check if a radio button was pressed.
2939 (let ((children (widget-get widget :children))
2940 (buttons (widget-get widget :buttons))
2942 (when (memq child buttons)
2944 (setq current (car children)
2945 children (cdr children))
2946 (let* ((button (widget-get current :button)))
2947 (cond ((eq child button)
2948 (widget-value-set button t)
2949 (widget-apply current :activate))
2950 ((widget-value button)
2951 (widget-value-set button nil)
2952 (widget-apply current :deactivate)))))))
2953 ;; Pass notification to parent.
2954 (widget-apply widget :notify child event))
2956 ;;; The `insert-button' Widget.
2958 (define-widget 'insert-button 'push-button
2959 "An insert button for the `editable-list' widget."
2961 :help-echo "Insert a new item into the list at this position."
2962 :action 'widget-insert-button-action)
2964 (defun widget-insert-button-action (widget &optional event)
2965 ;; Ask the parent to insert a new item.
2966 (widget-apply (widget-get widget :parent)
2967 :insert-before (widget-get widget :widget)))
2969 ;;; The `delete-button' Widget.
2971 (define-widget 'delete-button 'push-button
2972 "A delete button for the `editable-list' widget."
2974 :help-echo "Delete this item from the list."
2975 :action 'widget-delete-button-action)
2977 (defun widget-delete-button-action (widget &optional event)
2978 ;; Ask the parent to insert a new item.
2979 (widget-apply (widget-get widget :parent)
2980 :delete-at (widget-get widget :widget)))
2982 ;;; The `editable-list' Widget.
2984 (defcustom widget-editable-list-gui nil
2985 "If non nil, use GUI push-buttons in editable list when available."
2989 (define-widget 'editable-list 'default
2990 "A variable list of widgets of the same type."
2991 :convert-widget 'widget-types-convert-widget
2994 :format-handler 'widget-editable-list-format-handler
2995 :entry-format "%i %d %v"
2996 :menu-tag "editable-list"
2997 :value-create 'widget-editable-list-value-create
2998 :value-delete 'widget-children-value-delete
2999 :value-get 'widget-editable-list-value-get
3000 :validate 'widget-children-validate
3001 :match 'widget-editable-list-match
3002 :match-inline 'widget-editable-list-match-inline
3003 :insert-before 'widget-editable-list-insert-before
3004 :delete-at 'widget-editable-list-delete-at)
3006 (defun widget-editable-list-format-handler (widget escape)
3007 ;; We recognize the insert button.
3008 (let ((widget-push-button-gui widget-editable-list-gui))
3009 (cond ((eq escape ?i)
3010 (and (widget-get widget :indent)
3011 (insert-char ?\ (widget-get widget :indent)))
3012 (apply 'widget-create-child-and-convert
3013 widget 'insert-button
3014 (widget-get widget :append-button-args)))
3016 (widget-default-format-handler widget escape)))))
3018 (defun widget-editable-list-value-create (widget)
3019 ;; Insert all values
3020 (let* ((value (widget-get widget :value))
3021 (type (nth 0 (widget-get widget :args)))
3023 (widget-put widget :value-pos (copy-marker (point)))
3024 (set-marker-insertion-type (widget-get widget :value-pos) t)
3026 (let ((answer (widget-match-inline type value)))
3028 (setq children (cons (widget-editable-list-entry-create
3030 (if (widget-get type :inline)
3037 (widget-put widget :children (nreverse children))))
3039 (defun widget-editable-list-value-get (widget)
3040 ;; Get value of the child widget.
3041 (apply 'append (mapcar (lambda (child) (widget-apply child :value-inline))
3042 (widget-get widget :children))))
3044 (defun widget-editable-list-match (widget value)
3045 ;; Value must be a list and all the members must match the type.
3047 (null (cdr (widget-editable-list-match-inline widget value)))))
3049 (defun widget-editable-list-match-inline (widget value)
3050 (let ((type (nth 0 (widget-get widget :args)))
3053 (while (and value ok)
3054 (let ((answer (widget-match-inline type value)))
3056 (setq found (append found (car answer))
3059 (cons found value)))
3061 (defun widget-editable-list-insert-before (widget before)
3062 ;; Insert a new child in the list of children.
3064 (let ((children (widget-get widget :children))
3065 (inhibit-read-only t)
3066 before-change-functions
3067 after-change-functions)
3069 (goto-char (widget-get before :entry-from)))
3071 (goto-char (widget-get widget :value-pos))))
3072 (let ((child (widget-editable-list-entry-create
3074 (when (< (widget-get child :entry-from) (widget-get widget :from))
3075 (set-marker (widget-get widget :from)
3076 (widget-get child :entry-from)))
3077 (if (eq (car children) before)
3078 (widget-put widget :children (cons child children))
3079 (while (not (eq (car (cdr children)) before))
3080 (setq children (cdr children)))
3081 (setcdr children (cons child (cdr children)))))))
3083 (widget-apply widget :notify widget))
3085 (defun widget-editable-list-delete-at (widget child)
3086 ;; Delete child from list of children.
3088 (let ((buttons (copy-sequence (widget-get widget :buttons)))
3090 (inhibit-read-only t)
3091 before-change-functions
3092 after-change-functions)
3094 (setq button (car buttons)
3095 buttons (cdr buttons))
3096 (when (eq (widget-get button :widget) child)
3098 :buttons (delq button (widget-get widget :buttons)))
3099 (widget-delete button))))
3100 (let ((entry-from (widget-get child :entry-from))
3101 (entry-to (widget-get child :entry-to))
3102 (inhibit-read-only t)
3103 before-change-functions
3104 after-change-functions)
3105 (widget-delete child)
3106 (delete-region entry-from entry-to)
3107 (set-marker entry-from nil)
3108 (set-marker entry-to nil))
3109 (widget-put widget :children (delq child (widget-get widget :children))))
3111 (widget-apply widget :notify widget))
3113 (defun widget-editable-list-entry-create (widget value conv)
3114 ;; Create a new entry to the list.
3115 (let ((type (nth 0 (widget-get widget :args)))
3116 (widget-push-button-gui widget-editable-list-gui)
3117 child delete insert)
3118 (widget-specify-insert
3120 (and (widget-get widget :indent)
3121 (insert-char ?\ (widget-get widget :indent)))
3122 (insert (widget-get widget :entry-format)))
3123 ;; Parse % escapes in format.
3124 (while (re-search-forward #r"%\(.\)" nil t)
3125 (let ((escape (aref (match-string 1) 0)))
3126 (replace-match "" t t)
3127 (cond ((eq escape ?%)
3130 (setq insert (apply 'widget-create-child-and-convert
3131 widget 'insert-button
3132 (widget-get widget :insert-button-args))))
3134 (setq delete (apply 'widget-create-child-and-convert
3135 widget 'delete-button
3136 (widget-get widget :delete-button-args))))
3139 (setq child (widget-create-child-value
3141 (setq child (widget-create-child-value
3142 widget type (widget-default-get type)))))
3144 (signal 'error (list "Unknown escape" escape))))))
3146 :buttons (cons delete
3148 (widget-get widget :buttons))))
3149 (let ((entry-from (copy-marker (point-min)))
3150 (entry-to (copy-marker (point-max))))
3151 (set-marker-insertion-type entry-from t)
3152 (set-marker-insertion-type entry-to nil)
3153 (widget-put child :entry-from entry-from)
3154 (widget-put child :entry-to entry-to)))
3155 (widget-put insert :widget child)
3156 (widget-put delete :widget child)
3159 ;;; The `group' Widget.
3161 (define-widget 'group 'default
3162 "A widget which groups other widgets inside."
3163 :convert-widget 'widget-types-convert-widget
3165 :value-create 'widget-group-value-create
3166 :value-delete 'widget-children-value-delete
3167 :value-get 'widget-editable-list-value-get
3168 :default-get 'widget-group-default-get
3169 :prompt-value 'widget-group-prompt-value
3170 :validate 'widget-children-validate
3171 :match 'widget-group-match
3172 :match-inline 'widget-group-match-inline)
3174 (defun widget-group-value-create (widget)
3175 ;; Create each component.
3176 (let ((args (widget-get widget :args))
3177 (value (widget-get widget :value))
3178 arg answer children)
3180 (setq arg (car args)
3182 answer (widget-match-inline arg value)
3184 (and (eq (preceding-char) ?\n)
3185 (widget-get widget :indent)
3186 (insert-char ?\ (widget-get widget :indent)))
3187 (push (cond ((null answer)
3188 (widget-create-child widget arg))
3189 ((widget-get arg :inline)
3190 (widget-create-child-value widget arg (car answer)))
3192 (widget-create-child-value widget arg (car (car answer)))))
3194 (widget-put widget :children (nreverse children))))
3196 (defun widget-group-default-get (widget)
3197 ;; Get the default of the components.
3198 (mapcar 'widget-default-get (widget-get widget :args)))
3200 (defun widget-group-prompt-value (widget prompt value unbound)
3201 ;; Prompt in turn for every component of the group.
3202 (let ((args (widget-get widget :args)))
3204 widget :value-to-external
3206 (mapcar #'(lambda (arg)
3207 (widget-prompt-value
3209 (concat (widget-prompt-spaceify prompt)
3210 (widget-prompt arg nil ""))
3213 ;; If VALUE is bound, the situation is a bit more complex because we
3214 ;; have to split it into a list of default values for every child. Oh,
3215 ;; boy, do I miss 'cl here... -- dvl
3216 (let ((children args)
3217 (defaults (widget-apply widget
3218 :value-to-internal value))
3219 child default result)
3220 (while (setq child (pop children))
3221 (setq default (pop defaults))
3223 (widget-prompt-value
3225 (concat (widget-prompt-spaceify prompt)
3226 (widget-prompt child nil ""))
3228 (nreverse result))))))
3230 (defun widget-group-match (widget values)
3231 ;; Match if the components match.
3233 (let ((match (widget-group-match-inline widget values)))
3234 (and match (null (cdr match))))))
3236 (defun widget-group-match-inline (widget vals)
3237 ;; Match if the components match.
3238 (let ((args (widget-get widget :args))
3239 argument answer found)
3241 (setq argument (car args)
3243 answer (widget-match-inline argument vals))
3245 (setq vals (cdr answer)
3246 found (append found (car answer)))
3253 ;;; The `visibility' Widget.
3255 (define-widget 'visibility 'item
3256 "An indicator and manipulator for hidden items."
3262 :value-create 'widget-visibility-value-create
3263 :action 'widget-toggle-action
3264 :match (lambda (widget value) t))
3266 (defun widget-visibility-value-create (widget)
3267 ;; Insert text representing the `on' and `off' states.
3268 (let ((on (widget-get widget :on))
3269 (off (widget-get widget :off)))
3271 (setq on (concat widget-push-button-prefix
3273 widget-push-button-suffix))
3276 (setq off (concat widget-push-button-prefix
3278 widget-push-button-suffix))
3280 (if (widget-value widget)
3281 (widget-glyph-insert widget on '("down" "down-pushed"))
3282 (widget-glyph-insert widget off '("right" "right-pushed")))))
3284 ;;; The `documentation-link' Widget.
3286 ;; This is a helper widget for `documentation-string'.
3288 (define-widget 'documentation-link 'link
3289 "Link type used in documentation strings."
3291 :help-echo 'widget-documentation-link-echo-help
3292 :action 'widget-documentation-link-action)
3294 (defun widget-documentation-link-echo-help (widget)
3295 "Tell what this link will describe."
3296 (concat "Describe the `" (widget-get widget :value) "' symbol."))
3298 (defun widget-documentation-link-action (widget &optional event)
3299 "Display documentation for WIDGET's value. Ignore optional argument EVENT."
3300 (let* ((string (widget-get widget :value))
3301 (symbol (intern string)))
3302 (if (and (fboundp symbol) (boundp symbol))
3303 ;; If there are two doc strings, give the user a way to pick one.
3304 (declare-fboundp (apropos (concat "\\`" (regexp-quote string) "\\'")))
3305 (if (fboundp symbol)
3306 (describe-function symbol)
3307 (describe-variable symbol)))))
3309 (defcustom widget-documentation-links t
3310 "Add hyperlinks to documentation strings when non-nil."
3312 :group 'widget-documentation)
3314 (defcustom widget-documentation-link-regexp "`\\([^\n`' ]+\\)'"
3315 "Regexp for matching potential links in documentation strings.
3316 The first group should be the link itself."
3318 :group 'widget-documentation)
3320 (defcustom widget-documentation-link-p 'intern-soft
3321 "Predicate used to test if a string is useful as a link.
3322 The value should be a function. The function will be called one
3323 argument, a string, and should return non-nil if there should be a
3324 link for that string."
3326 :options '(widget-documentation-link-p)
3327 :group 'widget-documentation)
3329 (defcustom widget-documentation-link-type 'documentation-link
3330 "Widget type used for links in documentation strings."
3332 :group 'widget-documentation)
3334 (defun widget-documentation-link-add (widget from to)
3335 (widget-specify-doc widget from to)
3336 (when widget-documentation-links
3337 (let ((regexp widget-documentation-link-regexp)
3338 (predicate widget-documentation-link-p)
3339 (type widget-documentation-link-type)
3340 (buttons (widget-get widget :buttons)))
3343 (while (re-search-forward regexp to t)
3344 (let ((name (match-string 1))
3345 (begin (match-beginning 1))
3346 (end (match-end 1)))
3347 (when (funcall predicate name)
3348 (push (widget-convert-button type begin end :value name)
3350 (widget-put widget :buttons buttons)))
3351 (let ((indent (widget-get widget :indent)))
3352 (when (and indent (not (zerop indent)))
3355 (narrow-to-region from to)
3356 (goto-char (point-min))
3357 (while (search-forward "\n" nil t)
3358 (insert-char ?\ indent)))))))
3360 ;;; The `documentation-string' Widget.
3362 (define-widget 'documentation-string 'item
3363 "A documentation string."
3365 :action 'widget-documentation-string-action
3366 :value-delete 'widget-children-value-delete
3367 :value-create 'widget-documentation-string-value-create)
3369 (defun widget-documentation-string-value-create (widget)
3370 ;; Insert documentation string.
3371 (let ((doc (widget-value widget))
3372 (indent (widget-get widget :indent))
3373 (shown (widget-get (widget-get widget :parent) :documentation-shown))
3375 (if (string-match "\n" doc)
3376 (let ((before (substring doc 0 (match-beginning 0)))
3377 (after (substring doc (match-beginning 0)))
3380 (widget-documentation-link-add widget start (point))
3381 (push (widget-create-child-and-convert
3383 :help-echo (lambda (widget)
3385 (if (widget-value widget)
3387 " the rest of the documentation"))
3389 :action 'widget-parent-action
3393 (setq start (point))
3395 (insert-char ?\ indent))
3397 (widget-documentation-link-add widget start (point)))
3398 (widget-put widget :buttons buttons))
3400 (widget-documentation-link-add widget start (point))))
3403 (defun widget-documentation-string-action (widget &rest ignore)
3404 ;; Toggle documentation.
3405 (let ((parent (widget-get widget :parent)))
3406 (widget-put parent :documentation-shown
3407 (not (widget-get parent :documentation-shown))))
3409 (widget-value-set widget (widget-value widget)))
3411 ;;; The Sexp Widgets.
3413 (define-widget 'sexp 'editable-field
3414 "An arbitrary Lisp expression."
3415 :tag "Lisp expression"
3416 :format "%{%t%}: %v"
3418 :validate 'widget-sexp-validate
3419 :match (lambda (widget value) t)
3420 :value-to-internal 'widget-sexp-value-to-internal
3421 :value-to-external (lambda (widget value) (read value))
3422 :prompt-history 'widget-sexp-prompt-value-history
3423 :prompt-value 'widget-sexp-prompt-value)
3425 (defun widget-sexp-value-to-internal (widget value)
3426 ;; Use cl-prettyprint for printer representation.
3427 (let ((pp (if (symbolp value)
3428 (prin1-to-string value)
3429 (widget-prettyprint-to-string value))))
3430 (if (> (length pp) 40)
3434 (defun widget-sexp-validate (widget)
3435 ;; Valid if we can read the string and there is no junk left after it.
3437 (let ((buffer (set-buffer (get-buffer-create " *Widget Scratch*"))))
3439 (insert (widget-apply widget :value-get))
3440 (goto-char (point-min))
3441 (condition-case data
3442 (let ((value (read buffer)))
3444 (if (widget-apply widget :match value)
3446 (widget-put widget :error (widget-get widget :type-error))
3449 :error (format "Junk at end of expression: %s"
3450 (buffer-substring (point)
3453 (error (widget-put widget :error (error-message-string data))
3456 (defvar widget-sexp-prompt-value-history nil
3457 "History of input to `widget-sexp-prompt-value'.")
3459 (defun widget-sexp-prompt-value (widget prompt value unbound)
3460 ;; Read an arbitrary sexp.
3461 (let ((found (read-string (concat prompt ": ")
3462 (if unbound nil (cons (prin1-to-string value) 0))
3463 (widget-get widget :prompt-history))))
3465 (let ((buffer (set-buffer (get-buffer-create " *Widget Scratch*"))))
3468 (goto-char (point-min))
3469 (let ((answer (read buffer)))
3472 (list "Junk at end of expression"
3473 (buffer-substring (point) (point-max)))))
3476 ;; Various constant sexps.
3478 (define-widget 'const 'item
3479 "An immutable sexp."
3480 :prompt-value 'widget-const-prompt-value
3483 (defun widget-const-prompt-value (widget prompt value unbound)
3484 ;; Return the value of the const.
3485 (widget-value widget))
3487 (define-widget 'function-item 'const
3488 "An immutable function name."
3490 :documentation-property (lambda (symbol)
3492 (documentation symbol t)
3495 (define-widget 'variable-item 'const
3496 "An immutable variable name."
3498 :documentation-property 'variable-documentation)
3500 (define-widget 'other 'sexp
3501 "Matches any value, but doesn't let the user edit the value.
3502 This is useful as last item in a `choice' widget.
3503 You should use this widget type with a default value,
3504 as in (other DEFAULT) or (other :tag \"NAME\" DEFAULT).
3505 If the user selects this alternative, that specifies DEFAULT
3511 (defvar widget-string-prompt-value-history nil
3512 "History of input to `widget-string-prompt-value'.")
3514 (define-widget 'string 'editable-field
3517 :format "%{%t%}: %v"
3518 :complete-function 'ispell-complete-word
3519 :prompt-history 'widget-string-prompt-value-history)
3521 (define-widget 'regexp 'string
3522 "A regular expression."
3523 :match 'widget-regexp-match
3524 :validate 'widget-regexp-validate
3525 ;; Doesn't work well with terminating newline.
3526 ;; :value-face 'widget-single-line-field-face
3529 (defun widget-regexp-match (widget value)
3530 ;; Match valid regexps.
3531 (and (stringp value)
3534 (string-match value ""))
3537 (defun widget-regexp-validate (widget)
3538 "Check that the value of WIDGET is a valid regexp."
3539 (condition-case data
3541 (string-match (widget-value widget) ""))
3542 (error (widget-put widget :error (error-message-string data))
3545 (define-widget 'file 'string
3547 It will read a file name from the minibuffer when invoked."
3548 :complete-function 'widget-file-complete
3549 :prompt-value 'widget-file-prompt-value
3550 :format "%{%t%}: %v"
3551 ;; Doesn't work well with terminating newline.
3552 ;; :value-face 'widget-single-line-field-face
3555 (defun widget-file-complete ()
3556 "Perform completion on file name preceding point."
3558 (let* ((end (point))
3559 (beg (save-excursion
3560 (skip-chars-backward "^ ")
3562 (pattern (buffer-substring beg end))
3563 (name-part (file-name-nondirectory pattern))
3564 (directory (file-name-directory pattern))
3565 (completion (file-name-completion name-part directory)))
3566 (cond ((eq completion t))
3568 (message "Can't find completion for \"%s\"" pattern)
3570 ((not (string= name-part completion))
3571 (delete-region beg end)
3572 (insert (expand-file-name completion directory)))
3574 (message "Making completion list...")
3575 (with-output-to-temp-buffer "*Completions*"
3576 (display-completion-list
3577 (sort (file-name-all-completions name-part directory)
3579 (message "Making completion list...%s" "done")))))
3581 (defun widget-file-prompt-value (widget prompt value unbound)
3582 ;; Read file from minibuffer.
3583 (abbreviate-file-name
3585 (read-file-name (concat prompt ": "))
3586 (let ((prompt2 (format "%s: (default %s) " prompt value))
3587 (dir (file-name-directory value))
3588 (file (file-name-nondirectory value))
3589 (must-match (widget-get widget :must-match)))
3590 (read-file-name prompt2 dir nil must-match file)))))
3592 ;;;(defun widget-file-action (widget &optional event)
3593 ;;; ;; Read a file name from the minibuffer.
3594 ;;; (let* ((value (widget-value widget))
3595 ;;; (dir (file-name-directory value))
3596 ;;; (file (file-name-nondirectory value))
3597 ;;; (menu-tag (widget-apply widget :menu-tag-get))
3598 ;;; (must-match (widget-get widget :must-match))
3599 ;;; (answer (read-file-name (concat menu-tag ": (default `" value "') ")
3600 ;;; dir nil must-match file)))
3601 ;;; (widget-value-set widget (abbreviate-file-name answer))
3603 ;;; (widget-apply widget :notify widget event)))
3605 ;; Fixme: use file-name-as-directory.
3606 (define-widget 'directory 'file
3607 "A directory widget.
3608 It will read a directory name from the minibuffer when invoked."
3611 (defvar widget-symbol-prompt-value-history nil
3612 "History of input to `widget-symbol-prompt-value'.")
3614 (define-widget 'symbol 'editable-field
3618 :format "%{%t%}: %v"
3619 :match (lambda (widget value) (symbolp value))
3620 :complete-function 'lisp-complete-symbol
3621 :prompt-internal 'widget-symbol-prompt-internal
3622 :prompt-match 'symbolp
3623 :prompt-history 'widget-symbol-prompt-value-history
3624 :value-to-internal (lambda (widget value)
3628 :value-to-external (lambda (widget value)
3633 (defun widget-symbol-prompt-internal (widget prompt initial history)
3634 ;; Read file from minibuffer.
3635 (let ((answer (completing-read (concat prompt ": ") obarray
3636 (widget-get widget :prompt-match)
3637 nil initial history)))
3638 (if (and (stringp answer)
3639 (not (zerop (length answer))))
3641 (error "No value"))))
3643 (defvar widget-function-prompt-value-history nil
3644 "History of input to `widget-function-prompt-value'.")
3646 (define-widget 'function 'sexp
3648 :complete-function 'lisp-complete-symbol
3649 :prompt-value 'widget-field-prompt-value
3650 :prompt-internal 'widget-symbol-prompt-internal
3651 :prompt-match 'fboundp
3652 :prompt-history 'widget-function-prompt-value-history
3653 :action 'widget-field-action
3656 (defvar widget-variable-prompt-value-history nil
3657 "History of input to `widget-variable-prompt-value'.")
3659 (define-widget 'variable 'symbol
3660 ;; Should complete on variables.
3662 :prompt-match 'boundp
3663 :prompt-history 'widget-variable-prompt-value-history
3666 ;; This part issues a warning when compiling without Mule. Is there a
3667 ;; way of shutting it up?
3669 ;; OK, I'll simply comment the whole thing out, until someone decides
3670 ;; to do something with it.
3672 ;; OK, _I_'ll simply comment it back in, so somebody will get irritated and
3673 ;; do something about it.
3675 (defvar widget-coding-system-prompt-value-history nil
3676 "History of input to `widget-coding-system-prompt-value'.")
3678 (define-widget 'coding-system 'symbol
3679 "A MULE coding-system."
3680 :format "%{%t%}: %v"
3681 :tag "Coding system"
3682 :prompt-history 'widget-coding-system-prompt-value-history
3683 :prompt-value 'widget-coding-system-prompt-value
3684 :action 'widget-coding-system-action)
3686 (defun widget-coding-system-prompt-value (widget prompt value unbound)
3687 ;; Read coding-system from minibuffer.
3689 (completing-read (format "%s (default %s) " prompt value)
3690 (mapcar (lambda (sym)
3691 (list (symbol-name sym)))
3692 (coding-system-list)))))
3694 (defun widget-coding-system-action (widget &optional event)
3695 ;; Read a file name from the minibuffer.
3697 (widget-coding-system-prompt-value
3699 (widget-apply widget :menu-tag-get)
3700 (widget-value widget)
3702 (widget-value-set widget answer)
3703 (widget-apply widget :notify widget event)
3706 (define-widget 'restricted-sexp 'sexp
3707 "A Lisp expression restricted to values that match.
3709 Either the `:match' or the `:match-alternatives' property must be defined."
3710 :type-error "The specified value is not valid"
3711 :match 'widget-restricted-sexp-match
3712 :value-to-internal (lambda (widget value)
3713 (if (widget-apply widget :match value)
3714 (prin1-to-string value)
3717 (defun widget-restricted-sexp-match (widget value)
3718 (let ((alternatives (widget-get widget :match-alternatives))
3720 (while (and alternatives (not matched))
3721 (if (cond ((functionp (car alternatives))
3722 (funcall (car alternatives) value))
3723 ((and (consp (car alternatives))
3724 (eq (car (car alternatives)) 'quote))
3725 (eq value (nth 1 (car alternatives)))))
3727 (setq alternatives (cdr alternatives)))
3730 (define-widget 'integer 'restricted-sexp
3734 :type-error "This field should contain an integer"
3735 :match-alternatives '(integerp))
3737 (define-widget 'number 'restricted-sexp
3738 "A number (floating point or integer)."
3741 :type-error "This field should contain a number (floating point or integer)"
3742 :match-alternatives '(numberp))
3744 (define-widget 'float 'restricted-sexp
3745 "A floating point number."
3746 :tag "Floating point number"
3748 :type-error "This field should contain a floating point number"
3749 :match-alternatives '(floatp))
3751 (define-widget 'character 'editable-field
3756 :format "%{%t%}: %v"
3757 ;; #### This is incorrect for Mule.
3758 :valid-regexp "\\`[\0-\377]\\'"
3759 :error "This field should contain a single character"
3760 :value-to-internal (lambda (widget value)
3763 (char-to-string value)))
3764 :value-to-external (lambda (widget value)
3768 :match (lambda (widget value)
3769 (characterp value)))
3771 (define-widget 'list 'group
3772 "A Lisp list of fixed length with fixed type for each element."
3774 :format "%{%t%}:\n%v")
3776 (define-widget 'vector 'group
3777 "A Lisp vector of fixed length with fixed type for each element."
3779 :format "%{%t%}:\n%v"
3780 :match 'widget-vector-match
3781 :value-to-internal (lambda (widget value) (append value nil))
3782 :value-to-external (lambda (widget value) (vconcat value)))
3784 (defun widget-vector-match (widget value)
3785 (and (vectorp value)
3786 (widget-group-match widget
3787 (widget-apply widget :value-to-internal value))))
3789 (define-widget 'cons 'group
3792 :format "%{%t%}:\n%v"
3793 :match 'widget-cons-match
3794 :value-to-internal (lambda (widget value)
3795 (list (car value) (cdr value)))
3796 :value-to-external (lambda (widget value)
3797 (cons (nth 0 value) (nth 1 value))))
3799 (defun widget-cons-match (widget value)
3801 (widget-group-match widget
3802 (widget-apply widget :value-to-internal value))))
3804 ;;; The `plist' Widget.
3808 (define-widget 'plist 'list
3810 :key-type '(symbol :tag "Key")
3811 :value-type '(sexp :tag "Value")
3812 :convert-widget 'widget-plist-convert-widget
3815 (defvar widget-plist-value-type) ;Dynamic variable
3817 (defun widget-plist-convert-widget (widget)
3818 ;; Handle `:options'.
3819 (let* ((options (widget-get widget :options))
3820 (widget-plist-value-type (widget-get widget :value-type))
3821 (other `(editable-list :inline t
3823 ,(widget-get widget :key-type)
3824 ,widget-plist-value-type)))
3826 (list `(checklist :inline t
3828 ,@(mapcar 'widget-plist-convert-option
3832 (widget-put widget :args args)
3835 (defun widget-plist-convert-option (option)
3836 ;; Convert a single plist option.
3837 (let (key-type value-type)
3839 (let ((key (nth 0 option)))
3840 (setq value-type (nth 1 option))
3843 (setq key-type `(const ,key))))
3844 (setq key-type `(const ,option)
3845 value-type widget-plist-value-type))
3846 `(group :format "Key: %v" :inline t ,key-type ,value-type)))
3849 ;;; The `alist' Widget.
3851 ;; Association lists.
3853 (define-widget 'alist 'list
3854 "An association list."
3855 :key-type '(sexp :tag "Key")
3856 :value-type '(sexp :tag "Value")
3857 :convert-widget 'widget-alist-convert-widget
3860 (defvar widget-alist-value-type) ;Dynamic variable
3862 (defun widget-alist-convert-widget (widget)
3863 ;; Handle `:options'.
3864 (let* ((options (widget-get widget :options))
3865 (widget-alist-value-type (widget-get widget :value-type))
3866 (other `(editable-list :inline t
3868 ,(widget-get widget :key-type)
3869 ,widget-alist-value-type)))
3871 (list `(checklist :inline t
3873 ,@(mapcar 'widget-alist-convert-option
3877 (widget-put widget :args args)
3880 (defun widget-alist-convert-option (option)
3881 ;; Convert a single alist option.
3882 (let (key-type value-type)
3884 (let ((key (nth 0 option)))
3885 (setq value-type (nth 1 option))
3888 (setq key-type `(const ,key))))
3889 (setq key-type `(const ,option)
3890 value-type widget-alist-value-type))
3891 `(cons :format "Key: %v" ,key-type ,value-type)))
3894 (define-widget 'choice 'menu-choice
3895 "A union of several sexp types."
3897 :format "%{%t%}: %[Value Menu%] %v"
3898 :button-prefix 'widget-push-button-prefix
3899 :button-suffix 'widget-push-button-suffix
3900 :prompt-value 'widget-choice-prompt-value)
3902 (defun widget-choice-prompt-value (widget prompt value unbound)
3904 (let ((args (widget-get widget :args))
3905 (completion-ignore-case (widget-get widget :case-fold))
3906 current choices old)
3907 ;; Find the first choice matching VALUE (if given):
3911 (if (widget-apply (car look) :match value)
3912 (setq old (car look)
3914 (setq look (cdr look)))))
3915 ;; If VALUE is invalid (it doesn't match any choice), discard it by
3916 ;; considering it unbound:
3919 ;; Now offer the choice, providing the given default value when/where
3922 (setq current (car args)
3925 (cons (cons (widget-apply current :menu-tag-get)
3929 (let ((val (completing-read (concat prompt ": ") choices nil t
3931 (widget-apply old :menu-tag-get)))))
3932 (if (stringp val) ;; #### is this really needed ? --dvl
3933 (let ((try (try-completion val choices)))
3934 (when (stringp try) ;; #### and this ? --dvl
3936 (cdr (assoc val choices)))
3939 (widget-prompt-value current
3940 (concat (widget-prompt-spaceify prompt)
3941 (widget-get current :tag))
3943 (when (eq current old) value))
3944 (or unbound (not (eq current old))))
3945 (and (not unbound) value))))
3947 (define-widget 'radio 'radio-button-choice
3948 "A set widget, selecting exactly one from many.
3950 The parent of several `radio-button' widgets, one for each option."
3952 :format "%{%t%}:\n%v"
3953 :prompt-value 'widget-choice-prompt-value)
3955 (define-widget 'repeat 'editable-list
3956 "A variable length homogeneous list."
3958 :format "%{%t%}:\n%v%i\n")
3960 (define-widget 'set 'checklist
3961 "A list of members from a fixed set."
3963 :format "%{%t%}:\n%v")
3965 (define-widget 'boolean 'toggle
3966 "To be nil or non-nil, that is the question."
3968 :prompt-value 'widget-boolean-prompt-value
3969 :button-prefix 'widget-push-button-prefix
3970 :button-suffix 'widget-push-button-suffix
3971 :format "%{%t%}: %[Toggle%] %v\n"
3975 (defun widget-boolean-prompt-value (widget prompt value unbound)
3976 ;; Toggle a boolean.
3977 (y-or-n-p (concat prompt ": ")))
3979 ;;; The `color' Widget.
3982 (define-widget 'color 'editable-field
3983 "Choose a color name (with sample)."
3984 :format "%[%t%]: %v (%{sample%})\n"
3988 :complete 'widget-color-complete
3989 :sample-face-get 'widget-color-sample-face-get
3990 :notify 'widget-color-notify
3991 :action 'widget-color-action)
3993 (defun widget-color-complete (widget)
3994 "Complete the color in WIDGET."
3995 (let* ((prefix (buffer-substring-no-properties (widget-field-start widget)
3997 (list (read-color-completion-table))
3998 (completion (try-completion prefix list)))
3999 (cond ((eq completion t)
4000 (message "Exact match."))
4002 (error "Can't find completion for \"%s\"" prefix))
4003 ((not (string-equal prefix completion))
4004 (insert (substring completion (length prefix))))
4006 (message "Making completion list...")
4007 (with-output-to-temp-buffer "*Completions*"
4008 (display-completion-list (all-completions prefix list nil)))
4009 (message "Making completion list...done")))))
4011 (defun widget-color-sample-face-get (widget)
4012 (or (widget-get widget :sample-face)
4013 (let ((color (widget-value widget))
4014 (face (make-face (gensym "sample-face-") nil t)))
4015 ;; Use the face object, not its name, to prevent lossage if gc
4016 ;; happens before applying the face.
4017 (widget-put widget :sample-face face)
4019 (not (equal color ""))
4020 (valid-color-name-p color)
4021 (set-face-foreground face color))
4024 (defvar widget-color-history nil
4025 "History of entered colors.")
4027 (defun widget-color-action (widget &optional event)
4028 "Prompt for a color."
4029 (let* ((tag (widget-apply widget :menu-tag-get))
4030 (answer (read-color (concat tag ": "))))
4031 (unless (zerop (length answer))
4032 (widget-value-set widget answer)
4034 (widget-apply widget :notify widget event))))
4036 (defun widget-color-notify (widget child &optional event)
4037 "Update the sample, and notify the parent."
4038 (let* ((face (widget-apply widget :sample-face-get))
4039 (color (widget-value widget)))
4040 (if (valid-color-name-p color)
4041 (set-face-foreground face color)
4042 (remove-face-property face 'foreground)))
4043 (widget-default-notify widget child event))
4047 (defun widget-echo-help (pos)
4048 "Display the help-echo text for widget at POS."
4049 (let* ((widget (widget-at pos))
4050 (help-echo (and widget (widget-get widget :help-echo))))
4051 (if (functionp help-echo)
4052 (setq help-echo (funcall help-echo widget)))
4053 (if (stringp help-echo)
4054 (display-message 'help-echo help-echo))))
4056 (define-widget 'lazy 'default
4057 "Base widget for recursive datastructures.
4059 The `lazy' widget will, when instantiated, contain a single inferior
4060 widget, of the widget type specified by the :type parameter. The
4061 value of the `lazy' widget is the same as the value of the inferior
4062 widget. When deriving a new widget from the 'lazy' widget, the :type
4063 parameter is allowed to refer to the widget currently being defined,
4064 thus allowing recursive datastructures to be described.
4066 The:type parameter takes the same arguments as the defcustom
4067 parameter with the same name.
4069 Most composite widgets, i.e. widgets containing other widgets, does
4070 not allow recursion. That is, when you define a new widget type, none
4071 of the inferior widgets may be of the same type you are currently
4074 In Lisp, however, it is custom to define datastructures in terms of
4075 themselves. A list, for example, is defined as either nil, or a cons
4076 cell whose cdr itself is a list. The obvious way to translate this
4077 into a widget type would be
4079 (define-widget 'my-list 'choice
4080 \"A list of sexps.\"
4082 :args '((const nil) (cons :value (nil) sexp my-list)))
4084 Here we attempt to define my-list as a choice of either the constant
4085 nil, or a cons-cell containing a sexp and my-lisp. This will not work
4086 because the `choice' widget does not allow recursion.
4088 Using the `lazy' widget you can overcome this problem, as in this
4091 (define-widget 'sexp-list 'lazy
4092 \"A list of sexps.\"
4094 :type '(choice (const nil) (cons :value (nil) sexp sexp-list)))"
4095 :format "%{%t%}: %v"
4096 ;; We don't convert :type because we want to allow recursive
4097 ;; datastructures. This is slow, so we should not create speed
4098 ;; critical widgets by deriving from this.
4099 :convert-widget 'widget-value-convert-widget
4100 :value-create 'widget-type-value-create
4101 :value-delete 'widget-children-value-delete
4102 :value-get 'widget-child-value-get
4103 :value-inline 'widget-child-value-inline
4104 :default-get 'widget-type-default-get
4105 :match 'widget-type-match
4106 :validate 'widget-child-validate)
4108 (defun widget-child-value-get (widget)
4109 "Get the value of the first member of :children in WIDGET."
4110 (widget-value (car (widget-get widget :children))))
4112 (defun widget-child-value-inline (widget)
4113 "Get the inline value of the first member of :children in WIDGET."
4114 (widget-apply (car (widget-get widget :children)) :value-inline))
4116 (defun widget-child-validate (widget)
4117 "The result of validating the first member of :children in WIDGET."
4118 (widget-apply (car (widget-get widget :children)) :validate))
4120 (defun widget-type-value-create (widget)
4121 "Convert and instantiate the value of the :type attribute of WIDGET.
4122 Store the newly created widget in the :children attribute.
4124 The value of the :type attribute should be an unconverted widget type."
4125 (let ((value (widget-get widget :value))
4126 (type (widget-get widget :type)))
4127 (widget-put widget :children
4128 (list (widget-create-child-value widget
4129 (widget-convert type)
4132 (defun widget-type-default-get (widget)
4133 "Get default value from the :type attribute of WIDGET.
4135 The value of the :type attribute should be an unconverted widget type."
4136 (widget-default-get (widget-convert (widget-get widget :type))))
4138 (defun widget-type-match (widget value)
4139 "Non-nil if the :type value of WIDGET matches VALUE.
4141 The value of the :type attribute should be an unconverted widget type."
4142 (widget-apply (widget-convert (widget-get widget :type)) :match value))
4148 ;;; wid-edit.el ends here