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