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