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