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
|
44
|
7 ;; Version: 1.67
|
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
|
32
|
736 (error "No buttons or fields found"))))))
|
|
737 (setq button (widget-at (point)))
|
|
738 (if (and button (widget-get button :tab-order)
|
|
739 (< (widget-get button :tab-order) 0))
|
|
740 (setq arg (1+ arg))))))
|
28
|
741 (while (< arg 0)
|
|
742 (if (= (point-min) (point))
|
|
743 (forward-char 1))
|
|
744 (setq arg (1+ arg))
|
|
745 (let ((previous (cond ((get-text-property (1- (point)) 'button)
|
|
746 (previous-single-property-change (point) 'button))
|
|
747 ((get-text-property (1- (point)) 'field)
|
|
748 (previous-single-property-change (point) 'field))
|
|
749 (t
|
|
750 (point)))))
|
|
751 (if (null previous) ; Widget extends to beg. of buffer
|
|
752 (setq previous (point-max)))
|
|
753 (let ((button (previous-single-property-change previous 'button))
|
|
754 (field (previous-single-property-change previous 'field)))
|
|
755 (cond ((and button field)
|
|
756 (goto-char (max button field)))
|
|
757 (button (goto-char button))
|
|
758 (field (goto-char field))
|
|
759 (t
|
|
760 (let ((button (previous-single-property-change
|
|
761 (point-max) 'button))
|
|
762 (field (previous-single-property-change
|
|
763 (point-max) 'field)))
|
|
764 (cond ((and button field) (goto-char (max button field)))
|
|
765 (button (goto-char button))
|
|
766 (field (goto-char field))
|
|
767 (t
|
|
768 (error "No buttons or fields found"))))))))
|
|
769 (let ((button (previous-single-property-change (point) 'button))
|
|
770 (field (previous-single-property-change (point) 'field)))
|
|
771 (cond ((and button field)
|
|
772 (goto-char (max button field)))
|
|
773 (button (goto-char button))
|
32
|
774 (field (goto-char field)))
|
|
775 (setq button (widget-at (point)))
|
|
776 (if (and button (widget-get button :tab-order)
|
|
777 (< (widget-get button :tab-order) 0))
|
|
778 (setq arg (1- arg)))))
|
28
|
779 (widget-echo-help (point))
|
|
780 (run-hooks 'widget-move-hook))
|
|
781
|
|
782 (defun widget-forward (arg)
|
|
783 "Move point to the next field or button.
|
|
784 With optional ARG, move across that many fields."
|
|
785 (interactive "p")
|
|
786 (run-hooks 'widget-forward-hook)
|
|
787 (widget-move arg))
|
|
788
|
|
789 (defun widget-backward (arg)
|
|
790 "Move point to the previous field or button.
|
|
791 With optional ARG, move across that many fields."
|
|
792 (interactive "p")
|
|
793 (run-hooks 'widget-backward-hook)
|
|
794 (widget-move (- arg)))
|
|
795
|
|
796 (defun widget-beginning-of-line ()
|
|
797 "Go to beginning of field or beginning of line, whichever is first."
|
|
798 (interactive)
|
|
799 (let ((bol (save-excursion (beginning-of-line) (point)))
|
|
800 (prev (previous-single-property-change (point) 'field)))
|
|
801 (goto-char (max bol (or prev bol)))))
|
|
802
|
|
803 (defun widget-end-of-line ()
|
|
804 "Go to end of field or end of line, whichever is first."
|
|
805 (interactive)
|
|
806 (let ((bol (save-excursion (end-of-line) (point)))
|
|
807 (prev (next-single-property-change (point) 'field)))
|
|
808 (goto-char (min bol (or prev bol)))))
|
|
809
|
|
810 (defun widget-kill-line ()
|
|
811 "Kill to end of field or end of line, whichever is first."
|
|
812 (interactive)
|
|
813 (let ((field (get-text-property (point) 'field))
|
|
814 (newline (save-excursion (search-forward "\n")))
|
|
815 (next (next-single-property-change (point) 'field)))
|
|
816 (if (and field (> newline next))
|
|
817 (kill-region (point) next)
|
|
818 (call-interactively 'kill-line))))
|
|
819
|
|
820 ;;; Setting up the buffer.
|
|
821
|
|
822 (defvar widget-field-new nil)
|
|
823 ;; List of all newly created editable fields in the buffer.
|
|
824 (make-variable-buffer-local 'widget-field-new)
|
|
825
|
|
826 (defvar widget-field-list nil)
|
|
827 ;; List of all editable fields in the buffer.
|
|
828 (make-variable-buffer-local 'widget-field-list)
|
|
829
|
|
830 (defun widget-setup ()
|
|
831 "Setup current buffer so editing string widgets works."
|
|
832 (let ((inhibit-read-only t)
|
|
833 (after-change-functions nil)
|
|
834 field)
|
|
835 (while widget-field-new
|
|
836 (setq field (car widget-field-new)
|
|
837 widget-field-new (cdr widget-field-new)
|
|
838 widget-field-list (cons field widget-field-list))
|
|
839 (let ((from (widget-get field :value-from))
|
|
840 (to (widget-get field :value-to)))
|
|
841 (widget-specify-field field from to)
|
|
842 (move-marker from (1- from))
|
|
843 (move-marker to (1+ to)))))
|
|
844 (widget-clear-undo)
|
|
845 ;; We need to maintain text properties and size of the editing fields.
|
|
846 (make-local-variable 'after-change-functions)
|
|
847 (if widget-field-list
|
|
848 (setq after-change-functions '(widget-after-change))
|
|
849 (setq after-change-functions nil)))
|
|
850
|
|
851 (defvar widget-field-last nil)
|
|
852 ;; Last field containing point.
|
|
853 (make-variable-buffer-local 'widget-field-last)
|
|
854
|
|
855 (defvar widget-field-was nil)
|
|
856 ;; The widget data before the change.
|
|
857 (make-variable-buffer-local 'widget-field-was)
|
|
858
|
|
859 (defun widget-field-find (pos)
|
|
860 ;; Find widget whose editing field is located at POS.
|
|
861 ;; Return nil if POS is not inside and editing field.
|
|
862 ;;
|
|
863 ;; This is only used in `widget-field-modified', since ordinarily
|
|
864 ;; you would just test the field property.
|
|
865 (let ((fields widget-field-list)
|
|
866 field found)
|
|
867 (while fields
|
|
868 (setq field (car fields)
|
|
869 fields (cdr fields))
|
|
870 (let ((from (widget-get field :value-from))
|
|
871 (to (widget-get field :value-to)))
|
|
872 (if (and from to (< from pos) (> to pos))
|
|
873 (setq fields nil
|
|
874 found field))))
|
|
875 found))
|
|
876
|
|
877 (defun widget-after-change (from to old)
|
|
878 ;; Adjust field size and text properties.
|
|
879 (condition-case nil
|
|
880 (let ((field (widget-field-find from))
|
|
881 (inhibit-read-only t))
|
|
882 (cond ((null field))
|
|
883 ((not (eq field (widget-field-find to)))
|
|
884 (debug)
|
|
885 (message "Error: `widget-after-change' called on two fields"))
|
|
886 (t
|
|
887 (let ((size (widget-get field :size)))
|
|
888 (if size
|
|
889 (let ((begin (1+ (widget-get field :value-from)))
|
|
890 (end (1- (widget-get field :value-to))))
|
|
891 (widget-specify-field-update field begin end)
|
|
892 (cond ((< (- end begin) size)
|
|
893 ;; Field too small.
|
|
894 (save-excursion
|
|
895 (goto-char end)
|
|
896 (insert-char ?\ (- (+ begin size) end))
|
|
897 (widget-specify-field-update field
|
|
898 begin
|
|
899 (+ begin size))))
|
|
900 ((> (- end begin) size)
|
|
901 ;; Field too large and
|
|
902 (if (or (< (point) (+ begin size))
|
|
903 (> (point) end))
|
|
904 ;; Point is outside extra space.
|
|
905 (setq begin (+ begin size))
|
|
906 ;; Point is within the extra space.
|
|
907 (setq begin (point)))
|
|
908 (save-excursion
|
|
909 (goto-char end)
|
|
910 (while (and (eq (preceding-char) ?\ )
|
|
911 (> (point) begin))
|
|
912 (delete-backward-char 1))))))
|
|
913 (widget-specify-field-update field from to)))
|
|
914 (widget-apply field :notify field))))
|
|
915 (error (debug))))
|
|
916
|
|
917 ;;; Widget Functions
|
|
918 ;;
|
|
919 ;; These functions are used in the definition of multiple widgets.
|
|
920
|
|
921 (defun widget-children-value-delete (widget)
|
|
922 "Delete all :children and :buttons in WIDGET."
|
|
923 (mapcar 'widget-delete (widget-get widget :children))
|
|
924 (widget-put widget :children nil)
|
|
925 (mapcar 'widget-delete (widget-get widget :buttons))
|
|
926 (widget-put widget :buttons nil))
|
|
927
|
|
928 (defun widget-types-convert-widget (widget)
|
|
929 "Convert :args as widget types in WIDGET."
|
|
930 (widget-put widget :args (mapcar 'widget-convert (widget-get widget :args)))
|
|
931 widget)
|
|
932
|
|
933 ;;; The `default' Widget.
|
|
934
|
|
935 (define-widget 'default nil
|
|
936 "Basic widget other widgets are derived from."
|
|
937 :value-to-internal (lambda (widget value) value)
|
|
938 :value-to-external (lambda (widget value) value)
|
|
939 :create 'widget-default-create
|
|
940 :indent nil
|
|
941 :offset 0
|
|
942 :format-handler 'widget-default-format-handler
|
|
943 :button-face-get 'widget-default-button-face-get
|
|
944 :sample-face-get 'widget-default-sample-face-get
|
|
945 :delete 'widget-default-delete
|
|
946 :value-set 'widget-default-value-set
|
|
947 :value-inline 'widget-default-value-inline
|
|
948 :menu-tag-get 'widget-default-menu-tag-get
|
|
949 :validate (lambda (widget) nil)
|
|
950 :action 'widget-default-action
|
|
951 :notify 'widget-default-notify)
|
|
952
|
|
953 (defun widget-default-create (widget)
|
|
954 "Create WIDGET at point in the current buffer."
|
|
955 (widget-specify-insert
|
|
956 (let ((from (point))
|
|
957 (tag (widget-get widget :tag))
|
|
958 (glyph (widget-get widget :tag-glyph))
|
|
959 (doc (widget-get widget :doc))
|
|
960 button-begin button-end
|
|
961 sample-begin sample-end
|
|
962 doc-begin doc-end
|
|
963 value-pos)
|
|
964 (insert (widget-get widget :format))
|
|
965 (goto-char from)
|
|
966 ;; Parse escapes in format.
|
|
967 (while (re-search-forward "%\\(.\\)" nil t)
|
|
968 (let ((escape (aref (match-string 1) 0)))
|
|
969 (replace-match "" t t)
|
|
970 (cond ((eq escape ?%)
|
|
971 (insert "%"))
|
|
972 ((eq escape ?\[)
|
|
973 (setq button-begin (point)))
|
|
974 ((eq escape ?\])
|
|
975 (setq button-end (point)))
|
|
976 ((eq escape ?\{)
|
|
977 (setq sample-begin (point)))
|
|
978 ((eq escape ?\})
|
|
979 (setq sample-end (point)))
|
|
980 ((eq escape ?n)
|
|
981 (when (widget-get widget :indent)
|
|
982 (insert "\n")
|
|
983 (insert-char ? (widget-get widget :indent))))
|
|
984 ((eq escape ?t)
|
|
985 (cond (glyph
|
|
986 (widget-glyph-insert widget (or tag "image") glyph))
|
|
987 (tag
|
|
988 (insert tag))
|
|
989 (t
|
|
990 (let ((standard-output (current-buffer)))
|
|
991 (princ (widget-get widget :value))))))
|
|
992 ((eq escape ?d)
|
|
993 (when doc
|
|
994 (setq doc-begin (point))
|
|
995 (insert doc)
|
|
996 (while (eq (preceding-char) ?\n)
|
|
997 (delete-backward-char 1))
|
|
998 (insert "\n")
|
|
999 (setq doc-end (point))))
|
|
1000 ((eq escape ?v)
|
|
1001 (if (and button-begin (not button-end))
|
|
1002 (widget-apply widget :value-create)
|
|
1003 (setq value-pos (point))))
|
|
1004 (t
|
|
1005 (widget-apply widget :format-handler escape)))))
|
|
1006 ;; Specify button, sample, and doc, and insert value.
|
|
1007 (and button-begin button-end
|
|
1008 (widget-specify-button widget button-begin button-end))
|
|
1009 (and sample-begin sample-end
|
|
1010 (widget-specify-sample widget sample-begin sample-end))
|
|
1011 (and doc-begin doc-end
|
|
1012 (widget-specify-doc widget doc-begin doc-end))
|
|
1013 (when value-pos
|
|
1014 (goto-char value-pos)
|
|
1015 (widget-apply widget :value-create)))
|
|
1016 (let ((from (copy-marker (point-min)))
|
|
1017 (to (copy-marker (point-max))))
|
|
1018 (widget-specify-text from to)
|
|
1019 (set-marker-insertion-type from t)
|
|
1020 (set-marker-insertion-type to nil)
|
|
1021 (widget-put widget :from from)
|
|
1022 (widget-put widget :to to))))
|
|
1023
|
|
1024 (defun widget-default-format-handler (widget escape)
|
|
1025 ;; We recognize the %h escape by default.
|
|
1026 (let* ((buttons (widget-get widget :buttons))
|
|
1027 (doc-property (widget-get widget :documentation-property))
|
|
1028 (doc-try (cond ((widget-get widget :doc))
|
|
1029 ((symbolp doc-property)
|
|
1030 (documentation-property (widget-get widget :value)
|
|
1031 doc-property))
|
|
1032 (t
|
|
1033 (funcall doc-property (widget-get widget :value)))))
|
|
1034 (doc-text (and (stringp doc-try)
|
|
1035 (> (length doc-try) 1)
|
|
1036 doc-try)))
|
|
1037 (cond ((eq escape ?h)
|
|
1038 (when doc-text
|
|
1039 (and (eq (preceding-char) ?\n)
|
|
1040 (widget-get widget :indent)
|
|
1041 (insert-char ? (widget-get widget :indent)))
|
|
1042 ;; The `*' in the beginning is redundant.
|
|
1043 (when (eq (aref doc-text 0) ?*)
|
|
1044 (setq doc-text (substring doc-text 1)))
|
|
1045 ;; Get rid of trailing newlines.
|
|
1046 (when (string-match "\n+\\'" doc-text)
|
|
1047 (setq doc-text (substring doc-text 0 (match-beginning 0))))
|
|
1048 (push (if (string-match "\n." doc-text)
|
|
1049 ;; Allow multiline doc to be hiden.
|
|
1050 (widget-create-child-and-convert
|
|
1051 widget 'widget-help
|
|
1052 :doc (progn
|
|
1053 (string-match "\\`.*" doc-text)
|
|
1054 (match-string 0 doc-text))
|
|
1055 :widget-doc doc-text
|
|
1056 "?")
|
|
1057 ;; A single line is just inserted.
|
|
1058 (widget-create-child-and-convert
|
|
1059 widget 'item :format "%d" :doc doc-text nil))
|
|
1060 buttons)))
|
|
1061 (t
|
|
1062 (error "Unknown escape `%c'" escape)))
|
|
1063 (widget-put widget :buttons buttons)))
|
|
1064
|
|
1065 (defun widget-default-button-face-get (widget)
|
|
1066 ;; Use :button-face or widget-button-face
|
|
1067 (or (widget-get widget :button-face) 'widget-button-face))
|
|
1068
|
|
1069 (defun widget-default-sample-face-get (widget)
|
|
1070 ;; Use :sample-face.
|
|
1071 (widget-get widget :sample-face))
|
|
1072
|
|
1073 (defun widget-default-delete (widget)
|
|
1074 ;; Remove widget from the buffer.
|
|
1075 (let ((from (widget-get widget :from))
|
|
1076 (to (widget-get widget :to))
|
|
1077 (inhibit-read-only t)
|
|
1078 after-change-functions)
|
|
1079 (widget-apply widget :value-delete)
|
44
|
1080 (when (< from to)
|
|
1081 ;; Kludge: this doesn't need to be true for empty formats.
|
|
1082 (delete-region from to))
|
28
|
1083 (set-marker from nil)
|
|
1084 (set-marker to nil)))
|
|
1085
|
|
1086 (defun widget-default-value-set (widget value)
|
|
1087 ;; Recreate widget with new value.
|
|
1088 (save-excursion
|
|
1089 (goto-char (widget-get widget :from))
|
|
1090 (widget-apply widget :delete)
|
|
1091 (widget-put widget :value value)
|
|
1092 (widget-apply widget :create)))
|
|
1093
|
|
1094 (defun widget-default-value-inline (widget)
|
|
1095 ;; Wrap value in a list unless it is inline.
|
|
1096 (if (widget-get widget :inline)
|
|
1097 (widget-value widget)
|
|
1098 (list (widget-value widget))))
|
|
1099
|
|
1100 (defun widget-default-menu-tag-get (widget)
|
|
1101 ;; Use tag or value for menus.
|
|
1102 (or (widget-get widget :menu-tag)
|
|
1103 (widget-get widget :tag)
|
|
1104 (widget-princ-to-string (widget-get widget :value))))
|
|
1105
|
|
1106 (defun widget-default-action (widget &optional event)
|
|
1107 ;; Notify the parent when a widget change
|
|
1108 (let ((parent (widget-get widget :parent)))
|
|
1109 (when parent
|
|
1110 (widget-apply parent :notify widget event))))
|
|
1111
|
|
1112 (defun widget-default-notify (widget child &optional event)
|
|
1113 ;; Pass notification to parent.
|
|
1114 (widget-default-action widget event))
|
|
1115
|
|
1116 ;;; The `item' Widget.
|
|
1117
|
|
1118 (define-widget 'item 'default
|
|
1119 "Constant items for inclusion in other widgets."
|
|
1120 :convert-widget 'widget-item-convert-widget
|
|
1121 :value-create 'widget-item-value-create
|
|
1122 :value-delete 'ignore
|
|
1123 :value-get 'widget-item-value-get
|
|
1124 :match 'widget-item-match
|
|
1125 :match-inline 'widget-item-match-inline
|
|
1126 :action 'widget-item-action
|
|
1127 :format "%t\n")
|
|
1128
|
|
1129 (defun widget-item-convert-widget (widget)
|
|
1130 ;; Initialize :value from :args in WIDGET.
|
|
1131 (let ((args (widget-get widget :args)))
|
|
1132 (when args
|
|
1133 (widget-put widget :value (widget-apply widget
|
|
1134 :value-to-internal (car args)))
|
|
1135 (widget-put widget :args nil)))
|
|
1136 widget)
|
|
1137
|
|
1138 (defun widget-item-value-create (widget)
|
|
1139 ;; Insert the printed representation of the value.
|
|
1140 (let ((standard-output (current-buffer)))
|
|
1141 (princ (widget-get widget :value))))
|
|
1142
|
|
1143 (defun widget-item-match (widget value)
|
|
1144 ;; Match if the value is the same.
|
|
1145 (equal (widget-get widget :value) value))
|
|
1146
|
|
1147 (defun widget-item-match-inline (widget values)
|
|
1148 ;; Match if the value is the same.
|
|
1149 (let ((value (widget-get widget :value)))
|
|
1150 (and (listp value)
|
|
1151 (<= (length value) (length values))
|
|
1152 (let ((head (subseq values 0 (length value))))
|
|
1153 (and (equal head value)
|
|
1154 (cons head (subseq values (length value))))))))
|
|
1155
|
|
1156 (defun widget-item-action (widget &optional event)
|
|
1157 ;; Just notify itself.
|
|
1158 (widget-apply widget :notify widget event))
|
|
1159
|
|
1160 (defun widget-item-value-get (widget)
|
|
1161 ;; Items are simple.
|
|
1162 (widget-get widget :value))
|
|
1163
|
|
1164 ;;; The `push-button' Widget.
|
|
1165
|
|
1166 (defcustom widget-push-button-gui t
|
|
1167 "If non nil, use GUI push buttons when available."
|
|
1168 :group 'widgets
|
|
1169 :type 'boolean)
|
|
1170
|
|
1171 ;; Cache already created GUI objects.
|
|
1172 (defvar widget-push-button-cache nil)
|
|
1173
|
|
1174 (define-widget 'push-button 'item
|
|
1175 "A pushable button."
|
|
1176 :value-create 'widget-push-button-value-create
|
|
1177 :format "%[%v%]")
|
|
1178
|
|
1179 (defun widget-push-button-value-create (widget)
|
|
1180 ;; Insert text representing the `on' and `off' states.
|
|
1181 (let* ((tag (or (widget-get widget :tag)
|
|
1182 (widget-get widget :value)))
|
|
1183 (text (concat "[" tag "]"))
|
|
1184 (gui (cdr (assoc tag widget-push-button-cache))))
|
|
1185 (if (and (fboundp 'make-gui-button)
|
|
1186 (fboundp 'make-glyph)
|
|
1187 widget-push-button-gui
|
|
1188 (fboundp 'device-on-window-system-p)
|
|
1189 (device-on-window-system-p)
|
|
1190 (string-match "XEmacs" emacs-version))
|
|
1191 (progn
|
|
1192 (unless gui
|
|
1193 (setq gui (make-gui-button tag 'widget-gui-action widget))
|
|
1194 (push (cons tag gui) widget-push-button-cache))
|
|
1195 (widget-glyph-insert-glyph widget text
|
|
1196 (make-glyph (car (aref gui 1)))))
|
|
1197 (insert text))))
|
|
1198
|
|
1199 (defun widget-gui-action (widget)
|
|
1200 "Apply :action for WIDGET."
|
|
1201 (widget-apply widget :action (this-command-keys)))
|
|
1202
|
|
1203 ;;; The `link' Widget.
|
|
1204
|
|
1205 (define-widget 'link 'item
|
|
1206 "An embedded link."
|
30
|
1207 :help-echo "Follow the link."
|
28
|
1208 :format "%[_%t_%]")
|
|
1209
|
|
1210 ;;; The `info-link' Widget.
|
|
1211
|
|
1212 (define-widget 'info-link 'link
|
|
1213 "A link to an info file."
|
|
1214 :action 'widget-info-link-action)
|
|
1215
|
|
1216 (defun widget-info-link-action (widget &optional event)
|
|
1217 "Open the info node specified by WIDGET."
|
|
1218 (Info-goto-node (widget-value widget)))
|
|
1219
|
|
1220 ;;; The `url-link' Widget.
|
|
1221
|
|
1222 (define-widget 'url-link 'link
|
|
1223 "A link to an www page."
|
|
1224 :action 'widget-url-link-action)
|
|
1225
|
|
1226 (defun widget-url-link-action (widget &optional event)
|
|
1227 "Open the url specified by WIDGET."
|
|
1228 (require 'browse-url)
|
|
1229 (funcall browse-url-browser-function (widget-value widget)))
|
|
1230
|
|
1231 ;;; The `editable-field' Widget.
|
|
1232
|
|
1233 (define-widget 'editable-field 'default
|
|
1234 "An editable text field."
|
|
1235 :convert-widget 'widget-item-convert-widget
|
|
1236 :keymap widget-field-keymap
|
|
1237 :format "%v"
|
|
1238 :value ""
|
|
1239 :action 'widget-field-action
|
|
1240 :validate 'widget-field-validate
|
|
1241 :valid-regexp ""
|
|
1242 :error "No match"
|
|
1243 :value-create 'widget-field-value-create
|
|
1244 :value-delete 'widget-field-value-delete
|
|
1245 :value-get 'widget-field-value-get
|
|
1246 :match 'widget-field-match)
|
|
1247
|
|
1248 ;; History of field minibuffer edits.
|
|
1249 (defvar widget-field-history nil)
|
|
1250
|
|
1251 (defun widget-field-action (widget &optional event)
|
|
1252 ;; Edit the value in the minibuffer.
|
|
1253 (let ((tag (widget-apply widget :menu-tag-get))
|
|
1254 (invalid (widget-apply widget :validate)))
|
|
1255 (when invalid
|
|
1256 (error (widget-get invalid :error)))
|
|
1257 (widget-value-set widget
|
|
1258 (widget-apply widget
|
|
1259 :value-to-external
|
|
1260 (read-string (concat tag ": ")
|
|
1261 (widget-apply
|
|
1262 widget
|
|
1263 :value-to-internal
|
|
1264 (widget-value widget))
|
|
1265 'widget-field-history)))
|
|
1266 (widget-apply widget :notify widget event)
|
|
1267 (widget-setup)))
|
|
1268
|
|
1269 (defun widget-field-validate (widget)
|
|
1270 ;; Valid if the content matches `:valid-regexp'.
|
|
1271 (save-excursion
|
|
1272 (let ((value (widget-apply widget :value-get))
|
|
1273 (regexp (widget-get widget :valid-regexp)))
|
|
1274 (if (string-match regexp value)
|
|
1275 nil
|
|
1276 widget))))
|
|
1277
|
|
1278 (defun widget-field-value-create (widget)
|
|
1279 ;; Create an editable text field.
|
|
1280 (insert " ")
|
|
1281 (let ((size (widget-get widget :size))
|
|
1282 (value (widget-get widget :value))
|
|
1283 (from (point)))
|
|
1284 (insert value)
|
|
1285 (and size
|
|
1286 (< (length value) size)
|
|
1287 (insert-char ?\ (- size (length value))))
|
|
1288 (unless (memq widget widget-field-list)
|
|
1289 (setq widget-field-new (cons widget widget-field-new)))
|
|
1290 (widget-put widget :value-to (copy-marker (point)))
|
|
1291 (set-marker-insertion-type (widget-get widget :value-to) nil)
|
|
1292 (if (null size)
|
|
1293 (insert ?\n)
|
|
1294 (insert ?\ ))
|
|
1295 (widget-put widget :value-from (copy-marker from))
|
|
1296 (set-marker-insertion-type (widget-get widget :value-from) t)))
|
|
1297
|
|
1298 (defun widget-field-value-delete (widget)
|
|
1299 ;; Remove the widget from the list of active editing fields.
|
|
1300 (setq widget-field-list (delq widget widget-field-list))
|
|
1301 ;; These are nil if the :format string doesn't contain `%v'.
|
|
1302 (when (widget-get widget :value-from)
|
|
1303 (set-marker (widget-get widget :value-from) nil))
|
|
1304 (when (widget-get widget :value-from)
|
|
1305 (set-marker (widget-get widget :value-to) nil)))
|
|
1306
|
|
1307 (defun widget-field-value-get (widget)
|
|
1308 ;; Return current text in editing field.
|
|
1309 (let ((from (widget-get widget :value-from))
|
|
1310 (to (widget-get widget :value-to))
|
|
1311 (size (widget-get widget :size))
|
|
1312 (secret (widget-get widget :secret))
|
|
1313 (old (current-buffer)))
|
|
1314 (if (and from to)
|
|
1315 (progn
|
|
1316 (set-buffer (marker-buffer from))
|
|
1317 (setq from (1+ from)
|
|
1318 to (1- to))
|
|
1319 (while (and size
|
|
1320 (not (zerop size))
|
|
1321 (> to from)
|
|
1322 (eq (char-after (1- to)) ?\ ))
|
|
1323 (setq to (1- to)))
|
|
1324 (let ((result (buffer-substring-no-properties from to)))
|
|
1325 (when secret
|
|
1326 (let ((index 0))
|
|
1327 (while (< (+ from index) to)
|
|
1328 (aset result index
|
|
1329 (get-text-property (+ from index) 'secret))
|
|
1330 (setq index (1+ index)))))
|
|
1331 (set-buffer old)
|
|
1332 result))
|
|
1333 (widget-get widget :value))))
|
|
1334
|
|
1335 (defun widget-field-match (widget value)
|
|
1336 ;; Match any string.
|
|
1337 (stringp value))
|
|
1338
|
|
1339 ;;; The `text' Widget.
|
|
1340
|
|
1341 (define-widget 'text 'editable-field
|
|
1342 :keymap widget-text-keymap
|
|
1343 "A multiline text area.")
|
|
1344
|
|
1345 ;;; The `menu-choice' Widget.
|
|
1346
|
|
1347 (define-widget 'menu-choice 'default
|
|
1348 "A menu of options."
|
|
1349 :convert-widget 'widget-types-convert-widget
|
|
1350 :format "%[%t%]: %v"
|
|
1351 :case-fold t
|
|
1352 :tag "choice"
|
|
1353 :void '(item :format "invalid (%t)\n")
|
|
1354 :value-create 'widget-choice-value-create
|
|
1355 :value-delete 'widget-children-value-delete
|
|
1356 :value-get 'widget-choice-value-get
|
|
1357 :value-inline 'widget-choice-value-inline
|
|
1358 :action 'widget-choice-action
|
|
1359 :error "Make a choice"
|
|
1360 :validate 'widget-choice-validate
|
|
1361 :match 'widget-choice-match
|
|
1362 :match-inline 'widget-choice-match-inline)
|
|
1363
|
|
1364 (defun widget-choice-value-create (widget)
|
|
1365 ;; Insert the first choice that matches the value.
|
|
1366 (let ((value (widget-get widget :value))
|
|
1367 (args (widget-get widget :args))
|
|
1368 current)
|
|
1369 (while args
|
|
1370 (setq current (car args)
|
|
1371 args (cdr args))
|
|
1372 (when (widget-apply current :match value)
|
|
1373 (widget-put widget :children (list (widget-create-child-value
|
|
1374 widget current value)))
|
|
1375 (widget-put widget :choice current)
|
|
1376 (setq args nil
|
|
1377 current nil)))
|
|
1378 (when current
|
|
1379 (let ((void (widget-get widget :void)))
|
|
1380 (widget-put widget :children (list (widget-create-child-and-convert
|
|
1381 widget void :value value)))
|
|
1382 (widget-put widget :choice void)))))
|
|
1383
|
|
1384 (defun widget-choice-value-get (widget)
|
|
1385 ;; Get value of the child widget.
|
|
1386 (widget-value (car (widget-get widget :children))))
|
|
1387
|
|
1388 (defun widget-choice-value-inline (widget)
|
|
1389 ;; Get value of the child widget.
|
|
1390 (widget-apply (car (widget-get widget :children)) :value-inline))
|
|
1391
|
|
1392 (defun widget-choice-action (widget &optional event)
|
|
1393 ;; Make a choice.
|
|
1394 (let ((args (widget-get widget :args))
|
|
1395 (old (widget-get widget :choice))
|
|
1396 (tag (widget-apply widget :menu-tag-get))
|
|
1397 (completion-ignore-case (widget-get widget :case-fold))
|
|
1398 current choices)
|
|
1399 ;; Remember old value.
|
|
1400 (if (and old (not (widget-apply widget :validate)))
|
|
1401 (let* ((external (widget-value widget))
|
|
1402 (internal (widget-apply old :value-to-internal external)))
|
|
1403 (widget-put old :value internal)))
|
|
1404 ;; Find new choice.
|
|
1405 (setq current
|
|
1406 (cond ((= (length args) 0)
|
|
1407 nil)
|
|
1408 ((= (length args) 1)
|
|
1409 (nth 0 args))
|
|
1410 ((and (= (length args) 2)
|
|
1411 (memq old args))
|
|
1412 (if (eq old (nth 0 args))
|
|
1413 (nth 1 args)
|
|
1414 (nth 0 args)))
|
|
1415 (t
|
|
1416 (while args
|
|
1417 (setq current (car args)
|
|
1418 args (cdr args))
|
|
1419 (setq choices
|
|
1420 (cons (cons (widget-apply current :menu-tag-get)
|
|
1421 current)
|
|
1422 choices)))
|
|
1423 (widget-choose tag (reverse choices) event))))
|
|
1424 (when current
|
|
1425 (widget-value-set widget
|
|
1426 (widget-apply current :value-to-external
|
|
1427 (widget-get current :value)))
|
|
1428 (widget-apply widget :notify widget event)
|
|
1429 (widget-setup)))
|
|
1430 ;; Notify parent.
|
|
1431 (widget-apply widget :notify widget event)
|
|
1432 (widget-clear-undo))
|
|
1433
|
|
1434 (defun widget-choice-validate (widget)
|
|
1435 ;; Valid if we have made a valid choice.
|
|
1436 (let ((void (widget-get widget :void))
|
|
1437 (choice (widget-get widget :choice))
|
|
1438 (child (car (widget-get widget :children))))
|
|
1439 (if (eq void choice)
|
|
1440 widget
|
|
1441 (widget-apply child :validate))))
|
|
1442
|
|
1443 (defun widget-choice-match (widget value)
|
|
1444 ;; Matches if one of the choices matches.
|
|
1445 (let ((args (widget-get widget :args))
|
|
1446 current found)
|
|
1447 (while (and args (not found))
|
|
1448 (setq current (car args)
|
|
1449 args (cdr args)
|
|
1450 found (widget-apply current :match value)))
|
|
1451 found))
|
|
1452
|
|
1453 (defun widget-choice-match-inline (widget values)
|
|
1454 ;; Matches if one of the choices matches.
|
|
1455 (let ((args (widget-get widget :args))
|
|
1456 current found)
|
|
1457 (while (and args (null found))
|
|
1458 (setq current (car args)
|
|
1459 args (cdr args)
|
|
1460 found (widget-match-inline current values)))
|
|
1461 found))
|
|
1462
|
|
1463 ;;; The `toggle' Widget.
|
|
1464
|
|
1465 (define-widget 'toggle 'item
|
|
1466 "Toggle between two states."
|
|
1467 :format "%[%v%]\n"
|
|
1468 :value-create 'widget-toggle-value-create
|
|
1469 :action 'widget-toggle-action
|
|
1470 :match (lambda (widget value) t)
|
|
1471 :on "on"
|
|
1472 :off "off")
|
|
1473
|
|
1474 (defun widget-toggle-value-create (widget)
|
|
1475 ;; Insert text representing the `on' and `off' states.
|
|
1476 (if (widget-value widget)
|
|
1477 (widget-glyph-insert widget
|
|
1478 (widget-get widget :on)
|
|
1479 (widget-get widget :on-glyph))
|
|
1480 (widget-glyph-insert widget
|
|
1481 (widget-get widget :off)
|
|
1482 (widget-get widget :off-glyph))))
|
|
1483
|
|
1484 (defun widget-toggle-action (widget &optional event)
|
|
1485 ;; Toggle value.
|
|
1486 (widget-value-set widget (not (widget-value widget)))
|
|
1487 (widget-apply widget :notify widget event))
|
|
1488
|
|
1489 ;;; The `checkbox' Widget.
|
|
1490
|
|
1491 (define-widget 'checkbox 'toggle
|
|
1492 "A checkbox toggle."
|
|
1493 :format "%[%v%]"
|
|
1494 :on "[X]"
|
|
1495 :on-glyph "check1"
|
|
1496 :off "[ ]"
|
|
1497 :off-glyph "check0")
|
|
1498
|
|
1499 ;;; The `checklist' Widget.
|
|
1500
|
|
1501 (define-widget 'checklist 'default
|
|
1502 "A multiple choice widget."
|
|
1503 :convert-widget 'widget-types-convert-widget
|
|
1504 :format "%v"
|
|
1505 :offset 4
|
|
1506 :entry-format "%b %v"
|
|
1507 :menu-tag "checklist"
|
|
1508 :greedy nil
|
|
1509 :value-create 'widget-checklist-value-create
|
|
1510 :value-delete 'widget-children-value-delete
|
|
1511 :value-get 'widget-checklist-value-get
|
|
1512 :validate 'widget-checklist-validate
|
|
1513 :match 'widget-checklist-match
|
|
1514 :match-inline 'widget-checklist-match-inline)
|
|
1515
|
|
1516 (defun widget-checklist-value-create (widget)
|
|
1517 ;; Insert all values
|
|
1518 (let ((alist (widget-checklist-match-find widget (widget-get widget :value)))
|
|
1519 (args (widget-get widget :args)))
|
|
1520 (while args
|
|
1521 (widget-checklist-add-item widget (car args) (assq (car args) alist))
|
|
1522 (setq args (cdr args)))
|
|
1523 (widget-put widget :children (nreverse (widget-get widget :children)))))
|
|
1524
|
|
1525 (defun widget-checklist-add-item (widget type chosen)
|
|
1526 ;; Create checklist item in WIDGET of type TYPE.
|
|
1527 ;; If the item is checked, CHOSEN is a cons whose cdr is the value.
|
|
1528 (and (eq (preceding-char) ?\n)
|
|
1529 (widget-get widget :indent)
|
|
1530 (insert-char ? (widget-get widget :indent)))
|
|
1531 (widget-specify-insert
|
|
1532 (let* ((children (widget-get widget :children))
|
|
1533 (buttons (widget-get widget :buttons))
|
30
|
1534 (button-args (or (widget-get type :sibling-args)
|
|
1535 (widget-get widget :button-args)))
|
28
|
1536 (from (point))
|
|
1537 child button)
|
|
1538 (insert (widget-get widget :entry-format))
|
|
1539 (goto-char from)
|
|
1540 ;; Parse % escapes in format.
|
|
1541 (while (re-search-forward "%\\([bv%]\\)" nil t)
|
|
1542 (let ((escape (aref (match-string 1) 0)))
|
|
1543 (replace-match "" t t)
|
|
1544 (cond ((eq escape ?%)
|
|
1545 (insert "%"))
|
|
1546 ((eq escape ?b)
|
30
|
1547 (setq button (apply 'widget-create-child-and-convert
|
|
1548 widget 'checkbox
|
|
1549 :value (not (null chosen))
|
|
1550 button-args)))
|
28
|
1551 ((eq escape ?v)
|
|
1552 (setq child
|
|
1553 (cond ((not chosen)
|
|
1554 (widget-create-child widget type))
|
|
1555 ((widget-get type :inline)
|
|
1556 (widget-create-child-value
|
|
1557 widget type (cdr chosen)))
|
|
1558 (t
|
|
1559 (widget-create-child-value
|
|
1560 widget type (car (cdr chosen)))))))
|
|
1561 (t
|
|
1562 (error "Unknown escape `%c'" escape)))))
|
|
1563 ;; Update properties.
|
|
1564 (and button child (widget-put child :button button))
|
|
1565 (and button (widget-put widget :buttons (cons button buttons)))
|
|
1566 (and child (widget-put widget :children (cons child children))))))
|
|
1567
|
|
1568 (defun widget-checklist-match (widget values)
|
|
1569 ;; All values must match a type in the checklist.
|
|
1570 (and (listp values)
|
|
1571 (null (cdr (widget-checklist-match-inline widget values)))))
|
|
1572
|
|
1573 (defun widget-checklist-match-inline (widget values)
|
|
1574 ;; Find the values which match a type in the checklist.
|
|
1575 (let ((greedy (widget-get widget :greedy))
|
|
1576 (args (copy-list (widget-get widget :args)))
|
|
1577 found rest)
|
|
1578 (while values
|
|
1579 (let ((answer (widget-checklist-match-up args values)))
|
|
1580 (cond (answer
|
|
1581 (let ((vals (widget-match-inline answer values)))
|
|
1582 (setq found (append found (car vals))
|
|
1583 values (cdr vals)
|
|
1584 args (delq answer args))))
|
|
1585 (greedy
|
|
1586 (setq rest (append rest (list (car values)))
|
|
1587 values (cdr values)))
|
|
1588 (t
|
|
1589 (setq rest (append rest values)
|
|
1590 values nil)))))
|
|
1591 (cons found rest)))
|
|
1592
|
|
1593 (defun widget-checklist-match-find (widget vals)
|
|
1594 ;; Find the vals which match a type in the checklist.
|
|
1595 ;; Return an alist of (TYPE MATCH).
|
|
1596 (let ((greedy (widget-get widget :greedy))
|
|
1597 (args (copy-list (widget-get widget :args)))
|
|
1598 found)
|
|
1599 (while vals
|
|
1600 (let ((answer (widget-checklist-match-up args vals)))
|
|
1601 (cond (answer
|
|
1602 (let ((match (widget-match-inline answer vals)))
|
|
1603 (setq found (cons (cons answer (car match)) found)
|
|
1604 vals (cdr match)
|
|
1605 args (delq answer args))))
|
|
1606 (greedy
|
|
1607 (setq vals (cdr vals)))
|
|
1608 (t
|
|
1609 (setq vals nil)))))
|
|
1610 found))
|
|
1611
|
|
1612 (defun widget-checklist-match-up (args vals)
|
|
1613 ;; Rerturn the first type from ARGS that matches VALS.
|
|
1614 (let (current found)
|
|
1615 (while (and args (null found))
|
|
1616 (setq current (car args)
|
|
1617 args (cdr args)
|
|
1618 found (widget-match-inline current vals)))
|
|
1619 (if found
|
|
1620 current
|
|
1621 nil)))
|
|
1622
|
|
1623 (defun widget-checklist-value-get (widget)
|
|
1624 ;; The values of all selected items.
|
|
1625 (let ((children (widget-get widget :children))
|
|
1626 child result)
|
|
1627 (while children
|
|
1628 (setq child (car children)
|
|
1629 children (cdr children))
|
|
1630 (if (widget-value (widget-get child :button))
|
|
1631 (setq result (append result (widget-apply child :value-inline)))))
|
|
1632 result))
|
|
1633
|
|
1634 (defun widget-checklist-validate (widget)
|
|
1635 ;; Ticked chilren must be valid.
|
|
1636 (let ((children (widget-get widget :children))
|
|
1637 child button found)
|
|
1638 (while (and children (not found))
|
|
1639 (setq child (car children)
|
|
1640 children (cdr children)
|
|
1641 button (widget-get child :button)
|
|
1642 found (and (widget-value button)
|
|
1643 (widget-apply child :validate))))
|
|
1644 found))
|
|
1645
|
|
1646 ;;; The `option' Widget
|
|
1647
|
|
1648 (define-widget 'option 'checklist
|
|
1649 "An widget with an optional item."
|
|
1650 :inline t)
|
|
1651
|
|
1652 ;;; The `choice-item' Widget.
|
|
1653
|
|
1654 (define-widget 'choice-item 'item
|
|
1655 "Button items that delegate action events to their parents."
|
|
1656 :action 'widget-choice-item-action
|
|
1657 :format "%[%t%] \n")
|
|
1658
|
|
1659 (defun widget-choice-item-action (widget &optional event)
|
|
1660 ;; Tell parent what happened.
|
|
1661 (widget-apply (widget-get widget :parent) :action event))
|
|
1662
|
|
1663 ;;; The `radio-button' Widget.
|
|
1664
|
|
1665 (define-widget 'radio-button 'toggle
|
|
1666 "A radio button for use in the `radio' widget."
|
|
1667 :notify 'widget-radio-button-notify
|
|
1668 :format "%[%v%]"
|
|
1669 :on "(*)"
|
|
1670 :on-glyph "radio1"
|
|
1671 :off "( )"
|
|
1672 :off-glyph "radio0")
|
|
1673
|
|
1674 (defun widget-radio-button-notify (widget child &optional event)
|
|
1675 ;; Tell daddy.
|
|
1676 (widget-apply (widget-get widget :parent) :action widget event))
|
|
1677
|
|
1678 ;;; The `radio-button-choice' Widget.
|
|
1679
|
|
1680 (define-widget 'radio-button-choice 'default
|
|
1681 "Select one of multiple options."
|
|
1682 :convert-widget 'widget-types-convert-widget
|
|
1683 :offset 4
|
|
1684 :format "%v"
|
|
1685 :entry-format "%b %v"
|
|
1686 :menu-tag "radio"
|
|
1687 :value-create 'widget-radio-value-create
|
|
1688 :value-delete 'widget-children-value-delete
|
|
1689 :value-get 'widget-radio-value-get
|
|
1690 :value-inline 'widget-radio-value-inline
|
|
1691 :value-set 'widget-radio-value-set
|
|
1692 :error "You must push one of the buttons"
|
|
1693 :validate 'widget-radio-validate
|
|
1694 :match 'widget-choice-match
|
|
1695 :match-inline 'widget-choice-match-inline
|
|
1696 :action 'widget-radio-action)
|
|
1697
|
|
1698 (defun widget-radio-value-create (widget)
|
|
1699 ;; Insert all values
|
|
1700 (let ((args (widget-get widget :args))
|
|
1701 arg)
|
|
1702 (while args
|
|
1703 (setq arg (car args)
|
|
1704 args (cdr args))
|
|
1705 (widget-radio-add-item widget arg))))
|
|
1706
|
|
1707 (defun widget-radio-add-item (widget type)
|
|
1708 "Add to radio widget WIDGET a new radio button item of type TYPE."
|
|
1709 ;; (setq type (widget-convert type))
|
|
1710 (and (eq (preceding-char) ?\n)
|
|
1711 (widget-get widget :indent)
|
|
1712 (insert-char ? (widget-get widget :indent)))
|
|
1713 (widget-specify-insert
|
|
1714 (let* ((value (widget-get widget :value))
|
|
1715 (children (widget-get widget :children))
|
|
1716 (buttons (widget-get widget :buttons))
|
30
|
1717 (button-args (or (widget-get type :sibling-args)
|
|
1718 (widget-get widget :button-args)))
|
28
|
1719 (from (point))
|
|
1720 (chosen (and (null (widget-get widget :choice))
|
|
1721 (widget-apply type :match value)))
|
|
1722 child button)
|
|
1723 (insert (widget-get widget :entry-format))
|
|
1724 (goto-char from)
|
|
1725 ;; Parse % escapes in format.
|
|
1726 (while (re-search-forward "%\\([bv%]\\)" nil t)
|
|
1727 (let ((escape (aref (match-string 1) 0)))
|
|
1728 (replace-match "" t t)
|
|
1729 (cond ((eq escape ?%)
|
|
1730 (insert "%"))
|
|
1731 ((eq escape ?b)
|
30
|
1732 (setq button (apply 'widget-create-child-and-convert
|
|
1733 widget 'radio-button
|
|
1734 :value (not (null chosen))
|
|
1735 button-args)))
|
28
|
1736 ((eq escape ?v)
|
|
1737 (setq child (if chosen
|
|
1738 (widget-create-child-value
|
|
1739 widget type value)
|
|
1740 (widget-create-child widget type))))
|
|
1741 (t
|
|
1742 (error "Unknown escape `%c'" escape)))))
|
|
1743 ;; Update properties.
|
|
1744 (when chosen
|
|
1745 (widget-put widget :choice type))
|
|
1746 (when button
|
|
1747 (widget-put child :button button)
|
|
1748 (widget-put widget :buttons (nconc buttons (list button))))
|
|
1749 (when child
|
|
1750 (widget-put widget :children (nconc children (list child))))
|
|
1751 child)))
|
|
1752
|
|
1753 (defun widget-radio-value-get (widget)
|
|
1754 ;; Get value of the child widget.
|
|
1755 (let ((chosen (widget-radio-chosen widget)))
|
|
1756 (and chosen (widget-value chosen))))
|
|
1757
|
|
1758 (defun widget-radio-chosen (widget)
|
|
1759 "Return the widget representing the chosen radio button."
|
|
1760 (let ((children (widget-get widget :children))
|
|
1761 current found)
|
|
1762 (while children
|
|
1763 (setq current (car children)
|
|
1764 children (cdr children))
|
|
1765 (let* ((button (widget-get current :button))
|
|
1766 (value (widget-apply button :value-get)))
|
|
1767 (when value
|
|
1768 (setq found current
|
|
1769 children nil))))
|
|
1770 found))
|
|
1771
|
|
1772 (defun widget-radio-value-inline (widget)
|
|
1773 ;; Get value of the child widget.
|
|
1774 (let ((children (widget-get widget :children))
|
|
1775 current found)
|
|
1776 (while children
|
|
1777 (setq current (car children)
|
|
1778 children (cdr children))
|
|
1779 (let* ((button (widget-get current :button))
|
|
1780 (value (widget-apply button :value-get)))
|
|
1781 (when value
|
|
1782 (setq found (widget-apply current :value-inline)
|
|
1783 children nil))))
|
|
1784 found))
|
|
1785
|
|
1786 (defun widget-radio-value-set (widget value)
|
|
1787 ;; We can't just delete and recreate a radio widget, since children
|
|
1788 ;; can be added after the original creation and won't be recreated
|
|
1789 ;; by `:create'.
|
|
1790 (let ((children (widget-get widget :children))
|
|
1791 current found)
|
|
1792 (while children
|
|
1793 (setq current (car children)
|
|
1794 children (cdr children))
|
|
1795 (let* ((button (widget-get current :button))
|
|
1796 (match (and (not found)
|
|
1797 (widget-apply current :match value))))
|
|
1798 (widget-value-set button match)
|
|
1799 (if match
|
|
1800 (widget-value-set current value))
|
|
1801 (setq found (or found match))))))
|
|
1802
|
|
1803 (defun widget-radio-validate (widget)
|
|
1804 ;; Valid if we have made a valid choice.
|
|
1805 (let ((children (widget-get widget :children))
|
|
1806 current found button)
|
|
1807 (while (and children (not found))
|
|
1808 (setq current (car children)
|
|
1809 children (cdr children)
|
|
1810 button (widget-get current :button)
|
|
1811 found (widget-apply button :value-get)))
|
|
1812 (if found
|
|
1813 (widget-apply current :validate)
|
|
1814 widget)))
|
|
1815
|
|
1816 (defun widget-radio-action (widget child event)
|
|
1817 ;; Check if a radio button was pressed.
|
|
1818 (let ((children (widget-get widget :children))
|
|
1819 (buttons (widget-get widget :buttons))
|
|
1820 current)
|
|
1821 (when (memq child buttons)
|
|
1822 (while children
|
|
1823 (setq current (car children)
|
|
1824 children (cdr children))
|
|
1825 (let* ((button (widget-get current :button)))
|
|
1826 (cond ((eq child button)
|
|
1827 (widget-value-set button t))
|
|
1828 ((widget-value button)
|
|
1829 (widget-value-set button nil)))))))
|
|
1830 ;; Pass notification to parent.
|
|
1831 (widget-apply widget :notify child event))
|
|
1832
|
|
1833 ;;; The `insert-button' Widget.
|
|
1834
|
|
1835 (define-widget 'insert-button 'push-button
|
|
1836 "An insert button for the `editable-list' widget."
|
|
1837 :tag "INS"
|
30
|
1838 :help-echo "Insert a new item into the list at this position."
|
28
|
1839 :action 'widget-insert-button-action)
|
|
1840
|
|
1841 (defun widget-insert-button-action (widget &optional event)
|
|
1842 ;; Ask the parent to insert a new item.
|
|
1843 (widget-apply (widget-get widget :parent)
|
|
1844 :insert-before (widget-get widget :widget)))
|
|
1845
|
|
1846 ;;; The `delete-button' Widget.
|
|
1847
|
|
1848 (define-widget 'delete-button 'push-button
|
|
1849 "A delete button for the `editable-list' widget."
|
|
1850 :tag "DEL"
|
30
|
1851 :help-echo "Delete this item from the list."
|
28
|
1852 :action 'widget-delete-button-action)
|
|
1853
|
|
1854 (defun widget-delete-button-action (widget &optional event)
|
|
1855 ;; Ask the parent to insert a new item.
|
|
1856 (widget-apply (widget-get widget :parent)
|
|
1857 :delete-at (widget-get widget :widget)))
|
|
1858
|
|
1859 ;;; The `editable-list' Widget.
|
|
1860
|
|
1861 (defcustom widget-editable-list-gui nil
|
|
1862 "If non nil, use GUI push-buttons in editable list when available."
|
|
1863 :type 'boolean
|
|
1864 :group 'widgets)
|
|
1865
|
|
1866 (define-widget 'editable-list 'default
|
|
1867 "A variable list of widgets of the same type."
|
|
1868 :convert-widget 'widget-types-convert-widget
|
|
1869 :offset 12
|
|
1870 :format "%v%i\n"
|
|
1871 :format-handler 'widget-editable-list-format-handler
|
|
1872 :entry-format "%i %d %v"
|
|
1873 :menu-tag "editable-list"
|
|
1874 :value-create 'widget-editable-list-value-create
|
|
1875 :value-delete 'widget-children-value-delete
|
|
1876 :value-get 'widget-editable-list-value-get
|
|
1877 :validate 'widget-editable-list-validate
|
|
1878 :match 'widget-editable-list-match
|
|
1879 :match-inline 'widget-editable-list-match-inline
|
|
1880 :insert-before 'widget-editable-list-insert-before
|
|
1881 :delete-at 'widget-editable-list-delete-at)
|
|
1882
|
|
1883 (defun widget-editable-list-format-handler (widget escape)
|
|
1884 ;; We recognize the insert button.
|
|
1885 (let ((widget-push-button-gui widget-editable-list-gui))
|
|
1886 (cond ((eq escape ?i)
|
|
1887 (and (widget-get widget :indent)
|
|
1888 (insert-char ? (widget-get widget :indent)))
|
30
|
1889 (apply 'widget-create-child-and-convert
|
|
1890 widget 'insert-button
|
|
1891 (widget-get widget :append-button-args)))
|
28
|
1892 (t
|
|
1893 (widget-default-format-handler widget escape)))))
|
|
1894
|
|
1895 (defun widget-editable-list-value-create (widget)
|
|
1896 ;; Insert all values
|
|
1897 (let* ((value (widget-get widget :value))
|
|
1898 (type (nth 0 (widget-get widget :args)))
|
|
1899 (inlinep (widget-get type :inline))
|
|
1900 children)
|
|
1901 (widget-put widget :value-pos (copy-marker (point)))
|
|
1902 (set-marker-insertion-type (widget-get widget :value-pos) t)
|
|
1903 (while value
|
|
1904 (let ((answer (widget-match-inline type value)))
|
|
1905 (if answer
|
|
1906 (setq children (cons (widget-editable-list-entry-create
|
|
1907 widget
|
|
1908 (if inlinep
|
|
1909 (car answer)
|
|
1910 (car (car answer)))
|
|
1911 t)
|
|
1912 children)
|
|
1913 value (cdr answer))
|
|
1914 (setq value nil))))
|
|
1915 (widget-put widget :children (nreverse children))))
|
|
1916
|
|
1917 (defun widget-editable-list-value-get (widget)
|
|
1918 ;; Get value of the child widget.
|
|
1919 (apply 'append (mapcar (lambda (child) (widget-apply child :value-inline))
|
|
1920 (widget-get widget :children))))
|
|
1921
|
|
1922 (defun widget-editable-list-validate (widget)
|
|
1923 ;; All the chilren must be valid.
|
|
1924 (let ((children (widget-get widget :children))
|
|
1925 child found)
|
|
1926 (while (and children (not found))
|
|
1927 (setq child (car children)
|
|
1928 children (cdr children)
|
|
1929 found (widget-apply child :validate)))
|
|
1930 found))
|
|
1931
|
|
1932 (defun widget-editable-list-match (widget value)
|
|
1933 ;; Value must be a list and all the members must match the type.
|
|
1934 (and (listp value)
|
|
1935 (null (cdr (widget-editable-list-match-inline widget value)))))
|
|
1936
|
|
1937 (defun widget-editable-list-match-inline (widget value)
|
|
1938 (let ((type (nth 0 (widget-get widget :args)))
|
|
1939 (ok t)
|
|
1940 found)
|
|
1941 (while (and value ok)
|
|
1942 (let ((answer (widget-match-inline type value)))
|
|
1943 (if answer
|
|
1944 (setq found (append found (car answer))
|
|
1945 value (cdr answer))
|
|
1946 (setq ok nil))))
|
|
1947 (cons found value)))
|
|
1948
|
|
1949 (defun widget-editable-list-insert-before (widget before)
|
|
1950 ;; Insert a new child in the list of children.
|
|
1951 (save-excursion
|
|
1952 (let ((children (widget-get widget :children))
|
|
1953 (inhibit-read-only t)
|
|
1954 after-change-functions)
|
|
1955 (cond (before
|
|
1956 (goto-char (widget-get before :entry-from)))
|
|
1957 (t
|
|
1958 (goto-char (widget-get widget :value-pos))))
|
|
1959 (let ((child (widget-editable-list-entry-create
|
|
1960 widget nil nil)))
|
|
1961 (when (< (widget-get child :entry-from) (widget-get widget :from))
|
|
1962 (set-marker (widget-get widget :from)
|
|
1963 (widget-get child :entry-from)))
|
|
1964 (widget-specify-text (widget-get child :entry-from)
|
|
1965 (widget-get child :entry-to))
|
|
1966 (if (eq (car children) before)
|
|
1967 (widget-put widget :children (cons child children))
|
|
1968 (while (not (eq (car (cdr children)) before))
|
|
1969 (setq children (cdr children)))
|
|
1970 (setcdr children (cons child (cdr children)))))))
|
|
1971 (widget-setup)
|
44
|
1972 widget (widget-apply widget :notify widget))
|
28
|
1973
|
|
1974 (defun widget-editable-list-delete-at (widget child)
|
|
1975 ;; Delete child from list of children.
|
|
1976 (save-excursion
|
|
1977 (let ((buttons (copy-list (widget-get widget :buttons)))
|
|
1978 button
|
|
1979 (inhibit-read-only t)
|
|
1980 after-change-functions)
|
|
1981 (while buttons
|
|
1982 (setq button (car buttons)
|
|
1983 buttons (cdr buttons))
|
|
1984 (when (eq (widget-get button :widget) child)
|
|
1985 (widget-put widget
|
|
1986 :buttons (delq button (widget-get widget :buttons)))
|
|
1987 (widget-delete button))))
|
|
1988 (let ((entry-from (widget-get child :entry-from))
|
|
1989 (entry-to (widget-get child :entry-to))
|
|
1990 (inhibit-read-only t)
|
|
1991 after-change-functions)
|
|
1992 (widget-delete child)
|
|
1993 (delete-region entry-from entry-to)
|
|
1994 (set-marker entry-from nil)
|
|
1995 (set-marker entry-to nil))
|
|
1996 (widget-put widget :children (delq child (widget-get widget :children))))
|
|
1997 (widget-setup)
|
|
1998 (widget-apply widget :notify widget))
|
|
1999
|
|
2000 (defun widget-editable-list-entry-create (widget value conv)
|
|
2001 ;; Create a new entry to the list.
|
|
2002 (let ((type (nth 0 (widget-get widget :args)))
|
|
2003 (widget-push-button-gui widget-editable-list-gui)
|
|
2004 child delete insert)
|
|
2005 (widget-specify-insert
|
|
2006 (save-excursion
|
|
2007 (and (widget-get widget :indent)
|
|
2008 (insert-char ? (widget-get widget :indent)))
|
|
2009 (insert (widget-get widget :entry-format)))
|
|
2010 ;; Parse % escapes in format.
|
|
2011 (while (re-search-forward "%\\(.\\)" nil t)
|
|
2012 (let ((escape (aref (match-string 1) 0)))
|
|
2013 (replace-match "" t t)
|
|
2014 (cond ((eq escape ?%)
|
|
2015 (insert "%"))
|
|
2016 ((eq escape ?i)
|
30
|
2017 (setq insert (apply 'widget-create-child-and-convert
|
|
2018 widget 'insert-button
|
|
2019 (widget-get widget :insert-button-args))))
|
28
|
2020 ((eq escape ?d)
|
30
|
2021 (setq delete (apply 'widget-create-child-and-convert
|
|
2022 widget 'delete-button
|
|
2023 (widget-get widget :delete-button-args))))
|
28
|
2024 ((eq escape ?v)
|
|
2025 (if conv
|
|
2026 (setq child (widget-create-child-value
|
|
2027 widget type value))
|
|
2028 (setq child (widget-create-child widget type))))
|
|
2029 (t
|
|
2030 (error "Unknown escape `%c'" escape)))))
|
|
2031 (widget-put widget
|
|
2032 :buttons (cons delete
|
|
2033 (cons insert
|
|
2034 (widget-get widget :buttons))))
|
|
2035 (let ((entry-from (copy-marker (point-min)))
|
|
2036 (entry-to (copy-marker (point-max))))
|
|
2037 (widget-specify-text entry-from entry-to)
|
|
2038 (set-marker-insertion-type entry-from t)
|
|
2039 (set-marker-insertion-type entry-to nil)
|
|
2040 (widget-put child :entry-from entry-from)
|
|
2041 (widget-put child :entry-to entry-to)))
|
|
2042 (widget-put insert :widget child)
|
|
2043 (widget-put delete :widget child)
|
|
2044 child))
|
|
2045
|
|
2046 ;;; The `group' Widget.
|
|
2047
|
|
2048 (define-widget 'group 'default
|
|
2049 "A widget which group other widgets inside."
|
|
2050 :convert-widget 'widget-types-convert-widget
|
|
2051 :format "%v"
|
|
2052 :value-create 'widget-group-value-create
|
|
2053 :value-delete 'widget-children-value-delete
|
|
2054 :value-get 'widget-editable-list-value-get
|
|
2055 :validate 'widget-editable-list-validate
|
|
2056 :match 'widget-group-match
|
|
2057 :match-inline 'widget-group-match-inline)
|
|
2058
|
|
2059 (defun widget-group-value-create (widget)
|
|
2060 ;; Create each component.
|
|
2061 (let ((args (widget-get widget :args))
|
|
2062 (value (widget-get widget :value))
|
|
2063 arg answer children)
|
|
2064 (while args
|
|
2065 (setq arg (car args)
|
|
2066 args (cdr args)
|
|
2067 answer (widget-match-inline arg value)
|
|
2068 value (cdr answer))
|
|
2069 (and (eq (preceding-char) ?\n)
|
|
2070 (widget-get widget :indent)
|
|
2071 (insert-char ? (widget-get widget :indent)))
|
|
2072 (push (cond ((null answer)
|
|
2073 (widget-create-child widget arg))
|
|
2074 ((widget-get arg :inline)
|
|
2075 (widget-create-child-value widget arg (car answer)))
|
|
2076 (t
|
|
2077 (widget-create-child-value widget arg (car (car answer)))))
|
|
2078 children))
|
|
2079 (widget-put widget :children (nreverse children))))
|
|
2080
|
|
2081 (defun widget-group-match (widget values)
|
|
2082 ;; Match if the components match.
|
|
2083 (and (listp values)
|
|
2084 (let ((match (widget-group-match-inline widget values)))
|
|
2085 (and match (null (cdr match))))))
|
|
2086
|
|
2087 (defun widget-group-match-inline (widget vals)
|
|
2088 ;; Match if the components match.
|
|
2089 (let ((args (widget-get widget :args))
|
|
2090 argument answer found)
|
|
2091 (while args
|
|
2092 (setq argument (car args)
|
|
2093 args (cdr args)
|
|
2094 answer (widget-match-inline argument vals))
|
|
2095 (if answer
|
|
2096 (setq vals (cdr answer)
|
|
2097 found (append found (car answer)))
|
|
2098 (setq vals nil
|
|
2099 args nil)))
|
|
2100 (if answer
|
|
2101 (cons found vals)
|
|
2102 nil)))
|
|
2103
|
|
2104 ;;; The `widget-help' Widget.
|
|
2105
|
|
2106 (define-widget 'widget-help 'push-button
|
|
2107 "The widget documentation button."
|
|
2108 :format "%[[%t]%] %d"
|
30
|
2109 :help-echo "Toggle display of documentation."
|
28
|
2110 :action 'widget-help-action)
|
|
2111
|
|
2112 (defun widget-help-action (widget &optional event)
|
|
2113 "Toggle documentation for WIDGET."
|
|
2114 (let ((old (widget-get widget :doc))
|
|
2115 (new (widget-get widget :widget-doc)))
|
|
2116 (widget-put widget :doc new)
|
|
2117 (widget-put widget :widget-doc old))
|
|
2118 (widget-value-set widget (widget-value widget)))
|
|
2119
|
|
2120 ;;; The Sexp Widgets.
|
|
2121
|
|
2122 (define-widget 'const 'item
|
|
2123 "An immutable sexp."
|
|
2124 :format "%t\n%d")
|
|
2125
|
|
2126 (define-widget 'function-item 'item
|
|
2127 "An immutable function name."
|
|
2128 :format "%v\n%h"
|
|
2129 :documentation-property (lambda (symbol)
|
|
2130 (condition-case nil
|
|
2131 (documentation symbol t)
|
|
2132 (error nil))))
|
|
2133
|
|
2134 (define-widget 'variable-item 'item
|
|
2135 "An immutable variable name."
|
|
2136 :format "%v\n%h"
|
|
2137 :documentation-property 'variable-documentation)
|
|
2138
|
|
2139 (define-widget 'string 'editable-field
|
|
2140 "A string"
|
|
2141 :tag "String"
|
|
2142 :format "%[%t%]: %v")
|
|
2143
|
|
2144 (define-widget 'regexp 'string
|
|
2145 "A regular expression."
|
|
2146 ;; Should do validation.
|
|
2147 :tag "Regexp")
|
|
2148
|
|
2149 (define-widget 'file 'string
|
|
2150 "A file widget.
|
|
2151 It will read a file name from the minibuffer when activated."
|
|
2152 :format "%[%t%]: %v"
|
|
2153 :tag "File"
|
|
2154 :action 'widget-file-action)
|
|
2155
|
|
2156 (defun widget-file-action (widget &optional event)
|
|
2157 ;; Read a file name from the minibuffer.
|
|
2158 (let* ((value (widget-value widget))
|
|
2159 (dir (file-name-directory value))
|
|
2160 (file (file-name-nondirectory value))
|
|
2161 (menu-tag (widget-apply widget :menu-tag-get))
|
|
2162 (must-match (widget-get widget :must-match))
|
36
|
2163 (answer (read-file-name (concat menu-tag ": (default `" value "') ")
|
28
|
2164 dir nil must-match file)))
|
|
2165 (widget-value-set widget (abbreviate-file-name answer))
|
|
2166 (widget-apply widget :notify widget event)
|
|
2167 (widget-setup)))
|
|
2168
|
|
2169 (define-widget 'directory 'file
|
|
2170 "A directory widget.
|
|
2171 It will read a directory name from the minibuffer when activated."
|
|
2172 :tag "Directory")
|
|
2173
|
|
2174 (define-widget 'symbol 'string
|
|
2175 "A lisp symbol."
|
|
2176 :value nil
|
|
2177 :tag "Symbol"
|
|
2178 :match (lambda (widget value) (symbolp value))
|
|
2179 :value-to-internal (lambda (widget value)
|
|
2180 (if (symbolp value)
|
|
2181 (symbol-name value)
|
|
2182 value))
|
|
2183 :value-to-external (lambda (widget value)
|
|
2184 (if (stringp value)
|
|
2185 (intern value)
|
|
2186 value)))
|
|
2187
|
|
2188 (define-widget 'function 'sexp
|
|
2189 ;; Should complete on functions.
|
|
2190 "A lisp function."
|
|
2191 :tag "Function")
|
|
2192
|
|
2193 (define-widget 'variable 'symbol
|
|
2194 ;; Should complete on variables.
|
|
2195 "A lisp variable."
|
|
2196 :tag "Variable")
|
|
2197
|
|
2198 (define-widget 'sexp 'string
|
|
2199 "An arbitrary lisp expression."
|
|
2200 :tag "Lisp expression"
|
|
2201 :value nil
|
|
2202 :validate 'widget-sexp-validate
|
|
2203 :match (lambda (widget value) t)
|
|
2204 :value-to-internal 'widget-sexp-value-to-internal
|
|
2205 :value-to-external (lambda (widget value) (read value)))
|
|
2206
|
|
2207 (defun widget-sexp-value-to-internal (widget value)
|
|
2208 ;; Use pp for printer representation.
|
|
2209 (let ((pp (pp-to-string value)))
|
|
2210 (while (string-match "\n\\'" pp)
|
|
2211 (setq pp (substring pp 0 -1)))
|
|
2212 (if (or (string-match "\n\\'" pp)
|
|
2213 (> (length pp) 40))
|
|
2214 (concat "\n" pp)
|
|
2215 pp)))
|
|
2216
|
|
2217 (defun widget-sexp-validate (widget)
|
|
2218 ;; Valid if we can read the string and there is no junk left after it.
|
|
2219 (save-excursion
|
|
2220 (let ((buffer (set-buffer (get-buffer-create " *Widget Scratch*"))))
|
|
2221 (erase-buffer)
|
|
2222 (insert (widget-apply widget :value-get))
|
|
2223 (goto-char (point-min))
|
|
2224 (condition-case data
|
|
2225 (let ((value (read buffer)))
|
|
2226 (if (eobp)
|
|
2227 (if (widget-apply widget :match value)
|
|
2228 nil
|
|
2229 (widget-put widget :error (widget-get widget :type-error))
|
|
2230 widget)
|
|
2231 (widget-put widget
|
|
2232 :error (format "Junk at end of expression: %s"
|
|
2233 (buffer-substring (point)
|
|
2234 (point-max))))
|
|
2235 widget))
|
|
2236 (error (widget-put widget :error (error-message-string data))
|
|
2237 widget)))))
|
|
2238
|
|
2239 (define-widget 'integer 'sexp
|
|
2240 "An integer."
|
|
2241 :tag "Integer"
|
|
2242 :value 0
|
|
2243 :type-error "This field should contain an integer"
|
|
2244 :value-to-internal (lambda (widget value)
|
|
2245 (if (integerp value)
|
|
2246 (prin1-to-string value)
|
|
2247 value))
|
|
2248 :match (lambda (widget value) (integerp value)))
|
|
2249
|
|
2250 (define-widget 'character 'string
|
|
2251 "An character."
|
|
2252 :tag "Character"
|
|
2253 :value 0
|
|
2254 :size 1
|
|
2255 :format "%{%t%}: %v\n"
|
|
2256 :type-error "This field should contain a character"
|
|
2257 :value-to-internal (lambda (widget value)
|
|
2258 (if (integerp value)
|
|
2259 (char-to-string value)
|
|
2260 value))
|
|
2261 :value-to-external (lambda (widget value)
|
|
2262 (if (stringp value)
|
|
2263 (aref value 0)
|
|
2264 value))
|
|
2265 :match (lambda (widget value) (integerp value)))
|
|
2266
|
|
2267 (define-widget 'number 'sexp
|
|
2268 "A floating point number."
|
|
2269 :tag "Number"
|
|
2270 :value 0.0
|
|
2271 :type-error "This field should contain a number"
|
|
2272 :value-to-internal (lambda (widget value)
|
|
2273 (if (numberp value)
|
|
2274 (prin1-to-string value)
|
|
2275 value))
|
|
2276 :match (lambda (widget value) (numberp value)))
|
|
2277
|
|
2278 (define-widget 'list 'group
|
|
2279 "A lisp list."
|
|
2280 :tag "List"
|
|
2281 :format "%{%t%}:\n%v")
|
|
2282
|
|
2283 (define-widget 'vector 'group
|
|
2284 "A lisp vector."
|
|
2285 :tag "Vector"
|
|
2286 :format "%{%t%}:\n%v"
|
|
2287 :match 'widget-vector-match
|
|
2288 :value-to-internal (lambda (widget value) (append value nil))
|
|
2289 :value-to-external (lambda (widget value) (apply 'vector value)))
|
|
2290
|
|
2291 (defun widget-vector-match (widget value)
|
|
2292 (and (vectorp value)
|
|
2293 (widget-group-match widget
|
|
2294 (widget-apply :value-to-internal widget value))))
|
|
2295
|
|
2296 (define-widget 'cons 'group
|
|
2297 "A cons-cell."
|
|
2298 :tag "Cons-cell"
|
|
2299 :format "%{%t%}:\n%v"
|
|
2300 :match 'widget-cons-match
|
|
2301 :value-to-internal (lambda (widget value)
|
|
2302 (list (car value) (cdr value)))
|
|
2303 :value-to-external (lambda (widget value)
|
|
2304 (cons (nth 0 value) (nth 1 value))))
|
|
2305
|
|
2306 (defun widget-cons-match (widget value)
|
|
2307 (and (consp value)
|
|
2308 (widget-group-match widget
|
|
2309 (widget-apply widget :value-to-internal value))))
|
|
2310
|
|
2311 (define-widget 'choice 'menu-choice
|
|
2312 "A union of several sexp types."
|
|
2313 :tag "Choice"
|
|
2314 :format "%[%t%]: %v")
|
|
2315
|
|
2316 (define-widget 'radio 'radio-button-choice
|
|
2317 "A union of several sexp types."
|
|
2318 :tag "Choice"
|
|
2319 :format "%{%t%}:\n%v")
|
|
2320
|
|
2321 (define-widget 'repeat 'editable-list
|
|
2322 "A variable length homogeneous list."
|
|
2323 :tag "Repeat"
|
|
2324 :format "%{%t%}:\n%v%i\n")
|
|
2325
|
|
2326 (define-widget 'set 'checklist
|
|
2327 "A list of members from a fixed set."
|
|
2328 :tag "Set"
|
|
2329 :format "%{%t%}:\n%v")
|
|
2330
|
|
2331 (define-widget 'boolean 'toggle
|
|
2332 "To be nil or non-nil, that is the question."
|
|
2333 :tag "Boolean"
|
|
2334 :format "%{%t%}: %[%v%]\n")
|
|
2335
|
|
2336 ;;; The `color' Widget.
|
|
2337
|
|
2338 (define-widget 'color-item 'choice-item
|
|
2339 "A color name (with sample)."
|
30
|
2340 :format "%v (%{sample%})\n"
|
32
|
2341 :sample-face-get 'widget-color-item-button-face-get)
|
28
|
2342
|
|
2343 (defun widget-color-item-button-face-get (widget)
|
|
2344 ;; We create a face from the value.
|
|
2345 (require 'facemenu)
|
|
2346 (condition-case nil
|
|
2347 (facemenu-get-face (intern (concat "fg:" (widget-value widget))))
|
|
2348 (error 'default)))
|
|
2349
|
|
2350 (define-widget 'color 'push-button
|
|
2351 "Choose a color name (with sample)."
|
|
2352 :format "%[%t%]: %v"
|
|
2353 :tag "Color"
|
32
|
2354 :value "black"
|
28
|
2355 :value-create 'widget-color-value-create
|
|
2356 :value-delete 'widget-children-value-delete
|
|
2357 :value-get 'widget-color-value-get
|
|
2358 :value-set 'widget-color-value-set
|
|
2359 :action 'widget-color-action
|
|
2360 :match 'widget-field-match
|
|
2361 :tag "Color")
|
|
2362
|
|
2363 (defvar widget-color-choice-list nil)
|
|
2364 ;; Variable holding the possible colors.
|
|
2365
|
|
2366 (defun widget-color-choice-list ()
|
|
2367 (unless widget-color-choice-list
|
|
2368 (setq widget-color-choice-list
|
|
2369 (mapcar '(lambda (color) (list color))
|
|
2370 (x-defined-colors))))
|
|
2371 widget-color-choice-list)
|
|
2372
|
|
2373 (defun widget-color-value-create (widget)
|
|
2374 (let ((child (widget-create-child-and-convert
|
|
2375 widget 'color-item (widget-get widget :value))))
|
|
2376 (widget-put widget :children (list child))))
|
|
2377
|
|
2378 (defun widget-color-value-get (widget)
|
|
2379 ;; Pass command to first child.
|
|
2380 (widget-apply (car (widget-get widget :children)) :value-get))
|
|
2381
|
|
2382 (defun widget-color-value-set (widget value)
|
|
2383 ;; Pass command to first child.
|
|
2384 (widget-apply (car (widget-get widget :children)) :value-set value))
|
|
2385
|
|
2386 (defvar widget-color-history nil
|
|
2387 "History of entered colors")
|
|
2388
|
|
2389 (defun widget-color-action (widget &optional event)
|
|
2390 ;; Prompt for a color.
|
|
2391 (let* ((tag (widget-apply widget :menu-tag-get))
|
|
2392 (prompt (concat tag ": "))
|
|
2393 (answer (cond ((string-match "XEmacs" emacs-version)
|
|
2394 (read-color prompt))
|
|
2395 ((fboundp 'x-defined-colors)
|
|
2396 (completing-read (concat tag ": ")
|
|
2397 (widget-color-choice-list)
|
|
2398 nil nil nil 'widget-color-history))
|
|
2399 (t
|
|
2400 (read-string prompt (widget-value widget))))))
|
|
2401 (unless (zerop (length answer))
|
|
2402 (widget-value-set widget answer)
|
|
2403 (widget-apply widget :notify widget event)
|
|
2404 (widget-setup))))
|
|
2405
|
|
2406 ;;; The Help Echo
|
|
2407
|
|
2408 (defun widget-echo-help-mouse ()
|
|
2409 "Display the help message for the widget under the mouse.
|
|
2410 Enable with (run-with-idle-timer 1 t 'widget-echo-help-mouse)"
|
|
2411 (let* ((pos (mouse-position))
|
|
2412 (frame (car pos))
|
|
2413 (x (car (cdr pos)))
|
|
2414 (y (cdr (cdr pos)))
|
|
2415 (win (window-at x y frame))
|
|
2416 (where (coordinates-in-window-p (cons x y) win)))
|
|
2417 (when (consp where)
|
|
2418 (save-window-excursion
|
|
2419 (progn ; save-excursion
|
|
2420 (select-window win)
|
|
2421 (let* ((result (compute-motion (window-start win)
|
|
2422 '(0 . 0)
|
|
2423 (window-end win)
|
|
2424 where
|
|
2425 (window-width win)
|
|
2426 (cons (window-hscroll) 0)
|
|
2427 win)))
|
|
2428 (when (and (eq (nth 1 result) x)
|
|
2429 (eq (nth 2 result) y))
|
|
2430 (widget-echo-help (nth 0 result))))))))
|
|
2431 (unless track-mouse
|
|
2432 (setq track-mouse t)
|
|
2433 (add-hook 'post-command-hook 'widget-stop-mouse-tracking)))
|
|
2434
|
|
2435 (defun widget-stop-mouse-tracking (&rest args)
|
|
2436 "Stop the mouse tracking done while idle."
|
|
2437 (remove-hook 'post-command-hook 'widget-stop-mouse-tracking)
|
|
2438 (setq track-mouse nil))
|
|
2439
|
|
2440 (defun widget-at (pos)
|
|
2441 "The button or field at POS."
|
|
2442 (or (get-text-property pos 'button)
|
|
2443 (get-text-property pos 'field)))
|
|
2444
|
|
2445 (defun widget-echo-help (pos)
|
|
2446 "Display the help echo for widget at POS."
|
|
2447 (let* ((widget (widget-at pos))
|
|
2448 (help-echo (and widget (widget-get widget :help-echo))))
|
|
2449 (cond ((stringp help-echo)
|
|
2450 (message "%s" help-echo))
|
|
2451 ((and (symbolp help-echo) (fboundp help-echo)
|
|
2452 (stringp (setq help-echo (funcall help-echo widget))))
|
|
2453 (message "%s" help-echo)))))
|
|
2454
|
|
2455 ;;; The End:
|
|
2456
|
|
2457 (provide 'wid-edit)
|
|
2458
|
|
2459 ;; wid-edit.el ends here
|