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