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