(custom-variable-type): Use copy-sequence, not copy-list.
[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
bd042c03 7;; Version: 1.84
d543e20b
PA
8;; X-URL: http://www.dina.kvl.dk/~abraham/custom/
9
10;;; Commentary:
11;;
12;; See `widget.el'.
13
14;;; Code:
15
16(require 'widget)
17
18(eval-and-compile
19 (require 'cl))
20
21;;; Compatibility.
22
23(eval-and-compile
24 (autoload 'pp-to-string "pp")
25 (autoload 'Info-goto-node "info")
26
27 (when (string-match "XEmacs" emacs-version)
28 (condition-case nil
29 (require 'overlay)
30 (error (load-library "x-overlay"))))
31
32 (if (string-match "XEmacs" emacs-version)
33 ;; XEmacs spell `intangible' as `atomic'.
34 (defun widget-make-intangible (from to side)
35 "Make text between FROM and TO atomic with regard to movement.
36Third argument should be `start-open' if it should be sticky to the rear,
37and `end-open' if it should sticky to the front."
38 (require 'atomic-extents)
39 (let ((ext (make-extent from to)))
40 ;; XEmacs doesn't understant different kinds of read-only, so
41 ;; we have to use extents instead.
42 (put-text-property from to 'read-only nil)
43 (set-extent-property ext 'read-only t)
44 (set-extent-property ext 'start-open nil)
45 (set-extent-property ext 'end-open nil)
46 (set-extent-property ext side t)
47 (set-extent-property ext 'atomic t)))
48 (defun widget-make-intangible (from to size)
49 "Make text between FROM and TO intangible."
50 (put-text-property from to 'intangible 'front)))
51
52;; The following should go away when bundled with Emacs.
53 (condition-case ()
54 (require 'custom)
55 (error nil))
56
57 (unless (and (featurep 'custom) (fboundp 'custom-declare-variable))
58 ;; We have the old custom-library, hack around it!
59 (defmacro defgroup (&rest args) nil)
60 (defmacro defcustom (var value doc &rest args)
61 `(defvar ,var ,value ,doc))
62 (defmacro defface (&rest args) nil)
63 (define-widget-keywords :prefix :tag :load :link :options :type :group)
64 (when (fboundp 'copy-face)
65 (copy-face 'default 'widget-documentation-face)
66 (copy-face 'bold 'widget-button-face)
67 (copy-face 'italic 'widget-field-face)))
68
69 (unless (fboundp 'event-point)
70 ;; XEmacs function missing in Emacs.
71 (defun event-point (event)
72 "Return the character position of the given mouse-motion, button-press,
73or button-release event. If the event did not occur over a window, or did
74not occur over text, then this returns nil. Otherwise, it returns an index
75into the buffer visible in the event's window."
76 (posn-point (event-start event))))
77
78 (unless (fboundp 'error-message-string)
79 ;; Emacs function missing in XEmacs.
80 (defun error-message-string (obj)
81 "Convert an error value to an error message."
82 (let ((buf (get-buffer-create " *error-message*")))
83 (erase-buffer buf)
84 (display-error obj buf)
85 (buffer-string buf)))))
86
87;;; Customization.
88
89(defgroup widgets nil
90 "Customization support for the Widget Library."
91 :link '(custom-manual "(widget)Top")
92 :link '(url-link :tag "Development Page"
93 "http://www.dina.kvl.dk/~abraham/custom/")
94 :prefix "widget-"
95 :group 'extensions
96 :group 'faces
97 :group 'hypermedia)
98
99(defface widget-documentation-face '((((class color)
100 (background dark))
101 (:foreground "lime green"))
102 (((class color)
103 (background light))
104 (:foreground "dark green"))
105 (t nil))
106 "Face used for documentation text."
107 :group 'widgets)
108
109(defface widget-button-face '((t (:bold t)))
110 "Face used for widget buttons."
111 :group 'widgets)
112
113(defcustom widget-mouse-face 'highlight
114 "Face used for widget buttons when the mouse is above them."
115 :type 'face
116 :group 'widgets)
117
118(defface widget-field-face '((((class grayscale color)
119 (background light))
120 (:background "light gray"))
121 (((class grayscale color)
122 (background dark))
123 (:background "dark gray"))
124 (t
125 (:italic t)))
126 "Face used for editable fields."
127 :group 'widgets)
128
129(defcustom widget-menu-max-size 40
130 "Largest number of items allowed in a popup-menu.
131Larger menus are read through the minibuffer."
132 :group 'widgets
133 :type 'integer)
134
135;;; Utility functions.
136;;
137;; These are not really widget specific.
138
139(defsubst widget-plist-member (plist prop)
140 ;; Return non-nil if PLIST has the property PROP.
141 ;; PLIST is a property list, which is a list of the form
142 ;; (PROP1 VALUE1 PROP2 VALUE2 ...). PROP is a symbol.
143 ;; Unlike `plist-get', this allows you to distinguish between a missing
144 ;; property and a property with the value nil.
145 ;; The value is actually the tail of PLIST whose car is PROP.
146 (while (and plist (not (eq (car plist) prop)))
147 (setq plist (cdr (cdr plist))))
148 plist)
149
150(defun widget-princ-to-string (object)
151 ;; Return string representation of OBJECT, any Lisp object.
152 ;; No quoting characters are used; no delimiters are printed around
153 ;; the contents of strings.
154 (save-excursion
155 (set-buffer (get-buffer-create " *widget-tmp*"))
156 (erase-buffer)
157 (let ((standard-output (current-buffer)))
158 (princ object))
159 (buffer-string)))
160
161(defun widget-clear-undo ()
162 "Clear all undo information."
163 (buffer-disable-undo (current-buffer))
164 (buffer-enable-undo))
165
166(defun widget-choose (title items &optional event)
167 "Choose an item from a list.
168
169First argument TITLE is the name of the list.
170Second argument ITEMS is an alist (NAME . VALUE).
171Optional third argument EVENT is an input event.
172
173The user is asked to choose between each NAME from the items alist,
174and the VALUE of the chosen element will be returned. If EVENT is a
175mouse event, and the number of elements in items is less than
176`widget-menu-max-size', a popup menu will be used, otherwise the
177minibuffer."
178 (cond ((and (< (length items) widget-menu-max-size)
179 event (fboundp 'x-popup-menu) window-system)
180 ;; We are in Emacs-19, pressed by the mouse
181 (x-popup-menu event
182 (list title (cons "" items))))
183 ((and (< (length items) widget-menu-max-size)
184 event (fboundp 'popup-menu) window-system)
185 ;; We are in XEmacs, pressed by the mouse
186 (let ((val (get-popup-menu-response
187 (cons title
188 (mapcar
189 (function
190 (lambda (x)
191 (vector (car x) (list (car x)) t)))
192 items)))))
193 (setq val (and val
194 (listp (event-object val))
195 (stringp (car-safe (event-object val)))
196 (car (event-object val))))
197 (cdr (assoc val items))))
198 (t
199 (let ((val (completing-read (concat title ": ") items nil t)))
200 (if (stringp val)
201 (let ((try (try-completion val items)))
202 (when (stringp try)
203 (setq val try))
204 (cdr (assoc val items)))
205 nil)))))
206
207(defun widget-get-sibling (widget)
208 "Get the item WIDGET is assumed to toggle.
209This is only meaningful for radio buttons or checkboxes in a list."
210 (let* ((parent (widget-get widget :parent))
211 (children (widget-get parent :children))
212 child)
213 (catch 'child
214 (while children
215 (setq child (car children)
216 children (cdr children))
217 (when (eq (widget-get child :button) widget)
218 (throw 'child child)))
219 nil)))
220
221;;; Widget text specifications.
222;;
223;; These functions are for specifying text properties.
224
225(defun widget-specify-none (from to)
226 ;; Clear all text properties between FROM and TO.
227 (set-text-properties from to nil))
228
229(defun widget-specify-text (from to)
230 ;; Default properties.
231 (add-text-properties from to (list 'read-only t
232 'front-sticky t
233 'start-open t
234 'end-open t
235 'rear-nonsticky nil)))
236
237(defun widget-specify-field (widget from to)
238 ;; Specify editable button for WIDGET between FROM and TO.
239 (widget-specify-field-update widget from to)
240
241 ;; Make it possible to edit the front end of the field.
242 (add-text-properties (1- from) from (list 'rear-nonsticky t
243 'end-open t
244 'invisible t))
245 (when (or (string-match "\\(.\\|\n\\)%v" (widget-get widget :format))
246 (widget-get widget :hide-front-space))
247 ;; WARNING: This is going to lose horrible if the character just
248 ;; before the field can be modified (e.g. if it belongs to a
249 ;; choice widget). We try to compensate by checking the format
250 ;; string, and hope the user hasn't changed the :create method.
251 (widget-make-intangible (- from 2) from 'end-open))
252
253 ;; Make it possible to edit back end of the field.
254 (add-text-properties to (1+ to) (list 'front-sticky nil
255 'read-only t
256 'start-open t))
257
258 (cond ((widget-get widget :size)
259 (put-text-property to (1+ to) 'invisible t)
260 (when (or (string-match "%v\\(.\\|\n\\)" (widget-get widget :format))
261 (widget-get widget :hide-rear-space))
262 ;; WARNING: This is going to lose horrible if the character just
263 ;; after the field can be modified (e.g. if it belongs to a
264 ;; choice widget). We try to compensate by checking the format
265 ;; string, and hope the user hasn't changed the :create method.
266 (widget-make-intangible to (+ to 2) 'start-open)))
267 ((string-match "XEmacs" emacs-version)
268 ;; XEmacs does not allow you to insert before a read-only
269 ;; character, even if it is start.open.
270 ;; XEmacs does allow you to delete an read-only extent, so
271 ;; making the terminating newline read only doesn't help.
272 ;; I tried putting an invisible intangible read-only space
273 ;; before the newline, which gave really weird effects.
274 ;; So for now, we just have trust the user not to delete the
275 ;; newline.
276 (put-text-property to (1+ to) 'read-only nil))))
277
278(defun widget-specify-field-update (widget from to)
279 ;; Specify editable button for WIDGET between FROM and TO.
280 (let ((map (widget-get widget :keymap))
281 (secret (widget-get widget :secret))
282 (secret-to to)
283 (size (widget-get widget :size))
284 (face (or (widget-get widget :value-face)
285 'widget-field-face))
286 (help-echo (widget-get widget :help-echo))
287 (help-property (if (featurep 'balloon-help)
288 'balloon-help
289 'help-echo)))
290 (unless (or (stringp help-echo) (null help-echo))
291 (setq help-echo 'widget-mouse-help))
292
293 (when secret
294 (while (and size
295 (not (zerop size))
296 (> secret-to from)
297 (eq (char-after (1- secret-to)) ?\ ))
298 (setq secret-to (1- secret-to)))
299
300 (save-excursion
301 (goto-char from)
302 (while (< (point) secret-to)
303 (let ((old (get-text-property (point) 'secret)))
304 (when old
305 (subst-char-in-region (point) (1+ (point)) secret old)))
306 (forward-char))))
307
308 (set-text-properties from to (list 'field widget
309 'read-only nil
310 'keymap map
311 'local-map map
312 help-property help-echo
313 'face face))
314
315 (when secret
316 (save-excursion
317 (goto-char from)
318 (while (< (point) secret-to)
319 (let ((old (following-char)))
320 (subst-char-in-region (point) (1+ (point)) old secret)
321 (put-text-property (point) (1+ (point)) 'secret old))
322 (forward-char))))
323
324 (unless (widget-get widget :size)
325 (add-text-properties to (1+ to) (list 'field widget
326 help-property help-echo
327 'face face)))
328 (add-text-properties to (1+ to) (list 'local-map map
329 'keymap map))))
330
331(defun widget-specify-button (widget from to)
332 ;; Specify button for WIDGET between FROM and TO.
333 (let ((face (widget-apply widget :button-face-get))
334 (help-echo (widget-get widget :help-echo))
335 (help-property (if (featurep 'balloon-help)
336 'balloon-help
337 'help-echo)))
338 (unless (or (null help-echo) (stringp help-echo))
339 (setq help-echo 'widget-mouse-help))
340 (add-text-properties from to (list 'button widget
341 'mouse-face widget-mouse-face
342 'start-open t
343 'end-open t
344 help-property help-echo
345 'face face))))
346
347(defun widget-mouse-help (extent)
348 "Find mouse help string for button in extent."
349 (let* ((widget (widget-at (extent-start-position extent)))
350 (help-echo (and widget (widget-get widget :help-echo))))
351 (cond ((stringp help-echo)
352 help-echo)
353 ((and (symbolp help-echo) (fboundp help-echo)
354 (stringp (setq help-echo (funcall help-echo widget))))
355 help-echo)
356 (t
357 (format "(widget %S :help-echo %S)" widget help-echo)))))
358
359(defun widget-specify-sample (widget from to)
360 ;; Specify sample for WIDGET between FROM and TO.
361 (let ((face (widget-apply widget :sample-face-get)))
362 (when face
363 (add-text-properties from to (list 'start-open t
364 'end-open t
365 'face face)))))
366
367(defun widget-specify-doc (widget from to)
368 ;; Specify documentation for WIDGET between FROM and TO.
369 (add-text-properties from to (list 'widget-doc widget
370 'face 'widget-documentation-face)))
371
372(defmacro widget-specify-insert (&rest form)
373 ;; Execute FORM without inheriting any text properties.
374 `(save-restriction
375 (let ((inhibit-read-only t)
376 result
377 after-change-functions)
378 (insert "<>")
379 (narrow-to-region (- (point) 2) (point))
380 (widget-specify-none (point-min) (point-max))
381 (goto-char (1+ (point-min)))
382 (setq result (progn ,@form))
383 (delete-region (point-min) (1+ (point-min)))
384 (delete-region (1- (point-max)) (point-max))
385 (goto-char (point-max))
386 result)))
387
388(defface widget-inactive-face '((((class grayscale color)
389 (background dark))
390 (:foreground "light gray"))
391 (((class grayscale color)
392 (background light))
393 (:foreground "dark gray"))
394 (t
395 (:italic t)))
396 "Face used for inactive widgets."
397 :group 'widgets)
398
399(defun widget-specify-inactive (widget from to)
400 "Make WIDGET inactive for user modifications."
401 (unless (widget-get widget :inactive)
402 (let ((overlay (make-overlay from to nil t nil)))
403 (overlay-put overlay 'face 'widget-inactive-face)
404 (overlay-put overlay 'evaporate 't)
405 (overlay-put overlay (if (string-match "XEmacs" emacs-version)
406 'read-only
407 'modification-hooks) '(widget-overlay-inactive))
408 (widget-put widget :inactive overlay))))
409
410(defun widget-overlay-inactive (&rest junk)
411 "Ignoring the arguments, signal an error."
412 (unless inhibit-read-only
413 (error "Attempt to modify inactive widget")))
414
415
416(defun widget-specify-active (widget)
417 "Make WIDGET active for user modifications."
418 (let ((inactive (widget-get widget :inactive)))
419 (when inactive
420 (delete-overlay inactive)
421 (widget-put widget :inactive nil))))
422
423;;; Widget Properties.
424
425(defsubst widget-type (widget)
426 "Return the type of WIDGET, a symbol."
427 (car widget))
428
429(defun widget-put (widget property value)
430 "In WIDGET set PROPERTY to VALUE.
431The value can later be retrived with `widget-get'."
432 (setcdr widget (plist-put (cdr widget) property value)))
433
434(defun widget-get (widget property)
435 "In WIDGET, get the value of PROPERTY.
436The value could either be specified when the widget was created, or
437later with `widget-put'."
438 (let ((missing t)
439 value tmp)
440 (while missing
441 (cond ((setq tmp (widget-plist-member (cdr widget) property))
442 (setq value (car (cdr tmp))
443 missing nil))
444 ((setq tmp (car widget))
445 (setq widget (get tmp 'widget-type)))
446 (t
447 (setq missing nil))))
448 value))
449
450(defun widget-member (widget property)
451 "Non-nil iff there is a definition in WIDGET for PROPERTY."
452 (cond ((widget-plist-member (cdr widget) property)
453 t)
454 ((car widget)
455 (widget-member (get (car widget) 'widget-type) property))
456 (t nil)))
457
458;;;###autoload
459(defun widget-apply (widget property &rest args)
460 "Apply the value of WIDGET's PROPERTY to the widget itself.
461ARGS are passed as extra arguments to the function."
462 (apply (widget-get widget property) widget args))
463
464(defun widget-value (widget)
465 "Extract the current value of WIDGET."
466 (widget-apply widget
467 :value-to-external (widget-apply widget :value-get)))
468
469(defun widget-value-set (widget value)
470 "Set the current value of WIDGET to VALUE."
471 (widget-apply widget
472 :value-set (widget-apply widget
473 :value-to-internal value)))
474
475(defun widget-match-inline (widget vals)
476 ;; In WIDGET, match the start of VALS.
477 (cond ((widget-get widget :inline)
478 (widget-apply widget :match-inline vals))
479 ((and vals
480 (widget-apply widget :match (car vals)))
481 (cons (list (car vals)) (cdr vals)))
482 (t nil)))
483
484(defun widget-apply-action (widget &optional event)
485 "Apply :action in WIDGET in response to EVENT."
486 (if (widget-apply widget :active)
487 (widget-apply widget :action event)
488 (error "Attempt to perform action on inactive widget")))
489
490;;; Glyphs.
491
492(defcustom widget-glyph-directory (concat data-directory "custom/")
493 "Where widget glyphs are located.
494If this variable is nil, widget will try to locate the directory
495automatically. This does not work yet."
496 :group 'widgets
497 :type 'directory)
498
499(defcustom widget-glyph-enable t
500 "If non nil, use glyphs in images when available."
501 :group 'widgets
502 :type 'boolean)
503
504(defun widget-glyph-insert (widget tag image)
505 "In WIDGET, insert the text TAG or, if supported, IMAGE.
506IMAGE should either be a glyph, or a name sans extension of an xpm or
507xbm file located in `widget-glyph-directory'.
508
509WARNING: If you call this with a glyph, and you want the user to be
510able to activate the glyph, make sure it is unique. If you use the
511same glyph for multiple widgets, activating any of the glyphs will
512cause the last created widget to be activated."
513 (cond ((not (and (string-match "XEmacs" emacs-version)
514 widget-glyph-enable
515 (fboundp 'make-glyph)
516 image))
517 ;; We don't want or can't use glyphs.
518 (insert tag))
519 ((and (fboundp 'glyphp)
520 (glyphp image))
521 ;; Already a glyph. Insert it.
522 (widget-glyph-insert-glyph widget tag image))
523 (t
524 ;; A string. Look it up in.
525 (let ((file (concat widget-glyph-directory
526 (if (string-match "/\\'" widget-glyph-directory)
527 ""
528 "/")
529 image
530 (if (featurep 'xpm) ".xpm" ".xbm"))))
531 (if (file-readable-p file)
532 (widget-glyph-insert-glyph widget tag (make-glyph file))
533 ;; File not readable, give up.
534 (insert tag))))))
535
536(defun widget-glyph-insert-glyph (widget tag glyph)
537 "In WIDGET, with alternative text TAG, insert GLYPH."
538 (set-glyph-image glyph (cons 'tty tag))
539 (set-glyph-property glyph 'widget widget)
540 (insert "*")
541 (add-text-properties (1- (point)) (point)
542 (list 'invisible t
543 'end-glyph glyph))
544 (let ((help-echo (widget-get widget :help-echo)))
545 (when help-echo
546 (let ((extent (extent-at (1- (point)) nil 'end-glyph))
547 (help-property (if (featurep 'balloon-help)
548 'balloon-help
549 'help-echo)))
550 (set-extent-property extent help-property (if (stringp help-echo)
551 help-echo
552 'widget-mouse-help))))))
553
554;;; Creating Widgets.
555
556;;;###autoload
557(defun widget-create (type &rest args)
558 "Create widget of TYPE.
559The optional ARGS are additional keyword arguments."
560 (let ((widget (apply 'widget-convert type args)))
561 (widget-apply widget :create)
562 widget))
563
564(defun widget-create-child-and-convert (parent type &rest args)
565 "As part of the widget PARENT, create a child widget TYPE.
566The child is converted, using the keyword arguments ARGS."
567 (let ((widget (apply 'widget-convert type args)))
568 (widget-put widget :parent parent)
569 (unless (widget-get widget :indent)
570 (widget-put widget :indent (+ (or (widget-get parent :indent) 0)
571 (or (widget-get widget :extra-offset) 0)
572 (widget-get parent :offset))))
573 (widget-apply widget :create)
574 widget))
575
576(defun widget-create-child (parent type)
577 "Create widget of TYPE."
578 (let ((widget (copy-list type)))
579 (widget-put widget :parent parent)
580 (unless (widget-get widget :indent)
581 (widget-put widget :indent (+ (or (widget-get parent :indent) 0)
582 (or (widget-get widget :extra-offset) 0)
583 (widget-get parent :offset))))
584 (widget-apply widget :create)
585 widget))
586
587(defun widget-create-child-value (parent type value)
588 "Create widget of TYPE with value VALUE."
589 (let ((widget (copy-list type)))
590 (widget-put widget :value (widget-apply widget :value-to-internal value))
591 (widget-put widget :parent parent)
592 (unless (widget-get widget :indent)
593 (widget-put widget :indent (+ (or (widget-get parent :indent) 0)
594 (or (widget-get widget :extra-offset) 0)
595 (widget-get parent :offset))))
596 (widget-apply widget :create)
597 widget))
598
599;;;###autoload
600(defun widget-delete (widget)
601 "Delete WIDGET."
602 (widget-apply widget :delete))
603
604(defun widget-convert (type &rest args)
605 "Convert TYPE to a widget without inserting it in the buffer.
606The optional ARGS are additional keyword arguments."
607 ;; Don't touch the type.
608 (let* ((widget (if (symbolp type)
609 (list type)
610 (copy-list type)))
611 (current widget)
612 (keys args))
613 ;; First set the :args keyword.
614 (while (cdr current) ;Look in the type.
615 (let ((next (car (cdr current))))
616 (if (and (symbolp next) (eq (aref (symbol-name next) 0) ?:))
617 (setq current (cdr (cdr current)))
618 (setcdr current (list :args (cdr current)))
619 (setq current nil))))
620 (while args ;Look in the args.
621 (let ((next (nth 0 args)))
622 (if (and (symbolp next) (eq (aref (symbol-name next) 0) ?:))
623 (setq args (nthcdr 2 args))
624 (widget-put widget :args args)
625 (setq args nil))))
626 ;; Then Convert the widget.
627 (setq type widget)
628 (while type
629 (let ((convert-widget (plist-get (cdr type) :convert-widget)))
630 (if convert-widget
631 (setq widget (funcall convert-widget widget))))
632 (setq type (get (car type) 'widget-type)))
633 ;; Finally set the keyword args.
634 (while keys
635 (let ((next (nth 0 keys)))
636 (if (and (symbolp next) (eq (aref (symbol-name next) 0) ?:))
637 (progn
638 (widget-put widget next (nth 1 keys))
639 (setq keys (nthcdr 2 keys)))
640 (setq keys nil))))
641 ;; Convert the :value to internal format.
642 (if (widget-member widget :value)
643 (let ((value (widget-get widget :value)))
644 (widget-put widget
645 :value (widget-apply widget :value-to-internal value))))
646 ;; Return the newly create widget.
647 widget))
648
649(defun widget-insert (&rest args)
650 "Call `insert' with ARGS and make the text read only."
651 (let ((inhibit-read-only t)
652 after-change-functions
653 (from (point)))
654 (apply 'insert args)
655 (widget-specify-text from (point))))
656
657;;; Keymap and Commands.
658
659(defvar widget-keymap nil
660 "Keymap containing useful binding for buffers containing widgets.
661Recommended as a parent keymap for modes using widgets.")
662
663(unless widget-keymap
664 (setq widget-keymap (make-sparse-keymap))
665 (define-key widget-keymap "\C-k" 'widget-kill-line)
666 (define-key widget-keymap "\t" 'widget-forward)
667 (define-key widget-keymap "\M-\t" 'widget-backward)
668 (define-key widget-keymap [(shift tab)] 'widget-backward)
669 (define-key widget-keymap [backtab] 'widget-backward)
670 (if (string-match "XEmacs" (emacs-version))
671 (progn
672 (define-key widget-keymap [button2] 'widget-button-click)
673 (define-key widget-keymap [button1] 'widget-button1-click))
674 (define-key widget-keymap [mouse-2] 'ignore)
675 (define-key widget-keymap [down-mouse-2] 'widget-button-click))
676 (define-key widget-keymap "\C-m" 'widget-button-press))
677
678(defvar widget-global-map global-map
679 "Keymap used for events the widget does not handle themselves.")
680(make-variable-buffer-local 'widget-global-map)
681
682(defvar widget-field-keymap nil
683 "Keymap used inside an editable field.")
684
685(unless widget-field-keymap
686 (setq widget-field-keymap (copy-keymap widget-keymap))
687 (unless (string-match "XEmacs" (emacs-version))
688 (define-key widget-field-keymap [menu-bar] 'nil))
689 (define-key widget-field-keymap "\C-m" 'widget-field-activate)
690 (define-key widget-field-keymap "\C-a" 'widget-beginning-of-line)
691 (define-key widget-field-keymap "\C-e" 'widget-end-of-line)
692 (set-keymap-parent widget-field-keymap global-map))
693
694(defvar widget-text-keymap nil
695 "Keymap used inside a text field.")
696
697(unless widget-text-keymap
698 (setq widget-text-keymap (copy-keymap widget-keymap))
699 (unless (string-match "XEmacs" (emacs-version))
700 (define-key widget-text-keymap [menu-bar] 'nil))
701 (define-key widget-text-keymap "\C-a" 'widget-beginning-of-line)
702 (define-key widget-text-keymap "\C-e" 'widget-end-of-line)
703 (set-keymap-parent widget-text-keymap global-map))
704
705(defun widget-field-activate (pos &optional event)
706 "Activate the ediable field at point."
707 (interactive "@d")
708 (let ((field (get-text-property pos 'field)))
709 (if field
710 (widget-apply-action field event)
711 (call-interactively
712 (lookup-key widget-global-map (this-command-keys))))))
713
714(defun widget-button-click (event)
715 "Activate button below mouse pointer."
716 (interactive "@e")
717 (cond ((and (fboundp 'event-glyph)
718 (event-glyph event))
719 (let ((widget (glyph-property (event-glyph event) 'widget)))
720 (if widget
721 (widget-apply-action widget event)
722 (message "You clicked on a glyph."))))
723 ((event-point event)
724 (let ((button (get-text-property (event-point event) 'button)))
725 (if button
726 (widget-apply-action button event)
727 (call-interactively
728 (or (lookup-key widget-global-map [ button2 ])
729 (lookup-key widget-global-map [ down-mouse-2 ])
730 (lookup-key widget-global-map [ mouse-2]))))))
731 (t
732 (message "You clicked somewhere weird."))))
733
734(defun widget-button1-click (event)
735 "Activate glyph below mouse pointer."
736 (interactive "@e")
737 (if (and (fboundp 'event-glyph)
738 (event-glyph event))
739 (let ((widget (glyph-property (event-glyph event) 'widget)))
740 (if widget
741 (widget-apply-action widget event)
742 (message "You clicked on a glyph.")))
743 (call-interactively (lookup-key widget-global-map (this-command-keys)))))
744
745(defun widget-button-press (pos &optional event)
746 "Activate button at POS."
747 (interactive "@d")
748 (let ((button (get-text-property pos 'button)))
749 (if button
750 (widget-apply-action button event)
751 (let ((command (lookup-key widget-global-map (this-command-keys))))
752 (when (commandp command)
753 (call-interactively command))))))
754
755(defun widget-move (arg)
756 "Move point to the ARG next field or button.
757ARG may be negative to move backward."
758 (while (> arg 0)
759 (setq arg (1- arg))
760 (let ((next (cond ((get-text-property (point) 'button)
761 (next-single-property-change (point) 'button))
762 ((get-text-property (point) 'field)
763 (next-single-property-change (point) 'field))
764 (t
765 (point)))))
766 (if (null next) ; Widget extends to end. of buffer
767 (setq next (point-min)))
768 (let ((button (next-single-property-change next 'button))
769 (field (next-single-property-change next 'field)))
770 (cond ((or (get-text-property next 'button)
771 (get-text-property next 'field))
772 (goto-char next))
773 ((and button field)
774 (goto-char (min button field)))
775 (button (goto-char button))
776 (field (goto-char field))
777 (t
778 (let ((button (next-single-property-change (point-min) 'button))
779 (field (next-single-property-change (point-min) 'field)))
780 (cond ((and button field) (goto-char (min button field)))
781 (button (goto-char button))
782 (field (goto-char field))
783 (t
784 (error "No buttons or fields found"))))))
785 (setq button (widget-at (point)))
786 (if (and button (widget-get button :tab-order)
787 (< (widget-get button :tab-order) 0))
788 (setq arg (1+ arg))))))
789 (while (< arg 0)
790 (if (= (point-min) (point))
791 (forward-char 1))
792 (setq arg (1+ arg))
793 (let ((previous (cond ((get-text-property (1- (point)) 'button)
794 (previous-single-property-change (point) 'button))
795 ((get-text-property (1- (point)) 'field)
796 (previous-single-property-change (point) 'field))
797 (t
798 (point)))))
799 (if (null previous) ; Widget extends to beg. of buffer
800 (setq previous (point-max)))
801 (let ((button (previous-single-property-change previous 'button))
802 (field (previous-single-property-change previous 'field)))
803 (cond ((and button field)
804 (goto-char (max button field)))
805 (button (goto-char button))
806 (field (goto-char field))
807 (t
808 (let ((button (previous-single-property-change
809 (point-max) 'button))
810 (field (previous-single-property-change
811 (point-max) 'field)))
812 (cond ((and button field) (goto-char (max button field)))
813 (button (goto-char button))
814 (field (goto-char field))
815 (t
816 (error "No buttons or fields found"))))))))
817 (let ((button (previous-single-property-change (point) 'button))
818 (field (previous-single-property-change (point) 'field)))
819 (cond ((and button field)
820 (goto-char (max button field)))
821 (button (goto-char button))
822 (field (goto-char field)))
823 (setq button (widget-at (point)))
824 (if (and button (widget-get button :tab-order)
825 (< (widget-get button :tab-order) 0))
826 (setq arg (1- arg)))))
827 (widget-echo-help (point))
828 (run-hooks 'widget-move-hook))
829
830(defun widget-forward (arg)
831 "Move point to the next field or button.
832With optional ARG, move across that many fields."
833 (interactive "p")
834 (run-hooks 'widget-forward-hook)
835 (widget-move arg))
836
837(defun widget-backward (arg)
838 "Move point to the previous field or button.
839With optional ARG, move across that many fields."
840 (interactive "p")
841 (run-hooks 'widget-backward-hook)
842 (widget-move (- arg)))
843
844(defun widget-beginning-of-line ()
845 "Go to beginning of field or beginning of line, whichever is first."
846 (interactive)
847 (let ((bol (save-excursion (beginning-of-line) (point)))
848 (prev (previous-single-property-change (point) 'field)))
849 (goto-char (max bol (or prev bol)))))
850
851(defun widget-end-of-line ()
852 "Go to end of field or end of line, whichever is first."
853 (interactive)
854 (let ((bol (save-excursion (end-of-line) (point)))
855 (prev (next-single-property-change (point) 'field)))
856 (goto-char (min bol (or prev bol)))))
857
858(defun widget-kill-line ()
859 "Kill to end of field or end of line, whichever is first."
860 (interactive)
861 (let ((field (get-text-property (point) 'field))
862 (newline (save-excursion (search-forward "\n")))
863 (next (next-single-property-change (point) 'field)))
864 (if (and field (> newline next))
865 (kill-region (point) next)
866 (call-interactively 'kill-line))))
867
868;;; Setting up the buffer.
869
870(defvar widget-field-new nil)
871;; List of all newly created editable fields in the buffer.
872(make-variable-buffer-local 'widget-field-new)
873
874(defvar widget-field-list nil)
875;; List of all editable fields in the buffer.
876(make-variable-buffer-local 'widget-field-list)
877
878(defun widget-setup ()
879 "Setup current buffer so editing string widgets works."
880 (let ((inhibit-read-only t)
881 (after-change-functions nil)
882 field)
883 (while widget-field-new
884 (setq field (car widget-field-new)
885 widget-field-new (cdr widget-field-new)
886 widget-field-list (cons field widget-field-list))
887 (let ((from (widget-get field :value-from))
888 (to (widget-get field :value-to)))
889 (widget-specify-field field from to)
890 (move-marker from (1- from))
891 (move-marker to (1+ to)))))
892 (widget-clear-undo)
893 ;; We need to maintain text properties and size of the editing fields.
894 (make-local-variable 'after-change-functions)
895 (if widget-field-list
896 (setq after-change-functions '(widget-after-change))
897 (setq after-change-functions nil)))
898
899(defvar widget-field-last nil)
900;; Last field containing point.
901(make-variable-buffer-local 'widget-field-last)
902
903(defvar widget-field-was nil)
904;; The widget data before the change.
905(make-variable-buffer-local 'widget-field-was)
906
907(defun widget-field-find (pos)
908 ;; Find widget whose editing field is located at POS.
909 ;; Return nil if POS is not inside and editing field.
910 ;;
911 ;; This is only used in `widget-field-modified', since ordinarily
912 ;; you would just test the field property.
913 (let ((fields widget-field-list)
914 field found)
915 (while fields
916 (setq field (car fields)
917 fields (cdr fields))
918 (let ((from (widget-get field :value-from))
919 (to (widget-get field :value-to)))
920 (if (and from to (< from pos) (> to pos))
921 (setq fields nil
922 found field))))
923 found))
924
925(defun widget-after-change (from to old)
926 ;; Adjust field size and text properties.
927 (condition-case nil
928 (let ((field (widget-field-find from))
929 (inhibit-read-only t))
930 (cond ((null field))
931 ((not (eq field (widget-field-find to)))
932 (debug)
933 (message "Error: `widget-after-change' called on two fields"))
934 (t
935 (let ((size (widget-get field :size)))
936 (if size
937 (let ((begin (1+ (widget-get field :value-from)))
938 (end (1- (widget-get field :value-to))))
939 (widget-specify-field-update field begin end)
940 (cond ((< (- end begin) size)
941 ;; Field too small.
942 (save-excursion
943 (goto-char end)
944 (insert-char ?\ (- (+ begin size) end))
945 (widget-specify-field-update field
946 begin
947 (+ begin size))))
948 ((> (- end begin) size)
949 ;; Field too large and
950 (if (or (< (point) (+ begin size))
951 (> (point) end))
952 ;; Point is outside extra space.
953 (setq begin (+ begin size))
954 ;; Point is within the extra space.
955 (setq begin (point)))
956 (save-excursion
957 (goto-char end)
958 (while (and (eq (preceding-char) ?\ )
959 (> (point) begin))
960 (delete-backward-char 1))))))
961 (widget-specify-field-update field from to)))
962 (widget-apply field :notify field))))
963 (error (debug))))
964
965;;; Widget Functions
966;;
967;; These functions are used in the definition of multiple widgets.
968
969(defun widget-children-value-delete (widget)
970 "Delete all :children and :buttons in WIDGET."
971 (mapcar 'widget-delete (widget-get widget :children))
972 (widget-put widget :children nil)
973 (mapcar 'widget-delete (widget-get widget :buttons))
974 (widget-put widget :buttons nil))
975
976(defun widget-types-convert-widget (widget)
977 "Convert :args as widget types in WIDGET."
978 (widget-put widget :args (mapcar 'widget-convert (widget-get widget :args)))
979 widget)
980
981;;; The `default' Widget.
982
983(define-widget 'default nil
984 "Basic widget other widgets are derived from."
985 :value-to-internal (lambda (widget value) value)
986 :value-to-external (lambda (widget value) value)
987 :create 'widget-default-create
988 :indent nil
989 :offset 0
990 :format-handler 'widget-default-format-handler
991 :button-face-get 'widget-default-button-face-get
992 :sample-face-get 'widget-default-sample-face-get
993 :delete 'widget-default-delete
994 :value-set 'widget-default-value-set
995 :value-inline 'widget-default-value-inline
996 :menu-tag-get 'widget-default-menu-tag-get
997 :validate (lambda (widget) nil)
998 :active 'widget-default-active
999 :activate 'widget-specify-active
1000 :deactivate 'widget-default-deactivate
1001 :action 'widget-default-action
1002 :notify 'widget-default-notify)
1003
1004(defun widget-default-create (widget)
1005 "Create WIDGET at point in the current buffer."
1006 (widget-specify-insert
1007 (let ((from (point))
1008 (tag (widget-get widget :tag))
1009 (glyph (widget-get widget :tag-glyph))
1010 (doc (widget-get widget :doc))
1011 button-begin button-end
1012 sample-begin sample-end
1013 doc-begin doc-end
1014 value-pos)
1015 (insert (widget-get widget :format))
1016 (goto-char from)
1017 ;; Parse escapes in format.
1018 (while (re-search-forward "%\\(.\\)" nil t)
1019 (let ((escape (aref (match-string 1) 0)))
1020 (replace-match "" t t)
1021 (cond ((eq escape ?%)
1022 (insert "%"))
1023 ((eq escape ?\[)
1024 (setq button-begin (point)))
1025 ((eq escape ?\])
1026 (setq button-end (point)))
1027 ((eq escape ?\{)
1028 (setq sample-begin (point)))
1029 ((eq escape ?\})
1030 (setq sample-end (point)))
1031 ((eq escape ?n)
1032 (when (widget-get widget :indent)
1033 (insert "\n")
1034 (insert-char ? (widget-get widget :indent))))
1035 ((eq escape ?t)
1036 (cond (glyph
1037 (widget-glyph-insert widget (or tag "image") glyph))
1038 (tag
1039 (insert tag))
1040 (t
1041 (let ((standard-output (current-buffer)))
1042 (princ (widget-get widget :value))))))
1043 ((eq escape ?d)
1044 (when doc
1045 (setq doc-begin (point))
1046 (insert doc)
1047 (while (eq (preceding-char) ?\n)
1048 (delete-backward-char 1))
1049 (insert "\n")
1050 (setq doc-end (point))))
1051 ((eq escape ?v)
1052 (if (and button-begin (not button-end))
1053 (widget-apply widget :value-create)
1054 (setq value-pos (point))))
1055 (t
1056 (widget-apply widget :format-handler escape)))))
1057 ;; Specify button, sample, and doc, and insert value.
1058 (and button-begin button-end
1059 (widget-specify-button widget button-begin button-end))
1060 (and sample-begin sample-end
1061 (widget-specify-sample widget sample-begin sample-end))
1062 (and doc-begin doc-end
1063 (widget-specify-doc widget doc-begin doc-end))
1064 (when value-pos
1065 (goto-char value-pos)
1066 (widget-apply widget :value-create)))
1067 (let ((from (copy-marker (point-min)))
1068 (to (copy-marker (point-max))))
1069 (widget-specify-text from to)
1070 (set-marker-insertion-type from t)
1071 (set-marker-insertion-type to nil)
1072 (widget-put widget :from from)
1073 (widget-put widget :to to))))
1074
1075(defun widget-default-format-handler (widget escape)
1076 ;; We recognize the %h escape by default.
1077 (let* ((buttons (widget-get widget :buttons))
1078 (doc-property (widget-get widget :documentation-property))
1079 (doc-try (cond ((widget-get widget :doc))
1080 ((symbolp doc-property)
1081 (documentation-property (widget-get widget :value)
1082 doc-property))
1083 (t
1084 (funcall doc-property (widget-get widget :value)))))
1085 (doc-text (and (stringp doc-try)
1086 (> (length doc-try) 1)
1087 doc-try)))
1088 (cond ((eq escape ?h)
1089 (when doc-text
1090 (and (eq (preceding-char) ?\n)
1091 (widget-get widget :indent)
1092 (insert-char ? (widget-get widget :indent)))
1093 ;; The `*' in the beginning is redundant.
1094 (when (eq (aref doc-text 0) ?*)
1095 (setq doc-text (substring doc-text 1)))
1096 ;; Get rid of trailing newlines.
1097 (when (string-match "\n+\\'" doc-text)
1098 (setq doc-text (substring doc-text 0 (match-beginning 0))))
1099 (push (if (string-match "\n." doc-text)
1100 ;; Allow multiline doc to be hiden.
1101 (widget-create-child-and-convert
1102 widget 'widget-help
1103 :doc (progn
1104 (string-match "\\`.*" doc-text)
1105 (match-string 0 doc-text))
1106 :widget-doc doc-text
1107 "?")
1108 ;; A single line is just inserted.
1109 (widget-create-child-and-convert
1110 widget 'item :format "%d" :doc doc-text nil))
1111 buttons)))
1112 (t
1113 (error "Unknown escape `%c'" escape)))
1114 (widget-put widget :buttons buttons)))
1115
1116(defun widget-default-button-face-get (widget)
1117 ;; Use :button-face or widget-button-face
1118 (or (widget-get widget :button-face) 'widget-button-face))
1119
1120(defun widget-default-sample-face-get (widget)
1121 ;; Use :sample-face.
1122 (widget-get widget :sample-face))
1123
1124(defun widget-default-delete (widget)
1125 ;; Remove widget from the buffer.
1126 (let ((from (widget-get widget :from))
1127 (to (widget-get widget :to))
1128 (inhibit-read-only t)
1129 after-change-functions)
1130 (widget-apply widget :value-delete)
1131 (when (< from to)
1132 ;; Kludge: this doesn't need to be true for empty formats.
1133 (delete-region from to))
1134 (set-marker from nil)
1135 (set-marker to nil)))
1136
1137(defun widget-default-value-set (widget value)
1138 ;; Recreate widget with new value.
1139 (save-excursion
1140 (goto-char (widget-get widget :from))
1141 (widget-apply widget :delete)
1142 (widget-put widget :value value)
1143 (widget-apply widget :create)))
1144
1145(defun widget-default-value-inline (widget)
1146 ;; Wrap value in a list unless it is inline.
1147 (if (widget-get widget :inline)
1148 (widget-value widget)
1149 (list (widget-value widget))))
1150
1151(defun widget-default-menu-tag-get (widget)
1152 ;; Use tag or value for menus.
1153 (or (widget-get widget :menu-tag)
1154 (widget-get widget :tag)
1155 (widget-princ-to-string (widget-get widget :value))))
1156
1157(defun widget-default-active (widget)
1158 "Return t iff this widget active (user modifiable)."
1159 (and (not (widget-get widget :inactive))
1160 (let ((parent (widget-get widget :parent)))
1161 (or (null parent)
1162 (widget-apply parent :active)))))
1163
1164(defun widget-default-deactivate (widget)
1165 "Make WIDGET inactive for user modifications."
1166 (widget-specify-inactive widget
1167 (widget-get widget :from)
1168 (widget-get widget :to)))
1169
1170(defun widget-default-action (widget &optional event)
1171 ;; Notify the parent when a widget change
1172 (let ((parent (widget-get widget :parent)))
1173 (when parent
1174 (widget-apply parent :notify widget event))))
1175
1176(defun widget-default-notify (widget child &optional event)
1177 ;; Pass notification to parent.
1178 (widget-default-action widget event))
1179
1180;;; The `item' Widget.
1181
1182(define-widget 'item 'default
1183 "Constant items for inclusion in other widgets."
1184 :convert-widget 'widget-item-convert-widget
1185 :value-create 'widget-item-value-create
1186 :value-delete 'ignore
1187 :value-get 'widget-item-value-get
1188 :match 'widget-item-match
1189 :match-inline 'widget-item-match-inline
1190 :action 'widget-item-action
1191 :format "%t\n")
1192
1193(defun widget-item-convert-widget (widget)
1194 ;; Initialize :value from :args in WIDGET.
1195 (let ((args (widget-get widget :args)))
1196 (when args
1197 (widget-put widget :value (widget-apply widget
1198 :value-to-internal (car args)))
1199 (widget-put widget :args nil)))
1200 widget)
1201
1202(defun widget-item-value-create (widget)
1203 ;; Insert the printed representation of the value.
1204 (let ((standard-output (current-buffer)))
1205 (princ (widget-get widget :value))))
1206
1207(defun widget-item-match (widget value)
1208 ;; Match if the value is the same.
1209 (equal (widget-get widget :value) value))
1210
1211(defun widget-item-match-inline (widget values)
1212 ;; Match if the value is the same.
1213 (let ((value (widget-get widget :value)))
1214 (and (listp value)
1215 (<= (length value) (length values))
1216 (let ((head (subseq values 0 (length value))))
1217 (and (equal head value)
1218 (cons head (subseq values (length value))))))))
1219
1220(defun widget-item-action (widget &optional event)
1221 ;; Just notify itself.
1222 (widget-apply widget :notify widget event))
1223
1224(defun widget-item-value-get (widget)
1225 ;; Items are simple.
1226 (widget-get widget :value))
1227
1228;;; The `push-button' Widget.
1229
1230(defcustom widget-push-button-gui t
1231 "If non nil, use GUI push buttons when available."
1232 :group 'widgets
1233 :type 'boolean)
1234
1235;; Cache already created GUI objects.
1236(defvar widget-push-button-cache nil)
1237
1238(define-widget 'push-button 'item
1239 "A pushable button."
1240 :value-create 'widget-push-button-value-create
bd042c03 1241 :text-format "[%s]"
d543e20b
PA
1242 :format "%[%v%]")
1243
1244(defun widget-push-button-value-create (widget)
1245 ;; Insert text representing the `on' and `off' states.
1246 (let* ((tag (or (widget-get widget :tag)
1247 (widget-get widget :value)))
bd042c03 1248 (text (format (widget-get widget :text-format) tag))
d543e20b
PA
1249 (gui (cdr (assoc tag widget-push-button-cache))))
1250 (if (and (fboundp 'make-gui-button)
1251 (fboundp 'make-glyph)
1252 widget-push-button-gui
1253 (fboundp 'device-on-window-system-p)
1254 (device-on-window-system-p)
1255 (string-match "XEmacs" emacs-version))
1256 (progn
1257 (unless gui
1258 (setq gui (make-gui-button tag 'widget-gui-action widget))
1259 (push (cons tag gui) widget-push-button-cache))
1260 (widget-glyph-insert-glyph widget text
1261 (make-glyph (car (aref gui 1)))))
1262 (insert text))))
1263
1264(defun widget-gui-action (widget)
1265 "Apply :action for WIDGET."
1266 (widget-apply-action widget (this-command-keys)))
1267
1268;;; The `link' Widget.
1269
1270(define-widget 'link 'item
1271 "An embedded link."
1272 :help-echo "Follow the link."
1273 :format "%[_%t_%]")
1274
1275;;; The `info-link' Widget.
1276
1277(define-widget 'info-link 'link
1278 "A link to an info file."
1279 :action 'widget-info-link-action)
1280
1281(defun widget-info-link-action (widget &optional event)
1282 "Open the info node specified by WIDGET."
1283 (Info-goto-node (widget-value widget)))
1284
1285;;; The `url-link' Widget.
1286
1287(define-widget 'url-link 'link
1288 "A link to an www page."
1289 :action 'widget-url-link-action)
1290
1291(defun widget-url-link-action (widget &optional event)
1292 "Open the url specified by WIDGET."
1293 (require 'browse-url)
1294 (funcall browse-url-browser-function (widget-value widget)))
1295
1296;;; The `editable-field' Widget.
1297
1298(define-widget 'editable-field 'default
1299 "An editable text field."
1300 :convert-widget 'widget-item-convert-widget
1301 :keymap widget-field-keymap
1302 :format "%v"
1303 :value ""
1304 :action 'widget-field-action
1305 :validate 'widget-field-validate
1306 :valid-regexp ""
1307 :error "No match"
1308 :value-create 'widget-field-value-create
1309 :value-delete 'widget-field-value-delete
1310 :value-get 'widget-field-value-get
1311 :match 'widget-field-match)
1312
1313;; History of field minibuffer edits.
1314(defvar widget-field-history nil)
1315
1316(defun widget-field-action (widget &optional event)
1317 ;; Edit the value in the minibuffer.
1318 (let ((tag (widget-apply widget :menu-tag-get))
1319 (invalid (widget-apply widget :validate)))
1320 (when invalid
1321 (error (widget-get invalid :error)))
1322 (widget-value-set widget
1323 (widget-apply widget
1324 :value-to-external
1325 (read-string (concat tag ": ")
1326 (widget-apply
1327 widget
1328 :value-to-internal
1329 (widget-value widget))
1330 'widget-field-history)))
1331 (widget-apply widget :notify widget event)
1332 (widget-setup)))
1333
1334(defun widget-field-validate (widget)
1335 ;; Valid if the content matches `:valid-regexp'.
1336 (save-excursion
1337 (let ((value (widget-apply widget :value-get))
1338 (regexp (widget-get widget :valid-regexp)))
1339 (if (string-match regexp value)
1340 nil
1341 widget))))
1342
1343(defun widget-field-value-create (widget)
1344 ;; Create an editable text field.
1345 (insert " ")
1346 (let ((size (widget-get widget :size))
1347 (value (widget-get widget :value))
1348 (from (point)))
1349 (insert value)
1350 (and size
1351 (< (length value) size)
1352 (insert-char ?\ (- size (length value))))
1353 (unless (memq widget widget-field-list)
1354 (setq widget-field-new (cons widget widget-field-new)))
1355 (widget-put widget :value-to (copy-marker (point)))
1356 (set-marker-insertion-type (widget-get widget :value-to) nil)
1357 (if (null size)
1358 (insert ?\n)
1359 (insert ?\ ))
1360 (widget-put widget :value-from (copy-marker from))
1361 (set-marker-insertion-type (widget-get widget :value-from) t)))
1362
1363(defun widget-field-value-delete (widget)
1364 ;; Remove the widget from the list of active editing fields.
1365 (setq widget-field-list (delq widget widget-field-list))
1366 ;; These are nil if the :format string doesn't contain `%v'.
1367 (when (widget-get widget :value-from)
1368 (set-marker (widget-get widget :value-from) nil))
1369 (when (widget-get widget :value-from)
1370 (set-marker (widget-get widget :value-to) nil)))
1371
1372(defun widget-field-value-get (widget)
1373 ;; Return current text in editing field.
1374 (let ((from (widget-get widget :value-from))
1375 (to (widget-get widget :value-to))
1376 (size (widget-get widget :size))
1377 (secret (widget-get widget :secret))
1378 (old (current-buffer)))
1379 (if (and from to)
1380 (progn
1381 (set-buffer (marker-buffer from))
1382 (setq from (1+ from)
1383 to (1- to))
1384 (while (and size
1385 (not (zerop size))
1386 (> to from)
1387 (eq (char-after (1- to)) ?\ ))
1388 (setq to (1- to)))
1389 (let ((result (buffer-substring-no-properties from to)))
1390 (when secret
1391 (let ((index 0))
1392 (while (< (+ from index) to)
1393 (aset result index
1394 (get-text-property (+ from index) 'secret))
1395 (setq index (1+ index)))))
1396 (set-buffer old)
1397 result))
1398 (widget-get widget :value))))
1399
1400(defun widget-field-match (widget value)
1401 ;; Match any string.
1402 (stringp value))
1403
1404;;; The `text' Widget.
1405
1406(define-widget 'text 'editable-field
1407 :keymap widget-text-keymap
1408 "A multiline text area.")
1409
1410;;; The `menu-choice' Widget.
1411
1412(define-widget 'menu-choice 'default
1413 "A menu of options."
1414 :convert-widget 'widget-types-convert-widget
1415 :format "%[%t%]: %v"
1416 :case-fold t
1417 :tag "choice"
1418 :void '(item :format "invalid (%t)\n")
1419 :value-create 'widget-choice-value-create
1420 :value-delete 'widget-children-value-delete
1421 :value-get 'widget-choice-value-get
1422 :value-inline 'widget-choice-value-inline
1423 :action 'widget-choice-action
1424 :error "Make a choice"
1425 :validate 'widget-choice-validate
1426 :match 'widget-choice-match
1427 :match-inline 'widget-choice-match-inline)
1428
1429(defun widget-choice-value-create (widget)
1430 ;; Insert the first choice that matches the value.
1431 (let ((value (widget-get widget :value))
1432 (args (widget-get widget :args))
1433 current)
1434 (while args
1435 (setq current (car args)
1436 args (cdr args))
1437 (when (widget-apply current :match value)
1438 (widget-put widget :children (list (widget-create-child-value
1439 widget current value)))
1440 (widget-put widget :choice current)
1441 (setq args nil
1442 current nil)))
1443 (when current
1444 (let ((void (widget-get widget :void)))
1445 (widget-put widget :children (list (widget-create-child-and-convert
1446 widget void :value value)))
1447 (widget-put widget :choice void)))))
1448
1449(defun widget-choice-value-get (widget)
1450 ;; Get value of the child widget.
1451 (widget-value (car (widget-get widget :children))))
1452
1453(defun widget-choice-value-inline (widget)
1454 ;; Get value of the child widget.
1455 (widget-apply (car (widget-get widget :children)) :value-inline))
1456
1457(defun widget-choice-action (widget &optional event)
1458 ;; Make a choice.
1459 (let ((args (widget-get widget :args))
1460 (old (widget-get widget :choice))
1461 (tag (widget-apply widget :menu-tag-get))
1462 (completion-ignore-case (widget-get widget :case-fold))
1463 current choices)
1464 ;; Remember old value.
1465 (if (and old (not (widget-apply widget :validate)))
1466 (let* ((external (widget-value widget))
1467 (internal (widget-apply old :value-to-internal external)))
1468 (widget-put old :value internal)))
1469 ;; Find new choice.
1470 (setq current
1471 (cond ((= (length args) 0)
1472 nil)
1473 ((= (length args) 1)
1474 (nth 0 args))
1475 ((and (= (length args) 2)
1476 (memq old args))
1477 (if (eq old (nth 0 args))
1478 (nth 1 args)
1479 (nth 0 args)))
1480 (t
1481 (while args
1482 (setq current (car args)
1483 args (cdr args))
1484 (setq choices
1485 (cons (cons (widget-apply current :menu-tag-get)
1486 current)
1487 choices)))
1488 (widget-choose tag (reverse choices) event))))
1489 (when current
1490 (widget-value-set widget
1491 (widget-apply current :value-to-external
1492 (widget-get current :value)))
1493 (widget-apply widget :notify widget event)
1494 (widget-setup)))
1495 ;; Notify parent.
1496 (widget-apply widget :notify widget event)
1497 (widget-clear-undo))
1498
1499(defun widget-choice-validate (widget)
1500 ;; Valid if we have made a valid choice.
1501 (let ((void (widget-get widget :void))
1502 (choice (widget-get widget :choice))
1503 (child (car (widget-get widget :children))))
1504 (if (eq void choice)
1505 widget
1506 (widget-apply child :validate))))
1507
1508(defun widget-choice-match (widget value)
1509 ;; Matches if one of the choices matches.
1510 (let ((args (widget-get widget :args))
1511 current found)
1512 (while (and args (not found))
1513 (setq current (car args)
1514 args (cdr args)
1515 found (widget-apply current :match value)))
1516 found))
1517
1518(defun widget-choice-match-inline (widget values)
1519 ;; Matches if one of the choices matches.
1520 (let ((args (widget-get widget :args))
1521 current found)
1522 (while (and args (null found))
1523 (setq current (car args)
1524 args (cdr args)
1525 found (widget-match-inline current values)))
1526 found))
1527
1528;;; The `toggle' Widget.
1529
1530(define-widget 'toggle 'item
1531 "Toggle between two states."
1532 :format "%[%v%]\n"
1533 :value-create 'widget-toggle-value-create
1534 :action 'widget-toggle-action
1535 :match (lambda (widget value) t)
1536 :on "on"
1537 :off "off")
1538
1539(defun widget-toggle-value-create (widget)
1540 ;; Insert text representing the `on' and `off' states.
1541 (if (widget-value widget)
1542 (widget-glyph-insert widget
1543 (widget-get widget :on)
1544 (widget-get widget :on-glyph))
1545 (widget-glyph-insert widget
1546 (widget-get widget :off)
1547 (widget-get widget :off-glyph))))
1548
1549(defun widget-toggle-action (widget &optional event)
1550 ;; Toggle value.
1551 (widget-value-set widget (not (widget-value widget)))
1552 (widget-apply widget :notify widget event))
1553
1554;;; The `checkbox' Widget.
1555
1556(define-widget 'checkbox 'toggle
1557 "A checkbox toggle."
1558 :format "%[%v%]"
1559 :on "[X]"
1560 :on-glyph "check1"
1561 :off "[ ]"
1562 :off-glyph "check0"
1563 :action 'widget-checkbox-action)
1564
1565(defun widget-checkbox-action (widget &optional event)
1566 "Toggle checkbox, notify parent, and set active state of sibling."
1567 (widget-toggle-action widget event)
1568 (let ((sibling (widget-get-sibling widget)))
1569 (when sibling
1570 (if (widget-value widget)
1571 (widget-apply sibling :activate)
1572 (widget-apply sibling :deactivate)))))
1573
1574;;; The `checklist' Widget.
1575
1576(define-widget 'checklist 'default
1577 "A multiple choice widget."
1578 :convert-widget 'widget-types-convert-widget
1579 :format "%v"
1580 :offset 4
1581 :entry-format "%b %v"
1582 :menu-tag "checklist"
1583 :greedy nil
1584 :value-create 'widget-checklist-value-create
1585 :value-delete 'widget-children-value-delete
1586 :value-get 'widget-checklist-value-get
1587 :validate 'widget-checklist-validate
1588 :match 'widget-checklist-match
1589 :match-inline 'widget-checklist-match-inline)
1590
1591(defun widget-checklist-value-create (widget)
1592 ;; Insert all values
1593 (let ((alist (widget-checklist-match-find widget (widget-get widget :value)))
1594 (args (widget-get widget :args)))
1595 (while args
1596 (widget-checklist-add-item widget (car args) (assq (car args) alist))
1597 (setq args (cdr args)))
1598 (widget-put widget :children (nreverse (widget-get widget :children)))))
1599
1600(defun widget-checklist-add-item (widget type chosen)
1601 ;; Create checklist item in WIDGET of type TYPE.
1602 ;; If the item is checked, CHOSEN is a cons whose cdr is the value.
1603 (and (eq (preceding-char) ?\n)
1604 (widget-get widget :indent)
1605 (insert-char ? (widget-get widget :indent)))
1606 (widget-specify-insert
1607 (let* ((children (widget-get widget :children))
1608 (buttons (widget-get widget :buttons))
1609 (button-args (or (widget-get type :sibling-args)
1610 (widget-get widget :button-args)))
1611 (from (point))
1612 child button)
1613 (insert (widget-get widget :entry-format))
1614 (goto-char from)
1615 ;; Parse % escapes in format.
1616 (while (re-search-forward "%\\([bv%]\\)" nil t)
1617 (let ((escape (aref (match-string 1) 0)))
1618 (replace-match "" t t)
1619 (cond ((eq escape ?%)
1620 (insert "%"))
1621 ((eq escape ?b)
1622 (setq button (apply 'widget-create-child-and-convert
1623 widget 'checkbox
1624 :value (not (null chosen))
1625 button-args)))
1626 ((eq escape ?v)
1627 (setq child
1628 (cond ((not chosen)
1629 (let ((child (widget-create-child widget type)))
1630 (widget-apply child :deactivate)
1631 child))
1632 ((widget-get type :inline)
1633 (widget-create-child-value
1634 widget type (cdr chosen)))
1635 (t
1636 (widget-create-child-value
1637 widget type (car (cdr chosen)))))))
1638 (t
1639 (error "Unknown escape `%c'" escape)))))
1640 ;; Update properties.
1641 (and button child (widget-put child :button button))
1642 (and button (widget-put widget :buttons (cons button buttons)))
1643 (and child (widget-put widget :children (cons child children))))))
1644
1645(defun widget-checklist-match (widget values)
1646 ;; All values must match a type in the checklist.
1647 (and (listp values)
1648 (null (cdr (widget-checklist-match-inline widget values)))))
1649
1650(defun widget-checklist-match-inline (widget values)
1651 ;; Find the values which match a type in the checklist.
1652 (let ((greedy (widget-get widget :greedy))
1653 (args (copy-list (widget-get widget :args)))
1654 found rest)
1655 (while values
1656 (let ((answer (widget-checklist-match-up args values)))
1657 (cond (answer
1658 (let ((vals (widget-match-inline answer values)))
1659 (setq found (append found (car vals))
1660 values (cdr vals)
1661 args (delq answer args))))
1662 (greedy
1663 (setq rest (append rest (list (car values)))
1664 values (cdr values)))
1665 (t
1666 (setq rest (append rest values)
1667 values nil)))))
1668 (cons found rest)))
1669
1670(defun widget-checklist-match-find (widget vals)
1671 ;; Find the vals which match a type in the checklist.
1672 ;; Return an alist of (TYPE MATCH).
1673 (let ((greedy (widget-get widget :greedy))
1674 (args (copy-list (widget-get widget :args)))
1675 found)
1676 (while vals
1677 (let ((answer (widget-checklist-match-up args vals)))
1678 (cond (answer
1679 (let ((match (widget-match-inline answer vals)))
1680 (setq found (cons (cons answer (car match)) found)
1681 vals (cdr match)
1682 args (delq answer args))))
1683 (greedy
1684 (setq vals (cdr vals)))
1685 (t
1686 (setq vals nil)))))
1687 found))
1688
1689(defun widget-checklist-match-up (args vals)
1690 ;; Rerturn the first type from ARGS that matches VALS.
1691 (let (current found)
1692 (while (and args (null found))
1693 (setq current (car args)
1694 args (cdr args)
1695 found (widget-match-inline current vals)))
1696 (if found
1697 current
1698 nil)))
1699
1700(defun widget-checklist-value-get (widget)
1701 ;; The values of all selected items.
1702 (let ((children (widget-get widget :children))
1703 child result)
1704 (while children
1705 (setq child (car children)
1706 children (cdr children))
1707 (if (widget-value (widget-get child :button))
1708 (setq result (append result (widget-apply child :value-inline)))))
1709 result))
1710
1711(defun widget-checklist-validate (widget)
1712 ;; Ticked chilren must be valid.
1713 (let ((children (widget-get widget :children))
1714 child button found)
1715 (while (and children (not found))
1716 (setq child (car children)
1717 children (cdr children)
1718 button (widget-get child :button)
1719 found (and (widget-value button)
1720 (widget-apply child :validate))))
1721 found))
1722
1723;;; The `option' Widget
1724
1725(define-widget 'option 'checklist
1726 "An widget with an optional item."
1727 :inline t)
1728
1729;;; The `choice-item' Widget.
1730
1731(define-widget 'choice-item 'item
1732 "Button items that delegate action events to their parents."
1733 :action 'widget-choice-item-action
1734 :format "%[%t%] \n")
1735
1736(defun widget-choice-item-action (widget &optional event)
1737 ;; Tell parent what happened.
1738 (widget-apply (widget-get widget :parent) :action event))
1739
1740;;; The `radio-button' Widget.
1741
1742(define-widget 'radio-button 'toggle
1743 "A radio button for use in the `radio' widget."
1744 :notify 'widget-radio-button-notify
1745 :format "%[%v%]"
1746 :on "(*)"
1747 :on-glyph "radio1"
1748 :off "( )"
1749 :off-glyph "radio0")
1750
1751(defun widget-radio-button-notify (widget child &optional event)
1752 ;; Tell daddy.
1753 (widget-apply (widget-get widget :parent) :action widget event))
1754
1755;;; The `radio-button-choice' Widget.
1756
1757(define-widget 'radio-button-choice 'default
1758 "Select one of multiple options."
1759 :convert-widget 'widget-types-convert-widget
1760 :offset 4
1761 :format "%v"
1762 :entry-format "%b %v"
1763 :menu-tag "radio"
1764 :value-create 'widget-radio-value-create
1765 :value-delete 'widget-children-value-delete
1766 :value-get 'widget-radio-value-get
1767 :value-inline 'widget-radio-value-inline
1768 :value-set 'widget-radio-value-set
1769 :error "You must push one of the buttons"
1770 :validate 'widget-radio-validate
1771 :match 'widget-choice-match
1772 :match-inline 'widget-choice-match-inline
1773 :action 'widget-radio-action)
1774
1775(defun widget-radio-value-create (widget)
1776 ;; Insert all values
1777 (let ((args (widget-get widget :args))
1778 arg)
1779 (while args
1780 (setq arg (car args)
1781 args (cdr args))
1782 (widget-radio-add-item widget arg))))
1783
1784(defun widget-radio-add-item (widget type)
1785 "Add to radio widget WIDGET a new radio button item of type TYPE."
1786 ;; (setq type (widget-convert type))
1787 (and (eq (preceding-char) ?\n)
1788 (widget-get widget :indent)
1789 (insert-char ? (widget-get widget :indent)))
1790 (widget-specify-insert
1791 (let* ((value (widget-get widget :value))
1792 (children (widget-get widget :children))
1793 (buttons (widget-get widget :buttons))
1794 (button-args (or (widget-get type :sibling-args)
1795 (widget-get widget :button-args)))
1796 (from (point))
1797 (chosen (and (null (widget-get widget :choice))
1798 (widget-apply type :match value)))
1799 child button)
1800 (insert (widget-get widget :entry-format))
1801 (goto-char from)
1802 ;; Parse % escapes in format.
1803 (while (re-search-forward "%\\([bv%]\\)" nil t)
1804 (let ((escape (aref (match-string 1) 0)))
1805 (replace-match "" t t)
1806 (cond ((eq escape ?%)
1807 (insert "%"))
1808 ((eq escape ?b)
1809 (setq button (apply 'widget-create-child-and-convert
1810 widget 'radio-button
1811 :value (not (null chosen))
1812 button-args)))
1813 ((eq escape ?v)
1814 (setq child (if chosen
1815 (widget-create-child-value
1816 widget type value)
1817 (widget-create-child widget type)))
1818 (unless chosen
1819 (widget-apply child :deactivate)))
1820 (t
1821 (error "Unknown escape `%c'" escape)))))
1822 ;; Update properties.
1823 (when chosen
1824 (widget-put widget :choice type))
1825 (when button
1826 (widget-put child :button button)
1827 (widget-put widget :buttons (nconc buttons (list button))))
1828 (when child
1829 (widget-put widget :children (nconc children (list child))))
1830 child)))
1831
1832(defun widget-radio-value-get (widget)
1833 ;; Get value of the child widget.
1834 (let ((chosen (widget-radio-chosen widget)))
1835 (and chosen (widget-value chosen))))
1836
1837(defun widget-radio-chosen (widget)
1838 "Return the widget representing the chosen radio button."
1839 (let ((children (widget-get widget :children))
1840 current found)
1841 (while children
1842 (setq current (car children)
1843 children (cdr children))
1844 (let* ((button (widget-get current :button))
1845 (value (widget-apply button :value-get)))
1846 (when value
1847 (setq found current
1848 children nil))))
1849 found))
1850
1851(defun widget-radio-value-inline (widget)
1852 ;; Get value of the child widget.
1853 (let ((children (widget-get widget :children))
1854 current found)
1855 (while children
1856 (setq current (car children)
1857 children (cdr children))
1858 (let* ((button (widget-get current :button))
1859 (value (widget-apply button :value-get)))
1860 (when value
1861 (setq found (widget-apply current :value-inline)
1862 children nil))))
1863 found))
1864
1865(defun widget-radio-value-set (widget value)
1866 ;; We can't just delete and recreate a radio widget, since children
1867 ;; can be added after the original creation and won't be recreated
1868 ;; by `:create'.
1869 (let ((children (widget-get widget :children))
1870 current found)
1871 (while children
1872 (setq current (car children)
1873 children (cdr children))
1874 (let* ((button (widget-get current :button))
1875 (match (and (not found)
1876 (widget-apply current :match value))))
1877 (widget-value-set button match)
1878 (if match
1879 (progn
1880 (widget-value-set current value)
1881 (widget-apply current :activate))
1882 (widget-apply current :deactivate))
1883 (setq found (or found match))))))
1884
1885(defun widget-radio-validate (widget)
1886 ;; Valid if we have made a valid choice.
1887 (let ((children (widget-get widget :children))
1888 current found button)
1889 (while (and children (not found))
1890 (setq current (car children)
1891 children (cdr children)
1892 button (widget-get current :button)
1893 found (widget-apply button :value-get)))
1894 (if found
1895 (widget-apply current :validate)
1896 widget)))
1897
1898(defun widget-radio-action (widget child event)
1899 ;; Check if a radio button was pressed.
1900 (let ((children (widget-get widget :children))
1901 (buttons (widget-get widget :buttons))
1902 current)
1903 (when (memq child buttons)
1904 (while children
1905 (setq current (car children)
1906 children (cdr children))
1907 (let* ((button (widget-get current :button)))
1908 (cond ((eq child button)
1909 (widget-value-set button t)
1910 (widget-apply current :activate))
1911 ((widget-value button)
1912 (widget-value-set button nil)
1913 (widget-apply current :deactivate)))))))
1914 ;; Pass notification to parent.
1915 (widget-apply widget :notify child event))
1916
1917;;; The `insert-button' Widget.
1918
1919(define-widget 'insert-button 'push-button
1920 "An insert button for the `editable-list' widget."
1921 :tag "INS"
1922 :help-echo "Insert a new item into the list at this position."
1923 :action 'widget-insert-button-action)
1924
1925(defun widget-insert-button-action (widget &optional event)
1926 ;; Ask the parent to insert a new item.
1927 (widget-apply (widget-get widget :parent)
1928 :insert-before (widget-get widget :widget)))
1929
1930;;; The `delete-button' Widget.
1931
1932(define-widget 'delete-button 'push-button
1933 "A delete button for the `editable-list' widget."
1934 :tag "DEL"
1935 :help-echo "Delete this item from the list."
1936 :action 'widget-delete-button-action)
1937
1938(defun widget-delete-button-action (widget &optional event)
1939 ;; Ask the parent to insert a new item.
1940 (widget-apply (widget-get widget :parent)
1941 :delete-at (widget-get widget :widget)))
1942
1943;;; The `editable-list' Widget.
1944
1945(defcustom widget-editable-list-gui nil
1946 "If non nil, use GUI push-buttons in editable list when available."
1947 :type 'boolean
1948 :group 'widgets)
1949
1950(define-widget 'editable-list 'default
1951 "A variable list of widgets of the same type."
1952 :convert-widget 'widget-types-convert-widget
1953 :offset 12
1954 :format "%v%i\n"
1955 :format-handler 'widget-editable-list-format-handler
1956 :entry-format "%i %d %v"
1957 :menu-tag "editable-list"
1958 :value-create 'widget-editable-list-value-create
1959 :value-delete 'widget-children-value-delete
1960 :value-get 'widget-editable-list-value-get
1961 :validate 'widget-editable-list-validate
1962 :match 'widget-editable-list-match
1963 :match-inline 'widget-editable-list-match-inline
1964 :insert-before 'widget-editable-list-insert-before
1965 :delete-at 'widget-editable-list-delete-at)
1966
1967(defun widget-editable-list-format-handler (widget escape)
1968 ;; We recognize the insert button.
1969 (let ((widget-push-button-gui widget-editable-list-gui))
1970 (cond ((eq escape ?i)
1971 (and (widget-get widget :indent)
1972 (insert-char ? (widget-get widget :indent)))
1973 (apply 'widget-create-child-and-convert
1974 widget 'insert-button
1975 (widget-get widget :append-button-args)))
1976 (t
1977 (widget-default-format-handler widget escape)))))
1978
1979(defun widget-editable-list-value-create (widget)
1980 ;; Insert all values
1981 (let* ((value (widget-get widget :value))
1982 (type (nth 0 (widget-get widget :args)))
1983 (inlinep (widget-get type :inline))
1984 children)
1985 (widget-put widget :value-pos (copy-marker (point)))
1986 (set-marker-insertion-type (widget-get widget :value-pos) t)
1987 (while value
1988 (let ((answer (widget-match-inline type value)))
1989 (if answer
1990 (setq children (cons (widget-editable-list-entry-create
1991 widget
1992 (if inlinep
1993 (car answer)
1994 (car (car answer)))
1995 t)
1996 children)
1997 value (cdr answer))
1998 (setq value nil))))
1999 (widget-put widget :children (nreverse children))))
2000
2001(defun widget-editable-list-value-get (widget)
2002 ;; Get value of the child widget.
2003 (apply 'append (mapcar (lambda (child) (widget-apply child :value-inline))
2004 (widget-get widget :children))))
2005
2006(defun widget-editable-list-validate (widget)
2007 ;; All the chilren must be valid.
2008 (let ((children (widget-get widget :children))
2009 child found)
2010 (while (and children (not found))
2011 (setq child (car children)
2012 children (cdr children)
2013 found (widget-apply child :validate)))
2014 found))
2015
2016(defun widget-editable-list-match (widget value)
2017 ;; Value must be a list and all the members must match the type.
2018 (and (listp value)
2019 (null (cdr (widget-editable-list-match-inline widget value)))))
2020
2021(defun widget-editable-list-match-inline (widget value)
2022 (let ((type (nth 0 (widget-get widget :args)))
2023 (ok t)
2024 found)
2025 (while (and value ok)
2026 (let ((answer (widget-match-inline type value)))
2027 (if answer
2028 (setq found (append found (car answer))
2029 value (cdr answer))
2030 (setq ok nil))))
2031 (cons found value)))
2032
2033(defun widget-editable-list-insert-before (widget before)
2034 ;; Insert a new child in the list of children.
2035 (save-excursion
2036 (let ((children (widget-get widget :children))
2037 (inhibit-read-only t)
2038 after-change-functions)
2039 (cond (before
2040 (goto-char (widget-get before :entry-from)))
2041 (t
2042 (goto-char (widget-get widget :value-pos))))
2043 (let ((child (widget-editable-list-entry-create
2044 widget nil nil)))
2045 (when (< (widget-get child :entry-from) (widget-get widget :from))
2046 (set-marker (widget-get widget :from)
2047 (widget-get child :entry-from)))
2048 (widget-specify-text (widget-get child :entry-from)
2049 (widget-get child :entry-to))
2050 (if (eq (car children) before)
2051 (widget-put widget :children (cons child children))
2052 (while (not (eq (car (cdr children)) before))
2053 (setq children (cdr children)))
2054 (setcdr children (cons child (cdr children)))))))
2055 (widget-setup)
2056 widget (widget-apply widget :notify widget))
2057
2058(defun widget-editable-list-delete-at (widget child)
2059 ;; Delete child from list of children.
2060 (save-excursion
2061 (let ((buttons (copy-list (widget-get widget :buttons)))
2062 button
2063 (inhibit-read-only t)
2064 after-change-functions)
2065 (while buttons
2066 (setq button (car buttons)
2067 buttons (cdr buttons))
2068 (when (eq (widget-get button :widget) child)
2069 (widget-put widget
2070 :buttons (delq button (widget-get widget :buttons)))
2071 (widget-delete button))))
2072 (let ((entry-from (widget-get child :entry-from))
2073 (entry-to (widget-get child :entry-to))
2074 (inhibit-read-only t)
2075 after-change-functions)
2076 (widget-delete child)
2077 (delete-region entry-from entry-to)
2078 (set-marker entry-from nil)
2079 (set-marker entry-to nil))
2080 (widget-put widget :children (delq child (widget-get widget :children))))
2081 (widget-setup)
2082 (widget-apply widget :notify widget))
2083
2084(defun widget-editable-list-entry-create (widget value conv)
2085 ;; Create a new entry to the list.
2086 (let ((type (nth 0 (widget-get widget :args)))
2087 (widget-push-button-gui widget-editable-list-gui)
2088 child delete insert)
2089 (widget-specify-insert
2090 (save-excursion
2091 (and (widget-get widget :indent)
2092 (insert-char ? (widget-get widget :indent)))
2093 (insert (widget-get widget :entry-format)))
2094 ;; Parse % escapes in format.
2095 (while (re-search-forward "%\\(.\\)" nil t)
2096 (let ((escape (aref (match-string 1) 0)))
2097 (replace-match "" t t)
2098 (cond ((eq escape ?%)
2099 (insert "%"))
2100 ((eq escape ?i)
2101 (setq insert (apply 'widget-create-child-and-convert
2102 widget 'insert-button
2103 (widget-get widget :insert-button-args))))
2104 ((eq escape ?d)
2105 (setq delete (apply 'widget-create-child-and-convert
2106 widget 'delete-button
2107 (widget-get widget :delete-button-args))))
2108 ((eq escape ?v)
2109 (if conv
2110 (setq child (widget-create-child-value
2111 widget type value))
2112 (setq child (widget-create-child widget type))))
2113 (t
2114 (error "Unknown escape `%c'" escape)))))
2115 (widget-put widget
2116 :buttons (cons delete
2117 (cons insert
2118 (widget-get widget :buttons))))
2119 (let ((entry-from (copy-marker (point-min)))
2120 (entry-to (copy-marker (point-max))))
2121 (widget-specify-text entry-from entry-to)
2122 (set-marker-insertion-type entry-from t)
2123 (set-marker-insertion-type entry-to nil)
2124 (widget-put child :entry-from entry-from)
2125 (widget-put child :entry-to entry-to)))
2126 (widget-put insert :widget child)
2127 (widget-put delete :widget child)
2128 child))
2129
2130;;; The `group' Widget.
2131
2132(define-widget 'group 'default
2133 "A widget which group other widgets inside."
2134 :convert-widget 'widget-types-convert-widget
2135 :format "%v"
2136 :value-create 'widget-group-value-create
2137 :value-delete 'widget-children-value-delete
2138 :value-get 'widget-editable-list-value-get
2139 :validate 'widget-editable-list-validate
2140 :match 'widget-group-match
2141 :match-inline 'widget-group-match-inline)
2142
2143(defun widget-group-value-create (widget)
2144 ;; Create each component.
2145 (let ((args (widget-get widget :args))
2146 (value (widget-get widget :value))
2147 arg answer children)
2148 (while args
2149 (setq arg (car args)
2150 args (cdr args)
2151 answer (widget-match-inline arg value)
2152 value (cdr answer))
2153 (and (eq (preceding-char) ?\n)
2154 (widget-get widget :indent)
2155 (insert-char ? (widget-get widget :indent)))
2156 (push (cond ((null answer)
2157 (widget-create-child widget arg))
2158 ((widget-get arg :inline)
2159 (widget-create-child-value widget arg (car answer)))
2160 (t
2161 (widget-create-child-value widget arg (car (car answer)))))
2162 children))
2163 (widget-put widget :children (nreverse children))))
2164
2165(defun widget-group-match (widget values)
2166 ;; Match if the components match.
2167 (and (listp values)
2168 (let ((match (widget-group-match-inline widget values)))
2169 (and match (null (cdr match))))))
2170
2171(defun widget-group-match-inline (widget vals)
2172 ;; Match if the components match.
2173 (let ((args (widget-get widget :args))
2174 argument answer found)
2175 (while args
2176 (setq argument (car args)
2177 args (cdr args)
2178 answer (widget-match-inline argument vals))
2179 (if answer
2180 (setq vals (cdr answer)
2181 found (append found (car answer)))
2182 (setq vals nil
2183 args nil)))
2184 (if answer
2185 (cons found vals)
2186 nil)))
2187
2188;;; The `widget-help' Widget.
2189
2190(define-widget 'widget-help 'push-button
2191 "The widget documentation button."
2192 :format "%[[%t]%] %d"
2193 :help-echo "Toggle display of documentation."
2194 :action 'widget-help-action)
2195
2196(defun widget-help-action (widget &optional event)
2197 "Toggle documentation for WIDGET."
2198 (let ((old (widget-get widget :doc))
2199 (new (widget-get widget :widget-doc)))
2200 (widget-put widget :doc new)
2201 (widget-put widget :widget-doc old))
2202 (widget-value-set widget (widget-value widget)))
2203
2204;;; The Sexp Widgets.
2205
2206(define-widget 'const 'item
2207 "An immutable sexp."
2208 :format "%t\n%d")
2209
2210(define-widget 'function-item 'item
2211 "An immutable function name."
2212 :format "%v\n%h"
2213 :documentation-property (lambda (symbol)
2214 (condition-case nil
2215 (documentation symbol t)
2216 (error nil))))
2217
2218(define-widget 'variable-item 'item
2219 "An immutable variable name."
2220 :format "%v\n%h"
2221 :documentation-property 'variable-documentation)
2222
2223(define-widget 'string 'editable-field
2224 "A string"
2225 :tag "String"
2226 :format "%[%t%]: %v")
2227
2228(define-widget 'regexp 'string
2229 "A regular expression."
2230 ;; Should do validation.
2231 :tag "Regexp")
2232
2233(define-widget 'file 'string
2234 "A file widget.
2235It will read a file name from the minibuffer when activated."
2236 :format "%[%t%]: %v"
2237 :tag "File"
2238 :action 'widget-file-action)
2239
2240(defun widget-file-action (widget &optional event)
2241 ;; Read a file name from the minibuffer.
2242 (let* ((value (widget-value widget))
2243 (dir (file-name-directory value))
2244 (file (file-name-nondirectory value))
2245 (menu-tag (widget-apply widget :menu-tag-get))
2246 (must-match (widget-get widget :must-match))
2247 (answer (read-file-name (concat menu-tag ": (default `" value "') ")
2248 dir nil must-match file)))
2249 (widget-value-set widget (abbreviate-file-name answer))
2250 (widget-apply widget :notify widget event)
2251 (widget-setup)))
2252
2253(define-widget 'directory 'file
2254 "A directory widget.
2255It will read a directory name from the minibuffer when activated."
2256 :tag "Directory")
2257
2258(define-widget 'symbol 'string
2259 "A lisp symbol."
2260 :value nil
2261 :tag "Symbol"
2262 :match (lambda (widget value) (symbolp value))
2263 :value-to-internal (lambda (widget value)
2264 (if (symbolp value)
2265 (symbol-name value)
2266 value))
2267 :value-to-external (lambda (widget value)
2268 (if (stringp value)
2269 (intern value)
2270 value)))
2271
2272(define-widget 'function 'sexp
2273 ;; Should complete on functions.
2274 "A lisp function."
2275 :tag "Function")
2276
2277(define-widget 'variable 'symbol
2278 ;; Should complete on variables.
2279 "A lisp variable."
2280 :tag "Variable")
2281
2282(define-widget 'sexp 'string
2283 "An arbitrary lisp expression."
2284 :tag "Lisp expression"
2285 :value nil
2286 :validate 'widget-sexp-validate
2287 :match (lambda (widget value) t)
2288 :value-to-internal 'widget-sexp-value-to-internal
2289 :value-to-external (lambda (widget value) (read value)))
2290
2291(defun widget-sexp-value-to-internal (widget value)
2292 ;; Use pp for printer representation.
2293 (let ((pp (pp-to-string value)))
2294 (while (string-match "\n\\'" pp)
2295 (setq pp (substring pp 0 -1)))
2296 (if (or (string-match "\n\\'" pp)
2297 (> (length pp) 40))
2298 (concat "\n" pp)
2299 pp)))
2300
2301(defun widget-sexp-validate (widget)
2302 ;; Valid if we can read the string and there is no junk left after it.
2303 (save-excursion
2304 (let ((buffer (set-buffer (get-buffer-create " *Widget Scratch*"))))
2305 (erase-buffer)
2306 (insert (widget-apply widget :value-get))
2307 (goto-char (point-min))
2308 (condition-case data
2309 (let ((value (read buffer)))
2310 (if (eobp)
2311 (if (widget-apply widget :match value)
2312 nil
2313 (widget-put widget :error (widget-get widget :type-error))
2314 widget)
2315 (widget-put widget
2316 :error (format "Junk at end of expression: %s"
2317 (buffer-substring (point)
2318 (point-max))))
2319 widget))
2320 (error (widget-put widget :error (error-message-string data))
2321 widget)))))
2322
2323(define-widget 'integer 'sexp
2324 "An integer."
2325 :tag "Integer"
2326 :value 0
2327 :type-error "This field should contain an integer"
2328 :value-to-internal (lambda (widget value)
2329 (if (integerp value)
2330 (prin1-to-string value)
2331 value))
2332 :match (lambda (widget value) (integerp value)))
2333
2334(define-widget 'character 'string
2335 "An character."
2336 :tag "Character"
2337 :value 0
2338 :size 1
2339 :format "%{%t%}: %v\n"
2340 :type-error "This field should contain a character"
2341 :value-to-internal (lambda (widget value)
2342 (if (integerp value)
2343 (char-to-string value)
2344 value))
2345 :value-to-external (lambda (widget value)
2346 (if (stringp value)
2347 (aref value 0)
2348 value))
2349 :match (lambda (widget value) (integerp value)))
2350
2351(define-widget 'number 'sexp
2352 "A floating point number."
2353 :tag "Number"
2354 :value 0.0
2355 :type-error "This field should contain a number"
2356 :value-to-internal (lambda (widget value)
2357 (if (numberp value)
2358 (prin1-to-string value)
2359 value))
2360 :match (lambda (widget value) (numberp value)))
2361
2362(define-widget 'list 'group
2363 "A lisp list."
2364 :tag "List"
2365 :format "%{%t%}:\n%v")
2366
2367(define-widget 'vector 'group
2368 "A lisp vector."
2369 :tag "Vector"
2370 :format "%{%t%}:\n%v"
2371 :match 'widget-vector-match
2372 :value-to-internal (lambda (widget value) (append value nil))
2373 :value-to-external (lambda (widget value) (apply 'vector value)))
2374
2375(defun widget-vector-match (widget value)
2376 (and (vectorp value)
2377 (widget-group-match widget
bd042c03 2378 (widget-apply widget :value-to-internal value))))
d543e20b
PA
2379
2380(define-widget 'cons 'group
2381 "A cons-cell."
2382 :tag "Cons-cell"
2383 :format "%{%t%}:\n%v"
2384 :match 'widget-cons-match
2385 :value-to-internal (lambda (widget value)
2386 (list (car value) (cdr value)))
2387 :value-to-external (lambda (widget value)
2388 (cons (nth 0 value) (nth 1 value))))
2389
2390(defun widget-cons-match (widget value)
2391 (and (consp value)
2392 (widget-group-match widget
2393 (widget-apply widget :value-to-internal value))))
2394
2395(define-widget 'choice 'menu-choice
2396 "A union of several sexp types."
2397 :tag "Choice"
2398 :format "%[%t%]: %v")
2399
2400(define-widget 'radio 'radio-button-choice
2401 "A union of several sexp types."
2402 :tag "Choice"
2403 :format "%{%t%}:\n%v")
2404
2405(define-widget 'repeat 'editable-list
2406 "A variable length homogeneous list."
2407 :tag "Repeat"
2408 :format "%{%t%}:\n%v%i\n")
2409
2410(define-widget 'set 'checklist
2411 "A list of members from a fixed set."
2412 :tag "Set"
2413 :format "%{%t%}:\n%v")
2414
2415(define-widget 'boolean 'toggle
2416 "To be nil or non-nil, that is the question."
2417 :tag "Boolean"
2418 :format "%{%t%}: %[%v%]\n")
2419
2420;;; The `color' Widget.
2421
2422(define-widget 'color-item 'choice-item
2423 "A color name (with sample)."
2424 :format "%v (%{sample%})\n"
2425 :sample-face-get 'widget-color-item-button-face-get)
2426
2427(defun widget-color-item-button-face-get (widget)
2428 ;; We create a face from the value.
2429 (require 'facemenu)
2430 (condition-case nil
2431 (facemenu-get-face (intern (concat "fg:" (widget-value widget))))
2432 (error 'default)))
2433
2434(define-widget 'color 'push-button
2435 "Choose a color name (with sample)."
2436 :format "%[%t%]: %v"
2437 :tag "Color"
2438 :value "black"
2439 :value-create 'widget-color-value-create
2440 :value-delete 'widget-children-value-delete
2441 :value-get 'widget-color-value-get
2442 :value-set 'widget-color-value-set
2443 :action 'widget-color-action
2444 :match 'widget-field-match
2445 :tag "Color")
2446
2447(defvar widget-color-choice-list nil)
2448;; Variable holding the possible colors.
2449
2450(defun widget-color-choice-list ()
2451 (unless widget-color-choice-list
2452 (setq widget-color-choice-list
2453 (mapcar '(lambda (color) (list color))
2454 (x-defined-colors))))
2455 widget-color-choice-list)
2456
2457(defun widget-color-value-create (widget)
2458 (let ((child (widget-create-child-and-convert
2459 widget 'color-item (widget-get widget :value))))
2460 (widget-put widget :children (list child))))
2461
2462(defun widget-color-value-get (widget)
2463 ;; Pass command to first child.
2464 (widget-apply (car (widget-get widget :children)) :value-get))
2465
2466(defun widget-color-value-set (widget value)
2467 ;; Pass command to first child.
2468 (widget-apply (car (widget-get widget :children)) :value-set value))
2469
2470(defvar widget-color-history nil
2471 "History of entered colors")
2472
2473(defun widget-color-action (widget &optional event)
2474 ;; Prompt for a color.
2475 (let* ((tag (widget-apply widget :menu-tag-get))
2476 (prompt (concat tag ": "))
2477 (answer (cond ((string-match "XEmacs" emacs-version)
2478 (read-color prompt))
2479 ((fboundp 'x-defined-colors)
2480 (completing-read (concat tag ": ")
2481 (widget-color-choice-list)
2482 nil nil nil 'widget-color-history))
2483 (t
2484 (read-string prompt (widget-value widget))))))
2485 (unless (zerop (length answer))
2486 (widget-value-set widget answer)
2487 (widget-apply widget :notify widget event)
2488 (widget-setup))))
2489
2490;;; The Help Echo
2491
2492(defun widget-echo-help-mouse ()
2493 "Display the help message for the widget under the mouse.
2494Enable with (run-with-idle-timer 1 t 'widget-echo-help-mouse)"
2495 (let* ((pos (mouse-position))
2496 (frame (car pos))
2497 (x (car (cdr pos)))
2498 (y (cdr (cdr pos)))
2499 (win (window-at x y frame))
2500 (where (coordinates-in-window-p (cons x y) win)))
2501 (when (consp where)
2502 (save-window-excursion
2503 (progn ; save-excursion
2504 (select-window win)
2505 (let* ((result (compute-motion (window-start win)
2506 '(0 . 0)
2507 (window-end win)
2508 where
2509 (window-width win)
2510 (cons (window-hscroll) 0)
2511 win)))
2512 (when (and (eq (nth 1 result) x)
2513 (eq (nth 2 result) y))
2514 (widget-echo-help (nth 0 result))))))))
2515 (unless track-mouse
2516 (setq track-mouse t)
2517 (add-hook 'post-command-hook 'widget-stop-mouse-tracking)))
2518
2519(defun widget-stop-mouse-tracking (&rest args)
2520 "Stop the mouse tracking done while idle."
2521 (remove-hook 'post-command-hook 'widget-stop-mouse-tracking)
2522 (setq track-mouse nil))
2523
2524(defun widget-at (pos)
2525 "The button or field at POS."
2526 (or (get-text-property pos 'button)
2527 (get-text-property pos 'field)))
2528
2529(defun widget-echo-help (pos)
2530 "Display the help echo for widget at POS."
2531 (let* ((widget (widget-at pos))
2532 (help-echo (and widget (widget-get widget :help-echo))))
2533 (cond ((stringp help-echo)
2534 (message "%s" help-echo))
2535 ((and (symbolp help-echo) (fboundp help-echo)
2536 (stringp (setq help-echo (funcall help-echo widget))))
2537 (message "%s" help-echo)))))
2538
2539;;; The End:
2540
2541(provide 'wid-edit)
2542
2543;; wid-edit.el ends here