Handle exclusive access to the minibuffer using a mutex, remove inhibit-yield
[emacs.git] / lisp / wid-edit.el
blob6296a965df9d097dab9657428d6f1333780d371f
1 ;;; wid-edit.el --- Functions for creating and using widgets -*-byte-compile-dynamic: t;-*-
2 ;;
3 ;; Copyright (C) 1996, 1997, 1999, 2000, 2001, 2002, 2003, 2004, 2005,
4 ;; 2006, 2007, 2008, 2009, 2010 Free Software Foundation, Inc.
5 ;;
6 ;; Author: Per Abrahamsen <abraham@dina.kvl.dk>
7 ;; Maintainer: FSF
8 ;; Keywords: extensions
10 ;; This file is part of GNU Emacs.
12 ;; GNU Emacs is free software: you can redistribute it and/or modify
13 ;; it under the terms of the GNU General Public License as published by
14 ;; the Free Software Foundation, either version 3 of the License, or
15 ;; (at your option) any later version.
17 ;; GNU Emacs is distributed in the hope that it will be useful,
18 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
19 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 ;; GNU General Public License for more details.
22 ;; You should have received a copy of the GNU General Public License
23 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
25 ;;; Wishlist items (from widget.texi):
27 ;; * The `menu-choice' tag should be prettier, something like the
28 ;; abbreviated menus in Open Look.
30 ;; * Finish `:tab-order'.
32 ;; * Make indentation work with glyphs and proportional fonts.
34 ;; * Add commands to show overview of object and class hierarchies to
35 ;; the browser.
37 ;; * Find a way to disable mouse highlight for inactive widgets.
39 ;; * Find a way to make glyphs look inactive.
41 ;; * Add `key-binding' widget.
43 ;; * Add `widget' widget for editing widget specifications.
45 ;; * Find clean way to implement variable length list. See
46 ;; `TeX-printer-list' for an explanation.
48 ;; * `C-h' in `widget-prompt-value' should give type specific help.
50 ;; * A mailto widget. [This should work OK as a url-link if with
51 ;; browse-url-browser-function' set up appropriately.]
53 ;;; Commentary:
55 ;; See `widget.el'.
57 ;;; Code:
59 (defvar widget)
61 ;;; Compatibility.
63 (defun widget-event-point (event)
64 "Character position of the end of event if that exists, or nil."
65 (posn-point (event-end event)))
67 (defun widget-button-release-event-p (event)
68 "Non-nil if EVENT is a mouse-button-release event object."
69 (and (eventp event)
70 (memq (event-basic-type event) '(mouse-1 mouse-2 mouse-3))
71 (or (memq 'click (event-modifiers event))
72 (memq 'drag (event-modifiers event)))))
74 ;;; Customization.
76 (defgroup widgets nil
77 "Customization support for the Widget Library."
78 :link '(custom-manual "(widget)Top")
79 :link '(emacs-library-link :tag "Lisp File" "widget.el")
80 :prefix "widget-"
81 :group 'extensions)
83 (defgroup widget-documentation nil
84 "Options controlling the display of documentation strings."
85 :group 'widgets)
87 (defgroup widget-faces nil
88 "Faces used by the widget library."
89 :group 'widgets
90 :group 'faces)
92 (defvar widget-documentation-face 'widget-documentation
93 "Face used for documentation strings in widgets.
94 This exists as a variable so it can be set locally in certain buffers.")
96 (defface widget-documentation '((((class color)
97 (background dark))
98 (:foreground "lime green"))
99 (((class color)
100 (background light))
101 (:foreground "dark green"))
102 (t nil))
103 "Face used for documentation text."
104 :group 'widget-documentation
105 :group 'widget-faces)
106 (define-obsolete-face-alias 'widget-documentation-face
107 'widget-documentation "22.1")
109 (defvar widget-button-face 'widget-button
110 "Face used for buttons in widgets.
111 This exists as a variable so it can be set locally in certain buffers.")
113 (defface widget-button '((t (:weight bold)))
114 "Face used for widget buttons."
115 :group 'widget-faces)
116 (define-obsolete-face-alias 'widget-button-face 'widget-button "22.1")
118 (defcustom widget-mouse-face 'highlight
119 "Face used for widget buttons when the mouse is above them."
120 :type 'face
121 :group 'widget-faces)
123 ;; TTY gets special definitions here and in the next defface, because
124 ;; the gray colors defined for other displays cause black text on a black
125 ;; background, at least on light-background TTYs.
126 (defface widget-field '((((type tty))
127 :background "yellow3"
128 :foreground "black")
129 (((class grayscale color)
130 (background light))
131 :background "gray85")
132 (((class grayscale color)
133 (background dark))
134 :background "dim gray")
136 :slant italic))
137 "Face used for editable fields."
138 :group 'widget-faces)
139 (define-obsolete-face-alias 'widget-field-face 'widget-field "22.1")
141 (defface widget-single-line-field '((((type tty))
142 :background "green3"
143 :foreground "black")
144 (((class grayscale color)
145 (background light))
146 :background "gray85")
147 (((class grayscale color)
148 (background dark))
149 :background "dim gray")
151 :slant italic))
152 "Face used for editable fields spanning only a single line."
153 :group 'widget-faces)
154 (define-obsolete-face-alias 'widget-single-line-field-face
155 'widget-single-line-field "22.1")
157 ;;; This causes display-table to be loaded, and not usefully.
158 ;;;(defvar widget-single-line-display-table
159 ;;; (let ((table (make-display-table)))
160 ;;; (aset table 9 "^I")
161 ;;; (aset table 10 "^J")
162 ;;; table)
163 ;;; "Display table used for single-line editable fields.")
165 ;;;(when (fboundp 'set-face-display-table)
166 ;;; (set-face-display-table 'widget-single-line-field-face
167 ;;; widget-single-line-display-table))
169 ;;; Utility functions.
171 ;; These are not really widget specific.
173 (defun widget-princ-to-string (object)
174 "Return string representation of OBJECT, any Lisp object.
175 No quoting characters are used; no delimiters are printed around
176 the contents of strings."
177 (with-output-to-string
178 (princ object)))
180 (defun widget-clear-undo ()
181 "Clear all undo information."
182 (buffer-disable-undo (current-buffer))
183 (buffer-enable-undo))
185 (defcustom widget-menu-max-size 40
186 "Largest number of items allowed in a popup-menu.
187 Larger menus are read through the minibuffer."
188 :group 'widgets
189 :type 'integer)
191 (defcustom widget-menu-max-shortcuts 40
192 "Largest number of items for which it works to choose one with a character.
193 For a larger number of items, the minibuffer is used."
194 :group 'widgets
195 :type 'integer)
197 (defcustom widget-menu-minibuffer-flag nil
198 "Control how to ask for a choice from the keyboard.
199 Non-nil means use the minibuffer;
200 nil means read a single character."
201 :group 'widgets
202 :type 'boolean)
204 (defun widget-choose (title items &optional event)
205 "Choose an item from a list.
207 First argument TITLE is the name of the list.
208 Second argument ITEMS is a list whose members are either
209 (NAME . VALUE), to indicate selectable items, or just strings to
210 indicate unselectable items.
211 Optional third argument EVENT is an input event.
213 The user is asked to choose between each NAME from the items alist,
214 and the VALUE of the chosen element will be returned. If EVENT is a
215 mouse event, and the number of elements in items is less than
216 `widget-menu-max-size', a popup menu will be used, otherwise the
217 minibuffer."
218 (cond ((and (< (length items) widget-menu-max-size)
219 event (display-popup-menus-p))
220 ;; Mouse click.
221 (x-popup-menu event
222 (list title (cons "" items))))
223 ((or widget-menu-minibuffer-flag
224 (> (length items) widget-menu-max-shortcuts))
225 ;; Read the choice of name from the minibuffer.
226 (setq items (widget-remove-if 'stringp items))
227 (let ((val (completing-read (concat title ": ") items nil t)))
228 (if (stringp val)
229 (let ((try (try-completion val items)))
230 (when (stringp try)
231 (setq val try))
232 (cdr (assoc val items))))))
234 ;; Construct a menu of the choices
235 ;; and then use it for prompting for a single character.
236 (let* ((overriding-terminal-local-map (make-sparse-keymap))
237 (next-digit ?0)
238 map choice some-choice-enabled value)
239 ;; Define SPC as a prefix char to get to this menu.
240 (define-key overriding-terminal-local-map " "
241 (setq map (make-sparse-keymap title)))
242 (with-current-buffer (get-buffer-create " widget-choose")
243 (erase-buffer)
244 (insert "Available choices:\n\n")
245 (while items
246 (setq choice (car items) items (cdr items))
247 (if (consp choice)
248 (let* ((name (car choice))
249 (function (cdr choice)))
250 (insert (format "%c = %s\n" next-digit name))
251 (define-key map (vector next-digit) function)
252 (setq some-choice-enabled t)))
253 ;; Allocate digits to disabled alternatives
254 ;; so that the digit of a given alternative never varies.
255 (setq next-digit (1+ next-digit)))
256 (insert "\nC-g = Quit"))
257 (or some-choice-enabled
258 (error "None of the choices is currently meaningful"))
259 (define-key map [?\C-g] 'keyboard-quit)
260 (define-key map [t] 'keyboard-quit)
261 (define-key map [?\M-\C-v] 'scroll-other-window)
262 (define-key map [?\M--] 'negative-argument)
263 (setcdr map (nreverse (cdr map)))
264 ;; Read a char with the menu, and return the result
265 ;; that corresponds to it.
266 (save-window-excursion
267 (let ((buf (get-buffer " widget-choose")))
268 (fit-window-to-buffer (display-buffer buf))
269 (let ((cursor-in-echo-area t)
270 keys
271 (char 0)
272 (arg 1))
273 (while (not (or (and (integerp char)
274 (>= char ?0) (< char next-digit))
275 (eq value 'keyboard-quit)))
276 ;; Unread a SPC to lead to our new menu.
277 (setq unread-command-events (cons ?\s unread-command-events))
278 (setq keys (read-key-sequence title))
279 (setq value
280 (lookup-key overriding-terminal-local-map keys t)
281 char (aref keys 1))
282 (cond ((eq value 'scroll-other-window)
283 (let ((minibuffer-scroll-window
284 (get-buffer-window buf)))
285 (if (> 0 arg)
286 (scroll-other-window-down
287 (window-height minibuffer-scroll-window))
288 (scroll-other-window))
289 (setq arg 1)))
290 ((eq value 'negative-argument)
291 (setq arg -1))
293 (setq arg 1)))))))
294 (when (eq value 'keyboard-quit)
295 (error "Canceled"))
296 value))))
298 (defun widget-remove-if (predictate list)
299 (let (result (tail list))
300 (while tail
301 (or (funcall predictate (car tail))
302 (setq result (cons (car tail) result)))
303 (setq tail (cdr tail)))
304 (nreverse result)))
306 ;;; Widget text specifications.
308 ;; These functions are for specifying text properties.
310 ;; We can set it to nil now that get_local_map uses get_pos_property.
311 (defconst widget-field-add-space nil
312 "Non-nil means add extra space at the end of editable text fields.
313 If you don't add the space, it will become impossible to edit a zero
314 size field.")
316 (defvar widget-field-use-before-change t
317 "Non-nil means use `before-change-functions' to track editable fields.
318 This enables the use of undo, but doesn't work on Emacs 19.34 and earlier.
319 Using before hooks also means that the :notify function can't know the
320 new value.")
322 (defun widget-specify-field (widget from to)
323 "Specify editable button for WIDGET between FROM and TO."
324 ;; Terminating space is not part of the field, but necessary in
325 ;; order for local-map to work. Remove next sexp if local-map works
326 ;; at the end of the overlay.
327 (save-excursion
328 (goto-char to)
329 (cond ((null (widget-get widget :size))
330 (forward-char 1))
331 (widget-field-add-space
332 (insert-and-inherit " ")))
333 (setq to (point)))
334 (let ((keymap (widget-get widget :keymap))
335 (face (or (widget-get widget :value-face) 'widget-field))
336 (help-echo (widget-get widget :help-echo))
337 (follow-link (widget-get widget :follow-link))
338 (rear-sticky
339 (or (not widget-field-add-space) (widget-get widget :size))))
340 (if (functionp help-echo)
341 (setq help-echo 'widget-mouse-help))
342 (when (= (char-before to) ?\n)
343 ;; When the last character in the field is a newline, we want to
344 ;; give it a `field' char-property of `boundary', which helps the
345 ;; C-n/C-p act more naturally when entering/leaving the field. We
346 ;; do this by making a small secondary overlay to contain just that
347 ;; one character.
348 (let ((overlay (make-overlay (1- to) to nil t nil)))
349 (overlay-put overlay 'field 'boundary)
350 ;; We need the real field for tabbing.
351 (overlay-put overlay 'real-field widget)
352 ;; Use `local-map' here, not `keymap', so that normal editing
353 ;; works in the field when, say, Custom uses `suppress-keymap'.
354 (overlay-put overlay 'local-map keymap)
355 (overlay-put overlay 'face face)
356 (overlay-put overlay 'follow-link follow-link)
357 (overlay-put overlay 'help-echo help-echo))
358 (setq to (1- to))
359 (setq rear-sticky t))
360 (let ((overlay (make-overlay from to nil nil rear-sticky)))
361 (widget-put widget :field-overlay overlay)
362 ;;(overlay-put overlay 'detachable nil)
363 (overlay-put overlay 'field widget)
364 (overlay-put overlay 'local-map keymap)
365 (overlay-put overlay 'face face)
366 (overlay-put overlay 'follow-link follow-link)
367 (overlay-put overlay 'help-echo help-echo)))
368 (widget-specify-secret widget))
370 (defun widget-specify-secret (field)
371 "Replace text in FIELD with value of `:secret', if non-nil."
372 (let ((secret (widget-get field :secret))
373 (size (widget-get field :size)))
374 (when secret
375 (let ((begin (widget-field-start field))
376 (end (widget-field-end field)))
377 (when size
378 (while (and (> end begin)
379 (eq (char-after (1- end)) ?\s))
380 (setq end (1- end))))
381 (while (< begin end)
382 (let ((old (char-after begin)))
383 (unless (eq old secret)
384 (subst-char-in-region begin (1+ begin) old secret)
385 (put-text-property begin (1+ begin) 'secret old))
386 (setq begin (1+ begin))))))))
388 (defun widget-specify-button (widget from to)
389 "Specify button for WIDGET between FROM and TO."
390 (let ((overlay (make-overlay from to nil t nil))
391 (follow-link (widget-get widget :follow-link))
392 (help-echo (widget-get widget :help-echo)))
393 (widget-put widget :button-overlay overlay)
394 (if (functionp help-echo)
395 (setq help-echo 'widget-mouse-help))
396 (overlay-put overlay 'button widget)
397 (overlay-put overlay 'keymap (widget-get widget :keymap))
398 (overlay-put overlay 'evaporate t)
399 ;; We want to avoid the face with image buttons.
400 (unless (widget-get widget :suppress-face)
401 (overlay-put overlay 'face (widget-apply widget :button-face-get))
402 (overlay-put overlay 'mouse-face
403 ;; Make new list structure for the mouse-face value
404 ;; so that different widgets will have
405 ;; different `mouse-face' property values
406 ;; and will highlight separately.
407 (let ((mouse-face-value
408 (widget-apply widget :mouse-face-get)))
409 ;; If it's a list, copy it.
410 (if (listp mouse-face-value)
411 (copy-sequence mouse-face-value)
412 ;; If it's a symbol, put it in a list.
413 (list mouse-face-value)))))
414 (overlay-put overlay 'pointer 'hand)
415 (overlay-put overlay 'follow-link follow-link)
416 (overlay-put overlay 'help-echo help-echo)))
418 (defun widget-mouse-help (window overlay point)
419 "Help-echo callback for widgets whose :help-echo is a function."
420 (with-current-buffer (overlay-buffer overlay)
421 (let* ((widget (widget-at (overlay-start overlay)))
422 (help-echo (if widget (widget-get widget :help-echo))))
423 (if (functionp help-echo)
424 (funcall help-echo widget)
425 help-echo))))
427 (defun widget-specify-sample (widget from to)
428 "Specify sample for WIDGET between FROM and TO."
429 (let ((overlay (make-overlay from to nil t nil)))
430 (overlay-put overlay 'face (widget-apply widget :sample-face-get))
431 (overlay-put overlay 'evaporate t)
432 (widget-put widget :sample-overlay overlay)))
434 (defun widget-specify-doc (widget from to)
435 "Specify documentation for WIDGET between FROM and TO."
436 (let ((overlay (make-overlay from to nil t nil)))
437 (overlay-put overlay 'widget-doc widget)
438 (overlay-put overlay 'face widget-documentation-face)
439 (overlay-put overlay 'evaporate t)
440 (widget-put widget :doc-overlay overlay)))
442 (defmacro widget-specify-insert (&rest form)
443 "Execute FORM without inheriting any text properties."
444 `(save-restriction
445 (let ((inhibit-read-only t)
446 (inhibit-modification-hooks t))
447 (narrow-to-region (point) (point))
448 (prog1 (progn ,@form)
449 (goto-char (point-max))))))
451 (defface widget-inactive
452 '((t :inherit shadow))
453 "Face used for inactive widgets."
454 :group 'widget-faces)
455 (define-obsolete-face-alias 'widget-inactive-face
456 'widget-inactive "22.1")
458 (defun widget-specify-inactive (widget from to)
459 "Make WIDGET inactive for user modifications."
460 (unless (widget-get widget :inactive)
461 (let ((overlay (make-overlay from to nil t nil)))
462 (overlay-put overlay 'face 'widget-inactive)
463 ;; This is disabled, as it makes the mouse cursor change shape.
464 ;; (overlay-put overlay 'mouse-face 'widget-inactive)
465 (overlay-put overlay 'evaporate t)
466 (overlay-put overlay 'priority 100)
467 (overlay-put overlay 'modification-hooks '(widget-overlay-inactive))
468 (widget-put widget :inactive overlay))))
470 (defun widget-overlay-inactive (&rest junk)
471 "Ignoring the arguments, signal an error."
472 (unless inhibit-read-only
473 (error "The widget here is not active")))
476 (defun widget-specify-active (widget)
477 "Make WIDGET active for user modifications."
478 (let ((inactive (widget-get widget :inactive)))
479 (when inactive
480 (delete-overlay inactive)
481 (widget-put widget :inactive nil))))
483 ;;; Widget Properties.
485 (defsubst widget-type (widget)
486 "Return the type of WIDGET. The type is a symbol."
487 (car widget))
489 ;;;###autoload
490 (defun widgetp (widget)
491 "Return non-nil if WIDGET is a widget."
492 (if (symbolp widget)
493 (get widget 'widget-type)
494 (and (consp widget)
495 (symbolp (car widget))
496 (get (car widget) 'widget-type))))
498 (defun widget-get-indirect (widget property)
499 "In WIDGET, get the value of PROPERTY.
500 If the value is a symbol, return its binding.
501 Otherwise, just return the value."
502 (let ((value (widget-get widget property)))
503 (if (symbolp value)
504 (symbol-value value)
505 value)))
507 (defun widget-member (widget property)
508 "Non-nil if there is a definition in WIDGET for PROPERTY."
509 (cond ((plist-member (cdr widget) property)
511 ((car widget)
512 (widget-member (get (car widget) 'widget-type) property))
513 (t nil)))
515 (defun widget-value (widget)
516 "Extract the current value of WIDGET."
517 (widget-apply widget
518 :value-to-external (widget-apply widget :value-get)))
520 (defun widget-value-set (widget value)
521 "Set the current value of WIDGET to VALUE."
522 (widget-apply widget
523 :value-set (widget-apply widget
524 :value-to-internal value)))
526 (defun widget-default-get (widget)
527 "Extract the default external value of WIDGET."
528 (widget-apply widget :value-to-external
529 (or (widget-get widget :value)
530 (widget-apply widget :default-get))))
532 (defun widget-match-inline (widget vals)
533 "In WIDGET, match the start of VALS."
534 (cond ((widget-get widget :inline)
535 (widget-apply widget :match-inline vals))
536 ((and (listp vals)
537 (widget-apply widget :match (car vals)))
538 (cons (list (car vals)) (cdr vals)))
539 (t nil)))
541 (defun widget-apply-action (widget &optional event)
542 "Apply :action in WIDGET in response to EVENT."
543 (if (widget-apply widget :active)
544 (widget-apply widget :action event)
545 (error "Attempt to perform action on inactive widget")))
547 ;;; Helper functions.
549 ;; These are widget specific.
551 ;;;###autoload
552 (defun widget-prompt-value (widget prompt &optional value unbound)
553 "Prompt for a value matching WIDGET, using PROMPT.
554 The current value is assumed to be VALUE, unless UNBOUND is non-nil."
555 (unless (listp widget)
556 (setq widget (list widget)))
557 (setq prompt (format "[%s] %s" (widget-type widget) prompt))
558 (setq widget (widget-convert widget))
559 (let ((answer (widget-apply widget :prompt-value prompt value unbound)))
560 (unless (widget-apply widget :match answer)
561 (error "Value does not match %S type" (car widget)))
562 answer))
564 (defun widget-get-sibling (widget)
565 "Get the item WIDGET is assumed to toggle.
566 This is only meaningful for radio buttons or checkboxes in a list."
567 (let* ((children (widget-get (widget-get widget :parent) :children))
568 child)
569 (catch 'child
570 (while children
571 (setq child (car children)
572 children (cdr children))
573 (when (eq (widget-get child :button) widget)
574 (throw 'child child)))
575 nil)))
577 (defun widget-map-buttons (function &optional buffer maparg)
578 "Map FUNCTION over the buttons in BUFFER.
579 FUNCTION is called with the arguments WIDGET and MAPARG.
581 If FUNCTION returns non-nil, the walk is cancelled.
583 The arguments MAPARG, and BUFFER default to nil and (current-buffer),
584 respectively."
585 (let ((cur (point-min))
586 (widget nil)
587 (overlays (if buffer
588 (with-current-buffer buffer (overlay-lists))
589 (overlay-lists))))
590 (setq overlays (append (car overlays) (cdr overlays)))
591 (while (setq cur (pop overlays))
592 (setq widget (overlay-get cur 'button))
593 (if (and widget (funcall function widget maparg))
594 (setq overlays nil)))))
596 ;;; Images.
598 (defcustom widget-image-directory (file-name-as-directory
599 (expand-file-name "images/custom" data-directory))
600 "Where widget button images are located.
601 If this variable is nil, widget will try to locate the directory
602 automatically."
603 :group 'widgets
604 :type 'directory)
606 (defcustom widget-image-enable t
607 "If non-nil, use image buttons in widgets when available."
608 :version "21.1"
609 :group 'widgets
610 :type 'boolean)
612 (defcustom widget-image-conversion
613 '((xpm ".xpm") (gif ".gif") (png ".png") (jpeg ".jpg" ".jpeg")
614 (xbm ".xbm"))
615 "Conversion alist from image formats to file name suffixes."
616 :group 'widgets
617 :type '(repeat (cons :format "%v"
618 (symbol :tag "Image Format" unknown)
619 (repeat :tag "Suffixes"
620 (string :format "%v")))))
622 (defun widget-image-find (image)
623 "Create a graphical button from IMAGE.
624 IMAGE should either already be an image, or be a file name sans
625 extension (xpm, xbm, gif, jpg, or png) located in
626 `widget-image-directory' or otherwise where `find-image' will find it."
627 (cond ((not (and image widget-image-enable (display-graphic-p)))
628 ;; We don't want or can't use images.
629 nil)
630 ((and (consp image)
631 (eq 'image (car image)))
632 ;; Already an image spec. Use it.
633 image)
634 ((stringp image)
635 ;; A string. Look it up in relevant directories.
636 (let* ((load-path (cons widget-image-directory load-path))
637 specs)
638 (dolist (elt widget-image-conversion)
639 (dolist (ext (cdr elt))
640 (push (list :type (car elt) :file (concat image ext)) specs)))
641 (find-image (nreverse specs))))
643 ;; Oh well.
644 nil)))
646 (defvar widget-button-pressed-face 'widget-button-pressed
647 "Face used for pressed buttons in widgets.
648 This exists as a variable so it can be set locally in certain
649 buffers.")
651 (defun widget-image-insert (widget tag image &optional down inactive)
652 "In WIDGET, insert the text TAG or, if supported, IMAGE.
653 IMAGE should either be an image or an image file name sans extension
654 \(xpm, xbm, gif, jpg, or png) located in `widget-image-directory'.
656 Optional arguments DOWN and INACTIVE are used instead of IMAGE when the
657 button is pressed or inactive, respectively. These are currently ignored."
658 (if (and (featurep 'image)
659 (setq image (widget-image-find image)))
660 (progn (widget-put widget :suppress-face t)
661 (insert-image image tag))
662 (insert tag)))
664 (defun widget-move-and-invoke (event)
665 "Move to where you click, and if it is an active field, invoke it."
666 (interactive "e")
667 (mouse-set-point event)
668 (let ((pos (widget-event-point event)))
669 (if (and pos (get-char-property pos 'button))
670 (widget-button-click event))))
672 ;;; Buttons.
674 (defgroup widget-button nil
675 "The look of various kinds of buttons."
676 :group 'widgets)
678 (defcustom widget-button-prefix ""
679 "String used as prefix for buttons."
680 :type 'string
681 :group 'widget-button)
683 (defcustom widget-button-suffix ""
684 "String used as suffix for buttons."
685 :type 'string
686 :group 'widget-button)
688 ;;; Creating Widgets.
690 ;;;###autoload
691 (defun widget-create (type &rest args)
692 "Create widget of TYPE.
693 The optional ARGS are additional keyword arguments."
694 (let ((widget (apply 'widget-convert type args)))
695 (widget-apply widget :create)
696 widget))
698 (defun widget-create-child-and-convert (parent type &rest args)
699 "As part of the widget PARENT, create a child widget TYPE.
700 The child is converted, using the keyword arguments ARGS."
701 (let ((widget (apply 'widget-convert type args)))
702 (widget-put widget :parent parent)
703 (unless (widget-get widget :indent)
704 (widget-put widget :indent (+ (or (widget-get parent :indent) 0)
705 (or (widget-get widget :extra-offset) 0)
706 (widget-get parent :offset))))
707 (widget-apply widget :create)
708 widget))
710 (defun widget-create-child (parent type)
711 "Create widget of TYPE."
712 (let ((widget (widget-copy type)))
713 (widget-put widget :parent parent)
714 (unless (widget-get widget :indent)
715 (widget-put widget :indent (+ (or (widget-get parent :indent) 0)
716 (or (widget-get widget :extra-offset) 0)
717 (widget-get parent :offset))))
718 (widget-apply widget :create)
719 widget))
721 (defun widget-create-child-value (parent type value)
722 "Create widget of TYPE with value VALUE."
723 (let ((widget (widget-copy type)))
724 (widget-put widget :value (widget-apply widget :value-to-internal value))
725 (widget-put widget :parent parent)
726 (unless (widget-get widget :indent)
727 (widget-put widget :indent (+ (or (widget-get parent :indent) 0)
728 (or (widget-get widget :extra-offset) 0)
729 (widget-get parent :offset))))
730 (widget-apply widget :create)
731 widget))
733 ;;;###autoload
734 (defun widget-delete (widget)
735 "Delete WIDGET."
736 (widget-apply widget :delete))
738 (defun widget-copy (widget)
739 "Make a deep copy of WIDGET."
740 (widget-apply (copy-sequence widget) :copy))
742 (defun widget-convert (type &rest args)
743 "Convert TYPE to a widget without inserting it in the buffer.
744 The optional ARGS are additional keyword arguments."
745 ;; Don't touch the type.
746 (let* ((widget (if (symbolp type)
747 (list type)
748 (copy-sequence type)))
749 (current widget)
750 done
751 (keys args))
752 ;; First set the :args keyword.
753 (while (cdr current) ;Look in the type.
754 (if (and (keywordp (cadr current))
755 ;; If the last element is a keyword,
756 ;; it is still the :args element,
757 ;; even though it is a keyword.
758 (cddr current))
759 (if (eq (cadr current) :args)
760 ;; If :args is explicitly specified, obey it.
761 (setq current nil)
762 ;; Some other irrelevant keyword.
763 (setq current (cdr (cdr current))))
764 (setcdr current (list :args (cdr current)))
765 (setq current nil)))
766 (while (and args (not done)) ;Look in ARGS.
767 (cond ((eq (car args) :args)
768 ;; Handle explicit specification of :args.
769 (setq args (cadr args)
770 done t))
771 ((keywordp (car args))
772 (setq args (cddr args)))
773 (t (setq done t))))
774 (when done
775 (widget-put widget :args args))
776 ;; Then Convert the widget.
777 (setq type widget)
778 (while type
779 (let ((convert-widget (plist-get (cdr type) :convert-widget)))
780 (if convert-widget
781 (setq widget (funcall convert-widget widget))))
782 (setq type (get (car type) 'widget-type)))
783 ;; Finally set the keyword args.
784 (while keys
785 (let ((next (nth 0 keys)))
786 (if (keywordp next)
787 (progn
788 (widget-put widget next (nth 1 keys))
789 (setq keys (nthcdr 2 keys)))
790 (setq keys nil))))
791 ;; Convert the :value to internal format.
792 (if (widget-member widget :value)
793 (widget-put widget
794 :value (widget-apply widget
795 :value-to-internal
796 (widget-get widget :value))))
797 ;; Return the newly create widget.
798 widget))
800 ;;;###autoload
801 (defun widget-insert (&rest args)
802 "Call `insert' with ARGS even if surrounding text is read only."
803 (let ((inhibit-read-only t)
804 (inhibit-modification-hooks t))
805 (apply 'insert args)))
807 (defun widget-convert-text (type from to
808 &optional button-from button-to
809 &rest args)
810 "Return a widget of type TYPE with endpoint FROM TO.
811 No text will be inserted to the buffer, instead the text between FROM
812 and TO will be used as the widgets end points. If optional arguments
813 BUTTON-FROM and BUTTON-TO are given, these will be used as the widgets
814 button end points.
815 Optional ARGS are extra keyword arguments for TYPE."
816 (let ((widget (apply 'widget-convert type :delete 'widget-leave-text args))
817 (from (copy-marker from))
818 (to (copy-marker to)))
819 (set-marker-insertion-type from t)
820 (set-marker-insertion-type to nil)
821 (widget-put widget :from from)
822 (widget-put widget :to to)
823 (when button-from
824 (widget-specify-button widget button-from button-to))
825 widget))
827 (defun widget-convert-button (type from to &rest args)
828 "Return a widget of type TYPE with endpoint FROM TO.
829 Optional ARGS are extra keyword arguments for TYPE.
830 No text will be inserted to the buffer, instead the text between FROM
831 and TO will be used as the widgets end points, as well as the widgets
832 button end points."
833 (apply 'widget-convert-text type from to from to args))
835 (defun widget-leave-text (widget)
836 "Remove markers and overlays from WIDGET and its children."
837 (let ((button (widget-get widget :button-overlay))
838 (sample (widget-get widget :sample-overlay))
839 (doc (widget-get widget :doc-overlay))
840 (field (widget-get widget :field-overlay)))
841 (set-marker (widget-get widget :from) nil)
842 (set-marker (widget-get widget :to) nil)
843 (when button
844 (delete-overlay button))
845 (when sample
846 (delete-overlay sample))
847 (when doc
848 (delete-overlay doc))
849 (when field
850 (delete-overlay field))
851 (mapc 'widget-leave-text (widget-get widget :children))))
853 ;;; Keymap and Commands.
855 ;; This alias exists only so that one can choose in doc-strings (e.g.
856 ;; Custom-mode) which key-binding of widget-keymap one wants to refer to.
857 ;; http://lists.gnu.org/archive/html/emacs-devel/2008-11/msg00480.html
858 (define-obsolete-function-alias 'advertised-widget-backward
859 'widget-backward "23.2")
861 ;;;###autoload
862 (defvar widget-keymap
863 (let ((map (make-sparse-keymap)))
864 (define-key map "\t" 'widget-forward)
865 (define-key map "\e\t" 'widget-backward)
866 (define-key map [(shift tab)] 'widget-backward)
867 (put 'widget-backward :advertised-binding [(shift tab)])
868 (define-key map [backtab] 'widget-backward)
869 (define-key map [down-mouse-2] 'widget-button-click)
870 (define-key map [down-mouse-1] 'widget-button-click)
871 ;; The following definition needs to avoid using escape sequences that
872 ;; might get converted to ^M when building loaddefs.el
873 (define-key map [(control ?m)] 'widget-button-press)
874 map)
875 "Keymap containing useful binding for buffers containing widgets.
876 Recommended as a parent keymap for modes using widgets.
877 Note that such modes will need to require wid-edit.")
879 (defvar widget-global-map global-map
880 "Keymap used for events a widget does not handle itself.")
881 (make-variable-buffer-local 'widget-global-map)
883 (defvar widget-field-keymap
884 (let ((map (copy-keymap widget-keymap)))
885 (define-key map "\C-k" 'widget-kill-line)
886 (define-key map "\M-\t" 'widget-complete)
887 (define-key map "\C-m" 'widget-field-activate)
888 ;; Since the widget code uses a `field' property to identify fields,
889 ;; ordinary beginning-of-line does the right thing.
890 ;; (define-key map "\C-a" 'widget-beginning-of-line)
891 (define-key map "\C-e" 'widget-end-of-line)
892 map)
893 "Keymap used inside an editable field.")
895 (defvar widget-text-keymap
896 (let ((map (copy-keymap widget-keymap)))
897 ;; Since the widget code uses a `field' property to identify fields,
898 ;; ordinary beginning-of-line does the right thing.
899 ;; (define-key map "\C-a" 'widget-beginning-of-line)
900 (define-key map "\C-e" 'widget-end-of-line)
901 map)
902 "Keymap used inside a text field.")
904 (defun widget-field-activate (pos &optional event)
905 "Invoke the editable field at point."
906 (interactive "@d")
907 (let ((field (widget-field-at pos)))
908 (if field
909 (widget-apply-action field event)
910 (call-interactively
911 (lookup-key widget-global-map (this-command-keys))))))
913 (defface widget-button-pressed
914 '((((min-colors 88) (class color))
915 (:foreground "red1"))
916 (((class color))
917 (:foreground "red"))
919 (:weight bold :underline t)))
920 "Face used for pressed buttons."
921 :group 'widget-faces)
922 (define-obsolete-face-alias 'widget-button-pressed-face
923 'widget-button-pressed "22.1")
925 (defvar widget-button-click-moves-point nil
926 "If non-nil, `widget-button-click' moves point to a button after invoking it.
927 If nil, point returns to its original position after invoking a button.")
929 (defun widget-button-click (event)
930 "Invoke the button that the mouse is pointing at."
931 (interactive "e")
932 (if (widget-event-point event)
933 (let* ((oevent event)
934 (mouse-1 (memq (event-basic-type event) '(mouse-1 down-mouse-1)))
935 (pos (widget-event-point event))
936 (start (event-start event))
937 (button (get-char-property
938 pos 'button (and (windowp (posn-window start))
939 (window-buffer (posn-window start)))))
940 newpoint)
941 (when (or (null button)
942 (catch 'button-press-cancelled
943 ;; Mouse click on a widget button. Do the following
944 ;; in a save-excursion so that the click on the button
945 ;; doesn't change point.
946 (save-selected-window
947 (select-window (posn-window (event-start event)))
948 (save-excursion
949 (goto-char (posn-point (event-start event)))
950 (let* ((overlay (widget-get button :button-overlay))
951 (pressed-face (or (widget-get button :pressed-face)
952 widget-button-pressed-face))
953 (face (overlay-get overlay 'face))
954 (mouse-face (overlay-get overlay 'mouse-face)))
955 (unwind-protect
956 ;; Read events, including mouse-movement
957 ;; events, waiting for a release event. If we
958 ;; began with a mouse-1 event and receive a
959 ;; movement event, that means the user wants
960 ;; to perform drag-selection, so cancel the
961 ;; button press and do the default mouse-1
962 ;; action. For mouse-2, just highlight/
963 ;; unhighlight the button the mouse was
964 ;; initially on when we move over it.
965 (save-excursion
966 (when face ; avoid changing around image
967 (overlay-put overlay 'face pressed-face)
968 (overlay-put overlay 'mouse-face pressed-face))
969 (unless (widget-apply button :mouse-down-action event)
970 (let ((track-mouse t))
971 (while (not (widget-button-release-event-p event))
972 (setq event (read-event))
973 (when (and mouse-1 (mouse-movement-p event))
974 (push event unread-command-events)
975 (setq event oevent)
976 (throw 'button-press-cancelled t))
977 (unless (or (integerp event)
978 (memq (car event) '(switch-frame select-window))
979 (eq (car event) 'scroll-bar-movement))
980 (setq pos (widget-event-point event))
981 (if (and pos
982 (eq (get-char-property pos 'button)
983 button))
984 (when face
985 (overlay-put overlay 'face pressed-face)
986 (overlay-put overlay 'mouse-face pressed-face))
987 (overlay-put overlay 'face face)
988 (overlay-put overlay 'mouse-face mouse-face))))))
990 ;; When mouse is released over the button, run
991 ;; its action function.
992 (when (and pos (eq (get-char-property pos 'button) button))
993 (goto-char pos)
994 (widget-apply-action button event)
995 (if widget-button-click-moves-point
996 (setq newpoint (point)))))
997 (overlay-put overlay 'face face)
998 (overlay-put overlay 'mouse-face mouse-face))))
1000 (if newpoint (goto-char newpoint))
1001 ;; This loses if the widget action switches windows. -- cyd
1002 ;; (unless (pos-visible-in-window-p (widget-event-point event))
1003 ;; (mouse-set-point event)
1004 ;; (beginning-of-line)
1005 ;; (recenter))
1007 nil))
1008 (let ((up t) command)
1009 ;; Mouse click not on a widget button. Find the global
1010 ;; command to run, and check whether it is bound to an
1011 ;; up event.
1012 (if mouse-1
1013 (cond ((setq command ;down event
1014 (lookup-key widget-global-map [down-mouse-1]))
1015 (setq up nil))
1016 ((setq command ;up event
1017 (lookup-key widget-global-map [mouse-1]))))
1018 (cond ((setq command ;down event
1019 (lookup-key widget-global-map [down-mouse-2]))
1020 (setq up nil))
1021 ((setq command ;up event
1022 (lookup-key widget-global-map [mouse-2])))))
1023 (when up
1024 ;; Don't execute up events twice.
1025 (while (not (widget-button-release-event-p event))
1026 (setq event (read-event))))
1027 (when command
1028 (call-interactively command)))))
1029 (message "You clicked somewhere weird.")))
1031 (defun widget-button-press (pos &optional event)
1032 "Invoke button at POS."
1033 (interactive "@d")
1034 (let ((button (get-char-property pos 'button)))
1035 (if button
1036 (widget-apply-action button event)
1037 (let ((command (lookup-key widget-global-map (this-command-keys))))
1038 (when (commandp command)
1039 (call-interactively command))))))
1041 (defun widget-tabable-at (&optional pos)
1042 "Return the tabable widget at POS, or nil.
1043 POS defaults to the value of (point)."
1044 (let ((widget (widget-at pos)))
1045 (if widget
1046 (let ((order (widget-get widget :tab-order)))
1047 (if order
1048 (if (>= order 0)
1049 widget)
1050 widget)))))
1052 (defvar widget-use-overlay-change t
1053 "If non-nil, use overlay change functions to tab around in the buffer.
1054 This is much faster, but doesn't work reliably on Emacs 19.34.")
1056 (defun widget-move (arg)
1057 "Move point to the ARG next field or button.
1058 ARG may be negative to move backward."
1059 (or (bobp) (> arg 0) (backward-char))
1060 (let ((wrapped 0)
1061 (number arg)
1062 (old (widget-tabable-at)))
1063 ;; Forward.
1064 (while (> arg 0)
1065 (cond ((eobp)
1066 (goto-char (point-min))
1067 (setq wrapped (1+ wrapped)))
1068 (widget-use-overlay-change
1069 (goto-char (next-overlay-change (point))))
1071 (forward-char 1)))
1072 (and (= wrapped 2)
1073 (eq arg number)
1074 (error "No buttons or fields found"))
1075 (let ((new (widget-tabable-at)))
1076 (when new
1077 (unless (eq new old)
1078 (setq arg (1- arg))
1079 (setq old new)))))
1080 ;; Backward.
1081 (while (< arg 0)
1082 (cond ((bobp)
1083 (goto-char (point-max))
1084 (setq wrapped (1+ wrapped)))
1085 (widget-use-overlay-change
1086 (goto-char (previous-overlay-change (point))))
1088 (backward-char 1)))
1089 (and (= wrapped 2)
1090 (eq arg number)
1091 (error "No buttons or fields found"))
1092 (let ((new (widget-tabable-at)))
1093 (when new
1094 (unless (eq new old)
1095 (setq arg (1+ arg))))))
1096 (let ((new (widget-tabable-at)))
1097 (while (eq (widget-tabable-at) new)
1098 (backward-char)))
1099 (forward-char))
1100 (widget-echo-help (point))
1101 (run-hooks 'widget-move-hook))
1103 (defun widget-forward (arg)
1104 "Move point to the next field or button.
1105 With optional ARG, move across that many fields."
1106 (interactive "p")
1107 (run-hooks 'widget-forward-hook)
1108 (widget-move arg))
1110 (defun widget-backward (arg)
1111 "Move point to the previous field or button.
1112 With optional ARG, move across that many fields."
1113 (interactive "p")
1114 (run-hooks 'widget-backward-hook)
1115 (widget-move (- arg)))
1117 ;; Since the widget code uses a `field' property to identify fields,
1118 ;; ordinary beginning-of-line does the right thing.
1119 (defalias 'widget-beginning-of-line 'beginning-of-line)
1121 (defun widget-end-of-line ()
1122 "Go to end of field or end of line, whichever is first.
1123 Trailing spaces at the end of padded fields are not considered part of
1124 the field."
1125 (interactive)
1126 ;; Ordinary end-of-line does the right thing, because we're inside
1127 ;; text with a `field' property.
1128 (end-of-line)
1129 (unless (eolp)
1130 ;; ... except that we want to ignore trailing spaces in fields that
1131 ;; aren't terminated by a newline, because they are used as padding,
1132 ;; and ignored when extracting the entered value of the field.
1133 (skip-chars-backward " " (field-beginning (1- (point))))))
1135 (defun widget-kill-line ()
1136 "Kill to end of field or end of line, whichever is first."
1137 (interactive)
1138 (let* ((field (widget-field-find (point)))
1139 (end (and field (widget-field-end field))))
1140 (if (and field (> (line-beginning-position 2) end))
1141 (kill-region (point) end)
1142 (call-interactively 'kill-line))))
1144 (defcustom widget-complete-field (lookup-key global-map "\M-\t")
1145 "Default function to call for completion inside fields."
1146 :options '(ispell-complete-word complete-tag lisp-complete-symbol)
1147 :type 'function
1148 :group 'widgets)
1150 (defun widget-narrow-to-field ()
1151 "Narrow to field."
1152 (interactive)
1153 (let ((field (widget-field-find (point))))
1154 (if field
1155 (narrow-to-region (line-beginning-position) (line-end-position)))))
1157 (defun widget-complete ()
1158 "Complete content of editable field from point.
1159 When not inside a field, move to the previous button or field."
1160 (interactive)
1161 (let ((field (widget-field-find (point))))
1162 (when field
1163 (widget-apply field :complete))
1164 (error "Not in an editable field")))
1166 ;;; Setting up the buffer.
1168 (defvar widget-field-new nil
1169 "List of all newly created editable fields in the buffer.")
1170 (make-variable-buffer-local 'widget-field-new)
1172 (defvar widget-field-list nil
1173 "List of all editable fields in the buffer.")
1174 (make-variable-buffer-local 'widget-field-list)
1176 (defun widget-at (&optional pos)
1177 "The button or field at POS (default, point)."
1178 (or (get-char-property (or pos (point)) 'button)
1179 (widget-field-at pos)))
1181 ;;;###autoload
1182 (defun widget-setup ()
1183 "Setup current buffer so editing string widgets works."
1184 (let ((inhibit-read-only t)
1185 (inhibit-modification-hooks t)
1186 field)
1187 (while widget-field-new
1188 (setq field (car widget-field-new)
1189 widget-field-new (cdr widget-field-new)
1190 widget-field-list (cons field widget-field-list))
1191 (let ((from (car (widget-get field :field-overlay)))
1192 (to (cdr (widget-get field :field-overlay))))
1193 (widget-specify-field field
1194 (marker-position from) (marker-position to))
1195 (set-marker from nil)
1196 (set-marker to nil))))
1197 (widget-clear-undo)
1198 (widget-add-change))
1200 (defvar widget-field-last nil)
1201 ;; Last field containing point.
1202 (make-variable-buffer-local 'widget-field-last)
1204 (defvar widget-field-was nil)
1205 ;; The widget data before the change.
1206 (make-variable-buffer-local 'widget-field-was)
1208 (defun widget-field-at (pos)
1209 "Return the widget field at POS, or nil if none."
1210 (let ((field (get-char-property (or pos (point)) 'field)))
1211 (if (eq field 'boundary)
1212 (get-char-property (or pos (point)) 'real-field)
1213 field)))
1215 (defun widget-field-buffer (widget)
1216 "Return the buffer of WIDGET's editing field."
1217 (let ((overlay (widget-get widget :field-overlay)))
1218 (cond ((overlayp overlay)
1219 (overlay-buffer overlay))
1220 ((consp overlay)
1221 (marker-buffer (car overlay))))))
1223 (defun widget-field-start (widget)
1224 "Return the start of WIDGET's editing field."
1225 (let ((overlay (widget-get widget :field-overlay)))
1226 (if (overlayp overlay)
1227 (overlay-start overlay)
1228 (car overlay))))
1230 (defun widget-field-end (widget)
1231 "Return the end of WIDGET's editing field."
1232 (let ((overlay (widget-get widget :field-overlay)))
1233 ;; Don't subtract one if local-map works at the end of the overlay,
1234 ;; or if a special `boundary' field has been added after the widget
1235 ;; field.
1236 (if (overlayp overlay)
1237 ;; Don't proceed if overlay has been removed from buffer.
1238 (when (overlay-buffer overlay)
1239 (if (and (not (eq (with-current-buffer
1240 (widget-field-buffer widget)
1241 (save-restriction
1242 ;; `widget-narrow-to-field' can be
1243 ;; active when this function is called
1244 ;; from an change-functions hook. So
1245 ;; temporarily remove field narrowing
1246 ;; before to call `get-char-property'.
1247 (widen)
1248 (get-char-property (overlay-end overlay)
1249 'field)))
1250 'boundary))
1251 (or widget-field-add-space
1252 (null (widget-get widget :size))))
1253 (1- (overlay-end overlay))
1254 (overlay-end overlay)))
1255 (cdr overlay))))
1257 (defun widget-field-text-end (widget)
1258 (let ((to (widget-field-end widget))
1259 (size (widget-get widget :size)))
1260 (if (or (null size) (zerop size))
1262 (let ((from (widget-field-start widget)))
1263 (if (and from to)
1264 (with-current-buffer (widget-field-buffer widget)
1265 (while (and (> to from)
1266 (eq (char-after (1- to)) ?\s))
1267 (setq to (1- to)))
1268 to))))))
1270 (defun widget-field-find (pos)
1271 "Return the field at POS.
1272 Unlike (get-char-property POS 'field), this works with empty fields too."
1273 (let ((fields widget-field-list)
1274 field found)
1275 (while fields
1276 (setq field (car fields)
1277 fields (cdr fields))
1278 (when (and (<= (widget-field-start field) pos)
1279 (<= pos (widget-field-end field)))
1280 (when found
1281 (error "Overlapping fields"))
1282 (setq found field)))
1283 found))
1285 (defun widget-before-change (from to)
1286 ;; This is how, for example, a variable changes its state to `modified'.
1287 ;; when it is being edited.
1288 (unless inhibit-read-only
1289 (let ((from-field (widget-field-find from))
1290 (to-field (widget-field-find to)))
1291 (cond ((not (eq from-field to-field))
1292 (add-hook 'post-command-hook 'widget-add-change nil t)
1293 (signal 'text-read-only
1294 '("Change should be restricted to a single field")))
1295 ((null from-field)
1296 (add-hook 'post-command-hook 'widget-add-change nil t)
1297 (signal 'text-read-only
1298 '("Attempt to change text outside editable field")))
1299 (widget-field-use-before-change
1300 (widget-apply from-field :notify from-field))))))
1302 (defun widget-add-change ()
1303 (remove-hook 'post-command-hook 'widget-add-change t)
1304 (add-hook 'before-change-functions 'widget-before-change nil t)
1305 (add-hook 'after-change-functions 'widget-after-change nil t))
1307 (defun widget-after-change (from to old)
1308 "Adjust field size and text properties."
1309 (let ((field (widget-field-find from))
1310 (other (widget-field-find to)))
1311 (when field
1312 (unless (eq field other)
1313 (error "Change in different fields"))
1314 (let ((size (widget-get field :size)))
1315 (when size
1316 (let ((begin (widget-field-start field))
1317 (end (widget-field-end field)))
1318 (cond ((< (- end begin) size)
1319 ;; Field too small.
1320 (save-excursion
1321 (goto-char end)
1322 (insert-char ?\s (- (+ begin size) end))))
1323 ((> (- end begin) size)
1324 ;; Field too large and
1325 (if (or (< (point) (+ begin size))
1326 (> (point) end))
1327 ;; Point is outside extra space.
1328 (setq begin (+ begin size))
1329 ;; Point is within the extra space.
1330 (setq begin (point)))
1331 (save-excursion
1332 (goto-char end)
1333 (while (and (eq (preceding-char) ?\s)
1334 (> (point) begin))
1335 (delete-backward-char 1)))))))
1336 (widget-specify-secret field))
1337 (widget-apply field :notify field))))
1339 ;;; Widget Functions
1341 ;; These functions are used in the definition of multiple widgets.
1343 (defun widget-parent-action (widget &optional event)
1344 "Tell :parent of WIDGET to handle the :action.
1345 Optional EVENT is the event that triggered the action."
1346 (widget-apply (widget-get widget :parent) :action event))
1348 (defun widget-children-value-delete (widget)
1349 "Delete all :children and :buttons in WIDGET."
1350 (mapc 'widget-delete (widget-get widget :children))
1351 (widget-put widget :children nil)
1352 (mapc 'widget-delete (widget-get widget :buttons))
1353 (widget-put widget :buttons nil))
1355 (defun widget-children-validate (widget)
1356 "All the :children must be valid."
1357 (let ((children (widget-get widget :children))
1358 child found)
1359 (while (and children (not found))
1360 (setq child (car children)
1361 children (cdr children)
1362 found (widget-apply child :validate)))
1363 found))
1365 (defun widget-child-value-get (widget)
1366 "Get the value of the first member of :children in WIDGET."
1367 (widget-value (car (widget-get widget :children))))
1369 (defun widget-child-value-inline (widget)
1370 "Get the inline value of the first member of :children in WIDGET."
1371 (widget-apply (car (widget-get widget :children)) :value-inline))
1373 (defun widget-child-validate (widget)
1374 "The result of validating the first member of :children in WIDGET."
1375 (widget-apply (car (widget-get widget :children)) :validate))
1377 (defun widget-type-value-create (widget)
1378 "Convert and instantiate the value of the :type attribute of WIDGET.
1379 Store the newly created widget in the :children attribute.
1381 The value of the :type attribute should be an unconverted widget type."
1382 (let ((value (widget-get widget :value))
1383 (type (widget-get widget :type)))
1384 (widget-put widget :children
1385 (list (widget-create-child-value widget
1386 (widget-convert type)
1387 value)))))
1389 (defun widget-type-default-get (widget)
1390 "Get default value from the :type attribute of WIDGET.
1392 The value of the :type attribute should be an unconverted widget type."
1393 (widget-default-get (widget-convert (widget-get widget :type))))
1395 (defun widget-type-match (widget value)
1396 "Non-nil if the :type value of WIDGET matches VALUE.
1398 The value of the :type attribute should be an unconverted widget type."
1399 (widget-apply (widget-convert (widget-get widget :type)) :match value))
1401 (defun widget-types-copy (widget)
1402 "Copy :args as widget types in WIDGET."
1403 (widget-put widget :args (mapcar 'widget-copy (widget-get widget :args)))
1404 widget)
1406 ;; Made defsubst to speed up face editor creation.
1407 (defsubst widget-types-convert-widget (widget)
1408 "Convert :args as widget types in WIDGET."
1409 (widget-put widget :args (mapcar 'widget-convert (widget-get widget :args)))
1410 widget)
1412 (defun widget-value-convert-widget (widget)
1413 "Initialize :value from :args in WIDGET."
1414 (let ((args (widget-get widget :args)))
1415 (when args
1416 (widget-put widget :value (car args))
1417 ;; Don't convert :value here, as this is done in `widget-convert'.
1418 ;; (widget-put widget :value (widget-apply widget
1419 ;; :value-to-internal (car args)))
1420 (widget-put widget :args nil)))
1421 widget)
1423 (defun widget-value-value-get (widget)
1424 "Return the :value property of WIDGET."
1425 (widget-get widget :value))
1427 ;;; The `default' Widget.
1429 (define-widget 'default nil
1430 "Basic widget other widgets are derived from."
1431 :value-to-internal (lambda (widget value) value)
1432 :value-to-external (lambda (widget value) value)
1433 :button-prefix 'widget-button-prefix
1434 :button-suffix 'widget-button-suffix
1435 :complete 'widget-default-complete
1436 :create 'widget-default-create
1437 :indent nil
1438 :offset 0
1439 :format-handler 'widget-default-format-handler
1440 :button-face-get 'widget-default-button-face-get
1441 :mouse-face-get 'widget-default-mouse-face-get
1442 :sample-face-get 'widget-default-sample-face-get
1443 :delete 'widget-default-delete
1444 :copy 'identity
1445 :value-set 'widget-default-value-set
1446 :value-inline 'widget-default-value-inline
1447 :value-delete 'ignore
1448 :default-get 'widget-default-default-get
1449 :menu-tag-get 'widget-default-menu-tag-get
1450 :validate #'ignore
1451 :active 'widget-default-active
1452 :activate 'widget-specify-active
1453 :deactivate 'widget-default-deactivate
1454 :mouse-down-action #'ignore
1455 :action 'widget-default-action
1456 :notify 'widget-default-notify
1457 :prompt-value 'widget-default-prompt-value)
1459 (defun widget-default-complete (widget)
1460 "Call the value of the :complete-function property of WIDGET.
1461 If that does not exist, call the value of `widget-complete-field'."
1462 (call-interactively (or (widget-get widget :complete-function)
1463 widget-complete-field)))
1465 (defun widget-default-create (widget)
1466 "Create WIDGET at point in the current buffer."
1467 (widget-specify-insert
1468 (let ((from (point))
1469 button-begin button-end
1470 sample-begin sample-end
1471 doc-begin doc-end
1472 value-pos)
1473 (insert (widget-get widget :format))
1474 (goto-char from)
1475 ;; Parse escapes in format.
1476 (while (re-search-forward "%\\(.\\)" nil t)
1477 (let ((escape (char-after (match-beginning 1))))
1478 (delete-backward-char 2)
1479 (cond ((eq escape ?%)
1480 (insert ?%))
1481 ((eq escape ?\[)
1482 (setq button-begin (point))
1483 (insert (widget-get-indirect widget :button-prefix)))
1484 ((eq escape ?\])
1485 (insert (widget-get-indirect widget :button-suffix))
1486 (setq button-end (point)))
1487 ((eq escape ?\{)
1488 (setq sample-begin (point)))
1489 ((eq escape ?\})
1490 (setq sample-end (point)))
1491 ((eq escape ?n)
1492 (when (widget-get widget :indent)
1493 (insert ?\n)
1494 (insert-char ?\s (widget-get widget :indent))))
1495 ((eq escape ?t)
1496 (let ((image (widget-get widget :tag-glyph))
1497 (tag (widget-get widget :tag)))
1498 (cond (image
1499 (widget-image-insert widget (or tag "image") image))
1500 (tag
1501 (insert tag))
1503 (princ (widget-get widget :value)
1504 (current-buffer))))))
1505 ((eq escape ?d)
1506 (let ((doc (widget-get widget :doc)))
1507 (when doc
1508 (setq doc-begin (point))
1509 (insert doc)
1510 (while (eq (preceding-char) ?\n)
1511 (delete-backward-char 1))
1512 (insert ?\n)
1513 (setq doc-end (point)))))
1514 ((eq escape ?h)
1515 (widget-add-documentation-string-button widget))
1516 ((eq escape ?v)
1517 (if (and button-begin (not button-end))
1518 (widget-apply widget :value-create)
1519 (setq value-pos (point))))
1521 (widget-apply widget :format-handler escape)))))
1522 ;; Specify button, sample, and doc, and insert value.
1523 (and button-begin button-end
1524 (widget-specify-button widget button-begin button-end))
1525 (and sample-begin sample-end
1526 (widget-specify-sample widget sample-begin sample-end))
1527 (and doc-begin doc-end
1528 (widget-specify-doc widget doc-begin doc-end))
1529 (when value-pos
1530 (goto-char value-pos)
1531 (widget-apply widget :value-create)))
1532 (let ((from (point-min-marker))
1533 (to (point-max-marker)))
1534 (set-marker-insertion-type from t)
1535 (set-marker-insertion-type to nil)
1536 (widget-put widget :from from)
1537 (widget-put widget :to to)))
1538 (widget-clear-undo))
1540 (defun widget-default-format-handler (widget escape)
1541 (error "Unknown escape `%c'" escape))
1543 (defun widget-default-button-face-get (widget)
1544 ;; Use :button-face or widget-button-face
1545 (or (widget-get widget :button-face)
1546 (let ((parent (widget-get widget :parent)))
1547 (if parent
1548 (widget-apply parent :button-face-get)
1549 widget-button-face))))
1551 (defun widget-default-mouse-face-get (widget)
1552 ;; Use :mouse-face or widget-mouse-face
1553 (or (widget-get widget :mouse-face)
1554 (let ((parent (widget-get widget :parent)))
1555 (if parent
1556 (widget-apply parent :mouse-face-get)
1557 widget-mouse-face))))
1559 (defun widget-default-sample-face-get (widget)
1560 ;; Use :sample-face.
1561 (widget-get widget :sample-face))
1563 (defun widget-default-delete (widget)
1564 "Remove widget from the buffer."
1565 (let ((from (widget-get widget :from))
1566 (to (widget-get widget :to))
1567 (inactive-overlay (widget-get widget :inactive))
1568 (button-overlay (widget-get widget :button-overlay))
1569 (sample-overlay (widget-get widget :sample-overlay))
1570 (doc-overlay (widget-get widget :doc-overlay))
1571 (inhibit-modification-hooks t)
1572 (inhibit-read-only t))
1573 (widget-apply widget :value-delete)
1574 (widget-children-value-delete widget)
1575 (when inactive-overlay
1576 (delete-overlay inactive-overlay))
1577 (when button-overlay
1578 (delete-overlay button-overlay))
1579 (when sample-overlay
1580 (delete-overlay sample-overlay))
1581 (when doc-overlay
1582 (delete-overlay doc-overlay))
1583 (when (< from to)
1584 ;; Kludge: this doesn't need to be true for empty formats.
1585 (delete-region from to))
1586 (set-marker from nil)
1587 (set-marker to nil))
1588 (widget-clear-undo))
1590 (defun widget-default-value-set (widget value)
1591 "Recreate widget with new value."
1592 (let* ((old-pos (point))
1593 (from (copy-marker (widget-get widget :from)))
1594 (to (copy-marker (widget-get widget :to)))
1595 (offset (if (and (<= from old-pos) (<= old-pos to))
1596 (if (>= old-pos (1- to))
1597 (- old-pos to 1)
1598 (- old-pos from)))))
1599 ;;??? Bug: this ought to insert the new value before deleting the old one,
1600 ;; so that markers on either side of the value automatically
1601 ;; stay on the same side. -- rms.
1602 (save-excursion
1603 (goto-char (widget-get widget :from))
1604 (widget-apply widget :delete)
1605 (widget-put widget :value value)
1606 (widget-apply widget :create))
1607 (if offset
1608 (if (< offset 0)
1609 (goto-char (+ (widget-get widget :to) offset 1))
1610 (goto-char (min (+ from offset) (1- (widget-get widget :to))))))))
1612 (defun widget-default-value-inline (widget)
1613 "Wrap value in a list unless it is inline."
1614 (if (widget-get widget :inline)
1615 (widget-value widget)
1616 (list (widget-value widget))))
1618 (defun widget-default-default-get (widget)
1619 "Get `:value'."
1620 (widget-get widget :value))
1622 (defun widget-default-menu-tag-get (widget)
1623 "Use tag or value for menus."
1624 (or (widget-get widget :menu-tag)
1625 (widget-get widget :tag)
1626 (widget-princ-to-string (widget-get widget :value))))
1628 (defun widget-default-active (widget)
1629 "Return t if this widget is active (user modifiable)."
1630 (or (widget-get widget :always-active)
1631 (and (not (widget-get widget :inactive))
1632 (let ((parent (widget-get widget :parent)))
1633 (or (null parent)
1634 (widget-apply parent :active))))))
1636 (defun widget-default-deactivate (widget)
1637 "Make WIDGET inactive for user modifications."
1638 (widget-specify-inactive widget
1639 (widget-get widget :from)
1640 (widget-get widget :to)))
1642 (defun widget-default-action (widget &optional event)
1643 "Notify the parent when a widget changes."
1644 (let ((parent (widget-get widget :parent)))
1645 (when parent
1646 (widget-apply parent :notify widget event))))
1648 (defun widget-default-notify (widget child &optional event)
1649 "Pass notification to parent."
1650 (widget-default-action widget event))
1652 (defun widget-default-prompt-value (widget prompt value unbound)
1653 "Read an arbitrary value."
1654 (eval-minibuffer prompt))
1656 (defun widget-docstring (widget)
1657 "Return the documentation string specificied by WIDGET, or nil if none.
1658 If WIDGET has a `:doc' property, that specifies the documentation string.
1659 Otherwise, try the `:documentation-property' property. If this
1660 is a function, call it with the widget's value as an argument; if
1661 it is a symbol, use this symbol together with the widget's value
1662 as the argument to `documentation-property'."
1663 (let ((doc (or (widget-get widget :doc)
1664 (let ((doc-prop (widget-get widget :documentation-property))
1665 (value (widget-get widget :value)))
1666 (cond ((functionp doc-prop)
1667 (funcall doc-prop value))
1668 ((symbolp doc-prop)
1669 (documentation-property value doc-prop)))))))
1670 (when (and (stringp doc) (> (length doc) 0))
1671 ;; Remove any redundant `*' in the beginning.
1672 (when (eq (aref doc 0) ?*)
1673 (setq doc (substring doc 1)))
1674 ;; Remove trailing newlines.
1675 (when (string-match "\n+\\'" doc)
1676 (setq doc (substring doc 0 (match-beginning 0))))
1677 doc)))
1679 ;;; The `item' Widget.
1681 (define-widget 'item 'default
1682 "Constant items for inclusion in other widgets."
1683 :convert-widget 'widget-value-convert-widget
1684 :value-create 'widget-item-value-create
1685 :value-delete 'ignore
1686 :value-get 'widget-value-value-get
1687 :match 'widget-item-match
1688 :match-inline 'widget-item-match-inline
1689 :action 'widget-item-action
1690 :format "%t\n")
1692 (defun widget-item-value-create (widget)
1693 "Insert the printed representation of the value."
1694 (princ (widget-get widget :value) (current-buffer)))
1696 (defun widget-item-match (widget value)
1697 ;; Match if the value is the same.
1698 (equal (widget-get widget :value) value))
1700 (defun widget-item-match-inline (widget values)
1701 ;; Match if the value is the same.
1702 (let ((value (widget-get widget :value)))
1703 (and (listp value)
1704 (<= (length value) (length values))
1705 (let ((head (widget-sublist values 0 (length value))))
1706 (and (equal head value)
1707 (cons head (widget-sublist values (length value))))))))
1709 (defun widget-sublist (list start &optional end)
1710 "Return the sublist of LIST from START to END.
1711 If END is omitted, it defaults to the length of LIST."
1712 (if (> start 0) (setq list (nthcdr start list)))
1713 (if end
1714 (unless (<= end start)
1715 (setq list (copy-sequence list))
1716 (setcdr (nthcdr (- end start 1) list) nil)
1717 list)
1718 (copy-sequence list)))
1720 (defun widget-item-action (widget &optional event)
1721 ;; Just notify itself.
1722 (widget-apply widget :notify widget event))
1724 ;;; The `push-button' Widget.
1726 ;; (defcustom widget-push-button-gui t
1727 ;; "If non-nil, use GUI push buttons when available."
1728 ;; :group 'widgets
1729 ;; :type 'boolean)
1731 ;; Cache already created GUI objects.
1732 ;; (defvar widget-push-button-cache nil)
1734 (defcustom widget-push-button-prefix "["
1735 "String used as prefix for buttons."
1736 :type 'string
1737 :group 'widget-button)
1739 (defcustom widget-push-button-suffix "]"
1740 "String used as suffix for buttons."
1741 :type 'string
1742 :group 'widget-button)
1744 (define-widget 'push-button 'item
1745 "A pushable button."
1746 :button-prefix ""
1747 :button-suffix ""
1748 :value-create 'widget-push-button-value-create
1749 :format "%[%v%]")
1751 (defun widget-push-button-value-create (widget)
1752 "Insert text representing the `on' and `off' states."
1753 (let* ((tag (or (widget-get widget :tag)
1754 (widget-get widget :value)))
1755 (tag-glyph (widget-get widget :tag-glyph))
1756 (text (concat widget-push-button-prefix
1757 tag widget-push-button-suffix)))
1758 (if tag-glyph
1759 (widget-image-insert widget text tag-glyph)
1760 (insert text))))
1762 ;; (defun widget-gui-action (widget)
1763 ;; "Apply :action for WIDGET."
1764 ;; (widget-apply-action widget (this-command-keys)))
1766 ;;; The `link' Widget.
1768 (defcustom widget-link-prefix "["
1769 "String used as prefix for links."
1770 :type 'string
1771 :group 'widget-button)
1773 (defcustom widget-link-suffix "]"
1774 "String used as suffix for links."
1775 :type 'string
1776 :group 'widget-button)
1778 (define-widget 'link 'item
1779 "An embedded link."
1780 :button-prefix 'widget-link-prefix
1781 :button-suffix 'widget-link-suffix
1782 :follow-link 'mouse-face
1783 :help-echo "Follow the link."
1784 :format "%[%t%]")
1786 ;;; The `info-link' Widget.
1788 (define-widget 'info-link 'link
1789 "A link to an info file."
1790 :action 'widget-info-link-action)
1792 (defun widget-info-link-action (widget &optional event)
1793 "Open the info node specified by WIDGET."
1794 (info (widget-value widget)))
1796 ;;; The `url-link' Widget.
1798 (define-widget 'url-link 'link
1799 "A link to an www page."
1800 :action 'widget-url-link-action)
1802 (defun widget-url-link-action (widget &optional event)
1803 "Open the URL specified by WIDGET."
1804 (browse-url (widget-value widget)))
1806 ;;; The `function-link' Widget.
1808 (define-widget 'function-link 'link
1809 "A link to an Emacs function."
1810 :action 'widget-function-link-action)
1812 (defun widget-function-link-action (widget &optional event)
1813 "Show the function specified by WIDGET."
1814 (describe-function (widget-value widget)))
1816 ;;; The `variable-link' Widget.
1818 (define-widget 'variable-link 'link
1819 "A link to an Emacs variable."
1820 :action 'widget-variable-link-action)
1822 (defun widget-variable-link-action (widget &optional event)
1823 "Show the variable specified by WIDGET."
1824 (describe-variable (widget-value widget)))
1826 ;;; The `file-link' Widget.
1828 (define-widget 'file-link 'link
1829 "A link to a file."
1830 :action 'widget-file-link-action)
1832 (defun widget-file-link-action (widget &optional event)
1833 "Find the file specified by WIDGET."
1834 (find-file (widget-value widget)))
1836 ;;; The `emacs-library-link' Widget.
1838 (define-widget 'emacs-library-link 'link
1839 "A link to an Emacs Lisp library file."
1840 :action 'widget-emacs-library-link-action)
1842 (defun widget-emacs-library-link-action (widget &optional event)
1843 "Find the Emacs library file specified by WIDGET."
1844 (find-file (locate-library (widget-value widget))))
1846 ;;; The `emacs-commentary-link' Widget.
1848 (define-widget 'emacs-commentary-link 'link
1849 "A link to Commentary in an Emacs Lisp library file."
1850 :action 'widget-emacs-commentary-link-action)
1852 (defun widget-emacs-commentary-link-action (widget &optional event)
1853 "Find the Commentary section of the Emacs file specified by WIDGET."
1854 (finder-commentary (widget-value widget)))
1856 ;;; The `editable-field' Widget.
1858 (define-widget 'editable-field 'default
1859 "An editable text field.
1860 Note: In an `editable-field' widget, the `%v' escape must be preceded
1861 by some other text in the `:format' string (if specified)."
1862 :convert-widget 'widget-value-convert-widget
1863 :keymap widget-field-keymap
1864 :format "%v"
1865 :help-echo "M-TAB: complete field; RET: enter value"
1866 :value ""
1867 :prompt-internal 'widget-field-prompt-internal
1868 :prompt-history 'widget-field-history
1869 :prompt-value 'widget-field-prompt-value
1870 :action 'widget-field-action
1871 :validate 'widget-field-validate
1872 :valid-regexp ""
1873 :error "Field's value doesn't match allowed forms"
1874 :value-create 'widget-field-value-create
1875 :value-set 'widget-field-value-set
1876 :value-delete 'widget-field-value-delete
1877 :value-get 'widget-field-value-get
1878 :match 'widget-field-match)
1880 (defvar widget-field-history nil
1881 "History of field minibuffer edits.")
1883 (defun widget-field-prompt-internal (widget prompt initial history)
1884 "Read string for WIDGET prompting with PROMPT.
1885 INITIAL is the initial input and HISTORY is a symbol containing
1886 the earlier input."
1887 (read-string prompt initial history))
1889 (defun widget-field-prompt-value (widget prompt value unbound)
1890 "Prompt for a string."
1891 (widget-apply widget
1892 :value-to-external
1893 (widget-apply widget
1894 :prompt-internal prompt
1895 (unless unbound
1896 (cons (widget-apply widget
1897 :value-to-internal value)
1899 (widget-get widget :prompt-history))))
1901 (defvar widget-edit-functions nil)
1903 (defun widget-field-action (widget &optional event)
1904 "Move to next field."
1905 (widget-forward 1)
1906 (run-hook-with-args 'widget-edit-functions widget))
1908 (defun widget-field-validate (widget)
1909 "Valid if the content matches `:valid-regexp'."
1910 (unless (string-match (widget-get widget :valid-regexp)
1911 (widget-apply widget :value-get))
1912 widget))
1914 (defun widget-field-value-set (widget value)
1915 "Set an editable text field WIDGET to VALUE"
1916 (let ((from (widget-field-start widget))
1917 (to (widget-field-text-end widget))
1918 (buffer (widget-field-buffer widget))
1919 (size (widget-get widget :size)))
1920 (when (and from to (buffer-live-p buffer))
1921 (with-current-buffer buffer
1922 (goto-char from)
1923 (delete-char (- to from))
1924 (insert value)))))
1926 (defun widget-field-value-create (widget)
1927 "Create an editable text field."
1928 (let ((size (widget-get widget :size))
1929 (value (widget-get widget :value))
1930 (from (point))
1931 ;; This is changed to a real overlay in `widget-setup'. We
1932 ;; need the end points to behave differently until
1933 ;; `widget-setup' is called.
1934 (overlay (cons (make-marker) (make-marker))))
1935 (widget-put widget :field-overlay overlay)
1936 (insert value)
1937 (and size
1938 (< (length value) size)
1939 (insert-char ?\s (- size (length value))))
1940 (unless (memq widget widget-field-list)
1941 (setq widget-field-new (cons widget widget-field-new)))
1942 (move-marker (cdr overlay) (point))
1943 (set-marker-insertion-type (cdr overlay) nil)
1944 (when (null size)
1945 (insert ?\n))
1946 (move-marker (car overlay) from)
1947 (set-marker-insertion-type (car overlay) t)))
1949 (defun widget-field-value-delete (widget)
1950 "Remove the widget from the list of active editing fields."
1951 (setq widget-field-list (delq widget widget-field-list))
1952 (setq widget-field-new (delq widget widget-field-new))
1953 ;; These are nil if the :format string doesn't contain `%v'.
1954 (let ((overlay (widget-get widget :field-overlay)))
1955 (when (overlayp overlay)
1956 (delete-overlay overlay))))
1958 (defun widget-field-value-get (widget)
1959 "Return current text in editing field."
1960 (let ((from (widget-field-start widget))
1961 (to (widget-field-text-end widget))
1962 (buffer (widget-field-buffer widget))
1963 (secret (widget-get widget :secret))
1964 (old (current-buffer)))
1965 (if (and from to)
1966 (progn
1967 (set-buffer buffer)
1968 (let ((result (buffer-substring-no-properties from to)))
1969 (when secret
1970 (let ((index 0))
1971 (while (< (+ from index) to)
1972 (aset result index
1973 (get-char-property (+ from index) 'secret))
1974 (setq index (1+ index)))))
1975 (set-buffer old)
1976 result))
1977 (widget-get widget :value))))
1979 (defun widget-field-match (widget value)
1980 ;; Match any string.
1981 (stringp value))
1983 ;;; The `text' Widget.
1985 (define-widget 'text 'editable-field
1986 "A multiline text area."
1987 :keymap widget-text-keymap)
1989 ;;; The `menu-choice' Widget.
1991 (define-widget 'menu-choice 'default
1992 "A menu of options."
1993 :convert-widget 'widget-types-convert-widget
1994 :copy 'widget-types-copy
1995 :format "%[%t%]: %v"
1996 :case-fold t
1997 :tag "choice"
1998 :void '(item :format "invalid (%t)\n")
1999 :value-create 'widget-choice-value-create
2000 :value-get 'widget-child-value-get
2001 :value-inline 'widget-child-value-inline
2002 :default-get 'widget-choice-default-get
2003 :mouse-down-action 'widget-choice-mouse-down-action
2004 :action 'widget-choice-action
2005 :error "Make a choice"
2006 :validate 'widget-choice-validate
2007 :match 'widget-choice-match
2008 :match-inline 'widget-choice-match-inline)
2010 (defun widget-choice-value-create (widget)
2011 "Insert the first choice that matches the value."
2012 (let ((value (widget-get widget :value))
2013 (args (widget-get widget :args))
2014 (explicit (widget-get widget :explicit-choice))
2015 current)
2016 (if explicit
2017 (progn
2018 ;; If the user specified the choice for this value,
2019 ;; respect that choice.
2020 (widget-put widget :children (list (widget-create-child-value
2021 widget explicit value)))
2022 (widget-put widget :choice explicit)
2023 (widget-put widget :explicit-choice nil))
2024 (while args
2025 (setq current (car args)
2026 args (cdr args))
2027 (when (widget-apply current :match value)
2028 (widget-put widget :children (list (widget-create-child-value
2029 widget current value)))
2030 (widget-put widget :choice current)
2031 (setq args nil
2032 current nil)))
2033 (when current
2034 (let ((void (widget-get widget :void)))
2035 (widget-put widget :children (list (widget-create-child-and-convert
2036 widget void :value value)))
2037 (widget-put widget :choice void))))))
2039 (defun widget-choice-default-get (widget)
2040 ;; Get default for the first choice.
2041 (widget-default-get (car (widget-get widget :args))))
2043 (defcustom widget-choice-toggle nil
2044 "If non-nil, a binary choice will just toggle between the values.
2045 Otherwise, the user will explicitly have to choose between the values
2046 when he invoked the menu."
2047 :type 'boolean
2048 :group 'widgets)
2050 (defun widget-choice-mouse-down-action (widget &optional event)
2051 ;; Return non-nil if we need a menu.
2052 (let ((args (widget-get widget :args))
2053 (old (widget-get widget :choice)))
2054 (cond ((not (display-popup-menus-p))
2055 ;; No place to pop up a menu.
2056 nil)
2057 ((< (length args) 2)
2058 ;; Empty or singleton list, just return the value.
2059 nil)
2060 ((> (length args) widget-menu-max-size)
2061 ;; Too long, prompt.
2062 nil)
2063 ((> (length args) 2)
2064 ;; Reasonable sized list, use menu.
2066 ((and widget-choice-toggle (memq old args))
2067 ;; We toggle.
2068 nil)
2070 ;; Ask which of the two.
2071 t))))
2073 (defun widget-choice-action (widget &optional event)
2074 ;; Make a choice.
2075 (let ((args (widget-get widget :args))
2076 (old (widget-get widget :choice))
2077 (tag (widget-apply widget :menu-tag-get))
2078 (completion-ignore-case (widget-get widget :case-fold))
2079 this-explicit
2080 current choices)
2081 ;; Remember old value.
2082 (if (and old (not (widget-apply widget :validate)))
2083 (let* ((external (widget-value widget))
2084 (internal (widget-apply old :value-to-internal external)))
2085 (widget-put old :value internal)))
2086 ;; Find new choice.
2087 (setq current
2088 (cond ((= (length args) 0)
2089 nil)
2090 ((= (length args) 1)
2091 (nth 0 args))
2092 ((and widget-choice-toggle
2093 (= (length args) 2)
2094 (memq old args))
2095 (if (eq old (nth 0 args))
2096 (nth 1 args)
2097 (nth 0 args)))
2099 (while args
2100 (setq current (car args)
2101 args (cdr args))
2102 (setq choices
2103 (cons (cons (widget-apply current :menu-tag-get)
2104 current)
2105 choices)))
2106 (setq this-explicit t)
2107 (widget-choose tag (reverse choices) event))))
2108 (when current
2109 ;; If this was an explicit user choice, record the choice,
2110 ;; so that widget-choice-value-create will respect it.
2111 (when this-explicit
2112 (widget-put widget :explicit-choice current))
2113 (widget-value-set widget (widget-default-get current))
2114 (widget-setup)
2115 (widget-apply widget :notify widget event)))
2116 (run-hook-with-args 'widget-edit-functions widget))
2118 (defun widget-choice-validate (widget)
2119 ;; Valid if we have made a valid choice.
2120 (if (eq (widget-get widget :void) (widget-get widget :choice))
2121 widget
2122 (widget-apply (car (widget-get widget :children)) :validate)))
2124 (defun widget-choice-match (widget value)
2125 ;; Matches if one of the choices matches.
2126 (let ((args (widget-get widget :args))
2127 current found)
2128 (while (and args (not found))
2129 (setq current (car args)
2130 args (cdr args)
2131 found (widget-apply current :match value)))
2132 found))
2134 (defun widget-choice-match-inline (widget values)
2135 ;; Matches if one of the choices matches.
2136 (let ((args (widget-get widget :args))
2137 current found)
2138 (while (and args (null found))
2139 (setq current (car args)
2140 args (cdr args)
2141 found (widget-match-inline current values)))
2142 found))
2144 ;;; The `toggle' Widget.
2146 (define-widget 'toggle 'item
2147 "Toggle between two states."
2148 :format "%[%v%]\n"
2149 :value-create 'widget-toggle-value-create
2150 :action 'widget-toggle-action
2151 :match (lambda (widget value) t)
2152 :on "on"
2153 :off "off")
2155 (defun widget-toggle-value-create (widget)
2156 "Insert text representing the `on' and `off' states."
2157 (if (widget-value widget)
2158 (let ((image (widget-get widget :on-glyph)))
2159 (and (display-graphic-p)
2160 (listp image)
2161 (not (eq (car image) 'image))
2162 (widget-put widget :on-glyph (setq image (eval image))))
2163 (widget-image-insert widget
2164 (widget-get widget :on)
2165 image))
2166 (let ((image (widget-get widget :off-glyph)))
2167 (and (display-graphic-p)
2168 (listp image)
2169 (not (eq (car image) 'image))
2170 (widget-put widget :off-glyph (setq image (eval image))))
2171 (widget-image-insert widget (widget-get widget :off) image))))
2173 (defun widget-toggle-action (widget &optional event)
2174 ;; Toggle value.
2175 (widget-value-set widget (not (widget-value widget)))
2176 (widget-apply widget :notify widget event)
2177 (run-hook-with-args 'widget-edit-functions widget))
2179 ;;; The `checkbox' Widget.
2181 (define-widget 'checkbox 'toggle
2182 "A checkbox toggle."
2183 :button-suffix ""
2184 :button-prefix ""
2185 :format "%[%v%]"
2186 :on "[X]"
2187 ;; We could probably do the same job as the images using single
2188 ;; space characters in a boxed face with a stretch specification to
2189 ;; make them square.
2190 :on-glyph '(create-image "\300\300\141\143\067\076\034\030"
2191 'xbm t :width 8 :height 8
2192 :background "grey75" ; like default mode line
2193 :foreground "black"
2194 :relief -2
2195 :ascent 'center)
2196 :off "[ ]"
2197 :off-glyph '(create-image (make-string 8 0)
2198 'xbm t :width 8 :height 8
2199 :background "grey75"
2200 :foreground "black"
2201 :relief -2
2202 :ascent 'center)
2203 :help-echo "Toggle this item."
2204 :action 'widget-checkbox-action)
2206 (defun widget-checkbox-action (widget &optional event)
2207 "Toggle checkbox, notify parent, and set active state of sibling."
2208 (widget-toggle-action widget event)
2209 (let ((sibling (widget-get-sibling widget)))
2210 (when sibling
2211 (if (widget-value widget)
2212 (widget-apply sibling :activate)
2213 (widget-apply sibling :deactivate))
2214 (widget-clear-undo))))
2216 ;;; The `checklist' Widget.
2218 (define-widget 'checklist 'default
2219 "A multiple choice widget."
2220 :convert-widget 'widget-types-convert-widget
2221 :copy 'widget-types-copy
2222 :format "%v"
2223 :offset 4
2224 :entry-format "%b %v"
2225 :greedy nil
2226 :value-create 'widget-checklist-value-create
2227 :value-get 'widget-checklist-value-get
2228 :validate 'widget-checklist-validate
2229 :match 'widget-checklist-match
2230 :match-inline 'widget-checklist-match-inline)
2232 (defun widget-checklist-value-create (widget)
2233 ;; Insert all values
2234 (let ((alist (widget-checklist-match-find widget (widget-get widget :value)))
2235 (args (widget-get widget :args)))
2236 (while args
2237 (widget-checklist-add-item widget (car args) (assq (car args) alist))
2238 (setq args (cdr args)))
2239 (widget-put widget :children (nreverse (widget-get widget :children)))))
2241 (defun widget-checklist-add-item (widget type chosen)
2242 "Create checklist item in WIDGET of type TYPE.
2243 If the item is checked, CHOSEN is a cons whose cdr is the value."
2244 (and (eq (preceding-char) ?\n)
2245 (widget-get widget :indent)
2246 (insert-char ?\s (widget-get widget :indent)))
2247 (widget-specify-insert
2248 (let* ((children (widget-get widget :children))
2249 (buttons (widget-get widget :buttons))
2250 (button-args (or (widget-get type :sibling-args)
2251 (widget-get widget :button-args)))
2252 (from (point))
2253 child button)
2254 (insert (widget-get widget :entry-format))
2255 (goto-char from)
2256 ;; Parse % escapes in format.
2257 (while (re-search-forward "%\\([bv%]\\)" nil t)
2258 (let ((escape (char-after (match-beginning 1))))
2259 (delete-backward-char 2)
2260 (cond ((eq escape ?%)
2261 (insert ?%))
2262 ((eq escape ?b)
2263 (setq button (apply 'widget-create-child-and-convert
2264 widget 'checkbox
2265 :value (not (null chosen))
2266 button-args)))
2267 ((eq escape ?v)
2268 (setq child
2269 (cond ((not chosen)
2270 (let ((child (widget-create-child widget type)))
2271 (widget-apply child :deactivate)
2272 child))
2273 ((widget-get type :inline)
2274 (widget-create-child-value
2275 widget type (cdr chosen)))
2277 (widget-create-child-value
2278 widget type (car (cdr chosen)))))))
2280 (error "Unknown escape `%c'" escape)))))
2281 ;; Update properties.
2282 (and button child (widget-put child :button button))
2283 (and button (widget-put widget :buttons (cons button buttons)))
2284 (and child (widget-put widget :children (cons child children))))))
2286 (defun widget-checklist-match (widget values)
2287 ;; All values must match a type in the checklist.
2288 (and (listp values)
2289 (null (cdr (widget-checklist-match-inline widget values)))))
2291 (defun widget-checklist-match-inline (widget values)
2292 ;; Find the values which match a type in the checklist.
2293 (let ((greedy (widget-get widget :greedy))
2294 (args (copy-sequence (widget-get widget :args)))
2295 found rest)
2296 (while values
2297 (let ((answer (widget-checklist-match-up args values)))
2298 (cond (answer
2299 (let ((vals (widget-match-inline answer values)))
2300 (setq found (append found (car vals))
2301 values (cdr vals)
2302 args (delq answer args))))
2303 (greedy
2304 (setq rest (append rest (list (car values)))
2305 values (cdr values)))
2307 (setq rest (append rest values)
2308 values nil)))))
2309 (cons found rest)))
2311 (defun widget-checklist-match-find (widget vals)
2312 "Find the vals which match a type in the checklist.
2313 Return an alist of (TYPE MATCH)."
2314 (let ((greedy (widget-get widget :greedy))
2315 (args (copy-sequence (widget-get widget :args)))
2316 found)
2317 (while vals
2318 (let ((answer (widget-checklist-match-up args vals)))
2319 (cond (answer
2320 (let ((match (widget-match-inline answer vals)))
2321 (setq found (cons (cons answer (car match)) found)
2322 vals (cdr match)
2323 args (delq answer args))))
2324 (greedy
2325 (setq vals (cdr vals)))
2327 (setq vals nil)))))
2328 found))
2330 (defun widget-checklist-match-up (args vals)
2331 "Return the first type from ARGS that matches VALS."
2332 (let (current found)
2333 (while (and args (null found))
2334 (setq current (car args)
2335 args (cdr args)
2336 found (widget-match-inline current vals)))
2337 (if found
2338 current)))
2340 (defun widget-checklist-value-get (widget)
2341 ;; The values of all selected items.
2342 (let ((children (widget-get widget :children))
2343 child result)
2344 (while children
2345 (setq child (car children)
2346 children (cdr children))
2347 (if (widget-value (widget-get child :button))
2348 (setq result (append result (widget-apply child :value-inline)))))
2349 result))
2351 (defun widget-checklist-validate (widget)
2352 ;; Ticked chilren must be valid.
2353 (let ((children (widget-get widget :children))
2354 child button found)
2355 (while (and children (not found))
2356 (setq child (car children)
2357 children (cdr children)
2358 button (widget-get child :button)
2359 found (and (widget-value button)
2360 (widget-apply child :validate))))
2361 found))
2363 ;;; The `option' Widget
2365 (define-widget 'option 'checklist
2366 "An widget with an optional item."
2367 :inline t)
2369 ;;; The `choice-item' Widget.
2371 (define-widget 'choice-item 'item
2372 "Button items that delegate action events to their parents."
2373 :action 'widget-parent-action
2374 :format "%[%t%] \n")
2376 ;;; The `radio-button' Widget.
2378 (define-widget 'radio-button 'toggle
2379 "A radio button for use in the `radio' widget."
2380 :notify 'widget-radio-button-notify
2381 :format "%[%v%]"
2382 :button-suffix ""
2383 :button-prefix ""
2384 :on "(*)"
2385 :on-glyph "radio1"
2386 :off "( )"
2387 :off-glyph "radio0")
2389 (defun widget-radio-button-notify (widget child &optional event)
2390 ;; Tell daddy.
2391 (widget-apply (widget-get widget :parent) :action widget event))
2393 ;;; The `radio-button-choice' Widget.
2395 (define-widget 'radio-button-choice 'default
2396 "Select one of multiple options."
2397 :convert-widget 'widget-types-convert-widget
2398 :copy 'widget-types-copy
2399 :offset 4
2400 :format "%v"
2401 :entry-format "%b %v"
2402 :value-create 'widget-radio-value-create
2403 :value-get 'widget-radio-value-get
2404 :value-inline 'widget-radio-value-inline
2405 :value-set 'widget-radio-value-set
2406 :error "You must push one of the buttons"
2407 :validate 'widget-radio-validate
2408 :match 'widget-choice-match
2409 :match-inline 'widget-choice-match-inline
2410 :action 'widget-radio-action)
2412 (defun widget-radio-value-create (widget)
2413 ;; Insert all values
2414 (let ((args (widget-get widget :args))
2415 arg)
2416 (while args
2417 (setq arg (car args)
2418 args (cdr args))
2419 (widget-radio-add-item widget arg))))
2421 (defun widget-radio-add-item (widget type)
2422 "Add to radio widget WIDGET a new radio button item of type TYPE."
2423 ;; (setq type (widget-convert type))
2424 (and (eq (preceding-char) ?\n)
2425 (widget-get widget :indent)
2426 (insert-char ?\s (widget-get widget :indent)))
2427 (widget-specify-insert
2428 (let* ((value (widget-get widget :value))
2429 (children (widget-get widget :children))
2430 (buttons (widget-get widget :buttons))
2431 (button-args (or (widget-get type :sibling-args)
2432 (widget-get widget :button-args)))
2433 (from (point))
2434 (chosen (and (null (widget-get widget :choice))
2435 (widget-apply type :match value)))
2436 child button)
2437 (insert (widget-get widget :entry-format))
2438 (goto-char from)
2439 ;; Parse % escapes in format.
2440 (while (re-search-forward "%\\([bv%]\\)" nil t)
2441 (let ((escape (char-after (match-beginning 1))))
2442 (delete-backward-char 2)
2443 (cond ((eq escape ?%)
2444 (insert ?%))
2445 ((eq escape ?b)
2446 (setq button (apply 'widget-create-child-and-convert
2447 widget 'radio-button
2448 :value (not (null chosen))
2449 button-args)))
2450 ((eq escape ?v)
2451 (setq child (if chosen
2452 (widget-create-child-value
2453 widget type value)
2454 (widget-create-child widget type)))
2455 (unless chosen
2456 (widget-apply child :deactivate)))
2458 (error "Unknown escape `%c'" escape)))))
2459 ;; Update properties.
2460 (when chosen
2461 (widget-put widget :choice type))
2462 (when button
2463 (widget-put child :button button)
2464 (widget-put widget :buttons (nconc buttons (list button))))
2465 (when child
2466 (widget-put widget :children (nconc children (list child))))
2467 child)))
2469 (defun widget-radio-value-get (widget)
2470 ;; Get value of the child widget.
2471 (let ((chosen (widget-radio-chosen widget)))
2472 (and chosen (widget-value chosen))))
2474 (defun widget-radio-chosen (widget)
2475 "Return the widget representing the chosen radio button."
2476 (let ((children (widget-get widget :children))
2477 current found)
2478 (while children
2479 (setq current (car children)
2480 children (cdr children))
2481 (when (widget-apply (widget-get current :button) :value-get)
2482 (setq found current
2483 children nil)))
2484 found))
2486 (defun widget-radio-value-inline (widget)
2487 ;; Get value of the child widget.
2488 (let ((children (widget-get widget :children))
2489 current found)
2490 (while children
2491 (setq current (car children)
2492 children (cdr children))
2493 (when (widget-apply (widget-get current :button) :value-get)
2494 (setq found (widget-apply current :value-inline)
2495 children nil)))
2496 found))
2498 (defun widget-radio-value-set (widget value)
2499 ;; We can't just delete and recreate a radio widget, since children
2500 ;; can be added after the original creation and won't be recreated
2501 ;; by `:create'.
2502 (let ((children (widget-get widget :children))
2503 current found)
2504 (while children
2505 (setq current (car children)
2506 children (cdr children))
2507 (let* ((button (widget-get current :button))
2508 (match (and (not found)
2509 (widget-apply current :match value))))
2510 (widget-value-set button match)
2511 (if match
2512 (progn
2513 (widget-value-set current value)
2514 (widget-apply current :activate))
2515 (widget-apply current :deactivate))
2516 (setq found (or found match))))))
2518 (defun widget-radio-validate (widget)
2519 ;; Valid if we have made a valid choice.
2520 (let ((children (widget-get widget :children))
2521 current found button)
2522 (while (and children (not found))
2523 (setq current (car children)
2524 children (cdr children)
2525 button (widget-get current :button)
2526 found (widget-apply button :value-get)))
2527 (if found
2528 (widget-apply current :validate)
2529 widget)))
2531 (defun widget-radio-action (widget child event)
2532 ;; Check if a radio button was pressed.
2533 (let ((children (widget-get widget :children))
2534 (buttons (widget-get widget :buttons))
2535 current)
2536 (when (memq child buttons)
2537 (while children
2538 (setq current (car children)
2539 children (cdr children))
2540 (let* ((button (widget-get current :button)))
2541 (cond ((eq child button)
2542 (widget-value-set button t)
2543 (widget-apply current :activate))
2544 ((widget-value button)
2545 (widget-value-set button nil)
2546 (widget-apply current :deactivate)))))))
2547 ;; Pass notification to parent.
2548 (widget-apply widget :notify child event))
2550 ;;; The `insert-button' Widget.
2552 (define-widget 'insert-button 'push-button
2553 "An insert button for the `editable-list' widget."
2554 :tag "INS"
2555 :help-echo "Insert a new item into the list at this position."
2556 :action 'widget-insert-button-action)
2558 (defun widget-insert-button-action (widget &optional event)
2559 ;; Ask the parent to insert a new item.
2560 (widget-apply (widget-get widget :parent)
2561 :insert-before (widget-get widget :widget)))
2563 ;;; The `delete-button' Widget.
2565 (define-widget 'delete-button 'push-button
2566 "A delete button for the `editable-list' widget."
2567 :tag "DEL"
2568 :help-echo "Delete this item from the list."
2569 :action 'widget-delete-button-action)
2571 (defun widget-delete-button-action (widget &optional event)
2572 ;; Ask the parent to insert a new item.
2573 (widget-apply (widget-get widget :parent)
2574 :delete-at (widget-get widget :widget)))
2576 ;;; The `editable-list' Widget.
2578 ;; (defcustom widget-editable-list-gui nil
2579 ;; "If non-nil, use GUI push-buttons in editable list when available."
2580 ;; :type 'boolean
2581 ;; :group 'widgets)
2583 (define-widget 'editable-list 'default
2584 "A variable list of widgets of the same type."
2585 :convert-widget 'widget-types-convert-widget
2586 :copy 'widget-types-copy
2587 :offset 12
2588 :format "%v%i\n"
2589 :format-handler 'widget-editable-list-format-handler
2590 :entry-format "%i %d %v"
2591 :value-create 'widget-editable-list-value-create
2592 :value-get 'widget-editable-list-value-get
2593 :validate 'widget-children-validate
2594 :match 'widget-editable-list-match
2595 :match-inline 'widget-editable-list-match-inline
2596 :insert-before 'widget-editable-list-insert-before
2597 :delete-at 'widget-editable-list-delete-at)
2599 (defun widget-editable-list-format-handler (widget escape)
2600 ;; We recognize the insert button.
2601 ;; (let ((widget-push-button-gui widget-editable-list-gui))
2602 (cond ((eq escape ?i)
2603 (and (widget-get widget :indent)
2604 (insert-char ?\s (widget-get widget :indent)))
2605 (apply 'widget-create-child-and-convert
2606 widget 'insert-button
2607 (widget-get widget :append-button-args)))
2609 (widget-default-format-handler widget escape)))
2610 ;; )
2613 (defun widget-editable-list-value-create (widget)
2614 ;; Insert all values
2615 (let* ((value (widget-get widget :value))
2616 (type (nth 0 (widget-get widget :args)))
2617 children)
2618 (widget-put widget :value-pos (copy-marker (point)))
2619 (set-marker-insertion-type (widget-get widget :value-pos) t)
2620 (while value
2621 (let ((answer (widget-match-inline type value)))
2622 (if answer
2623 (setq children (cons (widget-editable-list-entry-create
2624 widget
2625 (if (widget-get type :inline)
2626 (car answer)
2627 (car (car answer)))
2629 children)
2630 value (cdr answer))
2631 (setq value nil))))
2632 (widget-put widget :children (nreverse children))))
2634 (defun widget-editable-list-value-get (widget)
2635 ;; Get value of the child widget.
2636 (apply 'append (mapcar (lambda (child) (widget-apply child :value-inline))
2637 (widget-get widget :children))))
2639 (defun widget-editable-list-match (widget value)
2640 ;; Value must be a list and all the members must match the type.
2641 (and (listp value)
2642 (null (cdr (widget-editable-list-match-inline widget value)))))
2644 (defun widget-editable-list-match-inline (widget value)
2645 (let ((type (nth 0 (widget-get widget :args)))
2646 (ok t)
2647 found)
2648 (while (and value ok)
2649 (let ((answer (widget-match-inline type value)))
2650 (if answer
2651 (setq found (append found (car answer))
2652 value (cdr answer))
2653 (setq ok nil))))
2654 (cons found value)))
2656 (defun widget-editable-list-insert-before (widget before)
2657 ;; Insert a new child in the list of children.
2658 (save-excursion
2659 (let ((children (widget-get widget :children))
2660 (inhibit-read-only t)
2661 before-change-functions
2662 after-change-functions)
2663 (cond (before
2664 (goto-char (widget-get before :entry-from)))
2666 (goto-char (widget-get widget :value-pos))))
2667 (let ((child (widget-editable-list-entry-create
2668 widget nil nil)))
2669 (when (< (widget-get child :entry-from) (widget-get widget :from))
2670 (set-marker (widget-get widget :from)
2671 (widget-get child :entry-from)))
2672 (if (eq (car children) before)
2673 (widget-put widget :children (cons child children))
2674 (while (not (eq (car (cdr children)) before))
2675 (setq children (cdr children)))
2676 (setcdr children (cons child (cdr children)))))))
2677 (widget-setup)
2678 (widget-apply widget :notify widget))
2680 (defun widget-editable-list-delete-at (widget child)
2681 ;; Delete child from list of children.
2682 (save-excursion
2683 (let ((buttons (copy-sequence (widget-get widget :buttons)))
2684 button
2685 (inhibit-read-only t)
2686 before-change-functions
2687 after-change-functions)
2688 (while buttons
2689 (setq button (car buttons)
2690 buttons (cdr buttons))
2691 (when (eq (widget-get button :widget) child)
2692 (widget-put widget
2693 :buttons (delq button (widget-get widget :buttons)))
2694 (widget-delete button))))
2695 (let ((entry-from (widget-get child :entry-from))
2696 (entry-to (widget-get child :entry-to))
2697 (inhibit-read-only t)
2698 before-change-functions
2699 after-change-functions)
2700 (widget-delete child)
2701 (delete-region entry-from entry-to)
2702 (set-marker entry-from nil)
2703 (set-marker entry-to nil))
2704 (widget-put widget :children (delq child (widget-get widget :children))))
2705 (widget-setup)
2706 (widget-apply widget :notify widget))
2708 (defun widget-editable-list-entry-create (widget value conv)
2709 ;; Create a new entry to the list.
2710 (let ((type (nth 0 (widget-get widget :args)))
2711 ;; (widget-push-button-gui widget-editable-list-gui)
2712 child delete insert)
2713 (widget-specify-insert
2714 (save-excursion
2715 (and (widget-get widget :indent)
2716 (insert-char ?\s (widget-get widget :indent)))
2717 (insert (widget-get widget :entry-format)))
2718 ;; Parse % escapes in format.
2719 (while (re-search-forward "%\\(.\\)" nil t)
2720 (let ((escape (char-after (match-beginning 1))))
2721 (delete-backward-char 2)
2722 (cond ((eq escape ?%)
2723 (insert ?%))
2724 ((eq escape ?i)
2725 (setq insert (apply 'widget-create-child-and-convert
2726 widget 'insert-button
2727 (widget-get widget :insert-button-args))))
2728 ((eq escape ?d)
2729 (setq delete (apply 'widget-create-child-and-convert
2730 widget 'delete-button
2731 (widget-get widget :delete-button-args))))
2732 ((eq escape ?v)
2733 (if conv
2734 (setq child (widget-create-child-value
2735 widget type value))
2736 (setq child (widget-create-child-value
2737 widget type (widget-default-get type)))))
2739 (error "Unknown escape `%c'" escape)))))
2740 (let ((buttons (widget-get widget :buttons)))
2741 (if insert (push insert buttons))
2742 (if delete (push delete buttons))
2743 (widget-put widget :buttons buttons))
2744 (let ((entry-from (point-min-marker))
2745 (entry-to (point-max-marker)))
2746 (set-marker-insertion-type entry-from t)
2747 (set-marker-insertion-type entry-to nil)
2748 (widget-put child :entry-from entry-from)
2749 (widget-put child :entry-to entry-to)))
2750 (if insert (widget-put insert :widget child))
2751 (if delete (widget-put delete :widget child))
2752 child))
2754 ;;; The `group' Widget.
2756 (define-widget 'group 'default
2757 "A widget which groups other widgets inside."
2758 :convert-widget 'widget-types-convert-widget
2759 :copy 'widget-types-copy
2760 :format "%v"
2761 :value-create 'widget-group-value-create
2762 :value-get 'widget-editable-list-value-get
2763 :default-get 'widget-group-default-get
2764 :validate 'widget-children-validate
2765 :match 'widget-group-match
2766 :match-inline 'widget-group-match-inline)
2768 (defun widget-group-value-create (widget)
2769 ;; Create each component.
2770 (let ((args (widget-get widget :args))
2771 (value (widget-get widget :value))
2772 arg answer children)
2773 (while args
2774 (setq arg (car args)
2775 args (cdr args)
2776 answer (widget-match-inline arg value)
2777 value (cdr answer))
2778 (and (eq (preceding-char) ?\n)
2779 (widget-get widget :indent)
2780 (insert-char ?\s (widget-get widget :indent)))
2781 (push (cond ((null answer)
2782 (widget-create-child widget arg))
2783 ((widget-get arg :inline)
2784 (widget-create-child-value widget arg (car answer)))
2786 (widget-create-child-value widget arg (car (car answer)))))
2787 children))
2788 (widget-put widget :children (nreverse children))))
2790 (defun widget-group-default-get (widget)
2791 ;; Get the default of the components.
2792 (mapcar 'widget-default-get (widget-get widget :args)))
2794 (defun widget-group-match (widget values)
2795 ;; Match if the components match.
2796 (and (listp values)
2797 (let ((match (widget-group-match-inline widget values)))
2798 (and match (null (cdr match))))))
2800 (defun widget-group-match-inline (widget vals)
2801 ;; Match if the components match.
2802 (let ((args (widget-get widget :args))
2803 argument answer found)
2804 (while args
2805 (setq argument (car args)
2806 args (cdr args)
2807 answer (widget-match-inline argument vals))
2808 (if answer
2809 (setq vals (cdr answer)
2810 found (append found (car answer)))
2811 (setq vals nil
2812 args nil)))
2813 (if answer
2814 (cons found vals))))
2816 ;;; The `visibility' Widget.
2818 (define-widget 'visibility 'item
2819 "An indicator and manipulator for hidden items.
2821 The following properties have special meanings for this widget:
2822 :on-image Image filename or spec to display when the item is visible.
2823 :on Text shown if the \"on\" image is nil or cannot be displayed.
2824 :off-image Image filename or spec to display when the item is hidden.
2825 :off Text shown if the \"off\" image is nil cannot be displayed."
2826 :format "%[%v%]"
2827 :button-prefix ""
2828 :button-suffix ""
2829 :on-image "down"
2830 :on "Hide"
2831 :off-image "right"
2832 :off "Show"
2833 :value-create 'widget-visibility-value-create
2834 :action 'widget-toggle-action
2835 :match (lambda (widget value) t))
2837 (defun widget-visibility-value-create (widget)
2838 ;; Insert text representing the `on' and `off' states.
2839 (let* ((val (widget-value widget))
2840 (text (widget-get widget (if val :on :off)))
2841 (img (widget-image-find
2842 (widget-get widget (if val :on-image :off-image)))))
2843 (widget-image-insert widget
2844 (if text
2845 (concat widget-push-button-prefix text
2846 widget-push-button-suffix)
2848 (if img
2849 (append img '(:ascent center))))))
2851 ;;; The `documentation-link' Widget.
2853 ;; This is a helper widget for `documentation-string'.
2855 (define-widget 'documentation-link 'link
2856 "Link type used in documentation strings."
2857 :tab-order -1
2858 :help-echo "Describe this symbol"
2859 :action 'widget-documentation-link-action)
2861 (defun widget-documentation-link-action (widget &optional event)
2862 "Display documentation for WIDGET's value. Ignore optional argument EVENT."
2863 (let* ((string (widget-get widget :value))
2864 (symbol (intern string)))
2865 (if (and (fboundp symbol) (boundp symbol))
2866 ;; If there are two doc strings, give the user a way to pick one.
2867 (apropos (concat "\\`" (regexp-quote string) "\\'"))
2868 (if (fboundp symbol)
2869 (describe-function symbol)
2870 (describe-variable symbol)))))
2872 (defcustom widget-documentation-links t
2873 "Add hyperlinks to documentation strings when non-nil."
2874 :type 'boolean
2875 :group 'widget-documentation)
2877 (defcustom widget-documentation-link-regexp "`\\([^\n`' ]+\\)'"
2878 "Regexp for matching potential links in documentation strings.
2879 The first group should be the link itself."
2880 :type 'regexp
2881 :group 'widget-documentation)
2883 (defcustom widget-documentation-link-p 'intern-soft
2884 "Predicate used to test if a string is useful as a link.
2885 The value should be a function. The function will be called with one
2886 argument, a string, and should return non-nil if there should be a
2887 link for that string."
2888 :type 'function
2889 :options '(widget-documentation-link-p)
2890 :group 'widget-documentation)
2892 (defcustom widget-documentation-link-type 'documentation-link
2893 "Widget type used for links in documentation strings."
2894 :type 'symbol
2895 :group 'widget-documentation)
2897 (defun widget-documentation-link-add (widget from to)
2898 (widget-specify-doc widget from to)
2899 (when widget-documentation-links
2900 (let ((regexp widget-documentation-link-regexp)
2901 (buttons (widget-get widget :buttons))
2902 (widget-mouse-face (default-value 'widget-mouse-face))
2903 (widget-button-face widget-documentation-face)
2904 (widget-button-pressed-face widget-documentation-face))
2905 (save-excursion
2906 (goto-char from)
2907 (while (re-search-forward regexp to t)
2908 (let ((name (match-string 1))
2909 (begin (match-beginning 1))
2910 (end (match-end 1)))
2911 (when (funcall widget-documentation-link-p name)
2912 (push (widget-convert-button widget-documentation-link-type
2913 begin end :value name)
2914 buttons)))))
2915 (widget-put widget :buttons buttons)))
2916 (let ((indent (widget-get widget :indent)))
2917 (when (and indent (not (zerop indent)))
2918 (save-excursion
2919 (save-restriction
2920 (narrow-to-region from to)
2921 (goto-char (point-min))
2922 (while (search-forward "\n" nil t)
2923 (insert-char ?\s indent)))))))
2925 ;;; The `documentation-string' Widget.
2927 (define-widget 'documentation-string 'item
2928 "A documentation string."
2929 :format "%v"
2930 :action 'widget-documentation-string-action
2931 :value-create 'widget-documentation-string-value-create
2932 :visibility-widget 'visibility)
2934 (defun widget-documentation-string-value-create (widget)
2935 ;; Insert documentation string.
2936 (let ((doc (widget-value widget))
2937 (indent (widget-get widget :indent))
2938 (shown (widget-get (widget-get widget :parent) :documentation-shown))
2939 (start (point)))
2940 (if (string-match "\n" doc)
2941 (let ((before (substring doc 0 (match-beginning 0)))
2942 (after (substring doc (match-beginning 0)))
2943 button)
2944 (when (and indent (not (zerop indent)))
2945 (insert-char ?\s indent))
2946 (insert before ?\s)
2947 (widget-documentation-link-add widget start (point))
2948 (setq button
2949 (widget-create-child-and-convert
2950 widget (widget-get widget :visibility-widget)
2951 :help-echo "Show or hide rest of the documentation."
2952 :on "Hide"
2953 :off "More"
2954 :always-active t
2955 :action 'widget-parent-action
2956 shown))
2957 (when shown
2958 (setq start (point))
2959 (when (and indent (not (zerop indent)))
2960 (insert-char ?\s indent))
2961 (insert after)
2962 (widget-documentation-link-add widget start (point)))
2963 (widget-put widget :buttons (list button)))
2964 (when (and indent (not (zerop indent)))
2965 (insert-char ?\s indent))
2966 (insert doc)
2967 (widget-documentation-link-add widget start (point))))
2968 (insert ?\n))
2970 (defun widget-documentation-string-action (widget &rest ignore)
2971 ;; Toggle documentation.
2972 (let ((parent (widget-get widget :parent)))
2973 (widget-put parent :documentation-shown
2974 (not (widget-get parent :documentation-shown))))
2975 ;; Redraw.
2976 (widget-value-set widget (widget-value widget)))
2978 (defun widget-add-documentation-string-button (widget &rest args)
2979 "Insert a new `documentation-string' widget based on WIDGET.
2980 The new widget becomes a child of WIDGET, and is also added to
2981 its `:buttons' list. The documentation string is found from
2982 WIDGET using the function `widget-docstring'.
2983 Optional ARGS specifies additional keyword arguments for the
2984 `documentation-string' widget."
2985 (let ((doc (widget-docstring widget))
2986 (indent (widget-get widget :indent))
2987 (doc-indent (widget-get widget :documentation-indent)))
2988 (when doc
2989 (and (eq (preceding-char) ?\n)
2990 indent
2991 (insert-char ?\s indent))
2992 (unless (or (numberp doc-indent) (null doc-indent))
2993 (setq doc-indent 0))
2994 (widget-put widget :buttons
2995 (cons (apply 'widget-create-child-and-convert
2996 widget 'documentation-string
2997 :indent doc-indent
2998 (nconc args (list doc)))
2999 (widget-get widget :buttons))))))
3001 ;;; The Sexp Widgets.
3003 (define-widget 'const 'item
3004 "An immutable sexp."
3005 :prompt-value 'widget-const-prompt-value
3006 :format "%t\n%d")
3008 (defun widget-const-prompt-value (widget prompt value unbound)
3009 ;; Return the value of the const.
3010 (widget-value widget))
3012 (define-widget 'function-item 'const
3013 "An immutable function name."
3014 :format "%v\n%h"
3015 :documentation-property (lambda (symbol)
3016 (condition-case nil
3017 (documentation symbol t)
3018 (error nil))))
3020 (define-widget 'variable-item 'const
3021 "An immutable variable name."
3022 :format "%v\n%h"
3023 :documentation-property 'variable-documentation)
3025 (define-widget 'other 'sexp
3026 "Matches any value, but doesn't let the user edit the value.
3027 This is useful as last item in a `choice' widget.
3028 You should use this widget type with a default value,
3029 as in (other DEFAULT) or (other :tag \"NAME\" DEFAULT).
3030 If the user selects this alternative, that specifies DEFAULT
3031 as the value."
3032 :tag "Other"
3033 :format "%t%n"
3034 :value 'other)
3036 (defvar widget-string-prompt-value-history nil
3037 "History of input to `widget-string-prompt-value'.")
3039 (define-widget 'string 'editable-field
3040 "A string"
3041 :tag "String"
3042 :format "%{%t%}: %v"
3043 :complete-function 'ispell-complete-word
3044 :prompt-history 'widget-string-prompt-value-history)
3046 (defvar widget)
3048 (defun widget-string-complete ()
3049 "Complete contents of string field.
3050 Completions are taken from the :completion-alist property of the
3051 widget. If that isn't a list, it's evalled and expected to yield a list."
3052 (interactive)
3053 (let* ((completion-ignore-case (widget-get widget :completion-ignore-case))
3054 (alist (widget-get widget :completion-alist))
3055 (_ (unless (listp alist)
3056 (setq alist (eval alist)))))
3057 (completion-in-region (widget-field-start widget)
3058 (max (point) (widget-field-text-end widget))
3059 alist)))
3061 (define-widget 'regexp 'string
3062 "A regular expression."
3063 :match 'widget-regexp-match
3064 :validate 'widget-regexp-validate
3065 ;; Doesn't work well with terminating newline.
3066 ;; :value-face 'widget-single-line-field
3067 :tag "Regexp")
3069 (defun widget-regexp-match (widget value)
3070 ;; Match valid regexps.
3071 (and (stringp value)
3072 (condition-case nil
3073 (prog1 t
3074 (string-match value ""))
3075 (error nil))))
3077 (defun widget-regexp-validate (widget)
3078 "Check that the value of WIDGET is a valid regexp."
3079 (condition-case data
3080 (prog1 nil
3081 (string-match (widget-value widget) ""))
3082 (error (widget-put widget :error (error-message-string data))
3083 widget)))
3085 (define-widget 'file 'string
3086 "A file widget.
3087 It reads a file name from an editable text field."
3088 :complete-function 'widget-file-complete
3089 :prompt-value 'widget-file-prompt-value
3090 :format "%{%t%}: %v"
3091 ;; Doesn't work well with terminating newline.
3092 ;; :value-face 'widget-single-line-field
3093 :tag "File")
3095 (defun widget-file-complete ()
3096 "Perform completion on file name preceding point."
3097 (interactive)
3098 (completion-in-region (widget-field-start widget)
3099 (max (point) (widget-field-text-end widget))
3100 'completion-file-name-table))
3102 (defun widget-file-prompt-value (widget prompt value unbound)
3103 ;; Read file from minibuffer.
3104 (abbreviate-file-name
3105 (if unbound
3106 (read-file-name prompt)
3107 (let ((prompt2 (format "%s (default %s): " prompt value))
3108 (dir (file-name-directory value))
3109 (file (file-name-nondirectory value))
3110 (must-match (widget-get widget :must-match)))
3111 (read-file-name prompt2 dir nil must-match file)))))
3113 ;;;(defun widget-file-action (widget &optional event)
3114 ;;; ;; Read a file name from the minibuffer.
3115 ;;; (let* ((value (widget-value widget))
3116 ;;; (dir (file-name-directory value))
3117 ;;; (file (file-name-nondirectory value))
3118 ;;; (menu-tag (widget-apply widget :menu-tag-get))
3119 ;;; (must-match (widget-get widget :must-match))
3120 ;;; (answer (read-file-name (concat menu-tag " (default " value "): ")
3121 ;;; dir nil must-match file)))
3122 ;;; (widget-value-set widget (abbreviate-file-name answer))
3123 ;;; (widget-setup)
3124 ;;; (widget-apply widget :notify widget event)))
3126 ;; Fixme: use file-name-as-directory.
3127 (define-widget 'directory 'file
3128 "A directory widget.
3129 It reads a directory name from an editable text field."
3130 :tag "Directory")
3132 (defvar widget-symbol-prompt-value-history nil
3133 "History of input to `widget-symbol-prompt-value'.")
3135 (define-widget 'symbol 'editable-field
3136 "A Lisp symbol."
3137 :value nil
3138 :tag "Symbol"
3139 :format "%{%t%}: %v"
3140 :match (lambda (widget value) (symbolp value))
3141 :complete-function 'lisp-complete-symbol
3142 :prompt-internal 'widget-symbol-prompt-internal
3143 :prompt-match 'symbolp
3144 :prompt-history 'widget-symbol-prompt-value-history
3145 :value-to-internal (lambda (widget value)
3146 (if (symbolp value)
3147 (symbol-name value)
3148 value))
3149 :value-to-external (lambda (widget value)
3150 (if (stringp value)
3151 (intern value)
3152 value)))
3154 (defun widget-symbol-prompt-internal (widget prompt initial history)
3155 ;; Read file from minibuffer.
3156 (let ((answer (completing-read prompt obarray
3157 (widget-get widget :prompt-match)
3158 nil initial history)))
3159 (if (and (stringp answer)
3160 (not (zerop (length answer))))
3161 answer
3162 (error "No value"))))
3164 (defvar widget-function-prompt-value-history nil
3165 "History of input to `widget-function-prompt-value'.")
3167 (define-widget 'function 'restricted-sexp
3168 "A Lisp function."
3169 :complete-function (lambda ()
3170 (interactive)
3171 (lisp-complete-symbol 'fboundp))
3172 :prompt-value 'widget-field-prompt-value
3173 :prompt-internal 'widget-symbol-prompt-internal
3174 :prompt-match 'fboundp
3175 :prompt-history 'widget-function-prompt-value-history
3176 :action 'widget-field-action
3177 :match-alternatives '(functionp)
3178 :validate (lambda (widget)
3179 (unless (functionp (widget-value widget))
3180 (widget-put widget :error (format "Invalid function: %S"
3181 (widget-value widget)))
3182 widget))
3183 :value 'ignore
3184 :tag "Function")
3186 (defvar widget-variable-prompt-value-history nil
3187 "History of input to `widget-variable-prompt-value'.")
3189 (define-widget 'variable 'symbol
3190 "A Lisp variable."
3191 :prompt-match 'boundp
3192 :prompt-history 'widget-variable-prompt-value-history
3193 :complete-function (lambda ()
3194 (interactive)
3195 (lisp-complete-symbol 'boundp))
3196 :tag "Variable")
3198 (define-widget 'coding-system 'symbol
3199 "A MULE coding-system."
3200 :format "%{%t%}: %v"
3201 :tag "Coding system"
3202 :base-only nil
3203 :prompt-history 'coding-system-value-history
3204 :prompt-value 'widget-coding-system-prompt-value
3205 :action 'widget-coding-system-action
3206 :complete-function (lambda ()
3207 (interactive)
3208 (lisp-complete-symbol 'coding-system-p))
3209 :validate (lambda (widget)
3210 (unless (coding-system-p (widget-value widget))
3211 (widget-put widget :error (format "Invalid coding system: %S"
3212 (widget-value widget)))
3213 widget))
3214 :value 'undecided
3215 :prompt-match 'coding-system-p)
3217 (defun widget-coding-system-prompt-value (widget prompt value unbound)
3218 "Read coding-system from minibuffer."
3219 (if (widget-get widget :base-only)
3220 (intern
3221 (completing-read (format "%s (default %s): " prompt value)
3222 (mapcar #'list (coding-system-list t)) nil nil nil
3223 coding-system-history))
3224 (read-coding-system (format "%s (default %s): " prompt value) value)))
3226 (defun widget-coding-system-action (widget &optional event)
3227 (let ((answer
3228 (widget-coding-system-prompt-value
3229 widget
3230 (widget-apply widget :menu-tag-get)
3231 (widget-value widget)
3232 t)))
3233 (widget-value-set widget answer)
3234 (widget-apply widget :notify widget event)
3235 (widget-setup)))
3237 ;;; I'm not sure about what this is good for? KFS.
3238 (defvar widget-key-sequence-prompt-value-history nil
3239 "History of input to `widget-key-sequence-prompt-value'.")
3241 (defvar widget-key-sequence-default-value [ignore]
3242 "Default value for an empty key sequence.")
3244 (defvar widget-key-sequence-map
3245 (let ((map (make-sparse-keymap)))
3246 (set-keymap-parent map widget-field-keymap)
3247 (define-key map [(control ?q)] 'widget-key-sequence-read-event)
3248 map))
3250 (define-widget 'key-sequence 'restricted-sexp
3251 "A key sequence."
3252 :prompt-value 'widget-field-prompt-value
3253 :prompt-internal 'widget-symbol-prompt-internal
3254 ; :prompt-match 'fboundp ;; What was this good for? KFS
3255 :prompt-history 'widget-key-sequence-prompt-value-history
3256 :action 'widget-field-action
3257 :match-alternatives '(stringp vectorp)
3258 :format "%{%t%}: %v"
3259 :validate 'widget-key-sequence-validate
3260 :value-to-internal 'widget-key-sequence-value-to-internal
3261 :value-to-external 'widget-key-sequence-value-to-external
3262 :value widget-key-sequence-default-value
3263 :keymap widget-key-sequence-map
3264 :help-echo "C-q: insert KEY, EVENT, or CODE; RET: enter value"
3265 :tag "Key sequence")
3267 (defun widget-key-sequence-read-event (ev)
3268 (interactive (list
3269 (let ((inhibit-quit t) quit-flag)
3270 (read-event "Insert KEY, EVENT, or CODE: "))))
3271 (let ((ev2 (and (memq 'down (event-modifiers ev))
3272 (read-event)))
3273 (tr (and (keymapp function-key-map)
3274 (lookup-key function-key-map (vector ev)))))
3275 (when (and (integerp ev)
3276 (or (and (<= ?0 ev) (< ev (+ ?0 (min 10 read-quoted-char-radix))))
3277 (and (<= ?a (downcase ev))
3278 (< (downcase ev) (+ ?a -10 (min 36 read-quoted-char-radix))))))
3279 (setq unread-command-events (cons ev unread-command-events)
3280 ev (read-quoted-char (format "Enter code (radix %d)" read-quoted-char-radix))
3281 tr nil)
3282 (if (and (integerp ev) (not (characterp ev)))
3283 (insert (char-to-string ev)))) ;; throw invalid char error
3284 (setq ev (key-description (list ev)))
3285 (when (arrayp tr)
3286 (setq tr (key-description (list (aref tr 0))))
3287 (if (y-or-n-p (format "Key %s is translated to %s -- use %s? " ev tr tr))
3288 (setq ev tr ev2 nil)))
3289 (insert (if (= (char-before) ?\s) "" " ") ev " ")
3290 (if ev2
3291 (insert (key-description (list ev2)) " "))))
3293 (defun widget-key-sequence-validate (widget)
3294 (unless (or (stringp (widget-value widget))
3295 (vectorp (widget-value widget)))
3296 (widget-put widget :error (format "Invalid key sequence: %S"
3297 (widget-value widget)))
3298 widget))
3300 (defun widget-key-sequence-value-to-internal (widget value)
3301 (if (widget-apply widget :match value)
3302 (if (equal value widget-key-sequence-default-value)
3304 (key-description value))
3305 value))
3307 (defun widget-key-sequence-value-to-external (widget value)
3308 (if (stringp value)
3309 (if (string-match "\\`[[:space:]]*\\'" value)
3310 widget-key-sequence-default-value
3311 (read-kbd-macro value))
3312 value))
3315 (define-widget 'sexp 'editable-field
3316 "An arbitrary Lisp expression."
3317 :tag "Lisp expression"
3318 :format "%{%t%}: %v"
3319 :value nil
3320 :validate 'widget-sexp-validate
3321 :match (lambda (widget value) t)
3322 :value-to-internal 'widget-sexp-value-to-internal
3323 :value-to-external (lambda (widget value) (read value))
3324 :prompt-history 'widget-sexp-prompt-value-history
3325 :prompt-value 'widget-sexp-prompt-value)
3327 (defun widget-sexp-value-to-internal (widget value)
3328 ;; Use pp for printer representation.
3329 (let ((pp (if (symbolp value)
3330 (prin1-to-string value)
3331 (pp-to-string value))))
3332 (while (string-match "\n\\'" pp)
3333 (setq pp (substring pp 0 -1)))
3334 (if (or (string-match "\n\\'" pp)
3335 (> (length pp) 40))
3336 (concat "\n" pp)
3337 pp)))
3339 (defun widget-sexp-validate (widget)
3340 ;; Valid if we can read the string and there is no junk left after it.
3341 (with-temp-buffer
3342 (insert (widget-apply widget :value-get))
3343 (goto-char (point-min))
3344 (let (err)
3345 (condition-case data
3346 (progn
3347 ;; Avoid a confusing end-of-file error.
3348 (skip-syntax-forward "\\s-")
3349 (if (eobp)
3350 (setq err "Empty sexp -- use `nil'?")
3351 (unless (widget-apply widget :match (read (current-buffer)))
3352 (setq err (widget-get widget :type-error))))
3353 ;; Allow whitespace after expression.
3354 (skip-syntax-forward "\\s-")
3355 (if (and (not (eobp))
3356 (not err))
3357 (setq err (format "Junk at end of expression: %s"
3358 (buffer-substring (point)
3359 (point-max))))))
3360 (end-of-file ; Avoid confusing error message.
3361 (setq err "Unbalanced sexp"))
3362 (error (setq err (error-message-string data))))
3363 (if (not err)
3365 (widget-put widget :error err)
3366 widget))))
3368 (defvar widget-sexp-prompt-value-history nil
3369 "History of input to `widget-sexp-prompt-value'.")
3371 (defun widget-sexp-prompt-value (widget prompt value unbound)
3372 ;; Read an arbitrary sexp.
3373 (let ((found (read-string prompt
3374 (if unbound nil (cons (prin1-to-string value) 0))
3375 (widget-get widget :prompt-history))))
3376 (let ((answer (read-from-string found)))
3377 (unless (= (cdr answer) (length found))
3378 (error "Junk at end of expression: %s"
3379 (substring found (cdr answer))))
3380 (car answer))))
3382 (define-widget 'restricted-sexp 'sexp
3383 "A Lisp expression restricted to values that match.
3384 To use this type, you must define :match or :match-alternatives."
3385 :type-error "The specified value is not valid"
3386 :match 'widget-restricted-sexp-match
3387 :value-to-internal (lambda (widget value)
3388 (if (widget-apply widget :match value)
3389 (prin1-to-string value)
3390 value)))
3392 (defun widget-restricted-sexp-match (widget value)
3393 (let ((alternatives (widget-get widget :match-alternatives))
3394 matched)
3395 (while (and alternatives (not matched))
3396 (if (cond ((functionp (car alternatives))
3397 (funcall (car alternatives) value))
3398 ((and (consp (car alternatives))
3399 (eq (car (car alternatives)) 'quote))
3400 (eq value (nth 1 (car alternatives)))))
3401 (setq matched t))
3402 (setq alternatives (cdr alternatives)))
3403 matched))
3405 (define-widget 'integer 'restricted-sexp
3406 "An integer."
3407 :tag "Integer"
3408 :value 0
3409 :type-error "This field should contain an integer"
3410 :match-alternatives '(integerp))
3412 (define-widget 'number 'restricted-sexp
3413 "A number (floating point or integer)."
3414 :tag "Number"
3415 :value 0.0
3416 :type-error "This field should contain a number (floating point or integer)"
3417 :match-alternatives '(numberp))
3419 (define-widget 'float 'restricted-sexp
3420 "A floating point number."
3421 :tag "Floating point number"
3422 :value 0.0
3423 :type-error "This field should contain a floating point number"
3424 :match-alternatives '(floatp))
3426 (define-widget 'character 'editable-field
3427 "A character."
3428 :tag "Character"
3429 :value 0
3430 :size 1
3431 :format "%{%t%}: %v\n"
3432 :valid-regexp "\\`.\\'"
3433 :error "This field should contain a single character"
3434 :value-to-internal (lambda (widget value)
3435 (if (stringp value)
3436 value
3437 (char-to-string value)))
3438 :value-to-external (lambda (widget value)
3439 (if (stringp value)
3440 (aref value 0)
3441 value))
3442 :match (lambda (widget value)
3443 (characterp value)))
3445 (define-widget 'list 'group
3446 "A Lisp list."
3447 :tag "List"
3448 :format "%{%t%}:\n%v")
3450 (define-widget 'vector 'group
3451 "A Lisp vector."
3452 :tag "Vector"
3453 :format "%{%t%}:\n%v"
3454 :match 'widget-vector-match
3455 :value-to-internal (lambda (widget value) (append value nil))
3456 :value-to-external (lambda (widget value) (apply 'vector value)))
3458 (defun widget-vector-match (widget value)
3459 (and (vectorp value)
3460 (widget-group-match widget
3461 (widget-apply widget :value-to-internal value))))
3463 (define-widget 'cons 'group
3464 "A cons-cell."
3465 :tag "Cons-cell"
3466 :format "%{%t%}:\n%v"
3467 :match 'widget-cons-match
3468 :value-to-internal (lambda (widget value)
3469 (list (car value) (cdr value)))
3470 :value-to-external (lambda (widget value)
3471 (apply 'cons value)))
3473 (defun widget-cons-match (widget value)
3474 (and (consp value)
3475 (widget-group-match widget
3476 (widget-apply widget :value-to-internal value))))
3478 ;;; The `lazy' Widget.
3480 ;; Recursive datatypes.
3482 (define-widget 'lazy 'default
3483 "Base widget for recursive datastructures.
3485 The `lazy' widget will, when instantiated, contain a single inferior
3486 widget, of the widget type specified by the :type parameter. The
3487 value of the `lazy' widget is the same as the value of the inferior
3488 widget. When deriving a new widget from the 'lazy' widget, the :type
3489 parameter is allowed to refer to the widget currently being defined,
3490 thus allowing recursive datastructures to be described.
3492 The :type parameter takes the same arguments as the defcustom
3493 parameter with the same name.
3495 Most composite widgets, i.e. widgets containing other widgets, does
3496 not allow recursion. That is, when you define a new widget type, none
3497 of the inferior widgets may be of the same type you are currently
3498 defining.
3500 In Lisp, however, it is custom to define datastructures in terms of
3501 themselves. A list, for example, is defined as either nil, or a cons
3502 cell whose cdr itself is a list. The obvious way to translate this
3503 into a widget type would be
3505 (define-widget 'my-list 'choice
3506 \"A list of sexps.\"
3507 :tag \"Sexp list\"
3508 :args '((const nil) (cons :value (nil) sexp my-list)))
3510 Here we attempt to define my-list as a choice of either the constant
3511 nil, or a cons-cell containing a sexp and my-lisp. This will not work
3512 because the `choice' widget does not allow recursion.
3514 Using the `lazy' widget you can overcome this problem, as in this
3515 example:
3517 (define-widget 'sexp-list 'lazy
3518 \"A list of sexps.\"
3519 :tag \"Sexp list\"
3520 :type '(choice (const nil) (cons :value (nil) sexp sexp-list)))"
3521 :format "%{%t%}: %v"
3522 ;; We don't convert :type because we want to allow recursive
3523 ;; datastructures. This is slow, so we should not create speed
3524 ;; critical widgets by deriving from this.
3525 :convert-widget 'widget-value-convert-widget
3526 :value-create 'widget-type-value-create
3527 :value-get 'widget-child-value-get
3528 :value-inline 'widget-child-value-inline
3529 :default-get 'widget-type-default-get
3530 :match 'widget-type-match
3531 :validate 'widget-child-validate)
3534 ;;; The `plist' Widget.
3536 ;; Property lists.
3538 (define-widget 'plist 'list
3539 "A property list."
3540 :key-type '(symbol :tag "Key")
3541 :value-type '(sexp :tag "Value")
3542 :convert-widget 'widget-plist-convert-widget
3543 :tag "Plist")
3545 (defvar widget-plist-value-type) ;Dynamic variable
3547 (defun widget-plist-convert-widget (widget)
3548 ;; Handle `:options'.
3549 (let* ((options (widget-get widget :options))
3550 (widget-plist-value-type (widget-get widget :value-type))
3551 (other `(editable-list :inline t
3552 (group :inline t
3553 ,(widget-get widget :key-type)
3554 ,widget-plist-value-type)))
3555 (args (if options
3556 (list `(checklist :inline t
3557 :greedy t
3558 ,@(mapcar 'widget-plist-convert-option
3559 options))
3560 other)
3561 (list other))))
3562 (widget-put widget :args args)
3563 widget))
3565 (defun widget-plist-convert-option (option)
3566 ;; Convert a single plist option.
3567 (let (key-type value-type)
3568 (if (listp option)
3569 (let ((key (nth 0 option)))
3570 (setq value-type (nth 1 option))
3571 (if (listp key)
3572 (setq key-type key)
3573 (setq key-type `(const ,key))))
3574 (setq key-type `(const ,option)
3575 value-type widget-plist-value-type))
3576 `(group :format "Key: %v" :inline t ,key-type ,value-type)))
3579 ;;; The `alist' Widget.
3581 ;; Association lists.
3583 (define-widget 'alist 'list
3584 "An association list."
3585 :key-type '(sexp :tag "Key")
3586 :value-type '(sexp :tag "Value")
3587 :convert-widget 'widget-alist-convert-widget
3588 :tag "Alist")
3590 (defvar widget-alist-value-type) ;Dynamic variable
3592 (defun widget-alist-convert-widget (widget)
3593 ;; Handle `:options'.
3594 (let* ((options (widget-get widget :options))
3595 (widget-alist-value-type (widget-get widget :value-type))
3596 (other `(editable-list :inline t
3597 (cons :format "%v"
3598 ,(widget-get widget :key-type)
3599 ,widget-alist-value-type)))
3600 (args (if options
3601 (list `(checklist :inline t
3602 :greedy t
3603 ,@(mapcar 'widget-alist-convert-option
3604 options))
3605 other)
3606 (list other))))
3607 (widget-put widget :args args)
3608 widget))
3610 (defun widget-alist-convert-option (option)
3611 ;; Convert a single alist option.
3612 (let (key-type value-type)
3613 (if (listp option)
3614 (let ((key (nth 0 option)))
3615 (setq value-type (nth 1 option))
3616 (if (listp key)
3617 (setq key-type key)
3618 (setq key-type `(const ,key))))
3619 (setq key-type `(const ,option)
3620 value-type widget-alist-value-type))
3621 `(cons :format "Key: %v" ,key-type ,value-type)))
3623 (define-widget 'choice 'menu-choice
3624 "A union of several sexp types."
3625 :tag "Choice"
3626 :format "%{%t%}: %[Value Menu%] %v"
3627 :button-prefix 'widget-push-button-prefix
3628 :button-suffix 'widget-push-button-suffix
3629 :prompt-value 'widget-choice-prompt-value)
3631 (defun widget-choice-prompt-value (widget prompt value unbound)
3632 "Make a choice."
3633 (let ((args (widget-get widget :args))
3634 (completion-ignore-case (widget-get widget :case-fold))
3635 current choices old)
3636 ;; Find the first arg that matches VALUE.
3637 (let ((look args))
3638 (while look
3639 (if (widget-apply (car look) :match value)
3640 (setq old (car look)
3641 look nil)
3642 (setq look (cdr look)))))
3643 ;; Find new choice.
3644 (setq current
3645 (cond ((= (length args) 0)
3646 nil)
3647 ((= (length args) 1)
3648 (nth 0 args))
3649 ((and (= (length args) 2)
3650 (memq old args))
3651 (if (eq old (nth 0 args))
3652 (nth 1 args)
3653 (nth 0 args)))
3655 (while args
3656 (setq current (car args)
3657 args (cdr args))
3658 (setq choices
3659 (cons (cons (widget-apply current :menu-tag-get)
3660 current)
3661 choices)))
3662 (let ((val (completing-read prompt choices nil t)))
3663 (if (stringp val)
3664 (let ((try (try-completion val choices)))
3665 (when (stringp try)
3666 (setq val try))
3667 (cdr (assoc val choices)))
3668 nil)))))
3669 (if current
3670 (widget-prompt-value current prompt nil t)
3671 value)))
3673 (define-widget 'radio 'radio-button-choice
3674 "A union of several sexp types."
3675 :tag "Choice"
3676 :format "%{%t%}:\n%v"
3677 :prompt-value 'widget-choice-prompt-value)
3679 (define-widget 'repeat 'editable-list
3680 "A variable length homogeneous list."
3681 :tag "Repeat"
3682 :format "%{%t%}:\n%v%i\n")
3684 (define-widget 'set 'checklist
3685 "A list of members from a fixed set."
3686 :tag "Set"
3687 :format "%{%t%}:\n%v")
3689 (define-widget 'boolean 'toggle
3690 "To be nil or non-nil, that is the question."
3691 :tag "Boolean"
3692 :prompt-value 'widget-boolean-prompt-value
3693 :button-prefix 'widget-push-button-prefix
3694 :button-suffix 'widget-push-button-suffix
3695 :format "%{%t%}: %[Toggle%] %v\n"
3696 :on "on (non-nil)"
3697 :off "off (nil)")
3699 (defun widget-boolean-prompt-value (widget prompt value unbound)
3700 ;; Toggle a boolean.
3701 (y-or-n-p prompt))
3703 ;;; The `color' Widget.
3705 ;; Fixme: match
3706 (define-widget 'color 'editable-field
3707 "Choose a color name (with sample)."
3708 :format "%{%t%}: %v (%{sample%})\n"
3709 :value-create 'widget-color-value-create
3710 :size 10
3711 :tag "Color"
3712 :value "black"
3713 :complete 'widget-color-complete
3714 :sample-face-get 'widget-color-sample-face-get
3715 :notify 'widget-color-notify
3716 :action 'widget-color-action)
3718 (defun widget-color-value-create (widget)
3719 (widget-field-value-create widget)
3720 (widget-insert " ")
3721 (widget-create-child-and-convert
3722 widget 'push-button
3723 :tag "Choose" :action 'widget-color--choose-action)
3724 (widget-insert " "))
3726 (defun widget-color--choose-action (widget &optional event)
3727 (list-colors-display
3728 nil nil
3729 `(lambda (color)
3730 (when (buffer-live-p ,(current-buffer))
3731 (widget-value-set ',(widget-get widget :parent) color)
3732 (let* ((buf (get-buffer "*Colors*"))
3733 (win (get-buffer-window buf 0)))
3734 (bury-buffer buf)
3735 (and win (> (length (window-list)) 1)
3736 (delete-window win)))
3737 (pop-to-buffer ,(current-buffer))))))
3739 (defun widget-color-complete (widget)
3740 "Complete the color in WIDGET."
3741 (require 'facemenu) ; for facemenu-color-alist
3742 (completion-in-region (widget-field-start widget)
3743 (max (point) (widget-field-text-end widget))
3744 (or facemenu-color-alist
3745 (sort (defined-colors) 'string-lessp))))
3747 (defun widget-color-sample-face-get (widget)
3748 (let* ((value (condition-case nil
3749 (widget-value widget)
3750 (error (widget-get widget :value)))))
3751 (if (color-defined-p value)
3752 (list (cons 'foreground-color value))
3753 'default)))
3755 (defun widget-color-action (widget &optional event)
3756 "Prompt for a color."
3757 (let* ((tag (widget-apply widget :menu-tag-get))
3758 (prompt (concat tag ": "))
3759 (value (widget-value widget))
3760 (start (widget-field-start widget))
3761 (answer (facemenu-read-color prompt)))
3762 (unless (zerop (length answer))
3763 (widget-value-set widget answer)
3764 (widget-setup)
3765 (widget-apply widget :notify widget event))))
3767 (defun widget-color-notify (widget child &optional event)
3768 "Update the sample, and notify the parent."
3769 (overlay-put (widget-get widget :sample-overlay)
3770 'face (widget-apply widget :sample-face-get))
3771 (widget-default-notify widget child event))
3773 ;;; The Help Echo
3775 (defun widget-echo-help (pos)
3776 "Display help-echo text for widget at POS."
3777 (let* ((widget (widget-at pos))
3778 (help-echo (and widget (widget-get widget :help-echo))))
3779 (if (functionp help-echo)
3780 (setq help-echo (funcall help-echo widget)))
3781 (if help-echo (message "%s" (eval help-echo)))))
3783 ;;; The End:
3785 (provide 'wid-edit)
3787 ;; arch-tag: a076e75e-18a1-4b46-8be5-3f317bcbc707
3788 ;;; wid-edit.el ends here