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