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