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