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