Fix arg usage of format-decode-run-method (Bug#7488).
[emacs.git] / lisp / wid-edit.el
blobe75aba222690d51fa4129f54d1aec22b501a9395
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
82 :group 'hypermedia)
84 (defgroup widget-documentation nil
85 "Options controlling the display of documentation strings."
86 :group 'widgets)
88 (defgroup widget-faces nil
89 "Faces used by the widget library."
90 :group 'widgets
91 :group 'faces)
93 (defvar widget-documentation-face 'widget-documentation
94 "Face used for documentation strings in widgets.
95 This exists as a variable so it can be set locally in certain buffers.")
97 (defface widget-documentation '((((class color)
98 (background dark))
99 (:foreground "lime green"))
100 (((class color)
101 (background light))
102 (:foreground "dark green"))
103 (t nil))
104 "Face used for documentation text."
105 :group 'widget-documentation
106 :group 'widget-faces)
107 (define-obsolete-face-alias 'widget-documentation-face
108 'widget-documentation "22.1")
110 (defvar widget-button-face 'widget-button
111 "Face used for buttons in widgets.
112 This exists as a variable so it can be set locally in certain buffers.")
114 (defface widget-button '((t (:weight bold)))
115 "Face used for widget buttons."
116 :group 'widget-faces)
117 (define-obsolete-face-alias 'widget-button-face 'widget-button "22.1")
119 (defcustom widget-mouse-face 'highlight
120 "Face used for widget buttons when the mouse is above them."
121 :type 'face
122 :group 'widget-faces)
124 ;; TTY gets special definitions here and in the next defface, because
125 ;; the gray colors defined for other displays cause black text on a black
126 ;; background, at least on light-background TTYs.
127 (defface widget-field '((((type tty))
128 :background "yellow3"
129 :foreground "black")
130 (((class grayscale color)
131 (background light))
132 :background "gray85")
133 (((class grayscale color)
134 (background dark))
135 :background "dim gray")
137 :slant italic))
138 "Face used for editable fields."
139 :group 'widget-faces)
140 (define-obsolete-face-alias 'widget-field-face 'widget-field "22.1")
142 (defface widget-single-line-field '((((type tty))
143 :background "green3"
144 :foreground "black")
145 (((class grayscale color)
146 (background light))
147 :background "gray85")
148 (((class grayscale color)
149 (background dark))
150 :background "dim gray")
152 :slant italic))
153 "Face used for editable fields spanning only a single line."
154 :group 'widget-faces)
155 (define-obsolete-face-alias 'widget-single-line-field-face
156 'widget-single-line-field "22.1")
158 ;;; This causes display-table to be loaded, and not usefully.
159 ;;;(defvar widget-single-line-display-table
160 ;;; (let ((table (make-display-table)))
161 ;;; (aset table 9 "^I")
162 ;;; (aset table 10 "^J")
163 ;;; table)
164 ;;; "Display table used for single-line editable fields.")
166 ;;;(when (fboundp 'set-face-display-table)
167 ;;; (set-face-display-table 'widget-single-line-field-face
168 ;;; widget-single-line-display-table))
170 ;;; Utility functions.
172 ;; These are not really widget specific.
174 (defun widget-princ-to-string (object)
175 "Return string representation of OBJECT, any Lisp object.
176 No quoting characters are used; no delimiters are printed around
177 the contents of strings."
178 (with-output-to-string
179 (princ object)))
181 (defun widget-clear-undo ()
182 "Clear all undo information."
183 (buffer-disable-undo (current-buffer))
184 (buffer-enable-undo))
186 (defcustom widget-menu-max-size 40
187 "Largest number of items allowed in a popup-menu.
188 Larger menus are read through the minibuffer."
189 :group 'widgets
190 :type 'integer)
192 (defcustom widget-menu-max-shortcuts 40
193 "Largest number of items for which it works to choose one with a character.
194 For a larger number of items, the minibuffer is used."
195 :group 'widgets
196 :type 'integer)
198 (defcustom widget-menu-minibuffer-flag nil
199 "Control how to ask for a choice from the keyboard.
200 Non-nil means use the minibuffer;
201 nil means read a single character."
202 :group 'widgets
203 :type 'boolean)
205 (defun widget-choose (title items &optional event)
206 "Choose an item from a list.
208 First argument TITLE is the name of the list.
209 Second argument ITEMS is a list whose members are either
210 (NAME . VALUE), to indicate selectable items, or just strings to
211 indicate unselectable items.
212 Optional third argument EVENT is an input event.
214 The user is asked to choose between each NAME from the items alist,
215 and the VALUE of the chosen element will be returned. If EVENT is a
216 mouse event, and the number of elements in items is less than
217 `widget-menu-max-size', a popup menu will be used, otherwise the
218 minibuffer."
219 (cond ((and (< (length items) widget-menu-max-size)
220 event (display-popup-menus-p))
221 ;; Mouse click.
222 (x-popup-menu event
223 (list title (cons "" items))))
224 ((or widget-menu-minibuffer-flag
225 (> (length items) widget-menu-max-shortcuts))
226 ;; Read the choice of name from the minibuffer.
227 (setq items (widget-remove-if 'stringp items))
228 (let ((val (completing-read (concat title ": ") items nil t)))
229 (if (stringp val)
230 (let ((try (try-completion val items)))
231 (when (stringp try)
232 (setq val try))
233 (cdr (assoc val items))))))
235 ;; Construct a menu of the choices
236 ;; and then use it for prompting for a single character.
237 (let* ((overriding-terminal-local-map (make-sparse-keymap))
238 (next-digit ?0)
239 map choice some-choice-enabled value)
240 ;; Define SPC as a prefix char to get to this menu.
241 (define-key overriding-terminal-local-map " "
242 (setq map (make-sparse-keymap title)))
243 (with-current-buffer (get-buffer-create " widget-choose")
244 (erase-buffer)
245 (insert "Available choices:\n\n")
246 (while items
247 (setq choice (car items) items (cdr items))
248 (if (consp choice)
249 (let* ((name (car choice))
250 (function (cdr choice)))
251 (insert (format "%c = %s\n" next-digit name))
252 (define-key map (vector next-digit) function)
253 (setq some-choice-enabled t)))
254 ;; Allocate digits to disabled alternatives
255 ;; so that the digit of a given alternative never varies.
256 (setq next-digit (1+ next-digit)))
257 (insert "\nC-g = Quit"))
258 (or some-choice-enabled
259 (error "None of the choices is currently meaningful"))
260 (define-key map [?\C-g] 'keyboard-quit)
261 (define-key map [t] 'keyboard-quit)
262 (define-key map [?\M-\C-v] 'scroll-other-window)
263 (define-key map [?\M--] 'negative-argument)
264 (setcdr map (nreverse (cdr map)))
265 ;; Read a char with the menu, and return the result
266 ;; that corresponds to it.
267 (save-window-excursion
268 (let ((buf (get-buffer " widget-choose")))
269 (fit-window-to-buffer (display-buffer buf))
270 (let ((cursor-in-echo-area t)
271 keys
272 (char 0)
273 (arg 1))
274 (while (not (or (and (integerp char)
275 (>= char ?0) (< char next-digit))
276 (eq value 'keyboard-quit)))
277 ;; Unread a SPC to lead to our new menu.
278 (setq unread-command-events (cons ?\s unread-command-events))
279 (setq keys (read-key-sequence title))
280 (setq value
281 (lookup-key overriding-terminal-local-map keys t)
282 char (aref keys 1))
283 (cond ((eq value 'scroll-other-window)
284 (let ((minibuffer-scroll-window
285 (get-buffer-window buf)))
286 (if (> 0 arg)
287 (scroll-other-window-down
288 (window-height minibuffer-scroll-window))
289 (scroll-other-window))
290 (setq arg 1)))
291 ((eq value 'negative-argument)
292 (setq arg -1))
294 (setq arg 1)))))))
295 (when (eq value 'keyboard-quit)
296 (error "Canceled"))
297 value))))
299 (defun widget-remove-if (predictate list)
300 (let (result (tail list))
301 (while tail
302 (or (funcall predictate (car tail))
303 (setq result (cons (car tail) result)))
304 (setq tail (cdr tail)))
305 (nreverse result)))
307 ;;; Widget text specifications.
309 ;; These functions are for specifying text properties.
311 ;; We can set it to nil now that get_local_map uses get_pos_property.
312 (defconst widget-field-add-space nil
313 "Non-nil means add extra space at the end of editable text fields.
314 If you don't add the space, it will become impossible to edit a zero
315 size field.")
317 (defvar widget-field-use-before-change t
318 "Non-nil means use `before-change-functions' to track editable fields.
319 This enables the use of undo. Using before hooks also means that
320 the :notify function can't know the 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 (setq specs (nreverse specs))
642 (find-image specs)))
644 ;; Oh well.
645 nil)))
647 (defvar widget-button-pressed-face 'widget-button-pressed
648 "Face used for pressed buttons in widgets.
649 This exists as a variable so it can be set locally in certain
650 buffers.")
652 (defun widget-image-insert (widget tag image &optional down inactive)
653 "In WIDGET, insert the text TAG or, if supported, IMAGE.
654 IMAGE should either be an image or an image file name sans extension
655 \(xpm, xbm, gif, jpg, or png) located in `widget-image-directory'.
657 Optional arguments DOWN and INACTIVE are used instead of IMAGE when the
658 button is pressed or inactive, respectively. These are currently ignored."
659 (if (and (display-graphic-p)
660 (setq image (widget-image-find image)))
661 (progn (widget-put widget :suppress-face t)
662 (insert-image image tag))
663 (insert tag)))
665 (defun widget-move-and-invoke (event)
666 "Move to where you click, and if it is an active field, invoke it."
667 (interactive "e")
668 (mouse-set-point event)
669 (let ((pos (widget-event-point event)))
670 (if (and pos (get-char-property pos 'button))
671 (widget-button-click event))))
673 ;;; Buttons.
675 (defgroup widget-button nil
676 "The look of various kinds of buttons."
677 :group 'widgets)
679 (defcustom widget-button-prefix ""
680 "String used as prefix for buttons."
681 :type 'string
682 :group 'widget-button)
684 (defcustom widget-button-suffix ""
685 "String used as suffix for buttons."
686 :type 'string
687 :group 'widget-button)
689 ;;; Creating Widgets.
691 ;;;###autoload
692 (defun widget-create (type &rest args)
693 "Create widget of TYPE.
694 The optional ARGS are additional keyword arguments."
695 (let ((widget (apply 'widget-convert type args)))
696 (widget-apply widget :create)
697 widget))
699 (defun widget-create-child-and-convert (parent type &rest args)
700 "As part of the widget PARENT, create a child widget TYPE.
701 The child is converted, using the keyword arguments ARGS."
702 (let ((widget (apply 'widget-convert type args)))
703 (widget-put widget :parent parent)
704 (unless (widget-get widget :indent)
705 (widget-put widget :indent (+ (or (widget-get parent :indent) 0)
706 (or (widget-get widget :extra-offset) 0)
707 (widget-get parent :offset))))
708 (widget-apply widget :create)
709 widget))
711 (defun widget-create-child (parent type)
712 "Create widget of TYPE."
713 (let ((widget (widget-copy type)))
714 (widget-put widget :parent parent)
715 (unless (widget-get widget :indent)
716 (widget-put widget :indent (+ (or (widget-get parent :indent) 0)
717 (or (widget-get widget :extra-offset) 0)
718 (widget-get parent :offset))))
719 (widget-apply widget :create)
720 widget))
722 (defun widget-create-child-value (parent type value)
723 "Create widget of TYPE with value VALUE."
724 (let ((widget (widget-copy type)))
725 (widget-put widget :value (widget-apply widget :value-to-internal value))
726 (widget-put widget :parent parent)
727 (unless (widget-get widget :indent)
728 (widget-put widget :indent (+ (or (widget-get parent :indent) 0)
729 (or (widget-get widget :extra-offset) 0)
730 (widget-get parent :offset))))
731 (widget-apply widget :create)
732 widget))
734 ;;;###autoload
735 (defun widget-delete (widget)
736 "Delete WIDGET."
737 (widget-apply widget :delete))
739 (defun widget-copy (widget)
740 "Make a deep copy of WIDGET."
741 (widget-apply (copy-sequence widget) :copy))
743 (defun widget-convert (type &rest args)
744 "Convert TYPE to a widget without inserting it in the buffer.
745 The optional ARGS are additional keyword arguments."
746 ;; Don't touch the type.
747 (let* ((widget (if (symbolp type)
748 (list type)
749 (copy-sequence type)))
750 (current widget)
751 done
752 (keys args))
753 ;; First set the :args keyword.
754 (while (cdr current) ;Look in the type.
755 (if (and (keywordp (cadr current))
756 ;; If the last element is a keyword,
757 ;; it is still the :args element,
758 ;; even though it is a keyword.
759 (cddr current))
760 (if (eq (cadr current) :args)
761 ;; If :args is explicitly specified, obey it.
762 (setq current nil)
763 ;; Some other irrelevant keyword.
764 (setq current (cdr (cdr current))))
765 (setcdr current (list :args (cdr current)))
766 (setq current nil)))
767 (while (and args (not done)) ;Look in ARGS.
768 (cond ((eq (car args) :args)
769 ;; Handle explicit specification of :args.
770 (setq args (cadr args)
771 done t))
772 ((keywordp (car args))
773 (setq args (cddr args)))
774 (t (setq done t))))
775 (when done
776 (widget-put widget :args args))
777 ;; Then Convert the widget.
778 (setq type widget)
779 (while type
780 (let ((convert-widget (plist-get (cdr type) :convert-widget)))
781 (if convert-widget
782 (setq widget (funcall convert-widget widget))))
783 (setq type (get (car type) 'widget-type)))
784 ;; Finally set the keyword args.
785 (while keys
786 (let ((next (nth 0 keys)))
787 (if (keywordp next)
788 (progn
789 (widget-put widget next (nth 1 keys))
790 (setq keys (nthcdr 2 keys)))
791 (setq keys nil))))
792 ;; Convert the :value to internal format.
793 (if (widget-member widget :value)
794 (widget-put widget
795 :value (widget-apply widget
796 :value-to-internal
797 (widget-get widget :value))))
798 ;; Return the newly create widget.
799 widget))
801 ;;;###autoload
802 (defun widget-insert (&rest args)
803 "Call `insert' with ARGS even if surrounding text is read only."
804 (let ((inhibit-read-only t)
805 (inhibit-modification-hooks t))
806 (apply 'insert args)))
808 (defun widget-convert-text (type from to
809 &optional button-from button-to
810 &rest args)
811 "Return a widget of type TYPE with endpoint FROM TO.
812 No text will be inserted to the buffer, instead the text between FROM
813 and TO will be used as the widgets end points. If optional arguments
814 BUTTON-FROM and BUTTON-TO are given, these will be used as the widgets
815 button end points.
816 Optional ARGS are extra keyword arguments for TYPE."
817 (let ((widget (apply 'widget-convert type :delete 'widget-leave-text args))
818 (from (copy-marker from))
819 (to (copy-marker to)))
820 (set-marker-insertion-type from t)
821 (set-marker-insertion-type to nil)
822 (widget-put widget :from from)
823 (widget-put widget :to to)
824 (when button-from
825 (widget-specify-button widget button-from button-to))
826 widget))
828 (defun widget-convert-button (type from to &rest args)
829 "Return a widget of type TYPE with endpoint FROM TO.
830 Optional ARGS are extra keyword arguments for TYPE.
831 No text will be inserted to the buffer, instead the text between FROM
832 and TO will be used as the widgets end points, as well as the widgets
833 button end points."
834 (apply 'widget-convert-text type from to from to args))
836 (defun widget-leave-text (widget)
837 "Remove markers and overlays from WIDGET and its children."
838 (let ((button (widget-get widget :button-overlay))
839 (sample (widget-get widget :sample-overlay))
840 (doc (widget-get widget :doc-overlay))
841 (field (widget-get widget :field-overlay)))
842 (set-marker (widget-get widget :from) nil)
843 (set-marker (widget-get widget :to) nil)
844 (when button
845 (delete-overlay button))
846 (when sample
847 (delete-overlay sample))
848 (when doc
849 (delete-overlay doc))
850 (when field
851 (delete-overlay field))
852 (mapc 'widget-leave-text (widget-get widget :children))))
854 ;;; Keymap and Commands.
856 ;; This alias exists only so that one can choose in doc-strings (e.g.
857 ;; Custom-mode) which key-binding of widget-keymap one wants to refer to.
858 ;; http://lists.gnu.org/archive/html/emacs-devel/2008-11/msg00480.html
859 (define-obsolete-function-alias 'advertised-widget-backward
860 'widget-backward "23.2")
862 ;;;###autoload
863 (defvar widget-keymap
864 (let ((map (make-sparse-keymap)))
865 (define-key map "\t" 'widget-forward)
866 (define-key map "\e\t" 'widget-backward)
867 (define-key map [(shift tab)] 'widget-backward)
868 (put 'widget-backward :advertised-binding [(shift tab)])
869 (define-key map [backtab] 'widget-backward)
870 (define-key map [down-mouse-2] 'widget-button-click)
871 (define-key map [down-mouse-1] 'widget-button-click)
872 ;; The following definition needs to avoid using escape sequences that
873 ;; might get converted to ^M when building loaddefs.el
874 (define-key map [(control ?m)] 'widget-button-press)
875 map)
876 "Keymap containing useful binding for buffers containing widgets.
877 Recommended as a parent keymap for modes using widgets.
878 Note that such modes will need to require wid-edit.")
880 (defvar widget-global-map global-map
881 "Keymap used for events a widget does not handle itself.")
882 (make-variable-buffer-local 'widget-global-map)
884 (defvar widget-field-keymap
885 (let ((map (copy-keymap widget-keymap)))
886 (define-key map "\C-k" 'widget-kill-line)
887 (define-key map "\M-\t" 'widget-complete)
888 (define-key map "\C-m" 'widget-field-activate)
889 ;; Since the widget code uses a `field' property to identify fields,
890 ;; ordinary beginning-of-line does the right thing.
891 ;; (define-key map "\C-a" 'widget-beginning-of-line)
892 (define-key map "\C-e" 'widget-end-of-line)
893 map)
894 "Keymap used inside an editable field.")
896 (defvar widget-text-keymap
897 (let ((map (copy-keymap widget-keymap)))
898 ;; Since the widget code uses a `field' property to identify fields,
899 ;; ordinary beginning-of-line does the right thing.
900 ;; (define-key map "\C-a" 'widget-beginning-of-line)
901 (define-key map "\C-e" 'widget-end-of-line)
902 map)
903 "Keymap used inside a text field.")
905 (defun widget-field-activate (pos &optional event)
906 "Invoke the editable field at point."
907 (interactive "@d")
908 (let ((field (widget-field-at pos)))
909 (if field
910 (widget-apply-action field event)
911 (call-interactively
912 (lookup-key widget-global-map (this-command-keys))))))
914 (defface widget-button-pressed
915 '((((min-colors 88) (class color))
916 (:foreground "red1"))
917 (((class color))
918 (:foreground "red"))
920 (:weight bold :underline t)))
921 "Face used for pressed buttons."
922 :group 'widget-faces)
923 (define-obsolete-face-alias 'widget-button-pressed-face
924 'widget-button-pressed "22.1")
926 (defvar widget-button-click-moves-point nil
927 "If non-nil, `widget-button-click' moves point to a button after invoking it.
928 If nil, point returns to its original position after invoking a button.")
930 (defun widget-button-click (event)
931 "Invoke the button that the mouse is pointing at."
932 (interactive "e")
933 (if (widget-event-point event)
934 (let* ((oevent event)
935 (mouse-1 (memq (event-basic-type event) '(mouse-1 down-mouse-1)))
936 (pos (widget-event-point event))
937 (start (event-start event))
938 (button (get-char-property
939 pos 'button (and (windowp (posn-window start))
940 (window-buffer (posn-window start)))))
941 newpoint)
942 (when (or (null button)
943 (catch 'button-press-cancelled
944 ;; Mouse click on a widget button. Do the following
945 ;; in a save-excursion so that the click on the button
946 ;; doesn't change point.
947 (save-selected-window
948 (select-window (posn-window (event-start event)))
949 (save-excursion
950 (goto-char (posn-point (event-start event)))
951 (let* ((overlay (widget-get button :button-overlay))
952 (pressed-face (or (widget-get button :pressed-face)
953 widget-button-pressed-face))
954 (face (overlay-get overlay 'face))
955 (mouse-face (overlay-get overlay 'mouse-face)))
956 (unwind-protect
957 ;; Read events, including mouse-movement
958 ;; events, waiting for a release event. If we
959 ;; began with a mouse-1 event and receive a
960 ;; movement event, that means the user wants
961 ;; to perform drag-selection, so cancel the
962 ;; button press and do the default mouse-1
963 ;; action. For mouse-2, just highlight/
964 ;; unhighlight the button the mouse was
965 ;; initially on when we move over it.
966 (save-excursion
967 (when face ; avoid changing around image
968 (overlay-put overlay 'face pressed-face)
969 (overlay-put overlay 'mouse-face pressed-face))
970 (unless (widget-apply button :mouse-down-action event)
971 (let ((track-mouse t))
972 (while (not (widget-button-release-event-p event))
973 (setq event (read-event))
974 (when (and mouse-1 (mouse-movement-p event))
975 (push event unread-command-events)
976 (setq event oevent)
977 (throw 'button-press-cancelled t))
978 (unless (or (integerp event)
979 (memq (car event) '(switch-frame select-window))
980 (eq (car event) 'scroll-bar-movement))
981 (setq pos (widget-event-point event))
982 (if (and pos
983 (eq (get-char-property pos 'button)
984 button))
985 (when face
986 (overlay-put overlay 'face pressed-face)
987 (overlay-put overlay 'mouse-face pressed-face))
988 (overlay-put overlay 'face face)
989 (overlay-put overlay 'mouse-face mouse-face))))))
991 ;; When mouse is released over the button, run
992 ;; its action function.
993 (when (and pos (eq (get-char-property pos 'button) button))
994 (goto-char pos)
995 (widget-apply-action button event)
996 (if widget-button-click-moves-point
997 (setq newpoint (point)))))
998 (overlay-put overlay 'face face)
999 (overlay-put overlay 'mouse-face mouse-face))))
1001 (if newpoint (goto-char newpoint))
1002 ;; This loses if the widget action switches windows. -- cyd
1003 ;; (unless (pos-visible-in-window-p (widget-event-point event))
1004 ;; (mouse-set-point event)
1005 ;; (beginning-of-line)
1006 ;; (recenter))
1008 nil))
1009 (let ((up t) command)
1010 ;; Mouse click not on a widget button. Find the global
1011 ;; command to run, and check whether it is bound to an
1012 ;; up event.
1013 (if mouse-1
1014 (cond ((setq command ;down event
1015 (lookup-key widget-global-map [down-mouse-1]))
1016 (setq up nil))
1017 ((setq command ;up event
1018 (lookup-key widget-global-map [mouse-1]))))
1019 (cond ((setq command ;down event
1020 (lookup-key widget-global-map [down-mouse-2]))
1021 (setq up nil))
1022 ((setq command ;up event
1023 (lookup-key widget-global-map [mouse-2])))))
1024 (when up
1025 ;; Don't execute up events twice.
1026 (while (not (widget-button-release-event-p event))
1027 (setq event (read-event))))
1028 (when command
1029 (call-interactively command)))))
1030 (message "You clicked somewhere weird.")))
1032 (defun widget-button-press (pos &optional event)
1033 "Invoke button at POS."
1034 (interactive "@d")
1035 (let ((button (get-char-property pos 'button)))
1036 (if button
1037 (widget-apply-action button event)
1038 (let ((command (lookup-key widget-global-map (this-command-keys))))
1039 (when (commandp command)
1040 (call-interactively command))))))
1042 (defun widget-tabable-at (&optional pos)
1043 "Return the tabable widget at POS, or nil.
1044 POS defaults to the value of (point)."
1045 (let ((widget (widget-at pos)))
1046 (if widget
1047 (let ((order (widget-get widget :tab-order)))
1048 (if order
1049 (if (>= order 0)
1050 widget)
1051 widget)))))
1053 (defvar widget-use-overlay-change t
1054 "If non-nil, use overlay change functions to tab around in the buffer.
1055 This is much faster.")
1057 (defun widget-move (arg)
1058 "Move point to the ARG next field or button.
1059 ARG may be negative to move backward."
1060 (or (bobp) (> arg 0) (backward-char))
1061 (let ((wrapped 0)
1062 (number arg)
1063 (old (widget-tabable-at)))
1064 ;; Forward.
1065 (while (> arg 0)
1066 (cond ((eobp)
1067 (goto-char (point-min))
1068 (setq wrapped (1+ wrapped)))
1069 (widget-use-overlay-change
1070 (goto-char (next-overlay-change (point))))
1072 (forward-char 1)))
1073 (and (= wrapped 2)
1074 (eq arg number)
1075 (error "No buttons or fields found"))
1076 (let ((new (widget-tabable-at)))
1077 (when new
1078 (unless (eq new old)
1079 (setq arg (1- arg))
1080 (setq old new)))))
1081 ;; Backward.
1082 (while (< arg 0)
1083 (cond ((bobp)
1084 (goto-char (point-max))
1085 (setq wrapped (1+ wrapped)))
1086 (widget-use-overlay-change
1087 (goto-char (previous-overlay-change (point))))
1089 (backward-char 1)))
1090 (and (= wrapped 2)
1091 (eq arg number)
1092 (error "No buttons or fields found"))
1093 (let ((new (widget-tabable-at)))
1094 (when new
1095 (unless (eq new old)
1096 (setq arg (1+ arg))))))
1097 (let ((new (widget-tabable-at)))
1098 (while (eq (widget-tabable-at) new)
1099 (backward-char)))
1100 (forward-char))
1101 (widget-echo-help (point))
1102 (run-hooks 'widget-move-hook))
1104 (defun widget-forward (arg)
1105 "Move point to the next field or button.
1106 With optional ARG, move across that many fields."
1107 (interactive "p")
1108 (run-hooks 'widget-forward-hook)
1109 (widget-move arg))
1111 (defun widget-backward (arg)
1112 "Move point to the previous field or button.
1113 With optional ARG, move across that many fields."
1114 (interactive "p")
1115 (run-hooks 'widget-backward-hook)
1116 (widget-move (- arg)))
1118 ;; Since the widget code uses a `field' property to identify fields,
1119 ;; ordinary beginning-of-line does the right thing.
1120 (defalias 'widget-beginning-of-line 'beginning-of-line)
1122 (defun widget-end-of-line ()
1123 "Go to end of field or end of line, whichever is first.
1124 Trailing spaces at the end of padded fields are not considered part of
1125 the field."
1126 (interactive)
1127 ;; Ordinary end-of-line does the right thing, because we're inside
1128 ;; text with a `field' property.
1129 (end-of-line)
1130 (unless (eolp)
1131 ;; ... except that we want to ignore trailing spaces in fields that
1132 ;; aren't terminated by a newline, because they are used as padding,
1133 ;; and ignored when extracting the entered value of the field.
1134 (skip-chars-backward " " (field-beginning (1- (point))))))
1136 (defun widget-kill-line ()
1137 "Kill to end of field or end of line, whichever is first."
1138 (interactive)
1139 (let* ((field (widget-field-find (point)))
1140 (end (and field (widget-field-end field))))
1141 (if (and field (> (line-beginning-position 2) end))
1142 (kill-region (point) end)
1143 (call-interactively 'kill-line))))
1145 (defcustom widget-complete-field (lookup-key global-map "\M-\t")
1146 "Default function to call for completion inside fields."
1147 :options '(ispell-complete-word complete-tag lisp-complete-symbol)
1148 :type 'function
1149 :group 'widgets)
1151 (defun widget-narrow-to-field ()
1152 "Narrow to field."
1153 (interactive)
1154 (let ((field (widget-field-find (point))))
1155 (if field
1156 (narrow-to-region (line-beginning-position) (line-end-position)))))
1158 ;; This used to say:
1159 ;; "When not inside a field, move to the previous button or field."
1160 ;; but AFAICS, it has always just thrown an error.
1161 (defun widget-complete ()
1162 "Complete content of editable field from point.
1163 When not inside a field, signal an error."
1164 (interactive)
1165 (let ((field (widget-field-find (point))))
1166 (if field
1167 (widget-apply field :complete)
1168 (error "Not in an editable field"))))
1170 ;;; Setting up the buffer.
1172 (defvar widget-field-new nil
1173 "List of all newly created editable fields in the buffer.")
1174 (make-variable-buffer-local 'widget-field-new)
1176 (defvar widget-field-list nil
1177 "List of all editable fields in the buffer.")
1178 (make-variable-buffer-local 'widget-field-list)
1180 (defun widget-at (&optional pos)
1181 "The button or field at POS (default, point)."
1182 (or (get-char-property (or pos (point)) 'button)
1183 (widget-field-at pos)))
1185 ;;;###autoload
1186 (defun widget-setup ()
1187 "Setup current buffer so editing string widgets works."
1188 (let ((inhibit-read-only t)
1189 (inhibit-modification-hooks t)
1190 field)
1191 (while widget-field-new
1192 (setq field (car widget-field-new)
1193 widget-field-new (cdr widget-field-new)
1194 widget-field-list (cons field widget-field-list))
1195 (let ((from (car (widget-get field :field-overlay)))
1196 (to (cdr (widget-get field :field-overlay))))
1197 (widget-specify-field field
1198 (marker-position from) (marker-position to))
1199 (set-marker from nil)
1200 (set-marker to nil))))
1201 (widget-clear-undo)
1202 (widget-add-change))
1204 (defvar widget-field-last nil)
1205 ;; Last field containing point.
1206 (make-variable-buffer-local 'widget-field-last)
1208 (defvar widget-field-was nil)
1209 ;; The widget data before the change.
1210 (make-variable-buffer-local 'widget-field-was)
1212 (defun widget-field-at (pos)
1213 "Return the widget field at POS, or nil if none."
1214 (let ((field (get-char-property (or pos (point)) 'field)))
1215 (if (eq field 'boundary)
1216 (get-char-property (or pos (point)) 'real-field)
1217 field)))
1219 (defun widget-field-buffer (widget)
1220 "Return the buffer of WIDGET's editing field."
1221 (let ((overlay (widget-get widget :field-overlay)))
1222 (cond ((overlayp overlay)
1223 (overlay-buffer overlay))
1224 ((consp overlay)
1225 (marker-buffer (car overlay))))))
1227 (defun widget-field-start (widget)
1228 "Return the start of WIDGET's editing field."
1229 (let ((overlay (widget-get widget :field-overlay)))
1230 (if (overlayp overlay)
1231 (overlay-start overlay)
1232 (car overlay))))
1234 (defun widget-field-end (widget)
1235 "Return the end of WIDGET's editing field."
1236 (let ((overlay (widget-get widget :field-overlay)))
1237 ;; Don't subtract one if local-map works at the end of the overlay,
1238 ;; or if a special `boundary' field has been added after the widget
1239 ;; field.
1240 (if (overlayp overlay)
1241 ;; Don't proceed if overlay has been removed from buffer.
1242 (when (overlay-buffer overlay)
1243 (if (and (not (eq (with-current-buffer
1244 (widget-field-buffer widget)
1245 (save-restriction
1246 ;; `widget-narrow-to-field' can be
1247 ;; active when this function is called
1248 ;; from an change-functions hook. So
1249 ;; temporarily remove field narrowing
1250 ;; before to call `get-char-property'.
1251 (widen)
1252 (get-char-property (overlay-end overlay)
1253 'field)))
1254 'boundary))
1255 (or widget-field-add-space
1256 (null (widget-get widget :size))))
1257 (1- (overlay-end overlay))
1258 (overlay-end overlay)))
1259 (cdr overlay))))
1261 (defun widget-field-text-end (widget)
1262 (let ((to (widget-field-end widget))
1263 (size (widget-get widget :size)))
1264 (if (or (null size) (zerop size))
1266 (let ((from (widget-field-start widget)))
1267 (if (and from to)
1268 (with-current-buffer (widget-field-buffer widget)
1269 (while (and (> to from)
1270 (eq (char-after (1- to)) ?\s))
1271 (setq to (1- to)))
1272 to))))))
1274 (defun widget-field-find (pos)
1275 "Return the field at POS.
1276 Unlike (get-char-property POS 'field), this works with empty fields too."
1277 (let ((fields widget-field-list)
1278 field found)
1279 (while fields
1280 (setq field (car fields)
1281 fields (cdr fields))
1282 (when (and (<= (widget-field-start field) pos)
1283 (<= pos (widget-field-end field)))
1284 (when found
1285 (error "Overlapping fields"))
1286 (setq found field)))
1287 found))
1289 (defun widget-before-change (from to)
1290 ;; This is how, for example, a variable changes its state to `modified'.
1291 ;; when it is being edited.
1292 (unless inhibit-read-only
1293 (let ((from-field (widget-field-find from))
1294 (to-field (widget-field-find to)))
1295 (cond ((not (eq from-field to-field))
1296 (add-hook 'post-command-hook 'widget-add-change nil t)
1297 (signal 'text-read-only
1298 '("Change should be restricted to a single field")))
1299 ((null from-field)
1300 (add-hook 'post-command-hook 'widget-add-change nil t)
1301 (signal 'text-read-only
1302 '("Attempt to change text outside editable field")))
1303 (widget-field-use-before-change
1304 (widget-apply from-field :notify from-field))))))
1306 (defun widget-add-change ()
1307 (remove-hook 'post-command-hook 'widget-add-change t)
1308 (add-hook 'before-change-functions 'widget-before-change nil t)
1309 (add-hook 'after-change-functions 'widget-after-change nil t))
1311 (defun widget-after-change (from to old)
1312 "Adjust field size and text properties."
1313 (let ((field (widget-field-find from))
1314 (other (widget-field-find to)))
1315 (when field
1316 (unless (eq field other)
1317 (error "Change in different fields"))
1318 (let ((size (widget-get field :size)))
1319 (when size
1320 (let ((begin (widget-field-start field))
1321 (end (widget-field-end field)))
1322 (cond ((< (- end begin) size)
1323 ;; Field too small.
1324 (save-excursion
1325 (goto-char end)
1326 (insert-char ?\s (- (+ begin size) end))))
1327 ((> (- end begin) size)
1328 ;; Field too large and
1329 (if (or (< (point) (+ begin size))
1330 (> (point) end))
1331 ;; Point is outside extra space.
1332 (setq begin (+ begin size))
1333 ;; Point is within the extra space.
1334 (setq begin (point)))
1335 (save-excursion
1336 (goto-char end)
1337 (while (and (eq (preceding-char) ?\s)
1338 (> (point) begin))
1339 (delete-backward-char 1)))))))
1340 (widget-specify-secret field))
1341 (widget-apply field :notify field))))
1343 ;;; Widget Functions
1345 ;; These functions are used in the definition of multiple widgets.
1347 (defun widget-parent-action (widget &optional event)
1348 "Tell :parent of WIDGET to handle the :action.
1349 Optional EVENT is the event that triggered the action."
1350 (widget-apply (widget-get widget :parent) :action event))
1352 (defun widget-children-value-delete (widget)
1353 "Delete all :children and :buttons in WIDGET."
1354 (mapc 'widget-delete (widget-get widget :children))
1355 (widget-put widget :children nil)
1356 (mapc 'widget-delete (widget-get widget :buttons))
1357 (widget-put widget :buttons nil))
1359 (defun widget-children-validate (widget)
1360 "All the :children must be valid."
1361 (let ((children (widget-get widget :children))
1362 child found)
1363 (while (and children (not found))
1364 (setq child (car children)
1365 children (cdr children)
1366 found (widget-apply child :validate)))
1367 found))
1369 (defun widget-child-value-get (widget)
1370 "Get the value of the first member of :children in WIDGET."
1371 (widget-value (car (widget-get widget :children))))
1373 (defun widget-child-value-inline (widget)
1374 "Get the inline value of the first member of :children in WIDGET."
1375 (widget-apply (car (widget-get widget :children)) :value-inline))
1377 (defun widget-child-validate (widget)
1378 "The result of validating the first member of :children in WIDGET."
1379 (widget-apply (car (widget-get widget :children)) :validate))
1381 (defun widget-type-value-create (widget)
1382 "Convert and instantiate the value of the :type attribute of WIDGET.
1383 Store the newly created widget in the :children attribute.
1385 The value of the :type attribute should be an unconverted widget type."
1386 (let ((value (widget-get widget :value))
1387 (type (widget-get widget :type)))
1388 (widget-put widget :children
1389 (list (widget-create-child-value widget
1390 (widget-convert type)
1391 value)))))
1393 (defun widget-type-default-get (widget)
1394 "Get default value from the :type attribute of WIDGET.
1396 The value of the :type attribute should be an unconverted widget type."
1397 (widget-default-get (widget-convert (widget-get widget :type))))
1399 (defun widget-type-match (widget value)
1400 "Non-nil if the :type value of WIDGET matches VALUE.
1402 The value of the :type attribute should be an unconverted widget type."
1403 (widget-apply (widget-convert (widget-get widget :type)) :match value))
1405 (defun widget-types-copy (widget)
1406 "Copy :args as widget types in WIDGET."
1407 (widget-put widget :args (mapcar 'widget-copy (widget-get widget :args)))
1408 widget)
1410 ;; Made defsubst to speed up face editor creation.
1411 (defsubst widget-types-convert-widget (widget)
1412 "Convert :args as widget types in WIDGET."
1413 (widget-put widget :args (mapcar 'widget-convert (widget-get widget :args)))
1414 widget)
1416 (defun widget-value-convert-widget (widget)
1417 "Initialize :value from :args in WIDGET."
1418 (let ((args (widget-get widget :args)))
1419 (when args
1420 (widget-put widget :value (car args))
1421 ;; Don't convert :value here, as this is done in `widget-convert'.
1422 ;; (widget-put widget :value (widget-apply widget
1423 ;; :value-to-internal (car args)))
1424 (widget-put widget :args nil)))
1425 widget)
1427 (defun widget-value-value-get (widget)
1428 "Return the :value property of WIDGET."
1429 (widget-get widget :value))
1431 ;;; The `default' Widget.
1433 (define-widget 'default nil
1434 "Basic widget other widgets are derived from."
1435 :value-to-internal (lambda (widget value) value)
1436 :value-to-external (lambda (widget value) value)
1437 :button-prefix 'widget-button-prefix
1438 :button-suffix 'widget-button-suffix
1439 :complete 'widget-default-complete
1440 :create 'widget-default-create
1441 :indent nil
1442 :offset 0
1443 :format-handler 'widget-default-format-handler
1444 :button-face-get 'widget-default-button-face-get
1445 :mouse-face-get 'widget-default-mouse-face-get
1446 :sample-face-get 'widget-default-sample-face-get
1447 :delete 'widget-default-delete
1448 :copy 'identity
1449 :value-set 'widget-default-value-set
1450 :value-inline 'widget-default-value-inline
1451 :value-delete 'ignore
1452 :default-get 'widget-default-default-get
1453 :menu-tag-get 'widget-default-menu-tag-get
1454 :validate #'ignore
1455 :active 'widget-default-active
1456 :activate 'widget-specify-active
1457 :deactivate 'widget-default-deactivate
1458 :mouse-down-action #'ignore
1459 :action 'widget-default-action
1460 :notify 'widget-default-notify
1461 :prompt-value 'widget-default-prompt-value)
1463 (defun widget-default-complete (widget)
1464 "Call the value of the :complete-function property of WIDGET.
1465 If that does not exist, call the value of `widget-complete-field'."
1466 (call-interactively (or (widget-get widget :complete-function)
1467 widget-complete-field)))
1469 (defun widget-default-create (widget)
1470 "Create WIDGET at point in the current buffer."
1471 (widget-specify-insert
1472 (let ((from (point))
1473 button-begin button-end
1474 sample-begin sample-end
1475 doc-begin doc-end
1476 value-pos)
1477 (insert (widget-get widget :format))
1478 (goto-char from)
1479 ;; Parse escapes in format.
1480 (while (re-search-forward "%\\(.\\)" nil t)
1481 (let ((escape (char-after (match-beginning 1))))
1482 (delete-backward-char 2)
1483 (cond ((eq escape ?%)
1484 (insert ?%))
1485 ((eq escape ?\[)
1486 (setq button-begin (point))
1487 (insert (widget-get-indirect widget :button-prefix)))
1488 ((eq escape ?\])
1489 (insert (widget-get-indirect widget :button-suffix))
1490 (setq button-end (point)))
1491 ((eq escape ?\{)
1492 (setq sample-begin (point)))
1493 ((eq escape ?\})
1494 (setq sample-end (point)))
1495 ((eq escape ?n)
1496 (when (widget-get widget :indent)
1497 (insert ?\n)
1498 (insert-char ?\s (widget-get widget :indent))))
1499 ((eq escape ?t)
1500 (let ((image (widget-get widget :tag-glyph))
1501 (tag (widget-get widget :tag)))
1502 (cond (image
1503 (widget-image-insert widget (or tag "image") image))
1504 (tag
1505 (insert tag))
1507 (princ (widget-get widget :value)
1508 (current-buffer))))))
1509 ((eq escape ?d)
1510 (let ((doc (widget-get widget :doc)))
1511 (when doc
1512 (setq doc-begin (point))
1513 (insert doc)
1514 (while (eq (preceding-char) ?\n)
1515 (delete-backward-char 1))
1516 (insert ?\n)
1517 (setq doc-end (point)))))
1518 ((eq escape ?h)
1519 (widget-add-documentation-string-button widget))
1520 ((eq escape ?v)
1521 (if (and button-begin (not button-end))
1522 (widget-apply widget :value-create)
1523 (setq value-pos (point))))
1525 (widget-apply widget :format-handler escape)))))
1526 ;; Specify button, sample, and doc, and insert value.
1527 (and button-begin button-end
1528 (widget-specify-button widget button-begin button-end))
1529 (and sample-begin sample-end
1530 (widget-specify-sample widget sample-begin sample-end))
1531 (and doc-begin doc-end
1532 (widget-specify-doc widget doc-begin doc-end))
1533 (when value-pos
1534 (goto-char value-pos)
1535 (widget-apply widget :value-create)))
1536 (let ((from (point-min-marker))
1537 (to (point-max-marker)))
1538 (set-marker-insertion-type from t)
1539 (set-marker-insertion-type to nil)
1540 (widget-put widget :from from)
1541 (widget-put widget :to to)))
1542 (widget-clear-undo))
1544 (defun widget-default-format-handler (widget escape)
1545 (error "Unknown escape `%c'" escape))
1547 (defun widget-default-button-face-get (widget)
1548 ;; Use :button-face or widget-button-face
1549 (or (widget-get widget :button-face)
1550 (let ((parent (widget-get widget :parent)))
1551 (if parent
1552 (widget-apply parent :button-face-get)
1553 widget-button-face))))
1555 (defun widget-default-mouse-face-get (widget)
1556 ;; Use :mouse-face or widget-mouse-face
1557 (or (widget-get widget :mouse-face)
1558 (let ((parent (widget-get widget :parent)))
1559 (if parent
1560 (widget-apply parent :mouse-face-get)
1561 widget-mouse-face))))
1563 (defun widget-default-sample-face-get (widget)
1564 ;; Use :sample-face.
1565 (widget-get widget :sample-face))
1567 (defun widget-default-delete (widget)
1568 "Remove widget from the buffer."
1569 (let ((from (widget-get widget :from))
1570 (to (widget-get widget :to))
1571 (inactive-overlay (widget-get widget :inactive))
1572 (button-overlay (widget-get widget :button-overlay))
1573 (sample-overlay (widget-get widget :sample-overlay))
1574 (doc-overlay (widget-get widget :doc-overlay))
1575 (inhibit-modification-hooks t)
1576 (inhibit-read-only t))
1577 (widget-apply widget :value-delete)
1578 (widget-children-value-delete widget)
1579 (when inactive-overlay
1580 (delete-overlay inactive-overlay))
1581 (when button-overlay
1582 (delete-overlay button-overlay))
1583 (when sample-overlay
1584 (delete-overlay sample-overlay))
1585 (when doc-overlay
1586 (delete-overlay doc-overlay))
1587 (when (< from to)
1588 ;; Kludge: this doesn't need to be true for empty formats.
1589 (delete-region from to))
1590 (set-marker from nil)
1591 (set-marker to nil))
1592 (widget-clear-undo))
1594 (defun widget-default-value-set (widget value)
1595 "Recreate widget with new value."
1596 (let* ((old-pos (point))
1597 (from (copy-marker (widget-get widget :from)))
1598 (to (copy-marker (widget-get widget :to)))
1599 (offset (if (and (<= from old-pos) (<= old-pos to))
1600 (if (>= old-pos (1- to))
1601 (- old-pos to 1)
1602 (- old-pos from)))))
1603 ;;??? Bug: this ought to insert the new value before deleting the old one,
1604 ;; so that markers on either side of the value automatically
1605 ;; stay on the same side. -- rms.
1606 (save-excursion
1607 (goto-char (widget-get widget :from))
1608 (widget-apply widget :delete)
1609 (widget-put widget :value value)
1610 (widget-apply widget :create))
1611 (if offset
1612 (if (< offset 0)
1613 (goto-char (+ (widget-get widget :to) offset 1))
1614 (goto-char (min (+ from offset) (1- (widget-get widget :to))))))))
1616 (defun widget-default-value-inline (widget)
1617 "Wrap value in a list unless it is inline."
1618 (if (widget-get widget :inline)
1619 (widget-value widget)
1620 (list (widget-value widget))))
1622 (defun widget-default-default-get (widget)
1623 "Get `:value'."
1624 (widget-get widget :value))
1626 (defun widget-default-menu-tag-get (widget)
1627 "Use tag or value for menus."
1628 (or (widget-get widget :menu-tag)
1629 (widget-get widget :tag)
1630 (widget-princ-to-string (widget-get widget :value))))
1632 (defun widget-default-active (widget)
1633 "Return t if this widget is active (user modifiable)."
1634 (or (widget-get widget :always-active)
1635 (and (not (widget-get widget :inactive))
1636 (let ((parent (widget-get widget :parent)))
1637 (or (null parent)
1638 (widget-apply parent :active))))))
1640 (defun widget-default-deactivate (widget)
1641 "Make WIDGET inactive for user modifications."
1642 (widget-specify-inactive widget
1643 (widget-get widget :from)
1644 (widget-get widget :to)))
1646 (defun widget-default-action (widget &optional event)
1647 "Notify the parent when a widget changes."
1648 (let ((parent (widget-get widget :parent)))
1649 (when parent
1650 (widget-apply parent :notify widget event))))
1652 (defun widget-default-notify (widget child &optional event)
1653 "Pass notification to parent."
1654 (widget-default-action widget event))
1656 (defun widget-default-prompt-value (widget prompt value unbound)
1657 "Read an arbitrary value."
1658 (eval-minibuffer prompt))
1660 (defun widget-docstring (widget)
1661 "Return the documentation string specificied by WIDGET, or nil if none.
1662 If WIDGET has a `:doc' property, that specifies the documentation string.
1663 Otherwise, try the `:documentation-property' property. If this
1664 is a function, call it with the widget's value as an argument; if
1665 it is a symbol, use this symbol together with the widget's value
1666 as the argument to `documentation-property'."
1667 (let ((doc (or (widget-get widget :doc)
1668 (let ((doc-prop (widget-get widget :documentation-property))
1669 (value (widget-get widget :value)))
1670 (cond ((functionp doc-prop)
1671 (funcall doc-prop value))
1672 ((symbolp doc-prop)
1673 (documentation-property value doc-prop)))))))
1674 (when (and (stringp doc) (> (length doc) 0))
1675 ;; Remove any redundant `*' in the beginning.
1676 (when (eq (aref doc 0) ?*)
1677 (setq doc (substring doc 1)))
1678 ;; Remove trailing newlines.
1679 (when (string-match "\n+\\'" doc)
1680 (setq doc (substring doc 0 (match-beginning 0))))
1681 doc)))
1683 ;;; The `item' Widget.
1685 (define-widget 'item 'default
1686 "Constant items for inclusion in other widgets."
1687 :convert-widget 'widget-value-convert-widget
1688 :value-create 'widget-item-value-create
1689 :value-delete 'ignore
1690 :value-get 'widget-value-value-get
1691 :match 'widget-item-match
1692 :match-inline 'widget-item-match-inline
1693 :action 'widget-item-action
1694 :format "%t\n")
1696 (defun widget-item-value-create (widget)
1697 "Insert the printed representation of the value."
1698 (princ (widget-get widget :value) (current-buffer)))
1700 (defun widget-item-match (widget value)
1701 ;; Match if the value is the same.
1702 (equal (widget-get widget :value) value))
1704 (defun widget-item-match-inline (widget values)
1705 ;; Match if the value is the same.
1706 (let ((value (widget-get widget :value)))
1707 (and (listp value)
1708 (<= (length value) (length values))
1709 (let ((head (widget-sublist values 0 (length value))))
1710 (and (equal head value)
1711 (cons head (widget-sublist values (length value))))))))
1713 (defun widget-sublist (list start &optional end)
1714 "Return the sublist of LIST from START to END.
1715 If END is omitted, it defaults to the length of LIST."
1716 (if (> start 0) (setq list (nthcdr start list)))
1717 (if end
1718 (unless (<= end start)
1719 (setq list (copy-sequence list))
1720 (setcdr (nthcdr (- end start 1) list) nil)
1721 list)
1722 (copy-sequence list)))
1724 (defun widget-item-action (widget &optional event)
1725 ;; Just notify itself.
1726 (widget-apply widget :notify widget event))
1728 ;;; The `push-button' Widget.
1730 ;; (defcustom widget-push-button-gui t
1731 ;; "If non-nil, use GUI push buttons when available."
1732 ;; :group 'widgets
1733 ;; :type 'boolean)
1735 ;; Cache already created GUI objects.
1736 ;; (defvar widget-push-button-cache nil)
1738 (defcustom widget-push-button-prefix "["
1739 "String used as prefix for buttons."
1740 :type 'string
1741 :group 'widget-button)
1743 (defcustom widget-push-button-suffix "]"
1744 "String used as suffix for buttons."
1745 :type 'string
1746 :group 'widget-button)
1748 (define-widget 'push-button 'item
1749 "A pushable button."
1750 :button-prefix ""
1751 :button-suffix ""
1752 :value-create 'widget-push-button-value-create
1753 :format "%[%v%]")
1755 (defun widget-push-button-value-create (widget)
1756 "Insert text representing the `on' and `off' states."
1757 (let* ((tag (or (widget-get widget :tag)
1758 (widget-get widget :value)))
1759 (tag-glyph (widget-get widget :tag-glyph))
1760 (text (concat widget-push-button-prefix
1761 tag widget-push-button-suffix)))
1762 (if tag-glyph
1763 (widget-image-insert widget text tag-glyph)
1764 (insert text))))
1766 ;; (defun widget-gui-action (widget)
1767 ;; "Apply :action for WIDGET."
1768 ;; (widget-apply-action widget (this-command-keys)))
1770 ;;; The `link' Widget.
1772 (defcustom widget-link-prefix "["
1773 "String used as prefix for links."
1774 :type 'string
1775 :group 'widget-button)
1777 (defcustom widget-link-suffix "]"
1778 "String used as suffix for links."
1779 :type 'string
1780 :group 'widget-button)
1782 (define-widget 'link 'item
1783 "An embedded link."
1784 :button-prefix 'widget-link-prefix
1785 :button-suffix 'widget-link-suffix
1786 :follow-link 'mouse-face
1787 :help-echo "Follow the link."
1788 :format "%[%t%]")
1790 ;;; The `info-link' Widget.
1792 (define-widget 'info-link 'link
1793 "A link to an info file."
1794 :action 'widget-info-link-action)
1796 (defun widget-info-link-action (widget &optional event)
1797 "Open the info node specified by WIDGET."
1798 (info (widget-value widget)))
1800 ;;; The `url-link' Widget.
1802 (define-widget 'url-link 'link
1803 "A link to an www page."
1804 :action 'widget-url-link-action)
1806 (defun widget-url-link-action (widget &optional event)
1807 "Open the URL specified by WIDGET."
1808 (browse-url (widget-value widget)))
1810 ;;; The `function-link' Widget.
1812 (define-widget 'function-link 'link
1813 "A link to an Emacs function."
1814 :action 'widget-function-link-action)
1816 (defun widget-function-link-action (widget &optional event)
1817 "Show the function specified by WIDGET."
1818 (describe-function (widget-value widget)))
1820 ;;; The `variable-link' Widget.
1822 (define-widget 'variable-link 'link
1823 "A link to an Emacs variable."
1824 :action 'widget-variable-link-action)
1826 (defun widget-variable-link-action (widget &optional event)
1827 "Show the variable specified by WIDGET."
1828 (describe-variable (widget-value widget)))
1830 ;;; The `file-link' Widget.
1832 (define-widget 'file-link 'link
1833 "A link to a file."
1834 :action 'widget-file-link-action)
1836 (defun widget-file-link-action (widget &optional event)
1837 "Find the file specified by WIDGET."
1838 (find-file (widget-value widget)))
1840 ;;; The `emacs-library-link' Widget.
1842 (define-widget 'emacs-library-link 'link
1843 "A link to an Emacs Lisp library file."
1844 :action 'widget-emacs-library-link-action)
1846 (defun widget-emacs-library-link-action (widget &optional event)
1847 "Find the Emacs library file specified by WIDGET."
1848 (find-file (locate-library (widget-value widget))))
1850 ;;; The `emacs-commentary-link' Widget.
1852 (define-widget 'emacs-commentary-link 'link
1853 "A link to Commentary in an Emacs Lisp library file."
1854 :action 'widget-emacs-commentary-link-action)
1856 (defun widget-emacs-commentary-link-action (widget &optional event)
1857 "Find the Commentary section of the Emacs file specified by WIDGET."
1858 (finder-commentary (widget-value widget)))
1860 ;;; The `editable-field' Widget.
1862 (define-widget 'editable-field 'default
1863 "An editable text field.
1864 Note: In an `editable-field' widget, the `%v' escape must be preceded
1865 by some other text in the `:format' string (if specified)."
1866 :convert-widget 'widget-value-convert-widget
1867 :keymap widget-field-keymap
1868 :format "%v"
1869 :help-echo "M-TAB: complete field; RET: enter value"
1870 :value ""
1871 :prompt-internal 'widget-field-prompt-internal
1872 :prompt-history 'widget-field-history
1873 :prompt-value 'widget-field-prompt-value
1874 :action 'widget-field-action
1875 :validate 'widget-field-validate
1876 :valid-regexp ""
1877 :error "Field's value doesn't match allowed forms"
1878 :value-create 'widget-field-value-create
1879 :value-delete 'widget-field-value-delete
1880 :value-get 'widget-field-value-get
1881 :match 'widget-field-match)
1883 (defvar widget-field-history nil
1884 "History of field minibuffer edits.")
1886 (defun widget-field-prompt-internal (widget prompt initial history)
1887 "Read string for WIDGET prompting with PROMPT.
1888 INITIAL is the initial input and HISTORY is a symbol containing
1889 the earlier input."
1890 (read-string prompt initial history))
1892 (defun widget-field-prompt-value (widget prompt value unbound)
1893 "Prompt for a string."
1894 (widget-apply widget
1895 :value-to-external
1896 (widget-apply widget
1897 :prompt-internal prompt
1898 (unless unbound
1899 (cons (widget-apply widget
1900 :value-to-internal value)
1902 (widget-get widget :prompt-history))))
1904 (defvar widget-edit-functions nil)
1906 (defun widget-field-action (widget &optional event)
1907 "Move to next field."
1908 (widget-forward 1)
1909 (run-hook-with-args 'widget-edit-functions widget))
1911 (defun widget-field-validate (widget)
1912 "Valid if the content matches `:valid-regexp'."
1913 (unless (string-match (widget-get widget :valid-regexp)
1914 (widget-apply widget :value-get))
1915 widget))
1917 (defun widget-field-value-create (widget)
1918 "Create an editable text field."
1919 (let ((size (widget-get widget :size))
1920 (value (widget-get widget :value))
1921 (from (point))
1922 ;; This is changed to a real overlay in `widget-setup'. We
1923 ;; need the end points to behave differently until
1924 ;; `widget-setup' is called.
1925 (overlay (cons (make-marker) (make-marker))))
1926 (widget-put widget :field-overlay overlay)
1927 (insert value)
1928 (and size
1929 (< (length value) size)
1930 (insert-char ?\s (- size (length value))))
1931 (unless (memq widget widget-field-list)
1932 (setq widget-field-new (cons widget widget-field-new)))
1933 (move-marker (cdr overlay) (point))
1934 (set-marker-insertion-type (cdr overlay) nil)
1935 (when (null size)
1936 (insert ?\n))
1937 (move-marker (car overlay) from)
1938 (set-marker-insertion-type (car overlay) t)))
1940 (defun widget-field-value-delete (widget)
1941 "Remove the widget from the list of active editing fields."
1942 (setq widget-field-list (delq widget widget-field-list))
1943 (setq widget-field-new (delq widget widget-field-new))
1944 ;; These are nil if the :format string doesn't contain `%v'.
1945 (let ((overlay (widget-get widget :field-overlay)))
1946 (when (overlayp overlay)
1947 (delete-overlay overlay))))
1949 (defun widget-field-value-get (widget)
1950 "Return current text in editing field."
1951 (let ((from (widget-field-start widget))
1952 (to (widget-field-text-end widget))
1953 (buffer (widget-field-buffer widget))
1954 (size (widget-get widget :size))
1955 (secret (widget-get widget :secret))
1956 (old (current-buffer)))
1957 (if (and from to)
1958 (progn
1959 (set-buffer buffer)
1960 (let ((result (buffer-substring-no-properties from to)))
1961 (when secret
1962 (let ((index 0))
1963 (while (< (+ from index) to)
1964 (aset result index
1965 (get-char-property (+ from index) 'secret))
1966 (setq index (1+ index)))))
1967 (set-buffer old)
1968 result))
1969 (widget-get widget :value))))
1971 (defun widget-field-match (widget value)
1972 ;; Match any string.
1973 (stringp value))
1975 ;;; The `text' Widget.
1977 (define-widget 'text 'editable-field
1978 "A multiline text area."
1979 :keymap widget-text-keymap)
1981 ;;; The `menu-choice' Widget.
1983 (define-widget 'menu-choice 'default
1984 "A menu of options."
1985 :convert-widget 'widget-types-convert-widget
1986 :copy 'widget-types-copy
1987 :format "%[%t%]: %v"
1988 :case-fold t
1989 :tag "choice"
1990 :void '(item :format "invalid (%t)\n")
1991 :value-create 'widget-choice-value-create
1992 :value-get 'widget-child-value-get
1993 :value-inline 'widget-child-value-inline
1994 :default-get 'widget-choice-default-get
1995 :mouse-down-action 'widget-choice-mouse-down-action
1996 :action 'widget-choice-action
1997 :error "Make a choice"
1998 :validate 'widget-choice-validate
1999 :match 'widget-choice-match
2000 :match-inline 'widget-choice-match-inline)
2002 (defun widget-choice-value-create (widget)
2003 "Insert the first choice that matches the value."
2004 (let ((value (widget-get widget :value))
2005 (args (widget-get widget :args))
2006 (explicit (widget-get widget :explicit-choice))
2007 current)
2008 (if explicit
2009 (progn
2010 ;; If the user specified the choice for this value,
2011 ;; respect that choice.
2012 (widget-put widget :children (list (widget-create-child-value
2013 widget explicit value)))
2014 (widget-put widget :choice explicit)
2015 (widget-put widget :explicit-choice nil))
2016 (while args
2017 (setq current (car args)
2018 args (cdr args))
2019 (when (widget-apply current :match value)
2020 (widget-put widget :children (list (widget-create-child-value
2021 widget current value)))
2022 (widget-put widget :choice current)
2023 (setq args nil
2024 current nil)))
2025 (when current
2026 (let ((void (widget-get widget :void)))
2027 (widget-put widget :children (list (widget-create-child-and-convert
2028 widget void :value value)))
2029 (widget-put widget :choice void))))))
2031 (defun widget-choice-default-get (widget)
2032 ;; Get default for the first choice.
2033 (widget-default-get (car (widget-get widget :args))))
2035 (defcustom widget-choice-toggle nil
2036 "If non-nil, a binary choice will just toggle between the values.
2037 Otherwise, the user will explicitly have to choose between the values
2038 when he invoked the menu."
2039 :type 'boolean
2040 :group 'widgets)
2042 (defun widget-choice-mouse-down-action (widget &optional event)
2043 ;; Return non-nil if we need a menu.
2044 (let ((args (widget-get widget :args))
2045 (old (widget-get widget :choice)))
2046 (cond ((not (display-popup-menus-p))
2047 ;; No place to pop up a menu.
2048 nil)
2049 ((< (length args) 2)
2050 ;; Empty or singleton list, just return the value.
2051 nil)
2052 ((> (length args) widget-menu-max-size)
2053 ;; Too long, prompt.
2054 nil)
2055 ((> (length args) 2)
2056 ;; Reasonable sized list, use menu.
2058 ((and widget-choice-toggle (memq old args))
2059 ;; We toggle.
2060 nil)
2062 ;; Ask which of the two.
2063 t))))
2065 (defun widget-choice-action (widget &optional event)
2066 ;; Make a choice.
2067 (let ((args (widget-get widget :args))
2068 (old (widget-get widget :choice))
2069 (tag (widget-apply widget :menu-tag-get))
2070 (completion-ignore-case (widget-get widget :case-fold))
2071 this-explicit
2072 current choices)
2073 ;; Remember old value.
2074 (if (and old (not (widget-apply widget :validate)))
2075 (let* ((external (widget-value widget))
2076 (internal (widget-apply old :value-to-internal external)))
2077 (widget-put old :value internal)))
2078 ;; Find new choice.
2079 (setq current
2080 (cond ((= (length args) 0)
2081 nil)
2082 ((= (length args) 1)
2083 (nth 0 args))
2084 ((and widget-choice-toggle
2085 (= (length args) 2)
2086 (memq old args))
2087 (if (eq old (nth 0 args))
2088 (nth 1 args)
2089 (nth 0 args)))
2091 (while args
2092 (setq current (car args)
2093 args (cdr args))
2094 (setq choices
2095 (cons (cons (widget-apply current :menu-tag-get)
2096 current)
2097 choices)))
2098 (setq this-explicit t)
2099 (widget-choose tag (reverse choices) event))))
2100 (when current
2101 ;; If this was an explicit user choice, record the choice,
2102 ;; so that widget-choice-value-create will respect it.
2103 (when this-explicit
2104 (widget-put widget :explicit-choice current))
2105 (widget-value-set widget (widget-default-get current))
2106 (widget-setup)
2107 (widget-apply widget :notify widget event)))
2108 (run-hook-with-args 'widget-edit-functions widget))
2110 (defun widget-choice-validate (widget)
2111 ;; Valid if we have made a valid choice.
2112 (if (eq (widget-get widget :void) (widget-get widget :choice))
2113 widget
2114 (widget-apply (car (widget-get widget :children)) :validate)))
2116 (defun widget-choice-match (widget value)
2117 ;; Matches if one of the choices matches.
2118 (let ((args (widget-get widget :args))
2119 current found)
2120 (while (and args (not found))
2121 (setq current (car args)
2122 args (cdr args)
2123 found (widget-apply current :match value)))
2124 found))
2126 (defun widget-choice-match-inline (widget values)
2127 ;; Matches if one of the choices matches.
2128 (let ((args (widget-get widget :args))
2129 current found)
2130 (while (and args (null found))
2131 (setq current (car args)
2132 args (cdr args)
2133 found (widget-match-inline current values)))
2134 found))
2136 ;;; The `toggle' Widget.
2138 (define-widget 'toggle 'item
2139 "Toggle between two states."
2140 :format "%[%v%]\n"
2141 :value-create 'widget-toggle-value-create
2142 :action 'widget-toggle-action
2143 :match (lambda (widget value) t)
2144 :on "on"
2145 :off "off")
2147 (defun widget-toggle-value-create (widget)
2148 "Insert text representing the `on' and `off' states."
2149 (if (widget-value widget)
2150 (let ((image (widget-get widget :on-glyph)))
2151 (and (display-graphic-p)
2152 (listp image)
2153 (not (eq (car image) 'image))
2154 (widget-put widget :on-glyph (setq image (eval image))))
2155 (widget-image-insert widget
2156 (widget-get widget :on)
2157 image))
2158 (let ((image (widget-get widget :off-glyph)))
2159 (and (display-graphic-p)
2160 (listp image)
2161 (not (eq (car image) 'image))
2162 (widget-put widget :off-glyph (setq image (eval image))))
2163 (widget-image-insert widget (widget-get widget :off) image))))
2165 (defun widget-toggle-action (widget &optional event)
2166 ;; Toggle value.
2167 (widget-value-set widget (not (widget-value widget)))
2168 (widget-apply widget :notify widget event)
2169 (run-hook-with-args 'widget-edit-functions widget))
2171 ;;; The `checkbox' Widget.
2173 (define-widget 'checkbox 'toggle
2174 "A checkbox toggle."
2175 :button-suffix ""
2176 :button-prefix ""
2177 :format "%[%v%]"
2178 :on "[X]"
2179 ;; We could probably do the same job as the images using single
2180 ;; space characters in a boxed face with a stretch specification to
2181 ;; make them square.
2182 :on-glyph '(create-image "\300\300\141\143\067\076\034\030"
2183 'xbm t :width 8 :height 8
2184 :background "grey75" ; like default mode line
2185 :foreground "black"
2186 :relief -2
2187 :ascent 'center)
2188 :off "[ ]"
2189 :off-glyph '(create-image (make-string 8 0)
2190 'xbm t :width 8 :height 8
2191 :background "grey75"
2192 :foreground "black"
2193 :relief -2
2194 :ascent 'center)
2195 :help-echo "Toggle this item."
2196 :action 'widget-checkbox-action)
2198 (defun widget-checkbox-action (widget &optional event)
2199 "Toggle checkbox, notify parent, and set active state of sibling."
2200 (widget-toggle-action widget event)
2201 (let ((sibling (widget-get-sibling widget)))
2202 (when sibling
2203 (if (widget-value widget)
2204 (widget-apply sibling :activate)
2205 (widget-apply sibling :deactivate))
2206 (widget-clear-undo))))
2208 ;;; The `checklist' Widget.
2210 (define-widget 'checklist 'default
2211 "A multiple choice widget."
2212 :convert-widget 'widget-types-convert-widget
2213 :copy 'widget-types-copy
2214 :format "%v"
2215 :offset 4
2216 :entry-format "%b %v"
2217 :greedy nil
2218 :value-create 'widget-checklist-value-create
2219 :value-get 'widget-checklist-value-get
2220 :validate 'widget-checklist-validate
2221 :match 'widget-checklist-match
2222 :match-inline 'widget-checklist-match-inline)
2224 (defun widget-checklist-value-create (widget)
2225 ;; Insert all values
2226 (let ((alist (widget-checklist-match-find widget (widget-get widget :value)))
2227 (args (widget-get widget :args)))
2228 (while args
2229 (widget-checklist-add-item widget (car args) (assq (car args) alist))
2230 (setq args (cdr args)))
2231 (widget-put widget :children (nreverse (widget-get widget :children)))))
2233 (defun widget-checklist-add-item (widget type chosen)
2234 "Create checklist item in WIDGET of type TYPE.
2235 If the item is checked, CHOSEN is a cons whose cdr is the value."
2236 (and (eq (preceding-char) ?\n)
2237 (widget-get widget :indent)
2238 (insert-char ?\s (widget-get widget :indent)))
2239 (widget-specify-insert
2240 (let* ((children (widget-get widget :children))
2241 (buttons (widget-get widget :buttons))
2242 (button-args (or (widget-get type :sibling-args)
2243 (widget-get widget :button-args)))
2244 (from (point))
2245 child button)
2246 (insert (widget-get widget :entry-format))
2247 (goto-char from)
2248 ;; Parse % escapes in format.
2249 (while (re-search-forward "%\\([bv%]\\)" nil t)
2250 (let ((escape (char-after (match-beginning 1))))
2251 (delete-backward-char 2)
2252 (cond ((eq escape ?%)
2253 (insert ?%))
2254 ((eq escape ?b)
2255 (setq button (apply 'widget-create-child-and-convert
2256 widget 'checkbox
2257 :value (not (null chosen))
2258 button-args)))
2259 ((eq escape ?v)
2260 (setq child
2261 (cond ((not chosen)
2262 (let ((child (widget-create-child widget type)))
2263 (widget-apply child :deactivate)
2264 child))
2265 ((widget-get type :inline)
2266 (widget-create-child-value
2267 widget type (cdr chosen)))
2269 (widget-create-child-value
2270 widget type (car (cdr chosen)))))))
2272 (error "Unknown escape `%c'" escape)))))
2273 ;; Update properties.
2274 (and button child (widget-put child :button button))
2275 (and button (widget-put widget :buttons (cons button buttons)))
2276 (and child (widget-put widget :children (cons child children))))))
2278 (defun widget-checklist-match (widget values)
2279 ;; All values must match a type in the checklist.
2280 (and (listp values)
2281 (null (cdr (widget-checklist-match-inline widget values)))))
2283 (defun widget-checklist-match-inline (widget values)
2284 ;; Find the values which match a type in the checklist.
2285 (let ((greedy (widget-get widget :greedy))
2286 (args (copy-sequence (widget-get widget :args)))
2287 found rest)
2288 (while values
2289 (let ((answer (widget-checklist-match-up args values)))
2290 (cond (answer
2291 (let ((vals (widget-match-inline answer values)))
2292 (setq found (append found (car vals))
2293 values (cdr vals)
2294 args (delq answer args))))
2295 (greedy
2296 (setq rest (append rest (list (car values)))
2297 values (cdr values)))
2299 (setq rest (append rest values)
2300 values nil)))))
2301 (cons found rest)))
2303 (defun widget-checklist-match-find (widget vals)
2304 "Find the vals which match a type in the checklist.
2305 Return an alist of (TYPE MATCH)."
2306 (let ((greedy (widget-get widget :greedy))
2307 (args (copy-sequence (widget-get widget :args)))
2308 found)
2309 (while vals
2310 (let ((answer (widget-checklist-match-up args vals)))
2311 (cond (answer
2312 (let ((match (widget-match-inline answer vals)))
2313 (setq found (cons (cons answer (car match)) found)
2314 vals (cdr match)
2315 args (delq answer args))))
2316 (greedy
2317 (setq vals (cdr vals)))
2319 (setq vals nil)))))
2320 found))
2322 (defun widget-checklist-match-up (args vals)
2323 "Return the first type from ARGS that matches VALS."
2324 (let (current found)
2325 (while (and args (null found))
2326 (setq current (car args)
2327 args (cdr args)
2328 found (widget-match-inline current vals)))
2329 (if found
2330 current)))
2332 (defun widget-checklist-value-get (widget)
2333 ;; The values of all selected items.
2334 (let ((children (widget-get widget :children))
2335 child result)
2336 (while children
2337 (setq child (car children)
2338 children (cdr children))
2339 (if (widget-value (widget-get child :button))
2340 (setq result (append result (widget-apply child :value-inline)))))
2341 result))
2343 (defun widget-checklist-validate (widget)
2344 ;; Ticked chilren must be valid.
2345 (let ((children (widget-get widget :children))
2346 child button found)
2347 (while (and children (not found))
2348 (setq child (car children)
2349 children (cdr children)
2350 button (widget-get child :button)
2351 found (and (widget-value button)
2352 (widget-apply child :validate))))
2353 found))
2355 ;;; The `option' Widget
2357 (define-widget 'option 'checklist
2358 "An widget with an optional item."
2359 :inline t)
2361 ;;; The `choice-item' Widget.
2363 (define-widget 'choice-item 'item
2364 "Button items that delegate action events to their parents."
2365 :action 'widget-parent-action
2366 :format "%[%t%] \n")
2368 ;;; The `radio-button' Widget.
2370 (define-widget 'radio-button 'toggle
2371 "A radio button for use in the `radio' widget."
2372 :notify 'widget-radio-button-notify
2373 :format "%[%v%]"
2374 :button-suffix ""
2375 :button-prefix ""
2376 :on "(*)"
2377 :on-glyph "radio1"
2378 :off "( )"
2379 :off-glyph "radio0")
2381 (defun widget-radio-button-notify (widget child &optional event)
2382 ;; Tell daddy.
2383 (widget-apply (widget-get widget :parent) :action widget event))
2385 ;;; The `radio-button-choice' Widget.
2387 (define-widget 'radio-button-choice 'default
2388 "Select one of multiple options."
2389 :convert-widget 'widget-types-convert-widget
2390 :copy 'widget-types-copy
2391 :offset 4
2392 :format "%v"
2393 :entry-format "%b %v"
2394 :value-create 'widget-radio-value-create
2395 :value-get 'widget-radio-value-get
2396 :value-inline 'widget-radio-value-inline
2397 :value-set 'widget-radio-value-set
2398 :error "You must push one of the buttons"
2399 :validate 'widget-radio-validate
2400 :match 'widget-choice-match
2401 :match-inline 'widget-choice-match-inline
2402 :action 'widget-radio-action)
2404 (defun widget-radio-value-create (widget)
2405 ;; Insert all values
2406 (let ((args (widget-get widget :args))
2407 arg)
2408 (while args
2409 (setq arg (car args)
2410 args (cdr args))
2411 (widget-radio-add-item widget arg))))
2413 (defun widget-radio-add-item (widget type)
2414 "Add to radio widget WIDGET a new radio button item of type TYPE."
2415 ;; (setq type (widget-convert type))
2416 (and (eq (preceding-char) ?\n)
2417 (widget-get widget :indent)
2418 (insert-char ?\s (widget-get widget :indent)))
2419 (widget-specify-insert
2420 (let* ((value (widget-get widget :value))
2421 (children (widget-get widget :children))
2422 (buttons (widget-get widget :buttons))
2423 (button-args (or (widget-get type :sibling-args)
2424 (widget-get widget :button-args)))
2425 (from (point))
2426 (chosen (and (null (widget-get widget :choice))
2427 (widget-apply type :match value)))
2428 child button)
2429 (insert (widget-get widget :entry-format))
2430 (goto-char from)
2431 ;; Parse % escapes in format.
2432 (while (re-search-forward "%\\([bv%]\\)" nil t)
2433 (let ((escape (char-after (match-beginning 1))))
2434 (delete-backward-char 2)
2435 (cond ((eq escape ?%)
2436 (insert ?%))
2437 ((eq escape ?b)
2438 (setq button (apply 'widget-create-child-and-convert
2439 widget 'radio-button
2440 :value (not (null chosen))
2441 button-args)))
2442 ((eq escape ?v)
2443 (setq child (if chosen
2444 (widget-create-child-value
2445 widget type value)
2446 (widget-create-child widget type)))
2447 (unless chosen
2448 (widget-apply child :deactivate)))
2450 (error "Unknown escape `%c'" escape)))))
2451 ;; Update properties.
2452 (when chosen
2453 (widget-put widget :choice type))
2454 (when button
2455 (widget-put child :button button)
2456 (widget-put widget :buttons (nconc buttons (list button))))
2457 (when child
2458 (widget-put widget :children (nconc children (list child))))
2459 child)))
2461 (defun widget-radio-value-get (widget)
2462 ;; Get value of the child widget.
2463 (let ((chosen (widget-radio-chosen widget)))
2464 (and chosen (widget-value chosen))))
2466 (defun widget-radio-chosen (widget)
2467 "Return the widget representing the chosen radio button."
2468 (let ((children (widget-get widget :children))
2469 current found)
2470 (while children
2471 (setq current (car children)
2472 children (cdr children))
2473 (when (widget-apply (widget-get current :button) :value-get)
2474 (setq found current
2475 children nil)))
2476 found))
2478 (defun widget-radio-value-inline (widget)
2479 ;; Get value of the child widget.
2480 (let ((children (widget-get widget :children))
2481 current found)
2482 (while children
2483 (setq current (car children)
2484 children (cdr children))
2485 (when (widget-apply (widget-get current :button) :value-get)
2486 (setq found (widget-apply current :value-inline)
2487 children nil)))
2488 found))
2490 (defun widget-radio-value-set (widget value)
2491 ;; We can't just delete and recreate a radio widget, since children
2492 ;; can be added after the original creation and won't be recreated
2493 ;; by `:create'.
2494 (let ((children (widget-get widget :children))
2495 current found)
2496 (while children
2497 (setq current (car children)
2498 children (cdr children))
2499 (let* ((button (widget-get current :button))
2500 (match (and (not found)
2501 (widget-apply current :match value))))
2502 (widget-value-set button match)
2503 (if match
2504 (progn
2505 (widget-value-set current value)
2506 (widget-apply current :activate))
2507 (widget-apply current :deactivate))
2508 (setq found (or found match))))))
2510 (defun widget-radio-validate (widget)
2511 ;; Valid if we have made a valid choice.
2512 (let ((children (widget-get widget :children))
2513 current found button)
2514 (while (and children (not found))
2515 (setq current (car children)
2516 children (cdr children)
2517 button (widget-get current :button)
2518 found (widget-apply button :value-get)))
2519 (if found
2520 (widget-apply current :validate)
2521 widget)))
2523 (defun widget-radio-action (widget child event)
2524 ;; Check if a radio button was pressed.
2525 (let ((children (widget-get widget :children))
2526 (buttons (widget-get widget :buttons))
2527 current)
2528 (when (memq child buttons)
2529 (while children
2530 (setq current (car children)
2531 children (cdr children))
2532 (let* ((button (widget-get current :button)))
2533 (cond ((eq child button)
2534 (widget-value-set button t)
2535 (widget-apply current :activate))
2536 ((widget-value button)
2537 (widget-value-set button nil)
2538 (widget-apply current :deactivate)))))))
2539 ;; Pass notification to parent.
2540 (widget-apply widget :notify child event))
2542 ;;; The `insert-button' Widget.
2544 (define-widget 'insert-button 'push-button
2545 "An insert button for the `editable-list' widget."
2546 :tag "INS"
2547 :help-echo "Insert a new item into the list at this position."
2548 :action 'widget-insert-button-action)
2550 (defun widget-insert-button-action (widget &optional event)
2551 ;; Ask the parent to insert a new item.
2552 (widget-apply (widget-get widget :parent)
2553 :insert-before (widget-get widget :widget)))
2555 ;;; The `delete-button' Widget.
2557 (define-widget 'delete-button 'push-button
2558 "A delete button for the `editable-list' widget."
2559 :tag "DEL"
2560 :help-echo "Delete this item from the list."
2561 :action 'widget-delete-button-action)
2563 (defun widget-delete-button-action (widget &optional event)
2564 ;; Ask the parent to insert a new item.
2565 (widget-apply (widget-get widget :parent)
2566 :delete-at (widget-get widget :widget)))
2568 ;;; The `editable-list' Widget.
2570 ;; (defcustom widget-editable-list-gui nil
2571 ;; "If non-nil, use GUI push-buttons in editable list when available."
2572 ;; :type 'boolean
2573 ;; :group 'widgets)
2575 (define-widget 'editable-list 'default
2576 "A variable list of widgets of the same type."
2577 :convert-widget 'widget-types-convert-widget
2578 :copy 'widget-types-copy
2579 :offset 12
2580 :format "%v%i\n"
2581 :format-handler 'widget-editable-list-format-handler
2582 :entry-format "%i %d %v"
2583 :value-create 'widget-editable-list-value-create
2584 :value-get 'widget-editable-list-value-get
2585 :validate 'widget-children-validate
2586 :match 'widget-editable-list-match
2587 :match-inline 'widget-editable-list-match-inline
2588 :insert-before 'widget-editable-list-insert-before
2589 :delete-at 'widget-editable-list-delete-at)
2591 (defun widget-editable-list-format-handler (widget escape)
2592 ;; We recognize the insert button.
2593 ;; (let ((widget-push-button-gui widget-editable-list-gui))
2594 (cond ((eq escape ?i)
2595 (and (widget-get widget :indent)
2596 (insert-char ?\s (widget-get widget :indent)))
2597 (apply 'widget-create-child-and-convert
2598 widget 'insert-button
2599 (widget-get widget :append-button-args)))
2601 (widget-default-format-handler widget escape)))
2602 ;; )
2605 (defun widget-editable-list-value-create (widget)
2606 ;; Insert all values
2607 (let* ((value (widget-get widget :value))
2608 (type (nth 0 (widget-get widget :args)))
2609 children)
2610 (widget-put widget :value-pos (copy-marker (point)))
2611 (set-marker-insertion-type (widget-get widget :value-pos) t)
2612 (while value
2613 (let ((answer (widget-match-inline type value)))
2614 (if answer
2615 (setq children (cons (widget-editable-list-entry-create
2616 widget
2617 (if (widget-get type :inline)
2618 (car answer)
2619 (car (car answer)))
2621 children)
2622 value (cdr answer))
2623 (setq value nil))))
2624 (widget-put widget :children (nreverse children))))
2626 (defun widget-editable-list-value-get (widget)
2627 ;; Get value of the child widget.
2628 (apply 'append (mapcar (lambda (child) (widget-apply child :value-inline))
2629 (widget-get widget :children))))
2631 (defun widget-editable-list-match (widget value)
2632 ;; Value must be a list and all the members must match the type.
2633 (and (listp value)
2634 (null (cdr (widget-editable-list-match-inline widget value)))))
2636 (defun widget-editable-list-match-inline (widget value)
2637 (let ((type (nth 0 (widget-get widget :args)))
2638 (ok t)
2639 found)
2640 (while (and value ok)
2641 (let ((answer (widget-match-inline type value)))
2642 (if answer
2643 (setq found (append found (car answer))
2644 value (cdr answer))
2645 (setq ok nil))))
2646 (cons found value)))
2648 (defun widget-editable-list-insert-before (widget before)
2649 ;; Insert a new child in the list of children.
2650 (save-excursion
2651 (let ((children (widget-get widget :children))
2652 (inhibit-read-only t)
2653 before-change-functions
2654 after-change-functions)
2655 (cond (before
2656 (goto-char (widget-get before :entry-from)))
2658 (goto-char (widget-get widget :value-pos))))
2659 (let ((child (widget-editable-list-entry-create
2660 widget nil nil)))
2661 (when (< (widget-get child :entry-from) (widget-get widget :from))
2662 (set-marker (widget-get widget :from)
2663 (widget-get child :entry-from)))
2664 (if (eq (car children) before)
2665 (widget-put widget :children (cons child children))
2666 (while (not (eq (car (cdr children)) before))
2667 (setq children (cdr children)))
2668 (setcdr children (cons child (cdr children)))))))
2669 (widget-setup)
2670 (widget-apply widget :notify widget))
2672 (defun widget-editable-list-delete-at (widget child)
2673 ;; Delete child from list of children.
2674 (save-excursion
2675 (let ((buttons (copy-sequence (widget-get widget :buttons)))
2676 button
2677 (inhibit-read-only t)
2678 before-change-functions
2679 after-change-functions)
2680 (while buttons
2681 (setq button (car buttons)
2682 buttons (cdr buttons))
2683 (when (eq (widget-get button :widget) child)
2684 (widget-put widget
2685 :buttons (delq button (widget-get widget :buttons)))
2686 (widget-delete button))))
2687 (let ((entry-from (widget-get child :entry-from))
2688 (entry-to (widget-get child :entry-to))
2689 (inhibit-read-only t)
2690 before-change-functions
2691 after-change-functions)
2692 (widget-delete child)
2693 (delete-region entry-from entry-to)
2694 (set-marker entry-from nil)
2695 (set-marker entry-to nil))
2696 (widget-put widget :children (delq child (widget-get widget :children))))
2697 (widget-setup)
2698 (widget-apply widget :notify widget))
2700 (defun widget-editable-list-entry-create (widget value conv)
2701 ;; Create a new entry to the list.
2702 (let ((type (nth 0 (widget-get widget :args)))
2703 ;; (widget-push-button-gui widget-editable-list-gui)
2704 child delete insert)
2705 (widget-specify-insert
2706 (save-excursion
2707 (and (widget-get widget :indent)
2708 (insert-char ?\s (widget-get widget :indent)))
2709 (insert (widget-get widget :entry-format)))
2710 ;; Parse % escapes in format.
2711 (while (re-search-forward "%\\(.\\)" nil t)
2712 (let ((escape (char-after (match-beginning 1))))
2713 (delete-backward-char 2)
2714 (cond ((eq escape ?%)
2715 (insert ?%))
2716 ((eq escape ?i)
2717 (setq insert (apply 'widget-create-child-and-convert
2718 widget 'insert-button
2719 (widget-get widget :insert-button-args))))
2720 ((eq escape ?d)
2721 (setq delete (apply 'widget-create-child-and-convert
2722 widget 'delete-button
2723 (widget-get widget :delete-button-args))))
2724 ((eq escape ?v)
2725 (if conv
2726 (setq child (widget-create-child-value
2727 widget type value))
2728 (setq child (widget-create-child-value
2729 widget type (widget-default-get type)))))
2731 (error "Unknown escape `%c'" escape)))))
2732 (let ((buttons (widget-get widget :buttons)))
2733 (if insert (push insert buttons))
2734 (if delete (push delete buttons))
2735 (widget-put widget :buttons buttons))
2736 (let ((entry-from (point-min-marker))
2737 (entry-to (point-max-marker)))
2738 (set-marker-insertion-type entry-from t)
2739 (set-marker-insertion-type entry-to nil)
2740 (widget-put child :entry-from entry-from)
2741 (widget-put child :entry-to entry-to)))
2742 (if insert (widget-put insert :widget child))
2743 (if delete (widget-put delete :widget child))
2744 child))
2746 ;;; The `group' Widget.
2748 (define-widget 'group 'default
2749 "A widget which groups other widgets inside."
2750 :convert-widget 'widget-types-convert-widget
2751 :copy 'widget-types-copy
2752 :format "%v"
2753 :value-create 'widget-group-value-create
2754 :value-get 'widget-editable-list-value-get
2755 :default-get 'widget-group-default-get
2756 :validate 'widget-children-validate
2757 :match 'widget-group-match
2758 :match-inline 'widget-group-match-inline)
2760 (defun widget-group-value-create (widget)
2761 ;; Create each component.
2762 (let ((args (widget-get widget :args))
2763 (value (widget-get widget :value))
2764 arg answer children)
2765 (while args
2766 (setq arg (car args)
2767 args (cdr args)
2768 answer (widget-match-inline arg value)
2769 value (cdr answer))
2770 (and (eq (preceding-char) ?\n)
2771 (widget-get widget :indent)
2772 (insert-char ?\s (widget-get widget :indent)))
2773 (push (cond ((null answer)
2774 (widget-create-child widget arg))
2775 ((widget-get arg :inline)
2776 (widget-create-child-value widget arg (car answer)))
2778 (widget-create-child-value widget arg (car (car answer)))))
2779 children))
2780 (widget-put widget :children (nreverse children))))
2782 (defun widget-group-default-get (widget)
2783 ;; Get the default of the components.
2784 (mapcar 'widget-default-get (widget-get widget :args)))
2786 (defun widget-group-match (widget values)
2787 ;; Match if the components match.
2788 (and (listp values)
2789 (let ((match (widget-group-match-inline widget values)))
2790 (and match (null (cdr match))))))
2792 (defun widget-group-match-inline (widget vals)
2793 ;; Match if the components match.
2794 (let ((args (widget-get widget :args))
2795 argument answer found)
2796 (while args
2797 (setq argument (car args)
2798 args (cdr args)
2799 answer (widget-match-inline argument vals))
2800 (if answer
2801 (setq vals (cdr answer)
2802 found (append found (car answer)))
2803 (setq vals nil
2804 args nil)))
2805 (if answer
2806 (cons found vals))))
2808 ;;; The `visibility' Widget.
2810 (define-widget 'visibility 'item
2811 "An indicator and manipulator for hidden items."
2812 :format "%[%v%]"
2813 :button-prefix ""
2814 :button-suffix ""
2815 :on "Hide"
2816 :off "Show"
2817 :value-create 'widget-visibility-value-create
2818 :action 'widget-toggle-action
2819 :match (lambda (widget value) t))
2821 (defun widget-visibility-value-create (widget)
2822 ;; Insert text representing the `on' and `off' states.
2823 (let ((on (widget-get widget :on))
2824 (off (widget-get widget :off)))
2825 (if on
2826 (setq on (concat widget-push-button-prefix
2828 widget-push-button-suffix))
2829 (setq on ""))
2830 (if off
2831 (setq off (concat widget-push-button-prefix
2833 widget-push-button-suffix))
2834 (setq off ""))
2835 (if (widget-value widget)
2836 (widget-image-insert widget on "down" "down-pushed")
2837 (widget-image-insert widget off "right" "right-pushed"))))
2839 ;;; The `documentation-link' Widget.
2841 ;; This is a helper widget for `documentation-string'.
2843 (define-widget 'documentation-link 'link
2844 "Link type used in documentation strings."
2845 :tab-order -1
2846 :help-echo "Describe this symbol"
2847 :action 'widget-documentation-link-action)
2849 (defun widget-documentation-link-action (widget &optional event)
2850 "Display documentation for WIDGET's value. Ignore optional argument EVENT."
2851 (let* ((string (widget-get widget :value))
2852 (symbol (intern string)))
2853 (if (and (fboundp symbol) (boundp symbol))
2854 ;; If there are two doc strings, give the user a way to pick one.
2855 (apropos (concat "\\`" (regexp-quote string) "\\'"))
2856 (if (fboundp symbol)
2857 (describe-function symbol)
2858 (describe-variable symbol)))))
2860 (defcustom widget-documentation-links t
2861 "Add hyperlinks to documentation strings when non-nil."
2862 :type 'boolean
2863 :group 'widget-documentation)
2865 (defcustom widget-documentation-link-regexp "`\\([^\n`' ]+\\)'"
2866 "Regexp for matching potential links in documentation strings.
2867 The first group should be the link itself."
2868 :type 'regexp
2869 :group 'widget-documentation)
2871 (defcustom widget-documentation-link-p 'intern-soft
2872 "Predicate used to test if a string is useful as a link.
2873 The value should be a function. The function will be called with one
2874 argument, a string, and should return non-nil if there should be a
2875 link for that string."
2876 :type 'function
2877 :options '(widget-documentation-link-p)
2878 :group 'widget-documentation)
2880 (defcustom widget-documentation-link-type 'documentation-link
2881 "Widget type used for links in documentation strings."
2882 :type 'symbol
2883 :group 'widget-documentation)
2885 (defun widget-documentation-link-add (widget from to)
2886 (widget-specify-doc widget from to)
2887 (when widget-documentation-links
2888 (let ((regexp widget-documentation-link-regexp)
2889 (buttons (widget-get widget :buttons))
2890 (widget-mouse-face (default-value 'widget-mouse-face))
2891 (widget-button-face widget-documentation-face)
2892 (widget-button-pressed-face widget-documentation-face))
2893 (save-excursion
2894 (goto-char from)
2895 (while (re-search-forward regexp to t)
2896 (let ((name (match-string 1))
2897 (begin (match-beginning 1))
2898 (end (match-end 1)))
2899 (when (funcall widget-documentation-link-p name)
2900 (push (widget-convert-button widget-documentation-link-type
2901 begin end :value name)
2902 buttons)))))
2903 (widget-put widget :buttons buttons)))
2904 (let ((indent (widget-get widget :indent)))
2905 (when (and indent (not (zerop indent)))
2906 (save-excursion
2907 (save-restriction
2908 (narrow-to-region from to)
2909 (goto-char (point-min))
2910 (while (search-forward "\n" nil t)
2911 (insert-char ?\s indent)))))))
2913 ;;; The `documentation-string' Widget.
2915 (define-widget 'documentation-string 'item
2916 "A documentation string."
2917 :format "%v"
2918 :action 'widget-documentation-string-action
2919 :value-create 'widget-documentation-string-value-create
2920 :visibility-widget 'visibility)
2922 (defun widget-documentation-string-value-create (widget)
2923 ;; Insert documentation string.
2924 (let ((doc (widget-value widget))
2925 (indent (widget-get widget :indent))
2926 (shown (widget-get (widget-get widget :parent) :documentation-shown))
2927 (start (point)))
2928 (if (string-match "\n" doc)
2929 (let ((before (substring doc 0 (match-beginning 0)))
2930 (after (substring doc (match-beginning 0)))
2931 button)
2932 (when (and indent (not (zerop indent)))
2933 (insert-char ?\s indent))
2934 (insert before ?\s)
2935 (widget-documentation-link-add widget start (point))
2936 (setq button
2937 (widget-create-child-and-convert
2938 widget (widget-get widget :visibility-widget)
2939 :help-echo "Show or hide rest of the documentation."
2940 :on "Hide Rest"
2941 :off "More"
2942 :always-active t
2943 :action 'widget-parent-action
2944 shown))
2945 (when shown
2946 (setq start (point))
2947 (when (and indent (not (zerop indent)))
2948 (insert-char ?\s indent))
2949 (insert after)
2950 (widget-documentation-link-add widget start (point)))
2951 (widget-put widget :buttons (list button)))
2952 (when (and indent (not (zerop indent)))
2953 (insert-char ?\s indent))
2954 (insert doc)
2955 (widget-documentation-link-add widget start (point))))
2956 (insert ?\n))
2958 (defun widget-documentation-string-action (widget &rest ignore)
2959 ;; Toggle documentation.
2960 (let ((parent (widget-get widget :parent)))
2961 (widget-put parent :documentation-shown
2962 (not (widget-get parent :documentation-shown))))
2963 ;; Redraw.
2964 (widget-value-set widget (widget-value widget)))
2966 (defun widget-add-documentation-string-button (widget &rest args)
2967 "Insert a new `documentation-string' widget based on WIDGET.
2968 The new widget becomes a child of WIDGET, and is also added to
2969 its `:buttons' list. The documentation string is found from
2970 WIDGET using the function `widget-docstring'.
2971 Optional ARGS specifies additional keyword arguments for the
2972 `documentation-string' widget."
2973 (let ((doc (widget-docstring widget))
2974 (indent (widget-get widget :indent))
2975 (doc-indent (widget-get widget :documentation-indent)))
2976 (when doc
2977 (and (eq (preceding-char) ?\n)
2978 indent
2979 (insert-char ?\s indent))
2980 (unless (or (numberp doc-indent) (null doc-indent))
2981 (setq doc-indent 0))
2982 (widget-put widget :buttons
2983 (cons (apply 'widget-create-child-and-convert
2984 widget 'documentation-string
2985 :indent doc-indent
2986 (nconc args (list doc)))
2987 (widget-get widget :buttons))))))
2989 ;;; The Sexp Widgets.
2991 (define-widget 'const 'item
2992 "An immutable sexp."
2993 :prompt-value 'widget-const-prompt-value
2994 :format "%t\n%d")
2996 (defun widget-const-prompt-value (widget prompt value unbound)
2997 ;; Return the value of the const.
2998 (widget-value widget))
3000 (define-widget 'function-item 'const
3001 "An immutable function name."
3002 :format "%v\n%h"
3003 :documentation-property (lambda (symbol)
3004 (condition-case nil
3005 (documentation symbol t)
3006 (error nil))))
3008 (define-widget 'variable-item 'const
3009 "An immutable variable name."
3010 :format "%v\n%h"
3011 :documentation-property 'variable-documentation)
3013 (define-widget 'other 'sexp
3014 "Matches any value, but doesn't let the user edit the value.
3015 This is useful as last item in a `choice' widget.
3016 You should use this widget type with a default value,
3017 as in (other DEFAULT) or (other :tag \"NAME\" DEFAULT).
3018 If the user selects this alternative, that specifies DEFAULT
3019 as the value."
3020 :tag "Other"
3021 :format "%t%n"
3022 :value 'other)
3024 (defvar widget-string-prompt-value-history nil
3025 "History of input to `widget-string-prompt-value'.")
3027 (define-widget 'string 'editable-field
3028 "A string"
3029 :tag "String"
3030 :format "%{%t%}: %v"
3031 :complete-function 'ispell-complete-word
3032 :prompt-history 'widget-string-prompt-value-history)
3034 (defvar widget)
3036 (defun widget-string-complete ()
3037 "Complete contents of string field.
3038 Completions are taken from the :completion-alist property of the
3039 widget. If that isn't a list, it's evalled and expected to yield a list."
3040 (interactive)
3041 (let* ((completion-ignore-case (widget-get widget :completion-ignore-case))
3042 (alist (widget-get widget :completion-alist))
3043 (_ (unless (listp alist)
3044 (setq alist (eval alist)))))
3045 (completion-in-region (widget-field-start widget)
3046 (max (point) (widget-field-text-end widget))
3047 alist)))
3049 (define-widget 'regexp 'string
3050 "A regular expression."
3051 :match 'widget-regexp-match
3052 :validate 'widget-regexp-validate
3053 ;; Doesn't work well with terminating newline.
3054 ;; :value-face 'widget-single-line-field
3055 :tag "Regexp")
3057 (defun widget-regexp-match (widget value)
3058 ;; Match valid regexps.
3059 (and (stringp value)
3060 (condition-case nil
3061 (prog1 t
3062 (string-match value ""))
3063 (error nil))))
3065 (defun widget-regexp-validate (widget)
3066 "Check that the value of WIDGET is a valid regexp."
3067 (condition-case data
3068 (prog1 nil
3069 (string-match (widget-value widget) ""))
3070 (error (widget-put widget :error (error-message-string data))
3071 widget)))
3073 (define-widget 'file 'string
3074 "A file widget.
3075 It reads a file name from an editable text field."
3076 :complete-function 'widget-file-complete
3077 :prompt-value 'widget-file-prompt-value
3078 :format "%{%t%}: %v"
3079 ;; Doesn't work well with terminating newline.
3080 ;; :value-face 'widget-single-line-field
3081 :tag "File")
3083 (defun widget-file-complete ()
3084 "Perform completion on file name preceding point."
3085 (interactive)
3086 (completion-in-region (widget-field-start widget)
3087 (max (point) (widget-field-text-end widget))
3088 'completion-file-name-table))
3090 (defun widget-file-prompt-value (widget prompt value unbound)
3091 ;; Read file from minibuffer.
3092 (abbreviate-file-name
3093 (if unbound
3094 (read-file-name prompt)
3095 (let ((prompt2 (format "%s (default %s): " prompt value))
3096 (dir (file-name-directory value))
3097 (file (file-name-nondirectory value))
3098 (must-match (widget-get widget :must-match)))
3099 (read-file-name prompt2 dir nil must-match file)))))
3101 ;;;(defun widget-file-action (widget &optional event)
3102 ;;; ;; Read a file name from the minibuffer.
3103 ;;; (let* ((value (widget-value widget))
3104 ;;; (dir (file-name-directory value))
3105 ;;; (file (file-name-nondirectory value))
3106 ;;; (menu-tag (widget-apply widget :menu-tag-get))
3107 ;;; (must-match (widget-get widget :must-match))
3108 ;;; (answer (read-file-name (concat menu-tag " (default " value "): ")
3109 ;;; dir nil must-match file)))
3110 ;;; (widget-value-set widget (abbreviate-file-name answer))
3111 ;;; (widget-setup)
3112 ;;; (widget-apply widget :notify widget event)))
3114 ;; Fixme: use file-name-as-directory.
3115 (define-widget 'directory 'file
3116 "A directory widget.
3117 It reads a directory name from an editable text field."
3118 :tag "Directory")
3120 (defvar widget-symbol-prompt-value-history nil
3121 "History of input to `widget-symbol-prompt-value'.")
3123 (define-widget 'symbol 'editable-field
3124 "A Lisp symbol."
3125 :value nil
3126 :tag "Symbol"
3127 :format "%{%t%}: %v"
3128 :match (lambda (widget value) (symbolp value))
3129 :complete-function 'lisp-complete-symbol
3130 :prompt-internal 'widget-symbol-prompt-internal
3131 :prompt-match 'symbolp
3132 :prompt-history 'widget-symbol-prompt-value-history
3133 :value-to-internal (lambda (widget value)
3134 (if (symbolp value)
3135 (symbol-name value)
3136 value))
3137 :value-to-external (lambda (widget value)
3138 (if (stringp value)
3139 (intern value)
3140 value)))
3142 (defun widget-symbol-prompt-internal (widget prompt initial history)
3143 ;; Read file from minibuffer.
3144 (let ((answer (completing-read prompt obarray
3145 (widget-get widget :prompt-match)
3146 nil initial history)))
3147 (if (and (stringp answer)
3148 (not (zerop (length answer))))
3149 answer
3150 (error "No value"))))
3152 (defvar widget-function-prompt-value-history nil
3153 "History of input to `widget-function-prompt-value'.")
3155 (define-widget 'function 'restricted-sexp
3156 "A Lisp function."
3157 :complete-function (lambda ()
3158 (interactive)
3159 (lisp-complete-symbol 'fboundp))
3160 :prompt-value 'widget-field-prompt-value
3161 :prompt-internal 'widget-symbol-prompt-internal
3162 :prompt-match 'fboundp
3163 :prompt-history 'widget-function-prompt-value-history
3164 :action 'widget-field-action
3165 :match-alternatives '(functionp)
3166 :validate (lambda (widget)
3167 (unless (functionp (widget-value widget))
3168 (widget-put widget :error (format "Invalid function: %S"
3169 (widget-value widget)))
3170 widget))
3171 :value 'ignore
3172 :tag "Function")
3174 (defvar widget-variable-prompt-value-history nil
3175 "History of input to `widget-variable-prompt-value'.")
3177 (define-widget 'variable 'symbol
3178 "A Lisp variable."
3179 :prompt-match 'boundp
3180 :prompt-history 'widget-variable-prompt-value-history
3181 :complete-function (lambda ()
3182 (interactive)
3183 (lisp-complete-symbol 'boundp))
3184 :tag "Variable")
3186 (define-widget 'coding-system 'symbol
3187 "A MULE coding-system."
3188 :format "%{%t%}: %v"
3189 :tag "Coding system"
3190 :base-only nil
3191 :prompt-history 'coding-system-value-history
3192 :prompt-value 'widget-coding-system-prompt-value
3193 :action 'widget-coding-system-action
3194 :complete-function (lambda ()
3195 (interactive)
3196 (lisp-complete-symbol 'coding-system-p))
3197 :validate (lambda (widget)
3198 (unless (coding-system-p (widget-value widget))
3199 (widget-put widget :error (format "Invalid coding system: %S"
3200 (widget-value widget)))
3201 widget))
3202 :value 'undecided
3203 :prompt-match 'coding-system-p)
3205 (defun widget-coding-system-prompt-value (widget prompt value unbound)
3206 "Read coding-system from minibuffer."
3207 (if (widget-get widget :base-only)
3208 (intern
3209 (completing-read (format "%s (default %s): " prompt value)
3210 (mapcar #'list (coding-system-list t)) nil nil nil
3211 coding-system-history))
3212 (read-coding-system (format "%s (default %s): " prompt value) value)))
3214 (defun widget-coding-system-action (widget &optional event)
3215 (let ((answer
3216 (widget-coding-system-prompt-value
3217 widget
3218 (widget-apply widget :menu-tag-get)
3219 (widget-value widget)
3220 t)))
3221 (widget-value-set widget answer)
3222 (widget-apply widget :notify widget event)
3223 (widget-setup)))
3225 ;;; I'm not sure about what this is good for? KFS.
3226 (defvar widget-key-sequence-prompt-value-history nil
3227 "History of input to `widget-key-sequence-prompt-value'.")
3229 (defvar widget-key-sequence-default-value [ignore]
3230 "Default value for an empty key sequence.")
3232 (defvar widget-key-sequence-map
3233 (let ((map (make-sparse-keymap)))
3234 (set-keymap-parent map widget-field-keymap)
3235 (define-key map [(control ?q)] 'widget-key-sequence-read-event)
3236 map))
3238 (define-widget 'key-sequence 'restricted-sexp
3239 "A key sequence."
3240 :prompt-value 'widget-field-prompt-value
3241 :prompt-internal 'widget-symbol-prompt-internal
3242 ; :prompt-match 'fboundp ;; What was this good for? KFS
3243 :prompt-history 'widget-key-sequence-prompt-value-history
3244 :action 'widget-field-action
3245 :match-alternatives '(stringp vectorp)
3246 :format "%{%t%}: %v"
3247 :validate 'widget-key-sequence-validate
3248 :value-to-internal 'widget-key-sequence-value-to-internal
3249 :value-to-external 'widget-key-sequence-value-to-external
3250 :value widget-key-sequence-default-value
3251 :keymap widget-key-sequence-map
3252 :help-echo "C-q: insert KEY, EVENT, or CODE; RET: enter value"
3253 :tag "Key sequence")
3255 (defun widget-key-sequence-read-event (ev)
3256 (interactive (list
3257 (let ((inhibit-quit t) quit-flag)
3258 (read-event "Insert KEY, EVENT, or CODE: "))))
3259 (let ((ev2 (and (memq 'down (event-modifiers ev))
3260 (read-event)))
3261 (tr (and (keymapp function-key-map)
3262 (lookup-key function-key-map (vector ev)))))
3263 (when (and (integerp ev)
3264 (or (and (<= ?0 ev) (< ev (+ ?0 (min 10 read-quoted-char-radix))))
3265 (and (<= ?a (downcase ev))
3266 (< (downcase ev) (+ ?a -10 (min 36 read-quoted-char-radix))))))
3267 (setq unread-command-events (cons ev unread-command-events)
3268 ev (read-quoted-char (format "Enter code (radix %d)" read-quoted-char-radix))
3269 tr nil)
3270 (if (and (integerp ev) (not (characterp ev)))
3271 (insert (char-to-string ev)))) ;; throw invalid char error
3272 (setq ev (key-description (list ev)))
3273 (when (arrayp tr)
3274 (setq tr (key-description (list (aref tr 0))))
3275 (if (y-or-n-p (format "Key %s is translated to %s -- use %s? " ev tr tr))
3276 (setq ev tr ev2 nil)))
3277 (insert (if (= (char-before) ?\s) "" " ") ev " ")
3278 (if ev2
3279 (insert (key-description (list ev2)) " "))))
3281 (defun widget-key-sequence-validate (widget)
3282 (unless (or (stringp (widget-value widget))
3283 (vectorp (widget-value widget)))
3284 (widget-put widget :error (format "Invalid key sequence: %S"
3285 (widget-value widget)))
3286 widget))
3288 (defun widget-key-sequence-value-to-internal (widget value)
3289 (if (widget-apply widget :match value)
3290 (if (equal value widget-key-sequence-default-value)
3292 (key-description value))
3293 value))
3295 (defun widget-key-sequence-value-to-external (widget value)
3296 (if (stringp value)
3297 (if (string-match "\\`[[:space:]]*\\'" value)
3298 widget-key-sequence-default-value
3299 (read-kbd-macro value))
3300 value))
3303 (define-widget 'sexp 'editable-field
3304 "An arbitrary Lisp expression."
3305 :tag "Lisp expression"
3306 :format "%{%t%}: %v"
3307 :value nil
3308 :validate 'widget-sexp-validate
3309 :match (lambda (widget value) t)
3310 :value-to-internal 'widget-sexp-value-to-internal
3311 :value-to-external (lambda (widget value) (read value))
3312 :prompt-history 'widget-sexp-prompt-value-history
3313 :prompt-value 'widget-sexp-prompt-value)
3315 (defun widget-sexp-value-to-internal (widget value)
3316 ;; Use pp for printer representation.
3317 (let ((pp (if (symbolp value)
3318 (prin1-to-string value)
3319 (pp-to-string value))))
3320 (while (string-match "\n\\'" pp)
3321 (setq pp (substring pp 0 -1)))
3322 (if (or (string-match "\n\\'" pp)
3323 (> (length pp) 40))
3324 (concat "\n" pp)
3325 pp)))
3327 (defun widget-sexp-validate (widget)
3328 ;; Valid if we can read the string and there is no junk left after it.
3329 (with-temp-buffer
3330 (insert (widget-apply widget :value-get))
3331 (goto-char (point-min))
3332 (let (err)
3333 (condition-case data
3334 (progn
3335 ;; Avoid a confusing end-of-file error.
3336 (skip-syntax-forward "\\s-")
3337 (if (eobp)
3338 (setq err "Empty sexp -- use `nil'?")
3339 (unless (widget-apply widget :match (read (current-buffer)))
3340 (setq err (widget-get widget :type-error))))
3341 ;; Allow whitespace after expression.
3342 (skip-syntax-forward "\\s-")
3343 (if (and (not (eobp))
3344 (not err))
3345 (setq err (format "Junk at end of expression: %s"
3346 (buffer-substring (point)
3347 (point-max))))))
3348 (end-of-file ; Avoid confusing error message.
3349 (setq err "Unbalanced sexp"))
3350 (error (setq err (error-message-string data))))
3351 (if (not err)
3353 (widget-put widget :error err)
3354 widget))))
3356 (defvar widget-sexp-prompt-value-history nil
3357 "History of input to `widget-sexp-prompt-value'.")
3359 (defun widget-sexp-prompt-value (widget prompt value unbound)
3360 ;; Read an arbitrary sexp.
3361 (let ((found (read-string prompt
3362 (if unbound nil (cons (prin1-to-string value) 0))
3363 (widget-get widget :prompt-history))))
3364 (let ((answer (read-from-string found)))
3365 (unless (= (cdr answer) (length found))
3366 (error "Junk at end of expression: %s"
3367 (substring found (cdr answer))))
3368 (car answer))))
3370 (define-widget 'restricted-sexp 'sexp
3371 "A Lisp expression restricted to values that match.
3372 To use this type, you must define :match or :match-alternatives."
3373 :type-error "The specified value is not valid"
3374 :match 'widget-restricted-sexp-match
3375 :value-to-internal (lambda (widget value)
3376 (if (widget-apply widget :match value)
3377 (prin1-to-string value)
3378 value)))
3380 (defun widget-restricted-sexp-match (widget value)
3381 (let ((alternatives (widget-get widget :match-alternatives))
3382 matched)
3383 (while (and alternatives (not matched))
3384 (if (cond ((functionp (car alternatives))
3385 (funcall (car alternatives) value))
3386 ((and (consp (car alternatives))
3387 (eq (car (car alternatives)) 'quote))
3388 (eq value (nth 1 (car alternatives)))))
3389 (setq matched t))
3390 (setq alternatives (cdr alternatives)))
3391 matched))
3393 (define-widget 'integer 'restricted-sexp
3394 "An integer."
3395 :tag "Integer"
3396 :value 0
3397 :type-error "This field should contain an integer"
3398 :match-alternatives '(integerp))
3400 (define-widget 'number 'restricted-sexp
3401 "A number (floating point or integer)."
3402 :tag "Number"
3403 :value 0.0
3404 :type-error "This field should contain a number (floating point or integer)"
3405 :match-alternatives '(numberp))
3407 (define-widget 'float 'restricted-sexp
3408 "A floating point number."
3409 :tag "Floating point number"
3410 :value 0.0
3411 :type-error "This field should contain a floating point number"
3412 :match-alternatives '(floatp))
3414 (define-widget 'character 'editable-field
3415 "A character."
3416 :tag "Character"
3417 :value 0
3418 :size 1
3419 :format "%{%t%}: %v\n"
3420 :valid-regexp "\\`.\\'"
3421 :error "This field should contain a single character"
3422 :value-to-internal (lambda (widget value)
3423 (if (stringp value)
3424 value
3425 (char-to-string value)))
3426 :value-to-external (lambda (widget value)
3427 (if (stringp value)
3428 (aref value 0)
3429 value))
3430 :match (lambda (widget value)
3431 (characterp value)))
3433 (define-widget 'list 'group
3434 "A Lisp list."
3435 :tag "List"
3436 :format "%{%t%}:\n%v")
3438 (define-widget 'vector 'group
3439 "A Lisp vector."
3440 :tag "Vector"
3441 :format "%{%t%}:\n%v"
3442 :match 'widget-vector-match
3443 :value-to-internal (lambda (widget value) (append value nil))
3444 :value-to-external (lambda (widget value) (apply 'vector value)))
3446 (defun widget-vector-match (widget value)
3447 (and (vectorp value)
3448 (widget-group-match widget
3449 (widget-apply widget :value-to-internal value))))
3451 (define-widget 'cons 'group
3452 "A cons-cell."
3453 :tag "Cons-cell"
3454 :format "%{%t%}:\n%v"
3455 :match 'widget-cons-match
3456 :value-to-internal (lambda (widget value)
3457 (list (car value) (cdr value)))
3458 :value-to-external (lambda (widget value)
3459 (apply 'cons value)))
3461 (defun widget-cons-match (widget value)
3462 (and (consp value)
3463 (widget-group-match widget
3464 (widget-apply widget :value-to-internal value))))
3466 ;;; The `lazy' Widget.
3468 ;; Recursive datatypes.
3470 (define-widget 'lazy 'default
3471 "Base widget for recursive datastructures.
3473 The `lazy' widget will, when instantiated, contain a single inferior
3474 widget, of the widget type specified by the :type parameter. The
3475 value of the `lazy' widget is the same as the value of the inferior
3476 widget. When deriving a new widget from the 'lazy' widget, the :type
3477 parameter is allowed to refer to the widget currently being defined,
3478 thus allowing recursive datastructures to be described.
3480 The :type parameter takes the same arguments as the defcustom
3481 parameter with the same name.
3483 Most composite widgets, i.e. widgets containing other widgets, does
3484 not allow recursion. That is, when you define a new widget type, none
3485 of the inferior widgets may be of the same type you are currently
3486 defining.
3488 In Lisp, however, it is custom to define datastructures in terms of
3489 themselves. A list, for example, is defined as either nil, or a cons
3490 cell whose cdr itself is a list. The obvious way to translate this
3491 into a widget type would be
3493 (define-widget 'my-list 'choice
3494 \"A list of sexps.\"
3495 :tag \"Sexp list\"
3496 :args '((const nil) (cons :value (nil) sexp my-list)))
3498 Here we attempt to define my-list as a choice of either the constant
3499 nil, or a cons-cell containing a sexp and my-lisp. This will not work
3500 because the `choice' widget does not allow recursion.
3502 Using the `lazy' widget you can overcome this problem, as in this
3503 example:
3505 (define-widget 'sexp-list 'lazy
3506 \"A list of sexps.\"
3507 :tag \"Sexp list\"
3508 :type '(choice (const nil) (cons :value (nil) sexp sexp-list)))"
3509 :format "%{%t%}: %v"
3510 ;; We don't convert :type because we want to allow recursive
3511 ;; datastructures. This is slow, so we should not create speed
3512 ;; critical widgets by deriving from this.
3513 :convert-widget 'widget-value-convert-widget
3514 :value-create 'widget-type-value-create
3515 :value-get 'widget-child-value-get
3516 :value-inline 'widget-child-value-inline
3517 :default-get 'widget-type-default-get
3518 :match 'widget-type-match
3519 :validate 'widget-child-validate)
3522 ;;; The `plist' Widget.
3524 ;; Property lists.
3526 (define-widget 'plist 'list
3527 "A property list."
3528 :key-type '(symbol :tag "Key")
3529 :value-type '(sexp :tag "Value")
3530 :convert-widget 'widget-plist-convert-widget
3531 :tag "Plist")
3533 (defvar widget-plist-value-type) ;Dynamic variable
3535 (defun widget-plist-convert-widget (widget)
3536 ;; Handle `:options'.
3537 (let* ((options (widget-get widget :options))
3538 (widget-plist-value-type (widget-get widget :value-type))
3539 (other `(editable-list :inline t
3540 (group :inline t
3541 ,(widget-get widget :key-type)
3542 ,widget-plist-value-type)))
3543 (args (if options
3544 (list `(checklist :inline t
3545 :greedy t
3546 ,@(mapcar 'widget-plist-convert-option
3547 options))
3548 other)
3549 (list other))))
3550 (widget-put widget :args args)
3551 widget))
3553 (defun widget-plist-convert-option (option)
3554 ;; Convert a single plist option.
3555 (let (key-type value-type)
3556 (if (listp option)
3557 (let ((key (nth 0 option)))
3558 (setq value-type (nth 1 option))
3559 (if (listp key)
3560 (setq key-type key)
3561 (setq key-type `(const ,key))))
3562 (setq key-type `(const ,option)
3563 value-type widget-plist-value-type))
3564 `(group :format "Key: %v" :inline t ,key-type ,value-type)))
3567 ;;; The `alist' Widget.
3569 ;; Association lists.
3571 (define-widget 'alist 'list
3572 "An association list."
3573 :key-type '(sexp :tag "Key")
3574 :value-type '(sexp :tag "Value")
3575 :convert-widget 'widget-alist-convert-widget
3576 :tag "Alist")
3578 (defvar widget-alist-value-type) ;Dynamic variable
3580 (defun widget-alist-convert-widget (widget)
3581 ;; Handle `:options'.
3582 (let* ((options (widget-get widget :options))
3583 (widget-alist-value-type (widget-get widget :value-type))
3584 (other `(editable-list :inline t
3585 (cons :format "%v"
3586 ,(widget-get widget :key-type)
3587 ,widget-alist-value-type)))
3588 (args (if options
3589 (list `(checklist :inline t
3590 :greedy t
3591 ,@(mapcar 'widget-alist-convert-option
3592 options))
3593 other)
3594 (list other))))
3595 (widget-put widget :args args)
3596 widget))
3598 (defun widget-alist-convert-option (option)
3599 ;; Convert a single alist option.
3600 (let (key-type value-type)
3601 (if (listp option)
3602 (let ((key (nth 0 option)))
3603 (setq value-type (nth 1 option))
3604 (if (listp key)
3605 (setq key-type key)
3606 (setq key-type `(const ,key))))
3607 (setq key-type `(const ,option)
3608 value-type widget-alist-value-type))
3609 `(cons :format "Key: %v" ,key-type ,value-type)))
3611 (define-widget 'choice 'menu-choice
3612 "A union of several sexp types."
3613 :tag "Choice"
3614 :format "%{%t%}: %[Value Menu%] %v"
3615 :button-prefix 'widget-push-button-prefix
3616 :button-suffix 'widget-push-button-suffix
3617 :prompt-value 'widget-choice-prompt-value)
3619 (defun widget-choice-prompt-value (widget prompt value unbound)
3620 "Make a choice."
3621 (let ((args (widget-get widget :args))
3622 (completion-ignore-case (widget-get widget :case-fold))
3623 current choices old)
3624 ;; Find the first arg that matches VALUE.
3625 (let ((look args))
3626 (while look
3627 (if (widget-apply (car look) :match value)
3628 (setq old (car look)
3629 look nil)
3630 (setq look (cdr look)))))
3631 ;; Find new choice.
3632 (setq current
3633 (cond ((= (length args) 0)
3634 nil)
3635 ((= (length args) 1)
3636 (nth 0 args))
3637 ((and (= (length args) 2)
3638 (memq old args))
3639 (if (eq old (nth 0 args))
3640 (nth 1 args)
3641 (nth 0 args)))
3643 (while args
3644 (setq current (car args)
3645 args (cdr args))
3646 (setq choices
3647 (cons (cons (widget-apply current :menu-tag-get)
3648 current)
3649 choices)))
3650 (let ((val (completing-read prompt choices nil t)))
3651 (if (stringp val)
3652 (let ((try (try-completion val choices)))
3653 (when (stringp try)
3654 (setq val try))
3655 (cdr (assoc val choices)))
3656 nil)))))
3657 (if current
3658 (widget-prompt-value current prompt nil t)
3659 value)))
3661 (define-widget 'radio 'radio-button-choice
3662 "A union of several sexp types."
3663 :tag "Choice"
3664 :format "%{%t%}:\n%v"
3665 :prompt-value 'widget-choice-prompt-value)
3667 (define-widget 'repeat 'editable-list
3668 "A variable length homogeneous list."
3669 :tag "Repeat"
3670 :format "%{%t%}:\n%v%i\n")
3672 (define-widget 'set 'checklist
3673 "A list of members from a fixed set."
3674 :tag "Set"
3675 :format "%{%t%}:\n%v")
3677 (define-widget 'boolean 'toggle
3678 "To be nil or non-nil, that is the question."
3679 :tag "Boolean"
3680 :prompt-value 'widget-boolean-prompt-value
3681 :button-prefix 'widget-push-button-prefix
3682 :button-suffix 'widget-push-button-suffix
3683 :format "%{%t%}: %[Toggle%] %v\n"
3684 :on "on (non-nil)"
3685 :off "off (nil)")
3687 (defun widget-boolean-prompt-value (widget prompt value unbound)
3688 ;; Toggle a boolean.
3689 (y-or-n-p prompt))
3691 ;;; The `color' Widget.
3693 ;; Fixme: match
3694 (define-widget 'color 'editable-field
3695 "Choose a color name (with sample)."
3696 :format "%{%t%}: %v (%{sample%})\n"
3697 :size 10
3698 :tag "Color"
3699 :value "black"
3700 :complete 'widget-color-complete
3701 :sample-face-get 'widget-color-sample-face-get
3702 :notify 'widget-color-notify
3703 :action 'widget-color-action)
3705 (defun widget-color-complete (widget)
3706 "Complete the color in WIDGET."
3707 (require 'facemenu) ; for facemenu-color-alist
3708 (completion-in-region (widget-field-start widget)
3709 (max (point) (widget-field-text-end widget))
3710 (or facemenu-color-alist
3711 (sort (defined-colors) 'string-lessp))))
3713 (defun widget-color-sample-face-get (widget)
3714 (let* ((value (condition-case nil
3715 (widget-value widget)
3716 (error (widget-get widget :value)))))
3717 (if (color-defined-p value)
3718 (list (cons 'foreground-color value))
3719 'default)))
3721 (defun widget-color-action (widget &optional event)
3722 "Prompt for a color."
3723 (let* ((tag (widget-apply widget :menu-tag-get))
3724 (prompt (concat tag ": "))
3725 (value (widget-value widget))
3726 (start (widget-field-start widget))
3727 (answer (facemenu-read-color prompt)))
3728 (unless (zerop (length answer))
3729 (widget-value-set widget answer)
3730 (widget-setup)
3731 (widget-apply widget :notify widget event))))
3733 (defun widget-color-notify (widget child &optional event)
3734 "Update the sample, and notify the parent."
3735 (overlay-put (widget-get widget :sample-overlay)
3736 'face (widget-apply widget :sample-face-get))
3737 (widget-default-notify widget child event))
3739 ;;; The Help Echo
3741 (defun widget-echo-help (pos)
3742 "Display help-echo text for widget at POS."
3743 (let* ((widget (widget-at pos))
3744 (help-echo (and widget (widget-get widget :help-echo))))
3745 (if (functionp help-echo)
3746 (setq help-echo (funcall help-echo widget)))
3747 (if help-echo (message "%s" (eval help-echo)))))
3749 ;;; The End:
3751 (provide 'wid-edit)
3753 ;;; wid-edit.el ends here