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