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