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