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