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