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