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