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