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