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