Start redesigning portable mouse highlight. Not compiled.
[emacs.git] / lisp / wid-edit.el
blobe905e8f2a87fa68b0ffdbd4b349d0add9be2101c
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
9 ;; Package: emacs
11 ;; This file is part of GNU Emacs.
13 ;; GNU Emacs is free software: you can redistribute it and/or modify
14 ;; it under the terms of the GNU General Public License as published by
15 ;; the Free Software Foundation, either version 3 of the License, or
16 ;; (at your option) any later version.
18 ;; GNU Emacs is distributed in the hope that it will be useful,
19 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
20 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 ;; GNU General Public License for more details.
23 ;; You should have received a copy of the GNU General Public License
24 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
26 ;;; Wishlist items (from widget.texi):
28 ;; * The `menu-choice' tag should be prettier, something like the
29 ;; abbreviated menus in Open Look.
31 ;; * Finish `:tab-order'.
33 ;; * Make indentation work with glyphs and proportional fonts.
35 ;; * Add commands to show overview of object and class hierarchies to
36 ;; the browser.
38 ;; * Find a way to disable mouse highlight for inactive widgets.
40 ;; * Find a way to make glyphs look inactive.
42 ;; * Add `key-binding' widget.
44 ;; * Add `widget' widget for editing widget specifications.
46 ;; * Find clean way to implement variable length list. See
47 ;; `TeX-printer-list' for an explanation.
49 ;; * `C-h' in `widget-prompt-value' should give type specific help.
51 ;; * A mailto widget. [This should work OK as a url-link if with
52 ;; browse-url-browser-function' set up appropriately.]
54 ;;; Commentary:
56 ;; See `widget.el'.
58 ;;; Code:
60 ;;; Compatibility.
62 (defun widget-event-point (event)
63 "Character position of the end of event if that exists, or nil."
64 (posn-point (event-end event)))
66 (defun widget-button-release-event-p (event)
67 "Non-nil if EVENT is a mouse-button-release event object."
68 (and (eventp event)
69 (memq (event-basic-type event) '(mouse-1 mouse-2 mouse-3))
70 (or (memq 'click (event-modifiers event))
71 (memq 'drag (event-modifiers event)))))
73 ;;; Customization.
75 (defgroup widgets nil
76 "Customization support for the Widget Library."
77 :link '(custom-manual "(widget)Top")
78 :link '(emacs-library-link :tag "Lisp File" "widget.el")
79 :prefix "widget-"
80 :group 'extensions)
82 (defgroup widget-documentation nil
83 "Options controlling the display of documentation strings."
84 :group 'widgets)
86 (defgroup widget-faces nil
87 "Faces used by the widget library."
88 :group 'widgets
89 :group 'faces)
91 (defvar widget-documentation-face 'widget-documentation
92 "Face used for documentation strings in widgets.
93 This exists as a variable so it can be set locally in certain buffers.")
95 (defface widget-documentation '((((class color)
96 (background dark))
97 (:foreground "lime green"))
98 (((class color)
99 (background light))
100 (:foreground "dark green"))
101 (t nil))
102 "Face used for documentation text."
103 :group 'widget-documentation
104 :group 'widget-faces)
105 (define-obsolete-face-alias 'widget-documentation-face
106 'widget-documentation "22.1")
108 (defvar widget-button-face 'widget-button
109 "Face used for buttons in widgets.
110 This exists as a variable so it can be set locally in certain buffers.")
112 (defface widget-button '((t (:weight bold)))
113 "Face used for widget buttons."
114 :group 'widget-faces)
115 (define-obsolete-face-alias 'widget-button-face 'widget-button "22.1")
117 (defcustom widget-mouse-face 'highlight
118 "Face used for widget buttons when the mouse is above them."
119 :type 'face
120 :group 'widget-faces)
122 ;; TTY gets special definitions here and in the next defface, because
123 ;; the gray colors defined for other displays cause black text on a black
124 ;; background, at least on light-background TTYs.
125 (defface widget-field '((((type tty))
126 :background "yellow3"
127 :foreground "black")
128 (((class grayscale color)
129 (background light))
130 :background "gray85")
131 (((class grayscale color)
132 (background dark))
133 :background "dim gray")
135 :slant italic))
136 "Face used for editable fields."
137 :group 'widget-faces)
138 (define-obsolete-face-alias 'widget-field-face 'widget-field "22.1")
140 (defface widget-single-line-field '((((type tty))
141 :background "green3"
142 :foreground "black")
143 (((class grayscale color)
144 (background light))
145 :background "gray85")
146 (((class grayscale color)
147 (background dark))
148 :background "dim gray")
150 :slant italic))
151 "Face used for editable fields spanning only a single line."
152 :group 'widget-faces)
153 (define-obsolete-face-alias 'widget-single-line-field-face
154 'widget-single-line-field "22.1")
156 ;;; This causes display-table to be loaded, and not usefully.
157 ;;;(defvar widget-single-line-display-table
158 ;;; (let ((table (make-display-table)))
159 ;;; (aset table 9 "^I")
160 ;;; (aset table 10 "^J")
161 ;;; table)
162 ;;; "Display table used for single-line editable fields.")
164 ;;;(when (fboundp 'set-face-display-table)
165 ;;; (set-face-display-table 'widget-single-line-field-face
166 ;;; widget-single-line-display-table))
168 ;;; Utility functions.
170 ;; These are not really widget specific.
172 (defun widget-princ-to-string (object)
173 "Return string representation of OBJECT, any Lisp object.
174 No quoting characters are used; no delimiters are printed around
175 the contents of strings."
176 (with-output-to-string
177 (princ object)))
179 (defun widget-clear-undo ()
180 "Clear all undo information."
181 (buffer-disable-undo (current-buffer))
182 (buffer-enable-undo))
184 (defcustom widget-menu-max-size 40
185 "Largest number of items allowed in a popup-menu.
186 Larger menus are read through the minibuffer."
187 :group 'widgets
188 :type 'integer)
190 (defcustom widget-menu-max-shortcuts 40
191 "Largest number of items for which it works to choose one with a character.
192 For a larger number of items, the minibuffer is used."
193 :group 'widgets
194 :type 'integer)
196 (defcustom widget-menu-minibuffer-flag nil
197 "Control how to ask for a choice from the keyboard.
198 Non-nil means use the minibuffer;
199 nil means read a single character."
200 :group 'widgets
201 :type 'boolean)
203 (defun widget-choose (title items &optional event)
204 "Choose an item from a list.
206 First argument TITLE is the name of the list.
207 Second argument ITEMS is a list whose members are either
208 (NAME . VALUE), to indicate selectable items, or just strings to
209 indicate unselectable items.
210 Optional third argument EVENT is an input event.
212 The user is asked to choose between each NAME from the items alist,
213 and the VALUE of the chosen element will be returned. If EVENT is a
214 mouse event, and the number of elements in items is less than
215 `widget-menu-max-size', a popup menu will be used, otherwise the
216 minibuffer."
217 (cond ((and (< (length items) widget-menu-max-size)
218 event (display-popup-menus-p))
219 ;; Mouse click.
220 (x-popup-menu event
221 (list title (cons "" items))))
222 ((or widget-menu-minibuffer-flag
223 (> (length items) widget-menu-max-shortcuts))
224 ;; Read the choice of name from the minibuffer.
225 (setq items (widget-remove-if 'stringp items))
226 (let ((val (completing-read (concat title ": ") items nil t)))
227 (if (stringp val)
228 (let ((try (try-completion val items)))
229 (when (stringp try)
230 (setq val try))
231 (cdr (assoc val items))))))
233 ;; Construct a menu of the choices
234 ;; and then use it for prompting for a single character.
235 (let* ((overriding-terminal-local-map (make-sparse-keymap))
236 (next-digit ?0)
237 map choice some-choice-enabled value)
238 ;; Define SPC as a prefix char to get to this menu.
239 (define-key overriding-terminal-local-map " "
240 (setq map (make-sparse-keymap title)))
241 (with-current-buffer (get-buffer-create " widget-choose")
242 (erase-buffer)
243 (insert "Available choices:\n\n")
244 (while items
245 (setq choice (car items) items (cdr items))
246 (if (consp choice)
247 (let* ((name (car choice))
248 (function (cdr choice)))
249 (insert (format "%c = %s\n" next-digit name))
250 (define-key map (vector next-digit) function)
251 (setq some-choice-enabled t)))
252 ;; Allocate digits to disabled alternatives
253 ;; so that the digit of a given alternative never varies.
254 (setq next-digit (1+ next-digit)))
255 (insert "\nC-g = Quit")
256 (goto-char (point-min))
257 (forward-line))
258 (or some-choice-enabled
259 (error "None of the choices is currently meaningful"))
260 (define-key map [?\C-g] 'keyboard-quit)
261 (define-key map [t] 'keyboard-quit)
262 (define-key map [?\M-\C-v] 'scroll-other-window)
263 (define-key map [?\M--] 'negative-argument)
264 (setcdr map (nreverse (cdr map)))
265 ;; Read a char with the menu, and return the result
266 ;; that corresponds to it.
267 (save-window-excursion
268 (let ((buf (get-buffer " widget-choose")))
269 (fit-window-to-buffer (display-buffer buf))
270 (let ((cursor-in-echo-area t)
271 keys
272 (char 0)
273 (arg 1))
274 (while (not (or (and (integerp char)
275 (>= char ?0) (< char next-digit))
276 (eq value 'keyboard-quit)))
277 ;; Unread a SPC to lead to our new menu.
278 (setq unread-command-events (cons ?\s unread-command-events))
279 (setq keys (read-key-sequence title))
280 (setq value
281 (lookup-key overriding-terminal-local-map keys t)
282 char (aref keys 1))
283 (cond ((eq value 'scroll-other-window)
284 (let ((minibuffer-scroll-window
285 (get-buffer-window buf)))
286 (if (> 0 arg)
287 (scroll-other-window-down
288 (window-height minibuffer-scroll-window))
289 (scroll-other-window))
290 (setq arg 1)))
291 ((eq value 'negative-argument)
292 (setq arg -1))
294 (setq arg 1)))))))
295 (when (eq value 'keyboard-quit)
296 (error "Canceled"))
297 value))))
299 (defun widget-remove-if (predictate list)
300 (let (result (tail list))
301 (while tail
302 (or (funcall predictate (car tail))
303 (setq result (cons (car tail) result)))
304 (setq tail (cdr tail)))
305 (nreverse result)))
307 ;;; Widget text specifications.
309 ;; These functions are for specifying text properties.
311 ;; We can set it to nil now that get_local_map uses get_pos_property.
312 (defconst widget-field-add-space nil
313 "Non-nil means add extra space at the end of editable text fields.
314 If you don't add the space, it will become impossible to edit a zero
315 size field.")
317 (defvar widget-field-use-before-change t
318 "Non-nil means use `before-change-functions' to track editable fields.
319 This enables the use of undo, but doesn't work on Emacs 19.34 and earlier.
320 Using before hooks also means that the :notify function can't know the
321 new value.")
323 (defun widget-specify-field (widget from to)
324 "Specify editable button for WIDGET between FROM and TO."
325 ;; Terminating space is not part of the field, but necessary in
326 ;; order for local-map to work. Remove next sexp if local-map works
327 ;; at the end of the overlay.
328 (save-excursion
329 (goto-char to)
330 (cond ((null (widget-get widget :size))
331 (forward-char 1))
332 (widget-field-add-space
333 (insert-and-inherit " ")))
334 (setq to (point)))
335 (let ((keymap (widget-get widget :keymap))
336 (face (or (widget-get widget :value-face) 'widget-field))
337 (help-echo (widget-get widget :help-echo))
338 (follow-link (widget-get widget :follow-link))
339 (rear-sticky
340 (or (not widget-field-add-space) (widget-get widget :size))))
341 (if (functionp help-echo)
342 (setq help-echo 'widget-mouse-help))
343 (when (= (char-before to) ?\n)
344 ;; When the last character in the field is a newline, we want to
345 ;; give it a `field' char-property of `boundary', which helps the
346 ;; C-n/C-p act more naturally when entering/leaving the field. We
347 ;; do this by making a small secondary overlay to contain just that
348 ;; one character.
349 (let ((overlay (make-overlay (1- to) to nil t nil)))
350 (overlay-put overlay 'field 'boundary)
351 ;; We need the real field for tabbing.
352 (overlay-put overlay 'real-field widget)
353 ;; Use `local-map' here, not `keymap', so that normal editing
354 ;; works in the field when, say, Custom uses `suppress-keymap'.
355 (overlay-put overlay 'local-map keymap)
356 (overlay-put overlay 'face face)
357 (overlay-put overlay 'follow-link follow-link)
358 (overlay-put overlay 'help-echo help-echo))
359 (setq to (1- to))
360 (setq rear-sticky t))
361 (let ((overlay (make-overlay from to nil nil rear-sticky)))
362 (widget-put widget :field-overlay overlay)
363 ;;(overlay-put overlay 'detachable nil)
364 (overlay-put overlay 'field widget)
365 (overlay-put overlay 'local-map keymap)
366 (overlay-put overlay 'face face)
367 (overlay-put overlay 'follow-link follow-link)
368 (overlay-put overlay 'help-echo help-echo)))
369 (widget-specify-secret widget))
371 (defun widget-specify-secret (field)
372 "Replace text in FIELD with value of `:secret', if non-nil."
373 (let ((secret (widget-get field :secret))
374 (size (widget-get field :size)))
375 (when secret
376 (let ((begin (widget-field-start field))
377 (end (widget-field-end field)))
378 (when size
379 (while (and (> end begin)
380 (eq (char-after (1- end)) ?\s))
381 (setq end (1- end))))
382 (while (< begin end)
383 (let ((old (char-after begin)))
384 (unless (eq old secret)
385 (subst-char-in-region begin (1+ begin) old secret)
386 (put-text-property begin (1+ begin) 'secret old))
387 (setq begin (1+ begin))))))))
389 (defun widget-specify-button (widget from to)
390 "Specify button for WIDGET between FROM and TO."
391 (let ((overlay (make-overlay from to nil t nil))
392 (follow-link (widget-get widget :follow-link))
393 (help-echo (widget-get widget :help-echo)))
394 (widget-put widget :button-overlay overlay)
395 (if (functionp help-echo)
396 (setq help-echo 'widget-mouse-help))
397 (overlay-put overlay 'button widget)
398 (overlay-put overlay 'keymap (widget-get widget :keymap))
399 (overlay-put overlay 'evaporate t)
400 ;; We want to avoid the face with image buttons.
401 (unless (widget-get widget :suppress-face)
402 (overlay-put overlay 'face (widget-apply widget :button-face-get))
403 (overlay-put overlay 'mouse-face
404 ;; Make new list structure for the mouse-face value
405 ;; so that different widgets will have
406 ;; different `mouse-face' property values
407 ;; and will highlight separately.
408 (let ((mouse-face-value
409 (widget-apply widget :mouse-face-get)))
410 ;; If it's a list, copy it.
411 (if (listp mouse-face-value)
412 (copy-sequence mouse-face-value)
413 ;; If it's a symbol, put it in a list.
414 (list mouse-face-value)))))
415 (overlay-put overlay 'pointer 'hand)
416 (overlay-put overlay 'follow-link follow-link)
417 (overlay-put overlay 'help-echo help-echo)))
419 (defun widget-mouse-help (window overlay point)
420 "Help-echo callback for widgets whose :help-echo is a function."
421 (with-current-buffer (overlay-buffer overlay)
422 (let* ((widget (widget-at (overlay-start overlay)))
423 (help-echo (if widget (widget-get widget :help-echo))))
424 (if (functionp help-echo)
425 (funcall help-echo widget)
426 help-echo))))
428 (defun widget-specify-sample (widget from to)
429 "Specify sample for WIDGET between FROM and TO."
430 (let ((overlay (make-overlay from to nil t nil)))
431 (overlay-put overlay 'face (widget-apply widget :sample-face-get))
432 (overlay-put overlay 'evaporate t)
433 (widget-put widget :sample-overlay overlay)))
435 (defun widget-specify-doc (widget from to)
436 "Specify documentation for WIDGET between FROM and TO."
437 (let ((overlay (make-overlay from to nil t nil)))
438 (overlay-put overlay 'widget-doc widget)
439 (overlay-put overlay 'face widget-documentation-face)
440 (overlay-put overlay 'evaporate t)
441 (widget-put widget :doc-overlay overlay)))
443 (defmacro widget-specify-insert (&rest form)
444 "Execute FORM without inheriting any text properties."
445 `(save-restriction
446 (let ((inhibit-read-only t)
447 (inhibit-modification-hooks t))
448 (narrow-to-region (point) (point))
449 (prog1 (progn ,@form)
450 (goto-char (point-max))))))
452 (defface widget-inactive
453 '((t :inherit shadow))
454 "Face used for inactive widgets."
455 :group 'widget-faces)
456 (define-obsolete-face-alias 'widget-inactive-face
457 'widget-inactive "22.1")
459 (defun widget-specify-inactive (widget from to)
460 "Make WIDGET inactive for user modifications."
461 (unless (widget-get widget :inactive)
462 (let ((overlay (make-overlay from to nil t nil)))
463 (overlay-put overlay 'face 'widget-inactive)
464 ;; This is disabled, as it makes the mouse cursor change shape.
465 ;; (overlay-put overlay 'mouse-face 'widget-inactive)
466 (overlay-put overlay 'evaporate t)
467 (overlay-put overlay 'priority 100)
468 (overlay-put overlay 'modification-hooks '(widget-overlay-inactive))
469 (widget-put widget :inactive overlay))))
471 (defun widget-overlay-inactive (&rest junk)
472 "Ignoring the arguments, signal an error."
473 (unless inhibit-read-only
474 (error "The widget here is not active")))
477 (defun widget-specify-active (widget)
478 "Make WIDGET active for user modifications."
479 (let ((inactive (widget-get widget :inactive)))
480 (when inactive
481 (delete-overlay inactive)
482 (widget-put widget :inactive nil))))
484 ;;; Widget Properties.
486 (defsubst widget-type (widget)
487 "Return the type of WIDGET. The type is a symbol."
488 (car widget))
490 ;;;###autoload
491 (defun widgetp (widget)
492 "Return non-nil if WIDGET is a widget."
493 (if (symbolp widget)
494 (get widget 'widget-type)
495 (and (consp widget)
496 (symbolp (car widget))
497 (get (car widget) 'widget-type))))
499 (defun widget-get-indirect (widget property)
500 "In WIDGET, get the value of PROPERTY.
501 If the value is a symbol, return its binding.
502 Otherwise, just return the value."
503 (let ((value (widget-get widget property)))
504 (if (symbolp value)
505 (symbol-value value)
506 value)))
508 (defun widget-member (widget property)
509 "Non-nil if there is a definition in WIDGET for PROPERTY."
510 (cond ((plist-member (cdr widget) property)
512 ((car widget)
513 (widget-member (get (car widget) 'widget-type) property))
514 (t nil)))
516 (defun widget-value (widget)
517 "Extract the current value of WIDGET."
518 (widget-apply widget
519 :value-to-external (widget-apply widget :value-get)))
521 (defun widget-value-set (widget value)
522 "Set the current value of WIDGET to VALUE."
523 (widget-apply widget
524 :value-set (widget-apply widget
525 :value-to-internal value)))
527 (defun widget-default-get (widget)
528 "Extract the default external value of WIDGET."
529 (widget-apply widget :value-to-external
530 (or (widget-get widget :value)
531 (widget-apply widget :default-get))))
533 (defun widget-match-inline (widget vals)
534 "In WIDGET, match the start of VALS."
535 (cond ((widget-get widget :inline)
536 (widget-apply widget :match-inline vals))
537 ((and (listp vals)
538 (widget-apply widget :match (car vals)))
539 (cons (list (car vals)) (cdr vals)))
540 (t nil)))
542 (defun widget-apply-action (widget &optional event)
543 "Apply :action in WIDGET in response to EVENT."
544 (if (widget-apply widget :active)
545 (widget-apply widget :action event)
546 (error "Attempt to perform action on inactive widget")))
548 ;;; Helper functions.
550 ;; These are widget specific.
552 ;;;###autoload
553 (defun widget-prompt-value (widget prompt &optional value unbound)
554 "Prompt for a value matching WIDGET, using PROMPT.
555 The current value is assumed to be VALUE, unless UNBOUND is non-nil."
556 (unless (listp widget)
557 (setq widget (list widget)))
558 (setq prompt (format "[%s] %s" (widget-type widget) prompt))
559 (setq widget (widget-convert widget))
560 (let ((answer (widget-apply widget :prompt-value prompt value unbound)))
561 (unless (widget-apply widget :match answer)
562 (error "Value does not match %S type" (car widget)))
563 answer))
565 (defun widget-get-sibling (widget)
566 "Get the item WIDGET is assumed to toggle.
567 This is only meaningful for radio buttons or checkboxes in a list."
568 (let* ((children (widget-get (widget-get widget :parent) :children))
569 child)
570 (catch 'child
571 (while children
572 (setq child (car children)
573 children (cdr children))
574 (when (eq (widget-get child :button) widget)
575 (throw 'child child)))
576 nil)))
578 (defun widget-map-buttons (function &optional buffer maparg)
579 "Map FUNCTION over the buttons in BUFFER.
580 FUNCTION is called with the arguments WIDGET and MAPARG.
582 If FUNCTION returns non-nil, the walk is cancelled.
584 The arguments MAPARG, and BUFFER default to nil and (current-buffer),
585 respectively."
586 (let ((cur (point-min))
587 (widget nil)
588 (overlays (if buffer
589 (with-current-buffer buffer (overlay-lists))
590 (overlay-lists))))
591 (setq overlays (append (car overlays) (cdr overlays)))
592 (while (setq cur (pop overlays))
593 (setq widget (overlay-get cur 'button))
594 (if (and widget (funcall function widget maparg))
595 (setq overlays nil)))))
597 ;;; Images.
599 (defcustom widget-image-directory (file-name-as-directory
600 (expand-file-name "images/custom" data-directory))
601 "Where widget button images are located.
602 If this variable is nil, widget will try to locate the directory
603 automatically."
604 :group 'widgets
605 :type 'directory)
607 (defcustom widget-image-enable t
608 "If non-nil, use image buttons in widgets when available."
609 :version "21.1"
610 :group 'widgets
611 :type 'boolean)
613 (defcustom widget-image-conversion
614 '((xpm ".xpm") (gif ".gif") (png ".png") (jpeg ".jpg" ".jpeg")
615 (xbm ".xbm"))
616 "Conversion alist from image formats to file name suffixes."
617 :group 'widgets
618 :type '(repeat (cons :format "%v"
619 (symbol :tag "Image Format" unknown)
620 (repeat :tag "Suffixes"
621 (string :format "%v")))))
623 (defun widget-image-find (image)
624 "Create a graphical button from IMAGE.
625 IMAGE should either already be an image, or be a file name sans
626 extension (xpm, xbm, gif, jpg, or png) located in
627 `widget-image-directory' or otherwise where `find-image' will find it."
628 (cond ((not (and image widget-image-enable (display-graphic-p)))
629 ;; We don't want or can't use images.
630 nil)
631 ((and (consp image)
632 (eq 'image (car image)))
633 ;; Already an image spec. Use it.
634 image)
635 ((stringp image)
636 ;; A string. Look it up in relevant directories.
637 (let* ((load-path (cons widget-image-directory load-path))
638 specs)
639 (dolist (elt widget-image-conversion)
640 (dolist (ext (cdr elt))
641 (push (list :type (car elt) :file (concat image ext)
642 :ascent 'center) 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 ;; This used to say:
1160 ;; "When not inside a field, move to the previous button or field."
1161 ;; but AFAICS, it has always just thrown an error.
1162 (defun widget-complete ()
1163 "Complete content of editable field from point.
1164 When not inside a field, signal an error."
1165 (interactive)
1166 (let ((field (widget-field-find (point))))
1167 (if field
1168 (widget-apply field :complete)
1169 (error "Not in an editable field"))))
1171 ;;; Setting up the buffer.
1173 (defvar widget-field-new nil
1174 "List of all newly created editable fields in the buffer.")
1175 (make-variable-buffer-local 'widget-field-new)
1177 (defvar widget-field-list nil
1178 "List of all editable fields in the buffer.")
1179 (make-variable-buffer-local 'widget-field-list)
1181 (defun widget-at (&optional pos)
1182 "The button or field at POS (default, point)."
1183 (or (get-char-property (or pos (point)) 'button)
1184 (widget-field-at pos)))
1186 ;;;###autoload
1187 (defun widget-setup ()
1188 "Setup current buffer so editing string widgets works."
1189 (let ((inhibit-read-only t)
1190 (inhibit-modification-hooks t)
1191 field)
1192 (while widget-field-new
1193 (setq field (car widget-field-new)
1194 widget-field-new (cdr widget-field-new)
1195 widget-field-list (cons field widget-field-list))
1196 (let ((from (car (widget-get field :field-overlay)))
1197 (to (cdr (widget-get field :field-overlay))))
1198 (widget-specify-field field
1199 (marker-position from) (marker-position to))
1200 (set-marker from nil)
1201 (set-marker to nil))))
1202 (widget-clear-undo)
1203 (widget-add-change))
1205 (defvar widget-field-last nil)
1206 ;; Last field containing point.
1207 (make-variable-buffer-local 'widget-field-last)
1209 (defvar widget-field-was nil)
1210 ;; The widget data before the change.
1211 (make-variable-buffer-local 'widget-field-was)
1213 (defun widget-field-at (pos)
1214 "Return the widget field at POS, or nil if none."
1215 (let ((field (get-char-property (or pos (point)) 'field)))
1216 (if (eq field 'boundary)
1217 (get-char-property (or pos (point)) 'real-field)
1218 field)))
1220 (defun widget-field-buffer (widget)
1221 "Return the buffer of WIDGET's editing field."
1222 (let ((overlay (widget-get widget :field-overlay)))
1223 (cond ((overlayp overlay)
1224 (overlay-buffer overlay))
1225 ((consp overlay)
1226 (marker-buffer (car overlay))))))
1228 (defun widget-field-start (widget)
1229 "Return the start of WIDGET's editing field."
1230 (let ((overlay (widget-get widget :field-overlay)))
1231 (if (overlayp overlay)
1232 (overlay-start overlay)
1233 (car overlay))))
1235 (defun widget-field-end (widget)
1236 "Return the end of WIDGET's editing field."
1237 (let ((overlay (widget-get widget :field-overlay)))
1238 ;; Don't subtract one if local-map works at the end of the overlay,
1239 ;; or if a special `boundary' field has been added after the widget
1240 ;; field.
1241 (if (overlayp overlay)
1242 ;; Don't proceed if overlay has been removed from buffer.
1243 (when (overlay-buffer overlay)
1244 (if (and (not (eq (with-current-buffer
1245 (widget-field-buffer widget)
1246 (save-restriction
1247 ;; `widget-narrow-to-field' can be
1248 ;; active when this function is called
1249 ;; from an change-functions hook. So
1250 ;; temporarily remove field narrowing
1251 ;; before to call `get-char-property'.
1252 (widen)
1253 (get-char-property (overlay-end overlay)
1254 'field)))
1255 'boundary))
1256 (or widget-field-add-space
1257 (null (widget-get widget :size))))
1258 (1- (overlay-end overlay))
1259 (overlay-end overlay)))
1260 (cdr overlay))))
1262 (defun widget-field-text-end (widget)
1263 (let ((to (widget-field-end widget))
1264 (size (widget-get widget :size)))
1265 (if (or (null size) (zerop size))
1267 (let ((from (widget-field-start widget)))
1268 (if (and from to)
1269 (with-current-buffer (widget-field-buffer widget)
1270 (while (and (> to from)
1271 (eq (char-after (1- to)) ?\s))
1272 (setq to (1- to)))
1273 to))))))
1275 (defun widget-field-find (pos)
1276 "Return the field at POS.
1277 Unlike (get-char-property POS 'field), this works with empty fields too."
1278 (let ((fields widget-field-list)
1279 field found)
1280 (while fields
1281 (setq field (car fields)
1282 fields (cdr fields))
1283 (when (and (<= (widget-field-start field) pos)
1284 (<= pos (widget-field-end field)))
1285 (when found
1286 (error "Overlapping fields"))
1287 (setq found field)))
1288 found))
1290 (defun widget-before-change (from to)
1291 ;; This is how, for example, a variable changes its state to `modified'.
1292 ;; when it is being edited.
1293 (unless inhibit-read-only
1294 (let ((from-field (widget-field-find from))
1295 (to-field (widget-field-find to)))
1296 (cond ((not (eq from-field to-field))
1297 (add-hook 'post-command-hook 'widget-add-change nil t)
1298 (signal 'text-read-only
1299 '("Change should be restricted to a single field")))
1300 ((null from-field)
1301 (add-hook 'post-command-hook 'widget-add-change nil t)
1302 (signal 'text-read-only
1303 '("Attempt to change text outside editable field")))
1304 (widget-field-use-before-change
1305 (widget-apply from-field :notify from-field))))))
1307 (defun widget-add-change ()
1308 (remove-hook 'post-command-hook 'widget-add-change t)
1309 (add-hook 'before-change-functions 'widget-before-change nil t)
1310 (add-hook 'after-change-functions 'widget-after-change nil t))
1312 (defun widget-after-change (from to old)
1313 "Adjust field size and text properties."
1314 (let ((field (widget-field-find from))
1315 (other (widget-field-find to)))
1316 (when field
1317 (unless (eq field other)
1318 (error "Change in different fields"))
1319 (let ((size (widget-get field :size)))
1320 (when size
1321 (let ((begin (widget-field-start field))
1322 (end (widget-field-end field)))
1323 (cond ((< (- end begin) size)
1324 ;; Field too small.
1325 (save-excursion
1326 (goto-char end)
1327 (insert-char ?\s (- (+ begin size) end))))
1328 ((> (- end begin) size)
1329 ;; Field too large and
1330 (if (or (< (point) (+ begin size))
1331 (> (point) end))
1332 ;; Point is outside extra space.
1333 (setq begin (+ begin size))
1334 ;; Point is within the extra space.
1335 (setq begin (point)))
1336 (save-excursion
1337 (goto-char end)
1338 (while (and (eq (preceding-char) ?\s)
1339 (> (point) begin))
1340 (delete-char -1)))))))
1341 (widget-specify-secret field))
1342 (widget-apply field :notify field))))
1344 ;;; Widget Functions
1346 ;; These functions are used in the definition of multiple widgets.
1348 (defun widget-parent-action (widget &optional event)
1349 "Tell :parent of WIDGET to handle the :action.
1350 Optional EVENT is the event that triggered the action."
1351 (widget-apply (widget-get widget :parent) :action event))
1353 (defun widget-children-value-delete (widget)
1354 "Delete all :children and :buttons in WIDGET."
1355 (mapc 'widget-delete (widget-get widget :children))
1356 (widget-put widget :children nil)
1357 (mapc 'widget-delete (widget-get widget :buttons))
1358 (widget-put widget :buttons nil))
1360 (defun widget-children-validate (widget)
1361 "All the :children must be valid."
1362 (let ((children (widget-get widget :children))
1363 child found)
1364 (while (and children (not found))
1365 (setq child (car children)
1366 children (cdr children)
1367 found (widget-apply child :validate)))
1368 found))
1370 (defun widget-child-value-get (widget)
1371 "Get the value of the first member of :children in WIDGET."
1372 (widget-value (car (widget-get widget :children))))
1374 (defun widget-child-value-inline (widget)
1375 "Get the inline value of the first member of :children in WIDGET."
1376 (widget-apply (car (widget-get widget :children)) :value-inline))
1378 (defun widget-child-validate (widget)
1379 "The result of validating the first member of :children in WIDGET."
1380 (widget-apply (car (widget-get widget :children)) :validate))
1382 (defun widget-type-value-create (widget)
1383 "Convert and instantiate the value of the :type attribute of WIDGET.
1384 Store the newly created widget in the :children attribute.
1386 The value of the :type attribute should be an unconverted widget type."
1387 (let ((value (widget-get widget :value))
1388 (type (widget-get widget :type)))
1389 (widget-put widget :children
1390 (list (widget-create-child-value widget
1391 (widget-convert type)
1392 value)))))
1394 (defun widget-type-default-get (widget)
1395 "Get default value from the :type attribute of WIDGET.
1397 The value of the :type attribute should be an unconverted widget type."
1398 (widget-default-get (widget-convert (widget-get widget :type))))
1400 (defun widget-type-match (widget value)
1401 "Non-nil if the :type value of WIDGET matches VALUE.
1403 The value of the :type attribute should be an unconverted widget type."
1404 (widget-apply (widget-convert (widget-get widget :type)) :match value))
1406 (defun widget-types-copy (widget)
1407 "Copy :args as widget types in WIDGET."
1408 (widget-put widget :args (mapcar 'widget-copy (widget-get widget :args)))
1409 widget)
1411 ;; Made defsubst to speed up face editor creation.
1412 (defsubst widget-types-convert-widget (widget)
1413 "Convert :args as widget types in WIDGET."
1414 (widget-put widget :args (mapcar 'widget-convert (widget-get widget :args)))
1415 widget)
1417 (defun widget-value-convert-widget (widget)
1418 "Initialize :value from :args in WIDGET."
1419 (let ((args (widget-get widget :args)))
1420 (when args
1421 (widget-put widget :value (car args))
1422 ;; Don't convert :value here, as this is done in `widget-convert'.
1423 ;; (widget-put widget :value (widget-apply widget
1424 ;; :value-to-internal (car args)))
1425 (widget-put widget :args nil)))
1426 widget)
1428 (defun widget-value-value-get (widget)
1429 "Return the :value property of WIDGET."
1430 (widget-get widget :value))
1432 ;;; The `default' Widget.
1434 (define-widget 'default nil
1435 "Basic widget other widgets are derived from."
1436 :value-to-internal (lambda (widget value) value)
1437 :value-to-external (lambda (widget value) value)
1438 :button-prefix 'widget-button-prefix
1439 :button-suffix 'widget-button-suffix
1440 :complete 'widget-default-complete
1441 :create 'widget-default-create
1442 :indent nil
1443 :offset 0
1444 :format-handler 'widget-default-format-handler
1445 :button-face-get 'widget-default-button-face-get
1446 :mouse-face-get 'widget-default-mouse-face-get
1447 :sample-face-get 'widget-default-sample-face-get
1448 :delete 'widget-default-delete
1449 :copy 'identity
1450 :value-set 'widget-default-value-set
1451 :value-inline 'widget-default-value-inline
1452 :value-delete 'ignore
1453 :default-get 'widget-default-default-get
1454 :menu-tag-get 'widget-default-menu-tag-get
1455 :validate #'ignore
1456 :active 'widget-default-active
1457 :activate 'widget-specify-active
1458 :deactivate 'widget-default-deactivate
1459 :mouse-down-action #'ignore
1460 :action 'widget-default-action
1461 :notify 'widget-default-notify
1462 :prompt-value 'widget-default-prompt-value)
1464 (defvar widget--completing-widget)
1466 (defun widget-default-complete (widget)
1467 "Call the value of the :complete-function property of WIDGET.
1468 If that does not exist, call the value of `widget-complete-field'.
1469 During this call, `widget--completing-widget' is bound to WIDGET."
1470 (let ((widget--completing-widget widget))
1471 (call-interactively (or (widget-get widget :complete-function)
1472 widget-complete-field))))
1474 (defun widget-default-create (widget)
1475 "Create WIDGET at point in the current buffer."
1476 (widget-specify-insert
1477 (let ((from (point))
1478 button-begin button-end
1479 sample-begin sample-end
1480 doc-begin doc-end
1481 value-pos)
1482 (insert (widget-get widget :format))
1483 (goto-char from)
1484 ;; Parse escapes in format.
1485 (while (re-search-forward "%\\(.\\)" nil t)
1486 (let ((escape (char-after (match-beginning 1))))
1487 (delete-char -2)
1488 (cond ((eq escape ?%)
1489 (insert ?%))
1490 ((eq escape ?\[)
1491 (setq button-begin (point))
1492 (insert (widget-get-indirect widget :button-prefix)))
1493 ((eq escape ?\])
1494 (insert (widget-get-indirect widget :button-suffix))
1495 (setq button-end (point)))
1496 ((eq escape ?\{)
1497 (setq sample-begin (point)))
1498 ((eq escape ?\})
1499 (setq sample-end (point)))
1500 ((eq escape ?n)
1501 (when (widget-get widget :indent)
1502 (insert ?\n)
1503 (insert-char ?\s (widget-get widget :indent))))
1504 ((eq escape ?t)
1505 (let ((image (widget-get widget :tag-glyph))
1506 (tag (widget-get widget :tag)))
1507 (cond (image
1508 (widget-image-insert widget (or tag "image") image))
1509 (tag
1510 (insert tag))
1512 (princ (widget-get widget :value)
1513 (current-buffer))))))
1514 ((eq escape ?d)
1515 (let ((doc (widget-get widget :doc)))
1516 (when doc
1517 (setq doc-begin (point))
1518 (insert doc)
1519 (while (eq (preceding-char) ?\n)
1520 (delete-char -1))
1521 (insert ?\n)
1522 (setq doc-end (point)))))
1523 ((eq escape ?h)
1524 (widget-add-documentation-string-button widget))
1525 ((eq escape ?v)
1526 (if (and button-begin (not button-end))
1527 (widget-apply widget :value-create)
1528 (setq value-pos (point))))
1530 (widget-apply widget :format-handler escape)))))
1531 ;; Specify button, sample, and doc, and insert value.
1532 (and button-begin button-end
1533 (widget-specify-button widget button-begin button-end))
1534 (and sample-begin sample-end
1535 (widget-specify-sample widget sample-begin sample-end))
1536 (and doc-begin doc-end
1537 (widget-specify-doc widget doc-begin doc-end))
1538 (when value-pos
1539 (goto-char value-pos)
1540 (widget-apply widget :value-create)))
1541 (let ((from (point-min-marker))
1542 (to (point-max-marker)))
1543 (set-marker-insertion-type from t)
1544 (set-marker-insertion-type to nil)
1545 (widget-put widget :from from)
1546 (widget-put widget :to to)))
1547 (widget-clear-undo))
1549 (defun widget-default-format-handler (widget escape)
1550 (error "Unknown escape `%c'" escape))
1552 (defun widget-default-button-face-get (widget)
1553 ;; Use :button-face or widget-button-face
1554 (or (widget-get widget :button-face)
1555 (let ((parent (widget-get widget :parent)))
1556 (if parent
1557 (widget-apply parent :button-face-get)
1558 widget-button-face))))
1560 (defun widget-default-mouse-face-get (widget)
1561 ;; Use :mouse-face or widget-mouse-face
1562 (or (widget-get widget :mouse-face)
1563 (let ((parent (widget-get widget :parent)))
1564 (if parent
1565 (widget-apply parent :mouse-face-get)
1566 widget-mouse-face))))
1568 (defun widget-default-sample-face-get (widget)
1569 ;; Use :sample-face.
1570 (widget-get widget :sample-face))
1572 (defun widget-default-delete (widget)
1573 "Remove widget from the buffer."
1574 (let ((from (widget-get widget :from))
1575 (to (widget-get widget :to))
1576 (inactive-overlay (widget-get widget :inactive))
1577 (button-overlay (widget-get widget :button-overlay))
1578 (sample-overlay (widget-get widget :sample-overlay))
1579 (doc-overlay (widget-get widget :doc-overlay))
1580 (inhibit-modification-hooks t)
1581 (inhibit-read-only t))
1582 (widget-apply widget :value-delete)
1583 (widget-children-value-delete widget)
1584 (when inactive-overlay
1585 (delete-overlay inactive-overlay))
1586 (when button-overlay
1587 (delete-overlay button-overlay))
1588 (when sample-overlay
1589 (delete-overlay sample-overlay))
1590 (when doc-overlay
1591 (delete-overlay doc-overlay))
1592 (when (< from to)
1593 ;; Kludge: this doesn't need to be true for empty formats.
1594 (delete-region from to))
1595 (set-marker from nil)
1596 (set-marker to nil))
1597 (widget-clear-undo))
1599 (defun widget-default-value-set (widget value)
1600 "Recreate widget with new value."
1601 (let* ((old-pos (point))
1602 (from (copy-marker (widget-get widget :from)))
1603 (to (copy-marker (widget-get widget :to)))
1604 (offset (if (and (<= from old-pos) (<= old-pos to))
1605 (if (>= old-pos (1- to))
1606 (- old-pos to 1)
1607 (- old-pos from)))))
1608 ;;??? Bug: this ought to insert the new value before deleting the old one,
1609 ;; so that markers on either side of the value automatically
1610 ;; stay on the same side. -- rms.
1611 (save-excursion
1612 (goto-char (widget-get widget :from))
1613 (widget-apply widget :delete)
1614 (widget-put widget :value value)
1615 (widget-apply widget :create))
1616 (if offset
1617 (if (< offset 0)
1618 (goto-char (+ (widget-get widget :to) offset 1))
1619 (goto-char (min (+ from offset) (1- (widget-get widget :to))))))))
1621 (defun widget-default-value-inline (widget)
1622 "Wrap value in a list unless it is inline."
1623 (if (widget-get widget :inline)
1624 (widget-value widget)
1625 (list (widget-value widget))))
1627 (defun widget-default-default-get (widget)
1628 "Get `:value'."
1629 (widget-get widget :value))
1631 (defun widget-default-menu-tag-get (widget)
1632 "Use tag or value for menus."
1633 (or (widget-get widget :menu-tag)
1634 (widget-get widget :tag)
1635 (widget-princ-to-string (widget-get widget :value))))
1637 (defun widget-default-active (widget)
1638 "Return t if this widget is active (user modifiable)."
1639 (or (widget-get widget :always-active)
1640 (and (not (widget-get widget :inactive))
1641 (let ((parent (widget-get widget :parent)))
1642 (or (null parent)
1643 (widget-apply parent :active))))))
1645 (defun widget-default-deactivate (widget)
1646 "Make WIDGET inactive for user modifications."
1647 (widget-specify-inactive widget
1648 (widget-get widget :from)
1649 (widget-get widget :to)))
1651 (defun widget-default-action (widget &optional event)
1652 "Notify the parent when a widget changes."
1653 (let ((parent (widget-get widget :parent)))
1654 (when parent
1655 (widget-apply parent :notify widget event))))
1657 (defun widget-default-notify (widget child &optional event)
1658 "Pass notification to parent."
1659 (widget-default-action widget event))
1661 (defun widget-default-prompt-value (widget prompt value unbound)
1662 "Read an arbitrary value."
1663 (eval-minibuffer prompt))
1665 (defun widget-docstring (widget)
1666 "Return the documentation string specificied by WIDGET, or nil if none.
1667 If WIDGET has a `:doc' property, that specifies the documentation string.
1668 Otherwise, try the `:documentation-property' property. If this
1669 is a function, call it with the widget's value as an argument; if
1670 it is a symbol, use this symbol together with the widget's value
1671 as the argument to `documentation-property'."
1672 (let ((doc (or (widget-get widget :doc)
1673 (let ((doc-prop (widget-get widget :documentation-property))
1674 (value (widget-get widget :value)))
1675 (cond ((functionp doc-prop)
1676 (funcall doc-prop value))
1677 ((symbolp doc-prop)
1678 (documentation-property value doc-prop)))))))
1679 (when (and (stringp doc) (> (length doc) 0))
1680 ;; Remove any redundant `*' in the beginning.
1681 (when (eq (aref doc 0) ?*)
1682 (setq doc (substring doc 1)))
1683 ;; Remove trailing newlines.
1684 (when (string-match "\n+\\'" doc)
1685 (setq doc (substring doc 0 (match-beginning 0))))
1686 doc)))
1688 ;;; The `item' Widget.
1690 (define-widget 'item 'default
1691 "Constant items for inclusion in other widgets."
1692 :convert-widget 'widget-value-convert-widget
1693 :value-create 'widget-item-value-create
1694 :value-delete 'ignore
1695 :value-get 'widget-value-value-get
1696 :match 'widget-item-match
1697 :match-inline 'widget-item-match-inline
1698 :action 'widget-item-action
1699 :format "%t\n")
1701 (defun widget-item-value-create (widget)
1702 "Insert the printed representation of the value."
1703 (princ (widget-get widget :value) (current-buffer)))
1705 (defun widget-item-match (widget value)
1706 ;; Match if the value is the same.
1707 (equal (widget-get widget :value) value))
1709 (defun widget-item-match-inline (widget values)
1710 ;; Match if the value is the same.
1711 (let ((value (widget-get widget :value)))
1712 (and (listp value)
1713 (<= (length value) (length values))
1714 (let ((head (widget-sublist values 0 (length value))))
1715 (and (equal head value)
1716 (cons head (widget-sublist values (length value))))))))
1718 (defun widget-sublist (list start &optional end)
1719 "Return the sublist of LIST from START to END.
1720 If END is omitted, it defaults to the length of LIST."
1721 (if (> start 0) (setq list (nthcdr start list)))
1722 (if end
1723 (unless (<= end start)
1724 (setq list (copy-sequence list))
1725 (setcdr (nthcdr (- end start 1) list) nil)
1726 list)
1727 (copy-sequence list)))
1729 (defun widget-item-action (widget &optional event)
1730 ;; Just notify itself.
1731 (widget-apply widget :notify widget event))
1733 ;;; The `push-button' Widget.
1735 ;; (defcustom widget-push-button-gui t
1736 ;; "If non-nil, use GUI push buttons when available."
1737 ;; :group 'widgets
1738 ;; :type 'boolean)
1740 ;; Cache already created GUI objects.
1741 ;; (defvar widget-push-button-cache nil)
1743 (defcustom widget-push-button-prefix "["
1744 "String used as prefix for buttons."
1745 :type 'string
1746 :group 'widget-button)
1748 (defcustom widget-push-button-suffix "]"
1749 "String used as suffix for buttons."
1750 :type 'string
1751 :group 'widget-button)
1753 (define-widget 'push-button 'item
1754 "A pushable button."
1755 :button-prefix ""
1756 :button-suffix ""
1757 :value-create 'widget-push-button-value-create
1758 :format "%[%v%]")
1760 (defun widget-push-button-value-create (widget)
1761 "Insert text representing the `on' and `off' states."
1762 (let* ((tag (or (widget-get widget :tag)
1763 (widget-get widget :value)))
1764 (tag-glyph (widget-get widget :tag-glyph))
1765 (text (concat widget-push-button-prefix
1766 tag widget-push-button-suffix)))
1767 (if tag-glyph
1768 (widget-image-insert widget text tag-glyph)
1769 (insert text))))
1771 ;; (defun widget-gui-action (widget)
1772 ;; "Apply :action for WIDGET."
1773 ;; (widget-apply-action widget (this-command-keys)))
1775 ;;; The `link' Widget.
1777 (defcustom widget-link-prefix "["
1778 "String used as prefix for links."
1779 :type 'string
1780 :group 'widget-button)
1782 (defcustom widget-link-suffix "]"
1783 "String used as suffix for links."
1784 :type 'string
1785 :group 'widget-button)
1787 (define-widget 'link 'item
1788 "An embedded link."
1789 :button-prefix 'widget-link-prefix
1790 :button-suffix 'widget-link-suffix
1791 :follow-link 'mouse-face
1792 :help-echo "Follow the link."
1793 :format "%[%t%]")
1795 ;;; The `info-link' Widget.
1797 (define-widget 'info-link 'link
1798 "A link to an info file."
1799 :action 'widget-info-link-action)
1801 (defun widget-info-link-action (widget &optional event)
1802 "Open the info node specified by WIDGET."
1803 (info (widget-value widget)))
1805 ;;; The `url-link' Widget.
1807 (define-widget 'url-link 'link
1808 "A link to an www page."
1809 :action 'widget-url-link-action)
1811 (defun widget-url-link-action (widget &optional event)
1812 "Open the URL specified by WIDGET."
1813 (browse-url (widget-value widget)))
1815 ;;; The `function-link' Widget.
1817 (define-widget 'function-link 'link
1818 "A link to an Emacs function."
1819 :action 'widget-function-link-action)
1821 (defun widget-function-link-action (widget &optional event)
1822 "Show the function specified by WIDGET."
1823 (describe-function (widget-value widget)))
1825 ;;; The `variable-link' Widget.
1827 (define-widget 'variable-link 'link
1828 "A link to an Emacs variable."
1829 :action 'widget-variable-link-action)
1831 (defun widget-variable-link-action (widget &optional event)
1832 "Show the variable specified by WIDGET."
1833 (describe-variable (widget-value widget)))
1835 ;;; The `file-link' Widget.
1837 (define-widget 'file-link 'link
1838 "A link to a file."
1839 :action 'widget-file-link-action)
1841 (defun widget-file-link-action (widget &optional event)
1842 "Find the file specified by WIDGET."
1843 (find-file (widget-value widget)))
1845 ;;; The `emacs-library-link' Widget.
1847 (define-widget 'emacs-library-link 'link
1848 "A link to an Emacs Lisp library file."
1849 :action 'widget-emacs-library-link-action)
1851 (defun widget-emacs-library-link-action (widget &optional event)
1852 "Find the Emacs library file specified by WIDGET."
1853 (find-file (locate-library (widget-value widget))))
1855 ;;; The `emacs-commentary-link' Widget.
1857 (define-widget 'emacs-commentary-link 'link
1858 "A link to Commentary in an Emacs Lisp library file."
1859 :action 'widget-emacs-commentary-link-action)
1861 (defun widget-emacs-commentary-link-action (widget &optional event)
1862 "Find the Commentary section of the Emacs file specified by WIDGET."
1863 (finder-commentary (widget-value widget)))
1865 ;;; The `editable-field' Widget.
1867 (define-widget 'editable-field 'default
1868 "An editable text field.
1869 Note: In an `editable-field' widget, the `%v' escape must be preceded
1870 by some other text in the `:format' string (if specified)."
1871 :convert-widget 'widget-value-convert-widget
1872 :keymap widget-field-keymap
1873 :format "%v"
1874 :help-echo "M-TAB: complete field; RET: enter value"
1875 :value ""
1876 :prompt-internal 'widget-field-prompt-internal
1877 :prompt-history 'widget-field-history
1878 :prompt-value 'widget-field-prompt-value
1879 :action 'widget-field-action
1880 :validate 'widget-field-validate
1881 :valid-regexp ""
1882 :error "Field's value doesn't match allowed forms"
1883 :value-create 'widget-field-value-create
1884 :value-set 'widget-field-value-set
1885 :value-delete 'widget-field-value-delete
1886 :value-get 'widget-field-value-get
1887 :match 'widget-field-match)
1889 (defvar widget-field-history nil
1890 "History of field minibuffer edits.")
1892 (defun widget-field-prompt-internal (widget prompt initial history)
1893 "Read string for WIDGET prompting with PROMPT.
1894 INITIAL is the initial input and HISTORY is a symbol containing
1895 the earlier input."
1896 (read-string prompt initial history))
1898 (defun widget-field-prompt-value (widget prompt value unbound)
1899 "Prompt for a string."
1900 (widget-apply widget
1901 :value-to-external
1902 (widget-apply widget
1903 :prompt-internal prompt
1904 (unless unbound
1905 (cons (widget-apply widget
1906 :value-to-internal value)
1908 (widget-get widget :prompt-history))))
1910 (defvar widget-edit-functions nil)
1912 (defun widget-field-action (widget &optional event)
1913 "Move to next field."
1914 (widget-forward 1)
1915 (run-hook-with-args 'widget-edit-functions widget))
1917 (defun widget-field-validate (widget)
1918 "Valid if the content matches `:valid-regexp'."
1919 (unless (string-match (widget-get widget :valid-regexp)
1920 (widget-apply widget :value-get))
1921 widget))
1923 (defun widget-field-value-set (widget value)
1924 "Set an editable text field WIDGET to VALUE"
1925 (let ((from (widget-field-start widget))
1926 (to (widget-field-text-end widget))
1927 (buffer (widget-field-buffer widget))
1928 (size (widget-get widget :size)))
1929 (when (and from to (buffer-live-p buffer))
1930 (with-current-buffer buffer
1931 (goto-char from)
1932 (delete-char (- to from))
1933 (insert value)))))
1935 (defun widget-field-value-create (widget)
1936 "Create an editable text field."
1937 (let ((size (widget-get widget :size))
1938 (value (widget-get widget :value))
1939 (from (point))
1940 ;; This is changed to a real overlay in `widget-setup'. We
1941 ;; need the end points to behave differently until
1942 ;; `widget-setup' is called.
1943 (overlay (cons (make-marker) (make-marker))))
1944 (widget-put widget :field-overlay overlay)
1945 (insert value)
1946 (and size
1947 (< (length value) size)
1948 (insert-char ?\s (- size (length value))))
1949 (unless (memq widget widget-field-list)
1950 (setq widget-field-new (cons widget widget-field-new)))
1951 (move-marker (cdr overlay) (point))
1952 (set-marker-insertion-type (cdr overlay) nil)
1953 (when (null size)
1954 (insert ?\n))
1955 (move-marker (car overlay) from)
1956 (set-marker-insertion-type (car overlay) t)))
1958 (defun widget-field-value-delete (widget)
1959 "Remove the widget from the list of active editing fields."
1960 (setq widget-field-list (delq widget widget-field-list))
1961 (setq widget-field-new (delq widget widget-field-new))
1962 ;; These are nil if the :format string doesn't contain `%v'.
1963 (let ((overlay (widget-get widget :field-overlay)))
1964 (when (overlayp overlay)
1965 (delete-overlay overlay))))
1967 (defun widget-field-value-get (widget)
1968 "Return current text in editing field."
1969 (let ((from (widget-field-start widget))
1970 (to (widget-field-text-end widget))
1971 (buffer (widget-field-buffer widget))
1972 (secret (widget-get widget :secret))
1973 (old (current-buffer)))
1974 (if (and from to)
1975 (progn
1976 (set-buffer buffer)
1977 (let ((result (buffer-substring-no-properties from to)))
1978 (when secret
1979 (let ((index 0))
1980 (while (< (+ from index) to)
1981 (aset result index
1982 (get-char-property (+ from index) 'secret))
1983 (setq index (1+ index)))))
1984 (set-buffer old)
1985 result))
1986 (widget-get widget :value))))
1988 (defun widget-field-match (widget value)
1989 ;; Match any string.
1990 (stringp value))
1992 ;;; The `text' Widget.
1994 (define-widget 'text 'editable-field
1995 "A multiline text area."
1996 :keymap widget-text-keymap)
1998 ;;; The `menu-choice' Widget.
2000 (define-widget 'menu-choice 'default
2001 "A menu of options."
2002 :convert-widget 'widget-types-convert-widget
2003 :copy 'widget-types-copy
2004 :format "%[%t%]: %v"
2005 :case-fold t
2006 :tag "choice"
2007 :void '(item :format "invalid (%t)\n")
2008 :value-create 'widget-choice-value-create
2009 :value-get 'widget-child-value-get
2010 :value-inline 'widget-child-value-inline
2011 :default-get 'widget-choice-default-get
2012 :mouse-down-action 'widget-choice-mouse-down-action
2013 :action 'widget-choice-action
2014 :error "Make a choice"
2015 :validate 'widget-choice-validate
2016 :match 'widget-choice-match
2017 :match-inline 'widget-choice-match-inline)
2019 (defun widget-choice-value-create (widget)
2020 "Insert the first choice that matches the value."
2021 (let ((value (widget-get widget :value))
2022 (args (widget-get widget :args))
2023 (explicit (widget-get widget :explicit-choice))
2024 current)
2025 (if explicit
2026 (progn
2027 ;; If the user specified the choice for this value,
2028 ;; respect that choice.
2029 (widget-put widget :children (list (widget-create-child-value
2030 widget explicit value)))
2031 (widget-put widget :choice explicit)
2032 (widget-put widget :explicit-choice nil))
2033 (while args
2034 (setq current (car args)
2035 args (cdr args))
2036 (when (widget-apply current :match value)
2037 (widget-put widget :children (list (widget-create-child-value
2038 widget current value)))
2039 (widget-put widget :choice current)
2040 (setq args nil
2041 current nil)))
2042 (when current
2043 (let ((void (widget-get widget :void)))
2044 (widget-put widget :children (list (widget-create-child-and-convert
2045 widget void :value value)))
2046 (widget-put widget :choice void))))))
2048 (defun widget-choice-default-get (widget)
2049 ;; Get default for the first choice.
2050 (widget-default-get (car (widget-get widget :args))))
2052 (defcustom widget-choice-toggle nil
2053 "If non-nil, a binary choice will just toggle between the values.
2054 Otherwise, the user will explicitly have to choose between the values
2055 when he invoked the menu."
2056 :type 'boolean
2057 :group 'widgets)
2059 (defun widget-choice-mouse-down-action (widget &optional event)
2060 ;; Return non-nil if we need a menu.
2061 (let ((args (widget-get widget :args))
2062 (old (widget-get widget :choice)))
2063 (cond ((not (display-popup-menus-p))
2064 ;; No place to pop up a menu.
2065 nil)
2066 ((< (length args) 2)
2067 ;; Empty or singleton list, just return the value.
2068 nil)
2069 ((> (length args) widget-menu-max-size)
2070 ;; Too long, prompt.
2071 nil)
2072 ((> (length args) 2)
2073 ;; Reasonable sized list, use menu.
2075 ((and widget-choice-toggle (memq old args))
2076 ;; We toggle.
2077 nil)
2079 ;; Ask which of the two.
2080 t))))
2082 (defun widget-choice-action (widget &optional event)
2083 ;; Make a choice.
2084 (let ((args (widget-get widget :args))
2085 (old (widget-get widget :choice))
2086 (tag (widget-apply widget :menu-tag-get))
2087 (completion-ignore-case (widget-get widget :case-fold))
2088 this-explicit
2089 current choices)
2090 ;; Remember old value.
2091 (if (and old (not (widget-apply widget :validate)))
2092 (let* ((external (widget-value widget))
2093 (internal (widget-apply old :value-to-internal external)))
2094 (widget-put old :value internal)))
2095 ;; Find new choice.
2096 (setq current
2097 (cond ((= (length args) 0)
2098 nil)
2099 ((= (length args) 1)
2100 (nth 0 args))
2101 ((and widget-choice-toggle
2102 (= (length args) 2)
2103 (memq old args))
2104 (if (eq old (nth 0 args))
2105 (nth 1 args)
2106 (nth 0 args)))
2108 (while args
2109 (setq current (car args)
2110 args (cdr args))
2111 (setq choices
2112 (cons (cons (widget-apply current :menu-tag-get)
2113 current)
2114 choices)))
2115 (setq this-explicit t)
2116 (widget-choose tag (reverse choices) event))))
2117 (when current
2118 ;; If this was an explicit user choice, record the choice,
2119 ;; so that widget-choice-value-create will respect it.
2120 (when this-explicit
2121 (widget-put widget :explicit-choice current))
2122 (widget-value-set widget (widget-default-get current))
2123 (widget-setup)
2124 (widget-apply widget :notify widget event)))
2125 (run-hook-with-args 'widget-edit-functions widget))
2127 (defun widget-choice-validate (widget)
2128 ;; Valid if we have made a valid choice.
2129 (if (eq (widget-get widget :void) (widget-get widget :choice))
2130 widget
2131 (widget-apply (car (widget-get widget :children)) :validate)))
2133 (defun widget-choice-match (widget value)
2134 ;; Matches if one of the choices matches.
2135 (let ((args (widget-get widget :args))
2136 current found)
2137 (while (and args (not found))
2138 (setq current (car args)
2139 args (cdr args)
2140 found (widget-apply current :match value)))
2141 found))
2143 (defun widget-choice-match-inline (widget values)
2144 ;; Matches if one of the choices matches.
2145 (let ((args (widget-get widget :args))
2146 current found)
2147 (while (and args (null found))
2148 (setq current (car args)
2149 args (cdr args)
2150 found (widget-match-inline current values)))
2151 found))
2153 ;;; The `toggle' Widget.
2155 (define-widget 'toggle 'item
2156 "Toggle between two states."
2157 :format "%[%v%]\n"
2158 :value-create 'widget-toggle-value-create
2159 :action 'widget-toggle-action
2160 :match (lambda (widget value) t)
2161 :on "on"
2162 :off "off")
2164 (defun widget-toggle-value-create (widget)
2165 "Insert text representing the `on' and `off' states."
2166 (if (widget-value widget)
2167 (let ((image (widget-get widget :on-glyph)))
2168 (and (display-graphic-p)
2169 (listp image)
2170 (not (eq (car image) 'image))
2171 (widget-put widget :on-glyph (setq image (eval image))))
2172 (widget-image-insert widget
2173 (widget-get widget :on)
2174 image))
2175 (let ((image (widget-get widget :off-glyph)))
2176 (and (display-graphic-p)
2177 (listp image)
2178 (not (eq (car image) 'image))
2179 (widget-put widget :off-glyph (setq image (eval image))))
2180 (widget-image-insert widget (widget-get widget :off) image))))
2182 (defun widget-toggle-action (widget &optional event)
2183 ;; Toggle value.
2184 (widget-value-set widget (not (widget-value widget)))
2185 (widget-apply widget :notify widget event)
2186 (run-hook-with-args 'widget-edit-functions widget))
2188 ;;; The `checkbox' Widget.
2190 (define-widget 'checkbox 'toggle
2191 "A checkbox toggle."
2192 :button-suffix ""
2193 :button-prefix ""
2194 :format "%[%v%]"
2195 :on "[X]"
2196 ;; We could probably do the same job as the images using single
2197 ;; space characters in a boxed face with a stretch specification to
2198 ;; make them square.
2199 :on-glyph "checked"
2200 :off "[ ]"
2201 :off-glyph "unchecked"
2202 :help-echo "Toggle this item."
2203 :action 'widget-checkbox-action)
2205 (defun widget-checkbox-action (widget &optional event)
2206 "Toggle checkbox, notify parent, and set active state of sibling."
2207 (widget-toggle-action widget event)
2208 (let ((sibling (widget-get-sibling widget)))
2209 (when sibling
2210 (if (widget-value widget)
2211 (widget-apply sibling :activate)
2212 (widget-apply sibling :deactivate))
2213 (widget-clear-undo))))
2215 ;;; The `checklist' Widget.
2217 (define-widget 'checklist 'default
2218 "A multiple choice widget."
2219 :convert-widget 'widget-types-convert-widget
2220 :copy 'widget-types-copy
2221 :format "%v"
2222 :offset 4
2223 :entry-format "%b %v"
2224 :greedy nil
2225 :value-create 'widget-checklist-value-create
2226 :value-get 'widget-checklist-value-get
2227 :validate 'widget-checklist-validate
2228 :match 'widget-checklist-match
2229 :match-inline 'widget-checklist-match-inline)
2231 (defun widget-checklist-value-create (widget)
2232 ;; Insert all values
2233 (let ((alist (widget-checklist-match-find widget))
2234 (args (widget-get widget :args)))
2235 (dolist (item args)
2236 (widget-checklist-add-item widget item (assq item alist)))
2237 (widget-put widget :children (nreverse (widget-get widget :children)))))
2239 (defun widget-checklist-add-item (widget type chosen)
2240 "Create checklist item in WIDGET of type TYPE.
2241 If the item is checked, CHOSEN is a cons whose cdr is the value."
2242 (and (eq (preceding-char) ?\n)
2243 (widget-get widget :indent)
2244 (insert-char ?\s (widget-get widget :indent)))
2245 (widget-specify-insert
2246 (let* ((children (widget-get widget :children))
2247 (buttons (widget-get widget :buttons))
2248 (button-args (or (widget-get type :sibling-args)
2249 (widget-get widget :button-args)))
2250 (from (point))
2251 child button)
2252 (insert (widget-get widget :entry-format))
2253 (goto-char from)
2254 ;; Parse % escapes in format.
2255 (while (re-search-forward "%\\([bv%]\\)" nil t)
2256 (let ((escape (char-after (match-beginning 1))))
2257 (delete-char -2)
2258 (cond ((eq escape ?%)
2259 (insert ?%))
2260 ((eq escape ?b)
2261 (setq button (apply 'widget-create-child-and-convert
2262 widget 'checkbox
2263 :value (not (null chosen))
2264 button-args)))
2265 ((eq escape ?v)
2266 (setq child
2267 (cond ((not chosen)
2268 (let ((child (widget-create-child widget type)))
2269 (widget-apply child :deactivate)
2270 child))
2271 ((widget-get type :inline)
2272 (widget-create-child-value
2273 widget type (cdr chosen)))
2275 (widget-create-child-value
2276 widget type (car (cdr chosen)))))))
2278 (error "Unknown escape `%c'" escape)))))
2279 ;; Update properties.
2280 (and button child (widget-put child :button button))
2281 (and button (widget-put widget :buttons (cons button buttons)))
2282 (and child (widget-put widget :children (cons child children))))))
2284 (defun widget-checklist-match (widget values)
2285 ;; All values must match a type in the checklist.
2286 (and (listp values)
2287 (null (cdr (widget-checklist-match-inline widget values)))))
2289 (defun widget-checklist-match-inline (widget values)
2290 ;; Find the values which match a type in the checklist.
2291 (let ((greedy (widget-get widget :greedy))
2292 (args (copy-sequence (widget-get widget :args)))
2293 found rest)
2294 (while values
2295 (let ((answer (widget-checklist-match-up args values)))
2296 (cond (answer
2297 (let ((vals (widget-match-inline answer values)))
2298 (setq found (append found (car vals))
2299 values (cdr vals)
2300 args (delq answer args))))
2301 (greedy
2302 (setq rest (append rest (list (car values)))
2303 values (cdr values)))
2305 (setq rest (append rest values)
2306 values nil)))))
2307 (cons found rest)))
2309 (defun widget-checklist-match-find (widget &optional vals)
2310 "Find the vals which match a type in the checklist.
2311 Return an alist of (TYPE MATCH)."
2312 (or vals (setq vals (widget-get widget :value)))
2313 (let ((greedy (widget-get widget :greedy))
2314 (args (copy-sequence (widget-get widget :args)))
2315 found)
2316 (while vals
2317 (let ((answer (widget-checklist-match-up args vals)))
2318 (cond (answer
2319 (let ((match (widget-match-inline answer vals)))
2320 (setq found (cons (cons answer (car match)) found)
2321 vals (cdr match)
2322 args (delq answer args))))
2323 (greedy
2324 (setq vals (cdr vals)))
2326 (setq vals nil)))))
2327 found))
2329 (defun widget-checklist-match-up (args vals)
2330 "Return the first type from ARGS that matches VALS."
2331 (let (current found)
2332 (while (and args (null found))
2333 (setq current (car args)
2334 args (cdr args)
2335 found (widget-match-inline current vals)))
2336 (if found
2337 current)))
2339 (defun widget-checklist-value-get (widget)
2340 ;; The values of all selected items.
2341 (let ((children (widget-get widget :children))
2342 child result)
2343 (while children
2344 (setq child (car children)
2345 children (cdr children))
2346 (if (widget-value (widget-get child :button))
2347 (setq result (append result (widget-apply child :value-inline)))))
2348 result))
2350 (defun widget-checklist-validate (widget)
2351 ;; Ticked chilren must be valid.
2352 (let ((children (widget-get widget :children))
2353 child button found)
2354 (while (and children (not found))
2355 (setq child (car children)
2356 children (cdr children)
2357 button (widget-get child :button)
2358 found (and (widget-value button)
2359 (widget-apply child :validate))))
2360 found))
2362 ;;; The `option' Widget
2364 (define-widget 'option 'checklist
2365 "An widget with an optional item."
2366 :inline t)
2368 ;;; The `choice-item' Widget.
2370 (define-widget 'choice-item 'item
2371 "Button items that delegate action events to their parents."
2372 :action 'widget-parent-action
2373 :format "%[%t%] \n")
2375 ;;; The `radio-button' Widget.
2377 (define-widget 'radio-button 'toggle
2378 "A radio button for use in the `radio' widget."
2379 :notify 'widget-radio-button-notify
2380 :format "%[%v%]"
2381 :button-suffix ""
2382 :button-prefix ""
2383 :on "(*)"
2384 :on-glyph "radio1"
2385 :off "( )"
2386 :off-glyph "radio0")
2388 (defun widget-radio-button-notify (widget child &optional event)
2389 ;; Tell daddy.
2390 (widget-apply (widget-get widget :parent) :action widget event))
2392 ;;; The `radio-button-choice' Widget.
2394 (define-widget 'radio-button-choice 'default
2395 "Select one of multiple options."
2396 :convert-widget 'widget-types-convert-widget
2397 :copy 'widget-types-copy
2398 :offset 4
2399 :format "%v"
2400 :entry-format "%b %v"
2401 :value-create 'widget-radio-value-create
2402 :value-get 'widget-radio-value-get
2403 :value-inline 'widget-radio-value-inline
2404 :value-set 'widget-radio-value-set
2405 :error "You must push one of the buttons"
2406 :validate 'widget-radio-validate
2407 :match 'widget-choice-match
2408 :match-inline 'widget-choice-match-inline
2409 :action 'widget-radio-action)
2411 (defun widget-radio-value-create (widget)
2412 ;; Insert all values
2413 (let ((args (widget-get widget :args))
2414 arg)
2415 (while args
2416 (setq arg (car args)
2417 args (cdr args))
2418 (widget-radio-add-item widget arg))))
2420 (defun widget-radio-add-item (widget type)
2421 "Add to radio widget WIDGET a new radio button item of type TYPE."
2422 ;; (setq type (widget-convert type))
2423 (and (eq (preceding-char) ?\n)
2424 (widget-get widget :indent)
2425 (insert-char ?\s (widget-get widget :indent)))
2426 (widget-specify-insert
2427 (let* ((value (widget-get widget :value))
2428 (children (widget-get widget :children))
2429 (buttons (widget-get widget :buttons))
2430 (button-args (or (widget-get type :sibling-args)
2431 (widget-get widget :button-args)))
2432 (from (point))
2433 (chosen (and (null (widget-get widget :choice))
2434 (widget-apply type :match value)))
2435 child button)
2436 (insert (widget-get widget :entry-format))
2437 (goto-char from)
2438 ;; Parse % escapes in format.
2439 (while (re-search-forward "%\\([bv%]\\)" nil t)
2440 (let ((escape (char-after (match-beginning 1))))
2441 (delete-char -2)
2442 (cond ((eq escape ?%)
2443 (insert ?%))
2444 ((eq escape ?b)
2445 (setq button (apply 'widget-create-child-and-convert
2446 widget 'radio-button
2447 :value (not (null chosen))
2448 button-args)))
2449 ((eq escape ?v)
2450 (setq child (if chosen
2451 (widget-create-child-value
2452 widget type value)
2453 (widget-create-child widget type)))
2454 (unless chosen
2455 (widget-apply child :deactivate)))
2457 (error "Unknown escape `%c'" escape)))))
2458 ;; Update properties.
2459 (when chosen
2460 (widget-put widget :choice type))
2461 (when button
2462 (widget-put child :button button)
2463 (widget-put widget :buttons (nconc buttons (list button))))
2464 (when child
2465 (widget-put widget :children (nconc children (list child))))
2466 child)))
2468 (defun widget-radio-value-get (widget)
2469 ;; Get value of the child widget.
2470 (let ((chosen (widget-radio-chosen widget)))
2471 (and chosen (widget-value chosen))))
2473 (defun widget-radio-chosen (widget)
2474 "Return the widget representing the chosen radio button."
2475 (let ((children (widget-get widget :children))
2476 current found)
2477 (while children
2478 (setq current (car children)
2479 children (cdr children))
2480 (when (widget-apply (widget-get current :button) :value-get)
2481 (setq found current
2482 children nil)))
2483 found))
2485 (defun widget-radio-value-inline (widget)
2486 ;; Get value of the child widget.
2487 (let ((children (widget-get widget :children))
2488 current found)
2489 (while children
2490 (setq current (car children)
2491 children (cdr children))
2492 (when (widget-apply (widget-get current :button) :value-get)
2493 (setq found (widget-apply current :value-inline)
2494 children nil)))
2495 found))
2497 (defun widget-radio-value-set (widget value)
2498 ;; We can't just delete and recreate a radio widget, since children
2499 ;; can be added after the original creation and won't be recreated
2500 ;; by `:create'.
2501 (let ((children (widget-get widget :children))
2502 current found)
2503 (while children
2504 (setq current (car children)
2505 children (cdr children))
2506 (let* ((button (widget-get current :button))
2507 (match (and (not found)
2508 (widget-apply current :match value))))
2509 (widget-value-set button match)
2510 (if match
2511 (progn
2512 (widget-value-set current value)
2513 (widget-apply current :activate))
2514 (widget-apply current :deactivate))
2515 (setq found (or found match))))))
2517 (defun widget-radio-validate (widget)
2518 ;; Valid if we have made a valid choice.
2519 (let ((children (widget-get widget :children))
2520 current found button)
2521 (while (and children (not found))
2522 (setq current (car children)
2523 children (cdr children)
2524 button (widget-get current :button)
2525 found (widget-apply button :value-get)))
2526 (if found
2527 (widget-apply current :validate)
2528 widget)))
2530 (defun widget-radio-action (widget child event)
2531 ;; Check if a radio button was pressed.
2532 (let ((children (widget-get widget :children))
2533 (buttons (widget-get widget :buttons))
2534 current)
2535 (when (memq child buttons)
2536 (while children
2537 (setq current (car children)
2538 children (cdr children))
2539 (let* ((button (widget-get current :button)))
2540 (cond ((eq child button)
2541 (widget-value-set button t)
2542 (widget-apply current :activate))
2543 ((widget-value button)
2544 (widget-value-set button nil)
2545 (widget-apply current :deactivate)))))))
2546 ;; Pass notification to parent.
2547 (widget-apply widget :notify child event))
2549 ;;; The `insert-button' Widget.
2551 (define-widget 'insert-button 'push-button
2552 "An insert button for the `editable-list' widget."
2553 :tag "INS"
2554 :help-echo "Insert a new item into the list at this position."
2555 :action 'widget-insert-button-action)
2557 (defun widget-insert-button-action (widget &optional event)
2558 ;; Ask the parent to insert a new item.
2559 (widget-apply (widget-get widget :parent)
2560 :insert-before (widget-get widget :widget)))
2562 ;;; The `delete-button' Widget.
2564 (define-widget 'delete-button 'push-button
2565 "A delete button for the `editable-list' widget."
2566 :tag "DEL"
2567 :help-echo "Delete this item from the list."
2568 :action 'widget-delete-button-action)
2570 (defun widget-delete-button-action (widget &optional event)
2571 ;; Ask the parent to insert a new item.
2572 (widget-apply (widget-get widget :parent)
2573 :delete-at (widget-get widget :widget)))
2575 ;;; The `editable-list' Widget.
2577 ;; (defcustom widget-editable-list-gui nil
2578 ;; "If non-nil, use GUI push-buttons in editable list when available."
2579 ;; :type 'boolean
2580 ;; :group 'widgets)
2582 (define-widget 'editable-list 'default
2583 "A variable list of widgets of the same type."
2584 :convert-widget 'widget-types-convert-widget
2585 :copy 'widget-types-copy
2586 :offset 12
2587 :format "%v%i\n"
2588 :format-handler 'widget-editable-list-format-handler
2589 :entry-format "%i %d %v"
2590 :value-create 'widget-editable-list-value-create
2591 :value-get 'widget-editable-list-value-get
2592 :validate 'widget-children-validate
2593 :match 'widget-editable-list-match
2594 :match-inline 'widget-editable-list-match-inline
2595 :insert-before 'widget-editable-list-insert-before
2596 :delete-at 'widget-editable-list-delete-at)
2598 (defun widget-editable-list-format-handler (widget escape)
2599 ;; We recognize the insert button.
2600 ;; (let ((widget-push-button-gui widget-editable-list-gui))
2601 (cond ((eq escape ?i)
2602 (and (widget-get widget :indent)
2603 (insert-char ?\s (widget-get widget :indent)))
2604 (apply 'widget-create-child-and-convert
2605 widget 'insert-button
2606 (widget-get widget :append-button-args)))
2608 (widget-default-format-handler widget escape)))
2609 ;; )
2612 (defun widget-editable-list-value-create (widget)
2613 ;; Insert all values
2614 (let* ((value (widget-get widget :value))
2615 (type (nth 0 (widget-get widget :args)))
2616 children)
2617 (widget-put widget :value-pos (copy-marker (point)))
2618 (set-marker-insertion-type (widget-get widget :value-pos) t)
2619 (while value
2620 (let ((answer (widget-match-inline type value)))
2621 (if answer
2622 (setq children (cons (widget-editable-list-entry-create
2623 widget
2624 (if (widget-get type :inline)
2625 (car answer)
2626 (car (car answer)))
2628 children)
2629 value (cdr answer))
2630 (setq value nil))))
2631 (widget-put widget :children (nreverse children))))
2633 (defun widget-editable-list-value-get (widget)
2634 ;; Get value of the child widget.
2635 (apply 'append (mapcar (lambda (child) (widget-apply child :value-inline))
2636 (widget-get widget :children))))
2638 (defun widget-editable-list-match (widget value)
2639 ;; Value must be a list and all the members must match the type.
2640 (and (listp value)
2641 (null (cdr (widget-editable-list-match-inline widget value)))))
2643 (defun widget-editable-list-match-inline (widget value)
2644 (let ((type (nth 0 (widget-get widget :args)))
2645 (ok t)
2646 found)
2647 (while (and value ok)
2648 (let ((answer (widget-match-inline type value)))
2649 (if answer
2650 (setq found (append found (car answer))
2651 value (cdr answer))
2652 (setq ok nil))))
2653 (cons found value)))
2655 (defun widget-editable-list-insert-before (widget before)
2656 ;; Insert a new child in the list of children.
2657 (save-excursion
2658 (let ((children (widget-get widget :children))
2659 (inhibit-read-only t)
2660 before-change-functions
2661 after-change-functions)
2662 (cond (before
2663 (goto-char (widget-get before :entry-from)))
2665 (goto-char (widget-get widget :value-pos))))
2666 (let ((child (widget-editable-list-entry-create
2667 widget nil nil)))
2668 (when (< (widget-get child :entry-from) (widget-get widget :from))
2669 (set-marker (widget-get widget :from)
2670 (widget-get child :entry-from)))
2671 (if (eq (car children) before)
2672 (widget-put widget :children (cons child children))
2673 (while (not (eq (car (cdr children)) before))
2674 (setq children (cdr children)))
2675 (setcdr children (cons child (cdr children)))))))
2676 (widget-setup)
2677 (widget-apply widget :notify widget))
2679 (defun widget-editable-list-delete-at (widget child)
2680 ;; Delete child from list of children.
2681 (save-excursion
2682 (let ((buttons (copy-sequence (widget-get widget :buttons)))
2683 button
2684 (inhibit-read-only t)
2685 before-change-functions
2686 after-change-functions)
2687 (while buttons
2688 (setq button (car buttons)
2689 buttons (cdr buttons))
2690 (when (eq (widget-get button :widget) child)
2691 (widget-put widget
2692 :buttons (delq button (widget-get widget :buttons)))
2693 (widget-delete button))))
2694 (let ((entry-from (widget-get child :entry-from))
2695 (entry-to (widget-get child :entry-to))
2696 (inhibit-read-only t)
2697 before-change-functions
2698 after-change-functions)
2699 (widget-delete child)
2700 (delete-region entry-from entry-to)
2701 (set-marker entry-from nil)
2702 (set-marker entry-to nil))
2703 (widget-put widget :children (delq child (widget-get widget :children))))
2704 (widget-setup)
2705 (widget-apply widget :notify widget))
2707 (defun widget-editable-list-entry-create (widget value conv)
2708 ;; Create a new entry to the list.
2709 (let ((type (nth 0 (widget-get widget :args)))
2710 ;; (widget-push-button-gui widget-editable-list-gui)
2711 child delete insert)
2712 (widget-specify-insert
2713 (save-excursion
2714 (and (widget-get widget :indent)
2715 (insert-char ?\s (widget-get widget :indent)))
2716 (insert (widget-get widget :entry-format)))
2717 ;; Parse % escapes in format.
2718 (while (re-search-forward "%\\(.\\)" nil t)
2719 (let ((escape (char-after (match-beginning 1))))
2720 (delete-char -2)
2721 (cond ((eq escape ?%)
2722 (insert ?%))
2723 ((eq escape ?i)
2724 (setq insert (apply 'widget-create-child-and-convert
2725 widget 'insert-button
2726 (widget-get widget :insert-button-args))))
2727 ((eq escape ?d)
2728 (setq delete (apply 'widget-create-child-and-convert
2729 widget 'delete-button
2730 (widget-get widget :delete-button-args))))
2731 ((eq escape ?v)
2732 (if conv
2733 (setq child (widget-create-child-value
2734 widget type value))
2735 (setq child (widget-create-child-value
2736 widget type (widget-default-get type)))))
2738 (error "Unknown escape `%c'" escape)))))
2739 (let ((buttons (widget-get widget :buttons)))
2740 (if insert (push insert buttons))
2741 (if delete (push delete buttons))
2742 (widget-put widget :buttons buttons))
2743 (let ((entry-from (point-min-marker))
2744 (entry-to (point-max-marker)))
2745 (set-marker-insertion-type entry-from t)
2746 (set-marker-insertion-type entry-to nil)
2747 (widget-put child :entry-from entry-from)
2748 (widget-put child :entry-to entry-to)))
2749 (if insert (widget-put insert :widget child))
2750 (if delete (widget-put delete :widget child))
2751 child))
2753 ;;; The `group' Widget.
2755 (define-widget 'group 'default
2756 "A widget which groups other widgets inside."
2757 :convert-widget 'widget-types-convert-widget
2758 :copy 'widget-types-copy
2759 :format "%v"
2760 :value-create 'widget-group-value-create
2761 :value-get 'widget-editable-list-value-get
2762 :default-get 'widget-group-default-get
2763 :validate 'widget-children-validate
2764 :match 'widget-group-match
2765 :match-inline 'widget-group-match-inline)
2767 (defun widget-group-value-create (widget)
2768 ;; Create each component.
2769 (let ((args (widget-get widget :args))
2770 (value (widget-get widget :value))
2771 arg answer children)
2772 (while args
2773 (setq arg (car args)
2774 args (cdr args)
2775 answer (widget-match-inline arg value)
2776 value (cdr answer))
2777 (and (eq (preceding-char) ?\n)
2778 (widget-get widget :indent)
2779 (insert-char ?\s (widget-get widget :indent)))
2780 (push (cond ((null answer)
2781 (widget-create-child widget arg))
2782 ((widget-get arg :inline)
2783 (widget-create-child-value widget arg (car answer)))
2785 (widget-create-child-value widget arg (car (car answer)))))
2786 children))
2787 (widget-put widget :children (nreverse children))))
2789 (defun widget-group-default-get (widget)
2790 ;; Get the default of the components.
2791 (mapcar 'widget-default-get (widget-get widget :args)))
2793 (defun widget-group-match (widget values)
2794 ;; Match if the components match.
2795 (and (listp values)
2796 (let ((match (widget-group-match-inline widget values)))
2797 (and match (null (cdr match))))))
2799 (defun widget-group-match-inline (widget vals)
2800 ;; Match if the components match.
2801 (let ((args (widget-get widget :args))
2802 argument answer found)
2803 (while args
2804 (setq argument (car args)
2805 args (cdr args))
2806 (if (setq answer (widget-match-inline argument vals))
2807 (setq found (append found (car answer))
2808 vals (cdr answer))
2809 (setq vals nil
2810 args nil)))
2811 (if answer
2812 (cons found vals))))
2814 ;;; The `visibility' Widget.
2816 (define-widget 'visibility 'item
2817 "An indicator and manipulator for hidden items.
2819 The following properties have special meanings for this widget:
2820 :on-image Image filename or spec to display when the item is visible.
2821 :on Text shown if the \"on\" image is nil or cannot be displayed.
2822 :off-image Image filename or spec to display when the item is hidden.
2823 :off Text shown if the \"off\" image is nil cannot be displayed."
2824 :format "%[%v%]"
2825 :button-prefix ""
2826 :button-suffix ""
2827 :on-image "down"
2828 :on "Hide"
2829 :off-image "right"
2830 :off "Show"
2831 :value-create 'widget-visibility-value-create
2832 :action 'widget-toggle-action
2833 :match (lambda (widget value) t))
2835 (defun widget-visibility-value-create (widget)
2836 ;; Insert text representing the `on' and `off' states.
2837 (let* ((val (widget-value widget))
2838 (text (widget-get widget (if val :on :off)))
2839 (img (widget-image-find
2840 (widget-get widget (if val :on-image :off-image)))))
2841 (widget-image-insert widget
2842 (if text
2843 (concat widget-push-button-prefix text
2844 widget-push-button-suffix)
2846 (if img
2847 (append img '(:ascent center))))))
2849 ;;; The `documentation-link' Widget.
2851 ;; This is a helper widget for `documentation-string'.
2853 (define-widget 'documentation-link 'link
2854 "Link type used in documentation strings."
2855 :tab-order -1
2856 :help-echo "Describe this symbol"
2857 :action 'widget-documentation-link-action)
2859 (defun widget-documentation-link-action (widget &optional event)
2860 "Display documentation for WIDGET's value. Ignore optional argument EVENT."
2861 (let* ((string (widget-get widget :value))
2862 (symbol (intern string)))
2863 (if (and (fboundp symbol) (boundp symbol))
2864 ;; If there are two doc strings, give the user a way to pick one.
2865 (apropos (concat "\\`" (regexp-quote string) "\\'"))
2866 (if (fboundp symbol)
2867 (describe-function symbol)
2868 (describe-variable symbol)))))
2870 (defcustom widget-documentation-links t
2871 "Add hyperlinks to documentation strings when non-nil."
2872 :type 'boolean
2873 :group 'widget-documentation)
2875 (defcustom widget-documentation-link-regexp "`\\([^\n`' ]+\\)'"
2876 "Regexp for matching potential links in documentation strings.
2877 The first group should be the link itself."
2878 :type 'regexp
2879 :group 'widget-documentation)
2881 (defcustom widget-documentation-link-p 'intern-soft
2882 "Predicate used to test if a string is useful as a link.
2883 The value should be a function. The function will be called with one
2884 argument, a string, and should return non-nil if there should be a
2885 link for that string."
2886 :type 'function
2887 :options '(widget-documentation-link-p)
2888 :group 'widget-documentation)
2890 (defcustom widget-documentation-link-type 'documentation-link
2891 "Widget type used for links in documentation strings."
2892 :type 'symbol
2893 :group 'widget-documentation)
2895 (defun widget-documentation-link-add (widget from to)
2896 (widget-specify-doc widget from to)
2897 (when widget-documentation-links
2898 (let ((regexp widget-documentation-link-regexp)
2899 (buttons (widget-get widget :buttons))
2900 (widget-mouse-face (default-value 'widget-mouse-face))
2901 (widget-button-face widget-documentation-face)
2902 (widget-button-pressed-face widget-documentation-face))
2903 (save-excursion
2904 (goto-char from)
2905 (while (re-search-forward regexp to t)
2906 (let ((name (match-string 1))
2907 (begin (match-beginning 1))
2908 (end (match-end 1)))
2909 (when (funcall widget-documentation-link-p name)
2910 (push (widget-convert-button widget-documentation-link-type
2911 begin end :value name)
2912 buttons)))))
2913 (widget-put widget :buttons buttons)))
2914 (let ((indent (widget-get widget :indent)))
2915 (when (and indent (not (zerop indent)))
2916 (save-excursion
2917 (save-restriction
2918 (narrow-to-region from to)
2919 (goto-char (point-min))
2920 (while (search-forward "\n" nil t)
2921 (insert-char ?\s indent)))))))
2923 ;;; The `documentation-string' Widget.
2925 (define-widget 'documentation-string 'item
2926 "A documentation string."
2927 :format "%v"
2928 :action 'widget-documentation-string-action
2929 :value-create 'widget-documentation-string-value-create
2930 :visibility-widget 'visibility)
2932 (defun widget-documentation-string-value-create (widget)
2933 ;; Insert documentation string.
2934 (let ((doc (widget-value widget))
2935 (indent (widget-get widget :indent))
2936 (shown (widget-get (widget-get widget :parent) :documentation-shown))
2937 (start (point)))
2938 (if (string-match "\n" doc)
2939 (let ((before (substring doc 0 (match-beginning 0)))
2940 (after (substring doc (match-beginning 0)))
2941 button)
2942 (when (and indent (not (zerop indent)))
2943 (insert-char ?\s indent))
2944 (insert before ?\s)
2945 (widget-documentation-link-add widget start (point))
2946 (setq button
2947 (widget-create-child-and-convert
2948 widget (widget-get widget :visibility-widget)
2949 :help-echo "Show or hide rest of the documentation."
2950 :on "Hide"
2951 :off "More"
2952 :always-active t
2953 :action 'widget-parent-action
2954 shown))
2955 (when shown
2956 (setq start (point))
2957 (when (and indent (not (zerop indent)))
2958 (insert-char ?\s indent))
2959 (insert after)
2960 (widget-documentation-link-add widget start (point)))
2961 (widget-put widget :buttons (list button)))
2962 (when (and indent (not (zerop indent)))
2963 (insert-char ?\s indent))
2964 (insert doc)
2965 (widget-documentation-link-add widget start (point))))
2966 (insert ?\n))
2968 (defun widget-documentation-string-action (widget &rest ignore)
2969 ;; Toggle documentation.
2970 (let ((parent (widget-get widget :parent)))
2971 (widget-put parent :documentation-shown
2972 (not (widget-get parent :documentation-shown))))
2973 ;; Redraw.
2974 (widget-value-set widget (widget-value widget)))
2976 (defun widget-add-documentation-string-button (widget &rest args)
2977 "Insert a new `documentation-string' widget based on WIDGET.
2978 The new widget becomes a child of WIDGET, and is also added to
2979 its `:buttons' list. The documentation string is found from
2980 WIDGET using the function `widget-docstring'.
2981 Optional ARGS specifies additional keyword arguments for the
2982 `documentation-string' widget."
2983 (let ((doc (widget-docstring widget))
2984 (indent (widget-get widget :indent))
2985 (doc-indent (widget-get widget :documentation-indent)))
2986 (when doc
2987 (and (eq (preceding-char) ?\n)
2988 indent
2989 (insert-char ?\s indent))
2990 (unless (or (numberp doc-indent) (null doc-indent))
2991 (setq doc-indent 0))
2992 (widget-put widget :buttons
2993 (cons (apply 'widget-create-child-and-convert
2994 widget 'documentation-string
2995 :indent doc-indent
2996 (nconc args (list doc)))
2997 (widget-get widget :buttons))))))
2999 ;;; The Sexp Widgets.
3001 (define-widget 'const 'item
3002 "An immutable sexp."
3003 :prompt-value 'widget-const-prompt-value
3004 :format "%t\n%d")
3006 (defun widget-const-prompt-value (widget prompt value unbound)
3007 ;; Return the value of the const.
3008 (widget-value widget))
3010 (define-widget 'function-item 'const
3011 "An immutable function name."
3012 :format "%v\n%h"
3013 :documentation-property (lambda (symbol)
3014 (condition-case nil
3015 (documentation symbol t)
3016 (error nil))))
3018 (define-widget 'variable-item 'const
3019 "An immutable variable name."
3020 :format "%v\n%h"
3021 :documentation-property 'variable-documentation)
3023 (define-widget 'other 'sexp
3024 "Matches any value, but doesn't let the user edit the value.
3025 This is useful as last item in a `choice' widget.
3026 You should use this widget type with a default value,
3027 as in (other DEFAULT) or (other :tag \"NAME\" DEFAULT).
3028 If the user selects this alternative, that specifies DEFAULT
3029 as the value."
3030 :tag "Other"
3031 :format "%t%n"
3032 :value 'other)
3034 (defvar widget-string-prompt-value-history nil
3035 "History of input to `widget-string-prompt-value'.")
3037 (define-widget 'string 'editable-field
3038 "A string"
3039 :tag "String"
3040 :format "%{%t%}: %v"
3041 :complete-function 'ispell-complete-word
3042 :prompt-history 'widget-string-prompt-value-history)
3044 (defun widget-string-complete ()
3045 "Complete contents of string field.
3046 Completions are taken from the :completion-alist property of the
3047 widget. If that isn't a list, it's evalled and expected to yield a list."
3048 (interactive)
3049 (let* ((widget widget--completing-widget)
3050 (completion-ignore-case (widget-get widget :completion-ignore-case))
3051 (alist (widget-get widget :completion-alist))
3052 (_ (unless (listp alist)
3053 (setq alist (eval alist)))))
3054 (completion-in-region (widget-field-start widget)
3055 (max (point) (widget-field-text-end widget))
3056 alist)))
3058 (define-widget 'regexp 'string
3059 "A regular expression."
3060 :match 'widget-regexp-match
3061 :validate 'widget-regexp-validate
3062 ;; Doesn't work well with terminating newline.
3063 ;; :value-face 'widget-single-line-field
3064 :tag "Regexp")
3066 (defun widget-regexp-match (widget value)
3067 ;; Match valid regexps.
3068 (and (stringp value)
3069 (condition-case nil
3070 (prog1 t
3071 (string-match value ""))
3072 (error nil))))
3074 (defun widget-regexp-validate (widget)
3075 "Check that the value of WIDGET is a valid regexp."
3076 (condition-case data
3077 (prog1 nil
3078 (string-match (widget-value widget) ""))
3079 (error (widget-put widget :error (error-message-string data))
3080 widget)))
3082 (define-widget 'file 'string
3083 "A file widget.
3084 It reads a file name from an editable text field."
3085 :complete-function 'widget-file-complete
3086 :prompt-value 'widget-file-prompt-value
3087 :format "%{%t%}: %v"
3088 ;; Doesn't work well with terminating newline.
3089 ;; :value-face 'widget-single-line-field
3090 :tag "File")
3092 (defun widget-file-complete ()
3093 "Perform completion on file name preceding point."
3094 (interactive)
3095 (let ((widget widget--completing-widget))
3096 (completion-in-region (widget-field-start widget)
3097 (max (point) (widget-field-text-end widget))
3098 'completion-file-name-table)))
3100 (defun widget-file-prompt-value (widget prompt value unbound)
3101 ;; Read file from minibuffer.
3102 (abbreviate-file-name
3103 (if unbound
3104 (read-file-name prompt)
3105 (let ((prompt2 (format "%s (default %s): " prompt value))
3106 (dir (file-name-directory value))
3107 (file (file-name-nondirectory value))
3108 (must-match (widget-get widget :must-match)))
3109 (read-file-name prompt2 dir nil must-match file)))))
3111 ;;;(defun widget-file-action (widget &optional event)
3112 ;;; ;; Read a file name from the minibuffer.
3113 ;;; (let* ((value (widget-value widget))
3114 ;;; (dir (file-name-directory value))
3115 ;;; (file (file-name-nondirectory value))
3116 ;;; (menu-tag (widget-apply widget :menu-tag-get))
3117 ;;; (must-match (widget-get widget :must-match))
3118 ;;; (answer (read-file-name (concat menu-tag " (default " value "): ")
3119 ;;; dir nil must-match file)))
3120 ;;; (widget-value-set widget (abbreviate-file-name answer))
3121 ;;; (widget-setup)
3122 ;;; (widget-apply widget :notify widget event)))
3124 ;; Fixme: use file-name-as-directory.
3125 (define-widget 'directory 'file
3126 "A directory widget.
3127 It reads a directory name from an editable text field."
3128 :tag "Directory")
3130 (defvar widget-symbol-prompt-value-history nil
3131 "History of input to `widget-symbol-prompt-value'.")
3133 (define-widget 'symbol 'editable-field
3134 "A Lisp symbol."
3135 :value nil
3136 :tag "Symbol"
3137 :format "%{%t%}: %v"
3138 :match (lambda (widget value) (symbolp value))
3139 :complete-function 'lisp-complete-symbol
3140 :prompt-internal 'widget-symbol-prompt-internal
3141 :prompt-match 'symbolp
3142 :prompt-history 'widget-symbol-prompt-value-history
3143 :value-to-internal (lambda (widget value)
3144 (if (symbolp value)
3145 (symbol-name value)
3146 value))
3147 :value-to-external (lambda (widget value)
3148 (if (stringp value)
3149 (intern value)
3150 value)))
3152 (defun widget-symbol-prompt-internal (widget prompt initial history)
3153 ;; Read file from minibuffer.
3154 (let ((answer (completing-read prompt obarray
3155 (widget-get widget :prompt-match)
3156 nil initial history)))
3157 (if (and (stringp answer)
3158 (not (zerop (length answer))))
3159 answer
3160 (error "No value"))))
3162 (defvar widget-function-prompt-value-history nil
3163 "History of input to `widget-function-prompt-value'.")
3165 (define-widget 'function 'restricted-sexp
3166 "A Lisp function."
3167 :complete-function (lambda ()
3168 (interactive)
3169 (lisp-complete-symbol 'fboundp))
3170 :prompt-value 'widget-field-prompt-value
3171 :prompt-internal 'widget-symbol-prompt-internal
3172 :prompt-match 'fboundp
3173 :prompt-history 'widget-function-prompt-value-history
3174 :action 'widget-field-action
3175 :match-alternatives '(functionp)
3176 :validate (lambda (widget)
3177 (unless (functionp (widget-value widget))
3178 (widget-put widget :error (format "Invalid function: %S"
3179 (widget-value widget)))
3180 widget))
3181 :value 'ignore
3182 :tag "Function")
3184 (defvar widget-variable-prompt-value-history nil
3185 "History of input to `widget-variable-prompt-value'.")
3187 (define-widget 'variable 'symbol
3188 "A Lisp variable."
3189 :prompt-match 'boundp
3190 :prompt-history 'widget-variable-prompt-value-history
3191 :complete-function (lambda ()
3192 (interactive)
3193 (lisp-complete-symbol 'boundp))
3194 :tag "Variable")
3196 (define-widget 'coding-system 'symbol
3197 "A MULE coding-system."
3198 :format "%{%t%}: %v"
3199 :tag "Coding system"
3200 :base-only nil
3201 :prompt-history 'coding-system-value-history
3202 :prompt-value 'widget-coding-system-prompt-value
3203 :action 'widget-coding-system-action
3204 :complete-function (lambda ()
3205 (interactive)
3206 (lisp-complete-symbol 'coding-system-p))
3207 :validate (lambda (widget)
3208 (unless (coding-system-p (widget-value widget))
3209 (widget-put widget :error (format "Invalid coding system: %S"
3210 (widget-value widget)))
3211 widget))
3212 :value 'undecided
3213 :prompt-match 'coding-system-p)
3215 (defun widget-coding-system-prompt-value (widget prompt value unbound)
3216 "Read coding-system from minibuffer."
3217 (if (widget-get widget :base-only)
3218 (intern
3219 (completing-read (format "%s (default %s): " prompt value)
3220 (mapcar #'list (coding-system-list t)) nil nil nil
3221 coding-system-history))
3222 (read-coding-system (format "%s (default %s): " prompt value) value)))
3224 (defun widget-coding-system-action (widget &optional event)
3225 (let ((answer
3226 (widget-coding-system-prompt-value
3227 widget
3228 (widget-apply widget :menu-tag-get)
3229 (widget-value widget)
3230 t)))
3231 (widget-value-set widget answer)
3232 (widget-apply widget :notify widget event)
3233 (widget-setup)))
3235 ;;; I'm not sure about what this is good for? KFS.
3236 (defvar widget-key-sequence-prompt-value-history nil
3237 "History of input to `widget-key-sequence-prompt-value'.")
3239 (defvar widget-key-sequence-default-value [ignore]
3240 "Default value for an empty key sequence.")
3242 (defvar widget-key-sequence-map
3243 (let ((map (make-sparse-keymap)))
3244 (set-keymap-parent map widget-field-keymap)
3245 (define-key map [(control ?q)] 'widget-key-sequence-read-event)
3246 map))
3248 (define-widget 'key-sequence 'restricted-sexp
3249 "A key sequence."
3250 :prompt-value 'widget-field-prompt-value
3251 :prompt-internal 'widget-symbol-prompt-internal
3252 ; :prompt-match 'fboundp ;; What was this good for? KFS
3253 :prompt-history 'widget-key-sequence-prompt-value-history
3254 :action 'widget-field-action
3255 :match-alternatives '(stringp vectorp)
3256 :format "%{%t%}: %v"
3257 :validate 'widget-key-sequence-validate
3258 :value-to-internal 'widget-key-sequence-value-to-internal
3259 :value-to-external 'widget-key-sequence-value-to-external
3260 :value widget-key-sequence-default-value
3261 :keymap widget-key-sequence-map
3262 :help-echo "C-q: insert KEY, EVENT, or CODE; RET: enter value"
3263 :tag "Key sequence")
3265 (defun widget-key-sequence-read-event (ev)
3266 (interactive (list
3267 (let ((inhibit-quit t) quit-flag)
3268 (read-event "Insert KEY, EVENT, or CODE: "))))
3269 (let ((ev2 (and (memq 'down (event-modifiers ev))
3270 (read-event)))
3271 (tr (and (keymapp function-key-map)
3272 (lookup-key function-key-map (vector ev)))))
3273 (when (and (integerp ev)
3274 (or (and (<= ?0 ev) (< ev (+ ?0 (min 10 read-quoted-char-radix))))
3275 (and (<= ?a (downcase ev))
3276 (< (downcase ev) (+ ?a -10 (min 36 read-quoted-char-radix))))))
3277 (setq unread-command-events (cons ev unread-command-events)
3278 ev (read-quoted-char (format "Enter code (radix %d)" read-quoted-char-radix))
3279 tr nil)
3280 (if (and (integerp ev) (not (characterp ev)))
3281 (insert (char-to-string ev)))) ;; throw invalid char error
3282 (setq ev (key-description (list ev)))
3283 (when (arrayp tr)
3284 (setq tr (key-description (list (aref tr 0))))
3285 (if (y-or-n-p (format "Key %s is translated to %s -- use %s? " ev tr tr))
3286 (setq ev tr ev2 nil)))
3287 (insert (if (= (char-before) ?\s) "" " ") ev " ")
3288 (if ev2
3289 (insert (key-description (list ev2)) " "))))
3291 (defun widget-key-sequence-validate (widget)
3292 (unless (or (stringp (widget-value widget))
3293 (vectorp (widget-value widget)))
3294 (widget-put widget :error (format "Invalid key sequence: %S"
3295 (widget-value widget)))
3296 widget))
3298 (defun widget-key-sequence-value-to-internal (widget value)
3299 (if (widget-apply widget :match value)
3300 (if (equal value widget-key-sequence-default-value)
3302 (key-description value))
3303 value))
3305 (defun widget-key-sequence-value-to-external (widget value)
3306 (if (stringp value)
3307 (if (string-match "\\`[[:space:]]*\\'" value)
3308 widget-key-sequence-default-value
3309 (read-kbd-macro value))
3310 value))
3313 (define-widget 'sexp 'editable-field
3314 "An arbitrary Lisp expression."
3315 :tag "Lisp expression"
3316 :format "%{%t%}: %v"
3317 :value nil
3318 :validate 'widget-sexp-validate
3319 :match (lambda (widget value) t)
3320 :value-to-internal 'widget-sexp-value-to-internal
3321 :value-to-external (lambda (widget value) (read value))
3322 :prompt-history 'widget-sexp-prompt-value-history
3323 :prompt-value 'widget-sexp-prompt-value)
3325 (defun widget-sexp-value-to-internal (widget value)
3326 ;; Use pp for printer representation.
3327 (let ((pp (if (symbolp value)
3328 (prin1-to-string value)
3329 (pp-to-string value))))
3330 (while (string-match "\n\\'" pp)
3331 (setq pp (substring pp 0 -1)))
3332 (if (or (string-match "\n\\'" pp)
3333 (> (length pp) 40))
3334 (concat "\n" pp)
3335 pp)))
3337 (defun widget-sexp-validate (widget)
3338 ;; Valid if we can read the string and there is no junk left after it.
3339 (with-temp-buffer
3340 (insert (widget-apply widget :value-get))
3341 (goto-char (point-min))
3342 (let (err)
3343 (condition-case data
3344 (progn
3345 ;; Avoid a confusing end-of-file error.
3346 (skip-syntax-forward "\\s-")
3347 (if (eobp)
3348 (setq err "Empty sexp -- use `nil'?")
3349 (unless (widget-apply widget :match (read (current-buffer)))
3350 (setq err (widget-get widget :type-error))))
3351 ;; Allow whitespace after expression.
3352 (skip-syntax-forward "\\s-")
3353 (if (and (not (eobp))
3354 (not err))
3355 (setq err (format "Junk at end of expression: %s"
3356 (buffer-substring (point)
3357 (point-max))))))
3358 (end-of-file ; Avoid confusing error message.
3359 (setq err "Unbalanced sexp"))
3360 (error (setq err (error-message-string data))))
3361 (if (not err)
3363 (widget-put widget :error err)
3364 widget))))
3366 (defvar widget-sexp-prompt-value-history nil
3367 "History of input to `widget-sexp-prompt-value'.")
3369 (defun widget-sexp-prompt-value (widget prompt value unbound)
3370 ;; Read an arbitrary sexp.
3371 (let ((found (read-string prompt
3372 (if unbound nil (cons (prin1-to-string value) 0))
3373 (widget-get widget :prompt-history))))
3374 (let ((answer (read-from-string found)))
3375 (unless (= (cdr answer) (length found))
3376 (error "Junk at end of expression: %s"
3377 (substring found (cdr answer))))
3378 (car answer))))
3380 (define-widget 'restricted-sexp 'sexp
3381 "A Lisp expression restricted to values that match.
3382 To use this type, you must define :match or :match-alternatives."
3383 :type-error "The specified value is not valid"
3384 :match 'widget-restricted-sexp-match
3385 :value-to-internal (lambda (widget value)
3386 (if (widget-apply widget :match value)
3387 (prin1-to-string value)
3388 value)))
3390 (defun widget-restricted-sexp-match (widget value)
3391 (let ((alternatives (widget-get widget :match-alternatives))
3392 matched)
3393 (while (and alternatives (not matched))
3394 (if (cond ((functionp (car alternatives))
3395 (funcall (car alternatives) value))
3396 ((and (consp (car alternatives))
3397 (eq (car (car alternatives)) 'quote))
3398 (eq value (nth 1 (car alternatives)))))
3399 (setq matched t))
3400 (setq alternatives (cdr alternatives)))
3401 matched))
3403 (define-widget 'integer 'restricted-sexp
3404 "An integer."
3405 :tag "Integer"
3406 :value 0
3407 :type-error "This field should contain an integer"
3408 :match-alternatives '(integerp))
3410 (define-widget 'number 'restricted-sexp
3411 "A number (floating point or integer)."
3412 :tag "Number"
3413 :value 0.0
3414 :type-error "This field should contain a number (floating point or integer)"
3415 :match-alternatives '(numberp))
3417 (define-widget 'float 'restricted-sexp
3418 "A floating point number."
3419 :tag "Floating point number"
3420 :value 0.0
3421 :type-error "This field should contain a floating point number"
3422 :match-alternatives '(floatp))
3424 (define-widget 'character 'editable-field
3425 "A character."
3426 :tag "Character"
3427 :value 0
3428 :size 1
3429 :format "%{%t%}: %v\n"
3430 :valid-regexp "\\`.\\'"
3431 :error "This field should contain a single character"
3432 :value-to-internal (lambda (widget value)
3433 (if (stringp value)
3434 value
3435 (char-to-string value)))
3436 :value-to-external (lambda (widget value)
3437 (if (stringp value)
3438 (aref value 0)
3439 value))
3440 :match (lambda (widget value)
3441 (characterp value)))
3443 (define-widget 'list 'group
3444 "A Lisp list."
3445 :tag "List"
3446 :format "%{%t%}:\n%v")
3448 (define-widget 'vector 'group
3449 "A Lisp vector."
3450 :tag "Vector"
3451 :format "%{%t%}:\n%v"
3452 :match 'widget-vector-match
3453 :value-to-internal (lambda (widget value) (append value nil))
3454 :value-to-external (lambda (widget value) (apply 'vector value)))
3456 (defun widget-vector-match (widget value)
3457 (and (vectorp value)
3458 (widget-group-match widget
3459 (widget-apply widget :value-to-internal value))))
3461 (define-widget 'cons 'group
3462 "A cons-cell."
3463 :tag "Cons-cell"
3464 :format "%{%t%}:\n%v"
3465 :match 'widget-cons-match
3466 :value-to-internal (lambda (widget value)
3467 (list (car value) (cdr value)))
3468 :value-to-external (lambda (widget value)
3469 (apply 'cons value)))
3471 (defun widget-cons-match (widget value)
3472 (and (consp value)
3473 (widget-group-match widget
3474 (widget-apply widget :value-to-internal value))))
3476 ;;; The `lazy' Widget.
3478 ;; Recursive datatypes.
3480 (define-widget 'lazy 'default
3481 "Base widget for recursive datastructures.
3483 The `lazy' widget will, when instantiated, contain a single inferior
3484 widget, of the widget type specified by the :type parameter. The
3485 value of the `lazy' widget is the same as the value of the inferior
3486 widget. When deriving a new widget from the 'lazy' widget, the :type
3487 parameter is allowed to refer to the widget currently being defined,
3488 thus allowing recursive datastructures to be described.
3490 The :type parameter takes the same arguments as the defcustom
3491 parameter with the same name.
3493 Most composite widgets, i.e. widgets containing other widgets, does
3494 not allow recursion. That is, when you define a new widget type, none
3495 of the inferior widgets may be of the same type you are currently
3496 defining.
3498 In Lisp, however, it is custom to define datastructures in terms of
3499 themselves. A list, for example, is defined as either nil, or a cons
3500 cell whose cdr itself is a list. The obvious way to translate this
3501 into a widget type would be
3503 (define-widget 'my-list 'choice
3504 \"A list of sexps.\"
3505 :tag \"Sexp list\"
3506 :args '((const nil) (cons :value (nil) sexp my-list)))
3508 Here we attempt to define my-list as a choice of either the constant
3509 nil, or a cons-cell containing a sexp and my-lisp. This will not work
3510 because the `choice' widget does not allow recursion.
3512 Using the `lazy' widget you can overcome this problem, as in this
3513 example:
3515 (define-widget 'sexp-list 'lazy
3516 \"A list of sexps.\"
3517 :tag \"Sexp list\"
3518 :type '(choice (const nil) (cons :value (nil) sexp sexp-list)))"
3519 :format "%{%t%}: %v"
3520 ;; We don't convert :type because we want to allow recursive
3521 ;; datastructures. This is slow, so we should not create speed
3522 ;; critical widgets by deriving from this.
3523 :convert-widget 'widget-value-convert-widget
3524 :value-create 'widget-type-value-create
3525 :value-get 'widget-child-value-get
3526 :value-inline 'widget-child-value-inline
3527 :default-get 'widget-type-default-get
3528 :match 'widget-type-match
3529 :validate 'widget-child-validate)
3532 ;;; The `plist' Widget.
3534 ;; Property lists.
3536 (define-widget 'plist 'list
3537 "A property list."
3538 :key-type '(symbol :tag "Key")
3539 :value-type '(sexp :tag "Value")
3540 :convert-widget 'widget-plist-convert-widget
3541 :tag "Plist")
3543 (defvar widget-plist-value-type) ;Dynamic variable
3545 (defun widget-plist-convert-widget (widget)
3546 ;; Handle `:options'.
3547 (let* ((options (widget-get widget :options))
3548 (widget-plist-value-type (widget-get widget :value-type))
3549 (other `(editable-list :inline t
3550 (group :inline t
3551 ,(widget-get widget :key-type)
3552 ,widget-plist-value-type)))
3553 (args (if options
3554 (list `(checklist :inline t
3555 :greedy t
3556 ,@(mapcar 'widget-plist-convert-option
3557 options))
3558 other)
3559 (list other))))
3560 (widget-put widget :args args)
3561 widget))
3563 (defun widget-plist-convert-option (option)
3564 ;; Convert a single plist option.
3565 (let (key-type value-type)
3566 (if (listp option)
3567 (let ((key (nth 0 option)))
3568 (setq value-type (nth 1 option))
3569 (if (listp key)
3570 (setq key-type key)
3571 (setq key-type `(const ,key))))
3572 (setq key-type `(const ,option)
3573 value-type widget-plist-value-type))
3574 `(group :format "Key: %v" :inline t ,key-type ,value-type)))
3577 ;;; The `alist' Widget.
3579 ;; Association lists.
3581 (define-widget 'alist 'list
3582 "An association list."
3583 :key-type '(sexp :tag "Key")
3584 :value-type '(sexp :tag "Value")
3585 :convert-widget 'widget-alist-convert-widget
3586 :tag "Alist")
3588 (defvar widget-alist-value-type) ;Dynamic variable
3590 (defun widget-alist-convert-widget (widget)
3591 ;; Handle `:options'.
3592 (let* ((options (widget-get widget :options))
3593 (widget-alist-value-type (widget-get widget :value-type))
3594 (other `(editable-list :inline t
3595 (cons :format "%v"
3596 ,(widget-get widget :key-type)
3597 ,widget-alist-value-type)))
3598 (args (if options
3599 (list `(checklist :inline t
3600 :greedy t
3601 ,@(mapcar 'widget-alist-convert-option
3602 options))
3603 other)
3604 (list other))))
3605 (widget-put widget :args args)
3606 widget))
3608 (defun widget-alist-convert-option (option)
3609 ;; Convert a single alist option.
3610 (let (key-type value-type)
3611 (if (listp option)
3612 (let ((key (nth 0 option)))
3613 (setq value-type (nth 1 option))
3614 (if (listp key)
3615 (setq key-type key)
3616 (setq key-type `(const ,key))))
3617 (setq key-type `(const ,option)
3618 value-type widget-alist-value-type))
3619 `(cons :format "Key: %v" ,key-type ,value-type)))
3621 (define-widget 'choice 'menu-choice
3622 "A union of several sexp types."
3623 :tag "Choice"
3624 :format "%{%t%}: %[Value Menu%] %v"
3625 :button-prefix 'widget-push-button-prefix
3626 :button-suffix 'widget-push-button-suffix
3627 :prompt-value 'widget-choice-prompt-value)
3629 (defun widget-choice-prompt-value (widget prompt value unbound)
3630 "Make a choice."
3631 (let ((args (widget-get widget :args))
3632 (completion-ignore-case (widget-get widget :case-fold))
3633 current choices old)
3634 ;; Find the first arg that matches VALUE.
3635 (let ((look args))
3636 (while look
3637 (if (widget-apply (car look) :match value)
3638 (setq old (car look)
3639 look nil)
3640 (setq look (cdr look)))))
3641 ;; Find new choice.
3642 (setq current
3643 (cond ((= (length args) 0)
3644 nil)
3645 ((= (length args) 1)
3646 (nth 0 args))
3647 ((and (= (length args) 2)
3648 (memq old args))
3649 (if (eq old (nth 0 args))
3650 (nth 1 args)
3651 (nth 0 args)))
3653 (while args
3654 (setq current (car args)
3655 args (cdr args))
3656 (setq choices
3657 (cons (cons (widget-apply current :menu-tag-get)
3658 current)
3659 choices)))
3660 (let ((val (completing-read prompt choices nil t)))
3661 (if (stringp val)
3662 (let ((try (try-completion val choices)))
3663 (when (stringp try)
3664 (setq val try))
3665 (cdr (assoc val choices)))
3666 nil)))))
3667 (if current
3668 (widget-prompt-value current prompt nil t)
3669 value)))
3671 (define-widget 'radio 'radio-button-choice
3672 "A union of several sexp types."
3673 :tag "Choice"
3674 :format "%{%t%}:\n%v"
3675 :prompt-value 'widget-choice-prompt-value)
3677 (define-widget 'repeat 'editable-list
3678 "A variable length homogeneous list."
3679 :tag "Repeat"
3680 :format "%{%t%}:\n%v%i\n")
3682 (define-widget 'set 'checklist
3683 "A list of members from a fixed set."
3684 :tag "Set"
3685 :format "%{%t%}:\n%v")
3687 (define-widget 'boolean 'toggle
3688 "To be nil or non-nil, that is the question."
3689 :tag "Boolean"
3690 :prompt-value 'widget-boolean-prompt-value
3691 :button-prefix 'widget-push-button-prefix
3692 :button-suffix 'widget-push-button-suffix
3693 :format "%{%t%}: %[Toggle%] %v\n"
3694 :on "on (non-nil)"
3695 :off "off (nil)")
3697 (defun widget-boolean-prompt-value (widget prompt value unbound)
3698 ;; Toggle a boolean.
3699 (y-or-n-p prompt))
3701 ;;; The `color' Widget.
3703 ;; Fixme: match
3704 (define-widget 'color 'editable-field
3705 "Choose a color name (with sample)."
3706 :format "%{%t%}: %v (%{sample%})\n"
3707 :value-create 'widget-color-value-create
3708 :size 10
3709 :tag "Color"
3710 :value "black"
3711 :complete 'widget-color-complete
3712 :sample-face-get 'widget-color-sample-face-get
3713 :notify 'widget-color-notify
3714 :action 'widget-color-action)
3716 (defun widget-color-value-create (widget)
3717 (widget-field-value-create widget)
3718 (widget-insert " ")
3719 (widget-create-child-and-convert
3720 widget 'push-button
3721 :tag " Choose " :action 'widget-color--choose-action)
3722 (widget-insert " "))
3724 (defun widget-color--choose-action (widget &optional event)
3725 (list-colors-display
3726 nil nil
3727 `(lambda (color)
3728 (when (buffer-live-p ,(current-buffer))
3729 (widget-value-set ',(widget-get widget :parent) color)
3730 (let* ((buf (get-buffer "*Colors*"))
3731 (win (get-buffer-window buf 0)))
3732 (bury-buffer buf)
3733 (and win (> (length (window-list)) 1)
3734 (delete-window win)))
3735 (pop-to-buffer ,(current-buffer))))))
3737 (defun widget-color-complete (widget)
3738 "Complete the color in WIDGET."
3739 (require 'facemenu) ; for facemenu-color-alist
3740 (completion-in-region (widget-field-start widget)
3741 (max (point) (widget-field-text-end widget))
3742 (or facemenu-color-alist
3743 (sort (defined-colors) 'string-lessp))))
3745 (defun widget-color-sample-face-get (widget)
3746 (let* ((value (condition-case nil
3747 (widget-value widget)
3748 (error (widget-get widget :value)))))
3749 (if (color-defined-p value)
3750 (list (cons 'foreground-color value))
3751 'default)))
3753 (defun widget-color-action (widget &optional event)
3754 "Prompt for a color."
3755 (let* ((tag (widget-apply widget :menu-tag-get))
3756 (prompt (concat tag ": "))
3757 (value (widget-value widget))
3758 (start (widget-field-start widget))
3759 (answer (facemenu-read-color prompt)))
3760 (unless (zerop (length answer))
3761 (widget-value-set widget answer)
3762 (widget-setup)
3763 (widget-apply widget :notify widget event))))
3765 (defun widget-color-notify (widget child &optional event)
3766 "Update the sample, and notify the parent."
3767 (overlay-put (widget-get widget :sample-overlay)
3768 'face (widget-apply widget :sample-face-get))
3769 (widget-default-notify widget child event))
3771 ;;; The Help Echo
3773 (defun widget-echo-help (pos)
3774 "Display help-echo text for widget at POS."
3775 (let* ((widget (widget-at pos))
3776 (help-echo (and widget (widget-get widget :help-echo))))
3777 (if (functionp help-echo)
3778 (setq help-echo (funcall help-echo widget)))
3779 (if help-echo (message "%s" (eval help-echo)))))
3781 ;;; The End:
3783 (provide 'wid-edit)
3785 ;; arch-tag: a076e75e-18a1-4b46-8be5-3f317bcbc707
3786 ;;; wid-edit.el ends here