1 ;;; wid-edit.el --- Functions for creating and using widgets.
3 ;; Copyright (C) 1996, 1997 Free Software Foundation, Inc.
5 ;; Author: Per Abrahamsen <abraham@dina.kvl.dk>
6 ;; 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 (new-glyph (if activate-p up-glyph inactive-glyph)))
484 ;; Check that the new glyph exists, and differs from the
486 (and up-glyph inactive-glyph (not (eq up-glyph inactive-glyph))
487 ;; Check if the glyph is already installed.
488 (not (eq (extent-end-glyph extent) new-glyph))
490 (set-extent-end-glyph extent new-glyph)))))
493 (defun widget-specify-inactive (widget from to)
494 "Make WIDGET inactive for user modifications."
495 (unless (widget-get widget :inactive)
496 (let ((extent (make-extent from to)))
497 ;; It is no longer necessary for the extent to be read-only, as
498 ;; the inactive editable fields now lose their keymaps.
499 (set-extent-properties
500 extent '(start-open t face widget-inactive-face
501 detachable t priority 2001 widget-inactive t))
502 (widget-put widget :inactive extent))
503 ;; Deactivate the buttons and fields within the range. In some
504 ;; cases, the fields are not yet setup at the time this function
505 ;; is called. Those fields are deactivated explicitly by
507 (map-extents 'widget-activation-widget-mapper
508 nil from to :deactivate nil 'button-or-field)
509 ;; Deactivate glyphs.
510 (map-extents 'widget-activation-glyph-mapper
511 nil from to :deactivate nil 'glyph-widget)))
513 (defun widget-specify-active (widget)
514 "Make WIDGET active for user modifications."
515 (let ((inactive (widget-get widget :inactive)))
516 (when (and inactive (not (extent-detached-p inactive)))
517 ;; Reactivate the buttons and fields covered by the extent.
518 (map-extents 'widget-activation-widget-mapper
519 inactive nil nil :activate nil 'button-or-field)
520 ;; Reactivate the glyphs.
521 (map-extents 'widget-activation-glyph-mapper
522 inactive nil nil :activate nil 'end-glyph)
523 (delete-extent inactive)
524 (widget-put widget :inactive nil))))
527 ;;; Widget Properties.
529 (defsubst widget-type (widget)
530 "Return the type of WIDGET, a symbol."
533 (when (or (not (fboundp 'widget-put))
535 (defun widget-put (widget property value)
536 "In WIDGET set PROPERTY to VALUE.
537 The value can later be retrieved with `widget-get'."
538 (setcdr widget (plist-put (cdr widget) property value))))
540 ;; Recoded in C, for efficiency:
541 (when (or (not (fboundp 'widget-get))
543 (defun widget-get (widget property)
544 "In WIDGET, get the value of PROPERTY.
545 The value could either be specified when the widget was created, or
546 later with `widget-put'."
550 (cond ((setq tmp (widget-plist-member (cdr widget) property))
551 (setq value (car (cdr tmp))
553 ((setq tmp (car widget))
554 (setq widget (get tmp 'widget-type)))
556 (setq missing nil))))
559 (defun widget-get-indirect (widget property)
560 "In WIDGET, get the value of PROPERTY.
561 If the value is a symbol, return its binding.
562 Otherwise, just return the value."
563 (let ((value (widget-get widget property)))
568 (defun widget-member (widget property)
569 "Non-nil iff there is a definition in WIDGET for PROPERTY."
570 (cond ((widget-plist-member (cdr widget) property)
573 (widget-member (get (car widget) 'widget-type) property))
576 (when (or (not (fboundp 'widget-apply))
578 ;;This is in C, so don't ###utoload
579 (defun widget-apply (widget property &rest args)
580 "Apply the value of WIDGET's PROPERTY to the widget itself.
581 ARGS are passed as extra arguments to the function."
582 (apply (widget-get widget property) widget args)))
584 (defun widget-value (widget)
585 "Extract the current value of WIDGET."
587 :value-to-external (widget-apply widget :value-get)))
589 (defun widget-value-set (widget value)
590 "Set the current value of WIDGET to VALUE."
592 :value-set (widget-apply widget
593 :value-to-internal value)))
595 (defun widget-default-get (widget)
596 "Extract the defaylt value of WIDGET."
597 (or (widget-get widget :value)
598 (widget-apply widget :default-get)))
600 (defun widget-match-inline (widget vals)
601 ;; In WIDGET, match the start of VALS.
602 (cond ((widget-get widget :inline)
603 (widget-apply widget :match-inline vals))
605 (widget-apply widget :match (car vals)))
606 (cons (list (car vals)) (cdr vals)))
609 (defun widget-apply-action (widget &optional event)
610 "Apply :action in WIDGET in response to EVENT."
611 (if (widget-apply widget :active)
612 (widget-apply widget :action event)
613 (error "Attempt to perform action on inactive widget")))
616 ;;; Helper functions.
618 ;; These are widget specific.
621 (defun widget-prompt-value (widget prompt &optional value unbound)
622 "Prompt for a value matching WIDGET, using PROMPT.
623 The current value is assumed to be VALUE, unless UNBOUND is non-nil."
624 (unless (listp widget)
625 (setq widget (list widget)))
626 (setq prompt (format "[%s] %s" (widget-type widget) prompt))
627 (setq widget (widget-convert widget))
628 (let ((answer (widget-apply widget :prompt-value prompt value unbound)))
629 (while (not (widget-apply widget :match answer))
630 (setq answer (signal 'error (list "Answer does not match type"
631 answer (widget-type widget)))))
634 (defun widget-get-sibling (widget)
635 "Get the item WIDGET is assumed to toggle.
636 This is only meaningful for radio buttons or checkboxes in a list."
637 (let* ((parent (widget-get widget :parent))
638 (children (widget-get parent :children))
642 (setq child (car children)
643 children (cdr children))
644 (when (eq (widget-get child :button) widget)
645 (throw 'child child)))
648 (defun widget-map-buttons (function &optional buffer maparg)
649 "Map FUNCTION over the buttons in BUFFER.
650 FUNCTION is called with the arguments WIDGET and MAPARG.
652 If FUNCTION returns non-nil, the walk is cancelled.
654 The arguments MAPARG, and BUFFER default to nil and (current-buffer),
656 (map-extents (lambda (extent ignore)
657 ;; If FUNCTION returns non-nil, we bail out
658 (funcall function (extent-property extent 'button) maparg))
665 (defcustom widget-glyph-directory (locate-data-directory "custom")
666 "Where widget glyphs are located.
667 If this variable is nil, widget will try to locate the directory
672 (defcustom widget-glyph-enable t
673 "If non nil, use glyphs in images when available."
677 (defcustom widget-image-file-name-suffixes
678 '((xpm ".xpm") (gif ".gif") (png ".png") (jpeg ".jpg" ".jpeg")
680 "Conversion alist from image formats to file name suffixes."
682 :type '(repeat (cons :format "%v"
683 (symbol :tag "Image Format" unknown)
684 (repeat :tag "Suffixes"
685 (string :format "%v")))))
687 ;; Don't use this, because we cannot yet distinguish between widget
688 ;; glyphs associated with user action, and actionless ones.
689 ;(defvar widget-glyph-pointer-glyph
690 ; (make-pointer-glyph [cursor-font :data "hand2"])
691 ; "Glyph to be used as the mouse pointer shape over glyphs.
692 ;Use `set-glyph-image' to change this.")
694 (defvar widget-glyph-cache nil
695 "Cache of glyphs associated with strings (files).")
697 (defun widget-glyph-find (image tag)
698 "Create a glyph corresponding to IMAGE with string TAG as fallback.
699 IMAGE can already be a glyph, or a file name sans extension (xpm,
700 xbm, gif, jpg, or png) located in `widget-glyph-directory', or
701 in one of the data directories.
702 It can also be a valid image instantiator, in which case it will be
703 used to make the glyph, with an additional TAG string fallback."
704 (cond ((not (and image widget-glyph-enable))
705 ;; We don't want to use glyphs.
707 ((and (not (console-on-window-system-p))
708 ;; We don't use glyphs on TTY consoles, although we
709 ;; could. However, glyph faces aren't yet working
710 ;; properly, and movement through glyphs is unintuitive.
711 ;; As an exception, when TAG is nil, we assume that the
712 ;; caller knows what he is doing, and that the tag is
713 ;; encoded within the glyph.
714 (not (glyphp image)))
717 ;; Already a glyph. Use it.
720 ;; A string. Look it up in the cache first...
721 (or (lax-plist-get widget-glyph-cache image)
722 ;; ...and then in the relevant directories
723 (let* ((dirlist (cons (or widget-glyph-directory
724 (locate-data-directory "custom"))
725 data-directory-list))
730 (and (valid-image-instantiator-format-p (car el))
732 widget-image-file-name-suffixes)))
733 (file (locate-file image dirlist all-suffixes)))
735 (let* ((extension (concat "." (file-name-extension file)))
736 (format (car (rassoc* extension
737 widget-image-file-name-suffixes
739 ;; We create a glyph with the file as the default image
740 ;; instantiator, and the TAG fallback
741 (let ((glyph (make-glyph `([,format :file ,file]
742 [string :data ,tag]))))
744 (laxputf widget-glyph-cache image glyph)
747 ((valid-instantiator-p image 'image)
748 ;; A valid image instantiator (e.g. [gif :file "somefile"] etc.)
749 (make-glyph `(,image [string :data ,tag])))
754 (defun widget-glyph-insert (widget tag image &optional down inactive)
755 "In WIDGET, insert the text TAG or, if supported, IMAGE.
756 IMAGE should either be a glyph, an image instantiator, an image file
757 name sans extension (xpm, xbm, gif, jpg, or png) located in
758 `widget-glyph-directory', or anything else allowed by
761 If IMAGE is a list, it will be taken as a list of (UP DOWN INACTIVE)
762 glyphs. The down and inactive glyphs are shown when glyph is pressed
763 or inactive, respectively.
765 The optional DOWN and INACTIVE arguments are deprecated, and exist
766 only because of compatibility."
767 ;; Convert between IMAGE being a list, etc. Must use `psetq',
768 ;; because otherwise change to `image' screws up the rest.
769 (psetq image (or (and (consp image)
772 down (or (and (consp image)
775 inactive (or (and (consp image)
778 (let ((glyph (widget-glyph-find image tag)))
780 (widget-glyph-insert-glyph widget glyph
781 (widget-glyph-find down tag)
782 (widget-glyph-find inactive tag))
786 (defun widget-glyph-insert-glyph (widget glyph &optional down inactive)
787 "In WIDGET, insert GLYPH.
788 If optional arguments DOWN and INACTIVE are given, they should be
789 glyphs used when the widget is pushed and inactive, respectively."
791 (let ((extent (make-extent (point) (1- (point))))
792 (help-echo (and widget (widget-get widget :help-echo)))
793 (map (and widget (widget-get widget :button-keymap))))
794 (set-extent-property extent 'glyph-widget widget)
795 ;; It would be fun if we could make this extent atomic, so it
796 ;; doesn't mess with cursor motion. But atomic-extents library is
797 ;; currently a mess, so I'd rather not use it.
798 (set-extent-property extent 'invisible t)
799 (set-extent-property extent 'start-open t)
800 (set-extent-property extent 'end-open t)
801 (set-extent-property extent 'keymap map)
802 ;;(set-extent-property extent 'pointer widget-glyph-pointer-glyph)
803 (set-extent-end-glyph extent glyph)
804 (unless (or (stringp help-echo) (null help-echo))
805 (setq help-echo 'widget-mouse-help))
807 (widget-handle-help-echo extent help-echo)))
809 (widget-put widget :glyph-up glyph)
810 (when down (widget-put widget :glyph-down down))
811 (when inactive (widget-put widget :glyph-inactive inactive))))
816 (defgroup widget-button nil
817 "The look of various kinds of buttons."
820 (defcustom widget-button-prefix ""
821 "String used as prefix for buttons."
823 :group 'widget-button)
825 (defcustom widget-button-suffix ""
826 "String used as suffix for buttons."
828 :group 'widget-button)
831 ;;; Creating Widgets.
834 (defun widget-create (type &rest args)
835 "Create widget of TYPE.
836 The optional ARGS are additional keyword arguments."
837 (let ((widget (apply 'widget-convert type args)))
838 (widget-apply widget :create)
841 (defun widget-create-child-and-convert (parent type &rest args)
842 "As part of the widget PARENT, create a child widget TYPE.
843 The child is converted, using the keyword arguments ARGS."
844 (let ((widget (apply 'widget-convert type args)))
845 (widget-put widget :parent parent)
846 (unless (widget-get widget :indent)
847 (widget-put widget :indent (+ (or (widget-get parent :indent) 0)
848 (or (widget-get widget :extra-offset) 0)
849 (widget-get parent :offset))))
850 (widget-apply widget :create)
853 (defun widget-create-child (parent type)
854 "Create widget of TYPE."
855 (let ((widget (copy-sequence type)))
856 (widget-put widget :parent parent)
857 (unless (widget-get widget :indent)
858 (widget-put widget :indent (+ (or (widget-get parent :indent) 0)
859 (or (widget-get widget :extra-offset) 0)
860 (widget-get parent :offset))))
861 (widget-apply widget :create)
864 (defun widget-create-child-value (parent type value)
865 "Create widget of TYPE with value VALUE."
866 (let ((widget (copy-sequence type)))
867 (widget-put widget :value (widget-apply widget :value-to-internal value))
868 (widget-put widget :parent parent)
869 (unless (widget-get widget :indent)
870 (widget-put widget :indent (+ (or (widget-get parent :indent) 0)
871 (or (widget-get widget :extra-offset) 0)
872 (widget-get parent :offset))))
873 (widget-apply widget :create)
877 (defun widget-delete (widget)
879 (widget-apply widget :delete))
881 (defun widget-convert (type &rest args)
882 "Convert TYPE to a widget without inserting it in the buffer.
883 The optional ARGS are additional keyword arguments."
884 ;; Don't touch the type.
885 (let* ((widget (if (symbolp type)
887 (copy-sequence type)))
890 ;; First set the :args keyword.
891 (while (cdr current) ;Look in the type.
892 (let ((next (car (cdr current))))
893 (if (and (symbolp next) (eq (aref (symbol-name next) 0) ?:))
894 (setq current (cdr (cdr current)))
895 (setcdr current (list :args (cdr current)))
896 (setq current nil))))
897 (while args ;Look in the args.
898 (let ((next (nth 0 args)))
899 (if (and (symbolp next) (eq (aref (symbol-name next) 0) ?:))
900 (setq args (nthcdr 2 args))
901 (widget-put widget :args args)
903 ;; Then Convert the widget.
906 (let ((convert-widget (plist-get (cdr type) :convert-widget)))
908 (setq widget (funcall convert-widget widget))))
909 (setq type (get (car type) 'widget-type)))
910 ;; Finally set the keyword args.
912 (let ((next (nth 0 keys)))
913 (if (and (symbolp next) (eq (aref (symbol-name next) 0) ?:))
915 (widget-put widget next (nth 1 keys))
916 (setq keys (nthcdr 2 keys)))
918 ;; Convert the :value to internal format.
919 (if (widget-member widget :value)
920 (let ((value (widget-get widget :value)))
922 :value (widget-apply widget :value-to-internal value))))
923 ;; Return the newly created widget.
926 (defun widget-insert (&rest args)
927 "Call `insert' with ARGS and make the text read only."
928 (let ((inhibit-read-only t)
929 before-change-functions
930 after-change-functions)
931 (apply 'insert args)))
933 (defun widget-convert-text (type from to
934 &optional button-from button-to
936 "Return a widget of type TYPE with endpoint FROM TO.
937 Optional ARGS are extra keyword arguments for TYPE.
938 and TO will be used as the widgets end points. If optional arguments
939 BUTTON-FROM and BUTTON-TO are given, these will be used as the widgets
941 Optional ARGS are extra keyword arguments for TYPE."
942 (let ((widget (apply 'widget-convert type :delete 'widget-leave-text args))
943 (from (copy-marker from))
944 (to (copy-marker to)))
945 (set-marker-insertion-type from t)
946 (set-marker-insertion-type to nil)
947 (widget-put widget :from from)
948 (widget-put widget :to to)
950 (widget-specify-button widget button-from button-to))
953 (defun widget-convert-button (type from to &rest args)
954 "Return a widget of type TYPE with endpoint FROM TO.
955 Optional ARGS are extra keyword arguments for TYPE.
956 No text will be inserted to the buffer, instead the text between FROM
957 and TO will be used as the widgets end points, as well as the widgets
959 (apply 'widget-convert-text type from to from to args))
961 (defun widget-leave-text (widget)
962 "Remove markers and extents from WIDGET and its children."
963 (let ((from (widget-get widget :from))
964 (to (widget-get widget :to))
965 (button (widget-get widget :button-extent))
966 (sample (widget-get widget :sample-extent))
967 (doc (widget-get widget :doc-extent))
968 (field (widget-get widget :field-extent))
969 (children (widget-get widget :children)))
970 (set-marker from nil)
972 ;; Maybe we should delete the extents here? As this code doesn't
973 ;; remove them from widget structures, maybe it's safer to just
974 ;; detach them. That's what `delete-overlay' did.
976 (detach-extent button))
978 (detach-extent sample))
982 (detach-extent field))
983 (mapc 'widget-leave-text children)))
986 ;;; Keymap and Commands.
988 (defvar widget-keymap nil
989 "Keymap containing useful binding for buffers containing widgets.
990 Recommended as a parent keymap for modes using widgets.")
992 (unless widget-keymap
993 (setq widget-keymap (make-sparse-keymap))
994 (define-key widget-keymap [tab] 'widget-forward)
995 (define-key widget-keymap [(shift tab)] 'widget-backward)
996 (define-key widget-keymap [(meta tab)] 'widget-backward)
997 (define-key widget-keymap [backtab] 'widget-backward))
999 (defvar widget-global-map global-map
1000 "Keymap used for events the widget does not handle themselves.")
1001 (make-variable-buffer-local 'widget-global-map)
1003 (defvar widget-field-keymap nil
1004 "Keymap used inside an editable field.")
1006 (unless widget-field-keymap
1007 (setq widget-field-keymap (make-sparse-keymap))
1008 (set-keymap-parents widget-field-keymap global-map)
1009 (define-key widget-field-keymap "\C-k" 'widget-kill-line)
1010 (define-key widget-field-keymap [(meta tab)] 'widget-complete)
1011 (define-key widget-field-keymap [tab] 'widget-forward)
1012 (define-key widget-field-keymap [(shift tab)] 'widget-backward)
1013 (define-key widget-field-keymap "\C-m" 'widget-field-activate)
1014 (define-key widget-field-keymap "\C-a" 'widget-beginning-of-line)
1015 (define-key widget-field-keymap "\C-e" 'widget-end-of-line)
1016 (define-key widget-field-keymap "\C-t" 'widget-transpose-chars))
1018 (defvar widget-text-keymap nil
1019 "Keymap used inside a text field.")
1021 (unless widget-text-keymap
1022 (setq widget-text-keymap (make-sparse-keymap))
1023 (set-keymap-parents widget-field-keymap global-map)
1024 (define-key widget-text-keymap "\C-a" 'widget-beginning-of-line)
1025 (define-key widget-text-keymap "\C-e" 'widget-end-of-line)
1026 (define-key widget-text-keymap "\C-t" 'widget-transpose-chars))
1028 (defvar widget-button-keymap nil
1029 "Keymap used inside a button.")
1031 (unless widget-button-keymap
1032 (setq widget-button-keymap (make-sparse-keymap))
1033 (set-keymap-parents widget-button-keymap widget-keymap)
1034 (define-key widget-button-keymap "\C-m" 'widget-button-press)
1035 (define-key widget-button-keymap [button2] 'widget-button-click)
1036 ;; Ideally, button3 within a button should invoke a button-specific
1038 (define-key widget-button-keymap [button3] 'widget-button-click)
1040 (define-key widget-button-keymap [button1] 'widget-button1-click))
1043 (defun widget-field-activate (pos &optional event)
1044 "Invoke the ediable field at point."
1046 (let ((field (widget-field-find pos)))
1048 (widget-apply-action field event)
1050 (lookup-key widget-global-map (this-command-keys))))))
1052 (defface widget-button-pressed-face
1054 (:foreground "red"))
1056 (:bold t :underline t)))
1057 "Face used for pressed buttons."
1058 :group 'widget-faces)
1060 (defun widget-event-point (event)
1061 "Character position of the mouse event, or nil."
1062 (and (mouse-event-p event)
1063 (event-point event)))
1065 (defun widget-button-click (event)
1066 "Invoke button below mouse pointer."
1068 (with-current-buffer (event-buffer event)
1069 (cond ((event-glyph event)
1070 (widget-glyph-click event))
1071 ((widget-event-point event)
1072 (let* ((pos (widget-event-point event))
1073 (button (get-char-property pos 'button)))
1075 (let* ((extent (widget-get button :button-extent))
1076 (face (extent-property extent 'face))
1077 (mouse-face (extent-property extent 'mouse-face))
1078 (help-echo (extent-property extent 'help-echo)))
1081 ;; Merge relevant faces, and make the result mouse-face.
1082 (let ((merge `(widget-button-pressed-face ,mouse-face)))
1083 (nconc merge (if (listp face)
1085 (setq merge (delete-if-not 'find-face merge))
1086 (set-extent-property extent 'mouse-face merge))
1087 (unless (widget-apply button :mouse-down-action event)
1088 ;; Wait for button release.
1089 (while (not (button-release-event-p
1090 (setq event (next-event))))
1091 (dispatch-event event)))
1092 ;; Disallow mouse-face and help-echo.
1093 (set-extent-property extent 'mouse-face nil)
1094 (set-extent-property extent 'help-echo nil)
1095 (setq pos (widget-event-point event))
1096 (unless (eq (current-buffer) (extent-object extent))
1097 ;; Barf if dispatch-event tripped us by
1099 (error "Buffer changed during mouse motion"))
1100 ;; Do the associated action.
1101 (when (and pos (extent-in-region-p extent pos pos))
1102 (widget-apply-action button event)))
1103 ;; Unwinding: fully release the button.
1104 (set-extent-property extent 'mouse-face mouse-face)
1105 (set-extent-property extent 'help-echo help-echo)))
1106 ;; This should not happen!
1107 (error "`widget-button-click' called outside button"))))
1109 (message "You clicked somewhere weird")))))
1111 (defun widget-button1-click (event)
1112 "Invoke glyph below mouse pointer."
1114 (if (event-glyph event)
1115 (widget-glyph-click event)
1116 ;; Should somehow avoid this.
1117 (let ((command (lookup-key widget-global-map (this-command-keys))))
1118 (and (commandp command)
1119 (call-interactively command)))))
1121 (defun widget-glyph-click (event)
1122 "Handle click on a glyph."
1123 (let* ((glyph (event-glyph event))
1124 (extent (event-glyph-extent event))
1125 (widget (extent-property extent 'glyph-widget))
1126 (down-glyph (or (and widget (widget-get widget :glyph-down)) glyph))
1127 (up-glyph (or (and widget (widget-get widget :glyph-up)) glyph))
1129 (unless (widget-apply widget :active)
1130 (error "This widget is inactive"))
1131 (let ((current-glyph 'down))
1132 ;; We always know what glyph is drawn currently, to avoid
1133 ;; unnecessary extent changes. Is this any noticeable gain?
1137 (set-extent-end-glyph extent down-glyph)
1138 ;; Redisplay (shouldn't be needed, but...)
1140 (unless (widget-apply widget :mouse-down-action event)
1141 ;; Wait for the release.
1142 (while (not (button-release-event-p last))
1143 (unless (button-press-event-p last)
1144 (dispatch-event last))
1145 (when (motion-event-p last)
1146 ;; Update glyphs on mouse motion.
1147 (if (eq extent (event-glyph-extent last))
1148 (unless (eq current-glyph 'down)
1149 (set-extent-end-glyph extent down-glyph)
1150 (setq current-glyph 'down))
1151 (unless (eq current-glyph 'up)
1152 (set-extent-end-glyph extent up-glyph)
1153 (setq current-glyph 'up))))
1154 (setq last (next-event event))))
1155 (unless (eq (current-buffer) (extent-object extent))
1156 ;; Barf if dispatch-event tripped us by changing buffer.
1157 (error "Buffer changed during mouse motion"))
1158 ;; Apply widget action.
1159 (when (eq extent (event-glyph-extent last))
1160 (let ((widget (extent-property (event-glyph-extent event)
1162 (cond ((null widget)
1163 (message "You clicked on a glyph"))
1164 ((not (widget-apply widget :active))
1165 (error "This glyph is inactive"))
1167 (widget-apply-action widget event))))))
1168 ;; Release the glyph.
1169 (and (eq current-glyph 'down)
1170 ;; The extent might have been detached or deleted
1171 (extent-live-p extent)
1172 (not (extent-detached-p extent))
1173 (set-extent-end-glyph extent up-glyph))))))
1175 (defun widget-button-press (pos &optional event)
1176 "Invoke button at POS."
1178 (let ((button (get-char-property pos 'button)))
1180 (widget-apply-action button event)
1181 (let ((command (lookup-key widget-global-map (this-command-keys))))
1182 (when (commandp command)
1183 (call-interactively command))))))
1185 (defun widget-tabable-at (&optional pos last-tab backwardp)
1186 "Return the tabable widget at POS, or nil.
1187 POS defaults to the value of (point)."
1190 (let ((widget (widget-at pos)))
1192 (let ((order (widget-get widget :tab-order)))
1194 (if last-tab (and (= order (if backwardp
1198 (and (> order 0) widget))
1202 ;; Return the button or field extent at point.
1203 (defun widget-button-or-field-extent (pos)
1204 (or (and (get-char-property pos 'button)
1205 (widget-get (get-char-property pos 'button)
1207 (and (get-char-property pos 'field)
1208 (widget-get (get-char-property pos 'field)
1211 (defun widget-next-button-or-field (pos)
1212 "Find the next button, or field, and return its start position, or nil.
1213 Internal function, don't use it outside `wid-edit'."
1214 (let* ((at-point (widget-button-or-field-extent pos))
1215 (extent (map-extents
1216 (lambda (ext ignore)
1218 nil (if at-point (extent-end-position at-point) pos)
1219 nil nil 'start-open 'button-or-field)))
1221 (extent-start-position extent))))
1223 ;; This is too slow in buffers with many buttons (W3).
1224 (defun widget-previous-button-or-field (pos)
1225 "Find the previous 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))
1230 (lambda (ext ignore)
1231 (if (eq ext at-point)
1232 ;; We reached the extent we were on originally
1233 (if (= pos (extent-start-position at-point))
1235 (setq previous-extent at-point))
1236 (setq previous-extent ext)
1238 nil nil pos nil 'start-open 'button-or-field)
1239 (and previous-extent
1240 (extent-start-position previous-extent))))
1242 (defun widget-move (arg)
1243 "Move point to the ARG next field or button.
1244 ARG may be negative to move backward."
1245 (let ((opoint (point)) (wrapped 0)
1246 (last-tab (widget-get (widget-at (point)) :tab-order))
1248 ;; Movement backward
1250 (setq nextpos (widget-previous-button-or-field (point)))
1254 (when (and (not (get-char-property nextpos 'widget-inactive))
1255 (widget-tabable-at nil last-tab t))
1258 last-tab (widget-get (widget-at (point))
1260 (if (and (not found) (> wrapped 1))
1263 (goto-char (point-max))
1267 (setq nextpos (widget-next-button-or-field (point)))
1271 (when (and (not (get-char-property nextpos 'widget-inactive))
1272 (widget-tabable-at nil last-tab))
1275 last-tab (widget-get (widget-at (point))
1277 (if (and (not found) (> wrapped 1))
1280 (goto-char (point-min))
1284 (widget-echo-help (point))
1285 (run-hooks 'widget-move-hook))))
1287 (defun widget-forward (arg)
1288 "Move point to the next field or button.
1289 With optional ARG, move across that many fields."
1291 (run-hooks 'widget-forward-hook)
1294 (defun widget-backward (arg)
1295 "Move point to the previous field or button.
1296 With optional ARG, move across that many fields."
1298 (run-hooks 'widget-backward-hook)
1299 (widget-move (- arg)))
1301 (defun widget-beginning-of-line ()
1302 "Go to beginning of field or beginning of line, whichever is first."
1304 (let* ((field (widget-field-find (point)))
1305 (start (and field (widget-field-start field))))
1306 (if (and start (not (eq start (point))))
1308 (call-interactively 'beginning-of-line))))
1310 (defun widget-end-of-line ()
1311 "Go to end of field or end of line, whichever is first."
1313 (let* ((field (widget-field-find (point)))
1314 (end (and field (widget-field-end field))))
1315 (if (and end (not (eq end (point))))
1317 (call-interactively 'end-of-line))))
1319 (defun widget-kill-line ()
1320 "Kill to end of field or end of line, whichever is first."
1322 (let* ((field (widget-field-find (point)))
1323 (newline (save-excursion (forward-line 1) (point)))
1324 (end (and field (widget-field-end field))))
1325 (if (and field (> newline end))
1326 (kill-region (point) end)
1327 (call-interactively 'kill-line))))
1329 (defun widget-transpose-chars (arg)
1330 "Like `transpose-chars', but works correctly at end of widget."
1332 (let* ((field (widget-field-find (point)))
1333 (start (and field (widget-field-start field)))
1334 (end (and field (widget-field-end field)))
1335 (last-non-space (and start end
1338 (skip-chars-backward " \t\n" start)
1340 (cond ((and last-non-space
1341 (or (= last-non-space start)
1342 (= last-non-space (1+ start))))
1343 ;; empty or one-character field
1346 ;; at the beginning of the field -- we would get an error here.
1347 (error "Cannot transpose at beginning of field"))
1349 (when (and (null arg)
1350 (= last-non-space (point)))
1352 (transpose-chars arg)))))
1354 (defcustom widget-complete-field (lookup-key global-map "\M-\t")
1355 "Default function to call for completion inside fields."
1356 :options '(ispell-complete-word complete-tag lisp-complete-symbol)
1360 (defun widget-complete ()
1361 "Complete content of editable field from point.
1362 When not inside a field, move to the previous button or field."
1364 ;; Somehow, this should make pressing M-TAB twice scroll the
1365 ;; completions window.
1366 (let ((field (widget-field-find (point))))
1368 (widget-apply field :complete)
1369 (error "Not in an editable field"))))
1372 ;;; Setting up the buffer.
1374 (defvar widget-field-new nil)
1375 ;; List of all newly created editable fields in the buffer.
1376 (make-variable-buffer-local 'widget-field-new)
1378 (defvar widget-field-list nil)
1379 ;; List of all editable fields in the buffer.
1380 (make-variable-buffer-local 'widget-field-list)
1382 (defun widget-setup ()
1383 "Setup current buffer so editing string widgets works."
1384 (let ((inhibit-read-only t)
1385 (after-change-functions nil)
1386 before-change-functions
1388 (while widget-field-new
1389 (setq field (car widget-field-new)
1390 widget-field-new (cdr widget-field-new)
1391 widget-field-list (cons field widget-field-list))
1392 (let ((from (car (widget-get field :field-extent)))
1393 (to (cdr (widget-get field :field-extent))))
1394 (widget-specify-field field
1395 (marker-position from) (marker-position to))
1396 (set-marker from nil)
1397 (set-marker to nil))
1398 ;; If the field is placed within the inactive zone, deactivate it.
1399 (let ((extent (widget-get field :field-extent)))
1400 (when (get-char-property (extent-start-position extent)
1402 (widget-activation-widget-mapper extent :deactivate)))))
1404 (widget-add-change))
1406 (defvar widget-field-last nil)
1407 ;; Last field containing point.
1408 (make-variable-buffer-local 'widget-field-last)
1410 (defvar widget-field-was nil)
1411 ;; The widget data before the change.
1412 (make-variable-buffer-local 'widget-field-was)
1414 (defun widget-field-buffer (widget)
1415 "Return the start of WIDGET's editing field."
1416 (let ((extent (widget-get widget :field-extent)))
1417 (and extent (extent-object extent))))
1419 (defun widget-field-start (widget)
1420 "Return the start of WIDGET's editing field."
1421 (let ((extent (widget-get widget :field-extent)))
1422 (and extent (extent-start-position extent))))
1424 (defun widget-field-end (widget)
1425 "Return the end of WIDGET's editing field."
1426 (let ((extent (widget-get widget :field-extent)))
1427 ;; Don't subtract one if local-map works at the end of the extent.
1428 (and extent (if (or widget-field-add-space
1429 (null (widget-get widget :size)))
1430 (1- (extent-end-position extent))
1431 (extent-end-position extent)))))
1433 (defun widget-field-find (pos)
1434 "Return the field at POS.
1435 Unlike (get-char-property POS 'field) this, works with empty fields too."
1436 (let ((field-extent (map-extents (lambda (extent ignore)
1438 nil pos pos nil nil 'field)))
1440 (extent-property field-extent 'field))))
1442 ;; Old version, without `map-extents'.
1443 ;(defun widget-field-find (pos)
1444 ; (let ((fields widget-field-list)
1447 ; (setq field (car fields)
1448 ; fields (cdr fields))
1449 ; (let ((start (widget-field-start field))
1450 ; (end (widget-field-end field)))
1451 ; (when (and (<= start pos) (<= pos end))
1453 ; (debug "Overlapping fields"))
1454 ; (setq found field))))
1457 (defun widget-before-change (from to)
1458 ;; Barf if the text changed is outside the editable fields.
1459 (unless inhibit-read-only
1460 (let ((from-field (widget-field-find from))
1461 (to-field (widget-field-find to)))
1462 (cond ((or (null from-field)
1464 ;; Either end of change is not within a field.
1465 (add-hook 'post-command-hook 'widget-add-change nil t)
1466 (error "Attempt to change text outside editable field"))
1467 ((not (eq from-field to-field))
1468 ;; The change begins in one fields, and ends in another one.
1469 (add-hook 'post-command-hook 'widget-add-change nil t)
1470 (error "Change should be restricted to a single field"))
1471 ((or (and from-field
1472 (get-char-property from 'widget-inactive))
1474 (get-char-property to 'widget-inactive)))
1475 ;; Trying to change an inactive editable field.
1476 (add-hook 'post-command-hook 'widget-add-change nil t)
1477 (error "Attempt to change an inactive field"))
1478 (widget-field-use-before-change
1479 ;; #### Bletch! This loses because XEmacs get confused
1480 ;; if before-change-functions change the contents of
1481 ;; buffer before from/to.
1483 (widget-apply from-field :notify from-field)
1484 (error (debug "Before Change"))))))))
1486 (defun widget-add-change ()
1487 (make-local-hook 'post-command-hook)
1488 (remove-hook 'post-command-hook 'widget-add-change t)
1489 (make-local-hook 'before-change-functions)
1490 (add-hook 'before-change-functions 'widget-before-change nil t)
1491 (make-local-hook 'after-change-functions)
1492 (add-hook 'after-change-functions 'widget-after-change nil t))
1494 (defun widget-after-change (from to old)
1495 ;; Adjust field size and text properties.
1497 ;; Also, notify the widgets (so, for example, a variable changes its
1498 ;; state to `modified'. when it is being edited.)
1500 (let ((field (widget-field-find from))
1501 (other (widget-field-find to)))
1503 (unless (eq field other)
1504 (debug "Change in different fields"))
1505 (let ((size (widget-get field :size)))
1507 (let ((begin (widget-field-start field))
1508 (end (widget-field-end field)))
1509 (cond ((< (- end begin) size)
1513 (insert-char ?\ (- (+ begin size) end))))
1514 ((> (- end begin) size)
1515 ;; Field too large and
1516 (if (or (< (point) (+ begin size))
1518 ;; Point is outside extra space.
1519 (setq begin (+ begin size))
1520 ;; Point is within the extra space.
1521 (setq begin (point)))
1524 (while (and (eq (preceding-char) ?\ )
1526 (delete-backward-char 1)))))))
1527 (widget-specify-secret field))
1528 (widget-apply field :notify field)))
1529 (error (debug "After Change"))))
1532 ;;; Widget Functions
1534 ;; These functions are used in the definition of multiple widgets.
1536 (defun widget-parent-action (widget &optional event)
1537 "Tell :parent of WIDGET to handle the :action.
1538 Optional EVENT is the event that triggered the action."
1539 (widget-apply (widget-get widget :parent) :action event))
1541 (defun widget-children-value-delete (widget)
1542 "Delete all :children and :buttons in WIDGET."
1543 (mapc 'widget-delete (widget-get widget :children))
1544 (widget-put widget :children nil)
1545 (mapc 'widget-delete (widget-get widget :buttons))
1546 (widget-put widget :buttons nil))
1548 (defun widget-children-validate (widget)
1549 "All the :children must be valid."
1550 (let ((children (widget-get widget :children))
1552 (while (and children (not found))
1553 (setq child (car children)
1554 children (cdr children)
1555 found (widget-apply child :validate)))
1558 (defun widget-types-convert-widget (widget)
1559 "Convert :args as widget types in WIDGET."
1560 (widget-put widget :args (mapcar 'widget-convert (widget-get widget :args)))
1563 (defun widget-value-convert-widget (widget)
1564 "Initialize :value from :args in WIDGET."
1565 (let ((args (widget-get widget :args)))
1567 (widget-put widget :value (car args))
1568 ;; Don't convert :value here, as this is done in `widget-convert'.
1569 ;; (widget-put widget :value (widget-apply widget
1570 ;; :value-to-internal (car args)))
1571 (widget-put widget :args nil)))
1574 (defun widget-value-value-get (widget)
1575 "Return the :value property of WIDGET."
1576 (widget-get widget :value))
1578 ;;; The `default' Widget.
1580 (define-widget 'default nil
1581 "Basic widget other widgets are derived from."
1582 :value-to-internal (lambda (widget value) value)
1583 :value-to-external (lambda (widget value) value)
1584 :button-prefix 'widget-button-prefix
1585 :button-suffix 'widget-button-suffix
1586 :complete 'widget-default-complete
1587 :create 'widget-default-create
1590 :format-handler 'widget-default-format-handler
1591 :button-face-get 'widget-default-button-face-get
1592 :sample-face-get 'widget-default-sample-face-get
1593 :button-keymap widget-button-keymap
1594 :delete 'widget-default-delete
1595 :value-set 'widget-default-value-set
1596 :value-inline 'widget-default-value-inline
1597 :default-get 'widget-default-default-get
1598 :menu-tag-get 'widget-default-menu-tag-get
1599 :validate (lambda (widget) nil)
1600 :active 'widget-default-active
1601 :activate 'widget-specify-active
1602 :deactivate 'widget-default-deactivate
1603 :mouse-down-action (lambda (widget event) nil)
1604 :action 'widget-default-action
1605 :notify 'widget-default-notify
1606 :prompt-value 'widget-default-prompt-value)
1608 (defun widget-default-complete (widget)
1609 "Call the value of the :complete-function property of WIDGET.
1610 If that does not exists, call the value of `widget-complete-field'."
1611 (let ((fun (widget-get widget :complete-function)))
1612 (call-interactively (or fun widget-complete-field))))
1614 (defun widget-default-create (widget)
1615 "Create WIDGET at point in the current buffer."
1616 (widget-specify-insert
1617 (let ((from (point))
1618 button-begin button-end button-glyph
1619 sample-begin sample-end
1622 (insert (widget-get widget :format))
1624 ;; Parse escapes in format. Coding this in C would speed up
1626 (while (re-search-forward "%\\(.\\)" nil t)
1627 (let ((escape (aref (match-string 1) 0)))
1628 (replace-match "" t t)
1629 (cond ((eq escape ?%)
1632 (setq button-begin (point-marker))
1633 (set-marker-insertion-type button-begin nil))
1635 (setq button-end (point-marker))
1636 (set-marker-insertion-type button-end nil))
1638 (setq sample-begin (point)))
1640 (setq sample-end (point)))
1642 (when (widget-get widget :indent)
1644 (insert-char ?\ (widget-get widget :indent))))
1646 (let* ((tag (widget-get widget :tag))
1647 (glyph (widget-get widget :tag-glyph)))
1650 (widget-glyph-insert
1651 widget (or tag "Image") glyph)))
1655 (let ((standard-output (current-buffer)))
1656 (princ (widget-get widget :value)))))))
1658 (let ((doc (widget-get widget :doc)))
1660 (setq doc-begin (point))
1662 (while (eq (preceding-char) ?\n)
1663 (delete-backward-char 1))
1665 (setq doc-end (point)))))
1667 (if (and button-begin (not button-end))
1668 (widget-apply widget :value-create)
1669 (setq value-pos (point-marker))))
1671 (widget-apply widget :format-handler escape)))))
1672 ;; Specify button, sample, and doc, and insert value.
1673 (when (and button-begin button-end)
1674 (unless button-glyph
1675 (goto-char button-begin)
1676 (insert (widget-get-indirect widget :button-prefix))
1677 (goto-char button-end)
1678 (set-marker-insertion-type button-end t)
1679 (insert (widget-get-indirect widget :button-suffix)))
1680 (widget-specify-button widget button-begin button-end)
1681 ;; Is this necessary?
1682 (set-marker button-begin nil)
1683 (set-marker button-end nil))
1684 (and sample-begin sample-end
1685 (widget-specify-sample widget sample-begin sample-end))
1686 (and doc-begin doc-end
1687 (widget-specify-doc widget doc-begin doc-end))
1689 (goto-char value-pos)
1690 (widget-apply widget :value-create)))
1691 (let ((from (point-min-marker))
1692 (to (point-max-marker)))
1693 (set-marker-insertion-type from t)
1694 (set-marker-insertion-type to nil)
1695 (widget-put widget :from from)
1696 (widget-put widget :to to)))
1697 (widget-clear-undo))
1699 (defun widget-default-format-handler (widget escape)
1700 ;; We recognize the %h escape by default.
1701 (let* ((buttons (widget-get widget :buttons)))
1702 (cond ((eq escape ?h)
1703 (let* ((doc-property (widget-get widget :documentation-property))
1704 (doc-try (cond ((widget-get widget :doc))
1705 ((symbolp doc-property)
1706 (documentation-property
1707 (widget-get widget :value)
1710 (funcall doc-property
1711 (widget-get widget :value)))))
1712 (doc-text (and (stringp doc-try)
1713 (> (length doc-try) 1)
1715 (doc-indent (widget-get widget :documentation-indent)))
1717 (and (eq (preceding-char) ?\n)
1718 (widget-get widget :indent)
1719 (insert-char ?\ (widget-get widget :indent)))
1720 ;; The `*' in the beginning is redundant.
1721 (when (eq (aref doc-text 0) ?*)
1722 (setq doc-text (substring doc-text 1)))
1723 ;; Get rid of trailing newlines.
1724 (when (string-match "\n+\\'" doc-text)
1725 (setq doc-text (substring doc-text 0 (match-beginning 0))))
1726 (push (widget-create-child-and-convert
1727 widget 'documentation-string
1728 :indent (cond ((numberp doc-indent)
1736 (signal 'error (list "Unknown escape" escape))))
1737 (widget-put widget :buttons buttons)))
1739 (defun widget-default-button-face-get (widget)
1740 ;; Use :button-face or widget-button-face
1741 (or (widget-get widget :button-face)
1742 (let ((parent (widget-get widget :parent)))
1744 (widget-apply parent :button-face-get)
1745 widget-button-face))))
1747 (defun widget-default-sample-face-get (widget)
1748 ;; Use :sample-face.
1749 (widget-get widget :sample-face))
1751 (defun widget-default-delete (widget)
1752 ;; Remove widget from the buffer.
1753 (let ((from (widget-get widget :from))
1754 (to (widget-get widget :to))
1755 (inactive-extent (widget-get widget :inactive))
1756 (button-extent (widget-get widget :button-extent))
1757 (sample-extent (widget-get widget :sample-extent))
1758 (doc-extent (widget-get widget :doc-extent))
1759 before-change-functions
1760 after-change-functions
1761 (inhibit-read-only t))
1762 (widget-apply widget :value-delete)
1763 (when inactive-extent
1764 (detach-extent inactive-extent))
1766 (detach-extent button-extent))
1768 (detach-extent sample-extent))
1770 (detach-extent doc-extent))
1772 ;; Kludge: this doesn't need to be true for empty formats.
1773 (delete-region from to))
1774 (set-marker from nil)
1775 (set-marker to nil))
1776 (widget-clear-undo))
1778 (defun widget-default-value-set (widget value)
1779 ;; Recreate widget with new value.
1780 (let* ((old-pos (point))
1781 (from (copy-marker (widget-get widget :from)))
1782 (to (copy-marker (widget-get widget :to)))
1783 (offset (if (and (<= from old-pos) (<= old-pos to))
1784 (if (>= old-pos (1- to))
1786 (- old-pos from)))))
1787 ;;??? Bug: this ought to insert the new value before deleting the old one,
1788 ;; so that markers on either side of the value automatically
1789 ;; stay on the same side. -- rms.
1791 (goto-char (widget-get widget :from))
1792 (widget-apply widget :delete)
1793 (widget-put widget :value value)
1794 (widget-apply widget :create))
1797 (goto-char (+ (widget-get widget :to) offset 1))
1798 (goto-char (min (+ from offset) (1- (widget-get widget :to))))))))
1800 (defun widget-default-value-inline (widget)
1801 ;; Wrap value in a list unless it is inline.
1802 (if (widget-get widget :inline)
1803 (widget-value widget)
1804 (list (widget-value widget))))
1806 (defun widget-default-default-get (widget)
1808 (widget-get widget :value))
1810 (defun widget-default-menu-tag-get (widget)
1811 ;; Use tag or value for menus.
1812 (or (widget-get widget :menu-tag)
1813 (widget-get widget :tag)
1814 (widget-princ-to-string (widget-get widget :value))))
1816 (defun widget-default-active (widget)
1817 "Return t iff this widget active (user modifiable)."
1818 (and (not (widget-get widget :inactive))
1819 (let ((parent (widget-get widget :parent)))
1821 (widget-apply parent :active)))))
1823 (defun widget-default-deactivate (widget)
1824 "Make WIDGET inactive for user modifications."
1825 (widget-specify-inactive widget
1826 (widget-get widget :from)
1827 (widget-get widget :to)))
1829 (defun widget-default-action (widget &optional event)
1830 ;; Notify the parent when a widget change
1831 (let ((parent (widget-get widget :parent)))
1833 (widget-apply parent :notify widget event))))
1835 (defun widget-default-notify (widget child &optional event)
1836 ;; Pass notification to parent.
1837 (widget-default-action widget event))
1839 (defun widget-default-prompt-value (widget prompt value unbound)
1840 ;; Read an arbitrary value. Stolen from `set-variable'.
1841 ;; (let ((initial (if unbound
1843 ;; ;; It would be nice if we could do a `(cons val 1)' here.
1844 ;; (prin1-to-string (custom-quote value))))))
1845 (eval-minibuffer prompt ))
1847 ;;; The `item' Widget.
1849 (define-widget 'item 'default
1850 "Constant items for inclusion in other widgets."
1851 :convert-widget 'widget-value-convert-widget
1852 :value-create 'widget-item-value-create
1853 :value-delete 'ignore
1854 :value-get 'widget-value-value-get
1855 :match 'widget-item-match
1856 :match-inline 'widget-item-match-inline
1857 :action 'widget-item-action
1860 (defun widget-item-value-create (widget)
1861 ;; Insert the printed representation of the value.
1862 (let ((standard-output (current-buffer)))
1863 (princ (widget-get widget :value))))
1865 (defun widget-item-match (widget value)
1866 ;; Match if the value is the same.
1867 (equal (widget-get widget :value) value))
1869 (defun widget-item-match-inline (widget values)
1870 ;; Match if the value is the same.
1871 (let ((value (widget-get widget :value)))
1873 (<= (length value) (length values))
1874 (let ((head (widget-sublist values 0 (length value))))
1875 (and (equal head value)
1876 (cons head (widget-sublist values (length value))))))))
1878 (defun widget-sublist (list start &optional end)
1879 "Return the sublist of LIST from START to END.
1880 If END is omitted, it defaults to the length of LIST."
1881 (if (> start 0) (setq list (nthcdr start list)))
1885 (setq list (copy-sequence list))
1886 (setcdr (nthcdr (- end start 1) list) nil)
1888 (copy-sequence list)))
1890 (defun widget-item-action (widget &optional event)
1891 ;; Just notify itself.
1892 (widget-apply widget :notify widget event))
1894 ;;; The `push-button' Widget.
1896 (defcustom widget-push-button-gui widget-glyph-enable
1897 "If non nil, use GUI push buttons when available."
1901 ;; Cache already created GUI objects.
1902 (defvar widget-push-button-cache nil)
1904 (defcustom widget-push-button-prefix "["
1905 "String used as prefix for buttons."
1907 :group 'widget-button)
1909 (defcustom widget-push-button-suffix "]"
1910 "String used as suffix for buttons."
1912 :group 'widget-button)
1914 (define-widget 'push-button 'item
1915 "A pushable button."
1918 :value-create 'widget-push-button-value-create
1921 (defun widget-push-button-value-create (widget)
1922 ;; Insert text representing the `on' and `off' states.
1923 (let* ((tag (or (widget-get widget :tag)
1924 (widget-get widget :value)))
1925 (tag-glyph (widget-get widget :tag-glyph))
1926 (text (concat widget-push-button-prefix
1927 tag widget-push-button-suffix))
1928 (gui-glyphs (lax-plist-get widget-push-button-cache tag)))
1930 (widget-glyph-insert widget text tag-glyph))
1931 ;; We must check for console-on-window-system-p here,
1932 ;; because GUI will not work otherwise (it needs RGB
1933 ;; components for colors, and they are not known on TTYs).
1934 ((and widget-push-button-gui
1935 (console-on-window-system-p))
1937 (let* ((gui-button-shadow-thickness 1)
1939 (make-gui-button tag 'widget-gui-action widget))))
1940 (setq gui-glyphs gui)
1941 (laxputf widget-push-button-cache tag gui-glyphs)))
1942 (widget-glyph-insert-glyph widget gui-glyphs))
1946 (defun widget-gui-action (widget)
1947 "Apply :action for WIDGET."
1948 (widget-apply-action widget (this-command-keys)))
1950 ;;; The `link' Widget.
1952 (defcustom widget-link-prefix "["
1953 "String used as prefix for links."
1955 :group 'widget-button)
1957 (defcustom widget-link-suffix "]"
1958 "String used as suffix for links."
1960 :group 'widget-button)
1962 (define-widget 'link 'item
1964 :button-prefix 'widget-link-prefix
1965 :button-suffix 'widget-link-suffix
1966 :help-echo "Follow the link"
1969 ;;; The `info-link' Widget.
1971 (define-widget 'info-link 'link
1972 "A link to an info file."
1973 :help-echo 'widget-info-link-help-echo
1974 :action 'widget-info-link-action)
1976 (defun widget-info-link-help-echo (widget)
1977 (concat "Read the manual entry `" (widget-value widget) "'"))
1979 (defun widget-info-link-action (widget &optional event)
1980 "Open the info node specified by WIDGET."
1981 (Info-goto-node (widget-value widget)))
1983 ;;; The `url-link' Widget.
1985 (define-widget 'url-link 'link
1986 "A link to an www page."
1987 :help-echo 'widget-url-link-help-echo
1988 :action 'widget-url-link-action)
1990 (defun widget-url-link-help-echo (widget)
1991 (concat "Visit <URL:" (widget-value widget) ">"))
1993 (defun widget-url-link-action (widget &optional event)
1994 "Open the url specified by WIDGET."
1995 (if (boundp 'browse-url-browser-function)
1996 (funcall browse-url-browser-function (widget-value widget))
1997 (error "Cannot follow URLs in this XEmacs")))
1999 ;;; The `function-link' Widget.
2001 (define-widget 'function-link 'link
2002 "A link to an Emacs function."
2003 :action 'widget-function-link-action)
2005 (defun widget-function-link-action (widget &optional event)
2006 "Show the function specified by WIDGET."
2007 (describe-function (widget-value widget)))
2009 ;;; The `variable-link' Widget.
2011 (define-widget 'variable-link 'link
2012 "A link to an Emacs variable."
2013 :action 'widget-variable-link-action)
2015 (defun widget-variable-link-action (widget &optional event)
2016 "Show the variable specified by WIDGET."
2017 (describe-variable (widget-value widget)))
2019 ;;; The `file-link' Widget.
2021 (define-widget 'file-link 'link
2023 :action 'widget-file-link-action)
2025 (defun widget-file-link-action (widget &optional event)
2026 "Find the file specified by WIDGET."
2027 (find-file (widget-value widget)))
2029 ;;; The `emacs-library-link' Widget.
2031 (define-widget 'emacs-library-link 'link
2032 "A link to an Emacs Lisp library file."
2033 :help-echo 'widget-emacs-library-link-help-echo
2034 :action 'widget-emacs-library-link-action)
2036 (defun widget-emacs-library-link-help-echo (widget)
2037 (concat "Visit " (widget-value widget)))
2039 (defun widget-emacs-library-link-action (widget &optional event)
2040 "Find the Emacs Library file specified by WIDGET."
2041 (find-file (locate-library (widget-value widget))))
2043 ;;; The `emacs-commentary-link' Widget.
2045 (define-widget 'emacs-commentary-link 'link
2046 "A link to Commentary in an Emacs Lisp library file."
2047 :action 'widget-emacs-commentary-link-action)
2049 (defun widget-emacs-commentary-link-action (widget &optional event)
2050 "Find the Commentary section of the Emacs file specified by WIDGET."
2051 (finder-commentary (widget-value widget)))
2053 ;;; The `editable-field' Widget.
2055 (define-widget 'editable-field 'default
2056 "An editable text field."
2057 :convert-widget 'widget-value-convert-widget
2058 :keymap widget-field-keymap
2061 :prompt-internal 'widget-field-prompt-internal
2062 :prompt-history 'widget-field-history
2063 :prompt-value 'widget-field-prompt-value
2064 :action 'widget-field-action
2065 :validate 'widget-field-validate
2068 :value-create 'widget-field-value-create
2069 :value-delete 'widget-field-value-delete
2070 :value-get 'widget-field-value-get
2071 :match 'widget-field-match)
2073 (defvar widget-field-history nil
2074 "History of field minibuffer edits.")
2076 (defun widget-field-prompt-internal (widget prompt initial history)
2077 ;; Read string for WIDGET prompting with PROMPT.
2078 ;; INITIAL is the initial input and HISTORY is a symbol containing
2079 ;; the earlier input.
2080 (read-string prompt initial history))
2082 (defun widget-field-prompt-value (widget prompt value unbound)
2083 ;; Prompt for a string.
2084 (let ((initial (if unbound
2086 (cons (widget-apply widget :value-to-internal
2088 (history (widget-get widget :prompt-history)))
2089 (let ((answer (widget-apply widget
2090 :prompt-internal prompt initial history)))
2091 (widget-apply widget :value-to-external answer))))
2093 (defvar widget-edit-functions nil)
2095 (defun widget-field-action (widget &optional event)
2096 ;; Edit the value in the minibuffer.
2097 (let* ((invalid (widget-apply widget :validate))
2098 (prompt (concat (widget-apply widget :menu-tag-get) ": "))
2099 (value (unless invalid
2100 (widget-value widget)))
2101 (answer (widget-apply widget :prompt-value prompt value invalid)))
2102 (unless (equal value answer)
2103 ;; This is a hack. We can't properly validate the widget
2104 ;; because validation requires the new value to be in the field.
2105 ;; However, widget-field-value-create will not function unless
2106 ;; the new value matches. So, we check whether the thing
2107 ;; matches, and if it does, use either the real or a dummy error
2109 (unless (widget-apply widget :match answer)
2110 (let ((error-message (or (widget-get widget :type-error)
2111 "Invalid field contents")))
2112 (widget-put widget :error error-message)
2113 (error error-message)))
2114 (widget-value-set widget answer)
2115 (widget-apply widget :notify widget event)
2117 (run-hook-with-args 'widget-edit-functions widget)))
2119 ;(defun widget-field-action (widget &optional event)
2120 ; ;; Move to next field.
2121 ; (widget-forward 1)
2122 ; (run-hook-with-args 'widget-edit-functions widget))
2124 (defun widget-field-validate (widget)
2125 ;; Valid if the content matches `:valid-regexp'.
2127 (let ((value (widget-apply widget :value-get))
2128 (regexp (widget-get widget :valid-regexp)))
2129 (if (string-match regexp value)
2133 (defun widget-field-value-create (widget)
2134 ;; Create an editable text field.
2135 (let ((size (widget-get widget :size))
2136 (value (widget-get widget :value))
2138 ;; This is changed to a real extent in `widget-setup'. We
2139 ;; need the end points to behave differently until
2140 ;; `widget-setup' is called. Should probably be replaced with
2141 ;; a genuine extent, but some things break, then.
2142 (extent (cons (make-marker) (make-marker))))
2143 (widget-put widget :field-extent extent)
2146 (< (length value) size)
2147 (insert-char ?\ (- size (length value))))
2148 (unless (memq widget widget-field-list)
2149 (push widget widget-field-new))
2150 (move-marker (cdr extent) (point))
2151 (set-marker-insertion-type (cdr extent) nil)
2154 (move-marker (car extent) from)
2155 (set-marker-insertion-type (car extent) t)))
2157 (defun widget-field-value-delete (widget)
2158 ;; Remove the widget from the list of active editing fields.
2159 (setq widget-field-list (delq widget widget-field-list))
2160 ;; These are nil if the :format string doesn't contain `%v'.
2161 (let ((extent (widget-get widget :field-extent)))
2163 (detach-extent extent))))
2165 (defun widget-field-value-get (widget)
2166 ;; Return current text in editing field.
2167 (let ((from (widget-field-start widget))
2168 (to (widget-field-end widget))
2169 (buffer (widget-field-buffer widget))
2170 (size (widget-get widget :size))
2171 (secret (widget-get widget :secret))
2172 (old (current-buffer)))
2179 (eq (char-after (1- to)) ?\ ))
2181 (let ((result (buffer-substring-no-properties from to)))
2184 (while (< (+ from index) to)
2186 (get-char-property (+ from index) 'secret))
2191 (widget-get widget :value)))))
2193 (defun widget-field-match (widget value)
2194 ;; Match any string.
2197 ;;; The `text' Widget.
2199 (define-widget 'text 'editable-field
2200 :keymap widget-text-keymap
2201 "A multiline text area.")
2203 ;;; The `menu-choice' Widget.
2205 (define-widget 'menu-choice 'default
2206 "A menu of options."
2207 :convert-widget 'widget-types-convert-widget
2208 :format "%[%t%]: %v"
2211 :void '(item :format "invalid (%t)\n")
2212 :value-create 'widget-choice-value-create
2213 :value-delete 'widget-children-value-delete
2214 :value-get 'widget-choice-value-get
2215 :value-inline 'widget-choice-value-inline
2216 :default-get 'widget-choice-default-get
2217 :mouse-down-action 'widget-choice-mouse-down-action
2218 :action 'widget-choice-action
2219 :error "Make a choice"
2220 :validate 'widget-choice-validate
2221 :match 'widget-choice-match
2222 :match-inline 'widget-choice-match-inline)
2224 (defun widget-choice-value-create (widget)
2225 ;; Insert the first choice that matches the value.
2226 (let ((value (widget-get widget :value))
2227 (args (widget-get widget :args))
2228 (explicit (widget-get widget :explicit-choice))
2232 (widget-put widget :children (list (widget-create-child-value
2233 widget explicit value)))
2234 (widget-put widget :choice explicit))
2236 (setq current (car args)
2238 (when (widget-apply current :match value)
2239 (widget-put widget :children (list (widget-create-child-value
2240 widget current value)))
2241 (widget-put widget :choice current)
2245 (let ((void (widget-get widget :void)))
2246 (widget-put widget :children (list (widget-create-child-and-convert
2247 widget void :value value)))
2248 (widget-put widget :choice void))))))
2250 (defun widget-choice-value-get (widget)
2251 ;; Get value of the child widget.
2252 (widget-value (car (widget-get widget :children))))
2254 (defun widget-choice-value-inline (widget)
2255 ;; Get value of the child widget.
2256 (widget-apply (car (widget-get widget :children)) :value-inline))
2258 (defun widget-choice-default-get (widget)
2259 ;; Get default for the first choice.
2260 (widget-default-get (car (widget-get widget :args))))
2262 (defcustom widget-choice-toggle nil
2263 "If non-nil, a binary choice will just toggle between the values.
2264 Otherwise, the user will explicitly have to choose between the values
2265 when he invoked the menu."
2269 (defun widget-choice-mouse-down-action (widget &optional event)
2270 ;; Return non-nil if we need a menu.
2271 (let ((args (widget-get widget :args))
2272 (old (widget-get widget :choice)))
2273 (cond ((not (console-on-window-system-p))
2274 ;; No place to pop up a menu.
2276 ((< (length args) 2)
2277 ;; Empty or singleton list, just return the value.
2279 ((> (length args) widget-menu-max-size)
2280 ;; Too long, prompt.
2282 ((> (length args) 2)
2283 ;; Reasonable sized list, use menu.
2285 ((and widget-choice-toggle (memq old args))
2289 ;; Ask which of the two.
2292 (defun widget-choice-action (widget &optional event)
2294 (let ((args (widget-get widget :args))
2295 (old (widget-get widget :choice))
2296 (tag (widget-apply widget :menu-tag-get))
2297 (completion-ignore-case (widget-get widget :case-fold))
2299 ;; Remember old value.
2300 (if (and old (not (widget-apply widget :validate)))
2301 (let* ((external (widget-value widget))
2302 (internal (widget-apply old :value-to-internal external)))
2303 (widget-put old :value internal)))
2306 (cond ((= (length args) 0)
2308 ((= (length args) 1)
2310 ((and widget-choice-toggle
2313 (if (eq old (nth 0 args))
2318 (setq current (car args)
2321 (cons (cons (widget-apply current :menu-tag-get)
2325 (widget-choose tag (reverse choices) event)))
2326 (widget-put widget :explicit-choice choice)
2329 (let ((value (widget-default-get current)))
2330 (widget-value-set widget
2331 (widget-apply current :value-to-external value)))
2333 (widget-apply widget :notify widget event)))
2334 (run-hook-with-args 'widget-edit-functions widget))
2336 (defun widget-choice-validate (widget)
2337 ;; Valid if we have made a valid choice.
2338 (let ((void (widget-get widget :void))
2339 (choice (widget-get widget :choice))
2340 (child (car (widget-get widget :children))))
2341 (if (eq void choice)
2343 (widget-apply child :validate))))
2345 (defun widget-choice-match (widget value)
2346 ;; Matches if one of the choices matches.
2347 (let ((args (widget-get widget :args))
2349 (while (and args (not found))
2350 (setq current (car args)
2352 found (widget-apply current :match value)))
2355 (defun widget-choice-match-inline (widget values)
2356 ;; Matches if one of the choices matches.
2357 (let ((args (widget-get widget :args))
2359 (while (and args (null found))
2360 (setq current (car args)
2362 found (widget-match-inline current values)))
2365 ;;; The `toggle' Widget.
2367 (define-widget 'toggle 'item
2368 "Toggle between two states."
2370 :value-create 'widget-toggle-value-create
2371 :action 'widget-toggle-action
2372 :match (lambda (widget value) t)
2376 (defun widget-toggle-value-create (widget)
2377 ;; Insert text representing the `on' and `off' states.
2378 (if (widget-value widget)
2379 (widget-glyph-insert widget
2380 (widget-get widget :on)
2381 (widget-get widget :on-glyph))
2382 (widget-glyph-insert widget
2383 (widget-get widget :off)
2384 (widget-get widget :off-glyph))))
2386 (defun widget-toggle-action (widget &optional event)
2388 (widget-value-set widget (not (widget-value widget)))
2389 (widget-apply widget :notify widget event)
2390 (run-hook-with-args 'widget-edit-functions widget))
2392 ;;; The `checkbox' Widget.
2394 (define-widget 'checkbox 'toggle
2395 "A checkbox toggle."
2403 :action 'widget-checkbox-action)
2405 (defun widget-checkbox-action (widget &optional event)
2406 "Toggle checkbox, notify parent, and set active state of sibling."
2407 (widget-toggle-action widget event)
2408 (let ((sibling (widget-get-sibling widget)))
2410 (if (widget-value widget)
2411 (widget-apply sibling :activate)
2412 (widget-apply sibling :deactivate)))))
2414 ;;; The `checklist' Widget.
2416 (define-widget 'checklist 'default
2417 "A multiple choice widget."
2418 :convert-widget 'widget-types-convert-widget
2421 :entry-format "%b %v"
2422 :menu-tag "checklist"
2424 :value-create 'widget-checklist-value-create
2425 :value-delete 'widget-children-value-delete
2426 :value-get 'widget-checklist-value-get
2427 :validate 'widget-checklist-validate
2428 :match 'widget-checklist-match
2429 :match-inline 'widget-checklist-match-inline)
2431 (defun widget-checklist-value-create (widget)
2432 ;; Insert all values
2433 (let ((alist (widget-checklist-match-find widget (widget-get widget :value)))
2434 (args (widget-get widget :args)))
2436 (widget-checklist-add-item widget (car args) (assq (car args) alist))
2437 (setq args (cdr args)))
2438 (widget-put widget :children (nreverse (widget-get widget :children)))))
2440 (defun widget-checklist-add-item (widget type chosen)
2441 ;; Create checklist item in WIDGET of type TYPE.
2442 ;; If the item is checked, CHOSEN is a cons whose cdr is the value.
2443 (and (eq (preceding-char) ?\n)
2444 (widget-get widget :indent)
2445 (insert-char ?\ (widget-get widget :indent)))
2446 (widget-specify-insert
2447 (let* ((children (widget-get widget :children))
2448 (buttons (widget-get widget :buttons))
2449 (button-args (or (widget-get type :sibling-args)
2450 (widget-get widget :button-args)))
2453 (insert (widget-get widget :entry-format))
2455 ;; Parse % escapes in format.
2456 (while (re-search-forward "%\\([bv%]\\)" nil t)
2457 (let ((escape (aref (match-string 1) 0)))
2458 (replace-match "" t t)
2459 (cond ((eq escape ?%)
2462 (setq button (apply 'widget-create-child-and-convert
2464 :value (not (null chosen))
2469 (let ((child (widget-create-child widget type)))
2470 (widget-apply child :deactivate)
2472 ((widget-get type :inline)
2473 (widget-create-child-value
2474 widget type (cdr chosen)))
2476 (widget-create-child-value
2477 widget type (car (cdr chosen)))))))
2479 (signal 'error (list "Unknown escape" escape))))))
2480 ;; Update properties.
2481 (and button child (widget-put child :button button))
2482 (and button (widget-put widget :buttons (cons button buttons)))
2483 (and child (widget-put widget :children (cons child children))))))
2485 (defun widget-checklist-match (widget values)
2486 ;; All values must match a type in the checklist.
2488 (null (cdr (widget-checklist-match-inline widget values)))))
2490 (defun widget-checklist-match-inline (widget values)
2491 ;; Find the values which match a type in the checklist.
2492 (let ((greedy (widget-get widget :greedy))
2493 (args (copy-sequence (widget-get widget :args)))
2496 (let ((answer (widget-checklist-match-up args values)))
2498 (let ((vals (widget-match-inline answer values)))
2499 (setq found (append found (car vals))
2501 args (delq answer args))))
2503 (setq rest (append rest (list (car values)))
2504 values (cdr values)))
2506 (setq rest (append rest values)
2510 (defun widget-checklist-match-find (widget vals)
2511 ;; Find the vals which match a type in the checklist.
2512 ;; Return an alist of (TYPE MATCH).
2513 (let ((greedy (widget-get widget :greedy))
2514 (args (copy-sequence (widget-get widget :args)))
2517 (let ((answer (widget-checklist-match-up args vals)))
2519 (let ((match (widget-match-inline answer vals)))
2520 (setq found (cons (cons answer (car match)) found)
2522 args (delq answer args))))
2524 (setq vals (cdr vals)))
2529 (defun widget-checklist-match-up (args vals)
2530 ;; Rerturn the first type from ARGS that matches VALS.
2531 (let (current found)
2532 (while (and args (null found))
2533 (setq current (car args)
2535 found (widget-match-inline current vals)))
2540 (defun widget-checklist-value-get (widget)
2541 ;; The values of all selected items.
2542 (let ((children (widget-get widget :children))
2545 (setq child (car children)
2546 children (cdr children))
2547 (if (widget-value (widget-get child :button))
2548 (setq result (append result (widget-apply child :value-inline)))))
2551 (defun widget-checklist-validate (widget)
2552 ;; Ticked chilren must be valid.
2553 (let ((children (widget-get widget :children))
2555 (while (and children (not found))
2556 (setq child (car children)
2557 children (cdr children)
2558 button (widget-get child :button)
2559 found (and (widget-value button)
2560 (widget-apply child :validate))))
2563 ;;; The `option' Widget
2565 (define-widget 'option 'checklist
2566 "An widget with an optional item."
2569 ;;; The `choice-item' Widget.
2571 (define-widget 'choice-item 'item
2572 "Button items that delegate action events to their parents."
2573 :action 'widget-parent-action
2574 :format "%[%t%] \n")
2576 ;;; The `radio-button' Widget.
2578 (define-widget 'radio-button 'toggle
2579 "A radio button for use in the `radio' widget."
2580 :notify 'widget-radio-button-notify
2585 :on-glyph '("radio1" nil "radio0")
2587 :off-glyph "radio0")
2589 (defun widget-radio-button-notify (widget child &optional event)
2591 (widget-apply (widget-get widget :parent) :action widget event))
2593 ;;; The `radio-button-choice' Widget.
2595 (define-widget 'radio-button-choice 'default
2596 "Select one of multiple options."
2597 :convert-widget 'widget-types-convert-widget
2600 :entry-format "%b %v"
2602 :value-create 'widget-radio-value-create
2603 :value-delete 'widget-children-value-delete
2604 :value-get 'widget-radio-value-get
2605 :value-inline 'widget-radio-value-inline
2606 :value-set 'widget-radio-value-set
2607 :error "You must push one of the buttons"
2608 :validate 'widget-radio-validate
2609 :match 'widget-choice-match
2610 :match-inline 'widget-choice-match-inline
2611 :action 'widget-radio-action)
2613 (defun widget-radio-value-create (widget)
2614 ;; Insert all values
2615 (let ((args (widget-get widget :args))
2618 (setq arg (car args)
2620 (widget-radio-add-item widget arg))))
2622 (defun widget-radio-add-item (widget type)
2623 "Add to radio widget WIDGET a new radio button item of type TYPE."
2624 ;; (setq type (widget-convert type))
2625 (and (eq (preceding-char) ?\n)
2626 (widget-get widget :indent)
2627 (insert-char ?\ (widget-get widget :indent)))
2628 (widget-specify-insert
2629 (let* ((value (widget-get widget :value))
2630 (children (widget-get widget :children))
2631 (buttons (widget-get widget :buttons))
2632 (button-args (or (widget-get type :sibling-args)
2633 (widget-get widget :button-args)))
2635 (chosen (and (null (widget-get widget :choice))
2636 (widget-apply type :match value)))
2638 (insert (widget-get widget :entry-format))
2640 ;; Parse % escapes in format.
2641 (while (re-search-forward "%\\([bv%]\\)" nil t)
2642 (let ((escape (aref (match-string 1) 0)))
2643 (replace-match "" t t)
2644 (cond ((eq escape ?%)
2647 (setq button (apply 'widget-create-child-and-convert
2648 widget 'radio-button
2649 :value (not (null chosen))
2652 (setq child (if chosen
2653 (widget-create-child-value
2655 (widget-create-child widget type)))
2657 (widget-apply child :deactivate)))
2659 (signal 'error (list "Unknown escape" escape))))))
2660 ;; Update properties.
2662 (widget-put widget :choice type))
2664 (widget-put child :button button)
2665 (widget-put widget :buttons (nconc buttons (list button))))
2667 (widget-put widget :children (nconc children (list child))))
2670 (defun widget-radio-value-get (widget)
2671 ;; Get value of the child widget.
2672 (let ((chosen (widget-radio-chosen widget)))
2673 (and chosen (widget-value chosen))))
2675 (defun widget-radio-chosen (widget)
2676 "Return the widget representing the chosen radio button."
2677 (let ((children (widget-get widget :children))
2680 (setq current (car children)
2681 children (cdr children))
2682 (let* ((button (widget-get current :button))
2683 (value (widget-apply button :value-get)))
2689 (defun widget-radio-value-inline (widget)
2690 ;; Get value of the child widget.
2691 (let ((children (widget-get widget :children))
2694 (setq current (car children)
2695 children (cdr children))
2696 (let* ((button (widget-get current :button))
2697 (value (widget-apply button :value-get)))
2699 (setq found (widget-apply current :value-inline)
2703 (defun widget-radio-value-set (widget value)
2704 ;; We can't just delete and recreate a radio widget, since children
2705 ;; can be added after the original creation and won't be recreated
2707 (let ((children (widget-get widget :children))
2710 (setq current (car children)
2711 children (cdr children))
2712 (let* ((button (widget-get current :button))
2713 (match (and (not found)
2714 (widget-apply current :match value))))
2715 (widget-value-set button match)
2718 (widget-value-set current value)
2719 (widget-apply current :activate))
2720 (widget-apply current :deactivate))
2721 (setq found (or found match))))))
2723 (defun widget-radio-validate (widget)
2724 ;; Valid if we have made a valid choice.
2725 (let ((children (widget-get widget :children))
2726 current found button)
2727 (while (and children (not found))
2728 (setq current (car children)
2729 children (cdr children)
2730 button (widget-get current :button)
2731 found (widget-apply button :value-get)))
2733 (widget-apply current :validate)
2736 (defun widget-radio-action (widget child event)
2737 ;; Check if a radio button was pressed.
2738 (let ((children (widget-get widget :children))
2739 (buttons (widget-get widget :buttons))
2741 (when (memq child buttons)
2743 (setq current (car children)
2744 children (cdr children))
2745 (let* ((button (widget-get current :button)))
2746 (cond ((eq child button)
2747 (widget-value-set button t)
2748 (widget-apply current :activate))
2749 ((widget-value button)
2750 (widget-value-set button nil)
2751 (widget-apply current :deactivate)))))))
2752 ;; Pass notification to parent.
2753 (widget-apply widget :notify child event))
2755 ;;; The `insert-button' Widget.
2757 (define-widget 'insert-button 'push-button
2758 "An insert button for the `editable-list' widget."
2760 :help-echo "Insert a new item into the list at this position"
2761 :action 'widget-insert-button-action)
2763 (defun widget-insert-button-action (widget &optional event)
2764 ;; Ask the parent to insert a new item.
2765 (widget-apply (widget-get widget :parent)
2766 :insert-before (widget-get widget :widget)))
2768 ;;; The `delete-button' Widget.
2770 (define-widget 'delete-button 'push-button
2771 "A delete button for the `editable-list' widget."
2773 :help-echo "Delete this item from the list"
2774 :action 'widget-delete-button-action)
2776 (defun widget-delete-button-action (widget &optional event)
2777 ;; Ask the parent to insert a new item.
2778 (widget-apply (widget-get widget :parent)
2779 :delete-at (widget-get widget :widget)))
2781 ;;; The `editable-list' Widget.
2783 (defcustom widget-editable-list-gui nil
2784 "If non nil, use GUI push-buttons in editable list when available."
2788 (define-widget 'editable-list 'default
2789 "A variable list of widgets of the same type."
2790 :convert-widget 'widget-types-convert-widget
2793 :format-handler 'widget-editable-list-format-handler
2794 :entry-format "%i %d %v"
2795 :menu-tag "editable-list"
2796 :value-create 'widget-editable-list-value-create
2797 :value-delete 'widget-children-value-delete
2798 :value-get 'widget-editable-list-value-get
2799 :validate 'widget-children-validate
2800 :match 'widget-editable-list-match
2801 :match-inline 'widget-editable-list-match-inline
2802 :insert-before 'widget-editable-list-insert-before
2803 :delete-at 'widget-editable-list-delete-at)
2805 (defun widget-editable-list-format-handler (widget escape)
2806 ;; We recognize the insert button.
2807 (let ((widget-push-button-gui widget-editable-list-gui))
2808 (cond ((eq escape ?i)
2809 (and (widget-get widget :indent)
2810 (insert-char ?\ (widget-get widget :indent)))
2811 (apply 'widget-create-child-and-convert
2812 widget 'insert-button
2813 (widget-get widget :append-button-args)))
2815 (widget-default-format-handler widget escape)))))
2817 (defun widget-editable-list-value-create (widget)
2818 ;; Insert all values
2819 (let* ((value (widget-get widget :value))
2820 (type (nth 0 (widget-get widget :args)))
2821 (inlinep (widget-get type :inline))
2823 (widget-put widget :value-pos (copy-marker (point)))
2824 (set-marker-insertion-type (widget-get widget :value-pos) t)
2826 (let ((answer (widget-match-inline type value)))
2828 (setq children (cons (widget-editable-list-entry-create
2837 (widget-put widget :children (nreverse children))))
2839 (defun widget-editable-list-value-get (widget)
2840 ;; Get value of the child widget.
2841 (apply 'append (mapcar (lambda (child) (widget-apply child :value-inline))
2842 (widget-get widget :children))))
2844 (defun widget-editable-list-match (widget value)
2845 ;; Value must be a list and all the members must match the type.
2847 (null (cdr (widget-editable-list-match-inline widget value)))))
2849 (defun widget-editable-list-match-inline (widget value)
2850 (let ((type (nth 0 (widget-get widget :args)))
2853 (while (and value ok)
2854 (let ((answer (widget-match-inline type value)))
2856 (setq found (append found (car answer))
2859 (cons found value)))
2861 (defun widget-editable-list-insert-before (widget before)
2862 ;; Insert a new child in the list of children.
2864 (let ((children (widget-get widget :children))
2865 (inhibit-read-only t)
2866 before-change-functions
2867 after-change-functions)
2869 (goto-char (widget-get before :entry-from)))
2871 (goto-char (widget-get widget :value-pos))))
2872 (let ((child (widget-editable-list-entry-create
2874 (when (< (widget-get child :entry-from) (widget-get widget :from))
2875 (set-marker (widget-get widget :from)
2876 (widget-get child :entry-from)))
2877 (if (eq (car children) before)
2878 (widget-put widget :children (cons child children))
2879 (while (not (eq (car (cdr children)) before))
2880 (setq children (cdr children)))
2881 (setcdr children (cons child (cdr children)))))))
2883 (widget-apply widget :notify widget))
2885 (defun widget-editable-list-delete-at (widget child)
2886 ;; Delete child from list of children.
2888 (let ((buttons (copy-sequence (widget-get widget :buttons)))
2890 (inhibit-read-only t)
2891 before-change-functions
2892 after-change-functions)
2894 (setq button (car buttons)
2895 buttons (cdr buttons))
2896 (when (eq (widget-get button :widget) child)
2898 :buttons (delq button (widget-get widget :buttons)))
2899 (widget-delete button))))
2900 (let ((entry-from (widget-get child :entry-from))
2901 (entry-to (widget-get child :entry-to))
2902 (inhibit-read-only t)
2903 before-change-functions
2904 after-change-functions)
2905 (widget-delete child)
2906 (delete-region entry-from entry-to)
2907 (set-marker entry-from nil)
2908 (set-marker entry-to nil))
2909 (widget-put widget :children (delq child (widget-get widget :children))))
2911 (widget-apply widget :notify widget))
2913 (defun widget-editable-list-entry-create (widget value conv)
2914 ;; Create a new entry to the list.
2915 (let ((type (nth 0 (widget-get widget :args)))
2916 (widget-push-button-gui widget-editable-list-gui)
2917 child delete insert)
2918 (widget-specify-insert
2920 (and (widget-get widget :indent)
2921 (insert-char ?\ (widget-get widget :indent)))
2922 (insert (widget-get widget :entry-format)))
2923 ;; Parse % escapes in format.
2924 (while (re-search-forward "%\\(.\\)" nil t)
2925 (let ((escape (aref (match-string 1) 0)))
2926 (replace-match "" t t)
2927 (cond ((eq escape ?%)
2930 (setq insert (apply 'widget-create-child-and-convert
2931 widget 'insert-button
2932 (widget-get widget :insert-button-args))))
2934 (setq delete (apply 'widget-create-child-and-convert
2935 widget 'delete-button
2936 (widget-get widget :delete-button-args))))
2939 (setq child (widget-create-child-value
2941 (setq child (widget-create-child-value
2942 widget type (widget-default-get type)))))
2944 (signal 'error (list "Unknown escape" escape))))))
2946 :buttons (cons delete
2948 (widget-get widget :buttons))))
2949 (let ((entry-from (copy-marker (point-min)))
2950 (entry-to (copy-marker (point-max))))
2951 (set-marker-insertion-type entry-from t)
2952 (set-marker-insertion-type entry-to nil)
2953 (widget-put child :entry-from entry-from)
2954 (widget-put child :entry-to entry-to)))
2955 (widget-put insert :widget child)
2956 (widget-put delete :widget child)
2959 ;;; The `group' Widget.
2961 (define-widget 'group 'default
2962 "A widget which group other widgets inside."
2963 :convert-widget 'widget-types-convert-widget
2965 :value-create 'widget-group-value-create
2966 :value-delete 'widget-children-value-delete
2967 :value-get 'widget-editable-list-value-get
2968 :default-get 'widget-group-default-get
2969 :validate 'widget-children-validate
2970 :match 'widget-group-match
2971 :match-inline 'widget-group-match-inline)
2973 (defun widget-group-value-create (widget)
2974 ;; Create each component.
2975 (let ((args (widget-get widget :args))
2976 (value (widget-get widget :value))
2977 arg answer children)
2979 (setq arg (car args)
2981 answer (widget-match-inline arg value)
2983 (and (eq (preceding-char) ?\n)
2984 (widget-get widget :indent)
2985 (insert-char ?\ (widget-get widget :indent)))
2986 (push (cond ((null answer)
2987 (widget-create-child widget arg))
2988 ((widget-get arg :inline)
2989 (widget-create-child-value widget arg (car answer)))
2991 (widget-create-child-value widget arg (car (car answer)))))
2993 (widget-put widget :children (nreverse children))))
2995 (defun widget-group-default-get (widget)
2996 ;; Get the default of the components.
2997 (mapcar 'widget-default-get (widget-get widget :args)))
2999 (defun widget-group-match (widget values)
3000 ;; Match if the components match.
3002 (let ((match (widget-group-match-inline widget values)))
3003 (and match (null (cdr match))))))
3005 (defun widget-group-match-inline (widget vals)
3006 ;; Match if the components match.
3007 (let ((args (widget-get widget :args))
3008 argument answer found)
3010 (setq argument (car args)
3012 answer (widget-match-inline argument vals))
3014 (setq vals (cdr answer)
3015 found (append found (car answer)))
3022 ;;; The `visibility' Widget.
3024 (define-widget 'visibility 'item
3025 "An indicator and manipulator for hidden items."
3031 :value-create 'widget-visibility-value-create
3032 :action 'widget-toggle-action
3033 :match (lambda (widget value) t))
3035 (defun widget-visibility-value-create (widget)
3036 ;; Insert text representing the `on' and `off' states.
3037 (let ((on (widget-get widget :on))
3038 (off (widget-get widget :off)))
3040 (setq on (concat widget-push-button-prefix
3042 widget-push-button-suffix))
3045 (setq off (concat widget-push-button-prefix
3047 widget-push-button-suffix))
3049 (if (widget-value widget)
3050 (widget-glyph-insert widget on '("down" "down-pushed"))
3051 (widget-glyph-insert widget off '("right" "right-pushed")))))
3053 ;;; The `documentation-link' Widget.
3055 ;; This is a helper widget for `documentation-string'.
3057 (define-widget 'documentation-link 'link
3058 "Link type used in documentation strings."
3060 :help-echo 'widget-documentation-link-echo-help
3061 :action 'widget-documentation-link-action)
3063 (defun widget-documentation-link-echo-help (widget)
3064 "Tell what this link will describe."
3065 (concat "Describe the `" (widget-get widget :value) "' symbol."))
3067 (defun widget-documentation-link-action (widget &optional event)
3068 "Display documentation for WIDGET's value. Ignore optional argument EVENT."
3069 (let* ((string (widget-get widget :value))
3070 (symbol (intern string)))
3071 (if (and (fboundp symbol) (boundp symbol))
3072 ;; If there are two doc strings, give the user a way to pick one.
3073 (apropos (concat "\\`" (regexp-quote string) "\\'"))
3074 (if (fboundp symbol)
3075 (describe-function symbol)
3076 (describe-variable symbol)))))
3078 (defcustom widget-documentation-links t
3079 "Add hyperlinks to documentation strings when non-nil."
3081 :group 'widget-documentation)
3083 (defcustom widget-documentation-link-regexp "`\\([^\n`' ]+\\)'"
3084 "Regexp for matching potential links in documentation strings.
3085 The first group should be the link itself."
3087 :group 'widget-documentation)
3089 (defcustom widget-documentation-link-p 'intern-soft
3090 "Predicate used to test if a string is useful as a link.
3091 The value should be a function. The function will be called one
3092 argument, a string, and should return non-nil if there should be a
3093 link for that string."
3095 :options '(widget-documentation-link-p)
3096 :group 'widget-documentation)
3098 (defcustom widget-documentation-link-type 'documentation-link
3099 "Widget type used for links in documentation strings."
3101 :group 'widget-documentation)
3103 (defun widget-documentation-link-add (widget from to)
3104 (widget-specify-doc widget from to)
3105 (when widget-documentation-links
3106 (let ((regexp widget-documentation-link-regexp)
3107 (predicate widget-documentation-link-p)
3108 (type widget-documentation-link-type)
3109 (buttons (widget-get widget :buttons)))
3112 (while (re-search-forward regexp to t)
3113 (let ((name (match-string 1))
3114 (begin (match-beginning 1))
3115 (end (match-end 1)))
3116 (when (funcall predicate name)
3117 (push (widget-convert-button type begin end :value name)
3119 (widget-put widget :buttons buttons)))
3120 (let ((indent (widget-get widget :indent)))
3121 (when (and indent (not (zerop indent)))
3124 (narrow-to-region from to)
3125 (goto-char (point-min))
3126 (while (search-forward "\n" nil t)
3127 (insert-char ?\ indent)))))))
3129 ;;; The `documentation-string' Widget.
3131 (define-widget 'documentation-string 'item
3132 "A documentation string."
3134 :action 'widget-documentation-string-action
3135 :value-delete 'widget-children-value-delete
3136 :value-create 'widget-documentation-string-value-create)
3138 (defun widget-documentation-string-value-create (widget)
3139 ;; Insert documentation string.
3140 (let ((doc (widget-value widget))
3141 (indent (widget-get widget :indent))
3142 (shown (widget-get (widget-get widget :parent) :documentation-shown))
3144 (if (string-match "\n" doc)
3145 (let ((before (substring doc 0 (match-beginning 0)))
3146 (after (substring doc (match-beginning 0)))
3149 (widget-documentation-link-add widget start (point))
3150 (push (widget-create-child-and-convert
3152 :help-echo (lambda (widget)
3154 (if (widget-value widget)
3156 " the rest of the documentation"))
3158 :action 'widget-parent-action
3162 (setq start (point))
3164 (insert-char ?\ indent))
3166 (widget-documentation-link-add widget start (point)))
3167 (widget-put widget :buttons buttons))
3169 (widget-documentation-link-add widget start (point))))
3172 (defun widget-documentation-string-action (widget &rest ignore)
3173 ;; Toggle documentation.
3174 (let ((parent (widget-get widget :parent)))
3175 (widget-put parent :documentation-shown
3176 (not (widget-get parent :documentation-shown))))
3178 (widget-value-set widget (widget-value widget)))
3180 ;;; The Sexp Widgets.
3182 (define-widget 'const 'item
3183 "An immutable sexp."
3184 :prompt-value 'widget-const-prompt-value
3187 (defun widget-const-prompt-value (widget prompt value unbound)
3188 ;; Return the value of the const.
3189 (widget-value widget))
3191 (define-widget 'function-item 'const
3192 "An immutable function name."
3194 :documentation-property (lambda (symbol)
3196 (documentation symbol t)
3199 (define-widget 'variable-item 'const
3200 "An immutable variable name."
3202 :documentation-property 'variable-documentation)
3204 (defvar widget-string-prompt-value-history nil
3205 "History of input to `widget-string-prompt-value'.")
3207 (define-widget 'string 'editable-field
3210 :format "%{%t%}: %v"
3211 :complete-function 'ispell-complete-word
3212 :prompt-history 'widget-string-prompt-value-history)
3214 (define-widget 'regexp 'string
3215 "A regular expression."
3216 :match 'widget-regexp-match
3217 :validate 'widget-regexp-validate
3218 ;; Doesn't work well with terminating newline.
3219 ;; :value-face 'widget-single-line-field-face
3222 (defun widget-regexp-match (widget value)
3223 ;; Match valid regexps.
3224 (and (stringp value)
3227 (string-match value ""))
3230 (defun widget-regexp-validate (widget)
3231 "Check that the value of WIDGET is a valid regexp."
3232 (let ((value (widget-value widget)))
3233 (condition-case data
3235 (string-match value ""))
3236 (error (widget-put widget :error (error-message-string data))
3239 (define-widget 'file 'string
3241 It will read a file name from the minibuffer when invoked."
3242 :complete-function 'widget-file-complete
3243 :prompt-value 'widget-file-prompt-value
3244 :format "%{%t%}: %v"
3245 ;; Doesn't work well with terminating newline.
3246 ;; :value-face 'widget-single-line-field-face
3249 (defun widget-file-complete ()
3250 "Perform completion on file name preceding point."
3252 (let* ((end (point))
3253 (beg (save-excursion
3254 (skip-chars-backward "^ ")
3256 (pattern (buffer-substring beg end))
3257 (name-part (file-name-nondirectory pattern))
3258 (directory (file-name-directory pattern))
3259 (completion (file-name-completion name-part directory)))
3260 (cond ((eq completion t))
3262 (message "Can't find completion for \"%s\"" pattern)
3264 ((not (string= name-part completion))
3265 (delete-region beg end)
3266 (insert (expand-file-name completion directory)))
3268 (message "Making completion list...")
3269 (let ((list (file-name-all-completions name-part directory)))
3270 (setq list (sort list 'string<))
3271 (with-output-to-temp-buffer "*Completions*"
3272 (display-completion-list list)))
3273 (message "Making completion list...%s" "done")))))
3275 (defun widget-file-prompt-value (widget prompt value unbound)
3276 ;; Read file from minibuffer.
3277 (abbreviate-file-name
3279 (read-file-name prompt)
3280 (let ((prompt2 (format "%s (default %s) " prompt value))
3281 (dir (file-name-directory value))
3282 (file (file-name-nondirectory value))
3283 (must-match (widget-get widget :must-match)))
3284 (read-file-name prompt2 dir nil must-match file)))))
3286 ;;;(defun widget-file-action (widget &optional event)
3287 ;;; ;; Read a file name from the minibuffer.
3288 ;;; (let* ((value (widget-value widget))
3289 ;;; (dir (file-name-directory value))
3290 ;;; (file (file-name-nondirectory value))
3291 ;;; (menu-tag (widget-apply widget :menu-tag-get))
3292 ;;; (must-match (widget-get widget :must-match))
3293 ;;; (answer (read-file-name (concat menu-tag ": (default `" value "') ")
3294 ;;; dir nil must-match file)))
3295 ;;; (widget-value-set widget (abbreviate-file-name answer))
3297 ;;; (widget-apply widget :notify widget event)))
3299 (define-widget 'directory 'file
3300 "A directory widget.
3301 It will read a directory name from the minibuffer when invoked."
3304 (defvar widget-symbol-prompt-value-history nil
3305 "History of input to `widget-symbol-prompt-value'.")
3307 (define-widget 'symbol 'editable-field
3311 :format "%{%t%}: %v"
3312 :match (lambda (widget value) (symbolp value))
3313 :complete-function 'lisp-complete-symbol
3314 :prompt-internal 'widget-symbol-prompt-internal
3315 :prompt-match 'symbolp
3316 :prompt-history 'widget-symbol-prompt-value-history
3317 :value-to-internal (lambda (widget value)
3321 :value-to-external (lambda (widget value)
3326 (defun widget-symbol-prompt-internal (widget prompt initial history)
3327 ;; Read file from minibuffer.
3328 (let ((answer (completing-read prompt obarray
3329 (widget-get widget :prompt-match)
3330 nil initial history)))
3331 (if (and (stringp answer)
3332 (not (zerop (length answer))))
3334 (error "No value"))))
3336 (defvar widget-function-prompt-value-history nil
3337 "History of input to `widget-function-prompt-value'.")
3339 (define-widget 'function 'sexp
3341 :complete-function 'lisp-complete-symbol
3342 :prompt-value 'widget-field-prompt-value
3343 :prompt-internal 'widget-symbol-prompt-internal
3344 :prompt-match 'fboundp
3345 :prompt-history 'widget-function-prompt-value-history
3346 :action 'widget-field-action
3349 (defvar widget-variable-prompt-value-history nil
3350 "History of input to `widget-variable-prompt-value'.")
3352 (define-widget 'variable 'symbol
3353 ;; Should complete on variables.
3355 :prompt-match 'boundp
3356 :prompt-history 'widget-variable-prompt-value-history
3359 ;; This part issues a warning when compiling without Mule. Is there a
3360 ;; way of shutting it up?
3362 ;; OK, I'll simply comment the whole thing out, until someone decides
3363 ;; to do something with it.
3364 ;(defvar widget-coding-system-prompt-value-history nil
3365 ; "History of input to `widget-coding-system-prompt-value'.")
3367 ;(define-widget 'coding-system 'symbol
3368 ; "A MULE coding-system."
3369 ; :format "%{%t%}: %v"
3370 ; :tag "Coding system"
3371 ; :prompt-history 'widget-coding-system-prompt-value-history
3372 ; :prompt-value 'widget-coding-system-prompt-value
3373 ; :action 'widget-coding-system-action)
3375 ;(defun widget-coding-system-prompt-value (widget prompt value unbound)
3376 ; ;; Read coding-system from minibuffer.
3378 ; (completing-read (format "%s (default %s) " prompt value)
3379 ; (mapcar (lambda (sym)
3380 ; (list (symbol-name sym)))
3381 ; (coding-system-list)))))
3383 ;(defun widget-coding-system-action (widget &optional event)
3384 ; ;; Read a file name from the minibuffer.
3386 ; (widget-coding-system-prompt-value
3388 ; (widget-apply widget :menu-tag-get)
3389 ; (widget-value widget)
3391 ; (widget-value-set widget answer)
3392 ; (widget-apply widget :notify widget event)
3395 (define-widget 'sexp 'editable-field
3396 "An arbitrary lisp expression."
3397 :tag "Lisp expression"
3398 :format "%{%t%}: %v"
3400 :validate 'widget-sexp-validate
3401 :match (lambda (widget value) t)
3402 :value-to-internal 'widget-sexp-value-to-internal
3403 :value-to-external (lambda (widget value) (read value))
3404 :prompt-history 'widget-sexp-prompt-value-history
3405 :prompt-value 'widget-sexp-prompt-value)
3407 (defun widget-sexp-value-to-internal (widget value)
3408 ;; Use cl-prettyprint for printer representation.
3409 (let ((pp (if (symbolp value)
3410 (prin1-to-string value)
3411 (widget-prettyprint-to-string value))))
3412 (if (> (length pp) 40)
3416 (defun widget-sexp-validate (widget)
3417 ;; Valid if we can read the string and there is no junk left after it.
3419 (let ((buffer (set-buffer (get-buffer-create " *Widget Scratch*"))))
3421 (insert (widget-apply widget :value-get))
3422 (goto-char (point-min))
3423 (condition-case data
3424 (let ((value (read buffer)))
3426 (if (widget-apply widget :match value)
3428 (widget-put widget :error (widget-get widget :type-error))
3431 :error (format "Junk at end of expression: %s"
3432 (buffer-substring (point)
3435 (error (widget-put widget :error (error-message-string data))
3438 (defvar widget-sexp-prompt-value-history nil
3439 "History of input to `widget-sexp-prompt-value'.")
3441 (defun widget-sexp-prompt-value (widget prompt value unbound)
3442 ;; Read an arbitrary sexp.
3443 (let ((found (read-string prompt
3444 (if unbound nil (cons (prin1-to-string value) 0))
3445 (widget-get widget :prompt-history))))
3447 (let ((buffer (set-buffer (get-buffer-create " *Widget Scratch*"))))
3450 (goto-char (point-min))
3451 (let ((answer (read buffer)))
3454 (list "Junk at end of expression"
3455 (buffer-substring (point) (point-max)))))
3458 (define-widget 'restricted-sexp 'sexp
3459 "A Lisp expression restricted to values that match.
3460 To use this type, you must define :match or :match-alternatives."
3461 :type-error "The specified value is not valid"
3462 :match 'widget-restricted-sexp-match
3463 :value-to-internal (lambda (widget value)
3464 (if (widget-apply widget :match value)
3465 (prin1-to-string value)
3468 (defun widget-restricted-sexp-match (widget value)
3469 (let ((alternatives (widget-get widget :match-alternatives))
3471 (while (and alternatives (not matched))
3472 (if (cond ((functionp (car alternatives))
3473 (funcall (car alternatives) value))
3474 ((and (consp (car alternatives))
3475 (eq (car (car alternatives)) 'quote))
3476 (eq value (nth 1 (car alternatives)))))
3478 (setq alternatives (cdr alternatives)))
3481 (define-widget 'integer 'restricted-sexp
3485 :type-error "This field should contain an integer"
3486 :match-alternatives '(integerp))
3488 (define-widget 'number 'restricted-sexp
3489 "A floating point number."
3492 :type-error "This field should contain a number"
3493 :match-alternatives '(numberp))
3495 (define-widget 'character 'editable-field
3499 :format "%{%t%}: %v"
3500 :valid-regexp "\\`[\0-\377]\\'"
3501 :error "This field should contain a single character"
3502 :value-to-internal (lambda (widget value)
3505 (char-to-string value)))
3506 :value-to-external (lambda (widget value)
3510 :match (lambda (widget value)
3511 (characterp value)))
3513 (define-widget 'list 'group
3516 :format "%{%t%}:\n%v")
3518 (define-widget 'vector 'group
3521 :format "%{%t%}:\n%v"
3522 :match 'widget-vector-match
3523 :value-to-internal (lambda (widget value) (append value nil))
3524 :value-to-external (lambda (widget value) (vconcat value)))
3526 (defun widget-vector-match (widget value)
3527 (and (vectorp value)
3528 (widget-group-match widget
3529 (widget-apply widget :value-to-internal value))))
3531 (define-widget 'cons 'group
3534 :format "%{%t%}:\n%v"
3535 :match 'widget-cons-match
3536 :value-to-internal (lambda (widget value)
3537 (list (car value) (cdr value)))
3538 :value-to-external (lambda (widget value)
3539 (cons (car value) (cadr value))))
3541 (defun widget-cons-match (widget value)
3543 (widget-group-match widget
3544 (widget-apply widget :value-to-internal value))))
3546 (define-widget 'choice 'menu-choice
3547 "A union of several sexp types."
3549 :format "%{%t%}: %[Value Menu%] %v"
3550 :button-prefix 'widget-push-button-prefix
3551 :button-suffix 'widget-push-button-suffix
3552 :prompt-value 'widget-choice-prompt-value)
3554 (defun widget-choice-prompt-value (widget prompt value unbound)
3556 (let ((args (widget-get widget :args))
3557 (completion-ignore-case (widget-get widget :case-fold))
3558 current choices old)
3559 ;; Find the first arg that match VALUE.
3562 (if (widget-apply (car look) :match value)
3563 (setq old (car look)
3565 (setq look (cdr look)))))
3568 (cond ((= (length args) 0)
3570 ((= (length args) 1)
3572 ((and (= (length args) 2)
3574 (if (eq old (nth 0 args))
3579 (setq current (car args)
3582 (cons (cons (widget-apply current :menu-tag-get)
3585 (let ((val (completing-read prompt choices nil t)))
3587 (let ((try (try-completion val choices)))
3590 (cdr (assoc val choices)))
3593 (widget-prompt-value current prompt nil t)
3596 (define-widget 'radio 'radio-button-choice
3597 "A union of several sexp types."
3599 :format "%{%t%}:\n%v"
3600 :prompt-value 'widget-choice-prompt-value)
3602 (define-widget 'repeat 'editable-list
3603 "A variable length homogeneous list."
3605 :format "%{%t%}:\n%v%i\n")
3607 (define-widget 'set 'checklist
3608 "A list of members from a fixed set."
3610 :format "%{%t%}:\n%v")
3612 (define-widget 'boolean 'toggle
3613 "To be nil or non-nil, that is the question."
3615 :prompt-value 'widget-boolean-prompt-value
3616 :button-prefix 'widget-push-button-prefix
3617 :button-suffix 'widget-push-button-suffix
3618 :format "%{%t%}: %[Toggle%] %v\n"
3622 (defun widget-boolean-prompt-value (widget prompt value unbound)
3623 ;; Toggle a boolean.
3626 ;;; The `color' Widget.
3628 (define-widget 'color 'editable-field
3629 "Choose a color name (with sample)."
3630 :format "%[%t%]: %v (%{sample%})\n"
3634 :complete 'widget-color-complete
3635 :sample-face-get 'widget-color-sample-face-get
3636 :notify 'widget-color-notify
3637 :action 'widget-color-action)
3639 (defun widget-color-complete (widget)
3640 "Complete the color in WIDGET."
3641 (let* ((prefix (buffer-substring-no-properties (widget-field-start widget)
3643 (list (read-color-completion-table))
3644 (completion (try-completion prefix list)))
3645 (cond ((eq completion t)
3646 (message "Exact match"))
3648 (error "Can't find completion for \"%s\"" prefix))
3649 ((not (string-equal prefix completion))
3650 (insert (substring completion (length prefix))))
3652 (message "Making completion list...")
3653 (let ((list (all-completions prefix list nil)))
3654 (with-output-to-temp-buffer "*Completions*"
3655 (display-completion-list list)))
3656 (message "Making completion list...done")))))
3658 (defun widget-color-sample-face-get (widget)
3659 (or (widget-get widget :sample-face)
3660 (let ((color (widget-value widget))
3661 (face (make-face (gensym "sample-face-") nil t)))
3662 ;; Use the face object, not its name, to prevent lossage if gc
3663 ;; happens before applying the face.
3664 (widget-put widget :sample-face face)
3666 (not (equal color ""))
3667 (valid-color-name-p color)
3668 (set-face-foreground face color))
3671 (defvar widget-color-history nil
3672 "History of entered colors.")
3674 (defun widget-color-action (widget &optional event)
3675 ;; Prompt for a color.
3676 (let* ((tag (widget-apply widget :menu-tag-get))
3677 (answer (read-color (concat tag ": "))))
3678 (unless (zerop (length answer))
3679 (widget-value-set widget answer)
3681 (widget-apply widget :notify widget event))))
3683 (defun widget-color-notify (widget child &optional event)
3684 "Update the sample, and notify the parent."
3685 (let* ((face (widget-apply widget :sample-face-get))
3686 (color (widget-value widget)))
3687 (if (valid-color-name-p color)
3688 (set-face-foreground face color)
3689 (remove-face-property face 'foreground)))
3690 (widget-default-notify widget child event))
3692 ;; Is this a misnomer?
3693 (defun widget-at (pos)
3694 "The button or field at POS."
3695 (or (get-char-property pos 'button)
3696 (get-char-property pos 'field)))
3698 (defun widget-echo-help (pos)
3699 "Display the help echo for widget at POS."
3700 (let* ((widget (widget-at pos))
3701 (help-echo (and widget (widget-get widget :help-echo))))
3702 (and (functionp help-echo)
3703 (setq help-echo (funcall help-echo widget)))
3704 (when (stringp help-echo)
3705 (display-message 'help-echo help-echo))))
3711 ;; wid-edit.el ends here