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