0
|
1 ;;; widget-edit.el --- Functions for creating and using widgets.
|
|
2 ;;
|
|
3 ;; Copyright (C) 1996 Free Software Foundation, Inc.
|
|
4 ;;
|
|
5 ;; Author: Per Abrahamsen <abraham@dina.kvl.dk>
|
|
6 ;; Keywords: help, extensions, faces, hypermedia
|
|
7 ;; Version: 0.4
|
|
8
|
|
9 ;;; Commentary:
|
|
10 ;;
|
|
11 ;; See `widget.el'.
|
|
12
|
|
13 ;;; Code:
|
|
14
|
|
15 (require 'widget)
|
|
16 (require 'cl)
|
|
17
|
|
18 ;;; Compatibility.
|
|
19
|
|
20 (or (fboundp 'event-point)
|
|
21 ;; XEmacs function missing in Emacs.
|
|
22 (defun event-point (event)
|
|
23 "Return the character position of the given mouse-motion, button-press,
|
|
24 or button-release event. If the event did not occur over a window, or did
|
|
25 not occur over text, then this returns nil. Otherwise, it returns an index
|
|
26 into the buffer visible in the event's window."
|
|
27 (posn-point (event-start event))))
|
|
28
|
|
29 (or (fboundp 'set-keymap-parent)
|
|
30 ;; Xemacs function missing in Emacs.
|
|
31 ;; Definition stolen from `lucid.el'.
|
|
32 (defun set-keymap-parent (keymap new-parent)
|
|
33 (let ((tail keymap))
|
|
34 (while (and tail (cdr tail) (not (eq (car (cdr tail)) 'keymap)))
|
|
35 (setq tail (cdr tail)))
|
|
36 (if tail
|
|
37 (setcdr tail new-parent)))))
|
|
38
|
|
39 ;;; Customization.
|
|
40 ;;
|
|
41 ;; These should be specified with the custom package.
|
|
42
|
|
43 (defvar widget-button-face 'bold)
|
|
44 (defvar widget-mouse-face 'highlight)
|
|
45 (defvar widget-field-face 'italic)
|
|
46
|
2
|
47 (defvar widget-motion-hook nil
|
|
48 "*Hook to be run after widget traversal (via `widget-forward|backward').
|
|
49 The hooks will all be called with on argument - the widget that was just
|
|
50 selected.")
|
|
51
|
0
|
52 ;;; Utility functions.
|
|
53 ;;
|
|
54 ;; These are not really widget specific.
|
|
55
|
|
56 (defun widget-plist-member (plist prop)
|
|
57 ;; Return non-nil if PLIST has the property PROP.
|
|
58 ;; PLIST is a property list, which is a list of the form
|
|
59 ;; (PROP1 VALUE1 PROP2 VALUE2 ...). PROP is a symbol.
|
|
60 ;; Unlike `plist-get', this allows you to distinguish between a missing
|
|
61 ;; property and a property with the value nil.
|
|
62 ;; The value is actually the tail of PLIST whose car is PROP.
|
|
63 (while (and plist (not (eq (car plist) prop)))
|
|
64 (setq plist (cdr (cdr plist))))
|
|
65 plist)
|
|
66
|
|
67 (defun widget-princ-to-string (object)
|
|
68 ;; Return string representation of OBJECT, any Lisp object.
|
|
69 ;; No quoting characters are used; no delimiters are printed around
|
|
70 ;; the contents of strings.
|
|
71 (save-excursion
|
|
72 (set-buffer (get-buffer-create " *widget-tmp*"))
|
|
73 (erase-buffer)
|
|
74 (let ((standard-output (current-buffer)))
|
|
75 (princ object))
|
|
76 (buffer-string)))
|
|
77
|
|
78 (defun widget-clear-undo ()
|
|
79 "Clear all undo information."
|
|
80 (buffer-disable-undo (current-buffer))
|
|
81 (buffer-enable-undo))
|
|
82
|
|
83 ;;; Widget text specifications.
|
|
84 ;;
|
|
85 ;; These functions are for specifying text properties.
|
|
86
|
|
87 (defun widget-specify-none (from to)
|
|
88 ;; Clear all text properties between FROM and TO.
|
|
89 (set-text-properties from to nil))
|
|
90
|
|
91 (defun widget-specify-text (from to)
|
|
92 ;; Default properties.
|
|
93 (add-text-properties from to (list 'read-only t
|
|
94 'front-sticky t
|
|
95 'rear-nonsticky nil)))
|
|
96
|
|
97 (defun widget-specify-field (widget from to)
|
|
98 ;; Specify editable button for WIDGET between FROM and TO.
|
|
99 (widget-specify-field-update widget from to)
|
|
100 ;; Make it possible to edit both end of the field.
|
|
101 (add-text-properties (1- from) from (list 'rear-nonsticky t
|
|
102 'end-open t
|
|
103 'invisible t))
|
|
104 (add-text-properties to (1+ to) (list 'font-sticky nil
|
|
105 'start-open t)))
|
|
106
|
|
107 (defun widget-specify-field-update (widget from to)
|
|
108 ;; Specify editable button for WIDGET between FROM and TO.
|
|
109 (let ((map (widget-get widget :keymap))
|
|
110 (face (or (widget-get widget :value-face)
|
|
111 widget-field-face)))
|
|
112 (add-text-properties from to (list 'field widget
|
|
113 'read-only nil
|
|
114 'local-map map
|
|
115 'keymap map
|
|
116 'face widget-field-face))))
|
|
117
|
|
118 (defun widget-specify-button (widget from to)
|
|
119 ;; Specify button for WIDGET between FROM and TO.
|
|
120 (let ((face (or (widget-get widget :button-face)
|
|
121 widget-button-face)))
|
|
122 (add-text-properties from to (list 'button widget
|
|
123 'mouse-face widget-mouse-face
|
|
124 'face face))))
|
|
125
|
|
126 (defun widget-specify-doc (widget from to)
|
|
127 ;; Specify documentation for WIDGET between FROM and TO.
|
|
128 (put-text-property from to 'widget-doc widget))
|
|
129
|
|
130
|
|
131 (defmacro widget-specify-insert (&rest form)
|
|
132 ;; Execute FORM without inheriting any text properties.
|
2
|
133 (`
|
|
134 (save-restriction
|
0
|
135 (let ((inhibit-read-only t)
|
|
136 result
|
|
137 after-change-functions)
|
|
138 (insert "<>")
|
|
139 (narrow-to-region (- (point) 2) (point))
|
|
140 (widget-specify-none (point-min) (point-max))
|
|
141 (goto-char (1+ (point-min)))
|
2
|
142 (setq result (progn (,@ form)))
|
0
|
143 (delete-region (point-min) (1+ (point-min)))
|
|
144 (delete-region (1- (point-max)) (point-max))
|
|
145 (goto-char (point-max))
|
2
|
146 result))))
|
0
|
147
|
|
148 ;;; Widget Properties.
|
|
149
|
|
150 (defun widget-put (widget property value)
|
|
151 "In WIDGET set PROPERTY to VALUE.
|
|
152 The value can later be retrived with `widget-get'."
|
|
153 (setcdr widget (plist-put (cdr widget) property value)))
|
|
154
|
|
155 (defun widget-get (widget property)
|
|
156 "In WIDGET, get the value of PROPERTY.
|
|
157 The value could either be specified when the widget was created, or
|
|
158 later with `widget-put'."
|
|
159 (cond ((widget-plist-member (cdr widget) property)
|
|
160 (plist-get (cdr widget) property))
|
|
161 ((car widget)
|
|
162 (widget-get (get (car widget) 'widget-type) property))
|
|
163 (t nil)))
|
|
164
|
|
165 (defun widget-member (widget property)
|
|
166 "Non-nil iff there is a definition in WIDGET for PROPERTY."
|
|
167 (cond ((widget-plist-member (cdr widget) property)
|
|
168 t)
|
|
169 ((car widget)
|
|
170 (widget-member (get (car widget) 'widget-type) property))
|
|
171 (t nil)))
|
|
172
|
|
173 (defun widget-apply (widget property &rest args)
|
|
174 "Apply the value of WIDGET's PROPERTY to the widget itself.
|
|
175 ARGS are passed as extra argments to the function."
|
|
176 (apply (widget-get widget property) widget args))
|
|
177
|
|
178 (defun widget-value (widget)
|
|
179 "Extract the current value of WIDGET."
|
|
180 (widget-apply widget
|
|
181 :value-to-external (widget-apply widget :value-get)))
|
|
182
|
|
183 (defun widget-value-set (widget value)
|
|
184 "Set the current value of WIDGET to VALUE."
|
|
185 (widget-apply widget
|
|
186 :value-set (widget-apply widget
|
|
187 :value-to-internal value)))
|
|
188
|
|
189 (defun widget-match-inline (widget values)
|
|
190 ;; Match the head of values.
|
|
191 (cond ((widget-get widget :inline)
|
|
192 (widget-apply widget :match-inline values))
|
|
193 ((widget-apply widget :match (car values))
|
|
194 (cons (list (car values)) (cdr values)))
|
|
195 (t nil)))
|
|
196
|
|
197 ;;; Creating Widgets.
|
|
198
|
|
199 (defun widget-create (type &rest args)
|
|
200 "Create widget of TYPE.
|
|
201 The optional ARGS are additional keyword arguments."
|
|
202 (let ((widget (apply 'widget-convert type args)))
|
|
203 (widget-apply widget :create)
|
|
204 widget))
|
|
205
|
|
206 (defun widget-delete (widget)
|
|
207 "Delete WIDGET."
|
|
208 (widget-apply widget :delete))
|
|
209
|
|
210 (defun widget-convert (type &rest args)
|
|
211 "Convert TYPE to a widget without inserting it in the buffer.
|
|
212 The optional ARGS are additional keyword arguments."
|
|
213 ;; Don't touch the type.
|
|
214 (let* ((widget (if (symbolp type)
|
|
215 (list type)
|
|
216 (copy-list type)))
|
|
217 (current widget)
|
|
218 (keys args))
|
|
219 ;; First set the :args keyword.
|
|
220 (while (cdr current) ;Look in the type.
|
|
221 (let ((next (car (cdr current))))
|
|
222 (if (and (symbolp next) (eq (aref (symbol-name next) 0) ?:))
|
|
223 (setq current (cdr (cdr current)))
|
|
224 (setcdr current (list :args (cdr current)))
|
|
225 (setq current nil))))
|
|
226 (while args ;Look in the args.
|
|
227 (let ((next (nth 0 args)))
|
|
228 (if (and (symbolp next) (eq (aref (symbol-name next) 0) ?:))
|
|
229 (setq args (nthcdr 2 args))
|
|
230 (widget-put widget :args args)
|
|
231 (setq args nil))))
|
|
232 ;; Then Convert the widget.
|
|
233 (setq type widget)
|
|
234 (while type
|
|
235 (let ((convert-widget (widget-get type :convert-widget)))
|
|
236 (if convert-widget
|
|
237 (setq widget (funcall convert-widget widget))))
|
|
238 (setq type (get (car type) 'widget-type)))
|
|
239 ;; Finally set the keyword args.
|
|
240 (while keys
|
|
241 (let ((next (nth 0 keys)))
|
|
242 (if (and (symbolp next) (eq (aref (symbol-name next) 0) ?:))
|
|
243 (progn
|
|
244 (widget-put widget next (nth 1 keys))
|
|
245 (setq keys (nthcdr 2 keys)))
|
|
246 (setq keys nil))))
|
|
247 ;; Return the newly create widget.
|
|
248 widget))
|
|
249
|
|
250 (defun widget-insert (&rest args)
|
|
251 "Call `insert' with ARGS and make the text read only."
|
|
252 (let ((inhibit-read-only t)
|
|
253 after-change-functions
|
|
254 (from (point)))
|
|
255 (apply 'insert args)
|
|
256 (widget-specify-text from (point))))
|
|
257
|
|
258 ;;; Keymap and Comands.
|
|
259
|
|
260 (defvar widget-keymap nil
|
|
261 "Keymap containing useful binding for buffers containing widgets.
|
|
262 Recommended as a parent keymap for modes using widgets.")
|
|
263
|
|
264 (if widget-keymap
|
|
265 ()
|
|
266 (setq widget-keymap (make-sparse-keymap))
|
|
267 (set-keymap-parent widget-keymap global-map)
|
|
268 (define-key widget-keymap "\t" 'widget-forward)
|
|
269 (define-key widget-keymap "\M-\t" 'widget-backward)
|
|
270 (define-key widget-keymap [(shift tab)] 'widget-backward)
|
|
271 (if (string-match "XEmacs" (emacs-version))
|
|
272 (define-key widget-keymap [button2] 'widget-button-click)
|
|
273 (define-key widget-keymap [mouse-2] 'widget-button-click))
|
|
274 (define-key widget-keymap "\C-m" 'widget-button-press))
|
|
275
|
|
276 (defvar widget-global-map global-map
|
|
277 "Keymap used for events the widget does not handle themselves.")
|
|
278 (make-variable-buffer-local 'widget-global-map)
|
|
279
|
|
280 (defun widget-button-click (event)
|
|
281 "Activate button below mouse pointer."
|
|
282 (interactive "@e")
|
|
283 (widget-button-press (event-point event) event))
|
|
284
|
|
285 (defun widget-button-press (pos &optional event)
|
|
286 "Activate button at POS."
|
|
287 (interactive "@d")
|
|
288 (let* ((button (get-text-property pos 'button)))
|
|
289 (if button
|
|
290 (widget-apply button :action event)
|
|
291 (call-interactively
|
|
292 (lookup-key widget-global-map (this-command-keys))))))
|
|
293
|
|
294 (defun widget-forward (arg)
|
|
295 "Move point to the next field or button.
|
|
296 With optional ARG, move across that many fields."
|
|
297 (interactive "p")
|
|
298 (while (> arg 0)
|
|
299 (setq arg (1- arg))
|
|
300 (let ((next (cond ((get-text-property (point) 'button)
|
|
301 (next-single-property-change (point) 'button))
|
|
302 ((get-text-property (point) 'field)
|
|
303 (next-single-property-change (point) 'field))
|
|
304 (t
|
|
305 (point)))))
|
|
306 (if (null next) ; Widget extends to end. of buffer
|
|
307 (setq next (point-min)))
|
|
308 (let ((button (next-single-property-change next 'button))
|
|
309 (field (next-single-property-change next 'field)))
|
|
310 (cond ((or (get-text-property next 'button)
|
|
311 (get-text-property next 'field))
|
|
312 (goto-char next))
|
|
313 ((and button field)
|
|
314 (goto-char (min button field)))
|
|
315 (button (goto-char button))
|
|
316 (field (goto-char field))
|
|
317 (t
|
|
318 (let ((button (next-single-property-change (point-min) 'button))
|
|
319 (field (next-single-property-change (point-min) 'field)))
|
|
320 (cond ((and button field) (goto-char (min button field)))
|
|
321 (button (goto-char button))
|
|
322 (field (goto-char field))
|
|
323 (t
|
|
324 (error "No buttons or fields found")))))))))
|
|
325 (while (< arg 0)
|
|
326 (if (= (point-min) (point))
|
|
327 (forward-char 1))
|
|
328 (setq arg (1+ arg))
|
|
329 (let ((previous (cond ((get-text-property (1- (point)) 'button)
|
|
330 (previous-single-property-change (point) 'button))
|
|
331 ((get-text-property (1- (point)) 'field)
|
|
332 (previous-single-property-change (point) 'field))
|
|
333 (t
|
|
334 (point)))))
|
|
335 (if (null previous) ; Widget extends to beg. of buffer
|
|
336 (setq previous (point-max)))
|
|
337 (let ((button (previous-single-property-change previous 'button))
|
|
338 (field (previous-single-property-change previous 'field)))
|
|
339 (cond ((and button field)
|
|
340 (goto-char (max button field)))
|
|
341 (button (goto-char button))
|
|
342 (field (goto-char field))
|
|
343 (t
|
|
344 (let ((button (previous-single-property-change
|
|
345 (point-max) 'button))
|
|
346 (field (previous-single-property-change
|
|
347 (point-max) 'field)))
|
|
348 (cond ((and button field) (goto-char (max button field)))
|
|
349 (button (goto-char button))
|
|
350 (field (goto-char field))
|
|
351 (t
|
|
352 (error "No buttons or fields found"))))))))
|
|
353 (let ((button (previous-single-property-change (point) 'button))
|
|
354 (field (previous-single-property-change (point) 'field)))
|
|
355 (cond ((and button field)
|
|
356 (goto-char (max button field)))
|
|
357 (button (goto-char button))
|
|
358 (field (goto-char field)))))
|
2
|
359 (run-hook-with-args 'widget-motion-hook (or
|
|
360 (get-text-property (point) 'button)
|
|
361 (get-text-property (point) 'field)))
|
|
362 )
|
0
|
363
|
|
364 (defun widget-backward (arg)
|
|
365 "Move point to the previous field or button.
|
|
366 With optional ARG, move across that many fields."
|
|
367 (interactive "p")
|
|
368 (widget-forward (- arg)))
|
|
369
|
|
370 ;;; Setting up the buffer.
|
|
371
|
|
372 (defvar widget-field-new nil)
|
|
373 ;; List of all newly created editable fields in the buffer.
|
|
374 (make-variable-buffer-local 'widget-field-new)
|
|
375
|
|
376 (defvar widget-field-list nil)
|
|
377 ;; List of all editable fields in the buffer.
|
|
378 (make-variable-buffer-local 'widget-field-list)
|
|
379
|
|
380 (defun widget-setup ()
|
|
381 "Setup current buffer so editing string widgets works."
|
|
382 (let ((inhibit-read-only t)
|
|
383 field)
|
|
384 (while widget-field-new
|
|
385 (setq field (car widget-field-new)
|
|
386 widget-field-new (cdr widget-field-new)
|
|
387 widget-field-list (cons field widget-field-list))
|
|
388 (let ((from (widget-get field :value-from))
|
|
389 (to (widget-get field :value-to)))
|
|
390 (widget-specify-field field from to)
|
|
391 (move-marker from (1- from))
|
|
392 (move-marker to (1+ to)))))
|
|
393 (widget-clear-undo)
|
|
394 ;; We need to maintain text properties and size of the editing fields.
|
|
395 (make-local-variable 'after-change-functions)
|
|
396 (if widget-field-list
|
|
397 (setq after-change-functions '(widget-after-change))
|
|
398 (setq after-change-functions nil)))
|
|
399
|
|
400 (defvar widget-field-last nil)
|
|
401 ;; Last field containing point.
|
|
402 (make-variable-buffer-local 'widget-field-last)
|
|
403
|
|
404 (defvar widget-field-was nil)
|
|
405 ;; The widget data before the change.
|
|
406 (make-variable-buffer-local 'widget-field-was)
|
|
407
|
|
408 (defun widget-field-find (pos)
|
|
409 ;; Find widget whose editing field is located at POS.
|
|
410 ;; Return nil if POS is not inside and editing field.
|
|
411 ;;
|
|
412 ;; This is only used in `widget-field-modified', since ordinarily
|
|
413 ;; you would just test the field property.
|
|
414 (let ((fields widget-field-list)
|
|
415 field found)
|
|
416 (while fields
|
|
417 (setq field (car fields)
|
|
418 fields (cdr fields))
|
|
419 (let ((from (widget-get field :value-from))
|
|
420 (to (widget-get field :value-to)))
|
|
421 (if (and from to (< from pos) (> to pos))
|
|
422 (setq fields nil
|
|
423 found field))))
|
|
424 found))
|
|
425
|
|
426 (defun widget-after-change (from to old)
|
|
427 ;; Adjust field size and text properties.
|
|
428 (condition-case nil
|
|
429 (let ((field (widget-field-find from))
|
|
430 (inhibit-read-only t))
|
|
431 (cond ((null field))
|
|
432 ((not (eq field (widget-field-find to)))
|
|
433 (message "Error: `widget-after-change' called on two fields"))
|
|
434 (t
|
|
435 (let ((size (widget-get field :size)))
|
|
436 (if size
|
|
437 (let ((begin (1+ (widget-get field :value-from)))
|
|
438 (end (1- (widget-get field :value-to))))
|
|
439 (widget-specify-field-update field begin end)
|
|
440 (cond ((< (- end begin) size)
|
|
441 ;; Field too small.
|
|
442 (save-excursion
|
|
443 (goto-char end)
|
|
444 (insert-char ?\ (- (+ begin size) end))))
|
|
445 ((> (- end begin) size)
|
|
446 ;; Field too large and
|
|
447 (if (or (< (point) (+ begin size))
|
|
448 (> (point) end))
|
|
449 ;; Point is outside extra space.
|
|
450 (setq begin (+ begin size))
|
|
451 ;; Point is within the extra space.
|
|
452 (setq begin (point)))
|
|
453 (save-excursion
|
|
454 (goto-char end)
|
|
455 (while (and (eq (preceding-char) ?\ )
|
|
456 (> (point) begin))
|
|
457 (delete-backward-char 1))))))
|
|
458 (widget-specify-field-update field from to)))
|
|
459 (widget-apply field :notify field))))
|
|
460 (error (debug))))
|
|
461
|
|
462 ;;; The `default' Widget.
|
|
463
|
|
464 (define-widget 'default nil
|
|
465 "Basic widget other widgets are derived from."
|
|
466 :value-to-internal (lambda (widget value) value)
|
|
467 :value-to-external (lambda (widget value) value)
|
|
468 :create 'widget-default-create
|
|
469 :format-handler 'widget-default-format-handler
|
|
470 :delete 'widget-default-delete
|
|
471 :value-set 'widget-default-value-set
|
|
472 :value-inline 'widget-default-value-inline
|
|
473 :menu-tag-get 'widget-default-menu-tag-get
|
|
474 :validate (lambda (widget) t)
|
|
475 :action 'widget-default-action
|
|
476 :notify 'widget-default-notify)
|
|
477
|
|
478 (defun widget-default-create (widget)
|
|
479 "Create WIDGET at point in the current buffer."
|
|
480 (widget-specify-insert
|
|
481 (let ((from (point))
|
|
482 (tag (widget-get widget :tag))
|
|
483 (doc (widget-get widget :doc))
|
|
484 button-begin button-end
|
|
485 doc-begin doc-end
|
|
486 value-pos)
|
|
487 (insert (widget-get widget :format))
|
|
488 (goto-char from)
|
|
489 ;; Parse % escapes in format.
|
|
490 (while (re-search-forward "%\\(.\\)" nil t)
|
|
491 (let ((escape (aref (match-string 1) 0)))
|
|
492 (replace-match "" t t)
|
|
493 (cond ((eq escape ?%)
|
|
494 (insert "%"))
|
|
495 ((eq escape ?\[)
|
|
496 (setq button-begin (point)))
|
|
497 ((eq escape ?\])
|
|
498 (setq button-end (point)))
|
|
499 ((eq escape ?t)
|
|
500 (if tag
|
|
501 (insert tag)
|
|
502 (let ((standard-output (current-buffer)))
|
|
503 (princ (widget-get widget :value)))))
|
|
504 ((eq escape ?d)
|
|
505 (when doc
|
|
506 (setq doc-begin (point))
|
|
507 (insert doc)
|
|
508 (while (eq (preceding-char) ?\n)
|
|
509 (delete-backward-char 1))
|
|
510 (insert "\n")
|
|
511 (setq doc-end (point))))
|
|
512 ((eq escape ?v)
|
|
513 (if (and button-begin (not button-end))
|
|
514 (widget-apply widget :value-create)
|
|
515 (setq value-pos (point))))
|
|
516 (t
|
|
517 (widget-apply widget :format-handler escape)))))
|
|
518 ;; Specify button and doc, and insert value.
|
|
519 (and button-begin button-end
|
|
520 (widget-specify-button widget button-begin button-end))
|
|
521 (and doc-begin doc-end
|
|
522 (widget-specify-doc widget doc-begin doc-end))
|
|
523 (when value-pos
|
|
524 (goto-char value-pos)
|
|
525 (widget-apply widget :value-create)))
|
|
526 (let ((from (copy-marker (point-min)))
|
|
527 (to (copy-marker (point-max))))
|
|
528 (widget-specify-text from to)
|
|
529 (set-marker-insertion-type from t)
|
|
530 (set-marker-insertion-type to nil)
|
|
531 (widget-put widget :from from)
|
|
532 (widget-put widget :to to))))
|
|
533
|
|
534 (defun widget-default-format-handler (widget escape)
|
|
535 ;; By default unknown escapes are errors.
|
|
536 (error "Unknown escape `%c'" escape))
|
|
537
|
|
538 (defun widget-default-delete (widget)
|
|
539 ;; Remove widget from the buffer.
|
|
540 (let ((from (widget-get widget :from))
|
|
541 (to (widget-get widget :to))
|
|
542 (inhibit-read-only t)
|
|
543 after-change-functions)
|
|
544 (widget-apply widget :value-delete)
|
|
545 (delete-region from to)
|
|
546 (set-marker from nil)
|
|
547 (set-marker to nil)))
|
|
548
|
|
549 (defun widget-default-value-set (widget value)
|
|
550 ;; Recreate widget with new value.
|
|
551 (save-excursion
|
|
552 (goto-char (widget-get widget :from))
|
|
553 (widget-apply widget :delete)
|
|
554 (widget-put widget :value value)
|
|
555 (widget-apply widget :create)))
|
|
556
|
|
557 (defun widget-default-value-inline (widget)
|
|
558 ;; Wrap value in a list unless it is inline.
|
|
559 (if (widget-get widget :inline)
|
|
560 (widget-value widget)
|
|
561 (list (widget-value widget))))
|
|
562
|
|
563 (defun widget-default-menu-tag-get (widget)
|
|
564 ;; Use tag or value for menus.
|
|
565 (or (widget-get widget :menu-tag)
|
|
566 (widget-get widget :tag)
|
|
567 (widget-princ-to-string (widget-get widget :value))))
|
|
568
|
|
569 (defun widget-default-action (widget &optional event)
|
|
570 ;; Notify the parent when a widget change
|
|
571 (let ((parent (widget-get widget :parent)))
|
|
572 (when parent
|
|
573 (widget-apply parent :notify widget event))))
|
|
574
|
|
575 (defun widget-default-notify (widget child &optional event)
|
|
576 ;; Pass notification to parent.
|
|
577 (widget-default-action widget event))
|
|
578
|
|
579 ;;; The `item' Widget.
|
|
580
|
|
581 (define-widget 'item 'default
|
|
582 "Constant items for inclusion in other widgets."
|
|
583 :convert-widget 'widget-item-convert-widget
|
|
584 :value-create 'widget-item-value-create
|
|
585 :value-delete 'ignore
|
|
586 :value-get 'widget-item-value-get
|
|
587 :match 'widget-item-match
|
|
588 :match-inline 'widget-item-match-inline
|
|
589 :action 'widget-item-action
|
|
590 :format "%t\n")
|
|
591
|
|
592 (defun widget-item-convert-widget (widget)
|
|
593 ;; Initialize :value and :tag from :args in WIDGET.
|
|
594 (let ((args (widget-get widget :args)))
|
|
595 (when args
|
|
596 (widget-put widget :value (car args))
|
|
597 (widget-put widget :args nil)))
|
|
598 widget)
|
|
599
|
|
600 (defun widget-item-value-create (widget)
|
|
601 ;; Insert the printed representation of the value.
|
|
602 (let ((standard-output (current-buffer)))
|
|
603 (princ (widget-get widget :value))))
|
|
604
|
|
605 (defun widget-item-match (widget value)
|
|
606 ;; Match if the value is the same.
|
|
607 (equal (widget-get widget :value) value))
|
|
608
|
|
609 (defun widget-item-match-inline (widget values)
|
|
610 ;; Match if the value is the same.
|
|
611 (let ((value (widget-get widget :value)))
|
|
612 (and (listp value)
|
|
613 (<= (length value) (length values))
|
|
614 (let ((head (subseq values 0 (length value))))
|
|
615 (and (equal head value)
|
|
616 (cons head (subseq values (length value))))))))
|
|
617
|
|
618 (defun widget-item-action (widget &optional event)
|
|
619 ;; Just notify itself.
|
|
620 (widget-apply widget :notify widget event))
|
|
621
|
|
622 (defun widget-item-value-get (widget)
|
|
623 ;; Items are simple.
|
|
624 (widget-get widget :value))
|
|
625
|
|
626 ;;; The `push' Widget.
|
|
627
|
|
628 (define-widget 'push 'item
|
|
629 "A pushable button."
|
|
630 :format "%[[%t]%]")
|
|
631
|
|
632 ;;; The `link' Widget.
|
|
633
|
|
634 (define-widget 'link 'item
|
|
635 "An embedded link."
|
|
636 :format "%[_%t_%]")
|
|
637
|
|
638 ;;; The `field' Widget.
|
|
639
|
|
640 (define-widget 'field 'default
|
|
641 "An editable text field."
|
|
642 :convert-widget 'widget-item-convert-widget
|
|
643 :format "%v"
|
|
644 :value ""
|
|
645 :tag "field"
|
|
646 :value-create 'widget-field-value-create
|
|
647 :value-delete 'widget-field-value-delete
|
|
648 :value-get 'widget-field-value-get
|
|
649 :match 'widget-field-match)
|
|
650
|
|
651 (defun widget-field-value-create (widget)
|
|
652 ;; Create an editable text field.
|
|
653 (insert " ")
|
|
654 (let ((size (widget-get widget :size))
|
|
655 (value (widget-get widget :value))
|
|
656 (from (point)))
|
|
657 (if (null size)
|
|
658 (insert value)
|
|
659 (insert value)
|
|
660 (if (< (length value) size)
|
|
661 (insert-char ?\ (- size (length value)))))
|
|
662 (unless (memq widget widget-field-list)
|
|
663 (setq widget-field-new (cons widget widget-field-new)))
|
|
664 (widget-put widget :value-from (copy-marker from))
|
|
665 (set-marker-insertion-type (widget-get widget :value-from) t)
|
|
666 (widget-put widget :value-to (copy-marker (point)))
|
|
667 (set-marker-insertion-type (widget-get widget :value-to) nil)
|
|
668 (if (null size)
|
|
669 (insert ?\n)
|
|
670 (insert ?\ ))))
|
|
671
|
|
672 (defun widget-field-value-delete (widget)
|
|
673 ;; Remove the widget from the list of active editing fields.
|
|
674 (setq widget-field-list (delq widget widget-field-list))
|
|
675 (set-marker (widget-get widget :value-from) nil)
|
|
676 (set-marker (widget-get widget :value-to) nil))
|
|
677
|
|
678 (defun widget-field-value-get (widget)
|
|
679 ;; Return current text in editing field.
|
|
680 (let ((from (widget-get widget :value-from))
|
|
681 (to (widget-get widget :value-to)))
|
|
682 (if (and from to)
|
|
683 (progn
|
|
684 (setq from (1+ from)
|
|
685 to (1- to))
|
|
686 (while (and (> to from)
|
|
687 (eq (char-after (1- to)) ?\ ))
|
|
688 (setq to (1- to)))
|
|
689 (buffer-substring-no-properties from to))
|
|
690 (widget-get widget :value))))
|
|
691
|
|
692 (defun widget-field-match (widget value)
|
|
693 ;; Match any string.
|
|
694 (stringp value))
|
|
695
|
|
696 ;;; The `choice' Widget.
|
|
697
|
|
698 (define-widget 'choice 'default
|
|
699 "A menu of options."
|
|
700 :convert-widget 'widget-choice-convert-widget
|
|
701 :format "%[%t%]: %v"
|
|
702 :tag "choice"
|
|
703 :inline t
|
|
704 :void '(item "void")
|
|
705 :value-create 'widget-choice-value-create
|
|
706 :value-delete 'widget-radio-value-delete
|
|
707 :value-get 'widget-choice-value-get
|
|
708 :value-inline 'widget-choice-value-inline
|
|
709 :action 'widget-choice-action
|
|
710 :error "Make a choice"
|
|
711 :validate 'widget-choice-validate
|
|
712 :match 'widget-choice-match
|
|
713 :match-inline 'widget-choice-match-inline)
|
|
714
|
|
715 (defun widget-choice-convert-widget (widget)
|
|
716 ;; Expand type args into widget objects.
|
|
717 (widget-put widget :args (mapcar 'widget-convert (widget-get widget :args)))
|
|
718 widget)
|
|
719
|
|
720 (defun widget-choice-value-create (widget)
|
|
721 ;; Insert the first choice that matches the value.
|
|
722 (let ((value (widget-get widget :value))
|
|
723 (args (widget-get widget :args))
|
|
724 current)
|
|
725 (while args
|
|
726 (setq current (car args)
|
|
727 args (cdr args))
|
|
728 (when (widget-apply current :match value)
|
|
729 (widget-put widget :children (list (widget-create current
|
|
730 :parent widget
|
|
731 :value value)))
|
|
732 (widget-put widget :choice current)
|
|
733 (setq args nil
|
|
734 current nil)))
|
|
735 (when current
|
|
736 (let ((void (widget-get widget :void)))
|
|
737 (widget-put widget :children (list (widget-create void
|
|
738 :parent widget
|
|
739 :value value)))
|
|
740 (widget-put widget :choice void)))))
|
|
741
|
|
742 (defun widget-choice-value-get (widget)
|
|
743 ;; Get value of the child widget.
|
|
744 (widget-value (car (widget-get widget :children))))
|
|
745
|
|
746 (defun widget-choice-value-inline (widget)
|
|
747 ;; Get value of the child widget.
|
|
748 (widget-apply (car (widget-get widget :children)) :value-inline))
|
|
749
|
|
750 (defun widget-choice-action (widget &optional event)
|
|
751 ;; Make a choice.
|
|
752 (let ((args (widget-get widget :args))
|
|
753 (old (widget-get widget :choice))
|
|
754 (tag (widget-apply widget :menu-tag-get))
|
|
755 current choices)
|
|
756 (setq current
|
|
757 (cond ((= (length args) 0)
|
|
758 nil)
|
|
759 ((= (length args) 1)
|
|
760 (nth 0 args))
|
|
761 ((and (= (length args) 2)
|
|
762 (memq old args))
|
|
763 (if (eq old (nth 0 args))
|
|
764 (nth 1 args)
|
|
765 (nth 0 args)))
|
|
766 (t
|
|
767 (while args
|
|
768 (setq current (car args)
|
|
769 args (cdr args))
|
|
770 (setq choices
|
|
771 (cons (cons (widget-apply current :menu-tag-get)
|
|
772 current)
|
|
773 choices)))
|
|
774 (cond
|
|
775 ((and event (fboundp 'x-popup-menu) window-system)
|
|
776 ;; We are in Emacs-19, pressed by the mouse
|
|
777 (x-popup-menu event
|
|
778 (list tag (cons "" (reverse choices)))))
|
|
779 ((and event (fboundp 'popup-menu) window-system)
|
|
780 ;; We are in XEmacs, pressed by the mouse
|
|
781 (let ((val (get-popup-menu-response
|
|
782 (cons ""
|
|
783 (mapcar
|
|
784 (function
|
|
785 (lambda (x)
|
|
786 (vector (car x) (list (car x)) t)))
|
|
787 (reverse choices))))))
|
|
788 (setq val (and val
|
|
789 (listp (event-object val))
|
|
790 (stringp (car-safe (event-object val)))
|
|
791 (car (event-object val))))
|
|
792 (cdr (assoc val choices))))
|
|
793 (t
|
|
794 (cdr (assoc (completing-read (concat tag ": ")
|
|
795 choices nil t)
|
|
796 choices)))))))
|
|
797 (when current
|
|
798 (widget-value-set widget (widget-value current))
|
|
799 (widget-setup)))
|
|
800 ;; Notify parent.
|
|
801 (widget-apply widget :notify widget event)
|
|
802 (widget-clear-undo))
|
|
803
|
|
804 (defun widget-choice-validate (widget)
|
|
805 ;; Valid if we have made a valid choice.
|
|
806 (let ((void (widget-get widget :void))
|
|
807 (choice (widget-get widget :choice))
|
|
808 (child (car (widget-get widget :children))))
|
|
809 (if (eq void choice)
|
|
810 widget
|
|
811 (widget-apply child :validate))))
|
|
812
|
|
813 (defun widget-choice-match (widget value)
|
|
814 ;; Matches if one of the choices matches.
|
|
815 (let ((args (widget-get widget :args))
|
|
816 current found)
|
|
817 (while (and args (not found))
|
|
818 (setq current (car args)
|
|
819 args (cdr args)
|
|
820 found (widget-apply current :match value)))
|
|
821 found))
|
|
822
|
|
823 (defun widget-choice-match-inline (widget values)
|
|
824 ;; Matches if one of the choices matches.
|
|
825 (let ((args (widget-get widget :args))
|
|
826 current found)
|
|
827 (while (and args (null found))
|
|
828 (setq current (car args)
|
|
829 args (cdr args)
|
|
830 found (widget-match-inline current values)))
|
|
831 found))
|
|
832
|
|
833 ;;; The `toggle' Widget.
|
|
834
|
|
835 (define-widget 'toggle 'choice
|
|
836 "Toggle between two states."
|
|
837 :convert-widget 'widget-toggle-convert-widget
|
|
838 :format "%[%v%]"
|
|
839 :on "on"
|
|
840 :off "off")
|
|
841
|
|
842 (defun widget-toggle-convert-widget (widget)
|
|
843 ;; Create the types representing the `on' and `off' states.
|
|
844 (let ((args (widget-get widget :args))
|
|
845 (on-type (widget-get widget :on-type))
|
|
846 (off-type (widget-get widget :off-type)))
|
|
847 (unless on-type
|
|
848 (setq on-type (list 'item :value t :tag (widget-get widget :on))))
|
|
849 (unless off-type
|
|
850 (setq off-type (list 'item :value nil :tag (widget-get widget :off))))
|
|
851 (widget-put widget :args (list on-type off-type)))
|
|
852 widget)
|
|
853
|
|
854 ;;; The `checkbox' Widget.
|
|
855
|
|
856 (define-widget 'checkbox 'toggle
|
|
857 "A checkbox toggle."
|
|
858 :convert-widget 'widget-item-convert-widget
|
|
859 :on-type '(item :format "[X]" t)
|
|
860 :off-type '(item :format "[ ]" nil))
|
|
861
|
|
862 ;;; The `checklist' Widget.
|
|
863
|
|
864 (define-widget 'checklist 'default
|
|
865 "A multiple choice widget."
|
|
866 :convert-widget 'widget-choice-convert-widget
|
|
867 :format "%v"
|
|
868 :entry-format "%b %v"
|
|
869 :menu-tag "checklist"
|
|
870 :value-create 'widget-checklist-value-create
|
|
871 :value-delete 'widget-radio-value-delete
|
|
872 :value-get 'widget-checklist-value-get
|
|
873 :validate 'widget-checklist-validate
|
|
874 :match 'widget-checklist-match
|
|
875 :match-inline 'widget-checklist-match-inline)
|
|
876
|
|
877 (defun widget-checklist-value-create (widget)
|
|
878 ;; Insert all values
|
|
879 (let ((alist (widget-checklist-match-find widget (widget-get widget :value)))
|
|
880 (args (widget-get widget :args)))
|
|
881 (while args
|
|
882 (widget-checklist-add-item widget (car args) (assq (car args) alist))
|
|
883 (setq args (cdr args)))
|
|
884 (widget-put widget :children (nreverse (widget-get widget :children)))))
|
|
885
|
|
886 (defun widget-checklist-add-item (widget type chosen)
|
|
887 ;; Create checklist item in WIDGET of type TYPE.
|
|
888 ;; If the item is checked, CHOSEN is a cons whose cdr is the value.
|
|
889 (widget-specify-insert
|
|
890 (let* ((children (widget-get widget :children))
|
|
891 (buttons (widget-get widget :buttons))
|
|
892 (from (point))
|
|
893 child button)
|
|
894 (insert (widget-get widget :entry-format))
|
|
895 (goto-char from)
|
|
896 ;; Parse % escapes in format.
|
|
897 (while (re-search-forward "%\\([bv%]\\)" nil t)
|
|
898 (let ((escape (aref (match-string 1) 0)))
|
|
899 (replace-match "" t t)
|
|
900 (cond ((eq escape ?%)
|
|
901 (insert "%"))
|
|
902 ((eq escape ?b)
|
|
903 (setq button (widget-create 'checkbox
|
|
904 :parent widget
|
|
905 :value (not (null chosen)))))
|
|
906 ((eq escape ?v)
|
|
907 (setq child
|
|
908 (cond ((not chosen)
|
|
909 (widget-create type :parent widget))
|
|
910 ((widget-get type :inline)
|
|
911 (widget-create type
|
|
912 :parent widget
|
|
913 :value (cdr chosen)))
|
|
914 (t
|
|
915 (widget-create type
|
|
916 :parent widget
|
|
917 :value (car (cdr chosen)))))))
|
|
918 (t
|
|
919 (error "Unknown escape `%c'" escape)))))
|
|
920 ;; Update properties.
|
|
921 (and button child (widget-put child :button button))
|
|
922 (and button (widget-put widget :buttons (cons button buttons)))
|
|
923 (and child (widget-put widget :children (cons child children))))))
|
|
924
|
|
925 (defun widget-checklist-match (widget values)
|
|
926 ;; All values must match a type in the checklist.
|
|
927 (and (listp values)
|
|
928 (null (cdr (widget-checklist-match-inline widget values)))))
|
|
929
|
|
930 (defun widget-checklist-match-inline (widget values)
|
|
931 ;; Find the values which match a type in the checklist.
|
|
932 (let ((greedy (widget-get widget :greedy))
|
|
933 (args (copy-list (widget-get widget :args)))
|
|
934 found rest)
|
|
935 (while values
|
|
936 (let ((answer (widget-checklist-match-up args values)))
|
|
937 (cond (answer
|
|
938 (let ((vals (widget-match-inline answer values)))
|
|
939 (setq found (append found (car vals))
|
|
940 values (cdr vals)
|
|
941 args (delq answer args))))
|
|
942 (greedy
|
|
943 (setq rest (append rest (list (car values)))
|
|
944 values (cdr values)))
|
|
945 (t
|
|
946 (setq rest (append rest values)
|
|
947 values nil)))))
|
|
948 (cons found rest)))
|
|
949
|
|
950 (defun widget-checklist-match-find (widget values)
|
|
951 ;; Find the values which match a type in the checklist.
|
|
952 ;; Return an alist of (TYPE MATCH).
|
|
953 (let ((greedy (widget-get widget :greedy))
|
|
954 (args (copy-list (widget-get widget :args)))
|
|
955 found)
|
|
956 (while values
|
|
957 (let ((answer (widget-checklist-match-up args values)))
|
|
958 (cond (answer
|
|
959 (let ((vals (widget-match-inline answer values)))
|
|
960 (setq found (cons (cons answer (car vals)) found)
|
|
961 values (cdr vals)
|
|
962 args (delq answer args))))
|
|
963 (greedy
|
|
964 (setq values (cdr values)))
|
|
965 (t
|
|
966 (setq values nil)))))
|
|
967 found))
|
|
968
|
|
969 (defun widget-checklist-match-up (args values)
|
|
970 ;; Rerturn the first type from ARGS that matches VALUES.
|
|
971 (let (current found)
|
|
972 (while (and args (null found))
|
|
973 (setq current (car args)
|
|
974 args (cdr args)
|
|
975 found (widget-match-inline current values)))
|
|
976 (and found current)))
|
|
977
|
|
978 (defun widget-checklist-value-get (widget)
|
|
979 ;; The values of all selected items.
|
|
980 (let ((children (widget-get widget :children))
|
|
981 child result)
|
|
982 (while children
|
|
983 (setq child (car children)
|
|
984 children (cdr children))
|
|
985 (if (widget-value (widget-get child :button))
|
|
986 (setq result (append result (widget-apply child :value-inline)))))
|
|
987 result))
|
|
988
|
|
989 (defun widget-checklist-validate (widget)
|
|
990 ;; Ticked chilren must be valid.
|
|
991 (let ((children (widget-get widget :children))
|
|
992 child button found)
|
|
993 (while (and children (not found))
|
|
994 (setq child (car children)
|
|
995 children (cdr children)
|
|
996 button (widget-get child :button)
|
|
997 found (and (widget-value button)
|
|
998 (widget-apply child :validate))))
|
|
999 found))
|
|
1000
|
|
1001 ;;; The `option' Widget
|
|
1002
|
|
1003 (define-widget 'option 'checklist
|
|
1004 "An widget with an optional item."
|
|
1005 :inline t)
|
|
1006
|
|
1007 ;;; The `choice-item' Widget.
|
|
1008
|
|
1009 (define-widget 'choice-item 'item
|
|
1010 "Button items that delegate action events to their parents."
|
|
1011 :action 'widget-choice-item-action
|
|
1012 :format "%[%t%]\n")
|
|
1013
|
|
1014 (defun widget-choice-item-action (widget &optional event)
|
|
1015 ;; Tell parent what happened.
|
|
1016 (widget-apply (widget-get widget :parent) :action event))
|
|
1017
|
|
1018 ;;; The `radio-button' Widget.
|
|
1019
|
|
1020 (define-widget 'radio-button 'toggle
|
|
1021 "A radio button for use in the `radio' widget."
|
|
1022 :format "%v"
|
|
1023 :notify 'widget-radio-button-notify
|
|
1024 :on-type '(choice-item :format "%[(*)%]" t)
|
|
1025 :off-type '(choice-item :format "%[( )%]" nil))
|
|
1026
|
|
1027 (defun widget-radio-button-notify (widget child &optional event)
|
|
1028 ;; Notify the parent.
|
|
1029 (widget-apply (widget-get widget :parent) :action widget event))
|
|
1030
|
|
1031 ;;; The `radio' Widget.
|
|
1032
|
|
1033 (define-widget 'radio 'default
|
|
1034 "Select one of multiple options."
|
|
1035 :convert-widget 'widget-choice-convert-widget
|
|
1036 :format "%v"
|
|
1037 :entry-format "%b %v"
|
|
1038 :menu-tag "radio"
|
|
1039 :value-create 'widget-radio-value-create
|
|
1040 :value-delete 'widget-radio-value-delete
|
|
1041 :value-get 'widget-radio-value-get
|
|
1042 :value-inline 'widget-radio-value-inline
|
|
1043 :value-set 'widget-radio-value-set
|
|
1044 :error "You must push one of the buttons"
|
|
1045 :validate 'widget-radio-validate
|
|
1046 :match 'widget-choice-match
|
|
1047 :match-inline 'widget-choice-match-inline
|
|
1048 :action 'widget-radio-action)
|
|
1049
|
|
1050 (defun widget-radio-value-create (widget)
|
|
1051 ;; Insert all values
|
|
1052 (let ((args (widget-get widget :args))
|
|
1053 (indent (widget-get widget :indent))
|
|
1054 arg)
|
|
1055 (while args
|
|
1056 (setq arg (car args)
|
|
1057 args (cdr args))
|
|
1058 (widget-radio-add-item widget arg)
|
|
1059 (and indent args (insert-char ?\ indent)))))
|
|
1060
|
|
1061 (defun widget-radio-add-item (widget type)
|
|
1062 "Add to radio widget WIDGET a new radio button item of type TYPE."
|
|
1063 (setq type (widget-convert type))
|
|
1064 (widget-specify-insert
|
|
1065 (let* ((value (widget-get widget :value))
|
|
1066 (children (widget-get widget :children))
|
|
1067 (buttons (widget-get widget :buttons))
|
|
1068 (from (point))
|
|
1069 (chosen (and (null (widget-get widget :choice))
|
|
1070 (widget-apply type :match value)))
|
|
1071 child button)
|
|
1072 (insert (widget-get widget :entry-format))
|
|
1073 (goto-char from)
|
|
1074 ;; Parse % escapes in format.
|
|
1075 (while (re-search-forward "%\\([bv%]\\)" nil t)
|
|
1076 (let ((escape (aref (match-string 1) 0)))
|
|
1077 (replace-match "" t t)
|
|
1078 (cond ((eq escape ?%)
|
|
1079 (insert "%"))
|
|
1080 ((eq escape ?b)
|
|
1081 (setq button (widget-create 'radio-button
|
|
1082 :parent widget
|
|
1083 :value (not (null chosen)))))
|
|
1084 ((eq escape ?v)
|
|
1085 (setq child (if chosen
|
|
1086 (widget-create type
|
|
1087 :parent widget
|
|
1088 :value value)
|
|
1089 (widget-create type :parent widget))))
|
|
1090 (t
|
|
1091 (error "Unknown escape `%c'" escape)))))
|
|
1092 ;; Update properties.
|
|
1093 (when chosen
|
|
1094 (widget-put widget :choice type))
|
|
1095 (when button
|
|
1096 (widget-put child :button button)
|
|
1097 (widget-put widget :buttons (nconc buttons (list button))))
|
|
1098 (when child
|
|
1099 (widget-put widget :children (nconc children (list child))))
|
|
1100 child)))
|
|
1101
|
|
1102 (defun widget-radio-value-delete (widget)
|
|
1103 ;; Delete the child widgets.
|
|
1104 (mapcar 'widget-delete (widget-get widget :children))
|
|
1105 (widget-put widget :children nil)
|
|
1106 (mapcar 'widget-delete (widget-get widget :buttons))
|
|
1107 (widget-put widget :buttons nil))
|
|
1108
|
|
1109 (defun widget-radio-value-get (widget)
|
|
1110 ;; Get value of the child widget.
|
|
1111 (let ((chosen (widget-radio-chosen widget)))
|
|
1112 (and chosen (widget-value chosen))))
|
|
1113
|
|
1114 (defun widget-radio-chosen (widget)
|
|
1115 "Return the widget representing the chosen radio button."
|
|
1116 (let ((children (widget-get widget :children))
|
|
1117 current found)
|
|
1118 (while children
|
|
1119 (setq current (car children)
|
|
1120 children (cdr children))
|
|
1121 (let* ((button (widget-get current :button))
|
|
1122 (value (widget-apply button :value-get)))
|
|
1123 (when value
|
|
1124 (setq found current
|
|
1125 children nil))))
|
|
1126 found))
|
|
1127
|
|
1128 (defun widget-radio-value-inline (widget)
|
|
1129 ;; Get value of the child widget.
|
|
1130 (let ((children (widget-get widget :children))
|
|
1131 current found)
|
|
1132 (while children
|
|
1133 (setq current (car children)
|
|
1134 children (cdr children))
|
|
1135 (let* ((button (widget-get current :button))
|
|
1136 (value (widget-apply button :value-get)))
|
|
1137 (when value
|
|
1138 (setq found (widget-apply current :value-inline)
|
|
1139 children nil))))
|
|
1140 found))
|
|
1141
|
|
1142 (defun widget-radio-value-set (widget value)
|
|
1143 ;; We can't just delete and recreate a radio widget, since children
|
|
1144 ;; can be added after the original creation and won't be recreated
|
|
1145 ;; by `:create'.
|
|
1146 (let ((children (widget-get widget :children))
|
|
1147 current found)
|
|
1148 (while children
|
|
1149 (setq current (car children)
|
|
1150 children (cdr children))
|
|
1151 (let* ((button (widget-get current :button))
|
|
1152 (match (and (not found)
|
|
1153 (widget-apply current :match value))))
|
|
1154 (widget-value-set button match)
|
|
1155 (if match
|
|
1156 (widget-value-set current value))
|
|
1157 (setq found (or found match))))))
|
|
1158
|
|
1159 (defun widget-radio-validate (widget)
|
|
1160 ;; Valid if we have made a valid choice.
|
|
1161 (let ((children (widget-get widget :children))
|
|
1162 current found button)
|
|
1163 (while (and children (not found))
|
|
1164 (setq current (car children)
|
|
1165 children (cdr children)
|
|
1166 button (widget-get current :button)
|
|
1167 found (widget-apply button :value-get)))
|
|
1168 (if found
|
|
1169 (widget-apply current :validate)
|
|
1170 widget)))
|
|
1171
|
|
1172 (defun widget-radio-action (widget child event)
|
|
1173 ;; Check if a radio button was pressed.
|
|
1174 (let ((children (widget-get widget :children))
|
|
1175 (buttons (widget-get widget :buttons))
|
|
1176 current)
|
|
1177 (when (memq child buttons)
|
|
1178 (while children
|
|
1179 (setq current (car children)
|
|
1180 children (cdr children))
|
|
1181 (let* ((button (widget-get current :button)))
|
|
1182 (cond ((eq child button)
|
|
1183 (widget-value-set button t))
|
|
1184 ((widget-value button)
|
|
1185 (widget-value-set button nil)))))))
|
|
1186 ;; Pass notification to parent.
|
|
1187 (widget-apply widget :notify child event))
|
|
1188
|
|
1189 ;;; The `insert-button' Widget.
|
|
1190
|
|
1191 (define-widget 'insert-button 'push
|
|
1192 "An insert button for the `repeat' widget."
|
|
1193 :tag "INS"
|
|
1194 :action 'widget-insert-button-action)
|
|
1195
|
|
1196 (defun widget-insert-button-action (widget &optional event)
|
|
1197 ;; Ask the parent to insert a new item.
|
|
1198 (widget-apply (widget-get widget :parent)
|
|
1199 :insert-before (widget-get widget :widget)))
|
|
1200
|
|
1201 ;;; The `delete-button' Widget.
|
|
1202
|
|
1203 (define-widget 'delete-button 'push
|
|
1204 "A delete button for the `repeat' widget."
|
|
1205 :tag "DEL"
|
|
1206 :action 'widget-delete-button-action)
|
|
1207
|
|
1208 (defun widget-delete-button-action (widget &optional event)
|
|
1209 ;; Ask the parent to insert a new item.
|
|
1210 (widget-apply (widget-get widget :parent)
|
|
1211 :delete-at (widget-get widget :widget)))
|
|
1212
|
|
1213 ;;; The `repeat' Widget.
|
|
1214
|
|
1215 (define-widget 'repeat 'default
|
|
1216 "A variable list of widgets of the same type."
|
|
1217 :convert-widget 'widget-choice-convert-widget
|
|
1218 :format "%v%i\n"
|
|
1219 :format-handler 'widget-repeat-format-handler
|
|
1220 :entry-format "%i %d %v"
|
|
1221 :menu-tag "repeat"
|
|
1222 :value-create 'widget-repeat-value-create
|
|
1223 :value-delete 'widget-radio-value-delete
|
|
1224 :value-get 'widget-repeat-value-get
|
|
1225 :validate 'widget-repeat-validate
|
|
1226 :match 'widget-repeat-match
|
|
1227 :match-inline 'widget-repeat-match-inline
|
|
1228 :insert-before 'widget-repeat-insert-before
|
|
1229 :delete-at 'widget-repeat-delete-at)
|
|
1230
|
|
1231 (defun widget-repeat-format-handler (widget escape)
|
|
1232 ;; We recognize the insert button.
|
|
1233 (cond ((eq escape ?i)
|
|
1234 (insert " ")
|
|
1235 (backward-char 1)
|
|
1236 (let* ((from (point))
|
|
1237 (button (widget-create (list 'insert-button
|
|
1238 :parent widget))))
|
|
1239 (widget-specify-button button from (point)))
|
|
1240 (forward-char 1))
|
|
1241 (t
|
|
1242 (widget-default-format-handler widget escape))))
|
|
1243
|
|
1244 (defun widget-repeat-value-create (widget)
|
|
1245 ;; Insert all values
|
|
1246 (let* ((value (widget-get widget :value))
|
|
1247 (type (nth 0 (widget-get widget :args)))
|
|
1248 (inlinep (widget-get type :inline))
|
|
1249 children)
|
|
1250 (widget-put widget :value-pos (copy-marker (point)))
|
|
1251 (set-marker-insertion-type (widget-get widget :value-pos) t)
|
|
1252 (while value
|
|
1253 (let ((answer (widget-match-inline type value)))
|
|
1254 (if answer
|
|
1255 (setq children (cons (widget-repeat-entry-create
|
|
1256 widget (if inlinep
|
|
1257 (car answer)
|
|
1258 (car (car answer))))
|
|
1259 children)
|
|
1260 value (cdr answer))
|
|
1261 (setq value nil))))
|
|
1262 (widget-put widget :children (nreverse children))))
|
|
1263
|
|
1264 (defun widget-repeat-value-get (widget)
|
|
1265 ;; Get value of the child widget.
|
|
1266 (apply 'append (mapcar (lambda (child) (widget-apply child :value-inline))
|
|
1267 (widget-get widget :children))))
|
|
1268
|
|
1269 (defun widget-repeat-validate (widget)
|
|
1270 ;; All the chilren must be valid.
|
|
1271 (let ((children (widget-get widget :children))
|
|
1272 child found)
|
|
1273 (while (and children (not found))
|
|
1274 (setq child (car children)
|
|
1275 children (cdr children)
|
|
1276 found (widget-apply child :validate)))
|
|
1277 found))
|
|
1278
|
|
1279 (defun widget-repeat-match (widget value)
|
|
1280 ;; Value must be a list and all the members must match the repeat type.
|
|
1281 (and (listp value)
|
|
1282 (null (cdr (widget-repeat-match-inline widget value)))))
|
|
1283
|
|
1284 (defun widget-repeat-match-inline (widget value)
|
|
1285 (let ((type (nth 0 (widget-get widget :args)))
|
|
1286 (ok t)
|
|
1287 found)
|
|
1288 (while (and value ok)
|
|
1289 (let ((answer (widget-match-inline type value)))
|
|
1290 (if answer
|
|
1291 (setq found (append found (car answer))
|
|
1292 value (cdr answer))
|
|
1293 (setq ok nil))))
|
|
1294 (cons found value)))
|
|
1295
|
|
1296 (defun widget-repeat-insert-before (widget before)
|
|
1297 ;; Insert a new child in the list of children.
|
|
1298 (save-excursion
|
|
1299 (let ((children (widget-get widget :children))
|
|
1300 (inhibit-read-only t)
|
|
1301 after-change-functions)
|
|
1302 (cond (before
|
|
1303 (goto-char (widget-get before :from)))
|
|
1304 (t
|
|
1305 (goto-char (widget-get widget :value-pos))))
|
|
1306 (let ((child (widget-repeat-entry-create
|
|
1307 widget (widget-get (nth 0 (widget-get widget :args))
|
|
1308 :value))))
|
|
1309 (widget-specify-text (widget-get child :from)
|
|
1310 (widget-get child :to))
|
|
1311 (if (eq (car children) before)
|
|
1312 (widget-put widget :children (cons child children))
|
|
1313 (while (not (eq (car (cdr children)) before))
|
|
1314 (setq children (cdr children)))
|
|
1315 (setcdr children (cons child (cdr children)))))))
|
|
1316 (widget-setup)
|
|
1317 (widget-apply widget :notify widget))
|
|
1318
|
|
1319 (defun widget-repeat-delete-at (widget child)
|
|
1320 ;; Delete child from list of children.
|
|
1321 (save-excursion
|
|
1322 (let ((buttons (copy-list (widget-get widget :buttons)))
|
|
1323 button
|
|
1324 (inhibit-read-only t)
|
|
1325 after-change-functions)
|
|
1326 (while buttons
|
|
1327 (setq button (car buttons)
|
|
1328 buttons (cdr buttons))
|
|
1329 (when (eq (widget-get button :widget) child)
|
|
1330 (widget-put widget
|
|
1331 :buttons (delq button (widget-get widget :buttons)))
|
|
1332 (widget-delete button))))
|
|
1333 (widget-delete child)
|
|
1334 (widget-put widget :children (delq child (widget-get widget :children))))
|
|
1335 (widget-setup)
|
|
1336 (widget-apply widget :notify widget))
|
|
1337
|
|
1338 (defun widget-repeat-entry-create (widget value)
|
|
1339 ;; Create a new entry to the list.
|
|
1340 (let ((type (nth 0 (widget-get widget :args)))
|
|
1341 (indent (widget-get widget :indent))
|
|
1342 child delete insert)
|
|
1343 (widget-specify-insert
|
|
1344 (save-excursion
|
|
1345 (insert (widget-get widget :entry-format))
|
|
1346 (if indent
|
|
1347 (insert-char ?\ indent)))
|
|
1348 ;; Parse % escapes in format.
|
|
1349 (while (re-search-forward "%\\(.\\)" nil t)
|
|
1350 (let ((escape (aref (match-string 1) 0)))
|
|
1351 (replace-match "" t t)
|
|
1352 (cond ((eq escape ?%)
|
|
1353 (insert "%"))
|
|
1354 ((eq escape ?i)
|
|
1355 (setq insert (widget-create 'insert-button
|
|
1356 :parent widget)))
|
|
1357 ((eq escape ?d)
|
|
1358 (setq delete (widget-create 'delete-button
|
|
1359 :parent widget)))
|
|
1360 ((eq escape ?v)
|
|
1361 (setq child (widget-create type
|
|
1362 :parent widget
|
|
1363 :value value)))
|
|
1364 (t
|
|
1365 (error "Unknown escape `%c'" escape)))))
|
|
1366 (widget-put widget
|
|
1367 :buttons (cons delete
|
|
1368 (cons insert
|
|
1369 (widget-get widget :buttons))))
|
|
1370 (move-marker (widget-get child :from) (point-min))
|
|
1371 (move-marker (widget-get child :to) (point-max)))
|
|
1372 (widget-put insert :widget child)
|
|
1373 (widget-put delete :widget child)
|
|
1374 child))
|
|
1375
|
|
1376 ;;; The `group' Widget.
|
|
1377
|
|
1378 (define-widget 'group 'default
|
|
1379 "A widget which group other widgets inside."
|
|
1380 :convert-widget 'widget-choice-convert-widget
|
|
1381 :format "%v"
|
|
1382 :value-create 'widget-group-value-create
|
|
1383 :value-delete 'widget-radio-value-delete
|
|
1384 :value-get 'widget-repeat-value-get
|
|
1385 :validate 'widget-repeat-validate
|
|
1386 :match 'widget-group-match
|
|
1387 :match-inline 'widget-group-match-inline)
|
|
1388
|
|
1389 (defun widget-group-value-create (widget)
|
|
1390 ;; Create each component.
|
|
1391 (let ((args (widget-get widget :args))
|
|
1392 (value (widget-get widget :value))
|
|
1393 (indent (widget-get widget :indent))
|
|
1394 arg answer children)
|
|
1395 (while args
|
|
1396 (setq arg (car args)
|
|
1397 args (cdr args)
|
|
1398 answer (widget-match-inline arg value)
|
|
1399 value (cdr answer)
|
|
1400 children (cons (cond ((null answer)
|
|
1401 (widget-create arg :parent widget))
|
|
1402 ((widget-get arg :inline)
|
|
1403 (widget-create arg
|
|
1404 :parent widget
|
|
1405 :value (car answer)))
|
|
1406 (t
|
|
1407 (widget-create arg
|
|
1408 :parent widget
|
|
1409 :value (car (car answer)))))
|
|
1410 children))
|
|
1411 (and args indent (insert-char ?\ indent)))
|
|
1412 (widget-put widget :children (nreverse children))))
|
|
1413
|
|
1414 (defun widget-group-match (widget values)
|
|
1415 ;; Match if the components match.
|
|
1416 (and (listp values)
|
|
1417 (null (cdr (widget-group-match-inline widget values)))))
|
|
1418
|
|
1419 (defun widget-group-match-inline (widget values)
|
|
1420 ;; Match if the components match.
|
|
1421 (let ((args (widget-get widget :args))
|
|
1422 (match t)
|
|
1423 arg answer found)
|
|
1424 (while args
|
|
1425 (setq arg (car args)
|
|
1426 args (cdr args)
|
|
1427 answer (widget-match-inline arg values))
|
|
1428 (if answer
|
|
1429 (setq values (cdr answer)
|
|
1430 found (append found (car answer)))
|
|
1431 (setq values nil)))
|
|
1432 (if answer
|
|
1433 (cons found values)
|
|
1434 nil)))
|
|
1435
|
|
1436 ;;; The Sexp Widgets.
|
|
1437
|
|
1438 (define-widget 'const 'item
|
|
1439 nil
|
|
1440 :format "%t\n")
|
|
1441
|
|
1442 (define-widget 'string 'field
|
|
1443 nil)
|
|
1444
|
|
1445 (define-widget 'file 'string
|
|
1446 nil
|
|
1447 :format "%[%t%]:%v"
|
|
1448 :tag "File"
|
|
1449 :action 'widget-file-action)
|
|
1450
|
|
1451 (defun widget-file-action (widget &optional event)
|
|
1452 nil
|
|
1453 ;; Read a file name from the minibuffer.
|
|
1454 (widget-value-set widget
|
|
1455 (read-file-name (widget-apply widget :menu-tag-get)
|
|
1456 (widget-get widget :directory)
|
|
1457 (widget-value widget)
|
|
1458 (widget-get widget :must-match)
|
|
1459 (widget-get widget :initial))))
|
|
1460
|
|
1461 (define-widget 'directory 'file
|
|
1462 nil
|
|
1463 :tag "Directory")
|
|
1464
|
|
1465 (define-widget 'symbol 'string
|
|
1466 nil
|
|
1467 :match (lambda (widget value) (symbolp value))
|
|
1468 :value-to-internal (lambda (widget value) (symbol-name value))
|
|
1469 :value-to-external (lambda (widget value) (intern value)))
|
|
1470
|
|
1471 (define-widget 'sexp 'string
|
|
1472 nil
|
|
1473 :validate 'widget-sexp-validate
|
|
1474 :match (lambda (widget value) t)
|
|
1475 :value-to-internal (lambda (widget value) (pp-to-string value))
|
|
1476 :value-to-external (lambda (widget value) (read value)))
|
|
1477
|
|
1478 (defun widget-sexp-validate (widget)
|
|
1479 ;; Valid if we can read the string and there is no junk left after it.
|
|
1480 (save-excursion
|
|
1481 (set-buffer (get-buffer-create " *Widget Scratch*"))
|
|
1482 (erase-buffer)
|
|
1483 (insert (widget-apply :value-get widget))
|
|
1484 (goto-char (point-min))
|
|
1485 (condition-case data
|
|
1486 (let ((value (read (current-buffer))))
|
|
1487 (if (eobp)
|
|
1488 (if (widget-apply widget :match value)
|
|
1489 t
|
|
1490 (widget-put widget :error (widget-get widget :type-error))
|
|
1491 nil)
|
|
1492 (widget-put widget
|
|
1493 :error (format "Junk at end of expression: %s"
|
|
1494 (buffer-substring (point) (point-max))))
|
|
1495 nil))
|
|
1496 (error (widget-put widget :error (error-message-string data))
|
|
1497 nil))))
|
|
1498
|
|
1499 (define-widget 'integer 'sexp
|
|
1500 nil
|
|
1501 :type-error "This field should contain an integer"
|
|
1502 :match (lambda (widget value) (integerp value)))
|
|
1503
|
|
1504 (define-widget 'number 'sexp
|
|
1505 nil
|
|
1506 :type-error "This field should contain a number"
|
|
1507 :match (lambda (widget value) (numberp value)))
|
|
1508
|
|
1509 (define-widget 'list 'group
|
|
1510 nil)
|
|
1511
|
|
1512 (define-widget 'vector 'group
|
|
1513 nil
|
|
1514 :match 'widget-vector-match
|
|
1515 :value-to-internal (lambda (widget value) (append value nil))
|
|
1516 :value-to-external (lambda (widget value) (apply 'vector value)))
|
|
1517
|
|
1518 (defun widget-vector-match (widget value)
|
|
1519 (and (vectorp value)
|
|
1520 (widget-group-match widget
|
|
1521 (widget-apply :value-to-internal widget value))))
|
|
1522
|
|
1523 (define-widget 'cons 'group
|
|
1524 nil
|
|
1525 :match 'widget-cons-match
|
|
1526 :value-to-internal (lambda (widget value)
|
|
1527 (list (car value) (cdr value)))
|
|
1528 :value-to-external (lambda (widget value)
|
|
1529 (cons (nth 0 value) (nth 1 value))))
|
|
1530
|
|
1531 (defun widget-cons-match (widget value)
|
|
1532 (and (consp value)
|
|
1533 (widget-group-match widget
|
|
1534 (widget-apply :value-to-internal widget value))))
|
|
1535
|
|
1536 ;;; The End:
|
|
1537
|
|
1538 (provide 'widget-edit)
|
|
1539
|
|
1540 ;; widget-edit.el ends here
|