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