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