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