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