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