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