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