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