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