1 ;;; wid-edit.el --- Functions for creating and using widgets.
3 ;; Copyright (C) 1996, 1997 Free Software Foundation, Inc.
5 ;; Author: Per Abrahamsen <abraham@dina.kvl.dk>
6 ;; Keywords: extensions
8 ;; X-URL: http://www.dina.kvl.dk/~abraham/custom/
10 ;; This file is part of GNU Emacs.
12 ;; GNU Emacs is free software; you can redistribute it and/or modify
13 ;; it under the terms of the GNU General Public License as published by
14 ;; the Free Software Foundation; either version 2, or (at your option)
17 ;; GNU Emacs is distributed in the hope that it will be useful,
18 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
19 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 ;; GNU General Public License for more details.
22 ;; You should have received a copy of the GNU General Public License
23 ;; along with GNU Emacs; see the file COPYING. If not, write to the
24 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
25 ;; Boston, MA 02111-1307, USA.
35 (eval-when-compile (require 'cl))
40 (autoload 'pp-to-string "pp")
41 (autoload 'Info-goto-node "info")
43 (when (string-match "XEmacs" emacs-version)
46 (error (load-library "x-overlay"))))
48 (if (string-match "XEmacs" emacs-version)
49 ;; XEmacs spell `intangible' as `atomic'.
50 (defun widget-make-intangible (from to side)
51 "Make text between FROM and TO atomic with regard to movement.
52 Third argument should be `start-open' if it should be sticky to the rear,
53 and `end-open' if it should sticky to the front."
54 (require 'atomic-extents)
55 (let ((ext (make-extent from to)))
56 ;; XEmacs doesn't understant different kinds of read-only, so
57 ;; we have to use extents instead.
58 (put-text-property from to 'read-only nil)
59 (set-extent-property ext 'read-only t)
60 (set-extent-property ext 'start-open nil)
61 (set-extent-property ext 'end-open nil)
62 (set-extent-property ext side t)
63 (set-extent-property ext 'atomic t)))
64 (defun widget-make-intangible (from to size)
65 "Make text between FROM and TO intangible."
66 (put-text-property from to 'intangible 'front)))
68 (if (string-match "XEmacs" emacs-version)
69 (defun widget-event-point (event)
70 "Character position of the end of event if that exists, or nil."
71 (if (mouse-event-p event)
74 (defun widget-event-point (event)
75 "Character position of the end of event if that exists, or nil."
76 (posn-point (event-end event))))
78 ;; The following should go away when bundled with Emacs.
83 (unless (and (featurep 'custom) (fboundp 'custom-declare-variable))
84 ;; We have the old custom-library, hack around it!
85 (defmacro defgroup (&rest args) nil)
86 (defmacro defcustom (var value doc &rest args)
87 (` (defvar (, var) (, value) (, doc))))
88 (defmacro defface (&rest args) nil)
89 (define-widget-keywords :prefix :tag :load :link :options :type :group)
90 (when (fboundp 'copy-face)
91 (copy-face 'default 'widget-documentation-face)
92 (copy-face 'bold 'widget-button-face)
93 (copy-face 'italic 'widget-field-face)))
95 (unless (fboundp 'button-release-event-p)
96 ;; XEmacs function missing from Emacs.
97 (defun button-release-event-p (event)
98 "Non-nil if EVENT is a mouse-button-release event object."
100 (memq (event-basic-type event) '(mouse-1 mouse-2 mouse-3))
101 (or (memq 'click (event-modifiers event))
102 (memq 'drag (event-modifiers event))))))
104 (unless (fboundp 'error-message-string)
105 ;; Emacs function missing in XEmacs.
106 (defun error-message-string (obj)
107 "Convert an error value to an error message."
108 (let ((buf (get-buffer-create " *error-message*")))
110 (display-error obj buf)
111 (buffer-string buf)))))
115 (defgroup widgets nil
116 "Customization support for the Widget Library."
117 :link '(custom-manual "(widget)Top")
118 :link '(url-link :tag "Development Page"
119 "http://www.dina.kvl.dk/~abraham/custom/")
125 (defface widget-documentation-face '((((class color)
127 (:foreground "lime green"))
130 (:foreground "dark green"))
132 "Face used for documentation text."
135 (defface widget-button-face '((t (:bold t)))
136 "Face used for widget buttons."
139 (defcustom widget-mouse-face 'highlight
140 "Face used for widget buttons when the mouse is above them."
144 (defface widget-field-face '((((class grayscale color)
146 (:background "gray85"))
147 (((class grayscale color)
149 (:background "dark gray"))
152 "Face used for editable fields."
155 ;;; Utility functions.
157 ;; These are not really widget specific.
159 (defsubst widget-plist-member (plist prop)
160 ;; Return non-nil if PLIST has the property PROP.
161 ;; PLIST is a property list, which is a list of the form
162 ;; (PROP1 VALUE1 PROP2 VALUE2 ...). PROP is a symbol.
163 ;; Unlike `plist-get', this allows you to distinguish between a missing
164 ;; property and a property with the value nil.
165 ;; The value is actually the tail of PLIST whose car is PROP.
166 (while (and plist (not (eq (car plist) prop)))
167 (setq plist (cdr (cdr plist))))
170 (defun widget-princ-to-string (object)
171 ;; Return string representation of OBJECT, any Lisp object.
172 ;; No quoting characters are used; no delimiters are printed around
173 ;; the contents of strings.
175 (set-buffer (get-buffer-create " *widget-tmp*"))
177 (let ((standard-output (current-buffer)))
181 (defun widget-clear-undo ()
182 "Clear all undo information."
183 (buffer-disable-undo (current-buffer))
184 (buffer-enable-undo))
186 (defcustom widget-menu-max-size 40
187 "Largest number of items allowed in a popup-menu.
188 Larger menus are read through the minibuffer."
192 (defun widget-choose (title items &optional event)
193 "Choose an item from a list.
195 First argument TITLE is the name of the list.
196 Second argument ITEMS is an list whose members are either
197 (NAME . VALUE), to indicate selectable items, or just strings to
198 indicate unselectable items.
199 Optional third argument EVENT is an input event.
201 The user is asked to choose between each NAME from the items alist,
202 and the VALUE of the chosen element will be returned. If EVENT is a
203 mouse event, and the number of elements in items is less than
204 `widget-menu-max-size', a popup menu will be used, otherwise the
206 (cond ((and (< (length items) widget-menu-max-size)
207 event (fboundp 'x-popup-menu) window-system)
208 ;; We are in Emacs-19, pressed by the mouse
210 (list title (cons "" items))))
211 ((and (< (length items) widget-menu-max-size)
212 event (fboundp 'popup-menu) window-system)
213 ;; We are in XEmacs, pressed by the mouse
214 (let ((val (get-popup-menu-response
221 (vector (car x) (list (car x)) t))))
224 (listp (event-object val))
225 (stringp (car-safe (event-object val)))
226 (car (event-object val))))
227 (cdr (assoc val items))))
229 (setq items (remove-if 'stringp items))
230 (let ((val (completing-read (concat title ": ") items nil t)))
232 (let ((try (try-completion val items)))
235 (cdr (assoc val items)))
238 ;;; Widget text specifications.
240 ;; These functions are for specifying text properties.
242 (defun widget-specify-none (from to)
243 ;; Clear all text properties between FROM and TO.
244 (set-text-properties from to nil))
246 (defun widget-specify-text (from to)
247 ;; Default properties.
248 (add-text-properties from to (list 'read-only t
252 'rear-nonsticky nil)))
254 (defun widget-specify-field (widget from to)
255 ;; Specify editable button for WIDGET between FROM and TO.
256 (widget-specify-field-update widget from to)
258 ;; Make it possible to edit the front end of the field.
259 (add-text-properties (1- from) from (list 'rear-nonsticky t
262 (when (or (string-match "\\(.\\|\n\\)%v" (widget-get widget :format))
263 (widget-get widget :hide-front-space))
264 ;; WARNING: This is going to lose horrible if the character just
265 ;; before the field can be modified (e.g. if it belongs to a
266 ;; choice widget). We try to compensate by checking the format
267 ;; string, and hope the user hasn't changed the :create method.
268 (widget-make-intangible (- from 2) from 'end-open))
270 ;; Make it possible to edit back end of the field.
271 (add-text-properties to (1+ to) (list 'front-sticky nil
275 (cond ((widget-get widget :size)
276 (put-text-property to (1+ to) 'invisible t)
277 (when (or (string-match "%v\\(.\\|\n\\)" (widget-get widget :format))
278 (widget-get widget :hide-rear-space))
279 ;; WARNING: This is going to lose horrible if the character just
280 ;; after the field can be modified (e.g. if it belongs to a
281 ;; choice widget). We try to compensate by checking the format
282 ;; string, and hope the user hasn't changed the :create method.
283 (widget-make-intangible to (+ to 2) 'start-open)))
284 ((string-match "XEmacs" emacs-version)
285 ;; XEmacs does not allow you to insert before a read-only
286 ;; character, even if it is start.open.
287 ;; XEmacs does allow you to delete an read-only extent, so
288 ;; making the terminating newline read only doesn't help.
289 ;; I tried putting an invisible intangible read-only space
290 ;; before the newline, which gave really weird effects.
291 ;; So for now, we just have trust the user not to delete the
293 (put-text-property to (1+ to) 'read-only nil))))
295 (defun widget-specify-field-update (widget from to)
296 ;; Specify editable button for WIDGET between FROM and TO.
297 (let ((map (widget-get widget :keymap))
298 (secret (widget-get widget :secret))
300 (size (widget-get widget :size))
301 (face (or (widget-get widget :value-face)
303 (help-echo (widget-get widget :help-echo))
304 (help-property (if (featurep 'balloon-help)
307 (unless (or (stringp help-echo) (null help-echo))
308 (setq help-echo 'widget-mouse-help))
314 (eq (char-after (1- secret-to)) ?\ ))
315 (setq secret-to (1- secret-to)))
319 (while (< (point) secret-to)
320 (let ((old (get-text-property (point) 'secret)))
322 (subst-char-in-region (point) (1+ (point)) secret old)))
325 (set-text-properties from to (list 'field widget
329 help-property help-echo
335 (while (< (point) secret-to)
336 (let ((old (following-char)))
337 (subst-char-in-region (point) (1+ (point)) old secret)
338 (put-text-property (point) (1+ (point)) 'secret old))
341 (unless (widget-get widget :size)
342 (add-text-properties to (1+ to) (list 'field widget
343 help-property help-echo
345 (add-text-properties to (1+ to) (list 'local-map map
348 (defun widget-specify-button (widget from to)
349 ;; Specify button for WIDGET between FROM and TO.
350 (let ((face (widget-apply widget :button-face-get))
351 (help-echo (widget-get widget :help-echo))
352 (help-property (if (featurep 'balloon-help)
355 (unless (or (null help-echo) (stringp help-echo))
356 (setq help-echo 'widget-mouse-help))
357 (add-text-properties from to (list 'button widget
358 'mouse-face widget-mouse-face
361 help-property help-echo
364 (defun widget-mouse-help (extent)
365 "Find mouse help string for button in extent."
366 (let* ((widget (widget-at (extent-start-position extent)))
367 (help-echo (and widget (widget-get widget :help-echo))))
368 (cond ((stringp help-echo)
370 ((and (symbolp help-echo) (fboundp help-echo)
371 (stringp (setq help-echo (funcall help-echo widget))))
374 (format "(widget %S :help-echo %S)" widget help-echo)))))
376 (defun widget-specify-sample (widget from to)
377 ;; Specify sample for WIDGET between FROM and TO.
378 (let ((face (widget-apply widget :sample-face-get)))
380 (add-text-properties from to (list 'start-open t
384 (defun widget-specify-doc (widget from to)
385 ;; Specify documentation for WIDGET between FROM and TO.
386 (add-text-properties from to (list 'widget-doc widget
387 'face 'widget-documentation-face)))
389 (defmacro widget-specify-insert (&rest form)
390 ;; Execute FORM without inheriting any text properties.
393 (let ((inhibit-read-only t)
395 after-change-functions)
397 (narrow-to-region (- (point) 2) (point))
398 (widget-specify-none (point-min) (point-max))
399 (goto-char (1+ (point-min)))
400 (setq result (progn (,@ form)))
401 (delete-region (point-min) (1+ (point-min)))
402 (delete-region (1- (point-max)) (point-max))
403 (goto-char (point-max))
406 (defface widget-inactive-face '((((class grayscale color)
408 (:foreground "light gray"))
409 (((class grayscale color)
411 (:foreground "dark gray"))
414 "Face used for inactive widgets."
417 (defun widget-specify-inactive (widget from to)
418 "Make WIDGET inactive for user modifications."
419 (unless (widget-get widget :inactive)
420 (let ((overlay (make-overlay from to nil t nil)))
421 (overlay-put overlay 'face 'widget-inactive-face)
422 (overlay-put overlay 'evaporate t)
423 (overlay-put overlay 'priority 100)
424 (overlay-put overlay (if (string-match "XEmacs" emacs-version)
426 'modification-hooks) '(widget-overlay-inactive))
427 (widget-put widget :inactive overlay))))
429 (defun widget-overlay-inactive (&rest junk)
430 "Ignoring the arguments, signal an error."
431 (unless inhibit-read-only
432 (error "Attempt to modify inactive widget")))
435 (defun widget-specify-active (widget)
436 "Make WIDGET active for user modifications."
437 (let ((inactive (widget-get widget :inactive)))
439 (delete-overlay inactive)
440 (widget-put widget :inactive nil))))
442 ;;; Widget Properties.
444 (defsubst widget-type (widget)
445 "Return the type of WIDGET, a symbol."
448 (defun widget-put (widget property value)
449 "In WIDGET set PROPERTY to VALUE.
450 The value can later be retrived with `widget-get'."
451 (setcdr widget (plist-put (cdr widget) property value)))
453 (defun widget-get (widget property)
454 "In WIDGET, get the value of PROPERTY.
455 The value could either be specified when the widget was created, or
456 later with `widget-put'."
460 (cond ((setq tmp (widget-plist-member (cdr widget) property))
461 (setq value (car (cdr tmp))
463 ((setq tmp (car widget))
464 (setq widget (get tmp 'widget-type)))
466 (setq missing nil))))
469 (defun widget-member (widget property)
470 "Non-nil iff there is a definition in WIDGET for PROPERTY."
471 (cond ((widget-plist-member (cdr widget) property)
474 (widget-member (get (car widget) 'widget-type) property))
478 (defun widget-apply (widget property &rest args)
479 "Apply the value of WIDGET's PROPERTY to the widget itself.
480 ARGS are passed as extra arguments to the function."
481 (apply (widget-get widget property) widget args))
483 (defun widget-value (widget)
484 "Extract the current value of WIDGET."
486 :value-to-external (widget-apply widget :value-get)))
488 (defun widget-value-set (widget value)
489 "Set the current value of WIDGET to VALUE."
491 :value-set (widget-apply widget
492 :value-to-internal value)))
494 (defun widget-match-inline (widget vals)
495 ;; In WIDGET, match the start of VALS.
496 (cond ((widget-get widget :inline)
497 (widget-apply widget :match-inline vals))
499 (widget-apply widget :match (car vals)))
500 (cons (list (car vals)) (cdr vals)))
503 (defun widget-apply-action (widget &optional event)
504 "Apply :action in WIDGET in response to EVENT."
505 (if (widget-apply widget :active)
506 (widget-apply widget :action event)
507 (error "Attempt to perform action on inactive widget")))
509 ;;; Helper functions.
511 ;; These are widget specific.
514 (defun widget-prompt-value (widget prompt &optional value unbound)
515 "Prompt for a value matching WIDGET, using PROMPT.
516 The current value is assumed to be VALUE, unless UNBOUND is non-nil."
517 (unless (listp widget)
518 (setq widget (list widget)))
519 (setq prompt (format "[%s] %s" (widget-type widget) prompt))
520 (setq widget (widget-convert widget))
521 (let ((answer (widget-apply widget :prompt-value prompt value unbound)))
522 (unless (widget-apply widget :match answer)
523 (error "Value does not match %S type." (car widget)))
526 (defun widget-get-sibling (widget)
527 "Get the item WIDGET is assumed to toggle.
528 This is only meaningful for radio buttons or checkboxes in a list."
529 (let* ((parent (widget-get widget :parent))
530 (children (widget-get parent :children))
534 (setq child (car children)
535 children (cdr children))
536 (when (eq (widget-get child :button) widget)
537 (throw 'child child)))
542 (defcustom widget-glyph-directory (concat data-directory "custom/")
543 "Where widget glyphs are located.
544 If this variable is nil, widget will try to locate the directory
545 automatically. This does not work yet."
549 (defcustom widget-glyph-enable t
550 "If non nil, use glyphs in images when available."
554 (defun widget-glyph-insert (widget tag image)
555 "In WIDGET, insert the text TAG or, if supported, IMAGE.
556 IMAGE should either be a glyph, or a name sans extension of an xpm or
557 xbm file located in `widget-glyph-directory'.
559 WARNING: If you call this with a glyph, and you want the user to be
560 able to activate the glyph, make sure it is unique. If you use the
561 same glyph for multiple widgets, activating any of the glyphs will
562 cause the last created widget to be activated."
563 (cond ((not (and (string-match "XEmacs" emacs-version)
565 (fboundp 'make-glyph)
567 ;; We don't want or can't use glyphs.
569 ((and (fboundp 'glyphp)
571 ;; Already a glyph. Insert it.
572 (widget-glyph-insert-glyph widget tag image))
574 ;; A string. Look it up in.
575 (let ((file (concat widget-glyph-directory
576 (if (string-match "/\\'" widget-glyph-directory)
580 (if (featurep 'xpm) ".xpm" ".xbm"))))
581 (if (file-readable-p file)
582 (widget-glyph-insert-glyph widget tag (make-glyph file))
583 ;; File not readable, give up.
586 (defun widget-glyph-insert-glyph (widget tag glyph &optional down inactive)
587 "In WIDGET, with alternative text TAG, insert GLYPH."
588 (set-glyph-image glyph (cons 'tty tag))
589 (set-glyph-property glyph 'widget widget)
591 (set-glyph-image down (cons 'tty tag))
592 (set-glyph-property down 'widget widget))
594 (set-glyph-image inactive (cons 'tty tag))
595 (set-glyph-property inactive 'widget widget))
597 (add-text-properties (1- (point)) (point)
600 (widget-put widget :glyph-up glyph)
601 (when down (widget-put widget :glyph-down down))
602 (when inactive (widget-put widget :glyph-inactive inactive))
603 (let ((help-echo (widget-get widget :help-echo)))
605 (let ((extent (extent-at (1- (point)) nil 'end-glyph))
606 (help-property (if (featurep 'balloon-help)
609 (set-extent-property extent help-property (if (stringp help-echo)
611 'widget-mouse-help))))))
613 ;;; Creating Widgets.
616 (defun widget-create (type &rest args)
617 "Create widget of TYPE.
618 The optional ARGS are additional keyword arguments."
619 (let ((widget (apply 'widget-convert type args)))
620 (widget-apply widget :create)
623 (defun widget-create-child-and-convert (parent type &rest args)
624 "As part of the widget PARENT, create a child widget TYPE.
625 The child is converted, using the keyword arguments ARGS."
626 (let ((widget (apply 'widget-convert type args)))
627 (widget-put widget :parent parent)
628 (unless (widget-get widget :indent)
629 (widget-put widget :indent (+ (or (widget-get parent :indent) 0)
630 (or (widget-get widget :extra-offset) 0)
631 (widget-get parent :offset))))
632 (widget-apply widget :create)
635 (defun widget-create-child (parent type)
636 "Create widget of TYPE."
637 (let ((widget (copy-sequence type)))
638 (widget-put widget :parent parent)
639 (unless (widget-get widget :indent)
640 (widget-put widget :indent (+ (or (widget-get parent :indent) 0)
641 (or (widget-get widget :extra-offset) 0)
642 (widget-get parent :offset))))
643 (widget-apply widget :create)
646 (defun widget-create-child-value (parent type value)
647 "Create widget of TYPE with value VALUE."
648 (let ((widget (copy-sequence type)))
649 (widget-put widget :value (widget-apply widget :value-to-internal value))
650 (widget-put widget :parent parent)
651 (unless (widget-get widget :indent)
652 (widget-put widget :indent (+ (or (widget-get parent :indent) 0)
653 (or (widget-get widget :extra-offset) 0)
654 (widget-get parent :offset))))
655 (widget-apply widget :create)
659 (defun widget-delete (widget)
661 (widget-apply widget :delete))
663 (defun widget-convert (type &rest args)
664 "Convert TYPE to a widget without inserting it in the buffer.
665 The optional ARGS are additional keyword arguments."
666 ;; Don't touch the type.
667 (let* ((widget (if (symbolp type)
669 (copy-sequence type)))
672 ;; First set the :args keyword.
673 (while (cdr current) ;Look in the type.
674 (let ((next (car (cdr current))))
675 (if (and (symbolp next) (eq (aref (symbol-name next) 0) ?:))
676 (setq current (cdr (cdr current)))
677 (setcdr current (list :args (cdr current)))
678 (setq current nil))))
679 (while args ;Look in the args.
680 (let ((next (nth 0 args)))
681 (if (and (symbolp next) (eq (aref (symbol-name next) 0) ?:))
682 (setq args (nthcdr 2 args))
683 (widget-put widget :args args)
685 ;; Then Convert the widget.
688 (let ((convert-widget (plist-get (cdr type) :convert-widget)))
690 (setq widget (funcall convert-widget widget))))
691 (setq type (get (car type) 'widget-type)))
692 ;; Finally set the keyword args.
694 (let ((next (nth 0 keys)))
695 (if (and (symbolp next) (eq (aref (symbol-name next) 0) ?:))
697 (widget-put widget next (nth 1 keys))
698 (setq keys (nthcdr 2 keys)))
700 ;; Convert the :value to internal format.
701 (if (widget-member widget :value)
702 (let ((value (widget-get widget :value)))
704 :value (widget-apply widget :value-to-internal value))))
705 ;; Return the newly create widget.
708 (defun widget-insert (&rest args)
709 "Call `insert' with ARGS and make the text read only."
710 (let ((inhibit-read-only t)
711 after-change-functions
714 (widget-specify-text from (point))))
716 ;;; Keymap and Commands.
718 (defvar widget-keymap nil
719 "Keymap containing useful binding for buffers containing widgets.
720 Recommended as a parent keymap for modes using widgets.")
722 (unless widget-keymap
723 (setq widget-keymap (make-sparse-keymap))
724 (define-key widget-keymap "\C-k" 'widget-kill-line)
725 (define-key widget-keymap "\t" 'widget-forward)
726 (define-key widget-keymap "\M-\t" 'widget-backward)
727 (define-key widget-keymap [(shift tab)] 'widget-backward)
728 (define-key widget-keymap [backtab] 'widget-backward)
729 (if (string-match "XEmacs" emacs-version)
732 (define-key widget-keymap [button1] 'widget-button1-click)
733 (define-key widget-keymap [button2] 'widget-button-click))
734 (define-key widget-keymap [down-mouse-2] 'widget-button-click))
735 (define-key widget-keymap "\C-m" 'widget-button-press))
737 (defvar widget-global-map global-map
738 "Keymap used for events the widget does not handle themselves.")
739 (make-variable-buffer-local 'widget-global-map)
741 (defvar widget-field-keymap nil
742 "Keymap used inside an editable field.")
744 (unless widget-field-keymap
745 (setq widget-field-keymap (copy-keymap widget-keymap))
746 (unless (string-match "XEmacs" (emacs-version))
747 (define-key widget-field-keymap [menu-bar] 'nil))
748 (define-key widget-field-keymap "\C-m" 'widget-field-activate)
749 (define-key widget-field-keymap "\C-a" 'widget-beginning-of-line)
750 (define-key widget-field-keymap "\C-e" 'widget-end-of-line)
751 (set-keymap-parent widget-field-keymap global-map))
753 (defvar widget-text-keymap nil
754 "Keymap used inside a text field.")
756 (unless widget-text-keymap
757 (setq widget-text-keymap (copy-keymap widget-keymap))
758 (unless (string-match "XEmacs" (emacs-version))
759 (define-key widget-text-keymap [menu-bar] 'nil))
760 (define-key widget-text-keymap "\C-a" 'widget-beginning-of-line)
761 (define-key widget-text-keymap "\C-e" 'widget-end-of-line)
762 (set-keymap-parent widget-text-keymap global-map))
764 (defun widget-field-activate (pos &optional event)
765 "Activate the ediable field at point."
767 (let ((field (get-text-property pos 'field)))
769 (widget-apply-action field event)
771 (lookup-key widget-global-map (this-command-keys))))))
773 (defface widget-button-pressed-face
777 (:bold t :underline t)))
778 "Face used for pressed buttons."
781 (defun widget-button-click (event)
782 "Activate button below mouse pointer."
784 (cond ((and (fboundp 'event-glyph)
786 (widget-glyph-click event))
787 ((widget-event-point event)
788 (let* ((pos (widget-event-point event))
789 (button (get-text-property pos 'button)))
791 (let ((begin (previous-single-property-change (1+ pos) 'button))
792 (end (next-single-property-change pos 'button))
795 (let ((track-mouse t))
796 (setq overlay (make-overlay begin end))
797 (overlay-put overlay 'face 'widget-button-pressed-face)
799 'mouse-face 'widget-button-pressed-face)
800 (unless (widget-apply button :mouse-down-action event)
801 (while (not (button-release-event-p event))
802 (setq event (if (fboundp 'read-event)
805 pos (widget-event-point event))
807 (eq (get-text-property pos 'button)
812 'widget-button-pressed-face)
815 'widget-button-pressed-face))
816 (overlay-put overlay 'face nil)
817 (overlay-put overlay 'mouse-face nil))))
820 (eq (get-text-property pos 'button) button))
821 (widget-apply-action button event)))
822 (delete-overlay overlay)))
824 (or (lookup-key widget-global-map [ button2 ])
825 (lookup-key widget-global-map [ down-mouse-2 ])
826 (lookup-key widget-global-map [ mouse-2]))))))
828 (message "You clicked somewhere weird."))))
830 (defun widget-button1-click (event)
831 "Activate glyph below mouse pointer."
833 (if (and (fboundp 'event-glyph)
835 (widget-glyph-click event)
836 (call-interactively (lookup-key widget-global-map (this-command-keys)))))
838 (defun widget-glyph-click (event)
839 "Handle click on a glyph."
840 (let* ((glyph (event-glyph event))
841 (widget (glyph-property glyph 'widget))
842 (extent (event-glyph-extent event))
843 (down-glyph (or (and widget (widget-get widget :glyph-down)) glyph))
844 (up-glyph (or (and widget (widget-get widget :glyph-up)) glyph))
846 ;; Wait for the release.
847 (while (not (button-release-event-p last))
848 (if (eq extent (event-glyph-extent last))
849 (set-extent-property extent 'end-glyph down-glyph)
850 (set-extent-property extent 'end-glyph up-glyph))
851 (setq last (next-event event)))
854 (set-extent-property extent 'end-glyph up-glyph))
855 ;; Apply widget action.
856 (when (eq extent (event-glyph-extent last))
857 (let ((widget (glyph-property (event-glyph event) 'widget)))
859 (message "You clicked on a glyph."))
860 ((not (widget-apply widget :active))
861 (message "This glyph is inactive."))
863 (widget-apply-action widget event)))))))
865 (defun widget-button-press (pos &optional event)
866 "Activate button at POS."
868 (let ((button (get-text-property pos 'button)))
870 (widget-apply-action button event)
871 (let ((command (lookup-key widget-global-map (this-command-keys))))
872 (when (commandp command)
873 (call-interactively command))))))
875 (defun widget-move (arg)
876 "Move point to the ARG next field or button.
877 ARG may be negative to move backward."
880 (let ((next (cond ((get-text-property (point) 'button)
881 (next-single-property-change (point) 'button))
882 ((get-text-property (point) 'field)
883 (next-single-property-change (point) 'field))
886 (if (null next) ; Widget extends to end. of buffer
887 (setq next (point-min)))
888 (let ((button (next-single-property-change next 'button))
889 (field (next-single-property-change next 'field)))
890 (cond ((or (get-text-property next 'button)
891 (get-text-property next 'field))
894 (goto-char (min button field)))
895 (button (goto-char button))
896 (field (goto-char field))
898 (let ((button (next-single-property-change (point-min) 'button))
899 (field (next-single-property-change (point-min) 'field)))
900 (cond ((and button field) (goto-char (min button field)))
901 (button (goto-char button))
902 (field (goto-char field))
904 (error "No buttons or fields found"))))))
905 (setq button (widget-at (point)))
906 (if (or (and button (widget-get button :tab-order)
907 (< (widget-get button :tab-order) 0))
908 (and button (not (widget-apply button :active))))
909 (setq arg (1+ arg))))))
911 (if (= (point-min) (point))
914 (let ((previous (cond ((get-text-property (1- (point)) 'button)
915 (previous-single-property-change (point) 'button))
916 ((get-text-property (1- (point)) 'field)
917 (previous-single-property-change (point) 'field))
920 (if (null previous) ; Widget extends to beg. of buffer
921 (setq previous (point-max)))
922 (let ((button (previous-single-property-change previous 'button))
923 (field (previous-single-property-change previous 'field)))
924 (cond ((and button field)
925 (goto-char (max button field)))
926 (button (goto-char button))
927 (field (goto-char field))
929 (let ((button (previous-single-property-change
930 (point-max) 'button))
931 (field (previous-single-property-change
932 (point-max) 'field)))
933 (cond ((and button field) (goto-char (max button field)))
934 (button (goto-char button))
935 (field (goto-char field))
937 (error "No buttons or fields found"))))))))
938 (let ((button (previous-single-property-change (point) 'button))
939 (field (previous-single-property-change (point) 'field)))
940 (cond ((and button field)
941 (goto-char (max button field)))
942 (button (goto-char button))
943 (field (goto-char field)))
944 (setq button (widget-at (point)))
945 (if (or (and button (widget-get button :tab-order)
946 (< (widget-get button :tab-order) 0))
947 (and button (not (widget-apply button :active))))
948 (setq arg (1- arg)))))
949 (widget-echo-help (point))
950 (run-hooks 'widget-move-hook))
952 (defun widget-forward (arg)
953 "Move point to the next field or button.
954 With optional ARG, move across that many fields."
956 (run-hooks 'widget-forward-hook)
959 (defun widget-backward (arg)
960 "Move point to the previous field or button.
961 With optional ARG, move across that many fields."
963 (run-hooks 'widget-backward-hook)
964 (widget-move (- arg)))
966 (defun widget-beginning-of-line ()
967 "Go to beginning of field or beginning of line, whichever is first."
969 (let ((bol (save-excursion (beginning-of-line) (point)))
970 (prev (previous-single-property-change (point) 'field)))
971 (goto-char (max bol (or prev bol)))))
973 (defun widget-end-of-line ()
974 "Go to end of field or end of line, whichever is first."
976 (let ((bol (save-excursion (end-of-line) (point)))
977 (prev (next-single-property-change (point) 'field)))
978 (goto-char (min bol (or prev bol)))))
980 (defun widget-kill-line ()
981 "Kill to end of field or end of line, whichever is first."
983 (let ((field (get-text-property (point) 'field))
984 (newline (save-excursion (search-forward "\n")))
985 (next (next-single-property-change (point) 'field)))
986 (if (and field (> newline next))
987 (kill-region (point) next)
988 (call-interactively 'kill-line))))
990 ;;; Setting up the buffer.
992 (defvar widget-field-new nil)
993 ;; List of all newly created editable fields in the buffer.
994 (make-variable-buffer-local 'widget-field-new)
996 (defvar widget-field-list nil)
997 ;; List of all editable fields in the buffer.
998 (make-variable-buffer-local 'widget-field-list)
1000 (defun widget-setup ()
1001 "Setup current buffer so editing string widgets works."
1002 (let ((inhibit-read-only t)
1003 (after-change-functions nil)
1005 (while widget-field-new
1006 (setq field (car widget-field-new)
1007 widget-field-new (cdr widget-field-new)
1008 widget-field-list (cons field widget-field-list))
1009 (let ((from (widget-get field :value-from))
1010 (to (widget-get field :value-to)))
1011 (widget-specify-field field from to)
1012 (move-marker from (1- from))
1013 (move-marker to (1+ to)))))
1015 ;; We need to maintain text properties and size of the editing fields.
1016 (make-local-variable 'after-change-functions)
1017 (if widget-field-list
1018 (setq after-change-functions '(widget-after-change))
1019 (setq after-change-functions nil)))
1021 (defvar widget-field-last nil)
1022 ;; Last field containing point.
1023 (make-variable-buffer-local 'widget-field-last)
1025 (defvar widget-field-was nil)
1026 ;; The widget data before the change.
1027 (make-variable-buffer-local 'widget-field-was)
1029 (defun widget-field-find (pos)
1030 ;; Find widget whose editing field is located at POS.
1031 ;; Return nil if POS is not inside and editing field.
1033 ;; This is only used in `widget-field-modified', since ordinarily
1034 ;; you would just test the field property.
1035 (let ((fields widget-field-list)
1038 (setq field (car fields)
1039 fields (cdr fields))
1040 (let ((from (widget-get field :value-from))
1041 (to (widget-get field :value-to)))
1042 (if (and from to (< from pos) (> to pos))
1047 (defun widget-after-change (from to old)
1048 ;; Adjust field size and text properties.
1050 (let ((field (widget-field-find from))
1051 (inhibit-read-only t))
1052 (cond ((null field))
1053 ((not (eq field (widget-field-find to)))
1055 (message "Error: `widget-after-change' called on two fields"))
1057 (let ((size (widget-get field :size)))
1059 (let ((begin (1+ (widget-get field :value-from)))
1060 (end (1- (widget-get field :value-to))))
1061 (widget-specify-field-update field begin end)
1062 (cond ((< (- end begin) size)
1066 (insert-char ?\ (- (+ begin size) end))
1067 (widget-specify-field-update field
1070 ((> (- end begin) size)
1071 ;; Field too large and
1072 (if (or (< (point) (+ begin size))
1074 ;; Point is outside extra space.
1075 (setq begin (+ begin size))
1076 ;; Point is within the extra space.
1077 (setq begin (point)))
1080 (while (and (eq (preceding-char) ?\ )
1082 (delete-backward-char 1))))))
1083 (widget-specify-field-update field from to)))
1084 (widget-apply field :notify field))))
1087 ;;; Widget Functions
1089 ;; These functions are used in the definition of multiple widgets.
1091 (defun widget-parent-action (widget &optional event)
1092 "Tell :parent of WIDGET to handle the :action.
1093 Optional EVENT is the event that triggered the action."
1094 (widget-apply (widget-get widget :parent) :action event))
1096 (defun widget-children-value-delete (widget)
1097 "Delete all :children and :buttons in WIDGET."
1098 (mapcar 'widget-delete (widget-get widget :children))
1099 (widget-put widget :children nil)
1100 (mapcar 'widget-delete (widget-get widget :buttons))
1101 (widget-put widget :buttons nil))
1103 (defun widget-children-validate (widget)
1104 "All the :children must be valid."
1105 (let ((children (widget-get widget :children))
1107 (while (and children (not found))
1108 (setq child (car children)
1109 children (cdr children)
1110 found (widget-apply child :validate)))
1113 (defun widget-types-convert-widget (widget)
1114 "Convert :args as widget types in WIDGET."
1115 (widget-put widget :args (mapcar 'widget-convert (widget-get widget :args)))
1118 (defun widget-value-convert-widget (widget)
1119 "Initialize :value from :args in WIDGET."
1120 (let ((args (widget-get widget :args)))
1122 (widget-put widget :value (car args))
1123 ;; Don't convert :value here, as this is done in `widget-convert'.
1124 ;; (widget-put widget :value (widget-apply widget
1125 ;; :value-to-internal (car args)))
1126 (widget-put widget :args nil)))
1129 (defun widget-value-value-get (widget)
1130 "Return the :value property of WIDGET."
1131 (widget-get widget :value))
1133 ;;; The `default' Widget.
1135 (define-widget 'default nil
1136 "Basic widget other widgets are derived from."
1137 :value-to-internal (lambda (widget value) value)
1138 :value-to-external (lambda (widget value) value)
1139 :create 'widget-default-create
1142 :format-handler 'widget-default-format-handler
1143 :button-face-get 'widget-default-button-face-get
1144 :sample-face-get 'widget-default-sample-face-get
1145 :delete 'widget-default-delete
1146 :value-set 'widget-default-value-set
1147 :value-inline 'widget-default-value-inline
1148 :menu-tag-get 'widget-default-menu-tag-get
1149 :validate (lambda (widget) nil)
1150 :active 'widget-default-active
1151 :activate 'widget-specify-active
1152 :deactivate 'widget-default-deactivate
1153 :mouse-down-action (lambda (widget event) nil)
1154 :action 'widget-default-action
1155 :notify 'widget-default-notify
1156 :prompt-value 'widget-default-prompt-value)
1158 (defun widget-default-create (widget)
1159 "Create WIDGET at point in the current buffer."
1160 (widget-specify-insert
1161 (let ((from (point))
1162 (tag (widget-get widget :tag))
1163 (glyph (widget-get widget :tag-glyph))
1164 (doc (widget-get widget :doc))
1165 button-begin button-end
1166 sample-begin sample-end
1169 (insert (widget-get widget :format))
1171 ;; Parse escapes in format.
1172 (while (re-search-forward "%\\(.\\)" nil t)
1173 (let ((escape (aref (match-string 1) 0)))
1174 (replace-match "" t t)
1175 (cond ((eq escape ?%)
1178 (setq button-begin (point)))
1180 (setq button-end (point)))
1182 (setq sample-begin (point)))
1184 (setq sample-end (point)))
1186 (when (widget-get widget :indent)
1188 (insert-char ? (widget-get widget :indent))))
1191 (widget-glyph-insert widget (or tag "image") glyph))
1195 (let ((standard-output (current-buffer)))
1196 (princ (widget-get widget :value))))))
1199 (setq doc-begin (point))
1201 (while (eq (preceding-char) ?\n)
1202 (delete-backward-char 1))
1204 (setq doc-end (point))))
1206 (if (and button-begin (not button-end))
1207 (widget-apply widget :value-create)
1208 (setq value-pos (point))))
1210 (widget-apply widget :format-handler escape)))))
1211 ;; Specify button, sample, and doc, and insert value.
1212 (and button-begin button-end
1213 (widget-specify-button widget button-begin button-end))
1214 (and sample-begin sample-end
1215 (widget-specify-sample widget sample-begin sample-end))
1216 (and doc-begin doc-end
1217 (widget-specify-doc widget doc-begin doc-end))
1219 (goto-char value-pos)
1220 (widget-apply widget :value-create)))
1221 (let ((from (copy-marker (point-min)))
1222 (to (copy-marker (point-max))))
1223 (widget-specify-text from to)
1224 (set-marker-insertion-type from t)
1225 (set-marker-insertion-type to nil)
1226 (widget-put widget :from from)
1227 (widget-put widget :to to)))
1228 (widget-clear-undo))
1230 (defun widget-default-format-handler (widget escape)
1231 ;; We recognize the %h escape by default.
1232 (let* ((buttons (widget-get widget :buttons))
1233 (doc-property (widget-get widget :documentation-property))
1234 (doc-try (cond ((widget-get widget :doc))
1235 ((symbolp doc-property)
1236 (documentation-property (widget-get widget :value)
1239 (funcall doc-property (widget-get widget :value)))))
1240 (doc-text (and (stringp doc-try)
1241 (> (length doc-try) 1)
1243 (cond ((eq escape ?h)
1245 (and (eq (preceding-char) ?\n)
1246 (widget-get widget :indent)
1247 (insert-char ? (widget-get widget :indent)))
1248 ;; The `*' in the beginning is redundant.
1249 (when (eq (aref doc-text 0) ?*)
1250 (setq doc-text (substring doc-text 1)))
1251 ;; Get rid of trailing newlines.
1252 (when (string-match "\n+\\'" doc-text)
1253 (setq doc-text (substring doc-text 0 (match-beginning 0))))
1254 (push (if (string-match "\n." doc-text)
1255 ;; Allow multiline doc to be hiden.
1256 (widget-create-child-and-convert
1259 (string-match "\\`.*" doc-text)
1260 (match-string 0 doc-text))
1261 :widget-doc doc-text
1263 ;; A single line is just inserted.
1264 (widget-create-child-and-convert
1265 widget 'item :format "%d" :doc doc-text nil))
1268 (error "Unknown escape `%c'" escape)))
1269 (widget-put widget :buttons buttons)))
1271 (defun widget-default-button-face-get (widget)
1272 ;; Use :button-face or widget-button-face
1273 (or (widget-get widget :button-face) 'widget-button-face))
1275 (defun widget-default-sample-face-get (widget)
1276 ;; Use :sample-face.
1277 (widget-get widget :sample-face))
1279 (defun widget-default-delete (widget)
1280 ;; Remove widget from the buffer.
1281 (let ((from (widget-get widget :from))
1282 (to (widget-get widget :to))
1283 (inhibit-read-only t)
1284 after-change-functions)
1285 (widget-apply widget :value-delete)
1287 ;; Kludge: this doesn't need to be true for empty formats.
1288 (delete-region from to))
1289 (set-marker from nil)
1290 (set-marker to nil))
1291 (widget-clear-undo))
1293 (defun widget-default-value-set (widget value)
1294 ;; Recreate widget with new value.
1296 (goto-char (widget-get widget :from))
1297 (widget-apply widget :delete)
1298 (widget-put widget :value value)
1299 (widget-apply widget :create)))
1301 (defun widget-default-value-inline (widget)
1302 ;; Wrap value in a list unless it is inline.
1303 (if (widget-get widget :inline)
1304 (widget-value widget)
1305 (list (widget-value widget))))
1307 (defun widget-default-menu-tag-get (widget)
1308 ;; Use tag or value for menus.
1309 (or (widget-get widget :menu-tag)
1310 (widget-get widget :tag)
1311 (widget-princ-to-string (widget-get widget :value))))
1313 (defun widget-default-active (widget)
1314 "Return t iff this widget active (user modifiable)."
1315 (and (not (widget-get widget :inactive))
1316 (let ((parent (widget-get widget :parent)))
1318 (widget-apply parent :active)))))
1320 (defun widget-default-deactivate (widget)
1321 "Make WIDGET inactive for user modifications."
1322 (widget-specify-inactive widget
1323 (widget-get widget :from)
1324 (widget-get widget :to)))
1326 (defun widget-default-action (widget &optional event)
1327 ;; Notify the parent when a widget change
1328 (let ((parent (widget-get widget :parent)))
1330 (widget-apply parent :notify widget event))))
1332 (defun widget-default-notify (widget child &optional event)
1333 ;; Pass notification to parent.
1334 (widget-default-action widget event))
1336 (defun widget-default-prompt-value (widget prompt value unbound)
1337 ;; Read an arbitrary value. Stolen from `set-variable'.
1338 ;; (let ((initial (if unbound
1340 ;; ;; It would be nice if we could do a `(cons val 1)' here.
1341 ;; (prin1-to-string (custom-quote value))))))
1342 (eval-minibuffer prompt ))
1344 ;;; The `item' Widget.
1346 (define-widget 'item 'default
1347 "Constant items for inclusion in other widgets."
1348 :convert-widget 'widget-value-convert-widget
1349 :value-create 'widget-item-value-create
1350 :value-delete 'ignore
1351 :value-get 'widget-value-value-get
1352 :match 'widget-item-match
1353 :match-inline 'widget-item-match-inline
1354 :action 'widget-item-action
1357 (defun widget-item-value-create (widget)
1358 ;; Insert the printed representation of the value.
1359 (let ((standard-output (current-buffer)))
1360 (princ (widget-get widget :value))))
1362 (defun widget-item-match (widget value)
1363 ;; Match if the value is the same.
1364 (equal (widget-get widget :value) value))
1366 (defun widget-item-match-inline (widget values)
1367 ;; Match if the value is the same.
1368 (let ((value (widget-get widget :value)))
1370 (<= (length value) (length values))
1371 (let ((head (subseq values 0 (length value))))
1372 (and (equal head value)
1373 (cons head (subseq values (length value))))))))
1375 (defun widget-item-action (widget &optional event)
1376 ;; Just notify itself.
1377 (widget-apply widget :notify widget event))
1379 ;;; The `push-button' Widget.
1381 (defcustom widget-push-button-gui t
1382 "If non nil, use GUI push buttons when available."
1386 ;; Cache already created GUI objects.
1387 (defvar widget-push-button-cache nil)
1389 (define-widget 'push-button 'item
1390 "A pushable button."
1391 :value-create 'widget-push-button-value-create
1395 (defun widget-push-button-value-create (widget)
1396 ;; Insert text representing the `on' and `off' states.
1397 (let* ((tag (or (widget-get widget :tag)
1398 (widget-get widget :value)))
1399 (text (format (widget-get widget :text-format) tag))
1400 (gui (cdr (assoc tag widget-push-button-cache))))
1401 (if (and (fboundp 'make-gui-button)
1402 (fboundp 'make-glyph)
1403 widget-push-button-gui
1404 (fboundp 'device-on-window-system-p)
1405 (device-on-window-system-p)
1406 (string-match "XEmacs" emacs-version))
1409 (setq gui (make-gui-button tag 'widget-gui-action widget))
1410 (push (cons tag gui) widget-push-button-cache))
1411 (widget-glyph-insert-glyph widget text
1412 (make-glyph (nth 0 (aref gui 1)))
1413 (make-glyph (nth 1 (aref gui 1)))
1414 (make-glyph (nth 2 (aref gui 1)))))
1417 (defun widget-gui-action (widget)
1418 "Apply :action for WIDGET."
1419 (widget-apply-action widget (this-command-keys)))
1421 ;;; The `link' Widget.
1423 (define-widget 'link 'item
1425 :help-echo "Follow the link."
1428 ;;; The `info-link' Widget.
1430 (define-widget 'info-link 'link
1431 "A link to an info file."
1432 :action 'widget-info-link-action)
1434 (defun widget-info-link-action (widget &optional event)
1435 "Open the info node specified by WIDGET."
1436 (Info-goto-node (widget-value widget)))
1438 ;;; The `url-link' Widget.
1440 (define-widget 'url-link 'link
1441 "A link to an www page."
1442 :action 'widget-url-link-action)
1444 (defun widget-url-link-action (widget &optional event)
1445 "Open the url specified by WIDGET."
1446 (require 'browse-url)
1447 (funcall browse-url-browser-function (widget-value widget)))
1449 ;;; The `editable-field' Widget.
1451 (define-widget 'editable-field 'default
1452 "An editable text field."
1453 :convert-widget 'widget-value-convert-widget
1454 :keymap widget-field-keymap
1457 :prompt-internal 'widget-field-prompt-internal
1458 :prompt-history 'widget-field-history
1459 :prompt-value 'widget-field-prompt-value
1460 :action 'widget-field-action
1461 :validate 'widget-field-validate
1464 :value-create 'widget-field-value-create
1465 :value-delete 'widget-field-value-delete
1466 :value-get 'widget-field-value-get
1467 :match 'widget-field-match)
1469 (defvar widget-field-history nil
1470 "History of field minibuffer edits.")
1472 (defun widget-field-prompt-internal (widget prompt initial history)
1473 ;; Read string for WIDGET promptinhg with PROMPT.
1474 ;; INITIAL is the initial input and HISTORY is a symbol containing
1475 ;; the earlier input.
1476 (read-string prompt initial history))
1478 (defun widget-field-prompt-value (widget prompt value unbound)
1479 ;; Prompt for a string.
1480 (let ((initial (if unbound
1482 (cons (widget-apply widget :value-to-internal
1484 (history (widget-get widget :prompt-history)))
1485 (let ((answer (widget-apply widget
1486 :prompt-internal prompt initial history)))
1487 (widget-apply widget :value-to-external answer))))
1489 (defun widget-field-action (widget &optional event)
1490 ;; Edit the value in the minibuffer.
1491 (let ((invalid (widget-apply widget :validate)))
1492 (let ((prompt (concat (widget-apply widget :menu-tag-get) ": "))
1493 (value (unless invalid
1494 (widget-value widget))))
1495 (let ((answer (widget-apply widget :prompt-value prompt value invalid) ))
1496 (widget-value-set widget answer)))
1497 (widget-apply widget :notify widget event)
1500 (defun widget-field-validate (widget)
1501 ;; Valid if the content matches `:valid-regexp'.
1503 (let ((value (widget-apply widget :value-get))
1504 (regexp (widget-get widget :valid-regexp)))
1505 (if (string-match regexp value)
1509 (defun widget-field-value-create (widget)
1510 ;; Create an editable text field.
1512 (let ((size (widget-get widget :size))
1513 (value (widget-get widget :value))
1517 (< (length value) size)
1518 (insert-char ?\ (- size (length value))))
1519 (unless (memq widget widget-field-list)
1520 (setq widget-field-new (cons widget widget-field-new)))
1521 (widget-put widget :value-to (copy-marker (point)))
1522 (set-marker-insertion-type (widget-get widget :value-to) nil)
1526 (widget-put widget :value-from (copy-marker from))
1527 (set-marker-insertion-type (widget-get widget :value-from) t)))
1529 (defun widget-field-value-delete (widget)
1530 ;; Remove the widget from the list of active editing fields.
1531 (setq widget-field-list (delq widget widget-field-list))
1532 ;; These are nil if the :format string doesn't contain `%v'.
1533 (when (widget-get widget :value-from)
1534 (set-marker (widget-get widget :value-from) nil))
1535 (when (widget-get widget :value-from)
1536 (set-marker (widget-get widget :value-to) nil)))
1538 (defun widget-field-value-get (widget)
1539 ;; Return current text in editing field.
1540 (let ((from (widget-get widget :value-from))
1541 (to (widget-get widget :value-to))
1542 (size (widget-get widget :size))
1543 (secret (widget-get widget :secret))
1544 (old (current-buffer)))
1547 (set-buffer (marker-buffer from))
1548 (setq from (1+ from)
1553 (eq (char-after (1- to)) ?\ ))
1555 (let ((result (buffer-substring-no-properties from to)))
1556 (when (string-match "XEmacs" emacs-version)
1557 ;; XEmacs 20.1 bug: b-s-n-p doesn't clear all properties.
1558 (setq result (format "%s" result)))
1561 (while (< (+ from index) to)
1563 (get-text-property (+ from index) 'secret))
1564 (setq index (1+ index)))))
1567 (widget-get widget :value))))
1569 (defun widget-field-match (widget value)
1570 ;; Match any string.
1573 ;;; The `text' Widget.
1575 (define-widget 'text 'editable-field
1576 :keymap widget-text-keymap
1577 "A multiline text area.")
1579 ;;; The `menu-choice' Widget.
1581 (define-widget 'menu-choice 'default
1582 "A menu of options."
1583 :convert-widget 'widget-types-convert-widget
1584 :format "%[%t%]: %v"
1587 :void '(item :format "invalid (%t)\n")
1588 :value-create 'widget-choice-value-create
1589 :value-delete 'widget-children-value-delete
1590 :value-get 'widget-choice-value-get
1591 :value-inline 'widget-choice-value-inline
1592 :mouse-down-action 'widget-choice-mouse-down-action
1593 :action 'widget-choice-action
1594 :error "Make a choice"
1595 :validate 'widget-choice-validate
1596 :match 'widget-choice-match
1597 :match-inline 'widget-choice-match-inline)
1599 (defun widget-choice-value-create (widget)
1600 ;; Insert the first choice that matches the value.
1601 (let ((value (widget-get widget :value))
1602 (args (widget-get widget :args))
1605 (setq current (car args)
1607 (when (widget-apply current :match value)
1608 (widget-put widget :children (list (widget-create-child-value
1609 widget current value)))
1610 (widget-put widget :choice current)
1614 (let ((void (widget-get widget :void)))
1615 (widget-put widget :children (list (widget-create-child-and-convert
1616 widget void :value value)))
1617 (widget-put widget :choice void)))))
1619 (defun widget-choice-value-get (widget)
1620 ;; Get value of the child widget.
1621 (widget-value (car (widget-get widget :children))))
1623 (defun widget-choice-value-inline (widget)
1624 ;; Get value of the child widget.
1625 (widget-apply (car (widget-get widget :children)) :value-inline))
1627 (defcustom widget-choice-toggle nil
1628 "If non-nil, a binary choice will just toggle between the values.
1629 Otherwise, the user will explicitly have to choose between the values
1630 when he activate the menu."
1634 (defun widget-choice-mouse-down-action (widget &optional event)
1635 ;; Return non-nil if we need a menu.
1636 (let ((args (widget-get widget :args))
1637 (old (widget-get widget :choice)))
1638 (cond ((not window-system)
1639 ;; No place to pop up a menu.
1641 ((not (or (fboundp 'x-popup-menu) (fboundp 'popup-menu)))
1642 ;; No way to pop up a menu.
1644 ((< (length args) 2)
1645 ;; Empty or singleton list, just return the value.
1647 ((> (length args) widget-menu-max-size)
1648 ;; Too long, prompt.
1650 ((> (length args) 2)
1651 ;; Reasonable sized list, use menu.
1653 ((and widget-choice-toggle (memq old args))
1657 ;; Ask which of the two.
1660 (defun widget-choice-action (widget &optional event)
1662 (let ((args (widget-get widget :args))
1663 (old (widget-get widget :choice))
1664 (tag (widget-apply widget :menu-tag-get))
1665 (completion-ignore-case (widget-get widget :case-fold))
1667 ;; Remember old value.
1668 (if (and old (not (widget-apply widget :validate)))
1669 (let* ((external (widget-value widget))
1670 (internal (widget-apply old :value-to-internal external)))
1671 (widget-put old :value internal)))
1674 (cond ((= (length args) 0)
1676 ((= (length args) 1)
1678 ((and widget-choice-toggle
1681 (if (eq old (nth 0 args))
1686 (setq current (car args)
1689 (cons (cons (widget-apply current :menu-tag-get)
1692 (widget-choose tag (reverse choices) event))))
1694 (widget-value-set widget
1695 (widget-apply current :value-to-external
1696 (widget-get current :value)))
1697 (widget-apply widget :notify widget event)
1700 (defun widget-choice-validate (widget)
1701 ;; Valid if we have made a valid choice.
1702 (let ((void (widget-get widget :void))
1703 (choice (widget-get widget :choice))
1704 (child (car (widget-get widget :children))))
1705 (if (eq void choice)
1707 (widget-apply child :validate))))
1709 (defun widget-choice-match (widget value)
1710 ;; Matches if one of the choices matches.
1711 (let ((args (widget-get widget :args))
1713 (while (and args (not found))
1714 (setq current (car args)
1716 found (widget-apply current :match value)))
1719 (defun widget-choice-match-inline (widget values)
1720 ;; Matches if one of the choices matches.
1721 (let ((args (widget-get widget :args))
1723 (while (and args (null found))
1724 (setq current (car args)
1726 found (widget-match-inline current values)))
1729 ;;; The `toggle' Widget.
1731 (define-widget 'toggle 'item
1732 "Toggle between two states."
1734 :value-create 'widget-toggle-value-create
1735 :action 'widget-toggle-action
1736 :match (lambda (widget value) t)
1740 (defun widget-toggle-value-create (widget)
1741 ;; Insert text representing the `on' and `off' states.
1742 (if (widget-value widget)
1743 (widget-glyph-insert widget
1744 (widget-get widget :on)
1745 (widget-get widget :on-glyph))
1746 (widget-glyph-insert widget
1747 (widget-get widget :off)
1748 (widget-get widget :off-glyph))))
1750 (defun widget-toggle-action (widget &optional event)
1752 (widget-value-set widget (not (widget-value widget)))
1753 (widget-apply widget :notify widget event))
1755 ;;; The `checkbox' Widget.
1757 (define-widget 'checkbox 'toggle
1758 "A checkbox toggle."
1764 :action 'widget-checkbox-action)
1766 (defun widget-checkbox-action (widget &optional event)
1767 "Toggle checkbox, notify parent, and set active state of sibling."
1768 (widget-toggle-action widget event)
1769 (let ((sibling (widget-get-sibling widget)))
1771 (if (widget-value widget)
1772 (widget-apply sibling :activate)
1773 (widget-apply sibling :deactivate)))))
1775 ;;; The `checklist' Widget.
1777 (define-widget 'checklist 'default
1778 "A multiple choice widget."
1779 :convert-widget 'widget-types-convert-widget
1782 :entry-format "%b %v"
1783 :menu-tag "checklist"
1785 :value-create 'widget-checklist-value-create
1786 :value-delete 'widget-children-value-delete
1787 :value-get 'widget-checklist-value-get
1788 :validate 'widget-checklist-validate
1789 :match 'widget-checklist-match
1790 :match-inline 'widget-checklist-match-inline)
1792 (defun widget-checklist-value-create (widget)
1793 ;; Insert all values
1794 (let ((alist (widget-checklist-match-find widget (widget-get widget :value)))
1795 (args (widget-get widget :args)))
1797 (widget-checklist-add-item widget (car args) (assq (car args) alist))
1798 (setq args (cdr args)))
1799 (widget-put widget :children (nreverse (widget-get widget :children)))))
1801 (defun widget-checklist-add-item (widget type chosen)
1802 ;; Create checklist item in WIDGET of type TYPE.
1803 ;; If the item is checked, CHOSEN is a cons whose cdr is the value.
1804 (and (eq (preceding-char) ?\n)
1805 (widget-get widget :indent)
1806 (insert-char ? (widget-get widget :indent)))
1807 (widget-specify-insert
1808 (let* ((children (widget-get widget :children))
1809 (buttons (widget-get widget :buttons))
1810 (button-args (or (widget-get type :sibling-args)
1811 (widget-get widget :button-args)))
1814 (insert (widget-get widget :entry-format))
1816 ;; Parse % escapes in format.
1817 (while (re-search-forward "%\\([bv%]\\)" nil t)
1818 (let ((escape (aref (match-string 1) 0)))
1819 (replace-match "" t t)
1820 (cond ((eq escape ?%)
1823 (setq button (apply 'widget-create-child-and-convert
1825 :value (not (null chosen))
1830 (let ((child (widget-create-child widget type)))
1831 (widget-apply child :deactivate)
1833 ((widget-get type :inline)
1834 (widget-create-child-value
1835 widget type (cdr chosen)))
1837 (widget-create-child-value
1838 widget type (car (cdr chosen)))))))
1840 (error "Unknown escape `%c'" escape)))))
1841 ;; Update properties.
1842 (and button child (widget-put child :button button))
1843 (and button (widget-put widget :buttons (cons button buttons)))
1844 (and child (widget-put widget :children (cons child children))))))
1846 (defun widget-checklist-match (widget values)
1847 ;; All values must match a type in the checklist.
1849 (null (cdr (widget-checklist-match-inline widget values)))))
1851 (defun widget-checklist-match-inline (widget values)
1852 ;; Find the values which match a type in the checklist.
1853 (let ((greedy (widget-get widget :greedy))
1854 (args (copy-sequence (widget-get widget :args)))
1857 (let ((answer (widget-checklist-match-up args values)))
1859 (let ((vals (widget-match-inline answer values)))
1860 (setq found (append found (car vals))
1862 args (delq answer args))))
1864 (setq rest (append rest (list (car values)))
1865 values (cdr values)))
1867 (setq rest (append rest values)
1871 (defun widget-checklist-match-find (widget vals)
1872 ;; Find the vals which match a type in the checklist.
1873 ;; Return an alist of (TYPE MATCH).
1874 (let ((greedy (widget-get widget :greedy))
1875 (args (copy-sequence (widget-get widget :args)))
1878 (let ((answer (widget-checklist-match-up args vals)))
1880 (let ((match (widget-match-inline answer vals)))
1881 (setq found (cons (cons answer (car match)) found)
1883 args (delq answer args))))
1885 (setq vals (cdr vals)))
1890 (defun widget-checklist-match-up (args vals)
1891 ;; Rerturn the first type from ARGS that matches VALS.
1892 (let (current found)
1893 (while (and args (null found))
1894 (setq current (car args)
1896 found (widget-match-inline current vals)))
1901 (defun widget-checklist-value-get (widget)
1902 ;; The values of all selected items.
1903 (let ((children (widget-get widget :children))
1906 (setq child (car children)
1907 children (cdr children))
1908 (if (widget-value (widget-get child :button))
1909 (setq result (append result (widget-apply child :value-inline)))))
1912 (defun widget-checklist-validate (widget)
1913 ;; Ticked chilren must be valid.
1914 (let ((children (widget-get widget :children))
1916 (while (and children (not found))
1917 (setq child (car children)
1918 children (cdr children)
1919 button (widget-get child :button)
1920 found (and (widget-value button)
1921 (widget-apply child :validate))))
1924 ;;; The `option' Widget
1926 (define-widget 'option 'checklist
1927 "An widget with an optional item."
1930 ;;; The `choice-item' Widget.
1932 (define-widget 'choice-item 'item
1933 "Button items that delegate action events to their parents."
1934 :action 'widget-parent-action
1935 :format "%[%t%] \n")
1937 ;;; The `radio-button' Widget.
1939 (define-widget 'radio-button 'toggle
1940 "A radio button for use in the `radio' widget."
1941 :notify 'widget-radio-button-notify
1946 :off-glyph "radio0")
1948 (defun widget-radio-button-notify (widget child &optional event)
1950 (widget-apply (widget-get widget :parent) :action widget event))
1952 ;;; The `radio-button-choice' Widget.
1954 (define-widget 'radio-button-choice 'default
1955 "Select one of multiple options."
1956 :convert-widget 'widget-types-convert-widget
1959 :entry-format "%b %v"
1961 :value-create 'widget-radio-value-create
1962 :value-delete 'widget-children-value-delete
1963 :value-get 'widget-radio-value-get
1964 :value-inline 'widget-radio-value-inline
1965 :value-set 'widget-radio-value-set
1966 :error "You must push one of the buttons"
1967 :validate 'widget-radio-validate
1968 :match 'widget-choice-match
1969 :match-inline 'widget-choice-match-inline
1970 :action 'widget-radio-action)
1972 (defun widget-radio-value-create (widget)
1973 ;; Insert all values
1974 (let ((args (widget-get widget :args))
1977 (setq arg (car args)
1979 (widget-radio-add-item widget arg))))
1981 (defun widget-radio-add-item (widget type)
1982 "Add to radio widget WIDGET a new radio button item of type TYPE."
1983 ;; (setq type (widget-convert type))
1984 (and (eq (preceding-char) ?\n)
1985 (widget-get widget :indent)
1986 (insert-char ? (widget-get widget :indent)))
1987 (widget-specify-insert
1988 (let* ((value (widget-get widget :value))
1989 (children (widget-get widget :children))
1990 (buttons (widget-get widget :buttons))
1991 (button-args (or (widget-get type :sibling-args)
1992 (widget-get widget :button-args)))
1994 (chosen (and (null (widget-get widget :choice))
1995 (widget-apply type :match value)))
1997 (insert (widget-get widget :entry-format))
1999 ;; Parse % escapes in format.
2000 (while (re-search-forward "%\\([bv%]\\)" nil t)
2001 (let ((escape (aref (match-string 1) 0)))
2002 (replace-match "" t t)
2003 (cond ((eq escape ?%)
2006 (setq button (apply 'widget-create-child-and-convert
2007 widget 'radio-button
2008 :value (not (null chosen))
2011 (setq child (if chosen
2012 (widget-create-child-value
2014 (widget-create-child widget type)))
2016 (widget-apply child :deactivate)))
2018 (error "Unknown escape `%c'" escape)))))
2019 ;; Update properties.
2021 (widget-put widget :choice type))
2023 (widget-put child :button button)
2024 (widget-put widget :buttons (nconc buttons (list button))))
2026 (widget-put widget :children (nconc children (list child))))
2029 (defun widget-radio-value-get (widget)
2030 ;; Get value of the child widget.
2031 (let ((chosen (widget-radio-chosen widget)))
2032 (and chosen (widget-value chosen))))
2034 (defun widget-radio-chosen (widget)
2035 "Return the widget representing the chosen radio button."
2036 (let ((children (widget-get widget :children))
2039 (setq current (car children)
2040 children (cdr children))
2041 (let* ((button (widget-get current :button))
2042 (value (widget-apply button :value-get)))
2048 (defun widget-radio-value-inline (widget)
2049 ;; Get value of the child widget.
2050 (let ((children (widget-get widget :children))
2053 (setq current (car children)
2054 children (cdr children))
2055 (let* ((button (widget-get current :button))
2056 (value (widget-apply button :value-get)))
2058 (setq found (widget-apply current :value-inline)
2062 (defun widget-radio-value-set (widget value)
2063 ;; We can't just delete and recreate a radio widget, since children
2064 ;; can be added after the original creation and won't be recreated
2066 (let ((children (widget-get widget :children))
2069 (setq current (car children)
2070 children (cdr children))
2071 (let* ((button (widget-get current :button))
2072 (match (and (not found)
2073 (widget-apply current :match value))))
2074 (widget-value-set button match)
2077 (widget-value-set current value)
2078 (widget-apply current :activate))
2079 (widget-apply current :deactivate))
2080 (setq found (or found match))))))
2082 (defun widget-radio-validate (widget)
2083 ;; Valid if we have made a valid choice.
2084 (let ((children (widget-get widget :children))
2085 current found button)
2086 (while (and children (not found))
2087 (setq current (car children)
2088 children (cdr children)
2089 button (widget-get current :button)
2090 found (widget-apply button :value-get)))
2092 (widget-apply current :validate)
2095 (defun widget-radio-action (widget child event)
2096 ;; Check if a radio button was pressed.
2097 (let ((children (widget-get widget :children))
2098 (buttons (widget-get widget :buttons))
2100 (when (memq child buttons)
2102 (setq current (car children)
2103 children (cdr children))
2104 (let* ((button (widget-get current :button)))
2105 (cond ((eq child button)
2106 (widget-value-set button t)
2107 (widget-apply current :activate))
2108 ((widget-value button)
2109 (widget-value-set button nil)
2110 (widget-apply current :deactivate)))))))
2111 ;; Pass notification to parent.
2112 (widget-apply widget :notify child event))
2114 ;;; The `insert-button' Widget.
2116 (define-widget 'insert-button 'push-button
2117 "An insert button for the `editable-list' widget."
2119 :help-echo "Insert a new item into the list at this position."
2120 :action 'widget-insert-button-action)
2122 (defun widget-insert-button-action (widget &optional event)
2123 ;; Ask the parent to insert a new item.
2124 (widget-apply (widget-get widget :parent)
2125 :insert-before (widget-get widget :widget)))
2127 ;;; The `delete-button' Widget.
2129 (define-widget 'delete-button 'push-button
2130 "A delete button for the `editable-list' widget."
2132 :help-echo "Delete this item from the list."
2133 :action 'widget-delete-button-action)
2135 (defun widget-delete-button-action (widget &optional event)
2136 ;; Ask the parent to insert a new item.
2137 (widget-apply (widget-get widget :parent)
2138 :delete-at (widget-get widget :widget)))
2140 ;;; The `editable-list' Widget.
2142 (defcustom widget-editable-list-gui nil
2143 "If non nil, use GUI push-buttons in editable list when available."
2147 (define-widget 'editable-list 'default
2148 "A variable list of widgets of the same type."
2149 :convert-widget 'widget-types-convert-widget
2152 :format-handler 'widget-editable-list-format-handler
2153 :entry-format "%i %d %v"
2154 :menu-tag "editable-list"
2155 :value-create 'widget-editable-list-value-create
2156 :value-delete 'widget-children-value-delete
2157 :value-get 'widget-editable-list-value-get
2158 :validate 'widget-children-validate
2159 :match 'widget-editable-list-match
2160 :match-inline 'widget-editable-list-match-inline
2161 :insert-before 'widget-editable-list-insert-before
2162 :delete-at 'widget-editable-list-delete-at)
2164 (defun widget-editable-list-format-handler (widget escape)
2165 ;; We recognize the insert button.
2166 (let ((widget-push-button-gui widget-editable-list-gui))
2167 (cond ((eq escape ?i)
2168 (and (widget-get widget :indent)
2169 (insert-char ? (widget-get widget :indent)))
2170 (apply 'widget-create-child-and-convert
2171 widget 'insert-button
2172 (widget-get widget :append-button-args)))
2174 (widget-default-format-handler widget escape)))))
2176 (defun widget-editable-list-value-create (widget)
2177 ;; Insert all values
2178 (let* ((value (widget-get widget :value))
2179 (type (nth 0 (widget-get widget :args)))
2180 (inlinep (widget-get type :inline))
2182 (widget-put widget :value-pos (copy-marker (point)))
2183 (set-marker-insertion-type (widget-get widget :value-pos) t)
2185 (let ((answer (widget-match-inline type value)))
2187 (setq children (cons (widget-editable-list-entry-create
2196 (widget-put widget :children (nreverse children))))
2198 (defun widget-editable-list-value-get (widget)
2199 ;; Get value of the child widget.
2200 (apply 'append (mapcar (lambda (child) (widget-apply child :value-inline))
2201 (widget-get widget :children))))
2203 (defun widget-editable-list-match (widget value)
2204 ;; Value must be a list and all the members must match the type.
2206 (null (cdr (widget-editable-list-match-inline widget value)))))
2208 (defun widget-editable-list-match-inline (widget value)
2209 (let ((type (nth 0 (widget-get widget :args)))
2212 (while (and value ok)
2213 (let ((answer (widget-match-inline type value)))
2215 (setq found (append found (car answer))
2218 (cons found value)))
2220 (defun widget-editable-list-insert-before (widget before)
2221 ;; Insert a new child in the list of children.
2223 (let ((children (widget-get widget :children))
2224 (inhibit-read-only t)
2225 after-change-functions)
2227 (goto-char (widget-get before :entry-from)))
2229 (goto-char (widget-get widget :value-pos))))
2230 (let ((child (widget-editable-list-entry-create
2232 (when (< (widget-get child :entry-from) (widget-get widget :from))
2233 (set-marker (widget-get widget :from)
2234 (widget-get child :entry-from)))
2235 (widget-specify-text (widget-get child :entry-from)
2236 (widget-get child :entry-to))
2237 (if (eq (car children) before)
2238 (widget-put widget :children (cons child children))
2239 (while (not (eq (car (cdr children)) before))
2240 (setq children (cdr children)))
2241 (setcdr children (cons child (cdr children)))))))
2243 widget (widget-apply widget :notify widget))
2245 (defun widget-editable-list-delete-at (widget child)
2246 ;; Delete child from list of children.
2248 (let ((buttons (copy-sequence (widget-get widget :buttons)))
2250 (inhibit-read-only t)
2251 after-change-functions)
2253 (setq button (car buttons)
2254 buttons (cdr buttons))
2255 (when (eq (widget-get button :widget) child)
2257 :buttons (delq button (widget-get widget :buttons)))
2258 (widget-delete button))))
2259 (let ((entry-from (widget-get child :entry-from))
2260 (entry-to (widget-get child :entry-to))
2261 (inhibit-read-only t)
2262 after-change-functions)
2263 (widget-delete child)
2264 (delete-region entry-from entry-to)
2265 (set-marker entry-from nil)
2266 (set-marker entry-to nil))
2267 (widget-put widget :children (delq child (widget-get widget :children))))
2269 (widget-apply widget :notify widget))
2271 (defun widget-editable-list-entry-create (widget value conv)
2272 ;; Create a new entry to the list.
2273 (let ((type (nth 0 (widget-get widget :args)))
2274 (widget-push-button-gui widget-editable-list-gui)
2275 child delete insert)
2276 (widget-specify-insert
2278 (and (widget-get widget :indent)
2279 (insert-char ? (widget-get widget :indent)))
2280 (insert (widget-get widget :entry-format)))
2281 ;; Parse % escapes in format.
2282 (while (re-search-forward "%\\(.\\)" nil t)
2283 (let ((escape (aref (match-string 1) 0)))
2284 (replace-match "" t t)
2285 (cond ((eq escape ?%)
2288 (setq insert (apply 'widget-create-child-and-convert
2289 widget 'insert-button
2290 (widget-get widget :insert-button-args))))
2292 (setq delete (apply 'widget-create-child-and-convert
2293 widget 'delete-button
2294 (widget-get widget :delete-button-args))))
2297 (setq child (widget-create-child-value
2299 (setq child (widget-create-child widget type))))
2301 (error "Unknown escape `%c'" escape)))))
2303 :buttons (cons delete
2305 (widget-get widget :buttons))))
2306 (let ((entry-from (copy-marker (point-min)))
2307 (entry-to (copy-marker (point-max))))
2308 (widget-specify-text entry-from entry-to)
2309 (set-marker-insertion-type entry-from t)
2310 (set-marker-insertion-type entry-to nil)
2311 (widget-put child :entry-from entry-from)
2312 (widget-put child :entry-to entry-to)))
2313 (widget-put insert :widget child)
2314 (widget-put delete :widget child)
2317 ;;; The `group' Widget.
2319 (define-widget 'group 'default
2320 "A widget which group other widgets inside."
2321 :convert-widget 'widget-types-convert-widget
2323 :value-create 'widget-group-value-create
2324 :value-delete 'widget-children-value-delete
2325 :value-get 'widget-editable-list-value-get
2326 :validate 'widget-children-validate
2327 :match 'widget-group-match
2328 :match-inline 'widget-group-match-inline)
2330 (defun widget-group-value-create (widget)
2331 ;; Create each component.
2332 (let ((args (widget-get widget :args))
2333 (value (widget-get widget :value))
2334 arg answer children)
2336 (setq arg (car args)
2338 answer (widget-match-inline arg value)
2340 (and (eq (preceding-char) ?\n)
2341 (widget-get widget :indent)
2342 (insert-char ? (widget-get widget :indent)))
2343 (push (cond ((null answer)
2344 (widget-create-child widget arg))
2345 ((widget-get arg :inline)
2346 (widget-create-child-value widget arg (car answer)))
2348 (widget-create-child-value widget arg (car (car answer)))))
2350 (widget-put widget :children (nreverse children))))
2352 (defun widget-group-match (widget values)
2353 ;; Match if the components match.
2355 (let ((match (widget-group-match-inline widget values)))
2356 (and match (null (cdr match))))))
2358 (defun widget-group-match-inline (widget vals)
2359 ;; Match if the components match.
2360 (let ((args (widget-get widget :args))
2361 argument answer found)
2363 (setq argument (car args)
2365 answer (widget-match-inline argument vals))
2367 (setq vals (cdr answer)
2368 found (append found (car answer)))
2375 ;;; The `widget-help' Widget.
2377 (define-widget 'widget-help 'push-button
2378 "The widget documentation button."
2379 :format "%[[%t]%] %d"
2380 :help-echo "Toggle display of documentation."
2381 :action 'widget-help-action)
2383 (defun widget-help-action (widget &optional event)
2384 "Toggle documentation for WIDGET."
2385 (let ((old (widget-get widget :doc))
2386 (new (widget-get widget :widget-doc)))
2387 (widget-put widget :doc new)
2388 (widget-put widget :widget-doc old))
2389 (widget-value-set widget (widget-value widget)))
2391 ;;; The Sexp Widgets.
2393 (define-widget 'const 'item
2394 "An immutable sexp."
2395 :prompt-value 'widget-const-prompt-value
2398 (defun widget-const-prompt-value (widget prompt value unbound)
2399 ;; Return the value of the const.
2400 (widget-value widget))
2402 (define-widget 'function-item 'const
2403 "An immutable function name."
2405 :documentation-property (lambda (symbol)
2407 (documentation symbol t)
2410 (define-widget 'variable-item 'const
2411 "An immutable variable name."
2413 :documentation-property 'variable-documentation)
2415 (defvar widget-string-prompt-value-history nil
2416 "History of input to `widget-string-prompt-value'.")
2418 (define-widget 'string 'editable-field
2421 :format "%{%t%}: %v"
2422 :prompt-history 'widget-string-prompt-value-history)
2424 (define-widget 'regexp 'string
2425 "A regular expression."
2426 :match 'widget-regexp-match
2427 :validate 'widget-regexp-validate
2430 (defun widget-regexp-match (widget value)
2431 ;; Match valid regexps.
2432 (and (stringp value)
2435 (string-match value ""))
2438 (defun widget-regexp-validate (widget)
2439 "Check that the value of WIDGET is a valid regexp."
2440 (let ((val (widget-value widget)))
2441 (condition-case data
2443 (string-match val ""))
2444 (error (widget-put widget :error (error-message-string data))
2447 (define-widget 'file 'string
2449 It will read a file name from the minibuffer when activated."
2450 :prompt-value 'widget-file-prompt-value
2451 :format "%{%t%}: %v"
2453 :action 'widget-file-action)
2455 (defun widget-file-prompt-value (widget prompt value unbound)
2456 ;; Read file from minibuffer.
2457 (abbreviate-file-name
2459 (read-file-name prompt)
2460 (let ((prompt2 (format "%s (default %s) " prompt value))
2461 (dir (file-name-directory value))
2462 (file (file-name-nondirectory value))
2463 (must-match (widget-get widget :must-match)))
2464 (read-file-name prompt2 dir nil must-match file)))))
2466 (defun widget-file-action (widget &optional event)
2467 ;; Read a file name from the minibuffer.
2468 (let* ((value (widget-value widget))
2469 (dir (file-name-directory value))
2470 (file (file-name-nondirectory value))
2471 (menu-tag (widget-apply widget :menu-tag-get))
2472 (must-match (widget-get widget :must-match))
2473 (answer (read-file-name (concat menu-tag ": (default `" value "') ")
2474 dir nil must-match file)))
2475 (widget-value-set widget (abbreviate-file-name answer))
2476 (widget-apply widget :notify widget event)
2479 (define-widget 'directory 'file
2480 "A directory widget.
2481 It will read a directory name from the minibuffer when activated."
2484 (defvar widget-symbol-prompt-value-history nil
2485 "History of input to `widget-symbol-prompt-value'.")
2487 (define-widget 'symbol 'editable-field
2491 :format "%{%t%}: %v"
2492 :match (lambda (widget value) (symbolp value))
2493 :prompt-internal 'widget-symbol-prompt-internal
2494 :prompt-match 'symbolp
2495 :prompt-history 'widget-symbol-prompt-value-history
2496 :value-to-internal (lambda (widget value)
2500 :value-to-external (lambda (widget value)
2505 (defun widget-symbol-prompt-internal (widget prompt initial history)
2506 ;; Read file from minibuffer.
2507 (let ((answer (completing-read prompt obarray
2508 (widget-get widget :prompt-match)
2509 nil initial history)))
2510 (if (and (stringp answer)
2511 (not (zerop (length answer))))
2513 (error "No value"))))
2515 (defvar widget-function-prompt-value-history nil
2516 "History of input to `widget-function-prompt-value'.")
2518 (define-widget 'function 'sexp
2520 :prompt-value 'widget-field-prompt-value
2521 :prompt-internal 'widget-symbol-prompt-internal
2522 :prompt-match 'fboundp
2523 :prompt-history 'widget-function-prompt-value-history
2524 :action 'widget-field-action
2527 (defvar widget-variable-prompt-value-history nil
2528 "History of input to `widget-variable-prompt-value'.")
2530 (define-widget 'variable 'symbol
2531 ;; Should complete on variables.
2533 :prompt-match 'boundp
2534 :prompt-history 'widget-variable-prompt-value-history
2537 (define-widget 'sexp 'editable-field
2538 "An arbitrary lisp expression."
2539 :tag "Lisp expression"
2540 :format "%{%t%}: %v"
2542 :validate 'widget-sexp-validate
2543 :match (lambda (widget value) t)
2544 :value-to-internal 'widget-sexp-value-to-internal
2545 :value-to-external (lambda (widget value) (read value))
2546 :prompt-history 'widget-sexp-prompt-value-history
2547 :prompt-value 'widget-sexp-prompt-value)
2549 (defun widget-sexp-value-to-internal (widget value)
2550 ;; Use pp for printer representation.
2551 (let ((pp (pp-to-string value)))
2552 (while (string-match "\n\\'" pp)
2553 (setq pp (substring pp 0 -1)))
2554 (if (or (string-match "\n\\'" pp)
2559 (defun widget-sexp-validate (widget)
2560 ;; Valid if we can read the string and there is no junk left after it.
2562 (let ((buffer (set-buffer (get-buffer-create " *Widget Scratch*"))))
2564 (insert (widget-apply widget :value-get))
2565 (goto-char (point-min))
2566 (condition-case data
2567 (let ((value (read buffer)))
2569 (if (widget-apply widget :match value)
2571 (widget-put widget :error (widget-get widget :type-error))
2574 :error (format "Junk at end of expression: %s"
2575 (buffer-substring (point)
2578 (error (widget-put widget :error (error-message-string data))
2581 (defvar widget-sexp-prompt-value-history nil
2582 "History of input to `widget-sexp-prompt-value'.")
2584 (defun widget-sexp-prompt-value (widget prompt value unbound)
2585 ;; Read an arbitrary sexp.
2586 (let ((found (read-string prompt
2587 (if unbound nil (cons (prin1-to-string value) 0))
2588 (widget-get widget :prompt-history))))
2590 (let ((buffer (set-buffer (get-buffer-create " *Widget Scratch*"))))
2593 (goto-char (point-min))
2594 (let ((answer (read buffer)))
2596 (error "Junk at end of expression: %s"
2597 (buffer-substring (point) (point-max))))
2600 (define-widget 'integer 'sexp
2604 :type-error "This field should contain an integer"
2605 :value-to-internal (lambda (widget value)
2606 (if (integerp value)
2607 (prin1-to-string value)
2609 :match (lambda (widget value) (integerp value)))
2611 (define-widget 'character 'editable-field
2616 :format "%{%t%}: %v\n"
2617 :valid-regexp "\\`.\\'"
2618 :error "This field should contain a single character"
2619 :value-to-internal (lambda (widget value)
2622 (char-to-string value)))
2623 :value-to-external (lambda (widget value)
2627 :match (lambda (widget value)
2628 (if (fboundp 'characterp)
2632 (define-widget 'number 'sexp
2633 "A floating point number."
2636 :type-error "This field should contain a number"
2637 :value-to-internal (lambda (widget value)
2639 (prin1-to-string value)
2641 :match (lambda (widget value) (numberp value)))
2643 (define-widget 'list 'group
2646 :format "%{%t%}:\n%v")
2648 (define-widget 'vector 'group
2651 :format "%{%t%}:\n%v"
2652 :match 'widget-vector-match
2653 :value-to-internal (lambda (widget value) (append value nil))
2654 :value-to-external (lambda (widget value) (apply 'vector value)))
2656 (defun widget-vector-match (widget value)
2657 (and (vectorp value)
2658 (widget-group-match widget
2659 (widget-apply widget :value-to-internal value))))
2661 (define-widget 'cons 'group
2664 :format "%{%t%}:\n%v"
2665 :match 'widget-cons-match
2666 :value-to-internal (lambda (widget value)
2667 (list (car value) (cdr value)))
2668 :value-to-external (lambda (widget value)
2669 (cons (nth 0 value) (nth 1 value))))
2671 (defun widget-cons-match (widget value)
2673 (widget-group-match widget
2674 (widget-apply widget :value-to-internal value))))
2676 (define-widget 'choice 'menu-choice
2677 "A union of several sexp types."
2679 :format "%[%t%]: %v"
2680 :prompt-value 'widget-choice-prompt-value)
2682 (defun widget-choice-prompt-value (widget prompt value unbound)
2684 (let ((args (widget-get widget :args))
2685 (completion-ignore-case (widget-get widget :case-fold))
2686 current choices old)
2687 ;; Find the first arg that match VALUE.
2690 (if (widget-apply (car look) :match value)
2691 (setq old (car look)
2693 (setq look (cdr look)))))
2696 (cond ((= (length args) 0)
2698 ((= (length args) 1)
2700 ((and (= (length args) 2)
2702 (if (eq old (nth 0 args))
2707 (setq current (car args)
2710 (cons (cons (widget-apply current :menu-tag-get)
2713 (let ((val (completing-read prompt choices nil t)))
2715 (let ((try (try-completion val choices)))
2718 (cdr (assoc val choices)))
2721 (widget-prompt-value current prompt nil t)
2724 (define-widget 'radio 'radio-button-choice
2725 "A union of several sexp types."
2727 :format "%{%t%}:\n%v"
2728 :prompt-value 'widget-choice-prompt-value)
2730 (define-widget 'repeat 'editable-list
2731 "A variable length homogeneous list."
2733 :format "%{%t%}:\n%v%i\n")
2735 (define-widget 'set 'checklist
2736 "A list of members from a fixed set."
2738 :format "%{%t%}:\n%v")
2740 (define-widget 'boolean 'toggle
2741 "To be nil or non-nil, that is the question."
2743 :prompt-value 'widget-boolean-prompt-value
2744 :format "%[%t%]: %v\n")
2746 (defun widget-boolean-prompt-value (widget prompt value unbound)
2747 ;; Toggle a boolean.
2750 ;;; The `color' Widget.
2752 (define-widget 'color-item 'choice-item
2753 "A color name (with sample)."
2754 :format "%v (%{sample%})\n"
2755 :sample-face-get 'widget-color-item-button-face-get)
2757 (defun widget-color-item-button-face-get (widget)
2758 ;; We create a face from the value.
2761 (facemenu-get-face (intern (concat "fg:" (widget-value widget))))
2764 (define-widget 'color 'push-button
2765 "Choose a color name (with sample)."
2766 :format "%[%t%]: %v"
2769 :value-create 'widget-color-value-create
2770 :value-delete 'widget-children-value-delete
2771 :value-get 'widget-color-value-get
2772 :value-set 'widget-color-value-set
2773 :action 'widget-color-action
2774 :match 'widget-field-match
2777 (defvar widget-color-choice-list nil)
2778 ;; Variable holding the possible colors.
2780 (defun widget-color-choice-list ()
2781 (unless widget-color-choice-list
2782 (setq widget-color-choice-list
2783 (mapcar '(lambda (color) (list color))
2784 (x-defined-colors))))
2785 widget-color-choice-list)
2787 (defun widget-color-value-create (widget)
2788 (let ((child (widget-create-child-and-convert
2789 widget 'color-item (widget-get widget :value))))
2790 (widget-put widget :children (list child))))
2792 (defun widget-color-value-get (widget)
2793 ;; Pass command to first child.
2794 (widget-apply (car (widget-get widget :children)) :value-get))
2796 (defun widget-color-value-set (widget value)
2797 ;; Pass command to first child.
2798 (widget-apply (car (widget-get widget :children)) :value-set value))
2800 (defvar widget-color-history nil
2801 "History of entered colors")
2803 (defun widget-color-action (widget &optional event)
2804 ;; Prompt for a color.
2805 (let* ((tag (widget-apply widget :menu-tag-get))
2806 (prompt (concat tag ": "))
2807 (answer (cond ((string-match "XEmacs" emacs-version)
2808 (read-color prompt))
2809 ((fboundp 'x-defined-colors)
2810 (completing-read (concat tag ": ")
2811 (widget-color-choice-list)
2812 nil nil nil 'widget-color-history))
2814 (read-string prompt (widget-value widget))))))
2815 (unless (zerop (length answer))
2816 (widget-value-set widget answer)
2817 (widget-apply widget :notify widget event)
2822 (defun widget-echo-help-mouse ()
2823 "Display the help message for the widget under the mouse.
2824 Enable with (run-with-idle-timer 1 t 'widget-echo-help-mouse)"
2825 (let* ((pos (mouse-position))
2829 (win (window-at x y frame))
2830 (where (coordinates-in-window-p (cons x y) win)))
2832 (save-window-excursion
2833 (progn ; save-excursion
2835 (let* ((result (compute-motion (window-start win)
2840 (cons (window-hscroll) 0)
2842 (when (and (eq (nth 1 result) x)
2843 (eq (nth 2 result) y))
2844 (widget-echo-help (nth 0 result))))))))
2846 (setq track-mouse t)
2847 (add-hook 'post-command-hook 'widget-stop-mouse-tracking)))
2849 (defun widget-stop-mouse-tracking (&rest args)
2850 "Stop the mouse tracking done while idle."
2851 (remove-hook 'post-command-hook 'widget-stop-mouse-tracking)
2852 (setq track-mouse nil))
2854 (defun widget-at (pos)
2855 "The button or field at POS."
2856 (or (get-text-property pos 'button)
2857 (get-text-property pos 'field)))
2859 (defun widget-echo-help (pos)
2860 "Display the help echo for widget at POS."
2861 (let* ((widget (widget-at pos))
2862 (help-echo (and widget (widget-get widget :help-echo))))
2863 (cond ((stringp help-echo)
2864 (message "%s" help-echo))
2865 ((and (symbolp help-echo) (fboundp help-echo)
2866 (stringp (setq help-echo (funcall help-echo widget))))
2867 (message "%s" help-echo)))))
2873 ;; wid-edit.el ends here