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