1 ;;; wid-edit.el --- Functions for creating and using widgets.
3 ;; Copyright (C) 1996, 1997, 2000 Free Software Foundation, Inc.
5 ;; Author: Per Abrahamsen <abraham@dina.kvl.dk>
6 ;; Maintainer: Hrvoje Niksic <hniksic@xemacs.org>
7 ;; Keywords: extensions
9 ;; X-URL: http://www.dina.kvl.dk/~abraham/custom/
11 ;; This file is part of XEmacs.
13 ;; XEmacs is free software; you can redistribute it and/or modify
14 ;; it under the terms of the GNU General Public License as published by
15 ;; the Free Software Foundation; either version 2, or (at your option)
18 ;; XEmacs is distributed in the hope that it will be useful,
19 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
20 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 ;; GNU General Public License for more details.
23 ;; You should have received a copy of the GNU General Public License
24 ;; along with XEmacs; see the file COPYING. If not, write to the
25 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
26 ;; Boston, MA 02111-1307, USA.
37 (autoload 'finder-commentary "finder" nil t)
42 "Customization support for the Widget Library."
43 :link '(custom-manual "(widget)Top")
44 :link '(url-link :tag "Development Page"
45 "http://www.dina.kvl.dk/~abraham/custom/")
46 :link '(emacs-library-link :tag "Lisp File" "widget.el")
51 (defgroup widget-documentation nil
52 "Options controlling the display of documentation strings."
55 (defgroup widget-faces nil
56 "Faces used by the widget library."
60 (defvar widget-documentation-face 'widget-documentation-face
61 "Face used for documentation strings in widges.
62 This exists as a variable so it can be set locally in certain buffers.")
64 (defface widget-documentation-face '((((class color)
66 (:foreground "lime green"))
69 (:foreground "dark green"))
71 "Face used for documentation text."
72 :group 'widget-documentation
75 (defvar widget-button-face 'widget-button-face
76 "Face used for buttons in widges.
77 This exists as a variable so it can be set locally in certain buffers.")
79 (defface widget-button-face '((t (:bold t)))
80 "Face used for widget buttons."
83 (defcustom widget-mouse-face 'highlight
84 "Face used for widget buttons when the mouse is above them."
88 (defface widget-field-face '((((class grayscale color)
90 (:background "gray85"))
91 (((class grayscale color)
93 (:background "dim gray"))
96 "Face used for editable fields."
100 ;(defface widget-single-line-field-face '((((class grayscale color)
101 ; (background light))
102 ; (:background "gray85"))
103 ; (((class grayscale color)
105 ; (:background "dim gray"))
108 ; "Face used for editable fields spanning only a single line."
109 ; :group 'widget-faces)
111 ;(defvar widget-single-line-display-table
112 ; (let ((table (make-display-table)))
113 ; (aset table 9 "^I")
114 ; (aset table 10 "^J")
116 ; "Display table used for single-line editable fields.")
118 ;(set-face-display-table 'widget-single-line-field-face
119 ; widget-single-line-display-table)
122 ;; Some functions from this file have been ported to C for speed.
123 ;; Setting this to t (*before* loading wid-edit.el) will make them
124 ;; shadow the subrs. It should be used only for debugging purposes.
125 (defvar widget-shadow-subrs nil)
128 ;;; Utility functions.
130 ;; These are not really widget specific.
132 (when (or (not (fboundp 'widget-plist-member))
134 ;; Recoded in C, for efficiency. It used to be a defsubst, but old
135 ;; compiled code won't fail -- it will just be slower.
136 (defun widget-plist-member (plist prop)
137 ;; Return non-nil if PLIST has the property PROP.
138 ;; PLIST is a property list, which is a list of the form
139 ;; (PROP1 VALUE1 PROP2 VALUE2 ...). PROP is a symbol.
140 ;; Unlike `plist-get', this allows you to distinguish between a missing
141 ;; property and a property with the value nil.
142 ;; The value is actually the tail of PLIST whose car is PROP.
143 (while (and plist (not (eq (car plist) prop)))
144 (setq plist (cddr plist)))
147 (defun widget-princ-to-string (object)
148 ;; Return string representation of OBJECT, any Lisp object.
149 ;; No quoting characters are used; no delimiters are printed around
150 ;; the contents of strings.
151 (with-current-buffer (get-buffer-create " *widget-tmp*")
153 (princ object (current-buffer))
156 (defun widget-prettyprint-to-string (object)
157 ;; Like pp-to-string, but uses `cl-prettyprint'
158 (with-current-buffer (get-buffer-create " *widget-tmp*")
160 (cl-prettyprint object)
161 ;; `cl-prettyprint' always surrounds the text with newlines.
162 (when (eq (char-after (point-min)) ?\n)
163 (delete-region (point-min) (1+ (point-min))))
164 (when (eq (char-before (point-max)) ?\n)
165 (delete-region (1- (point-max)) (point-max)))
168 (defun widget-clear-undo ()
169 "Clear all undo information."
170 (buffer-disable-undo)
171 (buffer-enable-undo))
173 (defcustom widget-menu-max-size 40
174 "Largest number of items allowed in a popup-menu.
175 Larger menus are read through the minibuffer."
179 (defcustom widget-menu-minibuffer-flag nil
180 "*Control how to ask for a choice from the keyboard.
181 Non-nil means use the minibuffer;
182 nil means read a single character."
186 (defun widget-choose (title items &optional event)
187 "Choose an item from a list.
189 First argument TITLE is the name of the list.
190 Second argument ITEMS is an list whose members are either
191 (NAME . VALUE), to indicate selectable items, or just strings to
192 indicate unselectable items.
193 Optional third argument EVENT is an input event.
195 The user is asked to choose between each NAME from the items alist,
196 and the VALUE of the chosen element will be returned. If EVENT is a
197 mouse event, and the number of elements in items is less than
198 `widget-menu-max-size', a popup menu will be used, otherwise the
200 (cond ((and (< (length items) widget-menu-max-size)
202 (console-on-window-system-p))
203 ;; Pressed by the mouse.
204 (let ((val (get-popup-menu-response
209 (vector (car x) (list (car x)) t)))
212 (listp (event-object val))
213 (stringp (car-safe (event-object val)))
214 (car (event-object val))))
215 (cdr (assoc val items))))
216 ((and (not widget-menu-minibuffer-flag)
217 ;; Can't handle more than 10 items (as many digits)
218 (<= (length items) 10))
219 ;; Construct a menu of the choices
220 ;; and then use it for prompting for a single character.
221 (let* ((overriding-terminal-local-map (make-sparse-keymap))
222 (map (make-sparse-keymap title))
224 some-choice-enabled value)
225 ;; Define SPC as a prefix char to get to this menu.
226 (define-key overriding-terminal-local-map " " map)
227 (with-current-buffer (get-buffer-create " widget-choose")
229 (insert "Available choices:\n\n")
230 (dolist (choice items)
232 (let* ((name (car choice))
233 (function (cdr choice)))
234 (insert (format "%c = %s\n" next-digit name))
235 (define-key map (vector next-digit) function)
236 (setq some-choice-enabled t)))
237 ;; Allocate digits to disabled alternatives
238 ;; so that the digit of a given alternative never varies.
240 (insert "\nC-g = Quit"))
241 (or some-choice-enabled
242 (error "None of the choices is currently meaningful"))
243 (define-key map [?\C-g] 'keyboard-quit)
244 (define-key map [t] 'keyboard-quit)
245 ;(setcdr map (nreverse (cdr map)))
246 ;; Unread a SPC to lead to our new menu.
247 (push (character-to-event ?\ ) unread-command-events)
248 ;; Read a char with the menu, and return the result
249 ;; that corresponds to it.
250 (save-window-excursion
251 (display-buffer (get-buffer " widget-choose"))
252 (let ((cursor-in-echo-area t))
254 (lookup-key overriding-terminal-local-map
255 (read-key-sequence (concat title ": ") t)))))
257 (when (or (eq value 'keyboard-quit)
262 ;; Read the choice of name from the minibuffer.
263 (setq items (remove-if 'stringp items))
264 (let ((val (completing-read (concat title ": ") items nil t)))
266 (let ((try (try-completion val items)))
269 (cdr (assoc val items)))
273 ;;; Widget text specifications.
275 ;; These functions are for specifying text properties.
277 (defcustom widget-field-add-space t
278 ;; Setting this to nil might be available, once some problems are resolved.
279 "Non-nil means add extra space at the end of editable text fields.
281 This is needed on all versions of Emacs. If you don't add the space,
282 it will become impossible to edit a zero size field."
286 (defcustom widget-field-use-before-change
287 (and (or (> emacs-minor-version 34)
288 (> emacs-major-version 19))
289 (not (string-match "XEmacs" emacs-version)))
290 "Non-nil means use `before-change-functions' to track editable fields.
291 This enables the use of undo, but doesn't work on Emacs 19.34 and earlier.
292 Using before hooks also means that the :notify function can't know the
297 (defun widget-echo-this-extent (extent)
298 (let* ((widget (or (extent-property extent 'button)
299 (extent-property extent 'field)
300 (extent-property extent 'glyph-widget)))
301 (help-echo (and widget (widget-get widget :help-echo))))
302 (and (functionp help-echo)
303 (setq help-echo (funcall help-echo widget)))
304 (when (stringp help-echo)
305 (setq help-echo-owns-message t)
306 (display-message 'help-echo help-echo))))
308 (defsubst widget-handle-help-echo (extent help-echo)
309 (set-extent-property extent 'balloon-help help-echo)
310 (set-extent-property extent 'help-echo help-echo)
311 (when (functionp help-echo)
312 (set-extent-property extent 'balloon-help 'widget-echo-this-extent)
313 (set-extent-property extent 'help-echo 'widget-echo-this-extent)))
315 (defun widget-specify-field (widget from to)
316 "Specify editable button for WIDGET between FROM and TO."
319 (cond ((null (widget-get widget :size))
321 ;; Terminating space is not part of the field, but necessary in
322 ;; order for local-map to work. Remove next sexp if local-map works
323 ;; at the end of the extent.
324 (widget-field-add-space
325 (insert-and-inherit " ")))
327 (let ((map (widget-get widget :keymap))
328 (face (or (widget-get widget :value-face) 'widget-field-face))
329 (help-echo (widget-get widget :help-echo))
330 (extent (make-extent from to)))
331 (unless (or (stringp help-echo) (null help-echo))
332 (setq help-echo 'widget-mouse-help))
333 (widget-put widget :field-extent extent)
334 (and (or (not widget-field-add-space)
335 (widget-get widget :size))
336 (set-extent-property extent 'end-closed nil))
337 (set-extent-property extent 'detachable nil)
338 (set-extent-property extent 'field widget)
339 (set-extent-property extent 'button-or-field t)
340 (set-extent-property extent 'keymap map)
341 (set-extent-property extent 'face face)
342 (widget-handle-help-echo extent help-echo))
343 (widget-specify-secret widget))
345 (defun widget-specify-secret (field)
346 "Replace text in FIELD with value of `:secret', if non-nil."
347 (let ((secret (widget-get field :secret))
348 (size (widget-get field :size)))
350 (let ((begin (widget-field-start field))
351 (end (widget-field-end field)))
353 (while (and (> end begin)
354 (eq (char-after (1- end)) ?\ ))
355 (setq end (1- end))))
357 (let ((old (char-after begin)))
358 (unless (eq old secret)
359 (subst-char-in-region begin (1+ begin) old secret)
360 (put-text-property begin (1+ begin) 'secret old))
361 (setq begin (1+ begin))))))))
363 (defun widget-specify-button (widget from to)
364 "Specify button for WIDGET between FROM and TO."
365 (let ((face (widget-apply widget :button-face-get))
366 (help-echo (widget-get widget :help-echo))
367 (extent (make-extent from to))
368 (map (widget-get widget :button-keymap)))
369 (widget-put widget :button-extent extent)
370 (unless (or (null help-echo) (stringp help-echo))
371 (setq help-echo 'widget-mouse-help))
372 (set-extent-property extent 'start-open t)
373 (set-extent-property extent 'button widget)
374 (set-extent-property extent 'button-or-field t)
375 (set-extent-property extent 'mouse-face widget-mouse-face)
376 (widget-handle-help-echo extent help-echo)
377 (set-extent-property extent 'face face)
378 (set-extent-property extent 'keymap map)))
380 (defun widget-mouse-help (extent)
381 "Find mouse help string for button in extent."
382 (let* ((widget (widget-at (extent-start-position extent)))
383 (help-echo (and widget (widget-get widget :help-echo))))
384 (cond ((stringp help-echo)
386 ((and (functionp help-echo)
387 (stringp (setq help-echo (funcall help-echo widget))))
390 (format "(widget %S :help-echo %S)" widget help-echo)))))
392 (defun widget-specify-sample (widget from to)
393 ;; Specify sample for WIDGET between FROM and TO.
394 (let ((face (widget-apply widget :sample-face-get))
395 (extent (make-extent from to nil)))
396 (set-extent-property extent 'start-open t)
397 (set-extent-property extent 'face face)
398 (widget-put widget :sample-extent extent)))
400 (defun widget-specify-doc (widget from to)
401 ;; Specify documentation for WIDGET between FROM and TO.
402 (let ((extent (make-extent from to)))
403 (set-extent-property extent 'start-open t)
404 (set-extent-property extent 'widget-doc widget)
405 (set-extent-property extent 'face widget-documentation-face)
406 (widget-put widget :doc-extent extent)))
408 (defmacro widget-specify-insert (&rest form)
409 ;; Execute FORM without inheriting any text properties.
411 (let ((inhibit-read-only t)
412 before-change-functions
413 after-change-functions)
415 (narrow-to-region (- (point) 2) (point))
416 (goto-char (1+ (point-min)))
417 ;; We use `prog1' instead of a `result' variable, as the latter
418 ;; confuses the byte-compiler in some cases (a warning).
419 (prog1 (progn ,@form)
420 (delete-region (point-min) (1+ (point-min)))
421 (delete-region (1- (point-max)) (point-max))
422 (goto-char (point-max))))))
424 (put 'widget-specify-insert 'edebug-form-spec '(&rest form))
427 ;;; Inactive Widgets.
429 (defface widget-inactive-face '((((class grayscale color)
431 (:foreground "light gray"))
432 (((class grayscale color)
434 (:foreground "dim gray"))
437 "Face used for inactive widgets."
438 :group 'widget-faces)
440 ;; For inactiveness to work on complex structures, it is not
441 ;; sufficient to keep track of whether a button/field/glyph is
442 ;; inactive or not -- we must know how many time it was deactivated
443 ;; (inactiveness level). Successive deactivations of the same button
444 ;; increment its inactive-count, and activations decrement it. When
445 ;; inactive-count reaches 0, the button/field/glyph is reactivated.
447 (defun widget-activation-widget-mapper (extent action)
448 "Activate or deactivate EXTENT's widget (button or field).
449 Suitable for use with `map-extents'."
452 (decf (extent-property extent :inactive-count))
453 (when (zerop (extent-property extent :inactive-count))
454 (set-extent-properties
455 extent (extent-property extent :inactive-plist))
456 (set-extent-property extent :inactive-plist nil)))
458 (incf (extent-property extent :inactive-count 0))
459 ;; Store a plist of old properties, which will be fed to
460 ;; `set-extent-properties'.
461 (unless (extent-property extent :inactive-plist)
463 extent :inactive-plist
464 (list 'mouse-face (extent-property extent 'mouse-face)
465 'help-echo (extent-property extent 'help-echo)
466 'keymap (extent-property extent 'keymap)))
467 (set-extent-properties
468 extent '(mouse-face nil help-echo nil keymap nil)))))
471 (defun widget-activation-glyph-mapper (extent action)
472 (let ((activate-p (if (eq action :activate) t nil)))
474 (decf (extent-property extent :inactive-count))
475 (incf (extent-property extent :inactive-count 0)))
476 (when (or (and activate-p
477 (zerop (extent-property extent :inactive-count)))
478 (and (not activate-p)
479 (not (zerop (extent-property extent :inactive-count)))))
480 (let* ((glyph-widget (extent-property extent 'glyph-widget))
481 (up-glyph (widget-get glyph-widget :glyph-up))
482 (inactive-glyph (widget-get glyph-widget :glyph-inactive))
483 (instantiator (widget-get glyph-widget :glyph-instantiator))
484 (new-glyph (if activate-p up-glyph inactive-glyph)))
486 ;; Assume that an instantiator means a native widget.
489 (set-instantiator-property instantiator :active activate-p))
490 (widget-put glyph-widget :glyph-instantiator instantiator)
491 (set-glyph-image up-glyph instantiator))
492 ;; Check that the new glyph exists, and differs from the
494 ((and up-glyph inactive-glyph (not (eq up-glyph inactive-glyph))
495 ;; Check if the glyph is already installed.
496 (not (eq (extent-end-glyph extent) new-glyph)))
498 (set-extent-end-glyph extent new-glyph))))))
501 (defun widget-specify-inactive (widget from to)
502 "Make WIDGET inactive for user modifications."
503 (unless (widget-get widget :inactive)
504 (let ((extent (make-extent from to)))
505 ;; It is no longer necessary for the extent to be read-only, as
506 ;; the inactive editable fields now lose their keymaps.
507 (set-extent-properties
508 extent '(start-open t face widget-inactive-face
509 detachable t priority 2001 widget-inactive t))
510 (widget-put widget :inactive extent))
511 ;; Deactivate the buttons and fields within the range. In some
512 ;; cases, the fields are not yet setup at the time this function
513 ;; is called. Those fields are deactivated explicitly by
515 (map-extents 'widget-activation-widget-mapper
516 nil from to :deactivate nil 'button-or-field)
517 ;; Deactivate glyphs.
518 (map-extents 'widget-activation-glyph-mapper
519 nil from to :deactivate nil 'glyph-widget)))
521 (defun widget-specify-active (widget)
522 "Make WIDGET active for user modifications."
523 (let ((inactive (widget-get widget :inactive))
524 (from (widget-get widget :from))
525 (to (widget-get widget :to)))
526 (when (and inactive (not (extent-detached-p inactive)))
527 ;; Reactivate the buttons and fields covered by the extent.
528 (map-extents 'widget-activation-widget-mapper
529 nil from to :activate nil 'button-or-field)
530 ;; Reactivate the glyphs.
531 (map-extents 'widget-activation-glyph-mapper
532 nil from to :activate nil 'end-glyph)
533 (delete-extent inactive)
534 (widget-put widget :inactive nil))))
537 ;;; Widget Properties.
539 (defsubst widget-type (widget)
540 "Return the type of WIDGET, a symbol."
543 (when (or (not (fboundp 'widget-put))
545 (defun widget-put (widget property value)
546 "In WIDGET set PROPERTY to VALUE.
547 The value can later be retrieved with `widget-get'."
548 (setcdr widget (plist-put (cdr widget) property value))))
550 ;; Recoded in C, for efficiency:
551 (when (or (not (fboundp 'widget-get))
553 (defun widget-get (widget property)
554 "In WIDGET, get the value of PROPERTY.
555 The value could either be specified when the widget was created, or
556 later with `widget-put'."
560 (cond ((setq tmp (widget-plist-member (cdr widget) property))
561 (setq value (car (cdr tmp))
563 ((setq tmp (car widget))
564 (setq widget (get tmp 'widget-type)))
566 (setq missing nil))))
569 (defun widget-get-indirect (widget property)
570 "In WIDGET, get the value of PROPERTY.
571 If the value is a symbol, return its binding.
572 Otherwise, just return the value."
573 (let ((value (widget-get widget property)))
578 (defun widget-member (widget property)
579 "Return t if there is a definition in WIDGET for PROPERTY."
580 (cond ((widget-plist-member (cdr widget) property)
583 (widget-member (get (car widget) 'widget-type) property))
586 (when (or (not (fboundp 'widget-apply))
588 ;;This is in C, so don't ###utoload
589 (defun widget-apply (widget property &rest args)
590 "Apply the value of WIDGET's PROPERTY to the widget itself.
591 ARGS are passed as extra arguments to the function."
592 (apply (widget-get widget property) widget args)))
594 (defun widget-value (widget)
595 "Extract the current value of WIDGET."
597 :value-to-external (widget-apply widget :value-get)))
599 (defun widget-value-set (widget value)
600 "Set the current value of WIDGET to VALUE."
602 :value-set (widget-apply widget
603 :value-to-internal value)))
605 (defun widget-default-get (widget)
606 "Extract the defaylt value of WIDGET."
607 (or (widget-get widget :value)
608 (widget-apply widget :default-get)))
610 (defun widget-match-inline (widget vals)
611 ;; In WIDGET, match the start of VALS.
612 (cond ((widget-get widget :inline)
613 (widget-apply widget :match-inline vals))
615 (widget-apply widget :match (car vals)))
616 (cons (list (car vals)) (cdr vals)))
619 (defun widget-apply-action (widget &optional event)
620 "Apply :action in WIDGET in response to EVENT."
621 (if (widget-apply widget :active)
622 (widget-apply widget :action event)
623 (error "Attempt to perform action on inactive widget")))
626 ;;; Helper functions.
628 ;; These are widget specific.
631 (defun widget-prompt-value (widget prompt &optional value unbound)
632 "Prompt for a value matching WIDGET, using PROMPT.
633 The current value is assumed to be VALUE, unless UNBOUND is non-nil."
634 (unless (listp widget)
635 (setq widget (list widget)))
636 (setq prompt (format "[%s] %s" (widget-type widget) prompt))
637 (setq widget (widget-convert widget))
638 (let ((answer (widget-apply widget :prompt-value prompt value unbound)))
639 (while (not (widget-apply widget :match answer))
640 (setq answer (signal 'error (list "Answer does not match type"
641 answer (widget-type widget)))))
644 (defun widget-get-sibling (widget)
645 "Get the item WIDGET is assumed to toggle.
646 This is only meaningful for radio buttons or checkboxes in a list."
647 (let* ((parent (widget-get widget :parent))
648 (children (widget-get parent :children))
652 (setq child (car children)
653 children (cdr children))
654 (when (eq (widget-get child :button) widget)
655 (throw 'child child)))
658 (defun widget-map-buttons (function &optional buffer maparg)
659 "Map FUNCTION over the buttons in BUFFER.
660 FUNCTION is called with the arguments WIDGET and MAPARG.
662 If FUNCTION returns non-nil, the walk is cancelled.
664 The arguments MAPARG, and BUFFER default to nil and (current-buffer),
666 (map-extents (lambda (extent ignore)
667 ;; If FUNCTION returns non-nil, we bail out
668 (funcall function (extent-property extent 'button) maparg))
675 (defcustom widget-glyph-directory (locate-data-directory "custom")
676 "Where widget glyphs are located.
677 If this variable is nil, widget will try to locate the directory
682 (defcustom widget-glyph-enable t
683 "If non nil, use glyphs in images when available."
687 (defcustom widget-image-file-name-suffixes
688 '((xpm ".xpm") (gif ".gif") (png ".png") (jpeg ".jpg" ".jpeg")
690 "Conversion alist from image formats to file name suffixes."
692 :type '(repeat (cons :format "%v"
693 (symbol :tag "Image Format" unknown)
694 (repeat :tag "Suffixes"
695 (string :format "%v")))))
697 ;; Don't use this, because we cannot yet distinguish between widget
698 ;; glyphs associated with user action, and actionless ones.
699 ;(defvar widget-glyph-pointer-glyph
700 ; (make-pointer-glyph [cursor-font :data "hand2"])
701 ; "Glyph to be used as the mouse pointer shape over glyphs.
702 ;Use `set-glyph-image' to change this.")
704 (defvar widget-glyph-cache nil
705 "Cache of glyphs associated with strings (files).")
707 (defun widget-glyph-find (image tag)
708 "Create a glyph corresponding to IMAGE with string TAG as fallback.
709 IMAGE can already be a glyph, or a file name sans extension (xpm,
710 xbm, gif, jpg, or png) located in `widget-glyph-directory', or
711 in one of the data directories.
712 It can also be a valid image instantiator, in which case it will be
713 used to make the glyph, with an additional TAG string fallback."
714 (cond ((not (and image widget-glyph-enable))
715 ;; We don't want to use glyphs.
717 ((and (not (console-on-window-system-p))
718 ;; We don't use glyphs on TTY consoles, although we
719 ;; could. However, glyph faces aren't yet working
720 ;; properly, and movement through glyphs is unintuitive.
721 ;; As an exception, when TAG is nil, we assume that the
722 ;; caller knows what he is doing, and that the tag is
723 ;; encoded within the glyph.
724 (not (glyphp image)))
727 ;; Already a glyph. Use it.
730 ;; A string. Look it up in the cache first...
731 (or (lax-plist-get widget-glyph-cache image)
732 ;; ...and then in the relevant directories
733 (let* ((dirlist (cons (or widget-glyph-directory
734 (locate-data-directory "custom"))
735 data-directory-list))
740 (and (valid-image-instantiator-format-p (car el))
742 widget-image-file-name-suffixes)))
743 (file (locate-file image dirlist all-suffixes)))
745 (let* ((extension (concat "." (file-name-extension file)))
746 (format (car (rassoc* extension
747 widget-image-file-name-suffixes
749 ;; We create a glyph with the file as the default image
750 ;; instantiator, and the TAG fallback
751 (let ((glyph (make-glyph `([,format :file ,file]
752 [string :data ,tag]))))
754 (laxputf widget-glyph-cache image glyph)
757 ((valid-instantiator-p image 'image)
758 ;; A valid image instantiator (e.g. [gif :file "somefile"] etc.)
759 (make-glyph `(,image [string :data ,tag])))
764 (defun widget-glyph-insert (widget tag image &optional down inactive)
765 "In WIDGET, insert the text TAG or, if supported, IMAGE.
766 IMAGE should either be a glyph, an image instantiator, an image file
767 name sans extension (xpm, xbm, gif, jpg, or png) located in
768 `widget-glyph-directory', or anything else allowed by
771 If IMAGE is a list, it will be taken as a list of (UP DOWN INACTIVE)
772 glyphs. The down and inactive glyphs are shown when glyph is pressed
773 or inactive, respectively.
775 The optional DOWN and INACTIVE arguments are deprecated, and exist
776 only because of compatibility."
777 ;; Convert between IMAGE being a list, etc. Must use `psetq',
778 ;; because otherwise change to `image' screws up the rest.
779 (psetq image (or (and (consp image)
782 down (or (and (consp image)
785 inactive (or (and (consp image)
788 (let ((glyph (widget-glyph-find image tag)))
790 (widget-glyph-insert-glyph widget glyph
791 (widget-glyph-find down tag)
792 (widget-glyph-find inactive tag))
796 (defun widget-glyph-insert-glyph (widget glyph &optional down inactive
798 "In WIDGET, insert GLYPH.
799 If optional arguments DOWN and INACTIVE are given, they should be
800 glyphs used when the widget is pushed and inactive, respectively.
801 INSTANTIATOR is the vector used to create the glyph."
803 (let ((extent (make-extent (point) (1- (point))))
804 (help-echo (and widget (widget-get widget :help-echo)))
805 (map (and widget (widget-get widget :button-keymap))))
806 (set-extent-property extent 'glyph-widget widget)
807 ;; It would be fun if we could make this extent atomic, so it
808 ;; doesn't mess with cursor motion. But atomic-extents library is
809 ;; currently a mess, so I'd rather not use it.
810 (set-extent-property extent 'invisible t)
811 (set-extent-property extent 'start-open t)
812 (set-extent-property extent 'end-open t)
813 (set-extent-property extent 'keymap map)
814 ;;(set-extent-property extent 'pointer widget-glyph-pointer-glyph)
815 (set-extent-end-glyph extent glyph)
816 (unless (or (stringp help-echo) (null help-echo))
817 (setq help-echo 'widget-mouse-help))
819 (widget-handle-help-echo extent help-echo)))
821 (widget-put widget :glyph-up glyph)
822 (when down (widget-put widget :glyph-down down))
823 (when instantiator (widget-put widget :glyph-instantiator instantiator))
824 (when inactive (widget-put widget :glyph-inactive inactive))))
829 (defgroup widget-button nil
830 "The look of various kinds of buttons."
833 (defcustom widget-button-prefix ""
834 "String used as prefix for buttons."
836 :group 'widget-button)
838 (defcustom widget-button-suffix ""
839 "String used as suffix for buttons."
841 :group 'widget-button)
844 ;;; Creating Widgets.
847 (defun widget-create (type &rest args)
848 "Create widget of TYPE.
849 The optional ARGS are additional keyword arguments."
850 (let ((widget (apply 'widget-convert type args)))
851 (widget-apply widget :create)
854 (defun widget-create-child-and-convert (parent type &rest args)
855 "As part of the widget PARENT, create a child widget TYPE.
856 The child is converted, using the keyword arguments ARGS."
857 (let ((widget (apply 'widget-convert type args)))
858 (widget-put widget :parent parent)
859 (unless (widget-get widget :indent)
860 (widget-put widget :indent (+ (or (widget-get parent :indent) 0)
861 (or (widget-get widget :extra-offset) 0)
862 (widget-get parent :offset))))
863 (widget-apply widget :create)
866 (defun widget-create-child (parent type)
867 "Create widget of TYPE."
868 (let ((widget (copy-sequence type)))
869 (widget-put widget :parent parent)
870 (unless (widget-get widget :indent)
871 (widget-put widget :indent (+ (or (widget-get parent :indent) 0)
872 (or (widget-get widget :extra-offset) 0)
873 (widget-get parent :offset))))
874 (widget-apply widget :create)
877 (defun widget-create-child-value (parent type value)
878 "Create widget of TYPE with value VALUE."
879 (let ((widget (copy-sequence type)))
880 (widget-put widget :value (widget-apply widget :value-to-internal value))
881 (widget-put widget :parent parent)
882 (unless (widget-get widget :indent)
883 (widget-put widget :indent (+ (or (widget-get parent :indent) 0)
884 (or (widget-get widget :extra-offset) 0)
885 (widget-get parent :offset))))
886 (widget-apply widget :create)
890 (defun widget-delete (widget)
892 (widget-apply widget :delete))
894 (defun widget-convert (type &rest args)
895 "Convert TYPE to a widget without inserting it in the buffer.
896 The optional ARGS are additional keyword arguments."
897 ;; Don't touch the type.
898 (let* ((widget (if (symbolp type)
900 (copy-sequence type)))
903 ;; First set the :args keyword.
904 (while (cdr current) ;Look in the type.
905 (let ((next (car (cdr current))))
906 (if (and (symbolp next) (eq (aref (symbol-name next) 0) ?:))
907 (setq current (cdr (cdr current)))
908 (setcdr current (list :args (cdr current)))
909 (setq current nil))))
910 (while args ;Look in the args.
911 (let ((next (nth 0 args)))
912 (if (and (symbolp next) (eq (aref (symbol-name next) 0) ?:))
913 (setq args (nthcdr 2 args))
914 (widget-put widget :args args)
916 ;; Then Convert the widget.
919 (let ((convert-widget (plist-get (cdr type) :convert-widget)))
921 (setq widget (funcall convert-widget widget))))
922 (setq type (get (car type) 'widget-type)))
923 ;; Finally set the keyword args.
925 (let ((next (nth 0 keys)))
926 (if (and (symbolp next) (eq (aref (symbol-name next) 0) ?:))
928 (widget-put widget next (nth 1 keys))
929 (setq keys (nthcdr 2 keys)))
931 ;; Convert the :value to internal format.
932 (if (widget-member widget :value)
933 (let ((value (widget-get widget :value)))
935 :value (widget-apply widget :value-to-internal value))))
936 ;; Return the newly created widget.
939 (defun widget-insert (&rest args)
940 "Call `insert' with ARGS and make the text read only."
941 (let ((inhibit-read-only t)
942 before-change-functions
943 after-change-functions)
944 (apply 'insert args)))
946 (defun widget-convert-text (type from to
947 &optional button-from button-to
949 "Return a widget of type TYPE with endpoint FROM TO.
950 Optional ARGS are extra keyword arguments for TYPE.
951 and TO will be used as the widgets end points. If optional arguments
952 BUTTON-FROM and BUTTON-TO are given, these will be used as the widgets
954 Optional ARGS are extra keyword arguments for TYPE."
955 (let ((widget (apply 'widget-convert type :delete 'widget-leave-text args))
956 (from (copy-marker from))
957 (to (copy-marker to)))
958 (set-marker-insertion-type from t)
959 (set-marker-insertion-type to nil)
960 (widget-put widget :from from)
961 (widget-put widget :to to)
963 (widget-specify-button widget button-from button-to))
966 (defun widget-convert-button (type from to &rest args)
967 "Return a widget of type TYPE with endpoint FROM TO.
968 Optional ARGS are extra keyword arguments for TYPE.
969 No text will be inserted to the buffer, instead the text between FROM
970 and TO will be used as the widgets end points, as well as the widgets
972 (apply 'widget-convert-text type from to from to args))
974 (defun widget-leave-text (widget)
975 "Remove markers and extents from WIDGET and its children."
976 (let ((from (widget-get widget :from))
977 (to (widget-get widget :to))
978 (button (widget-get widget :button-extent))
979 (sample (widget-get widget :sample-extent))
980 (doc (widget-get widget :doc-extent))
981 (field (widget-get widget :field-extent))
982 (children (widget-get widget :children)))
983 (set-marker from nil)
985 ;; Maybe we should delete the extents here? As this code doesn't
986 ;; remove them from widget structures, maybe it's safer to just
987 ;; detach them. That's what `delete-overlay' did.
989 (detach-extent button))
991 (detach-extent sample))
995 (detach-extent field))
996 (mapc 'widget-leave-text children)))
999 ;;; Keymap and Commands.
1001 (defvar widget-keymap nil
1002 "Keymap containing useful binding for buffers containing widgets.
1003 Recommended as a parent keymap for modes using widgets.")
1005 (unless widget-keymap
1006 (setq widget-keymap (make-sparse-keymap))
1007 (define-key widget-keymap [tab] 'widget-forward)
1008 (define-key widget-keymap [(shift tab)] 'widget-backward)
1009 (define-key widget-keymap [(meta tab)] 'widget-backward)
1010 (define-key widget-keymap [backtab] 'widget-backward))
1012 (defvar widget-global-map global-map
1013 "Keymap used for events the widget does not handle themselves.")
1014 (make-variable-buffer-local 'widget-global-map)
1016 (defvar widget-field-keymap nil
1017 "Keymap used inside an editable field.")
1019 (unless widget-field-keymap
1020 (setq widget-field-keymap (make-sparse-keymap))
1021 (set-keymap-parents widget-field-keymap global-map)
1022 (define-key widget-field-keymap "\C-k" 'widget-kill-line)
1023 (define-key widget-field-keymap [(meta tab)] 'widget-complete)
1024 (define-key widget-field-keymap [tab] 'widget-forward)
1025 (define-key widget-field-keymap [(shift tab)] 'widget-backward)
1026 (define-key widget-field-keymap "\C-m" 'widget-field-activate)
1027 (define-key widget-field-keymap "\C-a" 'widget-beginning-of-line)
1028 (define-key widget-field-keymap "\C-e" 'widget-end-of-line)
1029 (define-key widget-field-keymap "\C-t" 'widget-transpose-chars))
1031 (defvar widget-text-keymap nil
1032 "Keymap used inside a text field.")
1034 (unless widget-text-keymap
1035 (setq widget-text-keymap (make-sparse-keymap))
1036 (set-keymap-parents widget-field-keymap global-map)
1037 (define-key widget-text-keymap "\C-a" 'widget-beginning-of-line)
1038 (define-key widget-text-keymap "\C-e" 'widget-end-of-line)
1039 (define-key widget-text-keymap "\C-t" 'widget-transpose-chars))
1041 (defvar widget-button-keymap nil
1042 "Keymap used inside a button.")
1044 (unless widget-button-keymap
1045 (setq widget-button-keymap (make-sparse-keymap))
1046 (set-keymap-parents widget-button-keymap widget-keymap)
1047 (define-key widget-button-keymap "\C-m" 'widget-button-press)
1048 (define-key widget-button-keymap [button2] 'widget-button-click)
1049 ;; Ideally, button3 within a button should invoke a button-specific
1051 (define-key widget-button-keymap [button3] 'widget-button-click)
1053 (define-key widget-button-keymap [button1] 'widget-button1-click))
1056 (defun widget-field-activate (pos &optional event)
1057 "Invoke the editable field at point."
1059 (let ((field (widget-field-find pos)))
1061 (widget-apply-action field event)
1063 (lookup-key widget-global-map (this-command-keys))))))
1065 (defface widget-button-pressed-face
1067 (:foreground "red"))
1069 (:bold t :underline t)))
1070 "Face used for pressed buttons."
1071 :group 'widget-faces)
1073 (defun widget-event-point (event)
1074 "Character position of the mouse event, or nil."
1075 (and (mouse-event-p event)
1076 (event-point event)))
1078 (defun widget-button-click (event)
1079 "Invoke button below mouse pointer."
1081 (with-current-buffer (event-buffer event)
1082 (cond ((event-glyph event)
1083 (widget-glyph-click event))
1084 ((widget-event-point event)
1085 (let* ((pos (widget-event-point event))
1086 (button (get-char-property pos 'button)))
1088 (let* ((extent (widget-get button :button-extent))
1089 (face (extent-property extent 'face))
1090 (mouse-face (extent-property extent 'mouse-face))
1091 (help-echo (extent-property extent 'help-echo)))
1094 ;; Merge relevant faces, and make the result mouse-face.
1095 (let ((merge `(widget-button-pressed-face ,mouse-face)))
1096 (nconc merge (if (listp face)
1098 (setq merge (delete-if-not 'find-face merge))
1099 (set-extent-property extent 'mouse-face merge))
1100 (unless (widget-apply button :mouse-down-action event)
1101 ;; Wait for button release.
1102 (while (not (button-release-event-p
1103 (setq event (next-event))))
1104 (dispatch-event event)))
1105 ;; Disallow mouse-face and help-echo.
1106 (set-extent-property extent 'mouse-face nil)
1107 (set-extent-property extent 'help-echo nil)
1108 (setq pos (widget-event-point event))
1109 (unless (eq (current-buffer) (extent-object extent))
1110 ;; Barf if dispatch-event tripped us by
1112 (error "Buffer changed during mouse motion"))
1113 ;; Do the associated action.
1114 (when (and pos (extent-in-region-p extent pos pos))
1115 (widget-apply-action button event)))
1116 ;; Unwinding: fully release the button.
1117 (set-extent-property extent 'mouse-face mouse-face)
1118 (set-extent-property extent 'help-echo help-echo)))
1119 ;; This should not happen!
1120 (error "`widget-button-click' called outside button"))))
1122 (message "You clicked somewhere weird")))))
1124 (defun widget-button1-click (event)
1125 "Invoke glyph below mouse pointer."
1127 (if (event-glyph event)
1128 (widget-glyph-click event)
1129 ;; Should somehow avoid this.
1130 (let ((command (lookup-key widget-global-map (this-command-keys))))
1131 (and (commandp command)
1132 (call-interactively command)))))
1134 (defun widget-glyph-click (event)
1135 "Handle click on a glyph."
1136 (let* ((glyph (event-glyph event))
1137 (extent (event-glyph-extent event))
1138 (widget (extent-property extent 'glyph-widget))
1139 (down-glyph (or (and widget (widget-get widget :glyph-down)) glyph))
1140 (up-glyph (or (and widget (widget-get widget :glyph-up)) glyph))
1142 (unless (widget-apply widget :active)
1143 (error "This widget is inactive"))
1144 (let ((current-glyph 'down))
1145 ;; We always know what glyph is drawn currently, to avoid
1146 ;; unnecessary extent changes. Is this any noticeable gain?
1150 (set-extent-end-glyph extent down-glyph)
1151 ;; Redisplay (shouldn't be needed, but...)
1153 (unless (widget-apply widget :mouse-down-action event)
1154 ;; Wait for the release.
1155 (while (not (button-release-event-p last))
1156 (unless (button-press-event-p last)
1157 (dispatch-event last))
1158 (when (motion-event-p last)
1159 ;; Update glyphs on mouse motion.
1160 (if (eq extent (event-glyph-extent last))
1161 (unless (eq current-glyph 'down)
1162 (set-extent-end-glyph extent down-glyph)
1163 (setq current-glyph 'down))
1164 (unless (eq current-glyph 'up)
1165 (set-extent-end-glyph extent up-glyph)
1166 (setq current-glyph 'up))))
1167 (setq last (next-event event))))
1168 (unless (eq (current-buffer) (extent-object extent))
1169 ;; Barf if dispatch-event tripped us by changing buffer.
1170 (error "Buffer changed during mouse motion"))
1171 ;; Apply widget action.
1172 (when (eq extent (event-glyph-extent last))
1173 (let ((widget (extent-property (event-glyph-extent event)
1175 (cond ((null widget)
1176 (message "You clicked on a glyph"))
1177 ((not (widget-apply widget :active))
1178 (error "This glyph is inactive"))
1180 (widget-apply-action widget event))))))
1181 ;; Release the glyph.
1182 (and (eq current-glyph 'down)
1183 ;; The extent might have been detached or deleted
1184 (extent-live-p extent)
1185 (not (extent-detached-p extent))
1186 (set-extent-end-glyph extent up-glyph))))))
1188 (defun widget-button-press (pos &optional event)
1189 "Invoke button at POS."
1191 (let ((button (get-char-property pos 'button)))
1193 (widget-apply-action button event)
1194 (let ((command (lookup-key widget-global-map (this-command-keys))))
1195 (when (commandp command)
1196 (call-interactively command))))))
1198 (defun widget-tabable-at (&optional pos last-tab backwardp)
1199 "Return the tabable widget at POS, or nil.
1200 POS defaults to the value of (point)."
1203 (let ((widget (widget-at pos)))
1205 (let ((order (widget-get widget :tab-order)))
1207 (if last-tab (and (= order (if backwardp
1211 (and (> order 0) widget))
1215 ;; Return the button or field extent at point.
1216 (defun widget-button-or-field-extent (pos)
1217 (or (and (get-char-property pos 'button)
1218 (widget-get (get-char-property pos 'button)
1220 (and (get-char-property pos 'field)
1221 (widget-get (get-char-property pos 'field)
1224 (defun widget-next-button-or-field (pos)
1225 "Find the next button, or field, and return its start position, or nil.
1226 Internal function, don't use it outside `wid-edit'."
1227 (let* ((at-point (widget-button-or-field-extent pos))
1228 (extent (map-extents
1229 (lambda (ext ignore)
1231 nil (if at-point (extent-end-position at-point) pos)
1232 nil nil 'start-open 'button-or-field)))
1234 (extent-start-position extent))))
1236 ;; This is too slow in buffers with many buttons (W3).
1237 (defun widget-previous-button-or-field (pos)
1238 "Find the previous button, or field, and return its start position, or nil.
1239 Internal function, don't use it outside `wid-edit'."
1240 (let* ((at-point (widget-button-or-field-extent pos))
1243 (lambda (ext ignore)
1244 (if (eq ext at-point)
1245 ;; We reached the extent we were on originally
1246 (if (= pos (extent-start-position at-point))
1248 (setq previous-extent at-point))
1249 (setq previous-extent ext)
1251 nil nil pos nil 'start-open 'button-or-field)
1252 (and previous-extent
1253 (extent-start-position previous-extent))))
1255 (defun widget-move (arg)
1256 "Move point to the ARG next field or button.
1257 ARG may be negative to move backward."
1258 (let ((opoint (point)) (wrapped 0)
1259 (last-tab (widget-get (widget-at (point)) :tab-order))
1261 ;; Movement backward
1263 (setq nextpos (widget-previous-button-or-field (point)))
1267 (when (and (not (get-char-property nextpos 'widget-inactive))
1268 (widget-tabable-at nil last-tab t))
1271 last-tab (widget-get (widget-at (point))
1273 (if (and (not found) (> wrapped 1))
1276 (goto-char (point-max))
1280 (setq nextpos (widget-next-button-or-field (point)))
1284 (when (and (not (get-char-property nextpos 'widget-inactive))
1285 (widget-tabable-at nil last-tab))
1288 last-tab (widget-get (widget-at (point))
1290 (if (and (not found) (> wrapped 1))
1293 (goto-char (point-min))
1297 (widget-echo-help (point))
1298 (run-hooks 'widget-move-hook))))
1300 (defun widget-forward (arg)
1301 "Move point to the next field or button.
1302 With optional ARG, move across that many fields."
1304 (run-hooks 'widget-forward-hook)
1307 (defun widget-backward (arg)
1308 "Move point to the previous field or button.
1309 With optional ARG, move across that many fields."
1311 (run-hooks 'widget-backward-hook)
1312 (widget-move (- arg)))
1314 (defun widget-beginning-of-line ()
1315 "Go to beginning of field or beginning of line, whichever is first."
1317 (let* ((field (widget-field-find (point)))
1318 (start (and field (widget-field-start field))))
1319 (if (and start (not (eq start (point))))
1321 (call-interactively 'beginning-of-line))))
1323 (defun widget-end-of-line ()
1324 "Go to end of field or end of line, whichever is first."
1326 (let* ((field (widget-field-find (point)))
1327 (end (and field (widget-field-end field))))
1328 (if (and end (not (eq end (point))))
1330 (call-interactively 'end-of-line))))
1332 (defun widget-kill-line ()
1333 "Kill to end of field or end of line, whichever is first."
1335 (let* ((field (widget-field-find (point)))
1336 (newline (save-excursion (forward-line 1) (point)))
1337 (end (and field (widget-field-end field))))
1338 (if (and field (> newline end))
1339 (kill-region (point) end)
1340 (call-interactively 'kill-line))))
1342 (defun widget-transpose-chars (arg)
1343 "Like `transpose-chars', but works correctly at end of widget."
1345 (let* ((field (widget-field-find (point)))
1346 (start (and field (widget-field-start field)))
1347 (end (and field (widget-field-end field)))
1348 (last-non-space (and start end
1351 (skip-chars-backward " \t\n" start)
1353 (cond ((and last-non-space
1354 (or (= last-non-space start)
1355 (= last-non-space (1+ start))))
1356 ;; empty or one-character field
1359 ;; at the beginning of the field -- we would get an error here.
1360 (error "Cannot transpose at beginning of field"))
1362 (when (and (null arg)
1363 (= last-non-space (point)))
1365 (transpose-chars arg)))))
1367 (defcustom widget-complete-field (lookup-key global-map "\M-\t")
1368 "Default function to call for completion inside fields."
1369 :options '(ispell-complete-word complete-tag lisp-complete-symbol)
1373 (defun widget-complete ()
1374 "Complete content of editable field from point.
1375 When not inside a field, move to the previous button or field."
1377 ;; Somehow, this should make pressing M-TAB twice scroll the
1378 ;; completions window.
1379 (let ((field (widget-field-find (point))))
1381 (widget-apply field :complete)
1382 (error "Not in an editable field"))))
1385 ;;; Setting up the buffer.
1387 (defvar widget-field-new nil)
1388 ;; List of all newly created editable fields in the buffer.
1389 (make-variable-buffer-local 'widget-field-new)
1391 (defvar widget-field-list nil)
1392 ;; List of all editable fields in the buffer.
1393 (make-variable-buffer-local 'widget-field-list)
1395 (defun widget-setup ()
1396 "Setup current buffer so editing string widgets works."
1397 (let ((inhibit-read-only t)
1398 (after-change-functions nil)
1399 before-change-functions
1401 (while widget-field-new
1402 (setq field (car widget-field-new)
1403 widget-field-new (cdr widget-field-new)
1404 widget-field-list (cons field widget-field-list))
1405 (let ((from (car (widget-get field :field-extent)))
1406 (to (cdr (widget-get field :field-extent))))
1407 (widget-specify-field field
1408 (marker-position from) (marker-position to))
1409 (set-marker from nil)
1410 (set-marker to nil))
1411 ;; If the field is placed within the inactive zone, deactivate it.
1412 (let ((extent (widget-get field :field-extent)))
1413 (when (get-char-property (extent-start-position extent)
1415 (widget-activation-widget-mapper extent :deactivate)))))
1417 (widget-add-change))
1419 (defvar widget-field-last nil)
1420 ;; Last field containing point.
1421 (make-variable-buffer-local 'widget-field-last)
1423 (defvar widget-field-was nil)
1424 ;; The widget data before the change.
1425 (make-variable-buffer-local 'widget-field-was)
1427 (defun widget-field-buffer (widget)
1428 "Return the start of WIDGET's editing field."
1429 (let ((extent (widget-get widget :field-extent)))
1430 (and extent (extent-object extent))))
1432 (defun widget-field-start (widget)
1433 "Return the start of WIDGET's editing field."
1434 (let ((extent (widget-get widget :field-extent)))
1435 (and extent (extent-start-position extent))))
1437 (defun widget-field-end (widget)
1438 "Return the end of WIDGET's editing field."
1439 (let ((extent (widget-get widget :field-extent)))
1440 ;; Don't subtract one if local-map works at the end of the extent.
1441 (and extent (if (or widget-field-add-space
1442 (null (widget-get widget :size)))
1443 (1- (extent-end-position extent))
1444 (extent-end-position extent)))))
1446 (defun widget-field-find (pos)
1447 "Return the field at POS.
1448 Unlike (get-char-property POS 'field) this, works with empty fields too."
1449 (let ((field-extent (map-extents (lambda (extent ignore)
1451 nil pos pos nil nil 'field)))
1453 (extent-property field-extent 'field))))
1455 ;; Old version, without `map-extents'.
1456 ;(defun widget-field-find (pos)
1457 ; (let ((fields widget-field-list)
1460 ; (setq field (car fields)
1461 ; fields (cdr fields))
1462 ; (let ((start (widget-field-start field))
1463 ; (end (widget-field-end field)))
1464 ; (when (and (<= start pos) (<= pos end))
1466 ; (debug "Overlapping fields"))
1467 ; (setq found field))))
1470 (defun widget-before-change (from to)
1471 ;; Barf if the text changed is outside the editable fields.
1472 (unless inhibit-read-only
1473 (let ((from-field (widget-field-find from))
1474 (to-field (widget-field-find to)))
1475 (cond ((or (null from-field)
1477 ;; Either end of change is not within a field.
1478 (add-hook 'post-command-hook 'widget-add-change nil t)
1479 (error "Attempt to change text outside editable field"))
1480 ((not (eq from-field to-field))
1481 ;; The change begins in one fields, and ends in another one.
1482 (add-hook 'post-command-hook 'widget-add-change nil t)
1483 (error "Change should be restricted to a single field"))
1484 ((or (and from-field
1485 (get-char-property from 'widget-inactive))
1487 (get-char-property to 'widget-inactive)))
1488 ;; Trying to change an inactive editable field.
1489 (add-hook 'post-command-hook 'widget-add-change nil t)
1490 (error "Attempt to change an inactive field"))
1491 (widget-field-use-before-change
1492 ;; #### Bletch! This loses because XEmacs get confused
1493 ;; if before-change-functions change the contents of
1494 ;; buffer before from/to.
1496 (widget-apply from-field :notify from-field)
1497 (error (debug "Before Change"))))))))
1499 (defun widget-add-change ()
1500 (make-local-hook 'post-command-hook)
1501 (remove-hook 'post-command-hook 'widget-add-change t)
1502 (make-local-hook 'before-change-functions)
1503 (add-hook 'before-change-functions 'widget-before-change nil t)
1504 (make-local-hook 'after-change-functions)
1505 (add-hook 'after-change-functions 'widget-after-change nil t))
1507 (defun widget-after-change (from to old)
1508 ;; Adjust field size and text properties.
1510 ;; Also, notify the widgets (so, for example, a variable changes its
1511 ;; state to `modified'. when it is being edited.)
1513 (let ((field (widget-field-find from))
1514 (other (widget-field-find to)))
1516 (unless (eq field other)
1517 (debug "Change in different fields"))
1518 (let ((size (widget-get field :size)))
1520 (let ((begin (widget-field-start field))
1521 (end (widget-field-end field)))
1522 (cond ((< (- end begin) size)
1526 (insert-char ?\ (- (+ begin size) end))))
1527 ((> (- end begin) size)
1528 ;; Field too large and
1529 (if (or (< (point) (+ begin size))
1531 ;; Point is outside extra space.
1532 (setq begin (+ begin size))
1533 ;; Point is within the extra space.
1534 (setq begin (point)))
1537 (while (and (eq (preceding-char) ?\ )
1539 (delete-backward-char 1)))))))
1540 (widget-specify-secret field))
1541 (widget-apply field :notify field)))
1542 (error (debug "After Change"))))
1545 ;;; Widget Functions
1547 ;; These functions are used in the definition of multiple widgets.
1549 (defun widget-parent-action (widget &optional event)
1550 "Tell :parent of WIDGET to handle the :action.
1551 Optional EVENT is the event that triggered the action."
1552 (widget-apply (widget-get widget :parent) :action event))
1554 (defun widget-children-value-delete (widget)
1555 "Delete all :children and :buttons in WIDGET."
1556 (mapc 'widget-delete (widget-get widget :children))
1557 (widget-put widget :children nil)
1558 (mapc 'widget-delete (widget-get widget :buttons))
1559 (widget-put widget :buttons nil))
1561 (defun widget-children-validate (widget)
1562 "All the :children must be valid."
1563 (let ((children (widget-get widget :children))
1565 (while (and children (not found))
1566 (setq child (car children)
1567 children (cdr children)
1568 found (widget-apply child :validate)))
1571 (defun widget-types-convert-widget (widget)
1572 "Convert :args as widget types in WIDGET."
1573 (widget-put widget :args (mapcar 'widget-convert (widget-get widget :args)))
1576 (defun widget-value-convert-widget (widget)
1577 "Initialize :value from :args in WIDGET."
1578 (let ((args (widget-get widget :args)))
1580 (widget-put widget :value (car args))
1581 ;; Don't convert :value here, as this is done in `widget-convert'.
1582 ;; (widget-put widget :value (widget-apply widget
1583 ;; :value-to-internal (car args)))
1584 (widget-put widget :args nil)))
1587 (defun widget-value-value-get (widget)
1588 "Return the :value property of WIDGET."
1589 (widget-get widget :value))
1591 ;;; The `default' Widget.
1593 (define-widget 'default nil
1594 "Basic widget other widgets are derived from."
1595 :value-to-internal (lambda (widget value) value)
1596 :value-to-external (lambda (widget value) value)
1597 :button-prefix 'widget-button-prefix
1598 :button-suffix 'widget-button-suffix
1599 :complete 'widget-default-complete
1600 :create 'widget-default-create
1603 :format-handler 'widget-default-format-handler
1604 :button-face-get 'widget-default-button-face-get
1605 :sample-face-get 'widget-default-sample-face-get
1606 :button-keymap widget-button-keymap
1607 :delete 'widget-default-delete
1608 :value-set 'widget-default-value-set
1609 :value-inline 'widget-default-value-inline
1610 :default-get 'widget-default-default-get
1611 :menu-tag-get 'widget-default-menu-tag-get
1612 :validate (lambda (widget) nil)
1613 :active 'widget-default-active
1614 :activate 'widget-specify-active
1615 :deactivate 'widget-default-deactivate
1616 :mouse-down-action (lambda (widget event) nil)
1617 :action 'widget-default-action
1618 :notify 'widget-default-notify
1619 :prompt-value 'widget-default-prompt-value)
1621 (defun widget-default-complete (widget)
1622 "Call the value of the :complete-function property of WIDGET.
1623 If that does not exists, call the value of `widget-complete-field'."
1624 (let ((fun (widget-get widget :complete-function)))
1625 (call-interactively (or fun widget-complete-field))))
1627 (defun widget-default-create (widget)
1628 "Create WIDGET at point in the current buffer."
1629 (widget-specify-insert
1630 (let ((from (point))
1631 button-begin button-end button-glyph
1632 sample-begin sample-end
1635 (insert (widget-get widget :format))
1637 ;; Parse escapes in format. Coding this in C would speed up
1639 (while (re-search-forward "%\\(.\\)" nil t)
1640 (let ((escape (aref (match-string 1) 0)))
1641 (replace-match "" t t)
1642 (cond ((eq escape ?%)
1645 (setq button-begin (point-marker))
1646 (set-marker-insertion-type button-begin nil))
1648 (setq button-end (point-marker))
1649 (set-marker-insertion-type button-end nil))
1651 (setq sample-begin (point)))
1653 (setq sample-end (point)))
1655 (when (widget-get widget :indent)
1657 (insert-char ?\ (widget-get widget :indent))))
1659 (let* ((tag (widget-get widget :tag))
1660 (glyph (widget-get widget :tag-glyph)))
1663 (widget-glyph-insert
1664 widget (or tag "Image") glyph)))
1668 (let ((standard-output (current-buffer)))
1669 (princ (widget-get widget :value)))))))
1671 (let ((doc (widget-get widget :doc)))
1673 (setq doc-begin (point))
1675 (while (eq (preceding-char) ?\n)
1676 (delete-backward-char 1))
1678 (setq doc-end (point)))))
1680 (if (and button-begin (not button-end))
1681 (widget-apply widget :value-create)
1682 (setq value-pos (point-marker))))
1684 (widget-apply widget :format-handler escape)))))
1685 ;; Specify button, sample, and doc, and insert value.
1686 (when (and button-begin button-end)
1687 (unless button-glyph
1688 (goto-char button-begin)
1689 (insert (widget-get-indirect widget :button-prefix))
1690 (goto-char button-end)
1691 (set-marker-insertion-type button-end t)
1692 (insert (widget-get-indirect widget :button-suffix)))
1693 (widget-specify-button widget button-begin button-end)
1694 ;; Is this necessary?
1695 (set-marker button-begin nil)
1696 (set-marker button-end nil))
1697 (and sample-begin sample-end
1698 (widget-specify-sample widget sample-begin sample-end))
1699 (and doc-begin doc-end
1700 (widget-specify-doc widget doc-begin doc-end))
1702 (goto-char value-pos)
1703 (widget-apply widget :value-create)))
1704 (let ((from (point-min-marker))
1705 (to (point-max-marker)))
1706 (set-marker-insertion-type from t)
1707 (set-marker-insertion-type to nil)
1708 (widget-put widget :from from)
1709 (widget-put widget :to to)))
1710 (widget-clear-undo))
1712 (defun widget-default-format-handler (widget escape)
1713 ;; We recognize the %h escape by default.
1714 (let* ((buttons (widget-get widget :buttons)))
1715 (cond ((eq escape ?h)
1716 (let* ((doc-property (widget-get widget :documentation-property))
1717 (doc-try (cond ((widget-get widget :doc))
1718 ((symbolp doc-property)
1719 (documentation-property
1720 (widget-get widget :value)
1723 (funcall doc-property
1724 (widget-get widget :value)))))
1725 (doc-text (and (stringp doc-try)
1726 (> (length doc-try) 1)
1728 (doc-indent (widget-get widget :documentation-indent)))
1730 (and (eq (preceding-char) ?\n)
1731 (widget-get widget :indent)
1732 (insert-char ?\ (widget-get widget :indent)))
1733 ;; The `*' in the beginning is redundant.
1734 (when (eq (aref doc-text 0) ?*)
1735 (setq doc-text (substring doc-text 1)))
1736 ;; Get rid of trailing newlines.
1737 (when (string-match "\n+\\'" doc-text)
1738 (setq doc-text (substring doc-text 0 (match-beginning 0))))
1739 (push (widget-create-child-and-convert
1740 widget 'documentation-string
1741 :indent (cond ((numberp doc-indent)
1749 (signal 'error (list "Unknown escape" escape))))
1750 (widget-put widget :buttons buttons)))
1752 (defun widget-default-button-face-get (widget)
1753 ;; Use :button-face or widget-button-face
1754 (or (widget-get widget :button-face)
1755 (let ((parent (widget-get widget :parent)))
1757 (widget-apply parent :button-face-get)
1758 widget-button-face))))
1760 (defun widget-default-sample-face-get (widget)
1761 ;; Use :sample-face.
1762 (widget-get widget :sample-face))
1764 (defun widget-default-delete (widget)
1765 ;; Remove widget from the buffer.
1766 (let ((from (widget-get widget :from))
1767 (to (widget-get widget :to))
1768 (inactive-extent (widget-get widget :inactive))
1769 (button-extent (widget-get widget :button-extent))
1770 (sample-extent (widget-get widget :sample-extent))
1771 (doc-extent (widget-get widget :doc-extent))
1772 before-change-functions
1773 after-change-functions
1774 (inhibit-read-only t))
1775 (widget-apply widget :value-delete)
1776 (when inactive-extent
1777 (detach-extent inactive-extent))
1779 (detach-extent button-extent))
1781 (detach-extent sample-extent))
1783 (detach-extent doc-extent))
1785 ;; Kludge: this doesn't need to be true for empty formats.
1786 (delete-region from to))
1787 (set-marker from nil)
1788 (set-marker to nil))
1789 (widget-clear-undo))
1791 (defun widget-default-value-set (widget value)
1792 ;; Recreate widget with new value.
1793 (let* ((old-pos (point))
1794 (from (copy-marker (widget-get widget :from)))
1795 (to (copy-marker (widget-get widget :to)))
1796 (offset (if (and (<= from old-pos) (<= old-pos to))
1797 (if (>= old-pos (1- to))
1799 (- old-pos from)))))
1800 ;;??? Bug: this ought to insert the new value before deleting the old one,
1801 ;; so that markers on either side of the value automatically
1802 ;; stay on the same side. -- rms.
1804 (goto-char (widget-get widget :from))
1805 (widget-apply widget :delete)
1806 (widget-put widget :value value)
1807 (widget-apply widget :create))
1810 (goto-char (+ (widget-get widget :to) offset 1))
1811 (goto-char (min (+ from offset) (1- (widget-get widget :to))))))))
1813 (defun widget-default-value-inline (widget)
1814 ;; Wrap value in a list unless it is inline.
1815 (if (widget-get widget :inline)
1816 (widget-value widget)
1817 (list (widget-value widget))))
1819 (defun widget-default-default-get (widget)
1821 (widget-get widget :value))
1823 (defun widget-default-menu-tag-get (widget)
1824 ;; Use tag or value for menus.
1825 (or (widget-get widget :menu-tag)
1826 (widget-get widget :tag)
1827 (widget-princ-to-string (widget-get widget :value))))
1829 (defun widget-default-active (widget)
1830 "Return t iff this widget active (user modifiable)."
1831 (and (not (widget-get widget :inactive))
1832 (let ((parent (widget-get widget :parent)))
1834 (widget-apply parent :active)))))
1836 (defun widget-default-deactivate (widget)
1837 "Make WIDGET inactive for user modifications."
1838 (widget-specify-inactive widget
1839 (widget-get widget :from)
1840 (widget-get widget :to)))
1842 (defun widget-default-action (widget &optional event)
1843 ;; Notify the parent when a widget change
1844 (let ((parent (widget-get widget :parent)))
1846 (widget-apply parent :notify widget event))))
1848 (defun widget-default-notify (widget child &optional event)
1849 ;; Pass notification to parent.
1850 (widget-default-action widget event))
1852 (defun widget-default-prompt-value (widget prompt value unbound)
1853 ;; Read an arbitrary value. Stolen from `set-variable'.
1854 ;; (let ((initial (if unbound
1856 ;; ;; It would be nice if we could do a `(cons val 1)' here.
1857 ;; (prin1-to-string (custom-quote value))))))
1858 (eval-minibuffer prompt ))
1860 ;;; The `item' Widget.
1862 (define-widget 'item 'default
1863 "Constant items for inclusion in other widgets."
1864 :convert-widget 'widget-value-convert-widget
1865 :value-create 'widget-item-value-create
1866 :value-delete 'ignore
1867 :value-get 'widget-value-value-get
1868 :match 'widget-item-match
1869 :match-inline 'widget-item-match-inline
1870 :action 'widget-item-action
1873 (defun widget-item-value-create (widget)
1874 ;; Insert the printed representation of the value.
1875 (let ((standard-output (current-buffer)))
1876 (princ (widget-get widget :value))))
1878 (defun widget-item-match (widget value)
1879 ;; Match if the value is the same.
1880 (equal (widget-get widget :value) value))
1882 (defun widget-item-match-inline (widget values)
1883 ;; Match if the value is the same.
1884 (let ((value (widget-get widget :value)))
1886 (<= (length value) (length values))
1887 (let ((head (widget-sublist values 0 (length value))))
1888 (and (equal head value)
1889 (cons head (widget-sublist values (length value))))))))
1891 (defun widget-sublist (list start &optional end)
1892 "Return the sublist of LIST from START to END.
1893 If END is omitted, it defaults to the length of LIST."
1894 (if (> start 0) (setq list (nthcdr start list)))
1898 (setq list (copy-sequence list))
1899 (setcdr (nthcdr (- end start 1) list) nil)
1901 (copy-sequence list)))
1903 (defun widget-item-action (widget &optional event)
1904 ;; Just notify itself.
1905 (widget-apply widget :notify widget event))
1907 ;;; The `push-button' Widget.
1909 (defcustom widget-push-button-gui widget-glyph-enable
1910 "If non nil, use GUI push buttons when available."
1914 (defcustom widget-push-button-prefix "["
1915 "String used as prefix for buttons."
1917 :group 'widget-button)
1919 (defcustom widget-push-button-suffix "]"
1920 "String used as suffix for buttons."
1922 :group 'widget-button)
1924 (define-widget 'push-button 'item
1925 "A pushable button."
1928 :value-create 'widget-push-button-value-create
1931 (defun widget-push-button-value-create (widget)
1932 ;; Insert text representing the `on' and `off' states.
1933 (let* ((tag (or (widget-get widget :tag)
1934 (widget-get widget :value)))
1935 (tag-glyph (widget-get widget :tag-glyph))
1936 (text (concat widget-push-button-prefix
1937 tag widget-push-button-suffix))
1940 (widget-glyph-insert widget text tag-glyph))
1941 ;; We must check for console-on-window-system-p here,
1942 ;; because GUI will not work otherwise (it needs RGB
1943 ;; components for colors, and they are not known on TTYs).
1944 ((and widget-push-button-gui
1945 (console-on-window-system-p))
1946 (let* ((gui-button-shadow-thickness 1))
1947 (setq inst (make-gui-button tag 'widget-gui-action widget))
1948 (setq gui (make-glyph inst)))
1949 (widget-glyph-insert-glyph widget gui nil nil inst))
1953 (defun widget-gui-action (widget)
1954 "Apply :action for WIDGET."
1955 (widget-apply-action widget (this-command-keys)))
1957 ;;; The `link' Widget.
1959 (defcustom widget-link-prefix "["
1960 "String used as prefix for links."
1962 :group 'widget-button)
1964 (defcustom widget-link-suffix "]"
1965 "String used as suffix for links."
1967 :group 'widget-button)
1969 (define-widget 'link 'item
1971 :button-prefix 'widget-link-prefix
1972 :button-suffix 'widget-link-suffix
1973 :help-echo "Follow the link"
1976 ;;; The `info-link' Widget.
1978 (define-widget 'info-link 'link
1979 "A link to an info file."
1980 :help-echo 'widget-info-link-help-echo
1981 :action 'widget-info-link-action)
1983 (defun widget-info-link-help-echo (widget)
1984 (concat "Read the manual entry `" (widget-value widget) "'"))
1986 (defun widget-info-link-action (widget &optional event)
1987 "Open the info node specified by WIDGET."
1988 (Info-goto-node (widget-value widget)))
1990 ;;; The `url-link' Widget.
1992 (define-widget 'url-link 'link
1993 "A link to an www page."
1994 :help-echo 'widget-url-link-help-echo
1995 :action 'widget-url-link-action)
1997 (defun widget-url-link-help-echo (widget)
1998 (concat "Visit <URL:" (widget-value widget) ">"))
2000 (defun widget-url-link-action (widget &optional event)
2001 "Open the url specified by WIDGET."
2002 (if (fboundp 'browse-url)
2003 (browse-url (widget-value widget))
2004 (error "Cannot follow URLs in this XEmacs")))
2006 ;;; The `function-link' Widget.
2008 (define-widget 'function-link 'link
2009 "A link to an Emacs function."
2010 :action 'widget-function-link-action)
2012 (defun widget-function-link-action (widget &optional event)
2013 "Show the function specified by WIDGET."
2014 (describe-function (widget-value widget)))
2016 ;;; The `variable-link' Widget.
2018 (define-widget 'variable-link 'link
2019 "A link to an Emacs variable."
2020 :action 'widget-variable-link-action)
2022 (defun widget-variable-link-action (widget &optional event)
2023 "Show the variable specified by WIDGET."
2024 (describe-variable (widget-value widget)))
2026 ;;; The `file-link' Widget.
2028 (define-widget 'file-link 'link
2030 :action 'widget-file-link-action)
2032 (defun widget-file-link-action (widget &optional event)
2033 "Find the file specified by WIDGET."
2034 (find-file (widget-value widget)))
2036 ;;; The `emacs-library-link' Widget.
2038 (define-widget 'emacs-library-link 'link
2039 "A link to an Emacs Lisp library file."
2040 :help-echo 'widget-emacs-library-link-help-echo
2041 :action 'widget-emacs-library-link-action)
2043 (defun widget-emacs-library-link-help-echo (widget)
2044 (concat "Visit " (widget-value widget)))
2046 (defun widget-emacs-library-link-action (widget &optional event)
2047 "Find the Emacs Library file specified by WIDGET."
2048 (find-file (locate-library (widget-value widget))))
2050 ;;; The `emacs-commentary-link' Widget.
2052 (define-widget 'emacs-commentary-link 'link
2053 "A link to Commentary in an Emacs Lisp library file."
2054 :action 'widget-emacs-commentary-link-action)
2056 (defun widget-emacs-commentary-link-action (widget &optional event)
2057 "Find the Commentary section of the Emacs file specified by WIDGET."
2058 (finder-commentary (widget-value widget)))
2060 ;;; The `editable-field' Widget.
2062 (define-widget 'editable-field 'default
2063 "An editable text field."
2064 :convert-widget 'widget-value-convert-widget
2065 :keymap widget-field-keymap
2068 :prompt-internal 'widget-field-prompt-internal
2069 :prompt-history 'widget-field-history
2070 :prompt-value 'widget-field-prompt-value
2071 :action 'widget-field-action
2072 :validate 'widget-field-validate
2075 :value-create 'widget-field-value-create
2076 :value-delete 'widget-field-value-delete
2077 :value-get 'widget-field-value-get
2078 :match 'widget-field-match)
2080 (defvar widget-field-history nil
2081 "History of field minibuffer edits.")
2083 (defun widget-field-prompt-internal (widget prompt initial history)
2084 ;; Read string for WIDGET prompting with PROMPT.
2085 ;; INITIAL is the initial input and HISTORY is a symbol containing
2086 ;; the earlier input.
2087 (read-string prompt initial history))
2089 (defun widget-field-prompt-value (widget prompt value unbound)
2090 ;; Prompt for a string.
2091 (let ((initial (if unbound
2093 (cons (widget-apply widget :value-to-internal
2095 (history (widget-get widget :prompt-history)))
2096 (let ((answer (widget-apply widget
2097 :prompt-internal prompt initial history)))
2098 (widget-apply widget :value-to-external answer))))
2100 (defvar widget-edit-functions nil)
2102 (defun widget-field-action (widget &optional event)
2103 ;; Edit the value in the minibuffer.
2104 (let* ((invalid (widget-apply widget :validate))
2105 (prompt (concat (widget-apply widget :menu-tag-get) ": "))
2106 (value (unless invalid
2107 (widget-value widget)))
2108 (answer (widget-apply widget :prompt-value prompt value invalid)))
2109 (unless (equal value answer)
2110 ;; This is a hack. We can't properly validate the widget
2111 ;; because validation requires the new value to be in the field.
2112 ;; However, widget-field-value-create will not function unless
2113 ;; the new value matches. So, we check whether the thing
2114 ;; matches, and if it does, use either the real or a dummy error
2116 (unless (widget-apply widget :match answer)
2117 (let ((error-message (or (widget-get widget :type-error)
2118 "Invalid field contents")))
2119 (widget-put widget :error error-message)
2120 (error error-message)))
2121 (widget-value-set widget answer)
2122 (widget-apply widget :notify widget event)
2124 (run-hook-with-args 'widget-edit-functions widget)))
2126 ;(defun widget-field-action (widget &optional event)
2127 ; ;; Move to next field.
2128 ; (widget-forward 1)
2129 ; (run-hook-with-args 'widget-edit-functions widget))
2131 (defun widget-field-validate (widget)
2132 ;; Valid if the content matches `:valid-regexp'.
2134 (let ((value (widget-apply widget :value-get))
2135 (regexp (widget-get widget :valid-regexp)))
2136 (if (string-match regexp value)
2140 (defun widget-field-value-create (widget)
2141 ;; Create an editable text field.
2142 (let ((size (widget-get widget :size))
2143 (value (widget-get widget :value))
2145 ;; This is changed to a real extent in `widget-setup'. We
2146 ;; need the end points to behave differently until
2147 ;; `widget-setup' is called. Should probably be replaced with
2148 ;; a genuine extent, but some things break, then.
2149 (extent (cons (make-marker) (make-marker))))
2150 (widget-put widget :field-extent extent)
2153 (< (length value) size)
2154 (insert-char ?\ (- size (length value))))
2155 (unless (memq widget widget-field-list)
2156 (push widget widget-field-new))
2157 (move-marker (cdr extent) (point))
2158 (set-marker-insertion-type (cdr extent) nil)
2161 (move-marker (car extent) from)
2162 (set-marker-insertion-type (car extent) t)))
2164 (defun widget-field-value-delete (widget)
2165 ;; Remove the widget from the list of active editing fields.
2166 (setq widget-field-list (delq widget widget-field-list))
2167 ;; These are nil if the :format string doesn't contain `%v'.
2168 (let ((extent (widget-get widget :field-extent)))
2170 (detach-extent extent))))
2172 (defun widget-field-value-get (widget)
2173 ;; Return current text in editing field.
2174 (let ((from (widget-field-start widget))
2175 (to (widget-field-end widget))
2176 (buffer (widget-field-buffer widget))
2177 (size (widget-get widget :size))
2178 (secret (widget-get widget :secret))
2179 (old (current-buffer)))
2186 (eq (char-after (1- to)) ?\ ))
2188 (let ((result (buffer-substring-no-properties from to)))
2191 (while (< (+ from index) to)
2193 (get-char-property (+ from index) 'secret))
2198 (widget-get widget :value)))))
2200 (defun widget-field-match (widget value)
2201 ;; Match any string.
2204 ;;; The `text' Widget.
2206 (define-widget 'text 'editable-field
2207 :keymap widget-text-keymap
2208 "A multiline text area.")
2210 ;;; The `menu-choice' Widget.
2212 (define-widget 'menu-choice 'default
2213 "A menu of options."
2214 :convert-widget 'widget-types-convert-widget
2215 :format "%[%t%]: %v"
2218 :void '(item :format "invalid (%t)\n")
2219 :value-create 'widget-choice-value-create
2220 :value-delete 'widget-children-value-delete
2221 :value-get 'widget-choice-value-get
2222 :value-inline 'widget-choice-value-inline
2223 :default-get 'widget-choice-default-get
2224 :mouse-down-action 'widget-choice-mouse-down-action
2225 :action 'widget-choice-action
2226 :error "Make a choice"
2227 :validate 'widget-choice-validate
2228 :match 'widget-choice-match
2229 :match-inline 'widget-choice-match-inline)
2231 (defun widget-choice-value-create (widget)
2232 ;; Insert the first choice that matches the value.
2233 (let ((value (widget-get widget :value))
2234 (args (widget-get widget :args))
2235 (explicit (widget-get widget :explicit-choice))
2239 (widget-put widget :children (list (widget-create-child-value
2240 widget explicit value)))
2241 (widget-put widget :choice explicit))
2243 (setq current (car args)
2245 (when (widget-apply current :match value)
2246 (widget-put widget :children (list (widget-create-child-value
2247 widget current value)))
2248 (widget-put widget :choice current)
2252 (let ((void (widget-get widget :void)))
2253 (widget-put widget :children (list (widget-create-child-and-convert
2254 widget void :value value)))
2255 (widget-put widget :choice void))))))
2257 (defun widget-choice-value-get (widget)
2258 ;; Get value of the child widget.
2259 (widget-value (car (widget-get widget :children))))
2261 (defun widget-choice-value-inline (widget)
2262 ;; Get value of the child widget.
2263 (widget-apply (car (widget-get widget :children)) :value-inline))
2265 (defun widget-choice-default-get (widget)
2266 ;; Get default for the first choice.
2267 (widget-default-get (car (widget-get widget :args))))
2269 (defcustom widget-choice-toggle nil
2270 "If non-nil, a binary choice will just toggle between the values.
2271 Otherwise, the user will explicitly have to choose between the values
2272 when he invoked the menu."
2276 (defun widget-choice-mouse-down-action (widget &optional event)
2277 ;; Return non-nil if we need a menu.
2278 (let ((args (widget-get widget :args))
2279 (old (widget-get widget :choice)))
2280 (cond ((not (console-on-window-system-p))
2281 ;; No place to pop up a menu.
2283 ((< (length args) 2)
2284 ;; Empty or singleton list, just return the value.
2286 ((> (length args) widget-menu-max-size)
2287 ;; Too long, prompt.
2289 ((> (length args) 2)
2290 ;; Reasonable sized list, use menu.
2292 ((and widget-choice-toggle (memq old args))
2296 ;; Ask which of the two.
2299 (defun widget-choice-action (widget &optional event)
2301 (let ((args (widget-get widget :args))
2302 (old (widget-get widget :choice))
2303 (tag (widget-apply widget :menu-tag-get))
2304 (completion-ignore-case (widget-get widget :case-fold))
2306 ;; Remember old value.
2307 (if (and old (not (widget-apply widget :validate)))
2308 (let* ((external (widget-value widget))
2309 (internal (widget-apply old :value-to-internal external)))
2310 (widget-put old :value internal)))
2313 (cond ((= (length args) 0)
2315 ((= (length args) 1)
2317 ((and widget-choice-toggle
2320 (if (eq old (nth 0 args))
2325 (setq current (car args)
2328 (cons (cons (widget-apply current :menu-tag-get)
2332 (widget-choose tag (reverse choices) event)))
2333 (widget-put widget :explicit-choice choice)
2336 (let ((value (widget-default-get current)))
2337 (widget-value-set widget
2338 (widget-apply current :value-to-external value)))
2340 (widget-apply widget :notify widget event)))
2341 (run-hook-with-args 'widget-edit-functions widget))
2343 (defun widget-choice-validate (widget)
2344 ;; Valid if we have made a valid choice.
2345 (let ((void (widget-get widget :void))
2346 (choice (widget-get widget :choice))
2347 (child (car (widget-get widget :children))))
2348 (if (eq void choice)
2350 (widget-apply child :validate))))
2352 (defun widget-choice-match (widget value)
2353 ;; Matches if one of the choices matches.
2354 (let ((args (widget-get widget :args))
2356 (while (and args (not found))
2357 (setq current (car args)
2359 found (widget-apply current :match value)))
2362 (defun widget-choice-match-inline (widget values)
2363 ;; Matches if one of the choices matches.
2364 (let ((args (widget-get widget :args))
2366 (while (and args (null found))
2367 (setq current (car args)
2369 found (widget-match-inline current values)))
2372 ;;; The `toggle' Widget.
2374 (define-widget 'toggle 'item
2375 "Toggle between two states."
2377 :value-create 'widget-toggle-value-create
2378 :action 'widget-toggle-action
2379 :match (lambda (widget value) t)
2383 (defun widget-toggle-value-create (widget)
2384 ;; Insert text representing the `on' and `off' states.
2385 (if (widget-value widget)
2386 (widget-glyph-insert widget
2387 (widget-get widget :on)
2388 (widget-get widget :on-glyph))
2389 (widget-glyph-insert widget
2390 (widget-get widget :off)
2391 (widget-get widget :off-glyph))))
2393 (defun widget-toggle-action (widget &optional event)
2395 (widget-value-set widget (not (widget-value widget)))
2396 (widget-apply widget :notify widget event)
2397 (run-hook-with-args 'widget-edit-functions widget))
2399 ;;; The `checkbox' Widget.
2401 (define-widget 'checkbox 'toggle
2402 "A checkbox toggle."
2410 :action 'widget-checkbox-action)
2412 (defun widget-checkbox-action (widget &optional event)
2413 "Toggle checkbox, notify parent, and set active state of sibling."
2414 (widget-toggle-action widget event)
2415 (let ((sibling (widget-get-sibling widget)))
2417 (if (widget-value widget)
2418 (widget-apply sibling :activate)
2419 (widget-apply sibling :deactivate)))))
2421 ;;; The `checklist' Widget.
2423 (define-widget 'checklist 'default
2424 "A multiple choice widget."
2425 :convert-widget 'widget-types-convert-widget
2428 :entry-format "%b %v"
2429 :menu-tag "checklist"
2431 :value-create 'widget-checklist-value-create
2432 :value-delete 'widget-children-value-delete
2433 :value-get 'widget-checklist-value-get
2434 :validate 'widget-checklist-validate
2435 :match 'widget-checklist-match
2436 :match-inline 'widget-checklist-match-inline)
2438 (defun widget-checklist-value-create (widget)
2439 ;; Insert all values
2440 (let ((alist (widget-checklist-match-find widget (widget-get widget :value)))
2441 (args (widget-get widget :args)))
2443 (widget-checklist-add-item widget (car args) (assq (car args) alist))
2444 (setq args (cdr args)))
2445 (widget-put widget :children (nreverse (widget-get widget :children)))))
2447 (defun widget-checklist-add-item (widget type chosen)
2448 ;; Create checklist item in WIDGET of type TYPE.
2449 ;; If the item is checked, CHOSEN is a cons whose cdr is the value.
2450 (and (eq (preceding-char) ?\n)
2451 (widget-get widget :indent)
2452 (insert-char ?\ (widget-get widget :indent)))
2453 (widget-specify-insert
2454 (let* ((children (widget-get widget :children))
2455 (buttons (widget-get widget :buttons))
2456 (button-args (or (widget-get type :sibling-args)
2457 (widget-get widget :button-args)))
2460 (insert (widget-get widget :entry-format))
2462 ;; Parse % escapes in format.
2463 (while (re-search-forward "%\\([bv%]\\)" nil t)
2464 (let ((escape (aref (match-string 1) 0)))
2465 (replace-match "" t t)
2466 (cond ((eq escape ?%)
2469 (setq button (apply 'widget-create-child-and-convert
2471 :value (not (null chosen))
2476 (let ((child (widget-create-child widget type)))
2477 (widget-apply child :deactivate)
2479 ((widget-get type :inline)
2480 (widget-create-child-value
2481 widget type (cdr chosen)))
2483 (widget-create-child-value
2484 widget type (car (cdr chosen)))))))
2486 (signal 'error (list "Unknown escape" escape))))))
2487 ;; Update properties.
2488 (and button child (widget-put child :button button))
2489 (and button (widget-put widget :buttons (cons button buttons)))
2490 (and child (widget-put widget :children (cons child children))))))
2492 (defun widget-checklist-match (widget values)
2493 ;; All values must match a type in the checklist.
2495 (null (cdr (widget-checklist-match-inline widget values)))))
2497 (defun widget-checklist-match-inline (widget values)
2498 ;; Find the values which match a type in the checklist.
2499 (let ((greedy (widget-get widget :greedy))
2500 (args (copy-sequence (widget-get widget :args)))
2503 (let ((answer (widget-checklist-match-up args values)))
2505 (let ((vals (widget-match-inline answer values)))
2506 (setq found (append found (car vals))
2508 args (delq answer args))))
2510 (setq rest (append rest (list (car values)))
2511 values (cdr values)))
2513 (setq rest (append rest values)
2517 (defun widget-checklist-match-find (widget vals)
2518 ;; Find the vals which match a type in the checklist.
2519 ;; Return an alist of (TYPE MATCH).
2520 (let ((greedy (widget-get widget :greedy))
2521 (args (copy-sequence (widget-get widget :args)))
2524 (let ((answer (widget-checklist-match-up args vals)))
2526 (let ((match (widget-match-inline answer vals)))
2527 (setq found (cons (cons answer (car match)) found)
2529 args (delq answer args))))
2531 (setq vals (cdr vals)))
2536 (defun widget-checklist-match-up (args vals)
2537 ;; Return the first type from ARGS that matches VALS.
2538 (let (current found)
2539 (while (and args (null found))
2540 (setq current (car args)
2542 found (widget-match-inline current vals)))
2547 (defun widget-checklist-value-get (widget)
2548 ;; The values of all selected items.
2549 (let ((children (widget-get widget :children))
2552 (setq child (car children)
2553 children (cdr children))
2554 (if (widget-value (widget-get child :button))
2555 (setq result (append result (widget-apply child :value-inline)))))
2558 (defun widget-checklist-validate (widget)
2559 ;; Ticked children must be valid.
2560 (let ((children (widget-get widget :children))
2562 (while (and children (not found))
2563 (setq child (car children)
2564 children (cdr children)
2565 button (widget-get child :button)
2566 found (and (widget-value button)
2567 (widget-apply child :validate))))
2570 ;;; The `option' Widget
2572 (define-widget 'option 'checklist
2573 "An widget with an optional item."
2576 ;;; The `choice-item' Widget.
2578 (define-widget 'choice-item 'item
2579 "Button items that delegate action events to their parents."
2580 :action 'widget-parent-action
2581 :format "%[%t%] \n")
2583 ;;; The `radio-button' Widget.
2585 (define-widget 'radio-button 'toggle
2586 "A radio button for use in the `radio' widget."
2587 :notify 'widget-radio-button-notify
2592 :on-glyph '("radio1" nil "radio0")
2594 :off-glyph "radio0")
2596 (defun widget-radio-button-notify (widget child &optional event)
2598 (widget-apply (widget-get widget :parent) :action widget event))
2600 ;;; The `radio-button-choice' Widget.
2602 (define-widget 'radio-button-choice 'default
2603 "Select one of multiple options."
2604 :convert-widget 'widget-types-convert-widget
2607 :entry-format "%b %v"
2609 :value-create 'widget-radio-value-create
2610 :value-delete 'widget-children-value-delete
2611 :value-get 'widget-radio-value-get
2612 :value-inline 'widget-radio-value-inline
2613 :value-set 'widget-radio-value-set
2614 :error "You must push one of the buttons"
2615 :validate 'widget-radio-validate
2616 :match 'widget-choice-match
2617 :match-inline 'widget-choice-match-inline
2618 :action 'widget-radio-action)
2620 (defun widget-radio-value-create (widget)
2621 ;; Insert all values
2622 (let ((args (widget-get widget :args))
2625 (setq arg (car args)
2627 (widget-radio-add-item widget arg))))
2629 (defun widget-radio-add-item (widget type)
2630 "Add to radio widget WIDGET a new radio button item of type TYPE."
2631 ;; (setq type (widget-convert type))
2632 (and (eq (preceding-char) ?\n)
2633 (widget-get widget :indent)
2634 (insert-char ?\ (widget-get widget :indent)))
2635 (widget-specify-insert
2636 (let* ((value (widget-get widget :value))
2637 (children (widget-get widget :children))
2638 (buttons (widget-get widget :buttons))
2639 (button-args (or (widget-get type :sibling-args)
2640 (widget-get widget :button-args)))
2642 (chosen (and (null (widget-get widget :choice))
2643 (widget-apply type :match value)))
2645 (insert (widget-get widget :entry-format))
2647 ;; Parse % escapes in format.
2648 (while (re-search-forward "%\\([bv%]\\)" nil t)
2649 (let ((escape (aref (match-string 1) 0)))
2650 (replace-match "" t t)
2651 (cond ((eq escape ?%)
2654 (setq button (apply 'widget-create-child-and-convert
2655 widget 'radio-button
2656 :value (not (null chosen))
2659 (setq child (if chosen
2660 (widget-create-child-value
2662 (widget-create-child widget type)))
2664 (widget-apply child :deactivate)))
2666 (signal 'error (list "Unknown escape" escape))))))
2667 ;; Update properties.
2669 (widget-put widget :choice type))
2671 (widget-put child :button button)
2672 (widget-put widget :buttons (nconc buttons (list button))))
2674 (widget-put widget :children (nconc children (list child))))
2677 (defun widget-radio-value-get (widget)
2678 ;; Get value of the child widget.
2679 (let ((chosen (widget-radio-chosen widget)))
2680 (and chosen (widget-value chosen))))
2682 (defun widget-radio-chosen (widget)
2683 "Return the widget representing the chosen radio button."
2684 (let ((children (widget-get widget :children))
2687 (setq current (car children)
2688 children (cdr children))
2689 (let* ((button (widget-get current :button))
2690 (value (widget-apply button :value-get)))
2696 (defun widget-radio-value-inline (widget)
2697 ;; Get value of the child widget.
2698 (let ((children (widget-get widget :children))
2701 (setq current (car children)
2702 children (cdr children))
2703 (let* ((button (widget-get current :button))
2704 (value (widget-apply button :value-get)))
2706 (setq found (widget-apply current :value-inline)
2710 (defun widget-radio-value-set (widget value)
2711 ;; We can't just delete and recreate a radio widget, since children
2712 ;; can be added after the original creation and won't be recreated
2714 (let ((children (widget-get widget :children))
2717 (setq current (car children)
2718 children (cdr children))
2719 (let* ((button (widget-get current :button))
2720 (match (and (not found)
2721 (widget-apply current :match value))))
2722 (widget-value-set button match)
2725 (widget-value-set current value)
2726 (widget-apply current :activate))
2727 (widget-apply current :deactivate))
2728 (setq found (or found match))))))
2730 (defun widget-radio-validate (widget)
2731 ;; Valid if we have made a valid choice.
2732 (let ((children (widget-get widget :children))
2733 current found button)
2734 (while (and children (not found))
2735 (setq current (car children)
2736 children (cdr children)
2737 button (widget-get current :button)
2738 found (widget-apply button :value-get)))
2740 (widget-apply current :validate)
2743 (defun widget-radio-action (widget child event)
2744 ;; Check if a radio button was pressed.
2745 (let ((children (widget-get widget :children))
2746 (buttons (widget-get widget :buttons))
2748 (when (memq child buttons)
2750 (setq current (car children)
2751 children (cdr children))
2752 (let* ((button (widget-get current :button)))
2753 (cond ((eq child button)
2754 (widget-value-set button t)
2755 (widget-apply current :activate))
2756 ((widget-value button)
2757 (widget-value-set button nil)
2758 (widget-apply current :deactivate)))))))
2759 ;; Pass notification to parent.
2760 (widget-apply widget :notify child event))
2762 ;;; The `insert-button' Widget.
2764 (define-widget 'insert-button 'push-button
2765 "An insert button for the `editable-list' widget."
2767 :help-echo "Insert a new item into the list at this position"
2768 :action 'widget-insert-button-action)
2770 (defun widget-insert-button-action (widget &optional event)
2771 ;; Ask the parent to insert a new item.
2772 (widget-apply (widget-get widget :parent)
2773 :insert-before (widget-get widget :widget)))
2775 ;;; The `delete-button' Widget.
2777 (define-widget 'delete-button 'push-button
2778 "A delete button for the `editable-list' widget."
2780 :help-echo "Delete this item from the list"
2781 :action 'widget-delete-button-action)
2783 (defun widget-delete-button-action (widget &optional event)
2784 ;; Ask the parent to insert a new item.
2785 (widget-apply (widget-get widget :parent)
2786 :delete-at (widget-get widget :widget)))
2788 ;;; The `editable-list' Widget.
2790 (defcustom widget-editable-list-gui nil
2791 "If non nil, use GUI push-buttons in editable list when available."
2795 (define-widget 'editable-list 'default
2796 "A variable list of widgets of the same type."
2797 :convert-widget 'widget-types-convert-widget
2800 :format-handler 'widget-editable-list-format-handler
2801 :entry-format "%i %d %v"
2802 :menu-tag "editable-list"
2803 :value-create 'widget-editable-list-value-create
2804 :value-delete 'widget-children-value-delete
2805 :value-get 'widget-editable-list-value-get
2806 :validate 'widget-children-validate
2807 :match 'widget-editable-list-match
2808 :match-inline 'widget-editable-list-match-inline
2809 :insert-before 'widget-editable-list-insert-before
2810 :delete-at 'widget-editable-list-delete-at)
2812 (defun widget-editable-list-format-handler (widget escape)
2813 ;; We recognize the insert button.
2814 (let ((widget-push-button-gui widget-editable-list-gui))
2815 (cond ((eq escape ?i)
2816 (and (widget-get widget :indent)
2817 (insert-char ?\ (widget-get widget :indent)))
2818 (apply 'widget-create-child-and-convert
2819 widget 'insert-button
2820 (widget-get widget :append-button-args)))
2822 (widget-default-format-handler widget escape)))))
2824 (defun widget-editable-list-value-create (widget)
2825 ;; Insert all values
2826 (let* ((value (widget-get widget :value))
2827 (type (nth 0 (widget-get widget :args)))
2828 (inlinep (widget-get type :inline))
2830 (widget-put widget :value-pos (copy-marker (point)))
2831 (set-marker-insertion-type (widget-get widget :value-pos) t)
2833 (let ((answer (widget-match-inline type value)))
2835 (setq children (cons (widget-editable-list-entry-create
2844 (widget-put widget :children (nreverse children))))
2846 (defun widget-editable-list-value-get (widget)
2847 ;; Get value of the child widget.
2848 (apply 'append (mapcar (lambda (child) (widget-apply child :value-inline))
2849 (widget-get widget :children))))
2851 (defun widget-editable-list-match (widget value)
2852 ;; Value must be a list and all the members must match the type.
2854 (null (cdr (widget-editable-list-match-inline widget value)))))
2856 (defun widget-editable-list-match-inline (widget value)
2857 (let ((type (nth 0 (widget-get widget :args)))
2860 (while (and value ok)
2861 (let ((answer (widget-match-inline type value)))
2863 (setq found (append found (car answer))
2866 (cons found value)))
2868 (defun widget-editable-list-insert-before (widget before)
2869 ;; Insert a new child in the list of children.
2871 (let ((children (widget-get widget :children))
2872 (inhibit-read-only t)
2873 before-change-functions
2874 after-change-functions)
2876 (goto-char (widget-get before :entry-from)))
2878 (goto-char (widget-get widget :value-pos))))
2879 (let ((child (widget-editable-list-entry-create
2881 (when (< (widget-get child :entry-from) (widget-get widget :from))
2882 (set-marker (widget-get widget :from)
2883 (widget-get child :entry-from)))
2884 (if (eq (car children) before)
2885 (widget-put widget :children (cons child children))
2886 (while (not (eq (car (cdr children)) before))
2887 (setq children (cdr children)))
2888 (setcdr children (cons child (cdr children)))))))
2890 (widget-apply widget :notify widget))
2892 (defun widget-editable-list-delete-at (widget child)
2893 ;; Delete child from list of children.
2895 (let ((buttons (copy-sequence (widget-get widget :buttons)))
2897 (inhibit-read-only t)
2898 before-change-functions
2899 after-change-functions)
2901 (setq button (car buttons)
2902 buttons (cdr buttons))
2903 (when (eq (widget-get button :widget) child)
2905 :buttons (delq button (widget-get widget :buttons)))
2906 (widget-delete button))))
2907 (let ((entry-from (widget-get child :entry-from))
2908 (entry-to (widget-get child :entry-to))
2909 (inhibit-read-only t)
2910 before-change-functions
2911 after-change-functions)
2912 (widget-delete child)
2913 (delete-region entry-from entry-to)
2914 (set-marker entry-from nil)
2915 (set-marker entry-to nil))
2916 (widget-put widget :children (delq child (widget-get widget :children))))
2918 (widget-apply widget :notify widget))
2920 (defun widget-editable-list-entry-create (widget value conv)
2921 ;; Create a new entry to the list.
2922 (let ((type (nth 0 (widget-get widget :args)))
2923 (widget-push-button-gui widget-editable-list-gui)
2924 child delete insert)
2925 (widget-specify-insert
2927 (and (widget-get widget :indent)
2928 (insert-char ?\ (widget-get widget :indent)))
2929 (insert (widget-get widget :entry-format)))
2930 ;; Parse % escapes in format.
2931 (while (re-search-forward "%\\(.\\)" nil t)
2932 (let ((escape (aref (match-string 1) 0)))
2933 (replace-match "" t t)
2934 (cond ((eq escape ?%)
2937 (setq insert (apply 'widget-create-child-and-convert
2938 widget 'insert-button
2939 (widget-get widget :insert-button-args))))
2941 (setq delete (apply 'widget-create-child-and-convert
2942 widget 'delete-button
2943 (widget-get widget :delete-button-args))))
2946 (setq child (widget-create-child-value
2948 (setq child (widget-create-child-value
2949 widget type (widget-default-get type)))))
2951 (signal 'error (list "Unknown escape" escape))))))
2953 :buttons (cons delete
2955 (widget-get widget :buttons))))
2956 (let ((entry-from (copy-marker (point-min)))
2957 (entry-to (copy-marker (point-max))))
2958 (set-marker-insertion-type entry-from t)
2959 (set-marker-insertion-type entry-to nil)
2960 (widget-put child :entry-from entry-from)
2961 (widget-put child :entry-to entry-to)))
2962 (widget-put insert :widget child)
2963 (widget-put delete :widget child)
2966 ;;; The `group' Widget.
2968 (define-widget 'group 'default
2969 "A widget which group other widgets inside."
2970 :convert-widget 'widget-types-convert-widget
2972 :value-create 'widget-group-value-create
2973 :value-delete 'widget-children-value-delete
2974 :value-get 'widget-editable-list-value-get
2975 :default-get 'widget-group-default-get
2976 :validate 'widget-children-validate
2977 :match 'widget-group-match
2978 :match-inline 'widget-group-match-inline)
2980 (defun widget-group-value-create (widget)
2981 ;; Create each component.
2982 (let ((args (widget-get widget :args))
2983 (value (widget-get widget :value))
2984 arg answer children)
2986 (setq arg (car args)
2988 answer (widget-match-inline arg value)
2990 (and (eq (preceding-char) ?\n)
2991 (widget-get widget :indent)
2992 (insert-char ?\ (widget-get widget :indent)))
2993 (push (cond ((null answer)
2994 (widget-create-child widget arg))
2995 ((widget-get arg :inline)
2996 (widget-create-child-value widget arg (car answer)))
2998 (widget-create-child-value widget arg (car (car answer)))))
3000 (widget-put widget :children (nreverse children))))
3002 (defun widget-group-default-get (widget)
3003 ;; Get the default of the components.
3004 (mapcar 'widget-default-get (widget-get widget :args)))
3006 (defun widget-group-match (widget values)
3007 ;; Match if the components match.
3009 (let ((match (widget-group-match-inline widget values)))
3010 (and match (null (cdr match))))))
3012 (defun widget-group-match-inline (widget vals)
3013 ;; Match if the components match.
3014 (let ((args (widget-get widget :args))
3015 argument answer found)
3017 (setq argument (car args)
3019 answer (widget-match-inline argument vals))
3021 (setq vals (cdr answer)
3022 found (append found (car answer)))
3029 ;;; The `visibility' Widget.
3031 (define-widget 'visibility 'item
3032 "An indicator and manipulator for hidden items."
3038 :value-create 'widget-visibility-value-create
3039 :action 'widget-toggle-action
3040 :match (lambda (widget value) t))
3042 (defun widget-visibility-value-create (widget)
3043 ;; Insert text representing the `on' and `off' states.
3044 (let ((on (widget-get widget :on))
3045 (off (widget-get widget :off)))
3047 (setq on (concat widget-push-button-prefix
3049 widget-push-button-suffix))
3052 (setq off (concat widget-push-button-prefix
3054 widget-push-button-suffix))
3056 (if (widget-value widget)
3057 (widget-glyph-insert widget on '("down" "down-pushed"))
3058 (widget-glyph-insert widget off '("right" "right-pushed")))))
3060 ;;; The `documentation-link' Widget.
3062 ;; This is a helper widget for `documentation-string'.
3064 (define-widget 'documentation-link 'link
3065 "Link type used in documentation strings."
3067 :help-echo 'widget-documentation-link-echo-help
3068 :action 'widget-documentation-link-action)
3070 (defun widget-documentation-link-echo-help (widget)
3071 "Tell what this link will describe."
3072 (concat "Describe the `" (widget-get widget :value) "' symbol."))
3074 (defun widget-documentation-link-action (widget &optional event)
3075 "Display documentation for WIDGET's value. Ignore optional argument EVENT."
3076 (let* ((string (widget-get widget :value))
3077 (symbol (intern string)))
3078 (if (and (fboundp symbol) (boundp symbol))
3079 ;; If there are two doc strings, give the user a way to pick one.
3080 (apropos (concat "\\`" (regexp-quote string) "\\'"))
3081 (if (fboundp symbol)
3082 (describe-function symbol)
3083 (describe-variable symbol)))))
3085 (defcustom widget-documentation-links t
3086 "Add hyperlinks to documentation strings when non-nil."
3088 :group 'widget-documentation)
3090 (defcustom widget-documentation-link-regexp "`\\([^\n`' ]+\\)'"
3091 "Regexp for matching potential links in documentation strings.
3092 The first group should be the link itself."
3094 :group 'widget-documentation)
3096 (defcustom widget-documentation-link-p 'intern-soft
3097 "Predicate used to test if a string is useful as a link.
3098 The value should be a function. The function will be called one
3099 argument, a string, and should return non-nil if there should be a
3100 link for that string."
3102 :options '(widget-documentation-link-p)
3103 :group 'widget-documentation)
3105 (defcustom widget-documentation-link-type 'documentation-link
3106 "Widget type used for links in documentation strings."
3108 :group 'widget-documentation)
3110 (defun widget-documentation-link-add (widget from to)
3111 (widget-specify-doc widget from to)
3112 (when widget-documentation-links
3113 (let ((regexp widget-documentation-link-regexp)
3114 (predicate widget-documentation-link-p)
3115 (type widget-documentation-link-type)
3116 (buttons (widget-get widget :buttons)))
3119 (while (re-search-forward regexp to t)
3120 (let ((name (match-string 1))
3121 (begin (match-beginning 1))
3122 (end (match-end 1)))
3123 (when (funcall predicate name)
3124 (push (widget-convert-button type begin end :value name)
3126 (widget-put widget :buttons buttons)))
3127 (let ((indent (widget-get widget :indent)))
3128 (when (and indent (not (zerop indent)))
3131 (narrow-to-region from to)
3132 (goto-char (point-min))
3133 (while (search-forward "\n" nil t)
3134 (insert-char ?\ indent)))))))
3136 ;;; The `documentation-string' Widget.
3138 (define-widget 'documentation-string 'item
3139 "A documentation string."
3141 :action 'widget-documentation-string-action
3142 :value-delete 'widget-children-value-delete
3143 :value-create 'widget-documentation-string-value-create)
3145 (defun widget-documentation-string-value-create (widget)
3146 ;; Insert documentation string.
3147 (let ((doc (widget-value widget))
3148 (indent (widget-get widget :indent))
3149 (shown (widget-get (widget-get widget :parent) :documentation-shown))
3151 (if (string-match "\n" doc)
3152 (let ((before (substring doc 0 (match-beginning 0)))
3153 (after (substring doc (match-beginning 0)))
3156 (widget-documentation-link-add widget start (point))
3157 (push (widget-create-child-and-convert
3159 :help-echo (lambda (widget)
3161 (if (widget-value widget)
3163 " the rest of the documentation"))
3165 :action 'widget-parent-action
3169 (setq start (point))
3171 (insert-char ?\ indent))
3173 (widget-documentation-link-add widget start (point)))
3174 (widget-put widget :buttons buttons))
3176 (widget-documentation-link-add widget start (point))))
3179 (defun widget-documentation-string-action (widget &rest ignore)
3180 ;; Toggle documentation.
3181 (let ((parent (widget-get widget :parent)))
3182 (widget-put parent :documentation-shown
3183 (not (widget-get parent :documentation-shown))))
3185 (widget-value-set widget (widget-value widget)))
3187 ;;; The Sexp Widgets.
3189 (define-widget 'const 'item
3190 "An immutable sexp."
3191 :prompt-value 'widget-const-prompt-value
3194 (defun widget-const-prompt-value (widget prompt value unbound)
3195 ;; Return the value of the const.
3196 (widget-value widget))
3198 (define-widget 'function-item 'const
3199 "An immutable function name."
3201 :documentation-property (lambda (symbol)
3203 (documentation symbol t)
3206 (define-widget 'variable-item 'const
3207 "An immutable variable name."
3209 :documentation-property 'variable-documentation)
3211 (defvar widget-string-prompt-value-history nil
3212 "History of input to `widget-string-prompt-value'.")
3214 (define-widget 'string 'editable-field
3217 :format "%{%t%}: %v"
3218 :complete-function 'ispell-complete-word
3219 :prompt-history 'widget-string-prompt-value-history)
3221 (define-widget 'regexp 'string
3222 "A regular expression."
3223 :match 'widget-regexp-match
3224 :validate 'widget-regexp-validate
3225 ;; Doesn't work well with terminating newline.
3226 ;; :value-face 'widget-single-line-field-face
3229 (defun widget-regexp-match (widget value)
3230 ;; Match valid regexps.
3231 (and (stringp value)
3234 (string-match value ""))
3237 (defun widget-regexp-validate (widget)
3238 "Check that the value of WIDGET is a valid regexp."
3239 (let ((value (widget-value widget)))
3240 (condition-case data
3242 (string-match value ""))
3243 (error (widget-put widget :error (error-message-string data))
3246 (define-widget 'file 'string
3248 It will read a file name from the minibuffer when invoked."
3249 :complete-function 'widget-file-complete
3250 :prompt-value 'widget-file-prompt-value
3251 :format "%{%t%}: %v"
3252 ;; Doesn't work well with terminating newline.
3253 ;; :value-face 'widget-single-line-field-face
3256 (defun widget-file-complete ()
3257 "Perform completion on file name preceding point."
3259 (let* ((end (point))
3260 (beg (save-excursion
3261 (skip-chars-backward "^ ")
3263 (pattern (buffer-substring beg end))
3264 (name-part (file-name-nondirectory pattern))
3265 (directory (file-name-directory pattern))
3266 (completion (file-name-completion name-part directory)))
3267 (cond ((eq completion t))
3269 (message "Can't find completion for \"%s\"" pattern)
3271 ((not (string= name-part completion))
3272 (delete-region beg end)
3273 (insert (expand-file-name completion directory)))
3275 (message "Making completion list...")
3276 (let ((list (file-name-all-completions name-part directory)))
3277 (setq list (sort list 'string<))
3278 (with-output-to-temp-buffer "*Completions*"
3279 (display-completion-list list)))
3280 (message "Making completion list...%s" "done")))))
3282 (defun widget-file-prompt-value (widget prompt value unbound)
3283 ;; Read file from minibuffer.
3284 (abbreviate-file-name
3286 (read-file-name prompt)
3287 (let ((prompt2 (format "%s (default %s) " prompt value))
3288 (dir (file-name-directory value))
3289 (file (file-name-nondirectory value))
3290 (must-match (widget-get widget :must-match)))
3291 (read-file-name prompt2 dir nil must-match file)))))
3293 ;;;(defun widget-file-action (widget &optional event)
3294 ;;; ;; Read a file name from the minibuffer.
3295 ;;; (let* ((value (widget-value widget))
3296 ;;; (dir (file-name-directory value))
3297 ;;; (file (file-name-nondirectory value))
3298 ;;; (menu-tag (widget-apply widget :menu-tag-get))
3299 ;;; (must-match (widget-get widget :must-match))
3300 ;;; (answer (read-file-name (concat menu-tag ": (default `" value "') ")
3301 ;;; dir nil must-match file)))
3302 ;;; (widget-value-set widget (abbreviate-file-name answer))
3304 ;;; (widget-apply widget :notify widget event)))
3306 (define-widget 'directory 'file
3307 "A directory widget.
3308 It will read a directory name from the minibuffer when invoked."
3311 (defvar widget-symbol-prompt-value-history nil
3312 "History of input to `widget-symbol-prompt-value'.")
3314 (define-widget 'symbol 'editable-field
3318 :format "%{%t%}: %v"
3319 :match (lambda (widget value) (symbolp value))
3320 :complete-function 'lisp-complete-symbol
3321 :prompt-internal 'widget-symbol-prompt-internal
3322 :prompt-match 'symbolp
3323 :prompt-history 'widget-symbol-prompt-value-history
3324 :value-to-internal (lambda (widget value)
3328 :value-to-external (lambda (widget value)
3333 (defun widget-symbol-prompt-internal (widget prompt initial history)
3334 ;; Read file from minibuffer.
3335 (let ((answer (completing-read prompt obarray
3336 (widget-get widget :prompt-match)
3337 nil initial history)))
3338 (if (and (stringp answer)
3339 (not (zerop (length answer))))
3341 (error "No value"))))
3343 (defvar widget-function-prompt-value-history nil
3344 "History of input to `widget-function-prompt-value'.")
3346 (define-widget 'function 'sexp
3348 :complete-function 'lisp-complete-symbol
3349 :prompt-value 'widget-field-prompt-value
3350 :prompt-internal 'widget-symbol-prompt-internal
3351 :prompt-match 'fboundp
3352 :prompt-history 'widget-function-prompt-value-history
3353 :action 'widget-field-action
3356 (defvar widget-variable-prompt-value-history nil
3357 "History of input to `widget-variable-prompt-value'.")
3359 (define-widget 'variable 'symbol
3360 ;; Should complete on variables.
3362 :prompt-match 'boundp
3363 :prompt-history 'widget-variable-prompt-value-history
3366 ;; This part issues a warning when compiling without Mule. Is there a
3367 ;; way of shutting it up?
3369 ;; OK, I'll simply comment the whole thing out, until someone decides
3370 ;; to do something with it.
3371 ;(defvar widget-coding-system-prompt-value-history nil
3372 ; "History of input to `widget-coding-system-prompt-value'.")
3374 ;(define-widget 'coding-system 'symbol
3375 ; "A MULE coding-system."
3376 ; :format "%{%t%}: %v"
3377 ; :tag "Coding system"
3378 ; :prompt-history 'widget-coding-system-prompt-value-history
3379 ; :prompt-value 'widget-coding-system-prompt-value
3380 ; :action 'widget-coding-system-action)
3382 ;(defun widget-coding-system-prompt-value (widget prompt value unbound)
3383 ; ;; Read coding-system from minibuffer.
3385 ; (completing-read (format "%s (default %s) " prompt value)
3386 ; (mapcar (lambda (sym)
3387 ; (list (symbol-name sym)))
3388 ; (coding-system-list)))))
3390 ;(defun widget-coding-system-action (widget &optional event)
3391 ; ;; Read a file name from the minibuffer.
3393 ; (widget-coding-system-prompt-value
3395 ; (widget-apply widget :menu-tag-get)
3396 ; (widget-value widget)
3398 ; (widget-value-set widget answer)
3399 ; (widget-apply widget :notify widget event)
3402 (define-widget 'sexp 'editable-field
3403 "An arbitrary lisp expression."
3404 :tag "Lisp expression"
3405 :format "%{%t%}: %v"
3407 :validate 'widget-sexp-validate
3408 :match (lambda (widget value) t)
3409 :value-to-internal 'widget-sexp-value-to-internal
3410 :value-to-external (lambda (widget value) (read value))
3411 :prompt-history 'widget-sexp-prompt-value-history
3412 :prompt-value 'widget-sexp-prompt-value)
3414 (defun widget-sexp-value-to-internal (widget value)
3415 ;; Use cl-prettyprint for printer representation.
3416 (let ((pp (if (symbolp value)
3417 (prin1-to-string value)
3418 (widget-prettyprint-to-string value))))
3419 (if (> (length pp) 40)
3423 (defun widget-sexp-validate (widget)
3424 ;; Valid if we can read the string and there is no junk left after it.
3426 (let ((buffer (set-buffer (get-buffer-create " *Widget Scratch*"))))
3428 (insert (widget-apply widget :value-get))
3429 (goto-char (point-min))
3430 (condition-case data
3431 (let ((value (read buffer)))
3433 (if (widget-apply widget :match value)
3435 (widget-put widget :error (widget-get widget :type-error))
3438 :error (format "Junk at end of expression: %s"
3439 (buffer-substring (point)
3442 (error (widget-put widget :error (error-message-string data))
3445 (defvar widget-sexp-prompt-value-history nil
3446 "History of input to `widget-sexp-prompt-value'.")
3448 (defun widget-sexp-prompt-value (widget prompt value unbound)
3449 ;; Read an arbitrary sexp.
3450 (let ((found (read-string prompt
3451 (if unbound nil (cons (prin1-to-string value) 0))
3452 (widget-get widget :prompt-history))))
3454 (let ((buffer (set-buffer (get-buffer-create " *Widget Scratch*"))))
3457 (goto-char (point-min))
3458 (let ((answer (read buffer)))
3461 (list "Junk at end of expression"
3462 (buffer-substring (point) (point-max)))))
3465 (define-widget 'restricted-sexp 'sexp
3466 "A Lisp expression restricted to values that match.
3467 To use this type, you must define :match or :match-alternatives."
3468 :type-error "The specified value is not valid"
3469 :match 'widget-restricted-sexp-match
3470 :value-to-internal (lambda (widget value)
3471 (if (widget-apply widget :match value)
3472 (prin1-to-string value)
3475 (defun widget-restricted-sexp-match (widget value)
3476 (let ((alternatives (widget-get widget :match-alternatives))
3478 (while (and alternatives (not matched))
3479 (if (cond ((functionp (car alternatives))
3480 (funcall (car alternatives) value))
3481 ((and (consp (car alternatives))
3482 (eq (car (car alternatives)) 'quote))
3483 (eq value (nth 1 (car alternatives)))))
3485 (setq alternatives (cdr alternatives)))
3488 (define-widget 'integer 'restricted-sexp
3492 :type-error "This field should contain an integer"
3493 :match-alternatives '(integerp))
3495 (define-widget 'number 'restricted-sexp
3496 "A floating point number."
3499 :type-error "This field should contain a number"
3500 :match-alternatives '(numberp))
3502 (define-widget 'character 'editable-field
3506 :format "%{%t%}: %v"
3507 :valid-regexp "\\`[\0-\377]\\'"
3508 :error "This field should contain a single character"
3509 :value-to-internal (lambda (widget value)
3512 (char-to-string value)))
3513 :value-to-external (lambda (widget value)
3517 :match (lambda (widget value)
3518 (characterp value)))
3520 (define-widget 'list 'group
3523 :format "%{%t%}:\n%v")
3525 (define-widget 'vector 'group
3528 :format "%{%t%}:\n%v"
3529 :match 'widget-vector-match
3530 :value-to-internal (lambda (widget value) (append value nil))
3531 :value-to-external (lambda (widget value) (vconcat value)))
3533 (defun widget-vector-match (widget value)
3534 (and (vectorp value)
3535 (widget-group-match widget
3536 (widget-apply widget :value-to-internal value))))
3538 (define-widget 'cons 'group
3541 :format "%{%t%}:\n%v"
3542 :match 'widget-cons-match
3543 :value-to-internal (lambda (widget value)
3544 (list (car value) (cdr value)))
3545 :value-to-external (lambda (widget value)
3546 (cons (car value) (cadr value))))
3548 (defun widget-cons-match (widget value)
3550 (widget-group-match widget
3551 (widget-apply widget :value-to-internal value))))
3553 (define-widget 'choice 'menu-choice
3554 "A union of several sexp types."
3556 :format "%{%t%}: %[Value Menu%] %v"
3557 :button-prefix 'widget-push-button-prefix
3558 :button-suffix 'widget-push-button-suffix
3559 :prompt-value 'widget-choice-prompt-value)
3561 (defun widget-choice-prompt-value (widget prompt value unbound)
3563 (let ((args (widget-get widget :args))
3564 (completion-ignore-case (widget-get widget :case-fold))
3565 current choices old)
3566 ;; Find the first arg that match VALUE.
3569 (if (widget-apply (car look) :match value)
3570 (setq old (car look)
3572 (setq look (cdr look)))))
3575 (cond ((= (length args) 0)
3577 ((= (length args) 1)
3579 ((and (= (length args) 2)
3581 (if (eq old (nth 0 args))
3586 (setq current (car args)
3589 (cons (cons (widget-apply current :menu-tag-get)
3592 (let ((val (completing-read prompt choices nil t)))
3594 (let ((try (try-completion val choices)))
3597 (cdr (assoc val choices)))
3600 (widget-prompt-value current prompt nil t)
3603 (define-widget 'radio 'radio-button-choice
3604 "A union of several sexp types."
3606 :format "%{%t%}:\n%v"
3607 :prompt-value 'widget-choice-prompt-value)
3609 (define-widget 'repeat 'editable-list
3610 "A variable length homogeneous list."
3612 :format "%{%t%}:\n%v%i\n")
3614 (define-widget 'set 'checklist
3615 "A list of members from a fixed set."
3617 :format "%{%t%}:\n%v")
3619 (define-widget 'boolean 'toggle
3620 "To be nil or non-nil, that is the question."
3622 :prompt-value 'widget-boolean-prompt-value
3623 :button-prefix 'widget-push-button-prefix
3624 :button-suffix 'widget-push-button-suffix
3625 :format "%{%t%}: %[Toggle%] %v\n"
3629 (defun widget-boolean-prompt-value (widget prompt value unbound)
3630 ;; Toggle a boolean.
3633 ;;; The `color' Widget.
3635 (define-widget 'color 'editable-field
3636 "Choose a color name (with sample)."
3637 :format "%[%t%]: %v (%{sample%})\n"
3641 :complete 'widget-color-complete
3642 :sample-face-get 'widget-color-sample-face-get
3643 :notify 'widget-color-notify
3644 :action 'widget-color-action)
3646 (defun widget-color-complete (widget)
3647 "Complete the color in WIDGET."
3648 (let* ((prefix (buffer-substring-no-properties (widget-field-start widget)
3650 (list (read-color-completion-table))
3651 (completion (try-completion prefix list)))
3652 (cond ((eq completion t)
3653 (message "Exact match"))
3655 (error "Can't find completion for \"%s\"" prefix))
3656 ((not (string-equal prefix completion))
3657 (insert (substring completion (length prefix))))
3659 (message "Making completion list...")
3660 (let ((list (all-completions prefix list nil)))
3661 (with-output-to-temp-buffer "*Completions*"
3662 (display-completion-list list)))
3663 (message "Making completion list...done")))))
3665 (defun widget-color-sample-face-get (widget)
3666 (or (widget-get widget :sample-face)
3667 (let ((color (widget-value widget))
3668 (face (make-face (gensym "sample-face-") nil t)))
3669 ;; Use the face object, not its name, to prevent lossage if gc
3670 ;; happens before applying the face.
3671 (widget-put widget :sample-face face)
3673 (not (equal color ""))
3674 (valid-color-name-p color)
3675 (set-face-foreground face color))
3678 (defvar widget-color-history nil
3679 "History of entered colors.")
3681 (defun widget-color-action (widget &optional event)
3682 ;; Prompt for a color.
3683 (let* ((tag (widget-apply widget :menu-tag-get))
3684 (answer (read-color (concat tag ": "))))
3685 (unless (zerop (length answer))
3686 (widget-value-set widget answer)
3688 (widget-apply widget :notify widget event))))
3690 (defun widget-color-notify (widget child &optional event)
3691 "Update the sample, and notify the parent."
3692 (let* ((face (widget-apply widget :sample-face-get))
3693 (color (widget-value widget)))
3694 (if (valid-color-name-p color)
3695 (set-face-foreground face color)
3696 (remove-face-property face 'foreground)))
3697 (widget-default-notify widget child event))
3699 ;; Is this a misnomer?
3700 (defun widget-at (pos)
3701 "The button or field at POS."
3702 (or (get-char-property pos 'button)
3703 (get-char-property pos 'field)))
3705 (defun widget-echo-help (pos)
3706 "Display the help echo for widget at POS."
3707 (let* ((widget (widget-at pos))
3708 (help-echo (and widget (widget-get widget :help-echo))))
3709 (and (functionp help-echo)
3710 (setq help-echo (funcall help-echo widget)))
3711 (when (stringp help-echo)
3712 (display-message 'help-echo help-echo))))
3718 ;; wid-edit.el ends here