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