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