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