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