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