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