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