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