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