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