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