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