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