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