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