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
|
1333
|
945 ;;;###autoload
|
428
|
946 (defun widget-convert (type &rest args)
|
|
947 "Convert TYPE to a widget without inserting it in the buffer.
|
|
948 The optional ARGS are additional keyword arguments."
|
|
949 ;; Don't touch the type.
|
|
950 (let* ((widget (if (symbolp type)
|
|
951 (list type)
|
|
952 (copy-sequence type)))
|
|
953 (current widget)
|
|
954 (keys args))
|
|
955 ;; First set the :args keyword.
|
|
956 (while (cdr current) ;Look in the type.
|
|
957 (let ((next (car (cdr current))))
|
|
958 (if (and (symbolp next) (eq (aref (symbol-name next) 0) ?:))
|
|
959 (setq current (cdr (cdr current)))
|
|
960 (setcdr current (list :args (cdr current)))
|
|
961 (setq current nil))))
|
|
962 (while args ;Look in the args.
|
|
963 (let ((next (nth 0 args)))
|
|
964 (if (and (symbolp next) (eq (aref (symbol-name next) 0) ?:))
|
|
965 (setq args (nthcdr 2 args))
|
|
966 (widget-put widget :args args)
|
|
967 (setq args nil))))
|
652
|
968 ;; Then convert the widget.
|
428
|
969 (setq type widget)
|
|
970 (while type
|
|
971 (let ((convert-widget (plist-get (cdr type) :convert-widget)))
|
|
972 (if convert-widget
|
|
973 (setq widget (funcall convert-widget widget))))
|
|
974 (setq type (get (car type) 'widget-type)))
|
|
975 ;; Finally set the keyword args.
|
|
976 (while keys
|
|
977 (let ((next (nth 0 keys)))
|
1309
|
978 (if (keywordp next)
|
428
|
979 (progn
|
|
980 (widget-put widget next (nth 1 keys))
|
|
981 (setq keys (nthcdr 2 keys)))
|
|
982 (setq keys nil))))
|
|
983 ;; Convert the :value to internal format.
|
|
984 (if (widget-member widget :value)
|
1309
|
985 (widget-put widget
|
|
986 :value (widget-apply widget
|
|
987 :value-to-internal
|
|
988 (widget-get widget :value))))
|
428
|
989 ;; Return the newly created widget.
|
|
990 widget))
|
|
991
|
1309
|
992 ;;;###autoload
|
428
|
993 (defun widget-insert (&rest args)
|
1173
|
994 "Call `insert' with ARGS even if surrounding text is read only."
|
428
|
995 (let ((inhibit-read-only t)
|
|
996 before-change-functions
|
|
997 after-change-functions)
|
|
998 (apply 'insert args)))
|
|
999
|
|
1000 (defun widget-convert-text (type from to
|
|
1001 &optional button-from button-to
|
|
1002 &rest args)
|
652
|
1003 "Return a widget of type TYPE with endpoints FROM and TO.
|
|
1004 No text will be inserted in the buffer. Instead the positions FROM and TO
|
|
1005 will be used as the widget's end points. The widget is ``wrapped around''
|
|
1006 the text between them.
|
|
1007 If optional arguments BUTTON-FROM and BUTTON-TO are given, these will be
|
|
1008 used as the widget's button end points.
|
428
|
1009 Optional ARGS are extra keyword arguments for TYPE."
|
|
1010 (let ((widget (apply 'widget-convert type :delete 'widget-leave-text args))
|
|
1011 (from (copy-marker from))
|
|
1012 (to (copy-marker to)))
|
|
1013 (set-marker-insertion-type from t)
|
|
1014 (set-marker-insertion-type to nil)
|
|
1015 (widget-put widget :from from)
|
|
1016 (widget-put widget :to to)
|
|
1017 (when button-from
|
|
1018 (widget-specify-button widget button-from button-to))
|
|
1019 widget))
|
|
1020
|
|
1021 (defun widget-convert-button (type from to &rest args)
|
652
|
1022 "Return a widget of type TYPE with endpoints FROM and TO.
|
428
|
1023 Optional ARGS are extra keyword arguments for TYPE.
|
652
|
1024 No text will be inserted in the buffer. Instead the positions FROM and TO
|
|
1025 will be used as the widget's end points, as well as the widget's button's
|
|
1026 end points. The widget is ``wrapped around'' the text between them."
|
428
|
1027 (apply 'widget-convert-text type from to from to args))
|
|
1028
|
|
1029 (defun widget-leave-text (widget)
|
|
1030 "Remove markers and extents from WIDGET and its children."
|
|
1031 (let ((from (widget-get widget :from))
|
|
1032 (to (widget-get widget :to))
|
|
1033 (button (widget-get widget :button-extent))
|
|
1034 (sample (widget-get widget :sample-extent))
|
|
1035 (doc (widget-get widget :doc-extent))
|
1309
|
1036 (field (widget-get widget :field-extent)))
|
428
|
1037 (set-marker from nil)
|
|
1038 (set-marker to nil)
|
|
1039 ;; Maybe we should delete the extents here? As this code doesn't
|
|
1040 ;; remove them from widget structures, maybe it's safer to just
|
1309
|
1041 ;; detach them. That's what GNU-compatible `delete-overlay' does.
|
428
|
1042 (when button
|
|
1043 (detach-extent button))
|
|
1044 (when sample
|
|
1045 (detach-extent sample))
|
|
1046 (when doc
|
|
1047 (detach-extent doc))
|
|
1048 (when field
|
|
1049 (detach-extent field))
|
1309
|
1050 (mapc 'widget-leave-text (widget-get widget :children))))
|
428
|
1051
|
|
1052
|
|
1053 ;;; Keymap and Commands.
|
|
1054
|
|
1055 (defvar widget-keymap nil
|
1173
|
1056 "Keymap containing useful bindings for buffers containing widgets.
|
428
|
1057 Recommended as a parent keymap for modes using widgets.")
|
|
1058
|
|
1059 (unless widget-keymap
|
|
1060 (setq widget-keymap (make-sparse-keymap))
|
|
1061 (define-key widget-keymap [tab] 'widget-forward)
|
|
1062 (define-key widget-keymap [(shift tab)] 'widget-backward)
|
|
1063 (define-key widget-keymap [(meta tab)] 'widget-backward)
|
|
1064 (define-key widget-keymap [backtab] 'widget-backward))
|
|
1065
|
|
1066 (defvar widget-global-map global-map
|
1173
|
1067 "Keymap used for events a widget does not handle itself.")
|
428
|
1068 (make-variable-buffer-local 'widget-global-map)
|
|
1069
|
|
1070 (defvar widget-field-keymap nil
|
|
1071 "Keymap used inside an editable field.")
|
|
1072
|
|
1073 (unless widget-field-keymap
|
|
1074 (setq widget-field-keymap (make-sparse-keymap))
|
|
1075 (set-keymap-parents widget-field-keymap global-map)
|
|
1076 (define-key widget-field-keymap "\C-k" 'widget-kill-line)
|
|
1077 (define-key widget-field-keymap [(meta tab)] 'widget-complete)
|
|
1078 (define-key widget-field-keymap [tab] 'widget-forward)
|
|
1079 (define-key widget-field-keymap [(shift tab)] 'widget-backward)
|
|
1080 (define-key widget-field-keymap "\C-m" 'widget-field-activate)
|
|
1081 (define-key widget-field-keymap "\C-a" 'widget-beginning-of-line)
|
|
1082 (define-key widget-field-keymap "\C-e" 'widget-end-of-line)
|
|
1083 (define-key widget-field-keymap "\C-t" 'widget-transpose-chars))
|
|
1084
|
|
1085 (defvar widget-text-keymap nil
|
|
1086 "Keymap used inside a text field.")
|
|
1087
|
|
1088 (unless widget-text-keymap
|
|
1089 (setq widget-text-keymap (make-sparse-keymap))
|
|
1090 (set-keymap-parents widget-field-keymap global-map)
|
|
1091 (define-key widget-text-keymap "\C-a" 'widget-beginning-of-line)
|
|
1092 (define-key widget-text-keymap "\C-e" 'widget-end-of-line)
|
|
1093 (define-key widget-text-keymap "\C-t" 'widget-transpose-chars))
|
|
1094
|
|
1095 (defvar widget-button-keymap nil
|
|
1096 "Keymap used inside a button.")
|
|
1097
|
|
1098 (unless widget-button-keymap
|
|
1099 (setq widget-button-keymap (make-sparse-keymap))
|
|
1100 (set-keymap-parents widget-button-keymap widget-keymap)
|
|
1101 (define-key widget-button-keymap "\C-m" 'widget-button-press)
|
|
1102 (define-key widget-button-keymap [button2] 'widget-button-click)
|
|
1103 ;; Ideally, button3 within a button should invoke a button-specific
|
|
1104 ;; menu.
|
|
1105 (define-key widget-button-keymap [button3] 'widget-button-click)
|
|
1106 ;;Glyph support.
|
|
1107 (define-key widget-button-keymap [button1] 'widget-button1-click))
|
|
1108
|
|
1109
|
|
1110 (defun widget-field-activate (pos &optional event)
|
652
|
1111 "Invoke the editable field at point."
|
428
|
1112 (interactive "@d")
|
|
1113 (let ((field (widget-field-find pos)))
|
|
1114 (if field
|
|
1115 (widget-apply-action field event)
|
|
1116 (call-interactively
|
|
1117 (lookup-key widget-global-map (this-command-keys))))))
|
|
1118
|
|
1119 (defface widget-button-pressed-face
|
|
1120 '((((class color))
|
|
1121 (:foreground "red"))
|
|
1122 (t
|
|
1123 (:bold t :underline t)))
|
|
1124 "Face used for pressed buttons."
|
|
1125 :group 'widget-faces)
|
|
1126
|
|
1127 (defun widget-event-point (event)
|
|
1128 "Character position of the mouse event, or nil."
|
|
1129 (and (mouse-event-p event)
|
|
1130 (event-point event)))
|
|
1131
|
|
1132 (defun widget-button-click (event)
|
1173
|
1133 "Invoke button under mouse pointer."
|
428
|
1134 (interactive "e")
|
|
1135 (with-current-buffer (event-buffer event)
|
|
1136 (cond ((event-glyph event)
|
|
1137 (widget-glyph-click event))
|
|
1138 ((widget-event-point event)
|
|
1139 (let* ((pos (widget-event-point event))
|
|
1140 (button (get-char-property pos 'button)))
|
|
1141 (if button
|
|
1142 (let* ((extent (widget-get button :button-extent))
|
|
1143 (face (extent-property extent 'face))
|
|
1144 (mouse-face (extent-property extent 'mouse-face))
|
|
1145 (help-echo (extent-property extent 'help-echo)))
|
|
1146 (unwind-protect
|
|
1147 (progn
|
|
1148 ;; Merge relevant faces, and make the result mouse-face.
|
|
1149 (let ((merge `(widget-button-pressed-face ,mouse-face)))
|
|
1150 (nconc merge (if (listp face)
|
|
1151 face (list face)))
|
|
1152 (setq merge (delete-if-not 'find-face merge))
|
|
1153 (set-extent-property extent 'mouse-face merge))
|
|
1154 (unless (widget-apply button :mouse-down-action event)
|
|
1155 ;; Wait for button release.
|
|
1156 (while (not (button-release-event-p
|
|
1157 (setq event (next-event))))
|
|
1158 (dispatch-event event)))
|
|
1159 ;; Disallow mouse-face and help-echo.
|
|
1160 (set-extent-property extent 'mouse-face nil)
|
|
1161 (set-extent-property extent 'help-echo nil)
|
|
1162 (setq pos (widget-event-point event))
|
|
1163 (unless (eq (current-buffer) (extent-object extent))
|
|
1164 ;; Barf if dispatch-event tripped us by
|
|
1165 ;; changing buffer.
|
|
1166 (error "Buffer changed during mouse motion"))
|
|
1167 ;; Do the associated action.
|
|
1168 (when (and pos (extent-in-region-p extent pos pos))
|
|
1169 (widget-apply-action button event)))
|
|
1170 ;; Unwinding: fully release the button.
|
|
1171 (set-extent-property extent 'mouse-face mouse-face)
|
|
1172 (set-extent-property extent 'help-echo help-echo)))
|
|
1173 ;; This should not happen!
|
|
1174 (error "`widget-button-click' called outside button"))))
|
|
1175 (t
|
|
1176 (message "You clicked somewhere weird")))))
|
|
1177
|
|
1178 (defun widget-button1-click (event)
|
|
1179 "Invoke glyph below mouse pointer."
|
|
1180 (interactive "@e")
|
|
1181 (if (event-glyph event)
|
|
1182 (widget-glyph-click event)
|
|
1183 ;; Should somehow avoid this.
|
|
1184 (let ((command (lookup-key widget-global-map (this-command-keys))))
|
|
1185 (and (commandp command)
|
|
1186 (call-interactively command)))))
|
|
1187
|
|
1188 (defun widget-glyph-click (event)
|
|
1189 "Handle click on a glyph."
|
|
1190 (let* ((glyph (event-glyph event))
|
|
1191 (extent (event-glyph-extent event))
|
|
1192 (widget (extent-property extent 'glyph-widget))
|
|
1193 (down-glyph (or (and widget (widget-get widget :glyph-down)) glyph))
|
|
1194 (up-glyph (or (and widget (widget-get widget :glyph-up)) glyph))
|
|
1195 (last event))
|
|
1196 (unless (widget-apply widget :active)
|
|
1197 (error "This widget is inactive"))
|
|
1198 (let ((current-glyph 'down))
|
|
1199 ;; We always know what glyph is drawn currently, to avoid
|
|
1200 ;; unnecessary extent changes. Is this any noticeable gain?
|
|
1201 (unwind-protect
|
|
1202 (progn
|
|
1203 ;; Press the glyph.
|
|
1204 (set-extent-end-glyph extent down-glyph)
|
|
1205 ;; Redisplay (shouldn't be needed, but...)
|
|
1206 (sit-for 0)
|
|
1207 (unless (widget-apply widget :mouse-down-action event)
|
|
1208 ;; Wait for the release.
|
|
1209 (while (not (button-release-event-p last))
|
|
1210 (unless (button-press-event-p last)
|
|
1211 (dispatch-event last))
|
|
1212 (when (motion-event-p last)
|
|
1213 ;; Update glyphs on mouse motion.
|
|
1214 (if (eq extent (event-glyph-extent last))
|
|
1215 (unless (eq current-glyph 'down)
|
|
1216 (set-extent-end-glyph extent down-glyph)
|
|
1217 (setq current-glyph 'down))
|
|
1218 (unless (eq current-glyph 'up)
|
|
1219 (set-extent-end-glyph extent up-glyph)
|
|
1220 (setq current-glyph 'up))))
|
|
1221 (setq last (next-event event))))
|
|
1222 (unless (eq (current-buffer) (extent-object extent))
|
|
1223 ;; Barf if dispatch-event tripped us by changing buffer.
|
|
1224 (error "Buffer changed during mouse motion"))
|
|
1225 ;; Apply widget action.
|
|
1226 (when (eq extent (event-glyph-extent last))
|
|
1227 (let ((widget (extent-property (event-glyph-extent event)
|
|
1228 'glyph-widget)))
|
|
1229 (cond ((null widget)
|
|
1230 (message "You clicked on a glyph"))
|
|
1231 ((not (widget-apply widget :active))
|
|
1232 (error "This glyph is inactive"))
|
|
1233 (t
|
|
1234 (widget-apply-action widget event))))))
|
|
1235 ;; Release the glyph.
|
|
1236 (and (eq current-glyph 'down)
|
|
1237 ;; The extent might have been detached or deleted
|
|
1238 (extent-live-p extent)
|
|
1239 (not (extent-detached-p extent))
|
|
1240 (set-extent-end-glyph extent up-glyph))))))
|
|
1241
|
|
1242 (defun widget-button-press (pos &optional event)
|
|
1243 "Invoke button at POS."
|
|
1244 (interactive "@d")
|
|
1245 (let ((button (get-char-property pos 'button)))
|
|
1246 (if button
|
|
1247 (widget-apply-action button event)
|
|
1248 (let ((command (lookup-key widget-global-map (this-command-keys))))
|
|
1249 (when (commandp command)
|
|
1250 (call-interactively command))))))
|
|
1251
|
|
1252 (defun widget-tabable-at (&optional pos last-tab backwardp)
|
|
1253 "Return the tabable widget at POS, or nil.
|
|
1254 POS defaults to the value of (point)."
|
|
1255 (unless pos
|
|
1256 (setq pos (point)))
|
|
1257 (let ((widget (widget-at pos)))
|
|
1258 (if widget
|
|
1259 (let ((order (widget-get widget :tab-order)))
|
|
1260 (if order
|
|
1261 (if last-tab (and (= order (if backwardp
|
|
1262 (1- last-tab)
|
|
1263 (1+ last-tab)))
|
|
1264 widget)
|
|
1265 (and (> order 0) widget))
|
|
1266 widget))
|
|
1267 nil)))
|
|
1268
|
|
1269 ;; Return the button or field extent at point.
|
|
1270 (defun widget-button-or-field-extent (pos)
|
|
1271 (or (and (get-char-property pos 'button)
|
|
1272 (widget-get (get-char-property pos 'button)
|
|
1273 :button-extent))
|
|
1274 (and (get-char-property pos 'field)
|
|
1275 (widget-get (get-char-property pos 'field)
|
|
1276 :field-extent))))
|
|
1277
|
|
1278 (defun widget-next-button-or-field (pos)
|
|
1279 "Find the next button, or field, and return its start position, or nil.
|
|
1280 Internal function, don't use it outside `wid-edit'."
|
|
1281 (let* ((at-point (widget-button-or-field-extent pos))
|
|
1282 (extent (map-extents
|
|
1283 (lambda (ext ignore)
|
|
1284 ext)
|
|
1285 nil (if at-point (extent-end-position at-point) pos)
|
|
1286 nil nil 'start-open 'button-or-field)))
|
|
1287 (and extent
|
|
1288 (extent-start-position extent))))
|
|
1289
|
|
1290 ;; This is too slow in buffers with many buttons (W3).
|
|
1291 (defun widget-previous-button-or-field (pos)
|
|
1292 "Find the previous button, or field, and return its start position, or nil.
|
|
1293 Internal function, don't use it outside `wid-edit'."
|
|
1294 (let* ((at-point (widget-button-or-field-extent pos))
|
|
1295 previous-extent)
|
|
1296 (map-extents
|
|
1297 (lambda (ext ignore)
|
|
1298 (if (eq ext at-point)
|
|
1299 ;; We reached the extent we were on originally
|
|
1300 (if (= pos (extent-start-position at-point))
|
|
1301 previous-extent
|
|
1302 (setq previous-extent at-point))
|
|
1303 (setq previous-extent ext)
|
|
1304 nil))
|
|
1305 nil nil pos nil 'start-open 'button-or-field)
|
|
1306 (and previous-extent
|
|
1307 (extent-start-position previous-extent))))
|
|
1308
|
|
1309 (defun widget-move (arg)
|
|
1310 "Move point to the ARG next field or button.
|
|
1311 ARG may be negative to move backward."
|
|
1312 (let ((opoint (point)) (wrapped 0)
|
|
1313 (last-tab (widget-get (widget-at (point)) :tab-order))
|
|
1314 nextpos found)
|
|
1315 ;; Movement backward
|
|
1316 (while (< arg 0)
|
|
1317 (setq nextpos (widget-previous-button-or-field (point)))
|
|
1318 (if nextpos
|
|
1319 (progn
|
|
1320 (goto-char nextpos)
|
|
1321 (when (and (not (get-char-property nextpos 'widget-inactive))
|
|
1322 (widget-tabable-at nil last-tab t))
|
|
1323 (incf arg)
|
|
1324 (setq found t
|
|
1325 last-tab (widget-get (widget-at (point))
|
|
1326 :tab-order))))
|
|
1327 (if (and (not found) (> wrapped 1))
|
|
1328 (setq arg 0
|
|
1329 found nil)
|
|
1330 (goto-char (point-max))
|
|
1331 (incf wrapped))))
|
|
1332 ;; Movement forward
|
|
1333 (while (> arg 0)
|
|
1334 (setq nextpos (widget-next-button-or-field (point)))
|
|
1335 (if nextpos
|
|
1336 (progn
|
|
1337 (goto-char nextpos)
|
|
1338 (when (and (not (get-char-property nextpos 'widget-inactive))
|
|
1339 (widget-tabable-at nil last-tab))
|
|
1340 (decf arg)
|
|
1341 (setq found t
|
|
1342 last-tab (widget-get (widget-at (point))
|
|
1343 :tab-order))))
|
|
1344 (if (and (not found) (> wrapped 1))
|
|
1345 (setq arg 0
|
|
1346 found nil)
|
|
1347 (goto-char (point-min))
|
|
1348 (incf wrapped))))
|
|
1349 (if (not found)
|
|
1350 (goto-char opoint)
|
|
1351 (widget-echo-help (point))
|
|
1352 (run-hooks 'widget-move-hook))))
|
|
1353
|
|
1354 (defun widget-forward (arg)
|
|
1355 "Move point to the next field or button.
|
|
1356 With optional ARG, move across that many fields."
|
|
1357 (interactive "p")
|
|
1358 (run-hooks 'widget-forward-hook)
|
|
1359 (widget-move arg))
|
|
1360
|
|
1361 (defun widget-backward (arg)
|
|
1362 "Move point to the previous field or button.
|
|
1363 With optional ARG, move across that many fields."
|
|
1364 (interactive "p")
|
|
1365 (run-hooks 'widget-backward-hook)
|
|
1366 (widget-move (- arg)))
|
|
1367
|
|
1368 (defun widget-beginning-of-line ()
|
652
|
1369 "Go to beginning of field or beginning of line, whichever is first.
|
|
1370
|
|
1371 It is an error to use this function after creating the widget but before
|
|
1372 invoking `widget-setup'."
|
428
|
1373 (interactive "_")
|
|
1374 (let* ((field (widget-field-find (point)))
|
|
1375 (start (and field (widget-field-start field))))
|
|
1376 (if (and start (not (eq start (point))))
|
|
1377 (goto-char start)
|
|
1378 (call-interactively 'beginning-of-line))))
|
|
1379
|
|
1380 (defun widget-end-of-line ()
|
652
|
1381 "Go to end of field or end of line, whichever is first.
|
|
1382
|
|
1383 It is an error to use this function after creating the widget but before
|
|
1384 invoking `widget-setup'."
|
428
|
1385 (interactive "_")
|
|
1386 (let* ((field (widget-field-find (point)))
|
|
1387 (end (and field (widget-field-end field))))
|
|
1388 (if (and end (not (eq end (point))))
|
|
1389 (goto-char end)
|
|
1390 (call-interactively 'end-of-line))))
|
|
1391
|
|
1392 (defun widget-kill-line ()
|
652
|
1393 "Kill to end of field or end of line, whichever is first.
|
|
1394
|
|
1395 It is an error to use this function after creating the widget but before
|
|
1396 invoking `widget-setup'."
|
428
|
1397 (interactive)
|
|
1398 (let* ((field (widget-field-find (point)))
|
|
1399 (newline (save-excursion (forward-line 1) (point)))
|
|
1400 (end (and field (widget-field-end field))))
|
|
1401 (if (and field (> newline end))
|
|
1402 (kill-region (point) end)
|
|
1403 (call-interactively 'kill-line))))
|
|
1404
|
|
1405 (defun widget-transpose-chars (arg)
|
|
1406 "Like `transpose-chars', but works correctly at end of widget."
|
|
1407 (interactive "*P")
|
|
1408 (let* ((field (widget-field-find (point)))
|
|
1409 (start (and field (widget-field-start field)))
|
|
1410 (end (and field (widget-field-end field)))
|
|
1411 (last-non-space (and start end
|
|
1412 (save-excursion
|
|
1413 (goto-char end)
|
|
1414 (skip-chars-backward " \t\n" start)
|
|
1415 (point)))))
|
|
1416 (cond ((and last-non-space
|
|
1417 (or (= last-non-space start)
|
|
1418 (= last-non-space (1+ start))))
|
|
1419 ;; empty or one-character field
|
|
1420 nil)
|
|
1421 ((= (point) start)
|
|
1422 ;; at the beginning of the field -- we would get an error here.
|
|
1423 (error "Cannot transpose at beginning of field"))
|
|
1424 (t
|
|
1425 (when (and (null arg)
|
|
1426 (= last-non-space (point)))
|
446
|
1427 (backward-char 1))
|
428
|
1428 (transpose-chars arg)))))
|
|
1429
|
|
1430 (defcustom widget-complete-field (lookup-key global-map "\M-\t")
|
|
1431 "Default function to call for completion inside fields."
|
|
1432 :options '(ispell-complete-word complete-tag lisp-complete-symbol)
|
|
1433 :type 'function
|
|
1434 :group 'widgets)
|
|
1435
|
|
1436 (defun widget-complete ()
|
|
1437 "Complete content of editable field from point.
|
|
1438 When not inside a field, move to the previous button or field."
|
|
1439 (interactive)
|
|
1440 ;; Somehow, this should make pressing M-TAB twice scroll the
|
|
1441 ;; completions window.
|
|
1442 (let ((field (widget-field-find (point))))
|
|
1443 (if field
|
|
1444 (widget-apply field :complete)
|
|
1445 (error "Not in an editable field"))))
|
|
1446
|
|
1447
|
|
1448 ;;; Setting up the buffer.
|
|
1449
|
|
1450 (defvar widget-field-new nil)
|
|
1451 ;; List of all newly created editable fields in the buffer.
|
|
1452 (make-variable-buffer-local 'widget-field-new)
|
|
1453
|
|
1454 (defvar widget-field-list nil)
|
|
1455 ;; List of all editable fields in the buffer.
|
|
1456 (make-variable-buffer-local 'widget-field-list)
|
|
1457
|
1309
|
1458 ;; Is this a misnomer?
|
|
1459 (defun widget-at (pos)
|
|
1460 "The button or field at POS."
|
|
1461 (or (get-char-property pos 'button)
|
|
1462 (get-char-property pos 'field)))
|
|
1463
|
|
1464 ;;;###autoload
|
428
|
1465 (defun widget-setup ()
|
|
1466 "Setup current buffer so editing string widgets works."
|
|
1467 (let ((inhibit-read-only t)
|
|
1468 (after-change-functions nil)
|
|
1469 before-change-functions
|
|
1470 field)
|
|
1471 (while widget-field-new
|
|
1472 (setq field (car widget-field-new)
|
|
1473 widget-field-new (cdr widget-field-new)
|
|
1474 widget-field-list (cons field widget-field-list))
|
|
1475 (let ((from (car (widget-get field :field-extent)))
|
|
1476 (to (cdr (widget-get field :field-extent))))
|
|
1477 (widget-specify-field field
|
|
1478 (marker-position from) (marker-position to))
|
|
1479 (set-marker from nil)
|
|
1480 (set-marker to nil))
|
|
1481 ;; If the field is placed within the inactive zone, deactivate it.
|
|
1482 (let ((extent (widget-get field :field-extent)))
|
|
1483 (when (get-char-property (extent-start-position extent)
|
|
1484 'widget-inactive)
|
|
1485 (widget-activation-widget-mapper extent :deactivate)))))
|
|
1486 (widget-clear-undo)
|
|
1487 (widget-add-change))
|
|
1488
|
|
1489 (defvar widget-field-last nil)
|
|
1490 ;; Last field containing point.
|
|
1491 (make-variable-buffer-local 'widget-field-last)
|
|
1492
|
|
1493 (defvar widget-field-was nil)
|
|
1494 ;; The widget data before the change.
|
|
1495 (make-variable-buffer-local 'widget-field-was)
|
|
1496
|
1309
|
1497 (defun widget-field-at (pos)
|
|
1498 "Return the widget field at POS, or nil if none."
|
|
1499 (let ((field (get-char-property (or pos (point)) 'field)))
|
|
1500 (if (eq field 'boundary)
|
|
1501 nil
|
|
1502 field)))
|
|
1503
|
428
|
1504 (defun widget-field-buffer (widget)
|
652
|
1505 "Return the buffer containing WIDGET.
|
|
1506
|
|
1507 It is an error to use this function after creating the widget but before
|
|
1508 invoking `widget-setup'."
|
428
|
1509 (let ((extent (widget-get widget :field-extent)))
|
|
1510 (and extent (extent-object extent))))
|
|
1511
|
|
1512 (defun widget-field-start (widget)
|
652
|
1513 "Return the start of WIDGET's editing field.
|
|
1514
|
|
1515 It is an error to use this function after creating the widget but before
|
|
1516 invoking `widget-setup'."
|
428
|
1517 (let ((extent (widget-get widget :field-extent)))
|
|
1518 (and extent (extent-start-position extent))))
|
|
1519
|
|
1520 (defun widget-field-end (widget)
|
652
|
1521 "Return the end of WIDGET's editing field.
|
|
1522
|
|
1523 It is an error to use this function after creating the widget but before
|
|
1524 invoking `widget-setup'."
|
428
|
1525 (let ((extent (widget-get widget :field-extent)))
|
|
1526 ;; Don't subtract one if local-map works at the end of the extent.
|
|
1527 (and extent (if (or widget-field-add-space
|
|
1528 (null (widget-get widget :size)))
|
|
1529 (1- (extent-end-position extent))
|
|
1530 (extent-end-position extent)))))
|
|
1531
|
|
1532 (defun widget-field-find (pos)
|
|
1533 "Return the field at POS.
|
652
|
1534 Unlike (get-char-property POS 'field) this, works with empty fields too.
|
|
1535
|
|
1536 Warning: using this function after creating the widget but before invoking
|
|
1537 `widget-setup' will always fail."
|
1309
|
1538 ;; XEmacs: use `map-extents' instead of a while loop
|
428
|
1539 (let ((field-extent (map-extents (lambda (extent ignore)
|
|
1540 extent)
|
|
1541 nil pos pos nil nil 'field)))
|
|
1542 (and field-extent
|
|
1543 (extent-property field-extent 'field))))
|
|
1544
|
652
|
1545 ;; Warning: using this function after creating the widget but before
|
|
1546 ;; invoking `widget-setup' will always fail.
|
428
|
1547 (defun widget-before-change (from to)
|
|
1548 ;; Barf if the text changed is outside the editable fields.
|
|
1549 (unless inhibit-read-only
|
|
1550 (let ((from-field (widget-field-find from))
|
|
1551 (to-field (widget-field-find to)))
|
|
1552 (cond ((or (null from-field)
|
|
1553 (null to-field))
|
|
1554 ;; Either end of change is not within a field.
|
|
1555 (add-hook 'post-command-hook 'widget-add-change nil t)
|
|
1556 (error "Attempt to change text outside editable field"))
|
|
1557 ((not (eq from-field to-field))
|
|
1558 ;; The change begins in one fields, and ends in another one.
|
|
1559 (add-hook 'post-command-hook 'widget-add-change nil t)
|
|
1560 (error "Change should be restricted to a single field"))
|
|
1561 ((or (and from-field
|
|
1562 (get-char-property from 'widget-inactive))
|
|
1563 (and to-field
|
|
1564 (get-char-property to 'widget-inactive)))
|
|
1565 ;; Trying to change an inactive editable field.
|
|
1566 (add-hook 'post-command-hook 'widget-add-change nil t)
|
|
1567 (error "Attempt to change an inactive field"))
|
|
1568 (widget-field-use-before-change
|
|
1569 ;; #### Bletch! This loses because XEmacs get confused
|
|
1570 ;; if before-change-functions change the contents of
|
|
1571 ;; buffer before from/to.
|
|
1572 (condition-case nil
|
|
1573 (widget-apply from-field :notify from-field)
|
776
|
1574 (error (declare-fboundp (debug "Before Change")))))))))
|
428
|
1575
|
|
1576 (defun widget-add-change ()
|
|
1577 (make-local-hook 'post-command-hook)
|
|
1578 (remove-hook 'post-command-hook 'widget-add-change t)
|
|
1579 (make-local-hook 'before-change-functions)
|
|
1580 (add-hook 'before-change-functions 'widget-before-change nil t)
|
|
1581 (make-local-hook 'after-change-functions)
|
|
1582 (add-hook 'after-change-functions 'widget-after-change nil t))
|
|
1583
|
|
1584 (defun widget-after-change (from to old)
|
1309
|
1585 "Adjust field size and text properties.
|
|
1586
|
|
1587 Also, notify the widgets (so, for example, a variable changes its
|
|
1588 state to `modified'. when it is being edited)."
|
428
|
1589 (condition-case nil
|
|
1590 (let ((field (widget-field-find from))
|
|
1591 (other (widget-field-find to)))
|
|
1592 (when field
|
|
1593 (unless (eq field other)
|
776
|
1594 (declare-fboundp (debug "Change in different fields")))
|
428
|
1595 (let ((size (widget-get field :size)))
|
|
1596 (when size
|
|
1597 (let ((begin (widget-field-start field))
|
|
1598 (end (widget-field-end field)))
|
|
1599 (cond ((< (- end begin) size)
|
|
1600 ;; Field too small.
|
|
1601 (save-excursion
|
|
1602 (goto-char end)
|
|
1603 (insert-char ?\ (- (+ begin size) end))))
|
|
1604 ((> (- end begin) size)
|
|
1605 ;; Field too large and
|
|
1606 (if (or (< (point) (+ begin size))
|
|
1607 (> (point) end))
|
|
1608 ;; Point is outside extra space.
|
|
1609 (setq begin (+ begin size))
|
|
1610 ;; Point is within the extra space.
|
|
1611 (setq begin (point)))
|
|
1612 (save-excursion
|
|
1613 (goto-char end)
|
|
1614 (while (and (eq (preceding-char) ?\ )
|
|
1615 (> (point) begin))
|
|
1616 (delete-backward-char 1)))))))
|
|
1617 (widget-specify-secret field))
|
|
1618 (widget-apply field :notify field)))
|
776
|
1619 (error (declare-fboundp (debug "After Change")))))
|
428
|
1620
|
|
1621
|
|
1622 ;;; Widget Functions
|
|
1623 ;;
|
|
1624 ;; These functions are used in the definition of multiple widgets.
|
|
1625
|
|
1626 (defun widget-parent-action (widget &optional event)
|
|
1627 "Tell :parent of WIDGET to handle the :action.
|
|
1628 Optional EVENT is the event that triggered the action."
|
|
1629 (widget-apply (widget-get widget :parent) :action event))
|
|
1630
|
|
1631 (defun widget-children-value-delete (widget)
|
|
1632 "Delete all :children and :buttons in WIDGET."
|
|
1633 (mapc 'widget-delete (widget-get widget :children))
|
|
1634 (widget-put widget :children nil)
|
|
1635 (mapc 'widget-delete (widget-get widget :buttons))
|
|
1636 (widget-put widget :buttons nil))
|
|
1637
|
|
1638 (defun widget-children-validate (widget)
|
|
1639 "All the :children must be valid."
|
|
1640 (let ((children (widget-get widget :children))
|
|
1641 child found)
|
|
1642 (while (and children (not found))
|
|
1643 (setq child (car children)
|
|
1644 children (cdr children)
|
|
1645 found (widget-apply child :validate)))
|
|
1646 found))
|
|
1647
|
1309
|
1648 (defun widget-types-copy (widget)
|
|
1649 "Copy :args as widget types in WIDGET."
|
|
1650 (widget-put widget :args (mapcar 'widget-copy (widget-get widget :args)))
|
|
1651 widget)
|
|
1652
|
|
1653 ;; Made defsubst to speed up face editor creation.
|
|
1654 (defsubst widget-types-convert-widget (widget)
|
428
|
1655 "Convert :args as widget types in WIDGET."
|
|
1656 (widget-put widget :args (mapcar 'widget-convert (widget-get widget :args)))
|
|
1657 widget)
|
|
1658
|
|
1659 (defun widget-value-convert-widget (widget)
|
|
1660 "Initialize :value from :args in WIDGET."
|
|
1661 (let ((args (widget-get widget :args)))
|
|
1662 (when args
|
|
1663 (widget-put widget :value (car args))
|
|
1664 ;; Don't convert :value here, as this is done in `widget-convert'.
|
|
1665 ;; (widget-put widget :value (widget-apply widget
|
|
1666 ;; :value-to-internal (car args)))
|
|
1667 (widget-put widget :args nil)))
|
|
1668 widget)
|
|
1669
|
|
1670 (defun widget-value-value-get (widget)
|
|
1671 "Return the :value property of WIDGET."
|
|
1672 (widget-get widget :value))
|
|
1673
|
|
1674 ;;; The `default' Widget.
|
|
1675
|
|
1676 (define-widget 'default nil
|
|
1677 "Basic widget other widgets are derived from."
|
|
1678 :value-to-internal (lambda (widget value) value)
|
|
1679 :value-to-external (lambda (widget value) value)
|
|
1680 :button-prefix 'widget-button-prefix
|
|
1681 :button-suffix 'widget-button-suffix
|
|
1682 :complete 'widget-default-complete
|
|
1683 :create 'widget-default-create
|
|
1684 :indent nil
|
|
1685 :offset 0
|
|
1686 :format-handler 'widget-default-format-handler
|
|
1687 :button-face-get 'widget-default-button-face-get
|
|
1688 :sample-face-get 'widget-default-sample-face-get
|
|
1689 :button-keymap widget-button-keymap
|
|
1690 :delete 'widget-default-delete
|
|
1691 :value-set 'widget-default-value-set
|
|
1692 :value-inline 'widget-default-value-inline
|
|
1693 :default-get 'widget-default-default-get
|
|
1694 :menu-tag-get 'widget-default-menu-tag-get
|
1309
|
1695 :validate #'ignore
|
428
|
1696 :active 'widget-default-active
|
|
1697 :activate 'widget-specify-active
|
|
1698 :deactivate 'widget-default-deactivate
|
1309
|
1699 :mouse-down-action #'ignore
|
428
|
1700 :action 'widget-default-action
|
|
1701 :notify 'widget-default-notify
|
|
1702 :prompt-value 'widget-default-prompt-value)
|
|
1703
|
|
1704 (defun widget-default-complete (widget)
|
|
1705 "Call the value of the :complete-function property of WIDGET.
|
|
1706 If that does not exists, call the value of `widget-complete-field'."
|
1309
|
1707 (call-interactively (or (widget-get widget :complete-function)
|
|
1708 widget-complete-field)))
|
428
|
1709
|
|
1710 (defun widget-default-create (widget)
|
|
1711 "Create WIDGET at point in the current buffer."
|
|
1712 (widget-specify-insert
|
|
1713 (let ((from (point))
|
|
1714 button-begin button-end button-glyph
|
|
1715 sample-begin sample-end
|
|
1716 doc-begin doc-end
|
|
1717 value-pos)
|
|
1718 (insert (widget-get widget :format))
|
|
1719 (goto-char from)
|
1309
|
1720 ;; Parse escapes in format.
|
|
1721 ;; Coding this in C would speed up things *a lot*.
|
428
|
1722 (while (re-search-forward "%\\(.\\)" nil t)
|
|
1723 (let ((escape (aref (match-string 1) 0)))
|
|
1724 (replace-match "" t t)
|
|
1725 (cond ((eq escape ?%)
|
1309
|
1726 (insert ?%))
|
428
|
1727 ((eq escape ?\[)
|
|
1728 (setq button-begin (point-marker))
|
|
1729 (set-marker-insertion-type button-begin nil))
|
|
1730 ((eq escape ?\])
|
|
1731 (setq button-end (point-marker))
|
|
1732 (set-marker-insertion-type button-end nil))
|
|
1733 ((eq escape ?\{)
|
|
1734 (setq sample-begin (point)))
|
|
1735 ((eq escape ?\})
|
|
1736 (setq sample-end (point)))
|
|
1737 ((eq escape ?n)
|
|
1738 (when (widget-get widget :indent)
|
1309
|
1739 (insert ?\n)
|
428
|
1740 (insert-char ?\ (widget-get widget :indent))))
|
|
1741 ((eq escape ?t)
|
|
1742 (let* ((tag (widget-get widget :tag))
|
|
1743 (glyph (widget-get widget :tag-glyph)))
|
|
1744 (cond (glyph
|
|
1745 (setq button-glyph
|
|
1746 (widget-glyph-insert
|
|
1747 widget (or tag "Image") glyph)))
|
|
1748 (tag
|
|
1749 (insert tag))
|
|
1750 (t
|
1309
|
1751 (princ (widget-get widget :value)
|
|
1752 (current-buffer))))))
|
428
|
1753 ((eq escape ?d)
|
|
1754 (let ((doc (widget-get widget :doc)))
|
|
1755 (when doc
|
|
1756 (setq doc-begin (point))
|
|
1757 (insert doc)
|
|
1758 (while (eq (preceding-char) ?\n)
|
|
1759 (delete-backward-char 1))
|
1309
|
1760 (insert ?\n)
|
428
|
1761 (setq doc-end (point)))))
|
|
1762 ((eq escape ?v)
|
|
1763 (if (and button-begin (not button-end))
|
|
1764 (widget-apply widget :value-create)
|
|
1765 (setq value-pos (point-marker))))
|
|
1766 (t
|
|
1767 (widget-apply widget :format-handler escape)))))
|
|
1768 ;; Specify button, sample, and doc, and insert value.
|
|
1769 (when (and button-begin button-end)
|
|
1770 (unless button-glyph
|
|
1771 (goto-char button-begin)
|
|
1772 (insert (widget-get-indirect widget :button-prefix))
|
|
1773 (goto-char button-end)
|
|
1774 (set-marker-insertion-type button-end t)
|
|
1775 (insert (widget-get-indirect widget :button-suffix)))
|
|
1776 (widget-specify-button widget button-begin button-end)
|
|
1777 ;; Is this necessary?
|
|
1778 (set-marker button-begin nil)
|
|
1779 (set-marker button-end nil))
|
|
1780 (and sample-begin sample-end
|
|
1781 (widget-specify-sample widget sample-begin sample-end))
|
|
1782 (and doc-begin doc-end
|
|
1783 (widget-specify-doc widget doc-begin doc-end))
|
|
1784 (when value-pos
|
|
1785 (goto-char value-pos)
|
|
1786 (widget-apply widget :value-create)))
|
|
1787 (let ((from (point-min-marker))
|
|
1788 (to (point-max-marker)))
|
|
1789 (set-marker-insertion-type from t)
|
|
1790 (set-marker-insertion-type to nil)
|
|
1791 (widget-put widget :from from)
|
|
1792 (widget-put widget :to to)))
|
|
1793 (widget-clear-undo))
|
|
1794
|
|
1795 (defun widget-default-format-handler (widget escape)
|
|
1796 ;; We recognize the %h escape by default.
|
|
1797 (let* ((buttons (widget-get widget :buttons)))
|
|
1798 (cond ((eq escape ?h)
|
|
1799 (let* ((doc-property (widget-get widget :documentation-property))
|
|
1800 (doc-try (cond ((widget-get widget :doc))
|
1309
|
1801 ((functionp doc-property)
|
|
1802 (funcall doc-property
|
|
1803 (widget-get widget :value)))
|
428
|
1804 ((symbolp doc-property)
|
|
1805 (documentation-property
|
|
1806 (widget-get widget :value)
|
1309
|
1807 doc-property))))
|
428
|
1808 (doc-text (and (stringp doc-try)
|
|
1809 (> (length doc-try) 1)
|
|
1810 doc-try))
|
|
1811 (doc-indent (widget-get widget :documentation-indent)))
|
|
1812 (when doc-text
|
|
1813 (and (eq (preceding-char) ?\n)
|
|
1814 (widget-get widget :indent)
|
|
1815 (insert-char ?\ (widget-get widget :indent)))
|
|
1816 ;; The `*' in the beginning is redundant.
|
|
1817 (when (eq (aref doc-text 0) ?*)
|
|
1818 (setq doc-text (substring doc-text 1)))
|
|
1819 ;; Get rid of trailing newlines.
|
|
1820 (when (string-match "\n+\\'" doc-text)
|
|
1821 (setq doc-text (substring doc-text 0 (match-beginning 0))))
|
|
1822 (push (widget-create-child-and-convert
|
|
1823 widget 'documentation-string
|
|
1824 :indent (cond ((numberp doc-indent)
|
|
1825 doc-indent)
|
|
1826 ((null doc-indent)
|
|
1827 nil)
|
|
1828 (t 0))
|
|
1829 doc-text)
|
|
1830 buttons))))
|
|
1831 (t
|
|
1832 (signal 'error (list "Unknown escape" escape))))
|
|
1833 (widget-put widget :buttons buttons)))
|
|
1834
|
|
1835 (defun widget-default-button-face-get (widget)
|
|
1836 ;; Use :button-face or widget-button-face
|
|
1837 (or (widget-get widget :button-face)
|
|
1838 (let ((parent (widget-get widget :parent)))
|
|
1839 (if parent
|
|
1840 (widget-apply parent :button-face-get)
|
|
1841 widget-button-face))))
|
|
1842
|
|
1843 (defun widget-default-sample-face-get (widget)
|
|
1844 ;; Use :sample-face.
|
|
1845 (widget-get widget :sample-face))
|
|
1846
|
|
1847 (defun widget-default-delete (widget)
|
1173
|
1848 "Remove widget from the buffer."
|
428
|
1849 (let ((from (widget-get widget :from))
|
|
1850 (to (widget-get widget :to))
|
|
1851 (inactive-extent (widget-get widget :inactive))
|
|
1852 (button-extent (widget-get widget :button-extent))
|
|
1853 (sample-extent (widget-get widget :sample-extent))
|
|
1854 (doc-extent (widget-get widget :doc-extent))
|
|
1855 before-change-functions
|
|
1856 after-change-functions
|
|
1857 (inhibit-read-only t))
|
|
1858 (widget-apply widget :value-delete)
|
|
1859 (when inactive-extent
|
|
1860 (detach-extent inactive-extent))
|
|
1861 (when button-extent
|
|
1862 (detach-extent button-extent))
|
|
1863 (when sample-extent
|
|
1864 (detach-extent sample-extent))
|
|
1865 (when doc-extent
|
|
1866 (detach-extent doc-extent))
|
|
1867 (when (< from to)
|
|
1868 ;; Kludge: this doesn't need to be true for empty formats.
|
|
1869 (delete-region from to))
|
|
1870 (set-marker from nil)
|
|
1871 (set-marker to nil))
|
|
1872 (widget-clear-undo))
|
|
1873
|
|
1874 (defun widget-default-value-set (widget value)
|
1173
|
1875 "Recreate widget with new value."
|
428
|
1876 (let* ((old-pos (point))
|
|
1877 (from (copy-marker (widget-get widget :from)))
|
|
1878 (to (copy-marker (widget-get widget :to)))
|
|
1879 (offset (if (and (<= from old-pos) (<= old-pos to))
|
|
1880 (if (>= old-pos (1- to))
|
|
1881 (- old-pos to 1)
|
|
1882 (- old-pos from)))))
|
|
1883 ;;??? Bug: this ought to insert the new value before deleting the old one,
|
|
1884 ;; so that markers on either side of the value automatically
|
|
1885 ;; stay on the same side. -- rms.
|
|
1886 (save-excursion
|
|
1887 (goto-char (widget-get widget :from))
|
|
1888 (widget-apply widget :delete)
|
|
1889 (widget-put widget :value value)
|
|
1890 (widget-apply widget :create))
|
1309
|
1891 (if offset
|
|
1892 (if (< offset 0)
|
|
1893 (goto-char (+ (widget-get widget :to) offset 1))
|
|
1894 (goto-char (min (+ from offset) (1- (widget-get widget :to))))))))
|
428
|
1895
|
|
1896 (defun widget-default-value-inline (widget)
|
1173
|
1897 "Wrap value in a list unless it is inline."
|
428
|
1898 (if (widget-get widget :inline)
|
|
1899 (widget-value widget)
|
|
1900 (list (widget-value widget))))
|
|
1901
|
|
1902 (defun widget-default-default-get (widget)
|
1173
|
1903 "Get `:value'."
|
428
|
1904 (widget-get widget :value))
|
|
1905
|
|
1906 (defun widget-default-menu-tag-get (widget)
|
1173
|
1907 "Use tag or value for menus."
|
428
|
1908 (or (widget-get widget :menu-tag)
|
|
1909 (widget-get widget :tag)
|
|
1910 (widget-princ-to-string (widget-get widget :value))))
|
|
1911
|
|
1912 (defun widget-default-active (widget)
|
|
1913 "Return t iff this widget active (user modifiable)."
|
|
1914 (and (not (widget-get widget :inactive))
|
|
1915 (let ((parent (widget-get widget :parent)))
|
|
1916 (or (null parent)
|
|
1917 (widget-apply parent :active)))))
|
|
1918
|
|
1919 (defun widget-default-deactivate (widget)
|
|
1920 "Make WIDGET inactive for user modifications."
|
|
1921 (widget-specify-inactive widget
|
|
1922 (widget-get widget :from)
|
|
1923 (widget-get widget :to)))
|
|
1924
|
|
1925 (defun widget-default-action (widget &optional event)
|
1173
|
1926 "Notify the parent when a widget changes."
|
428
|
1927 (let ((parent (widget-get widget :parent)))
|
|
1928 (when parent
|
|
1929 (widget-apply parent :notify widget event))))
|
|
1930
|
|
1931 (defun widget-default-notify (widget child &optional event)
|
1173
|
1932 "Pass notification to parent."
|
428
|
1933 (widget-default-action widget event))
|
|
1934
|
|
1935 (defun widget-default-prompt-value (widget prompt value unbound)
|
1173
|
1936 "Read an arbitrary value. Stolen from `set-variable'."
|
|
1937 ;; (let ((initial (if unbound
|
|
1938 ;; nil
|
|
1939 ;; It would be nice if we could do a `(cons val 1)' here.
|
|
1940 ;; (prin1-to-string (custom-quote value))))))
|
|
1941 (eval-minibuffer prompt))
|
428
|
1942
|
|
1943 ;;; The `item' Widget.
|
|
1944
|
|
1945 (define-widget 'item 'default
|
|
1946 "Constant items for inclusion in other widgets."
|
|
1947 :convert-widget 'widget-value-convert-widget
|
|
1948 :value-create 'widget-item-value-create
|
|
1949 :value-delete 'ignore
|
|
1950 :value-get 'widget-value-value-get
|
|
1951 :match 'widget-item-match
|
|
1952 :match-inline 'widget-item-match-inline
|
|
1953 :action 'widget-item-action
|
|
1954 :format "%t\n")
|
|
1955
|
|
1956 (defun widget-item-value-create (widget)
|
1173
|
1957 "Insert the printed representation of the value."
|
1309
|
1958 (princ (widget-get widget :value) (current-buffer)))
|
428
|
1959
|
|
1960 (defun widget-item-match (widget value)
|
|
1961 ;; Match if the value is the same.
|
|
1962 (equal (widget-get widget :value) value))
|
|
1963
|
|
1964 (defun widget-item-match-inline (widget values)
|
|
1965 ;; Match if the value is the same.
|
|
1966 (let ((value (widget-get widget :value)))
|
|
1967 (and (listp value)
|
|
1968 (<= (length value) (length values))
|
|
1969 (let ((head (widget-sublist values 0 (length value))))
|
|
1970 (and (equal head value)
|
|
1971 (cons head (widget-sublist values (length value))))))))
|
|
1972
|
|
1973 (defun widget-sublist (list start &optional end)
|
|
1974 "Return the sublist of LIST from START to END.
|
|
1975 If END is omitted, it defaults to the length of LIST."
|
|
1976 (if (> start 0) (setq list (nthcdr start list)))
|
|
1977 (if end
|
1309
|
1978 (unless (<= end start)
|
428
|
1979 (setq list (copy-sequence list))
|
|
1980 (setcdr (nthcdr (- end start 1) list) nil)
|
|
1981 list)
|
|
1982 (copy-sequence list)))
|
|
1983
|
|
1984 (defun widget-item-action (widget &optional event)
|
|
1985 ;; Just notify itself.
|
|
1986 (widget-apply widget :notify widget event))
|
|
1987
|
|
1988 ;;; The `push-button' Widget.
|
|
1989
|
|
1990 (defcustom widget-push-button-gui widget-glyph-enable
|
|
1991 "If non nil, use GUI push buttons when available."
|
|
1992 :group 'widgets
|
|
1993 :type 'boolean)
|
|
1994
|
|
1995 (defcustom widget-push-button-prefix "["
|
|
1996 "String used as prefix for buttons."
|
|
1997 :type 'string
|
|
1998 :group 'widget-button)
|
|
1999
|
|
2000 (defcustom widget-push-button-suffix "]"
|
|
2001 "String used as suffix for buttons."
|
|
2002 :type 'string
|
|
2003 :group 'widget-button)
|
|
2004
|
|
2005 (define-widget 'push-button 'item
|
|
2006 "A pushable button."
|
|
2007 :button-prefix ""
|
|
2008 :button-suffix ""
|
|
2009 :value-create 'widget-push-button-value-create
|
|
2010 :format "%[%v%]")
|
|
2011
|
|
2012 (defun widget-push-button-value-create (widget)
|
1173
|
2013 "Insert text representing the `on' and `off' states."
|
428
|
2014 (let* ((tag (or (widget-get widget :tag)
|
|
2015 (widget-get widget :value)))
|
|
2016 (tag-glyph (widget-get widget :tag-glyph))
|
|
2017 (text (concat widget-push-button-prefix
|
|
2018 tag widget-push-button-suffix))
|
454
|
2019 gui inst)
|
428
|
2020 (cond (tag-glyph
|
|
2021 (widget-glyph-insert widget text tag-glyph))
|
|
2022 ;; We must check for console-on-window-system-p here,
|
|
2023 ;; because GUI will not work otherwise (it needs RGB
|
|
2024 ;; components for colors, and they are not known on TTYs).
|
|
2025 ((and widget-push-button-gui
|
|
2026 (console-on-window-system-p))
|
436
|
2027 (let* ((gui-button-shadow-thickness 1))
|
454
|
2028 (setq inst (make-gui-button tag 'widget-gui-action widget))
|
|
2029 (setq gui (make-glyph inst)))
|
|
2030 (widget-glyph-insert-glyph widget gui nil nil inst))
|
428
|
2031 (t
|
|
2032 (insert text)))))
|
|
2033
|
|
2034 (defun widget-gui-action (widget)
|
|
2035 "Apply :action for WIDGET."
|
|
2036 (widget-apply-action widget (this-command-keys)))
|
|
2037
|
|
2038 ;;; The `link' Widget.
|
|
2039
|
|
2040 (defcustom widget-link-prefix "["
|
|
2041 "String used as prefix for links."
|
|
2042 :type 'string
|
|
2043 :group 'widget-button)
|
|
2044
|
|
2045 (defcustom widget-link-suffix "]"
|
|
2046 "String used as suffix for links."
|
|
2047 :type 'string
|
|
2048 :group 'widget-button)
|
|
2049
|
|
2050 (define-widget 'link 'item
|
|
2051 "An embedded link."
|
|
2052 :button-prefix 'widget-link-prefix
|
|
2053 :button-suffix 'widget-link-suffix
|
1173
|
2054 :help-echo "Follow the link."
|
428
|
2055 :format "%[%t%]")
|
|
2056
|
|
2057 ;;; The `info-link' Widget.
|
|
2058
|
|
2059 (define-widget 'info-link 'link
|
|
2060 "A link to an info file."
|
|
2061 :help-echo 'widget-info-link-help-echo
|
|
2062 :action 'widget-info-link-action)
|
|
2063
|
|
2064 (defun widget-info-link-help-echo (widget)
|
|
2065 (concat "Read the manual entry `" (widget-value widget) "'"))
|
|
2066
|
|
2067 (defun widget-info-link-action (widget &optional event)
|
|
2068 "Open the info node specified by WIDGET."
|
|
2069 (Info-goto-node (widget-value widget)))
|
|
2070
|
|
2071 ;;; The `url-link' Widget.
|
|
2072
|
|
2073 (define-widget 'url-link 'link
|
|
2074 "A link to an www page."
|
|
2075 :help-echo 'widget-url-link-help-echo
|
|
2076 :action 'widget-url-link-action)
|
|
2077
|
|
2078 (defun widget-url-link-help-echo (widget)
|
|
2079 (concat "Visit <URL:" (widget-value widget) ">"))
|
|
2080
|
|
2081 (defun widget-url-link-action (widget &optional event)
|
|
2082 "Open the url specified by WIDGET."
|
1333
|
2083 (if-fboundp 'browse-url
|
442
|
2084 (browse-url (widget-value widget))
|
1173
|
2085 ;; #### Should subclass a 'missing-package error.
|
1309
|
2086 (error 'unimplemented
|
|
2087 "No `browse-url' package; cannot follow URLs in this XEmacs")))
|
428
|
2088
|
|
2089 ;;; The `function-link' Widget.
|
|
2090
|
|
2091 (define-widget 'function-link 'link
|
|
2092 "A link to an Emacs function."
|
|
2093 :action 'widget-function-link-action)
|
|
2094
|
|
2095 (defun widget-function-link-action (widget &optional event)
|
|
2096 "Show the function specified by WIDGET."
|
|
2097 (describe-function (widget-value widget)))
|
|
2098
|
|
2099 ;;; The `variable-link' Widget.
|
|
2100
|
|
2101 (define-widget 'variable-link 'link
|
|
2102 "A link to an Emacs variable."
|
|
2103 :action 'widget-variable-link-action)
|
|
2104
|
|
2105 (defun widget-variable-link-action (widget &optional event)
|
|
2106 "Show the variable specified by WIDGET."
|
|
2107 (describe-variable (widget-value widget)))
|
|
2108
|
|
2109 ;;; The `file-link' Widget.
|
|
2110
|
|
2111 (define-widget 'file-link 'link
|
|
2112 "A link to a file."
|
|
2113 :action 'widget-file-link-action)
|
|
2114
|
|
2115 (defun widget-file-link-action (widget &optional event)
|
|
2116 "Find the file specified by WIDGET."
|
|
2117 (find-file (widget-value widget)))
|
|
2118
|
|
2119 ;;; The `emacs-library-link' Widget.
|
|
2120
|
|
2121 (define-widget 'emacs-library-link 'link
|
|
2122 "A link to an Emacs Lisp library file."
|
|
2123 :help-echo 'widget-emacs-library-link-help-echo
|
|
2124 :action 'widget-emacs-library-link-action)
|
|
2125
|
|
2126 (defun widget-emacs-library-link-help-echo (widget)
|
|
2127 (concat "Visit " (widget-value widget)))
|
|
2128
|
|
2129 (defun widget-emacs-library-link-action (widget &optional event)
|
|
2130 "Find the Emacs Library file specified by WIDGET."
|
|
2131 (find-file (locate-library (widget-value widget))))
|
|
2132
|
|
2133 ;;; The `emacs-commentary-link' Widget.
|
|
2134
|
|
2135 (define-widget 'emacs-commentary-link 'link
|
|
2136 "A link to Commentary in an Emacs Lisp library file."
|
|
2137 :action 'widget-emacs-commentary-link-action)
|
|
2138
|
|
2139 (defun widget-emacs-commentary-link-action (widget &optional event)
|
|
2140 "Find the Commentary section of the Emacs file specified by WIDGET."
|
|
2141 (finder-commentary (widget-value widget)))
|
|
2142
|
|
2143 ;;; The `editable-field' Widget.
|
|
2144
|
|
2145 (define-widget 'editable-field 'default
|
|
2146 "An editable text field."
|
|
2147 :convert-widget 'widget-value-convert-widget
|
|
2148 :keymap widget-field-keymap
|
|
2149 :format "%v"
|
1309
|
2150 :help-echo "M-TAB: complete field; RET: enter value"
|
428
|
2151 :value ""
|
|
2152 :prompt-internal 'widget-field-prompt-internal
|
|
2153 :prompt-history 'widget-field-history
|
|
2154 :prompt-value 'widget-field-prompt-value
|
|
2155 :action 'widget-field-action
|
|
2156 :validate 'widget-field-validate
|
|
2157 :valid-regexp ""
|
1309
|
2158 :error "Field's value doesn't match allowed forms"
|
428
|
2159 :value-create 'widget-field-value-create
|
|
2160 :value-delete 'widget-field-value-delete
|
|
2161 :value-get 'widget-field-value-get
|
|
2162 :match 'widget-field-match)
|
|
2163
|
|
2164 (defvar widget-field-history nil
|
|
2165 "History of field minibuffer edits.")
|
|
2166
|
|
2167 (defun widget-field-prompt-internal (widget prompt initial history)
|
1173
|
2168 "Read string for WIDGET prompting with PROMPT.
|
|
2169 INITIAL is the initial input and HISTORY is a symbol containing
|
|
2170 the earlier input."
|
428
|
2171 (read-string prompt initial history))
|
|
2172
|
|
2173 (defun widget-field-prompt-value (widget prompt value unbound)
|
1173
|
2174 "Prompt for a string."
|
1309
|
2175 (widget-apply widget
|
|
2176 :value-to-external
|
|
2177 (widget-apply widget
|
|
2178 :prompt-internal prompt
|
|
2179 (unless unbound
|
|
2180 (cons (widget-apply widget
|
|
2181 :value-to-internal value)
|
|
2182 0))
|
|
2183 (widget-get widget :prompt-history))))
|
428
|
2184
|
|
2185 (defvar widget-edit-functions nil)
|
|
2186
|
|
2187 (defun widget-field-action (widget &optional event)
|
|
2188 ;; Edit the value in the minibuffer.
|
|
2189 (let* ((invalid (widget-apply widget :validate))
|
|
2190 (prompt (concat (widget-apply widget :menu-tag-get) ": "))
|
|
2191 (value (unless invalid
|
|
2192 (widget-value widget)))
|
|
2193 (answer (widget-apply widget :prompt-value prompt value invalid)))
|
|
2194 (unless (equal value answer)
|
|
2195 ;; This is a hack. We can't properly validate the widget
|
|
2196 ;; because validation requires the new value to be in the field.
|
|
2197 ;; However, widget-field-value-create will not function unless
|
|
2198 ;; the new value matches. So, we check whether the thing
|
|
2199 ;; matches, and if it does, use either the real or a dummy error
|
|
2200 ;; message.
|
|
2201 (unless (widget-apply widget :match answer)
|
|
2202 (let ((error-message (or (widget-get widget :type-error)
|
|
2203 "Invalid field contents")))
|
|
2204 (widget-put widget :error error-message)
|
|
2205 (error error-message)))
|
|
2206 (widget-value-set widget answer)
|
|
2207 (widget-apply widget :notify widget event)
|
|
2208 (widget-setup))
|
|
2209 (run-hook-with-args 'widget-edit-functions widget)))
|
|
2210
|
|
2211 ;(defun widget-field-action (widget &optional event)
|
|
2212 ; ;; Move to next field.
|
|
2213 ; (widget-forward 1)
|
|
2214 ; (run-hook-with-args 'widget-edit-functions widget))
|
|
2215
|
|
2216 (defun widget-field-validate (widget)
|
1173
|
2217 "Valid if the content matches `:valid-regexp'."
|
1309
|
2218 (save-excursion ; XEmacs
|
|
2219 (unless (string-match (widget-get widget :valid-regexp)
|
|
2220 (widget-apply widget :value-get))
|
|
2221 widget)))
|
428
|
2222
|
|
2223 (defun widget-field-value-create (widget)
|
1173
|
2224 "Create an editable text field."
|
428
|
2225 (let ((size (widget-get widget :size))
|
|
2226 (value (widget-get widget :value))
|
|
2227 (from (point))
|
|
2228 ;; This is changed to a real extent in `widget-setup'. We
|
|
2229 ;; need the end points to behave differently until
|
|
2230 ;; `widget-setup' is called. Should probably be replaced with
|
|
2231 ;; a genuine extent, but some things break, then.
|
|
2232 (extent (cons (make-marker) (make-marker))))
|
|
2233 (widget-put widget :field-extent extent)
|
|
2234 (insert value)
|
|
2235 (and size
|
|
2236 (< (length value) size)
|
|
2237 (insert-char ?\ (- size (length value))))
|
|
2238 (unless (memq widget widget-field-list)
|
|
2239 (push widget widget-field-new))
|
|
2240 (move-marker (cdr extent) (point))
|
|
2241 (set-marker-insertion-type (cdr extent) nil)
|
|
2242 (when (null size)
|
|
2243 (insert ?\n))
|
|
2244 (move-marker (car extent) from)
|
|
2245 (set-marker-insertion-type (car extent) t)))
|
|
2246
|
|
2247 (defun widget-field-value-delete (widget)
|
1173
|
2248 "Remove the widget from the list of active editing fields."
|
428
|
2249 (setq widget-field-list (delq widget widget-field-list))
|
|
2250 ;; These are nil if the :format string doesn't contain `%v'.
|
|
2251 (let ((extent (widget-get widget :field-extent)))
|
|
2252 (when extent
|
|
2253 (detach-extent extent))))
|
|
2254
|
|
2255 (defun widget-field-value-get (widget)
|
1173
|
2256 "Return current text in editing field."
|
428
|
2257 (let ((from (widget-field-start widget))
|
|
2258 (to (widget-field-end widget))
|
|
2259 (buffer (widget-field-buffer widget))
|
|
2260 (size (widget-get widget :size))
|
|
2261 (secret (widget-get widget :secret))
|
|
2262 (old (current-buffer)))
|
|
2263 (cond
|
|
2264 ((and from to)
|
|
2265 (set-buffer buffer)
|
|
2266 (while (and size
|
|
2267 (not (zerop size))
|
|
2268 (> to from)
|
|
2269 (eq (char-after (1- to)) ?\ ))
|
|
2270 (setq to (1- to)))
|
|
2271 (let ((result (buffer-substring-no-properties from to)))
|
|
2272 (when secret
|
|
2273 (let ((index 0))
|
|
2274 (while (< (+ from index) to)
|
|
2275 (aset result index
|
|
2276 (get-char-property (+ from index) 'secret))
|
|
2277 (incf index))))
|
|
2278 (set-buffer old)
|
|
2279 result))
|
|
2280 (t
|
|
2281 (widget-get widget :value)))))
|
|
2282
|
|
2283 (defun widget-field-match (widget value)
|
|
2284 ;; Match any string.
|
|
2285 (stringp value))
|
|
2286
|
|
2287 ;;; The `text' Widget.
|
|
2288
|
|
2289 (define-widget 'text 'editable-field
|
1309
|
2290 "A multiline text area."
|
|
2291 :keymap widget-text-keymap)
|
428
|
2292
|
|
2293 ;;; The `menu-choice' Widget.
|
|
2294
|
|
2295 (define-widget 'menu-choice 'default
|
|
2296 "A menu of options."
|
|
2297 :convert-widget 'widget-types-convert-widget
|
|
2298 :format "%[%t%]: %v"
|
|
2299 :case-fold t
|
|
2300 :tag "choice"
|
|
2301 :void '(item :format "invalid (%t)\n")
|
|
2302 :value-create 'widget-choice-value-create
|
|
2303 :value-delete 'widget-children-value-delete
|
|
2304 :value-get 'widget-choice-value-get
|
|
2305 :value-inline 'widget-choice-value-inline
|
|
2306 :default-get 'widget-choice-default-get
|
|
2307 :mouse-down-action 'widget-choice-mouse-down-action
|
|
2308 :action 'widget-choice-action
|
|
2309 :error "Make a choice"
|
|
2310 :validate 'widget-choice-validate
|
|
2311 :match 'widget-choice-match
|
|
2312 :match-inline 'widget-choice-match-inline)
|
|
2313
|
|
2314 (defun widget-choice-value-create (widget)
|
1173
|
2315 "Insert the first choice that matches the value."
|
428
|
2316 (let ((value (widget-get widget :value))
|
|
2317 (args (widget-get widget :args))
|
|
2318 (explicit (widget-get widget :explicit-choice))
|
|
2319 current)
|
|
2320 (if explicit
|
|
2321 (progn
|
1173
|
2322 ;; If the user specified the choice for this value,
|
|
2323 ;; respect that choice as long as the value is the same.
|
428
|
2324 (widget-put widget :children (list (widget-create-child-value
|
|
2325 widget explicit value)))
|
|
2326 (widget-put widget :choice explicit))
|
|
2327 (while args
|
|
2328 (setq current (car args)
|
|
2329 args (cdr args))
|
|
2330 (when (widget-apply current :match value)
|
|
2331 (widget-put widget :children (list (widget-create-child-value
|
|
2332 widget current value)))
|
|
2333 (widget-put widget :choice current)
|
|
2334 (setq args nil
|
|
2335 current nil)))
|
|
2336 (when current
|
|
2337 (let ((void (widget-get widget :void)))
|
|
2338 (widget-put widget :children (list (widget-create-child-and-convert
|
|
2339 widget void :value value)))
|
|
2340 (widget-put widget :choice void))))))
|
|
2341
|
|
2342 (defun widget-choice-value-get (widget)
|
|
2343 ;; Get value of the child widget.
|
|
2344 (widget-value (car (widget-get widget :children))))
|
|
2345
|
|
2346 (defun widget-choice-value-inline (widget)
|
|
2347 ;; Get value of the child widget.
|
|
2348 (widget-apply (car (widget-get widget :children)) :value-inline))
|
|
2349
|
|
2350 (defun widget-choice-default-get (widget)
|
|
2351 ;; Get default for the first choice.
|
|
2352 (widget-default-get (car (widget-get widget :args))))
|
|
2353
|
|
2354 (defcustom widget-choice-toggle nil
|
|
2355 "If non-nil, a binary choice will just toggle between the values.
|
|
2356 Otherwise, the user will explicitly have to choose between the values
|
|
2357 when he invoked the menu."
|
|
2358 :type 'boolean
|
|
2359 :group 'widgets)
|
|
2360
|
|
2361 (defun widget-choice-mouse-down-action (widget &optional event)
|
|
2362 ;; Return non-nil if we need a menu.
|
|
2363 (let ((args (widget-get widget :args))
|
|
2364 (old (widget-get widget :choice)))
|
|
2365 (cond ((not (console-on-window-system-p))
|
|
2366 ;; No place to pop up a menu.
|
|
2367 nil)
|
|
2368 ((< (length args) 2)
|
|
2369 ;; Empty or singleton list, just return the value.
|
|
2370 nil)
|
|
2371 ((> (length args) widget-menu-max-size)
|
|
2372 ;; Too long, prompt.
|
|
2373 nil)
|
|
2374 ((> (length args) 2)
|
|
2375 ;; Reasonable sized list, use menu.
|
|
2376 t)
|
|
2377 ((and widget-choice-toggle (memq old args))
|
|
2378 ;; We toggle.
|
|
2379 nil)
|
|
2380 (t
|
|
2381 ;; Ask which of the two.
|
|
2382 t))))
|
|
2383
|
|
2384 (defun widget-choice-action (widget &optional event)
|
|
2385 ;; Make a choice.
|
|
2386 (let ((args (widget-get widget :args))
|
|
2387 (old (widget-get widget :choice))
|
|
2388 (tag (widget-apply widget :menu-tag-get))
|
|
2389 (completion-ignore-case (widget-get widget :case-fold))
|
|
2390 current choices)
|
|
2391 ;; Remember old value.
|
|
2392 (if (and old (not (widget-apply widget :validate)))
|
|
2393 (let* ((external (widget-value widget))
|
|
2394 (internal (widget-apply old :value-to-internal external)))
|
|
2395 (widget-put old :value internal)))
|
|
2396 ;; Find new choice.
|
|
2397 (setq current
|
|
2398 (cond ((= (length args) 0)
|
|
2399 nil)
|
|
2400 ((= (length args) 1)
|
|
2401 (nth 0 args))
|
|
2402 ((and widget-choice-toggle
|
|
2403 (= (length args) 2)
|
|
2404 (memq old args))
|
|
2405 (if (eq old (nth 0 args))
|
|
2406 (nth 1 args)
|
|
2407 (nth 0 args)))
|
|
2408 (t
|
|
2409 (while args
|
|
2410 (setq current (car args)
|
|
2411 args (cdr args))
|
|
2412 (setq choices
|
|
2413 (cons (cons (widget-apply current :menu-tag-get)
|
|
2414 current)
|
|
2415 choices)))
|
|
2416 (let ((choice
|
|
2417 (widget-choose tag (reverse choices) event)))
|
|
2418 (widget-put widget :explicit-choice choice)
|
|
2419 choice))))
|
|
2420 (when current
|
|
2421 (let ((value (widget-default-get current)))
|
|
2422 (widget-value-set widget
|
|
2423 (widget-apply current :value-to-external value)))
|
|
2424 (widget-setup)
|
|
2425 (widget-apply widget :notify widget event)))
|
|
2426 (run-hook-with-args 'widget-edit-functions widget))
|
|
2427
|
|
2428 (defun widget-choice-validate (widget)
|
|
2429 ;; Valid if we have made a valid choice.
|
1309
|
2430 (if (eq (widget-get widget :void) (widget-get widget :choice))
|
|
2431 widget
|
|
2432 (widget-apply (car (widget-get widget :children)) :validate)))
|
428
|
2433
|
|
2434 (defun widget-choice-match (widget value)
|
|
2435 ;; Matches if one of the choices matches.
|
|
2436 (let ((args (widget-get widget :args))
|
|
2437 current found)
|
|
2438 (while (and args (not found))
|
|
2439 (setq current (car args)
|
|
2440 args (cdr args)
|
|
2441 found (widget-apply current :match value)))
|
|
2442 found))
|
|
2443
|
|
2444 (defun widget-choice-match-inline (widget values)
|
|
2445 ;; Matches if one of the choices matches.
|
|
2446 (let ((args (widget-get widget :args))
|
|
2447 current found)
|
|
2448 (while (and args (null found))
|
|
2449 (setq current (car args)
|
|
2450 args (cdr args)
|
|
2451 found (widget-match-inline current values)))
|
|
2452 found))
|
|
2453
|
|
2454 ;;; The `toggle' Widget.
|
|
2455
|
|
2456 (define-widget 'toggle 'item
|
|
2457 "Toggle between two states."
|
|
2458 :format "%[%v%]\n"
|
|
2459 :value-create 'widget-toggle-value-create
|
|
2460 :action 'widget-toggle-action
|
|
2461 :match (lambda (widget value) t)
|
|
2462 :on "on"
|
|
2463 :off "off")
|
|
2464
|
|
2465 (defun widget-toggle-value-create (widget)
|
1173
|
2466 "Insert text representing the `on' and `off' states."
|
428
|
2467 (if (widget-value widget)
|
|
2468 (widget-glyph-insert widget
|
|
2469 (widget-get widget :on)
|
|
2470 (widget-get widget :on-glyph))
|
|
2471 (widget-glyph-insert widget
|
|
2472 (widget-get widget :off)
|
|
2473 (widget-get widget :off-glyph))))
|
|
2474
|
|
2475 (defun widget-toggle-action (widget &optional event)
|
|
2476 ;; Toggle value.
|
|
2477 (widget-value-set widget (not (widget-value widget)))
|
|
2478 (widget-apply widget :notify widget event)
|
|
2479 (run-hook-with-args 'widget-edit-functions widget))
|
|
2480
|
|
2481 ;;; The `checkbox' Widget.
|
|
2482
|
|
2483 (define-widget 'checkbox 'toggle
|
|
2484 "A checkbox toggle."
|
|
2485 :button-suffix ""
|
|
2486 :button-prefix ""
|
|
2487 :format "%[%v%]"
|
|
2488 :on "[X]"
|
|
2489 :on-glyph "check1"
|
|
2490 :off "[ ]"
|
|
2491 :off-glyph "check0"
|
|
2492 :action 'widget-checkbox-action)
|
|
2493
|
|
2494 (defun widget-checkbox-action (widget &optional event)
|
|
2495 "Toggle checkbox, notify parent, and set active state of sibling."
|
|
2496 (widget-toggle-action widget event)
|
|
2497 (let ((sibling (widget-get-sibling widget)))
|
|
2498 (when sibling
|
|
2499 (if (widget-value widget)
|
|
2500 (widget-apply sibling :activate)
|
|
2501 (widget-apply sibling :deactivate)))))
|
|
2502
|
|
2503 ;;; The `checklist' Widget.
|
|
2504
|
|
2505 (define-widget 'checklist 'default
|
|
2506 "A multiple choice widget."
|
|
2507 :convert-widget 'widget-types-convert-widget
|
|
2508 :format "%v"
|
|
2509 :offset 4
|
|
2510 :entry-format "%b %v"
|
|
2511 :menu-tag "checklist"
|
|
2512 :greedy nil
|
|
2513 :value-create 'widget-checklist-value-create
|
|
2514 :value-delete 'widget-children-value-delete
|
|
2515 :value-get 'widget-checklist-value-get
|
|
2516 :validate 'widget-checklist-validate
|
|
2517 :match 'widget-checklist-match
|
|
2518 :match-inline 'widget-checklist-match-inline)
|
|
2519
|
|
2520 (defun widget-checklist-value-create (widget)
|
|
2521 ;; Insert all values
|
|
2522 (let ((alist (widget-checklist-match-find widget (widget-get widget :value)))
|
|
2523 (args (widget-get widget :args)))
|
|
2524 (while args
|
|
2525 (widget-checklist-add-item widget (car args) (assq (car args) alist))
|
|
2526 (setq args (cdr args)))
|
|
2527 (widget-put widget :children (nreverse (widget-get widget :children)))))
|
|
2528
|
|
2529 (defun widget-checklist-add-item (widget type chosen)
|
1173
|
2530 "Create checklist item in WIDGET of type TYPE.
|
|
2531 If the item is checked, CHOSEN is a cons whose cdr is the value."
|
428
|
2532 (and (eq (preceding-char) ?\n)
|
|
2533 (widget-get widget :indent)
|
|
2534 (insert-char ?\ (widget-get widget :indent)))
|
|
2535 (widget-specify-insert
|
|
2536 (let* ((children (widget-get widget :children))
|
|
2537 (buttons (widget-get widget :buttons))
|
|
2538 (button-args (or (widget-get type :sibling-args)
|
|
2539 (widget-get widget :button-args)))
|
|
2540 (from (point))
|
|
2541 child button)
|
|
2542 (insert (widget-get widget :entry-format))
|
|
2543 (goto-char from)
|
|
2544 ;; Parse % escapes in format.
|
|
2545 (while (re-search-forward "%\\([bv%]\\)" nil t)
|
|
2546 (let ((escape (aref (match-string 1) 0)))
|
|
2547 (replace-match "" t t)
|
|
2548 (cond ((eq escape ?%)
|
1309
|
2549 (insert ?%))
|
428
|
2550 ((eq escape ?b)
|
|
2551 (setq button (apply 'widget-create-child-and-convert
|
|
2552 widget 'checkbox
|
|
2553 :value (not (null chosen))
|
|
2554 button-args)))
|
|
2555 ((eq escape ?v)
|
|
2556 (setq child
|
|
2557 (cond ((not chosen)
|
|
2558 (let ((child (widget-create-child widget type)))
|
|
2559 (widget-apply child :deactivate)
|
|
2560 child))
|
|
2561 ((widget-get type :inline)
|
|
2562 (widget-create-child-value
|
|
2563 widget type (cdr chosen)))
|
|
2564 (t
|
|
2565 (widget-create-child-value
|
|
2566 widget type (car (cdr chosen)))))))
|
|
2567 (t
|
|
2568 (signal 'error (list "Unknown escape" escape))))))
|
|
2569 ;; Update properties.
|
|
2570 (and button child (widget-put child :button button))
|
|
2571 (and button (widget-put widget :buttons (cons button buttons)))
|
|
2572 (and child (widget-put widget :children (cons child children))))))
|
|
2573
|
|
2574 (defun widget-checklist-match (widget values)
|
|
2575 ;; All values must match a type in the checklist.
|
|
2576 (and (listp values)
|
|
2577 (null (cdr (widget-checklist-match-inline widget values)))))
|
|
2578
|
|
2579 (defun widget-checklist-match-inline (widget values)
|
|
2580 ;; Find the values which match a type in the checklist.
|
|
2581 (let ((greedy (widget-get widget :greedy))
|
|
2582 (args (copy-sequence (widget-get widget :args)))
|
|
2583 found rest)
|
|
2584 (while values
|
|
2585 (let ((answer (widget-checklist-match-up args values)))
|
|
2586 (cond (answer
|
|
2587 (let ((vals (widget-match-inline answer values)))
|
|
2588 (setq found (append found (car vals))
|
|
2589 values (cdr vals)
|
|
2590 args (delq answer args))))
|
|
2591 (greedy
|
|
2592 (setq rest (append rest (list (car values)))
|
|
2593 values (cdr values)))
|
|
2594 (t
|
|
2595 (setq rest (append rest values)
|
|
2596 values nil)))))
|
|
2597 (cons found rest)))
|
|
2598
|
|
2599 (defun widget-checklist-match-find (widget vals)
|
1173
|
2600 "Find the vals which match a type in the checklist.
|
|
2601 Return an alist of (TYPE MATCH)."
|
428
|
2602 (let ((greedy (widget-get widget :greedy))
|
|
2603 (args (copy-sequence (widget-get widget :args)))
|
|
2604 found)
|
|
2605 (while vals
|
|
2606 (let ((answer (widget-checklist-match-up args vals)))
|
|
2607 (cond (answer
|
|
2608 (let ((match (widget-match-inline answer vals)))
|
|
2609 (setq found (cons (cons answer (car match)) found)
|
|
2610 vals (cdr match)
|
|
2611 args (delq answer args))))
|
|
2612 (greedy
|
|
2613 (setq vals (cdr vals)))
|
|
2614 (t
|
|
2615 (setq vals nil)))))
|
|
2616 found))
|
|
2617
|
|
2618 (defun widget-checklist-match-up (args vals)
|
1173
|
2619 "Return the first type from ARGS that matches VALS."
|
428
|
2620 (let (current found)
|
|
2621 (while (and args (null found))
|
|
2622 (setq current (car args)
|
|
2623 args (cdr args)
|
|
2624 found (widget-match-inline current vals)))
|
|
2625 (if found
|
|
2626 current
|
|
2627 nil)))
|
|
2628
|
|
2629 (defun widget-checklist-value-get (widget)
|
|
2630 ;; The values of all selected items.
|
|
2631 (let ((children (widget-get widget :children))
|
|
2632 child result)
|
|
2633 (while children
|
|
2634 (setq child (car children)
|
|
2635 children (cdr children))
|
|
2636 (if (widget-value (widget-get child :button))
|
|
2637 (setq result (append result (widget-apply child :value-inline)))))
|
|
2638 result))
|
|
2639
|
|
2640 (defun widget-checklist-validate (widget)
|
440
|
2641 ;; Ticked children must be valid.
|
428
|
2642 (let ((children (widget-get widget :children))
|
|
2643 child button found)
|
|
2644 (while (and children (not found))
|
|
2645 (setq child (car children)
|
|
2646 children (cdr children)
|
|
2647 button (widget-get child :button)
|
|
2648 found (and (widget-value button)
|
|
2649 (widget-apply child :validate))))
|
|
2650 found))
|
|
2651
|
|
2652 ;;; The `option' Widget
|
|
2653
|
|
2654 (define-widget 'option 'checklist
|
|
2655 "An widget with an optional item."
|
|
2656 :inline t)
|
|
2657
|
|
2658 ;;; The `choice-item' Widget.
|
|
2659
|
|
2660 (define-widget 'choice-item 'item
|
|
2661 "Button items that delegate action events to their parents."
|
|
2662 :action 'widget-parent-action
|
|
2663 :format "%[%t%] \n")
|
|
2664
|
|
2665 ;;; The `radio-button' Widget.
|
|
2666
|
|
2667 (define-widget 'radio-button 'toggle
|
|
2668 "A radio button for use in the `radio' widget."
|
|
2669 :notify 'widget-radio-button-notify
|
|
2670 :format "%[%v%]"
|
|
2671 :button-suffix ""
|
|
2672 :button-prefix ""
|
|
2673 :on "(*)"
|
|
2674 :on-glyph '("radio1" nil "radio0")
|
|
2675 :off "( )"
|
|
2676 :off-glyph "radio0")
|
|
2677
|
|
2678 (defun widget-radio-button-notify (widget child &optional event)
|
|
2679 ;; Tell daddy.
|
|
2680 (widget-apply (widget-get widget :parent) :action widget event))
|
|
2681
|
|
2682 ;;; The `radio-button-choice' Widget.
|
|
2683
|
|
2684 (define-widget 'radio-button-choice 'default
|
|
2685 "Select one of multiple options."
|
|
2686 :convert-widget 'widget-types-convert-widget
|
|
2687 :offset 4
|
|
2688 :format "%v"
|
|
2689 :entry-format "%b %v"
|
|
2690 :menu-tag "radio"
|
|
2691 :value-create 'widget-radio-value-create
|
|
2692 :value-delete 'widget-children-value-delete
|
|
2693 :value-get 'widget-radio-value-get
|
|
2694 :value-inline 'widget-radio-value-inline
|
|
2695 :value-set 'widget-radio-value-set
|
|
2696 :error "You must push one of the buttons"
|
|
2697 :validate 'widget-radio-validate
|
|
2698 :match 'widget-choice-match
|
|
2699 :match-inline 'widget-choice-match-inline
|
|
2700 :action 'widget-radio-action)
|
|
2701
|
|
2702 (defun widget-radio-value-create (widget)
|
|
2703 ;; Insert all values
|
|
2704 (let ((args (widget-get widget :args))
|
|
2705 arg)
|
|
2706 (while args
|
|
2707 (setq arg (car args)
|
|
2708 args (cdr args))
|
|
2709 (widget-radio-add-item widget arg))))
|
|
2710
|
|
2711 (defun widget-radio-add-item (widget type)
|
|
2712 "Add to radio widget WIDGET a new radio button item of type TYPE."
|
|
2713 ;; (setq type (widget-convert type))
|
|
2714 (and (eq (preceding-char) ?\n)
|
|
2715 (widget-get widget :indent)
|
|
2716 (insert-char ?\ (widget-get widget :indent)))
|
|
2717 (widget-specify-insert
|
|
2718 (let* ((value (widget-get widget :value))
|
|
2719 (children (widget-get widget :children))
|
|
2720 (buttons (widget-get widget :buttons))
|
|
2721 (button-args (or (widget-get type :sibling-args)
|
|
2722 (widget-get widget :button-args)))
|
|
2723 (from (point))
|
|
2724 (chosen (and (null (widget-get widget :choice))
|
|
2725 (widget-apply type :match value)))
|
|
2726 child button)
|
|
2727 (insert (widget-get widget :entry-format))
|
|
2728 (goto-char from)
|
|
2729 ;; Parse % escapes in format.
|
|
2730 (while (re-search-forward "%\\([bv%]\\)" nil t)
|
|
2731 (let ((escape (aref (match-string 1) 0)))
|
|
2732 (replace-match "" t t)
|
|
2733 (cond ((eq escape ?%)
|
1309
|
2734 (insert ?%))
|
428
|
2735 ((eq escape ?b)
|
|
2736 (setq button (apply 'widget-create-child-and-convert
|
|
2737 widget 'radio-button
|
|
2738 :value (not (null chosen))
|
|
2739 button-args)))
|
|
2740 ((eq escape ?v)
|
|
2741 (setq child (if chosen
|
|
2742 (widget-create-child-value
|
|
2743 widget type value)
|
|
2744 (widget-create-child widget type)))
|
|
2745 (unless chosen
|
|
2746 (widget-apply child :deactivate)))
|
|
2747 (t
|
|
2748 (signal 'error (list "Unknown escape" escape))))))
|
|
2749 ;; Update properties.
|
|
2750 (when chosen
|
|
2751 (widget-put widget :choice type))
|
|
2752 (when button
|
|
2753 (widget-put child :button button)
|
|
2754 (widget-put widget :buttons (nconc buttons (list button))))
|
|
2755 (when child
|
|
2756 (widget-put widget :children (nconc children (list child))))
|
|
2757 child)))
|
|
2758
|
|
2759 (defun widget-radio-value-get (widget)
|
|
2760 ;; Get value of the child widget.
|
|
2761 (let ((chosen (widget-radio-chosen widget)))
|
|
2762 (and chosen (widget-value chosen))))
|
|
2763
|
|
2764 (defun widget-radio-chosen (widget)
|
|
2765 "Return the widget representing the chosen radio button."
|
|
2766 (let ((children (widget-get widget :children))
|
|
2767 current found)
|
|
2768 (while children
|
|
2769 (setq current (car children)
|
|
2770 children (cdr children))
|
1309
|
2771 (when (widget-apply (widget-get current :button) :value-get)
|
|
2772 (setq found current
|
|
2773 children nil)))
|
428
|
2774 found))
|
|
2775
|
|
2776 (defun widget-radio-value-inline (widget)
|
|
2777 ;; Get value of the child widget.
|
|
2778 (let ((children (widget-get widget :children))
|
|
2779 current found)
|
|
2780 (while children
|
|
2781 (setq current (car children)
|
|
2782 children (cdr children))
|
1309
|
2783 (when (widget-apply (widget-get current :button) :value-get)
|
|
2784 (setq found (widget-apply current :value-inline)
|
|
2785 children nil)))
|
428
|
2786 found))
|
|
2787
|
|
2788 (defun widget-radio-value-set (widget value)
|
|
2789 ;; We can't just delete and recreate a radio widget, since children
|
|
2790 ;; can be added after the original creation and won't be recreated
|
|
2791 ;; by `:create'.
|
|
2792 (let ((children (widget-get widget :children))
|
|
2793 current found)
|
|
2794 (while children
|
|
2795 (setq current (car children)
|
|
2796 children (cdr children))
|
|
2797 (let* ((button (widget-get current :button))
|
|
2798 (match (and (not found)
|
|
2799 (widget-apply current :match value))))
|
|
2800 (widget-value-set button match)
|
|
2801 (if match
|
|
2802 (progn
|
|
2803 (widget-value-set current value)
|
|
2804 (widget-apply current :activate))
|
|
2805 (widget-apply current :deactivate))
|
|
2806 (setq found (or found match))))))
|
|
2807
|
|
2808 (defun widget-radio-validate (widget)
|
|
2809 ;; Valid if we have made a valid choice.
|
|
2810 (let ((children (widget-get widget :children))
|
|
2811 current found button)
|
|
2812 (while (and children (not found))
|
|
2813 (setq current (car children)
|
|
2814 children (cdr children)
|
|
2815 button (widget-get current :button)
|
|
2816 found (widget-apply button :value-get)))
|
|
2817 (if found
|
|
2818 (widget-apply current :validate)
|
|
2819 widget)))
|
|
2820
|
|
2821 (defun widget-radio-action (widget child event)
|
|
2822 ;; Check if a radio button was pressed.
|
|
2823 (let ((children (widget-get widget :children))
|
|
2824 (buttons (widget-get widget :buttons))
|
|
2825 current)
|
|
2826 (when (memq child buttons)
|
|
2827 (while children
|
|
2828 (setq current (car children)
|
|
2829 children (cdr children))
|
|
2830 (let* ((button (widget-get current :button)))
|
|
2831 (cond ((eq child button)
|
|
2832 (widget-value-set button t)
|
|
2833 (widget-apply current :activate))
|
|
2834 ((widget-value button)
|
|
2835 (widget-value-set button nil)
|
|
2836 (widget-apply current :deactivate)))))))
|
|
2837 ;; Pass notification to parent.
|
|
2838 (widget-apply widget :notify child event))
|
|
2839
|
|
2840 ;;; The `insert-button' Widget.
|
|
2841
|
|
2842 (define-widget 'insert-button 'push-button
|
|
2843 "An insert button for the `editable-list' widget."
|
|
2844 :tag "INS"
|
1173
|
2845 :help-echo "Insert a new item into the list at this position."
|
428
|
2846 :action 'widget-insert-button-action)
|
|
2847
|
|
2848 (defun widget-insert-button-action (widget &optional event)
|
|
2849 ;; Ask the parent to insert a new item.
|
|
2850 (widget-apply (widget-get widget :parent)
|
|
2851 :insert-before (widget-get widget :widget)))
|
|
2852
|
|
2853 ;;; The `delete-button' Widget.
|
|
2854
|
|
2855 (define-widget 'delete-button 'push-button
|
|
2856 "A delete button for the `editable-list' widget."
|
|
2857 :tag "DEL"
|
1173
|
2858 :help-echo "Delete this item from the list."
|
428
|
2859 :action 'widget-delete-button-action)
|
|
2860
|
|
2861 (defun widget-delete-button-action (widget &optional event)
|
|
2862 ;; Ask the parent to insert a new item.
|
|
2863 (widget-apply (widget-get widget :parent)
|
|
2864 :delete-at (widget-get widget :widget)))
|
|
2865
|
|
2866 ;;; The `editable-list' Widget.
|
|
2867
|
|
2868 (defcustom widget-editable-list-gui nil
|
|
2869 "If non nil, use GUI push-buttons in editable list when available."
|
|
2870 :type 'boolean
|
|
2871 :group 'widgets)
|
|
2872
|
|
2873 (define-widget 'editable-list 'default
|
|
2874 "A variable list of widgets of the same type."
|
|
2875 :convert-widget 'widget-types-convert-widget
|
|
2876 :offset 12
|
|
2877 :format "%v%i\n"
|
|
2878 :format-handler 'widget-editable-list-format-handler
|
|
2879 :entry-format "%i %d %v"
|
|
2880 :menu-tag "editable-list"
|
|
2881 :value-create 'widget-editable-list-value-create
|
|
2882 :value-delete 'widget-children-value-delete
|
|
2883 :value-get 'widget-editable-list-value-get
|
|
2884 :validate 'widget-children-validate
|
|
2885 :match 'widget-editable-list-match
|
|
2886 :match-inline 'widget-editable-list-match-inline
|
|
2887 :insert-before 'widget-editable-list-insert-before
|
|
2888 :delete-at 'widget-editable-list-delete-at)
|
|
2889
|
|
2890 (defun widget-editable-list-format-handler (widget escape)
|
|
2891 ;; We recognize the insert button.
|
|
2892 (let ((widget-push-button-gui widget-editable-list-gui))
|
|
2893 (cond ((eq escape ?i)
|
|
2894 (and (widget-get widget :indent)
|
|
2895 (insert-char ?\ (widget-get widget :indent)))
|
|
2896 (apply 'widget-create-child-and-convert
|
|
2897 widget 'insert-button
|
|
2898 (widget-get widget :append-button-args)))
|
|
2899 (t
|
|
2900 (widget-default-format-handler widget escape)))))
|
|
2901
|
|
2902 (defun widget-editable-list-value-create (widget)
|
|
2903 ;; Insert all values
|
|
2904 (let* ((value (widget-get widget :value))
|
|
2905 (type (nth 0 (widget-get widget :args)))
|
|
2906 children)
|
|
2907 (widget-put widget :value-pos (copy-marker (point)))
|
|
2908 (set-marker-insertion-type (widget-get widget :value-pos) t)
|
|
2909 (while value
|
|
2910 (let ((answer (widget-match-inline type value)))
|
|
2911 (if answer
|
|
2912 (setq children (cons (widget-editable-list-entry-create
|
|
2913 widget
|
1309
|
2914 (if (widget-get type :inline)
|
428
|
2915 (car answer)
|
|
2916 (car (car answer)))
|
|
2917 t)
|
|
2918 children)
|
|
2919 value (cdr answer))
|
|
2920 (setq value nil))))
|
|
2921 (widget-put widget :children (nreverse children))))
|
|
2922
|
|
2923 (defun widget-editable-list-value-get (widget)
|
|
2924 ;; Get value of the child widget.
|
|
2925 (apply 'append (mapcar (lambda (child) (widget-apply child :value-inline))
|
|
2926 (widget-get widget :children))))
|
|
2927
|
|
2928 (defun widget-editable-list-match (widget value)
|
|
2929 ;; Value must be a list and all the members must match the type.
|
|
2930 (and (listp value)
|
|
2931 (null (cdr (widget-editable-list-match-inline widget value)))))
|
|
2932
|
|
2933 (defun widget-editable-list-match-inline (widget value)
|
|
2934 (let ((type (nth 0 (widget-get widget :args)))
|
|
2935 (ok t)
|
|
2936 found)
|
|
2937 (while (and value ok)
|
|
2938 (let ((answer (widget-match-inline type value)))
|
|
2939 (if answer
|
|
2940 (setq found (append found (car answer))
|
|
2941 value (cdr answer))
|
|
2942 (setq ok nil))))
|
|
2943 (cons found value)))
|
|
2944
|
|
2945 (defun widget-editable-list-insert-before (widget before)
|
|
2946 ;; Insert a new child in the list of children.
|
|
2947 (save-excursion
|
|
2948 (let ((children (widget-get widget :children))
|
|
2949 (inhibit-read-only t)
|
|
2950 before-change-functions
|
|
2951 after-change-functions)
|
|
2952 (cond (before
|
|
2953 (goto-char (widget-get before :entry-from)))
|
|
2954 (t
|
|
2955 (goto-char (widget-get widget :value-pos))))
|
|
2956 (let ((child (widget-editable-list-entry-create
|
|
2957 widget nil nil)))
|
|
2958 (when (< (widget-get child :entry-from) (widget-get widget :from))
|
|
2959 (set-marker (widget-get widget :from)
|
|
2960 (widget-get child :entry-from)))
|
|
2961 (if (eq (car children) before)
|
|
2962 (widget-put widget :children (cons child children))
|
|
2963 (while (not (eq (car (cdr children)) before))
|
|
2964 (setq children (cdr children)))
|
|
2965 (setcdr children (cons child (cdr children)))))))
|
|
2966 (widget-setup)
|
|
2967 (widget-apply widget :notify widget))
|
|
2968
|
|
2969 (defun widget-editable-list-delete-at (widget child)
|
|
2970 ;; Delete child from list of children.
|
|
2971 (save-excursion
|
|
2972 (let ((buttons (copy-sequence (widget-get widget :buttons)))
|
|
2973 button
|
|
2974 (inhibit-read-only t)
|
|
2975 before-change-functions
|
|
2976 after-change-functions)
|
|
2977 (while buttons
|
|
2978 (setq button (car buttons)
|
|
2979 buttons (cdr buttons))
|
|
2980 (when (eq (widget-get button :widget) child)
|
|
2981 (widget-put widget
|
|
2982 :buttons (delq button (widget-get widget :buttons)))
|
|
2983 (widget-delete button))))
|
|
2984 (let ((entry-from (widget-get child :entry-from))
|
|
2985 (entry-to (widget-get child :entry-to))
|
|
2986 (inhibit-read-only t)
|
|
2987 before-change-functions
|
|
2988 after-change-functions)
|
|
2989 (widget-delete child)
|
|
2990 (delete-region entry-from entry-to)
|
|
2991 (set-marker entry-from nil)
|
|
2992 (set-marker entry-to nil))
|
|
2993 (widget-put widget :children (delq child (widget-get widget :children))))
|
|
2994 (widget-setup)
|
|
2995 (widget-apply widget :notify widget))
|
|
2996
|
|
2997 (defun widget-editable-list-entry-create (widget value conv)
|
|
2998 ;; Create a new entry to the list.
|
|
2999 (let ((type (nth 0 (widget-get widget :args)))
|
|
3000 (widget-push-button-gui widget-editable-list-gui)
|
|
3001 child delete insert)
|
|
3002 (widget-specify-insert
|
|
3003 (save-excursion
|
|
3004 (and (widget-get widget :indent)
|
|
3005 (insert-char ?\ (widget-get widget :indent)))
|
|
3006 (insert (widget-get widget :entry-format)))
|
|
3007 ;; Parse % escapes in format.
|
|
3008 (while (re-search-forward "%\\(.\\)" nil t)
|
|
3009 (let ((escape (aref (match-string 1) 0)))
|
|
3010 (replace-match "" t t)
|
|
3011 (cond ((eq escape ?%)
|
1309
|
3012 (insert ?%))
|
428
|
3013 ((eq escape ?i)
|
|
3014 (setq insert (apply 'widget-create-child-and-convert
|
|
3015 widget 'insert-button
|
|
3016 (widget-get widget :insert-button-args))))
|
|
3017 ((eq escape ?d)
|
|
3018 (setq delete (apply 'widget-create-child-and-convert
|
|
3019 widget 'delete-button
|
|
3020 (widget-get widget :delete-button-args))))
|
|
3021 ((eq escape ?v)
|
|
3022 (if conv
|
|
3023 (setq child (widget-create-child-value
|
|
3024 widget type value))
|
|
3025 (setq child (widget-create-child-value
|
|
3026 widget type (widget-default-get type)))))
|
|
3027 (t
|
|
3028 (signal 'error (list "Unknown escape" escape))))))
|
|
3029 (widget-put widget
|
|
3030 :buttons (cons delete
|
|
3031 (cons insert
|
|
3032 (widget-get widget :buttons))))
|
|
3033 (let ((entry-from (copy-marker (point-min)))
|
|
3034 (entry-to (copy-marker (point-max))))
|
|
3035 (set-marker-insertion-type entry-from t)
|
|
3036 (set-marker-insertion-type entry-to nil)
|
|
3037 (widget-put child :entry-from entry-from)
|
|
3038 (widget-put child :entry-to entry-to)))
|
|
3039 (widget-put insert :widget child)
|
|
3040 (widget-put delete :widget child)
|
|
3041 child))
|
|
3042
|
|
3043 ;;; The `group' Widget.
|
|
3044
|
|
3045 (define-widget 'group 'default
|
652
|
3046 "A widget which groups other widgets inside."
|
428
|
3047 :convert-widget 'widget-types-convert-widget
|
|
3048 :format "%v"
|
|
3049 :value-create 'widget-group-value-create
|
|
3050 :value-delete 'widget-children-value-delete
|
|
3051 :value-get 'widget-editable-list-value-get
|
|
3052 :default-get 'widget-group-default-get
|
|
3053 :validate 'widget-children-validate
|
|
3054 :match 'widget-group-match
|
|
3055 :match-inline 'widget-group-match-inline)
|
|
3056
|
|
3057 (defun widget-group-value-create (widget)
|
|
3058 ;; Create each component.
|
|
3059 (let ((args (widget-get widget :args))
|
|
3060 (value (widget-get widget :value))
|
|
3061 arg answer children)
|
|
3062 (while args
|
|
3063 (setq arg (car args)
|
|
3064 args (cdr args)
|
|
3065 answer (widget-match-inline arg value)
|
|
3066 value (cdr answer))
|
|
3067 (and (eq (preceding-char) ?\n)
|
|
3068 (widget-get widget :indent)
|
|
3069 (insert-char ?\ (widget-get widget :indent)))
|
|
3070 (push (cond ((null answer)
|
|
3071 (widget-create-child widget arg))
|
|
3072 ((widget-get arg :inline)
|
1173
|
3073 (widget-create-child-value widget arg (car answer)))
|
428
|
3074 (t
|
1173
|
3075 (widget-create-child-value widget arg (car (car answer)))))
|
428
|
3076 children))
|
|
3077 (widget-put widget :children (nreverse children))))
|
|
3078
|
|
3079 (defun widget-group-default-get (widget)
|
|
3080 ;; Get the default of the components.
|
|
3081 (mapcar 'widget-default-get (widget-get widget :args)))
|
|
3082
|
|
3083 (defun widget-group-match (widget values)
|
|
3084 ;; Match if the components match.
|
|
3085 (and (listp values)
|
|
3086 (let ((match (widget-group-match-inline widget values)))
|
|
3087 (and match (null (cdr match))))))
|
|
3088
|
|
3089 (defun widget-group-match-inline (widget vals)
|
|
3090 ;; Match if the components match.
|
|
3091 (let ((args (widget-get widget :args))
|
|
3092 argument answer found)
|
|
3093 (while args
|
|
3094 (setq argument (car args)
|
|
3095 args (cdr args)
|
|
3096 answer (widget-match-inline argument vals))
|
|
3097 (if answer
|
|
3098 (setq vals (cdr answer)
|
|
3099 found (append found (car answer)))
|
|
3100 (setq vals nil
|
|
3101 args nil)))
|
|
3102 (if answer
|
|
3103 (cons found vals)
|
|
3104 nil)))
|
|
3105
|
|
3106 ;;; The `visibility' Widget.
|
|
3107
|
|
3108 (define-widget 'visibility 'item
|
|
3109 "An indicator and manipulator for hidden items."
|
|
3110 :format "%[%v%]"
|
|
3111 :button-prefix ""
|
|
3112 :button-suffix ""
|
|
3113 :on "Hide"
|
|
3114 :off "Show"
|
|
3115 :value-create 'widget-visibility-value-create
|
|
3116 :action 'widget-toggle-action
|
|
3117 :match (lambda (widget value) t))
|
|
3118
|
|
3119 (defun widget-visibility-value-create (widget)
|
|
3120 ;; Insert text representing the `on' and `off' states.
|
|
3121 (let ((on (widget-get widget :on))
|
|
3122 (off (widget-get widget :off)))
|
|
3123 (if on
|
|
3124 (setq on (concat widget-push-button-prefix
|
|
3125 on
|
|
3126 widget-push-button-suffix))
|
|
3127 (setq on ""))
|
|
3128 (if off
|
|
3129 (setq off (concat widget-push-button-prefix
|
|
3130 off
|
|
3131 widget-push-button-suffix))
|
|
3132 (setq off ""))
|
|
3133 (if (widget-value widget)
|
|
3134 (widget-glyph-insert widget on '("down" "down-pushed"))
|
|
3135 (widget-glyph-insert widget off '("right" "right-pushed")))))
|
|
3136
|
|
3137 ;;; The `documentation-link' Widget.
|
|
3138 ;;
|
|
3139 ;; This is a helper widget for `documentation-string'.
|
|
3140
|
|
3141 (define-widget 'documentation-link 'link
|
|
3142 "Link type used in documentation strings."
|
|
3143 :tab-order -1
|
|
3144 :help-echo 'widget-documentation-link-echo-help
|
|
3145 :action 'widget-documentation-link-action)
|
|
3146
|
|
3147 (defun widget-documentation-link-echo-help (widget)
|
|
3148 "Tell what this link will describe."
|
|
3149 (concat "Describe the `" (widget-get widget :value) "' symbol."))
|
|
3150
|
|
3151 (defun widget-documentation-link-action (widget &optional event)
|
|
3152 "Display documentation for WIDGET's value. Ignore optional argument EVENT."
|
|
3153 (let* ((string (widget-get widget :value))
|
|
3154 (symbol (intern string)))
|
|
3155 (if (and (fboundp symbol) (boundp symbol))
|
|
3156 ;; If there are two doc strings, give the user a way to pick one.
|
|
3157 (apropos (concat "\\`" (regexp-quote string) "\\'"))
|
|
3158 (if (fboundp symbol)
|
|
3159 (describe-function symbol)
|
|
3160 (describe-variable symbol)))))
|
|
3161
|
|
3162 (defcustom widget-documentation-links t
|
|
3163 "Add hyperlinks to documentation strings when non-nil."
|
|
3164 :type 'boolean
|
|
3165 :group 'widget-documentation)
|
|
3166
|
|
3167 (defcustom widget-documentation-link-regexp "`\\([^\n`' ]+\\)'"
|
|
3168 "Regexp for matching potential links in documentation strings.
|
|
3169 The first group should be the link itself."
|
|
3170 :type 'regexp
|
|
3171 :group 'widget-documentation)
|
|
3172
|
|
3173 (defcustom widget-documentation-link-p 'intern-soft
|
|
3174 "Predicate used to test if a string is useful as a link.
|
|
3175 The value should be a function. The function will be called one
|
|
3176 argument, a string, and should return non-nil if there should be a
|
|
3177 link for that string."
|
|
3178 :type 'function
|
|
3179 :options '(widget-documentation-link-p)
|
|
3180 :group 'widget-documentation)
|
|
3181
|
|
3182 (defcustom widget-documentation-link-type 'documentation-link
|
|
3183 "Widget type used for links in documentation strings."
|
|
3184 :type 'symbol
|
|
3185 :group 'widget-documentation)
|
|
3186
|
|
3187 (defun widget-documentation-link-add (widget from to)
|
|
3188 (widget-specify-doc widget from to)
|
|
3189 (when widget-documentation-links
|
|
3190 (let ((regexp widget-documentation-link-regexp)
|
|
3191 (predicate widget-documentation-link-p)
|
|
3192 (type widget-documentation-link-type)
|
|
3193 (buttons (widget-get widget :buttons)))
|
|
3194 (save-excursion
|
|
3195 (goto-char from)
|
|
3196 (while (re-search-forward regexp to t)
|
|
3197 (let ((name (match-string 1))
|
|
3198 (begin (match-beginning 1))
|
|
3199 (end (match-end 1)))
|
|
3200 (when (funcall predicate name)
|
|
3201 (push (widget-convert-button type begin end :value name)
|
|
3202 buttons)))))
|
|
3203 (widget-put widget :buttons buttons)))
|
|
3204 (let ((indent (widget-get widget :indent)))
|
|
3205 (when (and indent (not (zerop indent)))
|
|
3206 (save-excursion
|
|
3207 (save-restriction
|
|
3208 (narrow-to-region from to)
|
|
3209 (goto-char (point-min))
|
|
3210 (while (search-forward "\n" nil t)
|
|
3211 (insert-char ?\ indent)))))))
|
|
3212
|
|
3213 ;;; The `documentation-string' Widget.
|
|
3214
|
|
3215 (define-widget 'documentation-string 'item
|
|
3216 "A documentation string."
|
|
3217 :format "%v"
|
|
3218 :action 'widget-documentation-string-action
|
|
3219 :value-delete 'widget-children-value-delete
|
|
3220 :value-create 'widget-documentation-string-value-create)
|
|
3221
|
|
3222 (defun widget-documentation-string-value-create (widget)
|
|
3223 ;; Insert documentation string.
|
|
3224 (let ((doc (widget-value widget))
|
|
3225 (indent (widget-get widget :indent))
|
|
3226 (shown (widget-get (widget-get widget :parent) :documentation-shown))
|
|
3227 (start (point)))
|
|
3228 (if (string-match "\n" doc)
|
|
3229 (let ((before (substring doc 0 (match-beginning 0)))
|
|
3230 (after (substring doc (match-beginning 0)))
|
|
3231 buttons)
|
1309
|
3232 (insert before ?\ )
|
428
|
3233 (widget-documentation-link-add widget start (point))
|
|
3234 (push (widget-create-child-and-convert
|
|
3235 widget 'visibility
|
|
3236 :help-echo (lambda (widget)
|
|
3237 (concat
|
|
3238 (if (widget-value widget)
|
|
3239 "Hide" "Show")
|
|
3240 " the rest of the documentation"))
|
|
3241 :off "More"
|
|
3242 :action 'widget-parent-action
|
|
3243 shown)
|
|
3244 buttons)
|
|
3245 (when shown
|
|
3246 (setq start (point))
|
|
3247 (when indent
|
|
3248 (insert-char ?\ indent))
|
|
3249 (insert after)
|
|
3250 (widget-documentation-link-add widget start (point)))
|
|
3251 (widget-put widget :buttons buttons))
|
|
3252 (insert doc)
|
|
3253 (widget-documentation-link-add widget start (point))))
|
1309
|
3254 (insert ?\n))
|
428
|
3255
|
|
3256 (defun widget-documentation-string-action (widget &rest ignore)
|
|
3257 ;; Toggle documentation.
|
|
3258 (let ((parent (widget-get widget :parent)))
|
|
3259 (widget-put parent :documentation-shown
|
|
3260 (not (widget-get parent :documentation-shown))))
|
|
3261 ;; Redraw.
|
|
3262 (widget-value-set widget (widget-value widget)))
|
|
3263
|
1309
|
3264
|
428
|
3265 ;;; The Sexp Widgets.
|
|
3266
|
|
3267 (define-widget 'const 'item
|
|
3268 "An immutable sexp."
|
|
3269 :prompt-value 'widget-const-prompt-value
|
|
3270 :format "%t\n%d")
|
|
3271
|
|
3272 (defun widget-const-prompt-value (widget prompt value unbound)
|
|
3273 ;; Return the value of the const.
|
|
3274 (widget-value widget))
|
|
3275
|
|
3276 (define-widget 'function-item 'const
|
|
3277 "An immutable function name."
|
|
3278 :format "%v\n%h"
|
|
3279 :documentation-property (lambda (symbol)
|
|
3280 (condition-case nil
|
|
3281 (documentation symbol t)
|
|
3282 (error nil))))
|
|
3283
|
|
3284 (define-widget 'variable-item 'const
|
|
3285 "An immutable variable name."
|
|
3286 :format "%v\n%h"
|
|
3287 :documentation-property 'variable-documentation)
|
|
3288
|
1309
|
3289 (define-widget 'other 'sexp
|
|
3290 "Matches any value, but doesn't let the user edit the value.
|
|
3291 This is useful as last item in a `choice' widget.
|
|
3292 You should use this widget type with a default value,
|
|
3293 as in (other DEFAULT) or (other :tag \"NAME\" DEFAULT).
|
|
3294 If the user selects this alternative, that specifies DEFAULT
|
|
3295 as the value."
|
|
3296 :tag "Other"
|
|
3297 :format "%t%n"
|
|
3298 :value 'other)
|
|
3299
|
428
|
3300 (defvar widget-string-prompt-value-history nil
|
|
3301 "History of input to `widget-string-prompt-value'.")
|
|
3302
|
|
3303 (define-widget 'string 'editable-field
|
|
3304 "A string"
|
|
3305 :tag "String"
|
|
3306 :format "%{%t%}: %v"
|
|
3307 :complete-function 'ispell-complete-word
|
|
3308 :prompt-history 'widget-string-prompt-value-history)
|
|
3309
|
|
3310 (define-widget 'regexp 'string
|
|
3311 "A regular expression."
|
|
3312 :match 'widget-regexp-match
|
|
3313 :validate 'widget-regexp-validate
|
|
3314 ;; Doesn't work well with terminating newline.
|
|
3315 ;; :value-face 'widget-single-line-field-face
|
|
3316 :tag "Regexp")
|
|
3317
|
|
3318 (defun widget-regexp-match (widget value)
|
|
3319 ;; Match valid regexps.
|
|
3320 (and (stringp value)
|
|
3321 (condition-case nil
|
|
3322 (prog1 t
|
|
3323 (string-match value ""))
|
|
3324 (error nil))))
|
|
3325
|
|
3326 (defun widget-regexp-validate (widget)
|
|
3327 "Check that the value of WIDGET is a valid regexp."
|
1309
|
3328 (condition-case data
|
|
3329 (prog1 nil
|
|
3330 (string-match (widget-value widget) ""))
|
|
3331 (error (widget-put widget :error (error-message-string data))
|
|
3332 widget)))
|
428
|
3333
|
|
3334 (define-widget 'file 'string
|
|
3335 "A file widget.
|
|
3336 It will read a file name from the minibuffer when invoked."
|
|
3337 :complete-function 'widget-file-complete
|
|
3338 :prompt-value 'widget-file-prompt-value
|
|
3339 :format "%{%t%}: %v"
|
|
3340 ;; Doesn't work well with terminating newline.
|
|
3341 ;; :value-face 'widget-single-line-field-face
|
|
3342 :tag "File")
|
|
3343
|
|
3344 (defun widget-file-complete ()
|
|
3345 "Perform completion on file name preceding point."
|
|
3346 (interactive)
|
|
3347 (let* ((end (point))
|
|
3348 (beg (save-excursion
|
|
3349 (skip-chars-backward "^ ")
|
|
3350 (point)))
|
|
3351 (pattern (buffer-substring beg end))
|
|
3352 (name-part (file-name-nondirectory pattern))
|
|
3353 (directory (file-name-directory pattern))
|
|
3354 (completion (file-name-completion name-part directory)))
|
|
3355 (cond ((eq completion t))
|
|
3356 ((null completion)
|
|
3357 (message "Can't find completion for \"%s\"" pattern)
|
|
3358 (ding))
|
|
3359 ((not (string= name-part completion))
|
|
3360 (delete-region beg end)
|
|
3361 (insert (expand-file-name completion directory)))
|
|
3362 (t
|
|
3363 (message "Making completion list...")
|
1309
|
3364 (with-output-to-temp-buffer "*Completions*"
|
|
3365 (display-completion-list
|
|
3366 (sort (file-name-all-completions name-part directory)
|
|
3367 'string<)))
|
428
|
3368 (message "Making completion list...%s" "done")))))
|
|
3369
|
|
3370 (defun widget-file-prompt-value (widget prompt value unbound)
|
|
3371 ;; Read file from minibuffer.
|
|
3372 (abbreviate-file-name
|
|
3373 (if unbound
|
|
3374 (read-file-name prompt)
|
|
3375 (let ((prompt2 (format "%s (default %s) " prompt value))
|
|
3376 (dir (file-name-directory value))
|
|
3377 (file (file-name-nondirectory value))
|
|
3378 (must-match (widget-get widget :must-match)))
|
|
3379 (read-file-name prompt2 dir nil must-match file)))))
|
|
3380
|
|
3381 ;;;(defun widget-file-action (widget &optional event)
|
|
3382 ;;; ;; Read a file name from the minibuffer.
|
|
3383 ;;; (let* ((value (widget-value widget))
|
|
3384 ;;; (dir (file-name-directory value))
|
|
3385 ;;; (file (file-name-nondirectory value))
|
|
3386 ;;; (menu-tag (widget-apply widget :menu-tag-get))
|
|
3387 ;;; (must-match (widget-get widget :must-match))
|
|
3388 ;;; (answer (read-file-name (concat menu-tag ": (default `" value "') ")
|
|
3389 ;;; dir nil must-match file)))
|
|
3390 ;;; (widget-value-set widget (abbreviate-file-name answer))
|
|
3391 ;;; (widget-setup)
|
|
3392 ;;; (widget-apply widget :notify widget event)))
|
|
3393
|
1173
|
3394 ;; Fixme: use file-name-as-directory.
|
428
|
3395 (define-widget 'directory 'file
|
|
3396 "A directory widget.
|
|
3397 It will read a directory name from the minibuffer when invoked."
|
|
3398 :tag "Directory")
|
|
3399
|
|
3400 (defvar widget-symbol-prompt-value-history nil
|
|
3401 "History of input to `widget-symbol-prompt-value'.")
|
|
3402
|
|
3403 (define-widget 'symbol 'editable-field
|
1173
|
3404 "A Lisp symbol."
|
428
|
3405 :value nil
|
|
3406 :tag "Symbol"
|
|
3407 :format "%{%t%}: %v"
|
|
3408 :match (lambda (widget value) (symbolp value))
|
|
3409 :complete-function 'lisp-complete-symbol
|
|
3410 :prompt-internal 'widget-symbol-prompt-internal
|
|
3411 :prompt-match 'symbolp
|
|
3412 :prompt-history 'widget-symbol-prompt-value-history
|
|
3413 :value-to-internal (lambda (widget value)
|
|
3414 (if (symbolp value)
|
|
3415 (symbol-name value)
|
|
3416 value))
|
|
3417 :value-to-external (lambda (widget value)
|
|
3418 (if (stringp value)
|
|
3419 (intern value)
|
|
3420 value)))
|
|
3421
|
|
3422 (defun widget-symbol-prompt-internal (widget prompt initial history)
|
|
3423 ;; Read file from minibuffer.
|
|
3424 (let ((answer (completing-read prompt obarray
|
|
3425 (widget-get widget :prompt-match)
|
|
3426 nil initial history)))
|
|
3427 (if (and (stringp answer)
|
|
3428 (not (zerop (length answer))))
|
|
3429 answer
|
|
3430 (error "No value"))))
|
|
3431
|
|
3432 (defvar widget-function-prompt-value-history nil
|
|
3433 "History of input to `widget-function-prompt-value'.")
|
|
3434
|
|
3435 (define-widget 'function 'sexp
|
1173
|
3436 "A Lisp function."
|
428
|
3437 :complete-function 'lisp-complete-symbol
|
|
3438 :prompt-value 'widget-field-prompt-value
|
|
3439 :prompt-internal 'widget-symbol-prompt-internal
|
|
3440 :prompt-match 'fboundp
|
|
3441 :prompt-history 'widget-function-prompt-value-history
|
|
3442 :action 'widget-field-action
|
|
3443 :tag "Function")
|
|
3444
|
|
3445 (defvar widget-variable-prompt-value-history nil
|
|
3446 "History of input to `widget-variable-prompt-value'.")
|
|
3447
|
|
3448 (define-widget 'variable 'symbol
|
|
3449 ;; Should complete on variables.
|
1173
|
3450 "A Lisp variable."
|
428
|
3451 :prompt-match 'boundp
|
|
3452 :prompt-history 'widget-variable-prompt-value-history
|
|
3453 :tag "Variable")
|
|
3454
|
|
3455 ;; This part issues a warning when compiling without Mule. Is there a
|
|
3456 ;; way of shutting it up?
|
|
3457 ;;
|
|
3458 ;; OK, I'll simply comment the whole thing out, until someone decides
|
|
3459 ;; to do something with it.
|
1309
|
3460
|
|
3461 ;; OK, _I_'ll simply comment it back in, so somebody will get irritated and
|
|
3462 ;; do something about it.
|
|
3463
|
|
3464 (defvar widget-coding-system-prompt-value-history nil
|
|
3465 "History of input to `widget-coding-system-prompt-value'.")
|
|
3466
|
|
3467 (define-widget 'coding-system 'symbol
|
|
3468 "A MULE coding-system."
|
|
3469 :format "%{%t%}: %v"
|
|
3470 :tag "Coding system"
|
|
3471 :prompt-history 'widget-coding-system-prompt-value-history
|
|
3472 :prompt-value 'widget-coding-system-prompt-value
|
|
3473 :action 'widget-coding-system-action)
|
|
3474
|
|
3475 (defun widget-coding-system-prompt-value (widget prompt value unbound)
|
|
3476 ;; Read coding-system from minibuffer.
|
|
3477 (intern
|
|
3478 (completing-read (format "%s (default %s) " prompt value)
|
|
3479 (mapcar (lambda (sym)
|
|
3480 (list (symbol-name sym)))
|
|
3481 (coding-system-list)))))
|
|
3482
|
|
3483 (defun widget-coding-system-action (widget &optional event)
|
|
3484 ;; Read a file name from the minibuffer.
|
|
3485 (let ((answer
|
|
3486 (widget-coding-system-prompt-value
|
|
3487 widget
|
|
3488 (widget-apply widget :menu-tag-get)
|
|
3489 (widget-value widget)
|
|
3490 t)))
|
|
3491 (widget-value-set widget answer)
|
|
3492 (widget-apply widget :notify widget event)
|
|
3493 (widget-setup)))
|
428
|
3494
|
|
3495 (define-widget 'sexp 'editable-field
|
1173
|
3496 "An arbitrary Lisp expression."
|
428
|
3497 :tag "Lisp expression"
|
|
3498 :format "%{%t%}: %v"
|
|
3499 :value nil
|
|
3500 :validate 'widget-sexp-validate
|
|
3501 :match (lambda (widget value) t)
|
|
3502 :value-to-internal 'widget-sexp-value-to-internal
|
|
3503 :value-to-external (lambda (widget value) (read value))
|
|
3504 :prompt-history 'widget-sexp-prompt-value-history
|
|
3505 :prompt-value 'widget-sexp-prompt-value)
|
|
3506
|
|
3507 (defun widget-sexp-value-to-internal (widget value)
|
|
3508 ;; Use cl-prettyprint for printer representation.
|
|
3509 (let ((pp (if (symbolp value)
|
|
3510 (prin1-to-string value)
|
|
3511 (widget-prettyprint-to-string value))))
|
|
3512 (if (> (length pp) 40)
|
|
3513 (concat "\n" pp)
|
|
3514 pp)))
|
|
3515
|
|
3516 (defun widget-sexp-validate (widget)
|
|
3517 ;; Valid if we can read the string and there is no junk left after it.
|
|
3518 (save-excursion
|
|
3519 (let ((buffer (set-buffer (get-buffer-create " *Widget Scratch*"))))
|
|
3520 (erase-buffer)
|
|
3521 (insert (widget-apply widget :value-get))
|
|
3522 (goto-char (point-min))
|
|
3523 (condition-case data
|
|
3524 (let ((value (read buffer)))
|
|
3525 (if (eobp)
|
|
3526 (if (widget-apply widget :match value)
|
|
3527 nil
|
|
3528 (widget-put widget :error (widget-get widget :type-error))
|
|
3529 widget)
|
|
3530 (widget-put widget
|
|
3531 :error (format "Junk at end of expression: %s"
|
|
3532 (buffer-substring (point)
|
|
3533 (point-max))))
|
|
3534 widget))
|
|
3535 (error (widget-put widget :error (error-message-string data))
|
|
3536 widget)))))
|
|
3537
|
|
3538 (defvar widget-sexp-prompt-value-history nil
|
|
3539 "History of input to `widget-sexp-prompt-value'.")
|
|
3540
|
|
3541 (defun widget-sexp-prompt-value (widget prompt value unbound)
|
|
3542 ;; Read an arbitrary sexp.
|
|
3543 (let ((found (read-string prompt
|
|
3544 (if unbound nil (cons (prin1-to-string value) 0))
|
|
3545 (widget-get widget :prompt-history))))
|
|
3546 (save-excursion
|
|
3547 (let ((buffer (set-buffer (get-buffer-create " *Widget Scratch*"))))
|
|
3548 (erase-buffer)
|
|
3549 (insert found)
|
|
3550 (goto-char (point-min))
|
|
3551 (let ((answer (read buffer)))
|
|
3552 (unless (eobp)
|
|
3553 (signal 'error
|
|
3554 (list "Junk at end of expression"
|
|
3555 (buffer-substring (point) (point-max)))))
|
|
3556 answer)))))
|
|
3557
|
|
3558 (define-widget 'restricted-sexp 'sexp
|
|
3559 "A Lisp expression restricted to values that match.
|
|
3560 To use this type, you must define :match or :match-alternatives."
|
|
3561 :type-error "The specified value is not valid"
|
|
3562 :match 'widget-restricted-sexp-match
|
|
3563 :value-to-internal (lambda (widget value)
|
|
3564 (if (widget-apply widget :match value)
|
|
3565 (prin1-to-string value)
|
|
3566 value)))
|
|
3567
|
|
3568 (defun widget-restricted-sexp-match (widget value)
|
|
3569 (let ((alternatives (widget-get widget :match-alternatives))
|
|
3570 matched)
|
|
3571 (while (and alternatives (not matched))
|
|
3572 (if (cond ((functionp (car alternatives))
|
|
3573 (funcall (car alternatives) value))
|
|
3574 ((and (consp (car alternatives))
|
|
3575 (eq (car (car alternatives)) 'quote))
|
|
3576 (eq value (nth 1 (car alternatives)))))
|
|
3577 (setq matched t))
|
|
3578 (setq alternatives (cdr alternatives)))
|
|
3579 matched))
|
|
3580
|
|
3581 (define-widget 'integer 'restricted-sexp
|
|
3582 "An integer."
|
|
3583 :tag "Integer"
|
|
3584 :value 0
|
|
3585 :type-error "This field should contain an integer"
|
|
3586 :match-alternatives '(integerp))
|
|
3587
|
|
3588 (define-widget 'number 'restricted-sexp
|
1173
|
3589 "A number (floating point or integer)."
|
428
|
3590 :tag "Number"
|
|
3591 :value 0.0
|
1173
|
3592 :type-error "This field should contain a number (floating point or integer)"
|
428
|
3593 :match-alternatives '(numberp))
|
|
3594
|
1309
|
3595 (define-widget 'float 'restricted-sexp
|
|
3596 "A floating point number."
|
|
3597 :tag "Floating point number"
|
|
3598 :value 0.0
|
|
3599 :type-error "This field should contain a floating point number"
|
|
3600 :match-alternatives '(floatp))
|
|
3601
|
428
|
3602 (define-widget 'character 'editable-field
|
|
3603 "A character."
|
|
3604 :tag "Character"
|
|
3605 :value ?\0
|
1309
|
3606 :size 1
|
428
|
3607 :format "%{%t%}: %v"
|
|
3608 :valid-regexp "\\`[\0-\377]\\'"
|
|
3609 :error "This field should contain a single character"
|
|
3610 :value-to-internal (lambda (widget value)
|
|
3611 (if (stringp value)
|
|
3612 value
|
|
3613 (char-to-string value)))
|
|
3614 :value-to-external (lambda (widget value)
|
|
3615 (if (stringp value)
|
|
3616 (aref value 0)
|
|
3617 value))
|
|
3618 :match (lambda (widget value)
|
|
3619 (characterp value)))
|
|
3620
|
|
3621 (define-widget 'list 'group
|
652
|
3622 "A Lisp list."
|
428
|
3623 :tag "List"
|
|
3624 :format "%{%t%}:\n%v")
|
|
3625
|
|
3626 (define-widget 'vector 'group
|
652
|
3627 "A Lisp vector."
|
428
|
3628 :tag "Vector"
|
|
3629 :format "%{%t%}:\n%v"
|
|
3630 :match 'widget-vector-match
|
|
3631 :value-to-internal (lambda (widget value) (append value nil))
|
|
3632 :value-to-external (lambda (widget value) (vconcat value)))
|
|
3633
|
|
3634 (defun widget-vector-match (widget value)
|
|
3635 (and (vectorp value)
|
|
3636 (widget-group-match widget
|
|
3637 (widget-apply widget :value-to-internal value))))
|
|
3638
|
|
3639 (define-widget 'cons 'group
|
|
3640 "A cons-cell."
|
|
3641 :tag "Cons-cell"
|
|
3642 :format "%{%t%}:\n%v"
|
|
3643 :match 'widget-cons-match
|
|
3644 :value-to-internal (lambda (widget value)
|
|
3645 (list (car value) (cdr value)))
|
|
3646 :value-to-external (lambda (widget value)
|
1309
|
3647 (cons (nth 0 value) (nth 1 value))))
|
428
|
3648
|
|
3649 (defun widget-cons-match (widget value)
|
|
3650 (and (consp value)
|
|
3651 (widget-group-match widget
|
|
3652 (widget-apply widget :value-to-internal value))))
|
1309
|
3653
|
|
3654 ;;; The `plist' Widget.
|
|
3655 ;;
|
|
3656 ;; Property lists.
|
|
3657
|
|
3658 (define-widget 'plist 'list
|
|
3659 "A property list."
|
|
3660 :key-type '(symbol :tag "Key")
|
|
3661 :value-type '(sexp :tag "Value")
|
|
3662 :convert-widget 'widget-plist-convert-widget
|
|
3663 :tag "Plist")
|
|
3664
|
|
3665 (defvar widget-plist-value-type) ;Dynamic variable
|
|
3666
|
|
3667 (defun widget-plist-convert-widget (widget)
|
|
3668 ;; Handle `:options'.
|
|
3669 (let* ((options (widget-get widget :options))
|
|
3670 (widget-plist-value-type (widget-get widget :value-type))
|
|
3671 (other `(editable-list :inline t
|
|
3672 (group :inline t
|
|
3673 ,(widget-get widget :key-type)
|
|
3674 ,widget-plist-value-type)))
|
|
3675 (args (if options
|
|
3676 (list `(checklist :inline t
|
|
3677 :greedy t
|
|
3678 ,@(mapcar 'widget-plist-convert-option
|
|
3679 options))
|
|
3680 other)
|
|
3681 (list other))))
|
|
3682 (widget-put widget :args args)
|
|
3683 widget))
|
|
3684
|
|
3685 (defun widget-plist-convert-option (option)
|
|
3686 ;; Convert a single plist option.
|
|
3687 (let (key-type value-type)
|
|
3688 (if (listp option)
|
|
3689 (let ((key (nth 0 option)))
|
|
3690 (setq value-type (nth 1 option))
|
|
3691 (if (listp key)
|
|
3692 (setq key-type key)
|
|
3693 (setq key-type `(const ,key))))
|
|
3694 (setq key-type `(const ,option)
|
|
3695 value-type widget-plist-value-type))
|
|
3696 `(group :format "Key: %v" :inline t ,key-type ,value-type)))
|
|
3697
|
|
3698
|
|
3699 ;;; The `alist' Widget.
|
|
3700 ;;
|
|
3701 ;; Association lists.
|
|
3702
|
|
3703 (define-widget 'alist 'list
|
|
3704 "An association list."
|
|
3705 :key-type '(sexp :tag "Key")
|
|
3706 :value-type '(sexp :tag "Value")
|
|
3707 :convert-widget 'widget-alist-convert-widget
|
|
3708 :tag "Alist")
|
|
3709
|
|
3710 (defvar widget-alist-value-type) ;Dynamic variable
|
|
3711
|
|
3712 (defun widget-alist-convert-widget (widget)
|
|
3713 ;; Handle `:options'.
|
|
3714 (let* ((options (widget-get widget :options))
|
|
3715 (widget-alist-value-type (widget-get widget :value-type))
|
|
3716 (other `(editable-list :inline t
|
|
3717 (cons :format "%v"
|
|
3718 ,(widget-get widget :key-type)
|
|
3719 ,widget-alist-value-type)))
|
|
3720 (args (if options
|
|
3721 (list `(checklist :inline t
|
|
3722 :greedy t
|
|
3723 ,@(mapcar 'widget-alist-convert-option
|
|
3724 options))
|
|
3725 other)
|
|
3726 (list other))))
|
|
3727 (widget-put widget :args args)
|
|
3728 widget))
|
|
3729
|
|
3730 (defun widget-alist-convert-option (option)
|
|
3731 ;; Convert a single alist option.
|
|
3732 (let (key-type value-type)
|
|
3733 (if (listp option)
|
|
3734 (let ((key (nth 0 option)))
|
|
3735 (setq value-type (nth 1 option))
|
|
3736 (if (listp key)
|
|
3737 (setq key-type key)
|
|
3738 (setq key-type `(const ,key))))
|
|
3739 (setq key-type `(const ,option)
|
|
3740 value-type widget-alist-value-type))
|
|
3741 `(cons :format "Key: %v" ,key-type ,value-type)))
|
|
3742
|
|
3743
|
428
|
3744 (define-widget 'choice 'menu-choice
|
|
3745 "A union of several sexp types."
|
|
3746 :tag "Choice"
|
|
3747 :format "%{%t%}: %[Value Menu%] %v"
|
|
3748 :button-prefix 'widget-push-button-prefix
|
|
3749 :button-suffix 'widget-push-button-suffix
|
|
3750 :prompt-value 'widget-choice-prompt-value)
|
|
3751
|
|
3752 (defun widget-choice-prompt-value (widget prompt value unbound)
|
|
3753 "Make a choice."
|
|
3754 (let ((args (widget-get widget :args))
|
|
3755 (completion-ignore-case (widget-get widget :case-fold))
|
|
3756 current choices old)
|
1173
|
3757 ;; Find the first arg that matches VALUE.
|
428
|
3758 (let ((look args))
|
|
3759 (while look
|
|
3760 (if (widget-apply (car look) :match value)
|
|
3761 (setq old (car look)
|
|
3762 look nil)
|
|
3763 (setq look (cdr look)))))
|
|
3764 ;; Find new choice.
|
|
3765 (setq current
|
|
3766 (cond ((= (length args) 0)
|
|
3767 nil)
|
|
3768 ((= (length args) 1)
|
|
3769 (nth 0 args))
|
|
3770 ((and (= (length args) 2)
|
|
3771 (memq old args))
|
|
3772 (if (eq old (nth 0 args))
|
|
3773 (nth 1 args)
|
|
3774 (nth 0 args)))
|
|
3775 (t
|
|
3776 (while args
|
|
3777 (setq current (car args)
|
|
3778 args (cdr args))
|
|
3779 (setq choices
|
|
3780 (cons (cons (widget-apply current :menu-tag-get)
|
|
3781 current)
|
|
3782 choices)))
|
|
3783 (let ((val (completing-read prompt choices nil t)))
|
|
3784 (if (stringp val)
|
|
3785 (let ((try (try-completion val choices)))
|
|
3786 (when (stringp try)
|
|
3787 (setq val try))
|
|
3788 (cdr (assoc val choices)))
|
|
3789 nil)))))
|
|
3790 (if current
|
|
3791 (widget-prompt-value current prompt nil t)
|
|
3792 value)))
|
|
3793
|
|
3794 (define-widget 'radio 'radio-button-choice
|
|
3795 "A union of several sexp types."
|
|
3796 :tag "Choice"
|
|
3797 :format "%{%t%}:\n%v"
|
|
3798 :prompt-value 'widget-choice-prompt-value)
|
|
3799
|
|
3800 (define-widget 'repeat 'editable-list
|
|
3801 "A variable length homogeneous list."
|
|
3802 :tag "Repeat"
|
|
3803 :format "%{%t%}:\n%v%i\n")
|
|
3804
|
|
3805 (define-widget 'set 'checklist
|
|
3806 "A list of members from a fixed set."
|
|
3807 :tag "Set"
|
|
3808 :format "%{%t%}:\n%v")
|
|
3809
|
|
3810 (define-widget 'boolean 'toggle
|
|
3811 "To be nil or non-nil, that is the question."
|
|
3812 :tag "Boolean"
|
|
3813 :prompt-value 'widget-boolean-prompt-value
|
|
3814 :button-prefix 'widget-push-button-prefix
|
|
3815 :button-suffix 'widget-push-button-suffix
|
|
3816 :format "%{%t%}: %[Toggle%] %v\n"
|
|
3817 :on "on (non-nil)"
|
|
3818 :off "off (nil)")
|
|
3819
|
|
3820 (defun widget-boolean-prompt-value (widget prompt value unbound)
|
|
3821 ;; Toggle a boolean.
|
|
3822 (y-or-n-p prompt))
|
|
3823
|
|
3824 ;;; The `color' Widget.
|
|
3825
|
1173
|
3826 ;; Fixme: match
|
428
|
3827 (define-widget 'color 'editable-field
|
|
3828 "Choose a color name (with sample)."
|
|
3829 :format "%[%t%]: %v (%{sample%})\n"
|
|
3830 :size 10
|
|
3831 :tag "Color"
|
|
3832 :value "black"
|
|
3833 :complete 'widget-color-complete
|
|
3834 :sample-face-get 'widget-color-sample-face-get
|
|
3835 :notify 'widget-color-notify
|
|
3836 :action 'widget-color-action)
|
|
3837
|
|
3838 (defun widget-color-complete (widget)
|
|
3839 "Complete the color in WIDGET."
|
|
3840 (let* ((prefix (buffer-substring-no-properties (widget-field-start widget)
|
|
3841 (point)))
|
|
3842 (list (read-color-completion-table))
|
|
3843 (completion (try-completion prefix list)))
|
|
3844 (cond ((eq completion t)
|
1173
|
3845 (message "Exact match."))
|
428
|
3846 ((null completion)
|
|
3847 (error "Can't find completion for \"%s\"" prefix))
|
|
3848 ((not (string-equal prefix completion))
|
|
3849 (insert (substring completion (length prefix))))
|
|
3850 (t
|
|
3851 (message "Making completion list...")
|
1309
|
3852 (with-output-to-temp-buffer "*Completions*"
|
|
3853 (display-completion-list (all-completions prefix list nil)))
|
428
|
3854 (message "Making completion list...done")))))
|
|
3855
|
|
3856 (defun widget-color-sample-face-get (widget)
|
|
3857 (or (widget-get widget :sample-face)
|
|
3858 (let ((color (widget-value widget))
|
|
3859 (face (make-face (gensym "sample-face-") nil t)))
|
|
3860 ;; Use the face object, not its name, to prevent lossage if gc
|
|
3861 ;; happens before applying the face.
|
|
3862 (widget-put widget :sample-face face)
|
|
3863 (and color
|
|
3864 (not (equal color ""))
|
|
3865 (valid-color-name-p color)
|
|
3866 (set-face-foreground face color))
|
|
3867 face)))
|
|
3868
|
|
3869 (defvar widget-color-history nil
|
|
3870 "History of entered colors.")
|
|
3871
|
|
3872 (defun widget-color-action (widget &optional event)
|
1173
|
3873 "Prompt for a color."
|
428
|
3874 (let* ((tag (widget-apply widget :menu-tag-get))
|
|
3875 (answer (read-color (concat tag ": "))))
|
|
3876 (unless (zerop (length answer))
|
|
3877 (widget-value-set widget answer)
|
|
3878 (widget-setup)
|
|
3879 (widget-apply widget :notify widget event))))
|
|
3880
|
|
3881 (defun widget-color-notify (widget child &optional event)
|
|
3882 "Update the sample, and notify the parent."
|
|
3883 (let* ((face (widget-apply widget :sample-face-get))
|
|
3884 (color (widget-value widget)))
|
|
3885 (if (valid-color-name-p color)
|
|
3886 (set-face-foreground face color)
|
|
3887 (remove-face-property face 'foreground)))
|
|
3888 (widget-default-notify widget child event))
|
|
3889
|
1173
|
3890 ;;; The Help Echo
|
|
3891
|
428
|
3892 (defun widget-echo-help (pos)
|
1173
|
3893 "Display the help-echo text for widget at POS."
|
428
|
3894 (let* ((widget (widget-at pos))
|
|
3895 (help-echo (and widget (widget-get widget :help-echo))))
|
1309
|
3896 (if (functionp help-echo)
|
|
3897 (setq help-echo (funcall help-echo widget)))
|
|
3898 (if (stringp help-echo)
|
|
3899 (display-message 'help-echo help-echo))))
|
428
|
3900
|
|
3901 ;;; The End:
|
|
3902
|
|
3903 (provide 'wid-edit)
|
|
3904
|
1173
|
3905 ;;; wid-edit.el ends here
|