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