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