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