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