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