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