18
|
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: extensions
|
20
|
7 ;; Version: 1.30
|
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)
|
|
50 (defmacro defcustom (&rest args) nil)
|
|
51 (defmacro defface (&rest args) nil)
|
|
52 (when (fboundp 'copy-face)
|
|
53 (copy-face 'default 'widget-documentation-face)
|
|
54 (copy-face 'bold 'widget-button-face)
|
|
55 (copy-face 'italic 'widget-field-face))
|
|
56 (defvar widget-mouse-face 'highlight)
|
|
57 (defvar widget-menu-max-size 40)))
|
|
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
|
|
129 (defun widget-plist-member (plist prop)
|
|
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
|
|
177 (cons ""
|
|
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
|
20
|
349 (defsubst widget-name (widget)
|
|
350 "Return the name of WIDGET, asymbol."
|
|
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'."
|
|
362 (cond ((widget-plist-member (cdr widget) property)
|
|
363 (plist-get (cdr widget) property))
|
|
364 ((car widget)
|
|
365 (widget-get (get (car widget) 'widget-type) property))
|
|
366 (t nil)))
|
|
367
|
|
368 (defun widget-member (widget property)
|
|
369 "Non-nil iff there is a definition in WIDGET for PROPERTY."
|
|
370 (cond ((widget-plist-member (cdr widget) property)
|
|
371 t)
|
|
372 ((car widget)
|
|
373 (widget-member (get (car widget) 'widget-type) property))
|
|
374 (t nil)))
|
|
375
|
|
376 (defun widget-apply (widget property &rest args)
|
|
377 "Apply the value of WIDGET's PROPERTY to the widget itself.
|
|
378 ARGS are passed as extra argments to the function."
|
|
379 (apply (widget-get widget property) widget args))
|
|
380
|
|
381 (defun widget-value (widget)
|
|
382 "Extract the current value of WIDGET."
|
|
383 (widget-apply widget
|
|
384 :value-to-external (widget-apply widget :value-get)))
|
|
385
|
|
386 (defun widget-value-set (widget value)
|
|
387 "Set the current value of WIDGET to VALUE."
|
|
388 (widget-apply widget
|
|
389 :value-set (widget-apply widget
|
|
390 :value-to-internal value)))
|
|
391
|
|
392 (defun widget-match-inline (widget vals)
|
|
393 ;; In WIDGET, match the start of VALS.
|
|
394 (cond ((widget-get widget :inline)
|
|
395 (widget-apply widget :match-inline vals))
|
|
396 ((and vals
|
|
397 (widget-apply widget :match (car vals)))
|
|
398 (cons (list (car vals)) (cdr vals)))
|
|
399 (t nil)))
|
|
400
|
|
401 ;;; Creating Widgets.
|
|
402
|
|
403 ;;;###autoload
|
|
404 (defun widget-create (type &rest args)
|
|
405 "Create widget of TYPE.
|
|
406 The optional ARGS are additional keyword arguments."
|
|
407 (let ((widget (apply 'widget-convert type args)))
|
|
408 (widget-apply widget :create)
|
|
409 widget))
|
|
410
|
|
411 (defun widget-create-child-and-convert (parent type &rest args)
|
|
412 "As part of the widget PARENT, create a child widget TYPE.
|
|
413 The child is converted, using the keyword arguments ARGS."
|
|
414 (let ((widget (apply 'widget-convert type args)))
|
|
415 (widget-put widget :parent parent)
|
|
416 (unless (widget-get widget :indent)
|
|
417 (widget-put widget :indent (+ (or (widget-get parent :indent) 0)
|
|
418 (or (widget-get widget :extra-offset) 0)
|
|
419 (widget-get parent :offset))))
|
|
420 (widget-apply widget :create)
|
|
421 widget))
|
|
422
|
|
423 (defun widget-create-child (parent type)
|
|
424 "Create widget of TYPE."
|
|
425 (let ((widget (copy-list type)))
|
|
426 (widget-put widget :parent parent)
|
|
427 (unless (widget-get widget :indent)
|
|
428 (widget-put widget :indent (+ (or (widget-get parent :indent) 0)
|
|
429 (or (widget-get widget :extra-offset) 0)
|
|
430 (widget-get parent :offset))))
|
|
431 (widget-apply widget :create)
|
|
432 widget))
|
|
433
|
|
434 (defun widget-create-child-value (parent type value)
|
|
435 "Create widget of TYPE with value VALUE."
|
|
436 (let ((widget (copy-list type)))
|
|
437 (widget-put widget :value (widget-apply widget :value-to-internal value))
|
|
438 (widget-put widget :parent parent)
|
|
439 (unless (widget-get widget :indent)
|
|
440 (widget-put widget :indent (+ (or (widget-get parent :indent) 0)
|
|
441 (or (widget-get widget :extra-offset) 0)
|
|
442 (widget-get parent :offset))))
|
|
443 (widget-apply widget :create)
|
|
444 widget))
|
|
445
|
|
446 ;;;###autoload
|
|
447 (defun widget-delete (widget)
|
|
448 "Delete WIDGET."
|
|
449 (widget-apply widget :delete))
|
|
450
|
|
451 (defun widget-convert (type &rest args)
|
|
452 "Convert TYPE to a widget without inserting it in the buffer.
|
|
453 The optional ARGS are additional keyword arguments."
|
|
454 ;; Don't touch the type.
|
|
455 (let* ((widget (if (symbolp type)
|
|
456 (list type)
|
|
457 (copy-list type)))
|
|
458 (current widget)
|
|
459 (keys args))
|
|
460 ;; First set the :args keyword.
|
|
461 (while (cdr current) ;Look in the type.
|
|
462 (let ((next (car (cdr current))))
|
|
463 (if (and (symbolp next) (eq (aref (symbol-name next) 0) ?:))
|
|
464 (setq current (cdr (cdr current)))
|
|
465 (setcdr current (list :args (cdr current)))
|
|
466 (setq current nil))))
|
|
467 (while args ;Look in the args.
|
|
468 (let ((next (nth 0 args)))
|
|
469 (if (and (symbolp next) (eq (aref (symbol-name next) 0) ?:))
|
|
470 (setq args (nthcdr 2 args))
|
|
471 (widget-put widget :args args)
|
|
472 (setq args nil))))
|
|
473 ;; Then Convert the widget.
|
|
474 (setq type widget)
|
|
475 (while type
|
|
476 (let ((convert-widget (plist-get (cdr type) :convert-widget)))
|
|
477 (if convert-widget
|
|
478 (setq widget (funcall convert-widget widget))))
|
|
479 (setq type (get (car type) 'widget-type)))
|
|
480 ;; Finally set the keyword args.
|
|
481 (while keys
|
|
482 (let ((next (nth 0 keys)))
|
|
483 (if (and (symbolp next) (eq (aref (symbol-name next) 0) ?:))
|
|
484 (progn
|
|
485 (widget-put widget next (nth 1 keys))
|
|
486 (setq keys (nthcdr 2 keys)))
|
|
487 (setq keys nil))))
|
|
488 ;; Convert the :value to internal format.
|
|
489 (if (widget-member widget :value)
|
|
490 (let ((value (widget-get widget :value)))
|
|
491 (widget-put widget
|
|
492 :value (widget-apply widget :value-to-internal value))))
|
|
493 ;; Return the newly create widget.
|
|
494 widget))
|
|
495
|
|
496 (defun widget-insert (&rest args)
|
|
497 "Call `insert' with ARGS and make the text read only."
|
|
498 (let ((inhibit-read-only t)
|
|
499 after-change-functions
|
|
500 (from (point)))
|
|
501 (apply 'insert args)
|
|
502 (widget-specify-text from (point))))
|
|
503
|
|
504 ;;; Keymap and Comands.
|
|
505
|
|
506 (defvar widget-keymap nil
|
|
507 "Keymap containing useful binding for buffers containing widgets.
|
|
508 Recommended as a parent keymap for modes using widgets.")
|
|
509
|
|
510 (unless widget-keymap
|
|
511 (setq widget-keymap (make-sparse-keymap))
|
20
|
512 (define-key widget-keymap "\C-k" 'widget-kill-line)
|
18
|
513 (define-key widget-keymap "\t" 'widget-forward)
|
|
514 (define-key widget-keymap "\M-\t" 'widget-backward)
|
|
515 (define-key widget-keymap [(shift tab)] 'widget-backward)
|
|
516 (define-key widget-keymap [(shift tab)] 'widget-backward)
|
|
517 (define-key widget-keymap [backtab] 'widget-backward)
|
|
518 (if (string-match "XEmacs" (emacs-version))
|
|
519 (define-key widget-keymap [button2] 'widget-button-click)
|
|
520 (define-key widget-keymap [menu-bar] 'nil)
|
|
521 (define-key widget-keymap [mouse-2] 'widget-button-click))
|
|
522 (define-key widget-keymap "\C-m" 'widget-button-press))
|
|
523
|
|
524 (defvar widget-global-map global-map
|
|
525 "Keymap used for events the widget does not handle themselves.")
|
|
526 (make-variable-buffer-local 'widget-global-map)
|
|
527
|
|
528 (defvar widget-field-keymap nil
|
|
529 "Keymap used inside an editable field.")
|
|
530
|
|
531 (unless widget-field-keymap
|
|
532 (setq widget-field-keymap (copy-keymap widget-keymap))
|
|
533 (define-key widget-field-keymap "\C-m" 'widget-field-activate)
|
20
|
534 (define-key widget-field-keymap "\C-a" 'widget-beginning-of-line)
|
|
535 (define-key widget-field-keymap "\C-e" 'widget-end-of-line)
|
18
|
536 (set-keymap-parent widget-field-keymap global-map))
|
|
537
|
|
538 (defvar widget-text-keymap nil
|
|
539 "Keymap used inside a text field.")
|
|
540
|
|
541 (unless widget-text-keymap
|
|
542 (setq widget-text-keymap (copy-keymap widget-keymap))
|
20
|
543 (define-key widget-text-keymap "\C-a" 'widget-beginning-of-line)
|
|
544 (define-key widget-text-keymap "\C-e" 'widget-end-of-line)
|
18
|
545 (set-keymap-parent widget-text-keymap global-map))
|
|
546
|
|
547 (defun widget-field-activate (pos &optional event)
|
|
548 "Activate the ediable field at point."
|
|
549 (interactive "@d")
|
|
550 (let* ((field (get-text-property pos 'field)))
|
|
551 (if field
|
|
552 (widget-apply field :action event)
|
|
553 (call-interactively
|
|
554 (lookup-key widget-global-map (this-command-keys))))))
|
|
555
|
|
556 (defun widget-button-click (event)
|
|
557 "Activate button below mouse pointer."
|
|
558 (interactive "@e")
|
|
559 (widget-button-press (event-point event) event))
|
|
560
|
|
561 (defun widget-button-press (pos &optional event)
|
|
562 "Activate button at POS."
|
|
563 (interactive "@d")
|
|
564 (let* ((button (get-text-property pos 'button)))
|
|
565 (if button
|
|
566 (widget-apply button :action event)
|
|
567 (call-interactively
|
|
568 (lookup-key widget-global-map (this-command-keys))))))
|
|
569
|
|
570 (defun widget-move (arg)
|
|
571 "Move point to the ARG next field or button.
|
|
572 ARG may be negative to move backward."
|
|
573 (while (> arg 0)
|
|
574 (setq arg (1- arg))
|
|
575 (let ((next (cond ((get-text-property (point) 'button)
|
|
576 (next-single-property-change (point) 'button))
|
|
577 ((get-text-property (point) 'field)
|
|
578 (next-single-property-change (point) 'field))
|
|
579 (t
|
|
580 (point)))))
|
|
581 (if (null next) ; Widget extends to end. of buffer
|
|
582 (setq next (point-min)))
|
|
583 (let ((button (next-single-property-change next 'button))
|
|
584 (field (next-single-property-change next 'field)))
|
|
585 (cond ((or (get-text-property next 'button)
|
|
586 (get-text-property next 'field))
|
|
587 (goto-char next))
|
|
588 ((and button field)
|
|
589 (goto-char (min button field)))
|
|
590 (button (goto-char button))
|
|
591 (field (goto-char field))
|
|
592 (t
|
|
593 (let ((button (next-single-property-change (point-min) 'button))
|
|
594 (field (next-single-property-change (point-min) 'field)))
|
|
595 (cond ((and button field) (goto-char (min button field)))
|
|
596 (button (goto-char button))
|
|
597 (field (goto-char field))
|
|
598 (t
|
|
599 (error "No buttons or fields found")))))))))
|
|
600 (while (< arg 0)
|
|
601 (if (= (point-min) (point))
|
|
602 (forward-char 1))
|
|
603 (setq arg (1+ arg))
|
|
604 (let ((previous (cond ((get-text-property (1- (point)) 'button)
|
|
605 (previous-single-property-change (point) 'button))
|
|
606 ((get-text-property (1- (point)) 'field)
|
|
607 (previous-single-property-change (point) 'field))
|
|
608 (t
|
|
609 (point)))))
|
|
610 (if (null previous) ; Widget extends to beg. of buffer
|
|
611 (setq previous (point-max)))
|
|
612 (let ((button (previous-single-property-change previous 'button))
|
|
613 (field (previous-single-property-change previous 'field)))
|
|
614 (cond ((and button field)
|
|
615 (goto-char (max button field)))
|
|
616 (button (goto-char button))
|
|
617 (field (goto-char field))
|
|
618 (t
|
|
619 (let ((button (previous-single-property-change
|
|
620 (point-max) 'button))
|
|
621 (field (previous-single-property-change
|
|
622 (point-max) 'field)))
|
|
623 (cond ((and button field) (goto-char (max button field)))
|
|
624 (button (goto-char button))
|
|
625 (field (goto-char field))
|
|
626 (t
|
|
627 (error "No buttons or fields found"))))))))
|
|
628 (let ((button (previous-single-property-change (point) 'button))
|
|
629 (field (previous-single-property-change (point) 'field)))
|
|
630 (cond ((and button field)
|
|
631 (goto-char (max button field)))
|
|
632 (button (goto-char button))
|
|
633 (field (goto-char field)))))
|
|
634 (widget-echo-help (point))
|
|
635 (run-hooks 'widget-move-hook))
|
|
636
|
|
637 (defun widget-forward (arg)
|
|
638 "Move point to the next field or button.
|
|
639 With optional ARG, move across that many fields."
|
|
640 (interactive "p")
|
|
641 (run-hooks 'widget-forward-hook)
|
|
642 (widget-move arg))
|
|
643
|
|
644 (defun widget-backward (arg)
|
|
645 "Move point to the previous field or button.
|
|
646 With optional ARG, move across that many fields."
|
|
647 (interactive "p")
|
|
648 (run-hooks 'widget-backward-hook)
|
|
649 (widget-move (- arg)))
|
|
650
|
20
|
651 (defun widget-beginning-of-line ()
|
|
652 "Go to beginning of field or beginning of line, whichever is first."
|
|
653 (interactive)
|
|
654 (let ((bol (save-excursion (beginning-of-line) (point)))
|
|
655 (prev (previous-single-property-change (point) 'field)))
|
|
656 (goto-char (max bol (or prev bol)))))
|
|
657
|
|
658 (defun widget-end-of-line ()
|
|
659 "Go to end of field or end of line, whichever is first."
|
|
660 (interactive)
|
|
661 (let ((bol (save-excursion (end-of-line) (point)))
|
|
662 (prev (next-single-property-change (point) 'field)))
|
|
663 (goto-char (min bol (or prev bol)))))
|
|
664
|
|
665 (defun widget-kill-line ()
|
|
666 "Kill to end of field or end of line, whichever is first."
|
|
667 (interactive)
|
|
668 (let ((field (get-text-property (point) 'field))
|
|
669 (newline (save-excursion (search-forward "\n")))
|
|
670 (next (next-single-property-change (point) 'field)))
|
|
671 (if (and field (> newline next))
|
|
672 (kill-region (point) next)
|
|
673 (call-interactively 'kill-line))))
|
|
674
|
|
675 (defun widget-identify (pos)
|
|
676 "Identify the widget under point."
|
|
677 (interactive "d")
|
|
678 (let* ((field (get-text-property pos 'field))
|
|
679 (button (get-text-property pos 'button))
|
|
680 (doc (get-text-property pos 'widget-doc))
|
|
681 (widget (or field button doc)))
|
|
682 (with-output-to-temp-buffer "*Widget Identity*"
|
|
683 (princ (cond (field "This is an editable text area.\n")
|
|
684 (button "This is an active area.\n")
|
|
685 (doc "This is documentation text.\n")
|
|
686 (t "This is unidentified text.\n")))
|
|
687 (while widget
|
|
688 (princ "It is part of a `")
|
|
689 (princ (car widget))
|
|
690 (princ "' widget (value: ")
|
|
691 (prin1 (condition-case nil
|
|
692 (widget-value widget)
|
|
693 (error 'error)))
|
|
694 (princ ").\n")
|
|
695 (when (eq (car widget) 'radio-button)
|
|
696 (let ((sibling (widget-get-sibling widget)))
|
|
697 (if (not sibling)
|
|
698 (princ "It doesn't seem to control anything.\n")
|
|
699 (princ "The value of its sibling is: ")
|
|
700 (prin1 (condition-case nil
|
|
701 (widget-value sibling)
|
|
702 (error 'error)))
|
|
703 (princ ".\n"))))
|
|
704 (setq widget (widget-get widget :parent))))))
|
|
705
|
18
|
706 ;;; Setting up the buffer.
|
|
707
|
|
708 (defvar widget-field-new nil)
|
|
709 ;; List of all newly created editable fields in the buffer.
|
|
710 (make-variable-buffer-local 'widget-field-new)
|
|
711
|
|
712 (defvar widget-field-list nil)
|
|
713 ;; List of all editable fields in the buffer.
|
|
714 (make-variable-buffer-local 'widget-field-list)
|
|
715
|
|
716 (defun widget-setup ()
|
|
717 "Setup current buffer so editing string widgets works."
|
|
718 (let ((inhibit-read-only t)
|
|
719 (after-change-functions nil)
|
|
720 field)
|
|
721 (while widget-field-new
|
|
722 (setq field (car widget-field-new)
|
|
723 widget-field-new (cdr widget-field-new)
|
|
724 widget-field-list (cons field widget-field-list))
|
|
725 (let ((from (widget-get field :value-from))
|
|
726 (to (widget-get field :value-to)))
|
|
727 (widget-specify-field field from to)
|
|
728 (move-marker from (1- from))
|
|
729 (move-marker to (1+ to)))))
|
|
730 (widget-clear-undo)
|
|
731 ;; We need to maintain text properties and size of the editing fields.
|
|
732 (make-local-variable 'after-change-functions)
|
|
733 (if widget-field-list
|
|
734 (setq after-change-functions '(widget-after-change))
|
|
735 (setq after-change-functions nil)))
|
|
736
|
|
737 (defvar widget-field-last nil)
|
|
738 ;; Last field containing point.
|
|
739 (make-variable-buffer-local 'widget-field-last)
|
|
740
|
|
741 (defvar widget-field-was nil)
|
|
742 ;; The widget data before the change.
|
|
743 (make-variable-buffer-local 'widget-field-was)
|
|
744
|
|
745 (defun widget-field-find (pos)
|
|
746 ;; Find widget whose editing field is located at POS.
|
|
747 ;; Return nil if POS is not inside and editing field.
|
|
748 ;;
|
|
749 ;; This is only used in `widget-field-modified', since ordinarily
|
|
750 ;; you would just test the field property.
|
|
751 (let ((fields widget-field-list)
|
|
752 field found)
|
|
753 (while fields
|
|
754 (setq field (car fields)
|
|
755 fields (cdr fields))
|
|
756 (let ((from (widget-get field :value-from))
|
|
757 (to (widget-get field :value-to)))
|
|
758 (if (and from to (< from pos) (> to pos))
|
|
759 (setq fields nil
|
|
760 found field))))
|
|
761 found))
|
|
762
|
|
763 (defun widget-after-change (from to old)
|
|
764 ;; Adjust field size and text properties.
|
|
765 (condition-case nil
|
|
766 (let ((field (widget-field-find from))
|
|
767 (inhibit-read-only t))
|
|
768 (cond ((null field))
|
|
769 ((not (eq field (widget-field-find to)))
|
|
770 (debug)
|
|
771 (message "Error: `widget-after-change' called on two fields"))
|
|
772 (t
|
|
773 (let ((size (widget-get field :size)))
|
|
774 (if size
|
|
775 (let ((begin (1+ (widget-get field :value-from)))
|
|
776 (end (1- (widget-get field :value-to))))
|
|
777 (widget-specify-field-update field begin end)
|
|
778 (cond ((< (- end begin) size)
|
|
779 ;; Field too small.
|
|
780 (save-excursion
|
|
781 (goto-char end)
|
|
782 (insert-char ?\ (- (+ begin size) end))
|
|
783 (widget-specify-field-update field
|
|
784 begin
|
|
785 (+ begin size))))
|
|
786 ((> (- end begin) size)
|
|
787 ;; Field too large and
|
|
788 (if (or (< (point) (+ begin size))
|
|
789 (> (point) end))
|
|
790 ;; Point is outside extra space.
|
|
791 (setq begin (+ begin size))
|
|
792 ;; Point is within the extra space.
|
|
793 (setq begin (point)))
|
|
794 (save-excursion
|
|
795 (goto-char end)
|
|
796 (while (and (eq (preceding-char) ?\ )
|
|
797 (> (point) begin))
|
|
798 (delete-backward-char 1))))))
|
|
799 (widget-specify-field-update field from to)))
|
|
800 (widget-apply field :notify field))))
|
|
801 (error (debug))))
|
|
802
|
|
803 ;;; Widget Functions
|
|
804 ;;
|
|
805 ;; These functions are used in the definition of multiple widgets.
|
|
806
|
|
807 (defun widget-children-value-delete (widget)
|
|
808 "Delete all :children and :buttons in WIDGET."
|
|
809 (mapcar 'widget-delete (widget-get widget :children))
|
|
810 (widget-put widget :children nil)
|
|
811 (mapcar 'widget-delete (widget-get widget :buttons))
|
|
812 (widget-put widget :buttons nil))
|
|
813
|
|
814 (defun widget-types-convert-widget (widget)
|
|
815 "Convert :args as widget types in WIDGET."
|
|
816 (widget-put widget :args (mapcar 'widget-convert (widget-get widget :args)))
|
|
817 widget)
|
|
818
|
|
819 ;;; The `default' Widget.
|
|
820
|
|
821 (define-widget 'default nil
|
|
822 "Basic widget other widgets are derived from."
|
|
823 :value-to-internal (lambda (widget value) value)
|
|
824 :value-to-external (lambda (widget value) value)
|
|
825 :create 'widget-default-create
|
|
826 :indent nil
|
|
827 :offset 0
|
|
828 :format-handler 'widget-default-format-handler
|
|
829 :button-face-get 'widget-default-button-face-get
|
|
830 :sample-face-get 'widget-default-sample-face-get
|
|
831 :delete 'widget-default-delete
|
|
832 :value-set 'widget-default-value-set
|
|
833 :value-inline 'widget-default-value-inline
|
|
834 :menu-tag-get 'widget-default-menu-tag-get
|
|
835 :validate (lambda (widget) nil)
|
|
836 :action 'widget-default-action
|
|
837 :notify 'widget-default-notify)
|
|
838
|
|
839 (defun widget-default-create (widget)
|
|
840 "Create WIDGET at point in the current buffer."
|
|
841 (widget-specify-insert
|
|
842 (let ((from (point))
|
|
843 (tag (widget-get widget :tag))
|
|
844 (doc (widget-get widget :doc))
|
|
845 button-begin button-end
|
|
846 sample-begin sample-end
|
|
847 doc-begin doc-end
|
|
848 value-pos)
|
|
849 (insert (widget-get widget :format))
|
|
850 (goto-char from)
|
|
851 ;; Parse % escapes in format.
|
|
852 (while (re-search-forward "%\\(.\\)" nil t)
|
|
853 (let ((escape (aref (match-string 1) 0)))
|
|
854 (replace-match "" t t)
|
|
855 (cond ((eq escape ?%)
|
|
856 (insert "%"))
|
|
857 ((eq escape ?\[)
|
|
858 (setq button-begin (point)))
|
|
859 ((eq escape ?\])
|
|
860 (setq button-end (point)))
|
|
861 ((eq escape ?\{)
|
|
862 (setq sample-begin (point)))
|
|
863 ((eq escape ?\})
|
|
864 (setq sample-end (point)))
|
|
865 ((eq escape ?n)
|
|
866 (when (widget-get widget :indent)
|
|
867 (insert "\n")
|
|
868 (insert-char ? (widget-get widget :indent))))
|
|
869 ((eq escape ?t)
|
|
870 (if tag
|
|
871 (insert tag)
|
|
872 (let ((standard-output (current-buffer)))
|
|
873 (princ (widget-get widget :value)))))
|
|
874 ((eq escape ?d)
|
|
875 (when doc
|
|
876 (setq doc-begin (point))
|
|
877 (insert doc)
|
|
878 (while (eq (preceding-char) ?\n)
|
|
879 (delete-backward-char 1))
|
|
880 (insert "\n")
|
|
881 (setq doc-end (point))))
|
|
882 ((eq escape ?v)
|
|
883 (if (and button-begin (not button-end))
|
|
884 (widget-apply widget :value-create)
|
|
885 (setq value-pos (point))))
|
|
886 (t
|
|
887 (widget-apply widget :format-handler escape)))))
|
|
888 ;; Specify button, sample, and doc, and insert value.
|
|
889 (and button-begin button-end
|
|
890 (widget-specify-button widget button-begin button-end))
|
|
891 (and sample-begin sample-end
|
|
892 (widget-specify-sample widget sample-begin sample-end))
|
|
893 (and doc-begin doc-end
|
|
894 (widget-specify-doc widget doc-begin doc-end))
|
|
895 (when value-pos
|
|
896 (goto-char value-pos)
|
|
897 (widget-apply widget :value-create)))
|
|
898 (let ((from (copy-marker (point-min)))
|
|
899 (to (copy-marker (point-max))))
|
|
900 (widget-specify-text from to)
|
|
901 (set-marker-insertion-type from t)
|
|
902 (set-marker-insertion-type to nil)
|
|
903 (widget-put widget :from from)
|
|
904 (widget-put widget :to to))))
|
|
905
|
|
906 (defun widget-default-format-handler (widget escape)
|
|
907 ;; We recognize the %h escape by default.
|
|
908 (let* ((buttons (widget-get widget :buttons))
|
|
909 (doc-property (widget-get widget :documentation-property))
|
|
910 (doc-try (cond ((widget-get widget :doc))
|
|
911 ((symbolp doc-property)
|
|
912 (documentation-property (widget-get widget :value)
|
|
913 doc-property))
|
|
914 (t
|
|
915 (funcall doc-property (widget-get widget :value)))))
|
|
916 (doc-text (and (stringp doc-try)
|
|
917 (> (length doc-try) 1)
|
|
918 doc-try)))
|
|
919 (cond ((eq escape ?h)
|
|
920 (when doc-text
|
|
921 (and (eq (preceding-char) ?\n)
|
|
922 (widget-get widget :indent)
|
|
923 (insert-char ? (widget-get widget :indent)))
|
|
924 ;; The `*' in the beginning is redundant.
|
|
925 (when (eq (aref doc-text 0) ?*)
|
|
926 (setq doc-text (substring doc-text 1)))
|
|
927 ;; Get rid of trailing newlines.
|
|
928 (when (string-match "\n+\\'" doc-text)
|
|
929 (setq doc-text (substring doc-text 0 (match-beginning 0))))
|
|
930 (push (if (string-match "\n." doc-text)
|
|
931 ;; Allow multiline doc to be hiden.
|
|
932 (widget-create-child-and-convert
|
|
933 widget 'widget-help
|
|
934 :doc (progn
|
|
935 (string-match "\\`.*" doc-text)
|
|
936 (match-string 0 doc-text))
|
|
937 :widget-doc doc-text
|
|
938 "?")
|
|
939 ;; A single line is just inserted.
|
|
940 (widget-create-child-and-convert
|
|
941 widget 'item :format "%d" :doc doc-text nil))
|
|
942 buttons)))
|
|
943 (t
|
|
944 (error "Unknown escape `%c'" escape)))
|
|
945 (widget-put widget :buttons buttons)))
|
|
946
|
|
947 (defun widget-default-button-face-get (widget)
|
|
948 ;; Use :button-face or widget-button-face
|
|
949 (or (widget-get widget :button-face) 'widget-button-face))
|
|
950
|
|
951 (defun widget-default-sample-face-get (widget)
|
|
952 ;; Use :sample-face.
|
|
953 (widget-get widget :sample-face))
|
|
954
|
|
955 (defun widget-default-delete (widget)
|
|
956 ;; Remove widget from the buffer.
|
|
957 (let ((from (widget-get widget :from))
|
|
958 (to (widget-get widget :to))
|
|
959 (inhibit-read-only t)
|
|
960 after-change-functions)
|
|
961 (widget-apply widget :value-delete)
|
|
962 (delete-region from to)
|
|
963 (set-marker from nil)
|
|
964 (set-marker to nil)))
|
|
965
|
|
966 (defun widget-default-value-set (widget value)
|
|
967 ;; Recreate widget with new value.
|
|
968 (save-excursion
|
|
969 (goto-char (widget-get widget :from))
|
|
970 (widget-apply widget :delete)
|
|
971 (widget-put widget :value value)
|
|
972 (widget-apply widget :create)))
|
|
973
|
|
974 (defun widget-default-value-inline (widget)
|
|
975 ;; Wrap value in a list unless it is inline.
|
|
976 (if (widget-get widget :inline)
|
|
977 (widget-value widget)
|
|
978 (list (widget-value widget))))
|
|
979
|
|
980 (defun widget-default-menu-tag-get (widget)
|
|
981 ;; Use tag or value for menus.
|
|
982 (or (widget-get widget :menu-tag)
|
|
983 (widget-get widget :tag)
|
|
984 (widget-princ-to-string (widget-get widget :value))))
|
|
985
|
|
986 (defun widget-default-action (widget &optional event)
|
|
987 ;; Notify the parent when a widget change
|
|
988 (let ((parent (widget-get widget :parent)))
|
|
989 (when parent
|
|
990 (widget-apply parent :notify widget event))))
|
|
991
|
|
992 (defun widget-default-notify (widget child &optional event)
|
|
993 ;; Pass notification to parent.
|
|
994 (widget-default-action widget event))
|
|
995
|
|
996 ;;; The `item' Widget.
|
|
997
|
|
998 (define-widget 'item 'default
|
|
999 "Constant items for inclusion in other widgets."
|
|
1000 :convert-widget 'widget-item-convert-widget
|
|
1001 :value-create 'widget-item-value-create
|
|
1002 :value-delete 'ignore
|
|
1003 :value-get 'widget-item-value-get
|
|
1004 :match 'widget-item-match
|
|
1005 :match-inline 'widget-item-match-inline
|
|
1006 :action 'widget-item-action
|
|
1007 :format "%t\n")
|
|
1008
|
|
1009 (defun widget-item-convert-widget (widget)
|
|
1010 ;; Initialize :value from :args in WIDGET.
|
|
1011 (let ((args (widget-get widget :args)))
|
|
1012 (when args
|
|
1013 (widget-put widget :value (widget-apply widget
|
|
1014 :value-to-internal (car args)))
|
|
1015 (widget-put widget :args nil)))
|
|
1016 widget)
|
|
1017
|
|
1018 (defun widget-item-value-create (widget)
|
|
1019 ;; Insert the printed representation of the value.
|
|
1020 (let ((standard-output (current-buffer)))
|
|
1021 (princ (widget-get widget :value))))
|
|
1022
|
|
1023 (defun widget-item-match (widget value)
|
|
1024 ;; Match if the value is the same.
|
|
1025 (equal (widget-get widget :value) value))
|
|
1026
|
|
1027 (defun widget-item-match-inline (widget values)
|
|
1028 ;; Match if the value is the same.
|
|
1029 (let ((value (widget-get widget :value)))
|
|
1030 (and (listp value)
|
|
1031 (<= (length value) (length values))
|
|
1032 (let ((head (subseq values 0 (length value))))
|
|
1033 (and (equal head value)
|
|
1034 (cons head (subseq values (length value))))))))
|
|
1035
|
|
1036 (defun widget-item-action (widget &optional event)
|
|
1037 ;; Just notify itself.
|
|
1038 (widget-apply widget :notify widget event))
|
|
1039
|
|
1040 (defun widget-item-value-get (widget)
|
|
1041 ;; Items are simple.
|
|
1042 (widget-get widget :value))
|
|
1043
|
|
1044 ;;; The `push-button' Widget.
|
|
1045
|
|
1046 (define-widget 'push-button 'item
|
|
1047 "A pushable button."
|
|
1048 :format "%[[%t]%]")
|
|
1049
|
|
1050 ;;; The `link' Widget.
|
|
1051
|
|
1052 (define-widget 'link 'item
|
|
1053 "An embedded link."
|
|
1054 :help-echo "Push me to follow the link."
|
|
1055 :format "%[_%t_%]")
|
|
1056
|
|
1057 ;;; The `info-link' Widget.
|
|
1058
|
|
1059 (define-widget 'info-link 'link
|
|
1060 "A link to an info file."
|
|
1061 :action 'widget-info-link-action)
|
|
1062
|
|
1063 (defun widget-info-link-action (widget &optional event)
|
|
1064 "Open the info node specified by WIDGET."
|
|
1065 (Info-goto-node (widget-value widget)))
|
|
1066
|
|
1067 ;;; The `url-link' Widget.
|
|
1068
|
|
1069 (define-widget 'url-link 'link
|
|
1070 "A link to an www page."
|
|
1071 :action 'widget-url-link-action)
|
|
1072
|
|
1073 (defun widget-url-link-action (widget &optional event)
|
|
1074 "Open the url specified by WIDGET."
|
|
1075 (require 'browse-url)
|
|
1076 (funcall browse-url-browser-function (widget-value widget)))
|
|
1077
|
|
1078 ;;; The `editable-field' Widget.
|
|
1079
|
|
1080 (define-widget 'editable-field 'default
|
|
1081 "An editable text field."
|
|
1082 :convert-widget 'widget-item-convert-widget
|
|
1083 :keymap widget-field-keymap
|
|
1084 :format "%v"
|
|
1085 :value ""
|
|
1086 :action 'widget-field-action
|
|
1087 :validate 'widget-field-validate
|
|
1088 :valid-regexp ""
|
|
1089 :error "No match"
|
|
1090 :value-create 'widget-field-value-create
|
|
1091 :value-delete 'widget-field-value-delete
|
|
1092 :value-get 'widget-field-value-get
|
|
1093 :match 'widget-field-match)
|
|
1094
|
|
1095 ;; History of field minibuffer edits.
|
|
1096 (defvar widget-field-history nil)
|
|
1097
|
|
1098 (defun widget-field-action (widget &optional event)
|
|
1099 ;; Edit the value in the minibuffer.
|
|
1100 (let ((tag (widget-apply widget :menu-tag-get))
|
|
1101 (invalid (widget-apply widget :validate)))
|
|
1102 (when invalid
|
|
1103 (error (widget-get invalid :error)))
|
|
1104 (widget-value-set widget
|
|
1105 (widget-apply widget
|
|
1106 :value-to-external
|
|
1107 (read-string (concat tag ": ")
|
|
1108 (widget-apply
|
|
1109 widget
|
|
1110 :value-to-internal
|
|
1111 (widget-value widget))
|
|
1112 'widget-field-history)))
|
|
1113 (widget-apply widget :notify widget event)
|
|
1114 (widget-setup)))
|
|
1115
|
|
1116 (defun widget-field-validate (widget)
|
|
1117 ;; Valid if the content matches `:valid-regexp'.
|
|
1118 (save-excursion
|
|
1119 (let ((value (widget-apply widget :value-get))
|
|
1120 (regexp (widget-get widget :valid-regexp)))
|
|
1121 (if (string-match regexp value)
|
|
1122 nil
|
|
1123 widget))))
|
|
1124
|
|
1125 (defun widget-field-value-create (widget)
|
|
1126 ;; Create an editable text field.
|
|
1127 (insert " ")
|
|
1128 (let ((size (widget-get widget :size))
|
|
1129 (value (widget-get widget :value))
|
|
1130 (from (point)))
|
|
1131 (insert value)
|
|
1132 (and size
|
|
1133 (< (length value) size)
|
|
1134 (insert-char ?\ (- size (length value))))
|
|
1135 (unless (memq widget widget-field-list)
|
|
1136 (setq widget-field-new (cons widget widget-field-new)))
|
|
1137 (widget-put widget :value-to (copy-marker (point)))
|
|
1138 (set-marker-insertion-type (widget-get widget :value-to) nil)
|
|
1139 (if (null size)
|
|
1140 (insert ?\n)
|
|
1141 (insert ?\ ))
|
|
1142 (widget-put widget :value-from (copy-marker from))
|
|
1143 (set-marker-insertion-type (widget-get widget :value-from) t)))
|
|
1144
|
|
1145 (defun widget-field-value-delete (widget)
|
|
1146 ;; Remove the widget from the list of active editing fields.
|
|
1147 (setq widget-field-list (delq widget widget-field-list))
|
|
1148 ;; These are nil if the :format string doesn't contain `%v'.
|
|
1149 (when (widget-get widget :value-from)
|
|
1150 (set-marker (widget-get widget :value-from) nil))
|
|
1151 (when (widget-get widget :value-from)
|
|
1152 (set-marker (widget-get widget :value-to) nil)))
|
|
1153
|
|
1154 (defun widget-field-value-get (widget)
|
|
1155 ;; Return current text in editing field.
|
|
1156 (let ((from (widget-get widget :value-from))
|
|
1157 (to (widget-get widget :value-to))
|
|
1158 (size (widget-get widget :size))
|
|
1159 (secret (widget-get widget :secret))
|
|
1160 (old (current-buffer)))
|
|
1161 (if (and from to)
|
|
1162 (progn
|
|
1163 (set-buffer (marker-buffer from))
|
|
1164 (setq from (1+ from)
|
|
1165 to (1- to))
|
|
1166 (while (and size
|
|
1167 (not (zerop size))
|
|
1168 (> to from)
|
|
1169 (eq (char-after (1- to)) ?\ ))
|
|
1170 (setq to (1- to)))
|
|
1171 (let ((result (buffer-substring-no-properties from to)))
|
|
1172 (when secret
|
|
1173 (let ((index 0))
|
|
1174 (while (< (+ from index) to)
|
|
1175 (aset result index
|
|
1176 (get-text-property (+ from index) 'secret))
|
|
1177 (setq index (1+ index)))))
|
|
1178 (set-buffer old)
|
|
1179 result))
|
|
1180 (widget-get widget :value))))
|
|
1181
|
|
1182 (defun widget-field-match (widget value)
|
|
1183 ;; Match any string.
|
|
1184 (stringp value))
|
|
1185
|
|
1186 ;;; The `text' Widget.
|
|
1187
|
|
1188 (define-widget 'text 'editable-field
|
|
1189 :keymap widget-text-keymap
|
|
1190 "A multiline text area.")
|
|
1191
|
|
1192 ;;; The `menu-choice' Widget.
|
|
1193
|
|
1194 (define-widget 'menu-choice 'default
|
|
1195 "A menu of options."
|
|
1196 :convert-widget 'widget-types-convert-widget
|
|
1197 :format "%[%t%]: %v"
|
|
1198 :case-fold t
|
|
1199 :tag "choice"
|
|
1200 :void '(item :format "invalid (%t)\n")
|
|
1201 :value-create 'widget-choice-value-create
|
|
1202 :value-delete 'widget-children-value-delete
|
|
1203 :value-get 'widget-choice-value-get
|
|
1204 :value-inline 'widget-choice-value-inline
|
|
1205 :action 'widget-choice-action
|
|
1206 :error "Make a choice"
|
|
1207 :validate 'widget-choice-validate
|
|
1208 :match 'widget-choice-match
|
|
1209 :match-inline 'widget-choice-match-inline)
|
|
1210
|
|
1211 (defun widget-choice-value-create (widget)
|
|
1212 ;; Insert the first choice that matches the value.
|
|
1213 (let ((value (widget-get widget :value))
|
|
1214 (args (widget-get widget :args))
|
|
1215 current)
|
|
1216 (while args
|
|
1217 (setq current (car args)
|
|
1218 args (cdr args))
|
|
1219 (when (widget-apply current :match value)
|
|
1220 (widget-put widget :children (list (widget-create-child-value
|
|
1221 widget current value)))
|
|
1222 (widget-put widget :choice current)
|
|
1223 (setq args nil
|
|
1224 current nil)))
|
|
1225 (when current
|
|
1226 (let ((void (widget-get widget :void)))
|
|
1227 (widget-put widget :children (list (widget-create-child-and-convert
|
|
1228 widget void :value value)))
|
|
1229 (widget-put widget :choice void)))))
|
|
1230
|
|
1231 (defun widget-choice-value-get (widget)
|
|
1232 ;; Get value of the child widget.
|
|
1233 (widget-value (car (widget-get widget :children))))
|
|
1234
|
|
1235 (defun widget-choice-value-inline (widget)
|
|
1236 ;; Get value of the child widget.
|
|
1237 (widget-apply (car (widget-get widget :children)) :value-inline))
|
|
1238
|
|
1239 (defun widget-choice-action (widget &optional event)
|
|
1240 ;; Make a choice.
|
|
1241 (let ((args (widget-get widget :args))
|
|
1242 (old (widget-get widget :choice))
|
|
1243 (tag (widget-apply widget :menu-tag-get))
|
|
1244 (completion-ignore-case (widget-get widget :case-fold))
|
|
1245 current choices)
|
|
1246 ;; Remember old value.
|
|
1247 (if (and old (not (widget-apply widget :validate)))
|
|
1248 (let* ((external (widget-value widget))
|
|
1249 (internal (widget-apply old :value-to-internal external)))
|
|
1250 (widget-put old :value internal)))
|
|
1251 ;; Find new choice.
|
|
1252 (setq current
|
|
1253 (cond ((= (length args) 0)
|
|
1254 nil)
|
|
1255 ((= (length args) 1)
|
|
1256 (nth 0 args))
|
|
1257 ((and (= (length args) 2)
|
|
1258 (memq old args))
|
|
1259 (if (eq old (nth 0 args))
|
|
1260 (nth 1 args)
|
|
1261 (nth 0 args)))
|
|
1262 (t
|
|
1263 (while args
|
|
1264 (setq current (car args)
|
|
1265 args (cdr args))
|
|
1266 (setq choices
|
|
1267 (cons (cons (widget-apply current :menu-tag-get)
|
|
1268 current)
|
|
1269 choices)))
|
|
1270 (widget-choose tag (reverse choices) event))))
|
|
1271 (when current
|
|
1272 (widget-value-set widget
|
|
1273 (widget-apply current :value-to-external
|
|
1274 (widget-get current :value)))
|
|
1275 (widget-apply widget :notify widget event)
|
|
1276 (widget-setup)))
|
|
1277 ;; Notify parent.
|
|
1278 (widget-apply widget :notify widget event)
|
|
1279 (widget-clear-undo))
|
|
1280
|
|
1281 (defun widget-choice-validate (widget)
|
|
1282 ;; Valid if we have made a valid choice.
|
|
1283 (let ((void (widget-get widget :void))
|
|
1284 (choice (widget-get widget :choice))
|
|
1285 (child (car (widget-get widget :children))))
|
|
1286 (if (eq void choice)
|
|
1287 widget
|
|
1288 (widget-apply child :validate))))
|
|
1289
|
|
1290 (defun widget-choice-match (widget value)
|
|
1291 ;; Matches if one of the choices matches.
|
|
1292 (let ((args (widget-get widget :args))
|
|
1293 current found)
|
|
1294 (while (and args (not found))
|
|
1295 (setq current (car args)
|
|
1296 args (cdr args)
|
|
1297 found (widget-apply current :match value)))
|
|
1298 found))
|
|
1299
|
|
1300 (defun widget-choice-match-inline (widget values)
|
|
1301 ;; Matches if one of the choices matches.
|
|
1302 (let ((args (widget-get widget :args))
|
|
1303 current found)
|
|
1304 (while (and args (null found))
|
|
1305 (setq current (car args)
|
|
1306 args (cdr args)
|
|
1307 found (widget-match-inline current values)))
|
|
1308 found))
|
|
1309
|
|
1310 ;;; The `toggle' Widget.
|
|
1311
|
20
|
1312 (define-widget 'toggle 'item
|
18
|
1313 "Toggle between two states."
|
20
|
1314 :format "%[%v%]\n"
|
|
1315 :value-create 'widget-toggle-value-create
|
|
1316 :action 'widget-toggle-action
|
|
1317 :match (lambda (widget value) t)
|
18
|
1318 :on "on"
|
|
1319 :off "off")
|
|
1320
|
20
|
1321 (defun widget-toggle-value-create (widget)
|
|
1322 ;; Insert text representing the `on' and `off' states.
|
|
1323 (if (widget-value widget)
|
|
1324 (insert (widget-get widget :on))
|
|
1325 (insert (widget-get widget :off))))
|
18
|
1326
|
20
|
1327 (defun widget-toggle-action (widget &optional event)
|
|
1328 ;; Toggle value.
|
|
1329 (widget-value-set widget (not (widget-value widget)))
|
|
1330 (widget-apply widget :notify widget event))
|
|
1331
|
18
|
1332 ;;; The `checkbox' Widget.
|
|
1333
|
|
1334 (define-widget 'checkbox 'toggle
|
|
1335 "A checkbox toggle."
|
20
|
1336 :format "%[%v%]"
|
|
1337 :on "[X]"
|
|
1338 :off "[ ]")
|
18
|
1339
|
|
1340 ;;; The `checklist' Widget.
|
|
1341
|
|
1342 (define-widget 'checklist 'default
|
|
1343 "A multiple choice widget."
|
|
1344 :convert-widget 'widget-types-convert-widget
|
|
1345 :format "%v"
|
|
1346 :offset 4
|
|
1347 :entry-format "%b %v"
|
|
1348 :menu-tag "checklist"
|
|
1349 :greedy nil
|
|
1350 :value-create 'widget-checklist-value-create
|
|
1351 :value-delete 'widget-children-value-delete
|
|
1352 :value-get 'widget-checklist-value-get
|
|
1353 :validate 'widget-checklist-validate
|
|
1354 :match 'widget-checklist-match
|
|
1355 :match-inline 'widget-checklist-match-inline)
|
|
1356
|
|
1357 (defun widget-checklist-value-create (widget)
|
|
1358 ;; Insert all values
|
|
1359 (let ((alist (widget-checklist-match-find widget (widget-get widget :value)))
|
|
1360 (args (widget-get widget :args)))
|
|
1361 (while args
|
|
1362 (widget-checklist-add-item widget (car args) (assq (car args) alist))
|
|
1363 (setq args (cdr args)))
|
|
1364 (widget-put widget :children (nreverse (widget-get widget :children)))))
|
|
1365
|
|
1366 (defun widget-checklist-add-item (widget type chosen)
|
|
1367 ;; Create checklist item in WIDGET of type TYPE.
|
|
1368 ;; If the item is checked, CHOSEN is a cons whose cdr is the value.
|
|
1369 (and (eq (preceding-char) ?\n)
|
|
1370 (widget-get widget :indent)
|
|
1371 (insert-char ? (widget-get widget :indent)))
|
|
1372 (widget-specify-insert
|
|
1373 (let* ((children (widget-get widget :children))
|
|
1374 (buttons (widget-get widget :buttons))
|
|
1375 (from (point))
|
|
1376 child button)
|
|
1377 (insert (widget-get widget :entry-format))
|
|
1378 (goto-char from)
|
|
1379 ;; Parse % escapes in format.
|
|
1380 (while (re-search-forward "%\\([bv%]\\)" nil t)
|
|
1381 (let ((escape (aref (match-string 1) 0)))
|
|
1382 (replace-match "" t t)
|
|
1383 (cond ((eq escape ?%)
|
|
1384 (insert "%"))
|
|
1385 ((eq escape ?b)
|
|
1386 (setq button (widget-create-child-and-convert
|
|
1387 widget 'checkbox :value (not (null chosen)))))
|
|
1388 ((eq escape ?v)
|
|
1389 (setq child
|
|
1390 (cond ((not chosen)
|
|
1391 (widget-create-child widget type))
|
|
1392 ((widget-get type :inline)
|
|
1393 (widget-create-child-value
|
|
1394 widget type (cdr chosen)))
|
|
1395 (t
|
|
1396 (widget-create-child-value
|
|
1397 widget type (car (cdr chosen)))))))
|
|
1398 (t
|
|
1399 (error "Unknown escape `%c'" escape)))))
|
|
1400 ;; Update properties.
|
|
1401 (and button child (widget-put child :button button))
|
|
1402 (and button (widget-put widget :buttons (cons button buttons)))
|
|
1403 (and child (widget-put widget :children (cons child children))))))
|
|
1404
|
|
1405 (defun widget-checklist-match (widget values)
|
|
1406 ;; All values must match a type in the checklist.
|
|
1407 (and (listp values)
|
|
1408 (null (cdr (widget-checklist-match-inline widget values)))))
|
|
1409
|
|
1410 (defun widget-checklist-match-inline (widget values)
|
|
1411 ;; Find the values which match a type in the checklist.
|
|
1412 (let ((greedy (widget-get widget :greedy))
|
|
1413 (args (copy-list (widget-get widget :args)))
|
|
1414 found rest)
|
|
1415 (while values
|
|
1416 (let ((answer (widget-checklist-match-up args values)))
|
|
1417 (cond (answer
|
|
1418 (let ((vals (widget-match-inline answer values)))
|
|
1419 (setq found (append found (car vals))
|
|
1420 values (cdr vals)
|
|
1421 args (delq answer args))))
|
|
1422 (greedy
|
|
1423 (setq rest (append rest (list (car values)))
|
|
1424 values (cdr values)))
|
|
1425 (t
|
|
1426 (setq rest (append rest values)
|
|
1427 values nil)))))
|
|
1428 (cons found rest)))
|
|
1429
|
|
1430 (defun widget-checklist-match-find (widget vals)
|
|
1431 ;; Find the vals which match a type in the checklist.
|
|
1432 ;; Return an alist of (TYPE MATCH).
|
|
1433 (let ((greedy (widget-get widget :greedy))
|
|
1434 (args (copy-list (widget-get widget :args)))
|
|
1435 found)
|
|
1436 (while vals
|
|
1437 (let ((answer (widget-checklist-match-up args vals)))
|
|
1438 (cond (answer
|
|
1439 (let ((match (widget-match-inline answer vals)))
|
|
1440 (setq found (cons (cons answer (car match)) found)
|
|
1441 vals (cdr match)
|
|
1442 args (delq answer args))))
|
|
1443 (greedy
|
|
1444 (setq vals (cdr vals)))
|
|
1445 (t
|
|
1446 (setq vals nil)))))
|
|
1447 found))
|
|
1448
|
|
1449 (defun widget-checklist-match-up (args vals)
|
|
1450 ;; Rerturn the first type from ARGS that matches VALS.
|
|
1451 (let (current found)
|
|
1452 (while (and args (null found))
|
|
1453 (setq current (car args)
|
|
1454 args (cdr args)
|
|
1455 found (widget-match-inline current vals)))
|
|
1456 (if found
|
|
1457 current
|
|
1458 nil)))
|
|
1459
|
|
1460 (defun widget-checklist-value-get (widget)
|
|
1461 ;; The values of all selected items.
|
|
1462 (let ((children (widget-get widget :children))
|
|
1463 child result)
|
|
1464 (while children
|
|
1465 (setq child (car children)
|
|
1466 children (cdr children))
|
|
1467 (if (widget-value (widget-get child :button))
|
|
1468 (setq result (append result (widget-apply child :value-inline)))))
|
|
1469 result))
|
|
1470
|
|
1471 (defun widget-checklist-validate (widget)
|
|
1472 ;; Ticked chilren must be valid.
|
|
1473 (let ((children (widget-get widget :children))
|
|
1474 child button found)
|
|
1475 (while (and children (not found))
|
|
1476 (setq child (car children)
|
|
1477 children (cdr children)
|
|
1478 button (widget-get child :button)
|
|
1479 found (and (widget-value button)
|
|
1480 (widget-apply child :validate))))
|
|
1481 found))
|
|
1482
|
|
1483 ;;; The `option' Widget
|
|
1484
|
|
1485 (define-widget 'option 'checklist
|
|
1486 "An widget with an optional item."
|
|
1487 :inline t)
|
|
1488
|
|
1489 ;;; The `choice-item' Widget.
|
|
1490
|
|
1491 (define-widget 'choice-item 'item
|
|
1492 "Button items that delegate action events to their parents."
|
|
1493 :action 'widget-choice-item-action
|
|
1494 :format "%[%t%] \n")
|
|
1495
|
|
1496 (defun widget-choice-item-action (widget &optional event)
|
|
1497 ;; Tell parent what happened.
|
|
1498 (widget-apply (widget-get widget :parent) :action event))
|
|
1499
|
|
1500 ;;; The `radio-button' Widget.
|
|
1501
|
|
1502 (define-widget 'radio-button 'toggle
|
|
1503 "A radio button for use in the `radio' widget."
|
|
1504 :notify 'widget-radio-button-notify
|
20
|
1505 :format "%[%v%]"
|
|
1506 :on "(*)"
|
|
1507 :off "( )")
|
18
|
1508
|
|
1509 (defun widget-radio-button-notify (widget child &optional event)
|
20
|
1510 ;; Tell daddy.
|
18
|
1511 (widget-apply (widget-get widget :parent) :action widget event))
|
|
1512
|
|
1513 ;;; The `radio-button-choice' Widget.
|
|
1514
|
|
1515 (define-widget 'radio-button-choice 'default
|
|
1516 "Select one of multiple options."
|
|
1517 :convert-widget 'widget-types-convert-widget
|
|
1518 :offset 4
|
|
1519 :format "%v"
|
|
1520 :entry-format "%b %v"
|
|
1521 :menu-tag "radio"
|
|
1522 :value-create 'widget-radio-value-create
|
|
1523 :value-delete 'widget-children-value-delete
|
|
1524 :value-get 'widget-radio-value-get
|
|
1525 :value-inline 'widget-radio-value-inline
|
|
1526 :value-set 'widget-radio-value-set
|
|
1527 :error "You must push one of the buttons"
|
|
1528 :validate 'widget-radio-validate
|
|
1529 :match 'widget-choice-match
|
|
1530 :match-inline 'widget-choice-match-inline
|
|
1531 :action 'widget-radio-action)
|
|
1532
|
|
1533 (defun widget-radio-value-create (widget)
|
|
1534 ;; Insert all values
|
|
1535 (let ((args (widget-get widget :args))
|
|
1536 arg)
|
|
1537 (while args
|
|
1538 (setq arg (car args)
|
|
1539 args (cdr args))
|
|
1540 (widget-radio-add-item widget arg))))
|
|
1541
|
|
1542 (defun widget-radio-add-item (widget type)
|
|
1543 "Add to radio widget WIDGET a new radio button item of type TYPE."
|
|
1544 ;; (setq type (widget-convert type))
|
|
1545 (and (eq (preceding-char) ?\n)
|
|
1546 (widget-get widget :indent)
|
|
1547 (insert-char ? (widget-get widget :indent)))
|
|
1548 (widget-specify-insert
|
|
1549 (let* ((value (widget-get widget :value))
|
|
1550 (children (widget-get widget :children))
|
|
1551 (buttons (widget-get widget :buttons))
|
|
1552 (from (point))
|
|
1553 (chosen (and (null (widget-get widget :choice))
|
|
1554 (widget-apply type :match value)))
|
|
1555 child button)
|
|
1556 (insert (widget-get widget :entry-format))
|
|
1557 (goto-char from)
|
|
1558 ;; Parse % escapes in format.
|
|
1559 (while (re-search-forward "%\\([bv%]\\)" nil t)
|
|
1560 (let ((escape (aref (match-string 1) 0)))
|
|
1561 (replace-match "" t t)
|
|
1562 (cond ((eq escape ?%)
|
|
1563 (insert "%"))
|
|
1564 ((eq escape ?b)
|
|
1565 (setq button (widget-create-child-and-convert
|
|
1566 widget 'radio-button
|
|
1567 :value (not (null chosen)))))
|
|
1568 ((eq escape ?v)
|
|
1569 (setq child (if chosen
|
|
1570 (widget-create-child-value
|
|
1571 widget type value)
|
|
1572 (widget-create-child widget type))))
|
|
1573 (t
|
|
1574 (error "Unknown escape `%c'" escape)))))
|
|
1575 ;; Update properties.
|
|
1576 (when chosen
|
|
1577 (widget-put widget :choice type))
|
|
1578 (when button
|
|
1579 (widget-put child :button button)
|
|
1580 (widget-put widget :buttons (nconc buttons (list button))))
|
|
1581 (when child
|
|
1582 (widget-put widget :children (nconc children (list child))))
|
|
1583 child)))
|
|
1584
|
|
1585 (defun widget-radio-value-get (widget)
|
|
1586 ;; Get value of the child widget.
|
|
1587 (let ((chosen (widget-radio-chosen widget)))
|
|
1588 (and chosen (widget-value chosen))))
|
|
1589
|
|
1590 (defun widget-radio-chosen (widget)
|
|
1591 "Return the widget representing the chosen radio button."
|
|
1592 (let ((children (widget-get widget :children))
|
|
1593 current found)
|
|
1594 (while children
|
|
1595 (setq current (car children)
|
|
1596 children (cdr children))
|
|
1597 (let* ((button (widget-get current :button))
|
|
1598 (value (widget-apply button :value-get)))
|
|
1599 (when value
|
|
1600 (setq found current
|
|
1601 children nil))))
|
|
1602 found))
|
|
1603
|
|
1604 (defun widget-radio-value-inline (widget)
|
|
1605 ;; Get value of the child widget.
|
|
1606 (let ((children (widget-get widget :children))
|
|
1607 current found)
|
|
1608 (while children
|
|
1609 (setq current (car children)
|
|
1610 children (cdr children))
|
|
1611 (let* ((button (widget-get current :button))
|
|
1612 (value (widget-apply button :value-get)))
|
|
1613 (when value
|
|
1614 (setq found (widget-apply current :value-inline)
|
|
1615 children nil))))
|
|
1616 found))
|
|
1617
|
|
1618 (defun widget-radio-value-set (widget value)
|
|
1619 ;; We can't just delete and recreate a radio widget, since children
|
|
1620 ;; can be added after the original creation and won't be recreated
|
|
1621 ;; by `:create'.
|
|
1622 (let ((children (widget-get widget :children))
|
|
1623 current found)
|
|
1624 (while children
|
|
1625 (setq current (car children)
|
|
1626 children (cdr children))
|
|
1627 (let* ((button (widget-get current :button))
|
|
1628 (match (and (not found)
|
|
1629 (widget-apply current :match value))))
|
|
1630 (widget-value-set button match)
|
|
1631 (if match
|
|
1632 (widget-value-set current value))
|
|
1633 (setq found (or found match))))))
|
|
1634
|
|
1635 (defun widget-radio-validate (widget)
|
|
1636 ;; Valid if we have made a valid choice.
|
|
1637 (let ((children (widget-get widget :children))
|
|
1638 current found button)
|
|
1639 (while (and children (not found))
|
|
1640 (setq current (car children)
|
|
1641 children (cdr children)
|
|
1642 button (widget-get current :button)
|
|
1643 found (widget-apply button :value-get)))
|
|
1644 (if found
|
|
1645 (widget-apply current :validate)
|
|
1646 widget)))
|
|
1647
|
|
1648 (defun widget-radio-action (widget child event)
|
|
1649 ;; Check if a radio button was pressed.
|
|
1650 (let ((children (widget-get widget :children))
|
|
1651 (buttons (widget-get widget :buttons))
|
|
1652 current)
|
|
1653 (when (memq child buttons)
|
|
1654 (while children
|
|
1655 (setq current (car children)
|
|
1656 children (cdr children))
|
|
1657 (let* ((button (widget-get current :button)))
|
|
1658 (cond ((eq child button)
|
|
1659 (widget-value-set button t))
|
|
1660 ((widget-value button)
|
|
1661 (widget-value-set button nil)))))))
|
|
1662 ;; Pass notification to parent.
|
|
1663 (widget-apply widget :notify child event))
|
|
1664
|
|
1665 ;;; The `insert-button' Widget.
|
|
1666
|
|
1667 (define-widget 'insert-button 'push-button
|
|
1668 "An insert button for the `editable-list' widget."
|
|
1669 :tag "INS"
|
|
1670 :action 'widget-insert-button-action)
|
|
1671
|
|
1672 (defun widget-insert-button-action (widget &optional event)
|
|
1673 ;; Ask the parent to insert a new item.
|
|
1674 (widget-apply (widget-get widget :parent)
|
|
1675 :insert-before (widget-get widget :widget)))
|
|
1676
|
|
1677 ;;; The `delete-button' Widget.
|
|
1678
|
|
1679 (define-widget 'delete-button 'push-button
|
|
1680 "A delete button for the `editable-list' widget."
|
|
1681 :tag "DEL"
|
|
1682 :action 'widget-delete-button-action)
|
|
1683
|
|
1684 (defun widget-delete-button-action (widget &optional event)
|
|
1685 ;; Ask the parent to insert a new item.
|
|
1686 (widget-apply (widget-get widget :parent)
|
|
1687 :delete-at (widget-get widget :widget)))
|
|
1688
|
|
1689 ;;; The `editable-list' Widget.
|
|
1690
|
|
1691 (define-widget 'editable-list 'default
|
|
1692 "A variable list of widgets of the same type."
|
|
1693 :convert-widget 'widget-types-convert-widget
|
|
1694 :offset 12
|
|
1695 :format "%v%i\n"
|
|
1696 :format-handler 'widget-editable-list-format-handler
|
|
1697 :entry-format "%i %d %v"
|
|
1698 :menu-tag "editable-list"
|
|
1699 :value-create 'widget-editable-list-value-create
|
|
1700 :value-delete 'widget-children-value-delete
|
|
1701 :value-get 'widget-editable-list-value-get
|
|
1702 :validate 'widget-editable-list-validate
|
|
1703 :match 'widget-editable-list-match
|
|
1704 :match-inline 'widget-editable-list-match-inline
|
|
1705 :insert-before 'widget-editable-list-insert-before
|
|
1706 :delete-at 'widget-editable-list-delete-at)
|
|
1707
|
|
1708 (defun widget-editable-list-format-handler (widget escape)
|
|
1709 ;; We recognize the insert button.
|
|
1710 (cond ((eq escape ?i)
|
|
1711 (and (widget-get widget :indent)
|
|
1712 (insert-char ? (widget-get widget :indent)))
|
|
1713 (widget-create-child-and-convert widget 'insert-button))
|
|
1714 (t
|
|
1715 (widget-default-format-handler widget escape))))
|
|
1716
|
|
1717 (defun widget-editable-list-value-create (widget)
|
|
1718 ;; Insert all values
|
|
1719 (let* ((value (widget-get widget :value))
|
|
1720 (type (nth 0 (widget-get widget :args)))
|
|
1721 (inlinep (widget-get type :inline))
|
|
1722 children)
|
|
1723 (widget-put widget :value-pos (copy-marker (point)))
|
|
1724 (set-marker-insertion-type (widget-get widget :value-pos) t)
|
|
1725 (while value
|
|
1726 (let ((answer (widget-match-inline type value)))
|
|
1727 (if answer
|
|
1728 (setq children (cons (widget-editable-list-entry-create
|
|
1729 widget
|
|
1730 (if inlinep
|
|
1731 (car answer)
|
|
1732 (car (car answer)))
|
|
1733 t)
|
|
1734 children)
|
|
1735 value (cdr answer))
|
|
1736 (setq value nil))))
|
|
1737 (widget-put widget :children (nreverse children))))
|
|
1738
|
|
1739 (defun widget-editable-list-value-get (widget)
|
|
1740 ;; Get value of the child widget.
|
|
1741 (apply 'append (mapcar (lambda (child) (widget-apply child :value-inline))
|
|
1742 (widget-get widget :children))))
|
|
1743
|
|
1744 (defun widget-editable-list-validate (widget)
|
|
1745 ;; All the chilren must be valid.
|
|
1746 (let ((children (widget-get widget :children))
|
|
1747 child found)
|
|
1748 (while (and children (not found))
|
|
1749 (setq child (car children)
|
|
1750 children (cdr children)
|
|
1751 found (widget-apply child :validate)))
|
|
1752 found))
|
|
1753
|
|
1754 (defun widget-editable-list-match (widget value)
|
|
1755 ;; Value must be a list and all the members must match the type.
|
|
1756 (and (listp value)
|
|
1757 (null (cdr (widget-editable-list-match-inline widget value)))))
|
|
1758
|
|
1759 (defun widget-editable-list-match-inline (widget value)
|
|
1760 (let ((type (nth 0 (widget-get widget :args)))
|
|
1761 (ok t)
|
|
1762 found)
|
|
1763 (while (and value ok)
|
|
1764 (let ((answer (widget-match-inline type value)))
|
|
1765 (if answer
|
|
1766 (setq found (append found (car answer))
|
|
1767 value (cdr answer))
|
|
1768 (setq ok nil))))
|
|
1769 (cons found value)))
|
|
1770
|
|
1771 (defun widget-editable-list-insert-before (widget before)
|
|
1772 ;; Insert a new child in the list of children.
|
|
1773 (save-excursion
|
|
1774 (let ((children (widget-get widget :children))
|
|
1775 (inhibit-read-only t)
|
|
1776 after-change-functions)
|
|
1777 (cond (before
|
|
1778 (goto-char (widget-get before :entry-from)))
|
|
1779 (t
|
|
1780 (goto-char (widget-get widget :value-pos))))
|
|
1781 (let ((child (widget-editable-list-entry-create
|
|
1782 widget nil nil)))
|
|
1783 (when (< (widget-get child :entry-from) (widget-get widget :from))
|
|
1784 (set-marker (widget-get widget :from)
|
|
1785 (widget-get child :entry-from)))
|
|
1786 (widget-specify-text (widget-get child :entry-from)
|
|
1787 (widget-get child :entry-to))
|
|
1788 (if (eq (car children) before)
|
|
1789 (widget-put widget :children (cons child children))
|
|
1790 (while (not (eq (car (cdr children)) before))
|
|
1791 (setq children (cdr children)))
|
|
1792 (setcdr children (cons child (cdr children)))))))
|
|
1793 (widget-setup)
|
|
1794 (widget-apply widget :notify widget))
|
|
1795
|
|
1796 (defun widget-editable-list-delete-at (widget child)
|
|
1797 ;; Delete child from list of children.
|
|
1798 (save-excursion
|
|
1799 (let ((buttons (copy-list (widget-get widget :buttons)))
|
|
1800 button
|
|
1801 (inhibit-read-only t)
|
|
1802 after-change-functions)
|
|
1803 (while buttons
|
|
1804 (setq button (car buttons)
|
|
1805 buttons (cdr buttons))
|
|
1806 (when (eq (widget-get button :widget) child)
|
|
1807 (widget-put widget
|
|
1808 :buttons (delq button (widget-get widget :buttons)))
|
|
1809 (widget-delete button))))
|
|
1810 (let ((entry-from (widget-get child :entry-from))
|
|
1811 (entry-to (widget-get child :entry-to))
|
|
1812 (inhibit-read-only t)
|
|
1813 after-change-functions)
|
|
1814 (widget-delete child)
|
|
1815 (delete-region entry-from entry-to)
|
|
1816 (set-marker entry-from nil)
|
|
1817 (set-marker entry-to nil))
|
|
1818 (widget-put widget :children (delq child (widget-get widget :children))))
|
|
1819 (widget-setup)
|
|
1820 (widget-apply widget :notify widget))
|
|
1821
|
|
1822 (defun widget-editable-list-entry-create (widget value conv)
|
|
1823 ;; Create a new entry to the list.
|
|
1824 (let ((type (nth 0 (widget-get widget :args)))
|
|
1825 child delete insert)
|
|
1826 (widget-specify-insert
|
|
1827 (save-excursion
|
|
1828 (and (widget-get widget :indent)
|
|
1829 (insert-char ? (widget-get widget :indent)))
|
|
1830 (insert (widget-get widget :entry-format)))
|
|
1831 ;; Parse % escapes in format.
|
|
1832 (while (re-search-forward "%\\(.\\)" nil t)
|
|
1833 (let ((escape (aref (match-string 1) 0)))
|
|
1834 (replace-match "" t t)
|
|
1835 (cond ((eq escape ?%)
|
|
1836 (insert "%"))
|
|
1837 ((eq escape ?i)
|
|
1838 (setq insert (widget-create-child-and-convert
|
|
1839 widget 'insert-button)))
|
|
1840 ((eq escape ?d)
|
|
1841 (setq delete (widget-create-child-and-convert
|
|
1842 widget 'delete-button)))
|
|
1843 ((eq escape ?v)
|
|
1844 (if conv
|
|
1845 (setq child (widget-create-child-value
|
|
1846 widget type value))
|
|
1847 (setq child (widget-create-child widget type))))
|
|
1848 (t
|
|
1849 (error "Unknown escape `%c'" escape)))))
|
|
1850 (widget-put widget
|
|
1851 :buttons (cons delete
|
|
1852 (cons insert
|
|
1853 (widget-get widget :buttons))))
|
|
1854 (let ((entry-from (copy-marker (point-min)))
|
|
1855 (entry-to (copy-marker (point-max))))
|
|
1856 (widget-specify-text entry-from entry-to)
|
|
1857 (set-marker-insertion-type entry-from t)
|
|
1858 (set-marker-insertion-type entry-to nil)
|
|
1859 (widget-put child :entry-from entry-from)
|
|
1860 (widget-put child :entry-to entry-to)))
|
|
1861 (widget-put insert :widget child)
|
|
1862 (widget-put delete :widget child)
|
|
1863 child))
|
|
1864
|
|
1865 ;;; The `group' Widget.
|
|
1866
|
|
1867 (define-widget 'group 'default
|
|
1868 "A widget which group other widgets inside."
|
|
1869 :convert-widget 'widget-types-convert-widget
|
|
1870 :format "%v"
|
|
1871 :value-create 'widget-group-value-create
|
|
1872 :value-delete 'widget-children-value-delete
|
|
1873 :value-get 'widget-editable-list-value-get
|
|
1874 :validate 'widget-editable-list-validate
|
|
1875 :match 'widget-group-match
|
|
1876 :match-inline 'widget-group-match-inline)
|
|
1877
|
|
1878 (defun widget-group-value-create (widget)
|
|
1879 ;; Create each component.
|
|
1880 (let ((args (widget-get widget :args))
|
|
1881 (value (widget-get widget :value))
|
|
1882 arg answer children)
|
|
1883 (while args
|
|
1884 (setq arg (car args)
|
|
1885 args (cdr args)
|
|
1886 answer (widget-match-inline arg value)
|
|
1887 value (cdr answer))
|
|
1888 (and (eq (preceding-char) ?\n)
|
|
1889 (widget-get widget :indent)
|
|
1890 (insert-char ? (widget-get widget :indent)))
|
|
1891 (push (cond ((null answer)
|
|
1892 (widget-create-child widget arg))
|
|
1893 ((widget-get arg :inline)
|
|
1894 (widget-create-child-value widget arg (car answer)))
|
|
1895 (t
|
|
1896 (widget-create-child-value widget arg (car (car answer)))))
|
|
1897 children))
|
|
1898 (widget-put widget :children (nreverse children))))
|
|
1899
|
|
1900 (defun widget-group-match (widget values)
|
|
1901 ;; Match if the components match.
|
|
1902 (and (listp values)
|
|
1903 (let ((match (widget-group-match-inline widget values)))
|
|
1904 (and match (null (cdr match))))))
|
|
1905
|
|
1906 (defun widget-group-match-inline (widget vals)
|
|
1907 ;; Match if the components match.
|
|
1908 (let ((args (widget-get widget :args))
|
|
1909 argument answer found)
|
|
1910 (while args
|
|
1911 (setq argument (car args)
|
|
1912 args (cdr args)
|
|
1913 answer (widget-match-inline argument vals))
|
|
1914 (if answer
|
|
1915 (setq vals (cdr answer)
|
|
1916 found (append found (car answer)))
|
|
1917 (setq vals nil
|
|
1918 args nil)))
|
|
1919 (if answer
|
|
1920 (cons found vals)
|
|
1921 nil)))
|
|
1922
|
|
1923 ;;; The `widget-help' Widget.
|
|
1924
|
|
1925 (define-widget 'widget-help 'push-button
|
|
1926 "The widget documentation button."
|
|
1927 :format "%[[%t]%] %d"
|
|
1928 :help-echo "Push me to toggle the documentation."
|
|
1929 :action 'widget-help-action)
|
|
1930
|
|
1931 (defun widget-help-action (widget &optional event)
|
|
1932 "Toggle documentation for WIDGET."
|
|
1933 (let ((old (widget-get widget :doc))
|
|
1934 (new (widget-get widget :widget-doc)))
|
|
1935 (widget-put widget :doc new)
|
|
1936 (widget-put widget :widget-doc old))
|
|
1937 (widget-value-set widget (widget-value widget)))
|
|
1938
|
|
1939 ;;; The Sexp Widgets.
|
|
1940
|
|
1941 (define-widget 'const 'item
|
|
1942 "An immutable sexp."
|
|
1943 :format "%t\n%d")
|
|
1944
|
|
1945 (define-widget 'function-item 'item
|
|
1946 "An immutable function name."
|
|
1947 :format "%v\n%h"
|
|
1948 :documentation-property (lambda (symbol)
|
|
1949 (condition-case nil
|
|
1950 (documentation symbol t)
|
|
1951 (error nil))))
|
|
1952
|
|
1953 (define-widget 'variable-item 'item
|
|
1954 "An immutable variable name."
|
|
1955 :format "%v\n%h"
|
|
1956 :documentation-property 'variable-documentation)
|
|
1957
|
|
1958 (define-widget 'string 'editable-field
|
|
1959 "A string"
|
|
1960 :tag "String"
|
|
1961 :format "%[%t%]: %v")
|
|
1962
|
|
1963 (define-widget 'regexp 'string
|
|
1964 "A regular expression."
|
|
1965 ;; Should do validation.
|
|
1966 :tag "Regexp")
|
|
1967
|
|
1968 (define-widget 'file 'string
|
|
1969 "A file widget.
|
|
1970 It will read a file name from the minibuffer when activated."
|
|
1971 :format "%[%t%]: %v"
|
|
1972 :tag "File"
|
|
1973 :action 'widget-file-action)
|
|
1974
|
|
1975 (defun widget-file-action (widget &optional event)
|
|
1976 ;; Read a file name from the minibuffer.
|
|
1977 (let* ((value (widget-value widget))
|
|
1978 (dir (file-name-directory value))
|
|
1979 (file (file-name-nondirectory value))
|
|
1980 (menu-tag (widget-apply widget :menu-tag-get))
|
|
1981 (must-match (widget-get widget :must-match))
|
|
1982 (answer (read-file-name (concat menu-tag ": (defalt `" value "') ")
|
|
1983 dir nil must-match file)))
|
|
1984 (widget-value-set widget (abbreviate-file-name answer))
|
|
1985 (widget-apply widget :notify widget event)
|
|
1986 (widget-setup)))
|
|
1987
|
|
1988 (define-widget 'directory 'file
|
|
1989 "A directory widget.
|
|
1990 It will read a directory name from the minibuffer when activated."
|
|
1991 :tag "Directory")
|
|
1992
|
|
1993 (define-widget 'symbol 'string
|
|
1994 "A lisp symbol."
|
|
1995 :value nil
|
|
1996 :tag "Symbol"
|
|
1997 :match (lambda (widget value) (symbolp value))
|
|
1998 :value-to-internal (lambda (widget value)
|
|
1999 (if (symbolp value)
|
|
2000 (symbol-name value)
|
|
2001 value))
|
|
2002 :value-to-external (lambda (widget value)
|
|
2003 (if (stringp value)
|
|
2004 (intern value)
|
|
2005 value)))
|
|
2006
|
|
2007 (define-widget 'function 'sexp
|
|
2008 ;; Should complete on functions.
|
|
2009 "A lisp function."
|
|
2010 :tag "Function")
|
|
2011
|
|
2012 (define-widget 'variable 'symbol
|
|
2013 ;; Should complete on variables.
|
|
2014 "A lisp variable."
|
|
2015 :tag "Variable")
|
|
2016
|
|
2017 (define-widget 'sexp 'string
|
|
2018 "An arbitrary lisp expression."
|
|
2019 :tag "Lisp expression"
|
|
2020 :value nil
|
|
2021 :validate 'widget-sexp-validate
|
|
2022 :match (lambda (widget value) t)
|
|
2023 :value-to-internal 'widget-sexp-value-to-internal
|
|
2024 :value-to-external (lambda (widget value) (read value)))
|
|
2025
|
|
2026 (defun widget-sexp-value-to-internal (widget value)
|
|
2027 ;; Use pp for printer representation.
|
|
2028 (let ((pp (pp-to-string value)))
|
|
2029 (while (string-match "\n\\'" pp)
|
|
2030 (setq pp (substring pp 0 -1)))
|
|
2031 (if (or (string-match "\n\\'" pp)
|
|
2032 (> (length pp) 40))
|
|
2033 (concat "\n" pp)
|
|
2034 pp)))
|
|
2035
|
|
2036 (defun widget-sexp-validate (widget)
|
|
2037 ;; Valid if we can read the string and there is no junk left after it.
|
|
2038 (save-excursion
|
|
2039 (let ((buffer (set-buffer (get-buffer-create " *Widget Scratch*"))))
|
|
2040 (erase-buffer)
|
|
2041 (insert (widget-apply widget :value-get))
|
|
2042 (goto-char (point-min))
|
|
2043 (condition-case data
|
|
2044 (let ((value (read buffer)))
|
|
2045 (if (eobp)
|
|
2046 (if (widget-apply widget :match value)
|
|
2047 nil
|
|
2048 (widget-put widget :error (widget-get widget :type-error))
|
|
2049 widget)
|
|
2050 (widget-put widget
|
|
2051 :error (format "Junk at end of expression: %s"
|
|
2052 (buffer-substring (point)
|
|
2053 (point-max))))
|
|
2054 widget))
|
|
2055 (error (widget-put widget :error (error-message-string data))
|
|
2056 widget)))))
|
|
2057
|
|
2058 (define-widget 'integer 'sexp
|
|
2059 "An integer."
|
|
2060 :tag "Integer"
|
|
2061 :value 0
|
|
2062 :type-error "This field should contain an integer"
|
|
2063 :value-to-internal (lambda (widget value)
|
|
2064 (if (integerp value)
|
|
2065 (prin1-to-string value)
|
|
2066 value))
|
|
2067 :match (lambda (widget value) (integerp value)))
|
|
2068
|
|
2069 (define-widget 'character 'string
|
|
2070 "An character."
|
|
2071 :tag "Character"
|
|
2072 :value 0
|
|
2073 :size 1
|
|
2074 :format "%{%t%}: %v\n"
|
|
2075 :type-error "This field should contain a character"
|
|
2076 :value-to-internal (lambda (widget value)
|
|
2077 (if (integerp value)
|
|
2078 (char-to-string value)
|
|
2079 value))
|
|
2080 :value-to-external (lambda (widget value)
|
|
2081 (if (stringp value)
|
|
2082 (aref value 0)
|
|
2083 value))
|
|
2084 :match (lambda (widget value) (integerp value)))
|
|
2085
|
|
2086 (define-widget 'number 'sexp
|
|
2087 "A floating point number."
|
|
2088 :tag "Number"
|
|
2089 :value 0.0
|
|
2090 :type-error "This field should contain a number"
|
|
2091 :value-to-internal (lambda (widget value)
|
|
2092 (if (numberp value)
|
|
2093 (prin1-to-string value)
|
|
2094 value))
|
|
2095 :match (lambda (widget value) (numberp value)))
|
|
2096
|
|
2097 (define-widget 'list 'group
|
|
2098 "A lisp list."
|
|
2099 :tag "List"
|
|
2100 :format "%{%t%}:\n%v")
|
|
2101
|
|
2102 (define-widget 'vector 'group
|
|
2103 "A lisp vector."
|
|
2104 :tag "Vector"
|
|
2105 :format "%{%t%}:\n%v"
|
|
2106 :match 'widget-vector-match
|
|
2107 :value-to-internal (lambda (widget value) (append value nil))
|
|
2108 :value-to-external (lambda (widget value) (apply 'vector value)))
|
|
2109
|
|
2110 (defun widget-vector-match (widget value)
|
|
2111 (and (vectorp value)
|
|
2112 (widget-group-match widget
|
|
2113 (widget-apply :value-to-internal widget value))))
|
|
2114
|
|
2115 (define-widget 'cons 'group
|
|
2116 "A cons-cell."
|
|
2117 :tag "Cons-cell"
|
|
2118 :format "%{%t%}:\n%v"
|
|
2119 :match 'widget-cons-match
|
|
2120 :value-to-internal (lambda (widget value)
|
|
2121 (list (car value) (cdr value)))
|
|
2122 :value-to-external (lambda (widget value)
|
|
2123 (cons (nth 0 value) (nth 1 value))))
|
|
2124
|
|
2125 (defun widget-cons-match (widget value)
|
|
2126 (and (consp value)
|
|
2127 (widget-group-match widget
|
|
2128 (widget-apply widget :value-to-internal value))))
|
|
2129
|
|
2130 (define-widget 'choice 'menu-choice
|
|
2131 "A union of several sexp types."
|
|
2132 :tag "Choice"
|
|
2133 :format "%[%t%]: %v")
|
|
2134
|
|
2135 (define-widget 'radio 'radio-button-choice
|
|
2136 "A union of several sexp types."
|
|
2137 :tag "Choice"
|
|
2138 :format "%{%t%}:\n%v")
|
|
2139
|
|
2140 (define-widget 'repeat 'editable-list
|
|
2141 "A variable length homogeneous list."
|
|
2142 :tag "Repeat"
|
|
2143 :format "%{%t%}:\n%v%i\n")
|
|
2144
|
|
2145 (define-widget 'set 'checklist
|
|
2146 "A list of members from a fixed set."
|
|
2147 :tag "Set"
|
|
2148 :format "%{%t%}:\n%v")
|
|
2149
|
|
2150 (define-widget 'boolean 'toggle
|
|
2151 "To be nil or non-nil, that is the question."
|
|
2152 :tag "Boolean"
|
20
|
2153 :format "%{%t%}: %[%v%]")
|
18
|
2154
|
|
2155 ;;; The `color' Widget.
|
|
2156
|
|
2157 (define-widget 'color-item 'choice-item
|
|
2158 "A color name (with sample)."
|
|
2159 :format "%v (%[sample%])\n"
|
|
2160 :button-face-get 'widget-color-item-button-face-get)
|
|
2161
|
|
2162 (defun widget-color-item-button-face-get (widget)
|
|
2163 ;; We create a face from the value.
|
|
2164 (require 'facemenu)
|
|
2165 (condition-case nil
|
|
2166 (facemenu-get-face (intern (concat "fg:" (widget-value widget))))
|
|
2167 (error 'default)))
|
|
2168
|
|
2169 (define-widget 'color 'push-button
|
|
2170 "Choose a color name (with sample)."
|
|
2171 :format "%[%t%]: %v"
|
|
2172 :tag "Color"
|
|
2173 :value "default"
|
|
2174 :value-create 'widget-color-value-create
|
|
2175 :value-delete 'widget-children-value-delete
|
|
2176 :value-get 'widget-color-value-get
|
|
2177 :value-set 'widget-color-value-set
|
|
2178 :action 'widget-color-action
|
|
2179 :match 'widget-field-match
|
|
2180 :tag "Color")
|
|
2181
|
|
2182 (defvar widget-color-choice-list nil)
|
|
2183 ;; Variable holding the possible colors.
|
|
2184
|
|
2185 (defun widget-color-choice-list ()
|
|
2186 (unless widget-color-choice-list
|
|
2187 (setq widget-color-choice-list
|
|
2188 (mapcar '(lambda (color) (list color))
|
|
2189 (x-defined-colors))))
|
|
2190 widget-color-choice-list)
|
|
2191
|
|
2192 (defun widget-color-value-create (widget)
|
|
2193 (let ((child (widget-create-child-and-convert
|
|
2194 widget 'color-item (widget-get widget :value))))
|
|
2195 (widget-put widget :children (list child))))
|
|
2196
|
|
2197 (defun widget-color-value-get (widget)
|
|
2198 ;; Pass command to first child.
|
|
2199 (widget-apply (car (widget-get widget :children)) :value-get))
|
|
2200
|
|
2201 (defun widget-color-value-set (widget value)
|
|
2202 ;; Pass command to first child.
|
|
2203 (widget-apply (car (widget-get widget :children)) :value-set value))
|
|
2204
|
|
2205 (defvar widget-color-history nil
|
|
2206 "History of entered colors")
|
|
2207
|
|
2208 (defun widget-color-action (widget &optional event)
|
|
2209 ;; Prompt for a color.
|
|
2210 (let* ((tag (widget-apply widget :menu-tag-get))
|
|
2211 (prompt (concat tag ": "))
|
|
2212 (answer (cond ((string-match "XEmacs" emacs-version)
|
|
2213 (read-color prompt))
|
|
2214 ((fboundp 'x-defined-colors)
|
|
2215 (completing-read (concat tag ": ")
|
|
2216 (widget-color-choice-list)
|
|
2217 nil nil nil 'widget-color-history))
|
|
2218 (t
|
|
2219 (read-string prompt (widget-value widget))))))
|
|
2220 (unless (zerop (length answer))
|
|
2221 (widget-value-set widget answer)
|
|
2222 (widget-apply widget :notify widget event)
|
|
2223 (widget-setup))))
|
|
2224
|
|
2225 ;;; The Help Echo
|
|
2226
|
|
2227 (defun widget-echo-help-mouse ()
|
|
2228 "Display the help message for the widget under the mouse.
|
|
2229 Enable with (run-with-idle-timer 1 t 'widget-echo-help-mouse)"
|
|
2230 (let* ((pos (mouse-position))
|
|
2231 (frame (car pos))
|
|
2232 (x (car (cdr pos)))
|
|
2233 (y (cdr (cdr pos)))
|
|
2234 (win (window-at x y frame))
|
|
2235 (where (coordinates-in-window-p (cons x y) win)))
|
|
2236 (when (consp where)
|
|
2237 (save-window-excursion
|
|
2238 (progn ; save-excursion
|
|
2239 (select-window win)
|
|
2240 (let* ((result (compute-motion (window-start win)
|
|
2241 '(0 . 0)
|
|
2242 (window-end win)
|
|
2243 where
|
|
2244 (window-width win)
|
|
2245 (cons (window-hscroll) 0)
|
|
2246 win)))
|
|
2247 (when (and (eq (nth 1 result) x)
|
|
2248 (eq (nth 2 result) y))
|
|
2249 (widget-echo-help (nth 0 result))))))))
|
|
2250 (unless track-mouse
|
|
2251 (setq track-mouse t)
|
|
2252 (add-hook 'post-command-hook 'widget-stop-mouse-tracking)))
|
|
2253
|
|
2254 (defun widget-stop-mouse-tracking (&rest args)
|
|
2255 "Stop the mouse tracking done while idle."
|
|
2256 (remove-hook 'post-command-hook 'widget-stop-mouse-tracking)
|
|
2257 (setq track-mouse nil))
|
|
2258
|
|
2259 (defun widget-at (pos)
|
|
2260 "The button or field at POS."
|
|
2261 (or (get-text-property pos 'button)
|
|
2262 (get-text-property pos 'field)))
|
|
2263
|
|
2264 (defun widget-echo-help (pos)
|
|
2265 "Display the help echo for widget at POS."
|
|
2266 (let* ((widget (widget-at pos))
|
|
2267 (help-echo (and widget (widget-get widget :help-echo))))
|
|
2268 (cond ((stringp help-echo)
|
|
2269 (message "%s" help-echo))
|
|
2270 ((and (symbolp help-echo) (fboundp help-echo)
|
|
2271 (stringp (setq help-echo (funcall help-echo widget))))
|
|
2272 (message "%s" help-echo)))))
|
|
2273
|
|
2274 ;;; The End:
|
|
2275
|
|
2276 (provide 'widget-edit)
|
|
2277
|
|
2278 ;; widget-edit.el ends here
|