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