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